thinkpad_acpi.c 231 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210
  1. /*
  2. * thinkpad_acpi.c - ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #define TPACPI_VERSION "0.24"
  24. #define TPACPI_SYSFS_VERSION 0x020700
  25. /*
  26. * Changelog:
  27. * 2007-10-20 changelog trimmed down
  28. *
  29. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  30. * drivers/misc.
  31. *
  32. * 2006-11-22 0.13 new maintainer
  33. * changelog now lives in git commit history, and will
  34. * not be updated further in-file.
  35. *
  36. * 2005-03-17 0.11 support for 600e, 770x
  37. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  38. *
  39. * 2005-01-16 0.9 use MODULE_VERSION
  40. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  41. * fix parameter passing on module loading
  42. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  43. * thanks to Jim Radford <radford@blackbean.org>
  44. * 2004-11-08 0.8 fix init error case, don't return from a macro
  45. * thanks to Chris Wright <chrisw@osdl.org>
  46. */
  47. #include <linux/kernel.h>
  48. #include <linux/module.h>
  49. #include <linux/init.h>
  50. #include <linux/types.h>
  51. #include <linux/string.h>
  52. #include <linux/list.h>
  53. #include <linux/mutex.h>
  54. #include <linux/sched.h>
  55. #include <linux/kthread.h>
  56. #include <linux/freezer.h>
  57. #include <linux/delay.h>
  58. #include <linux/slab.h>
  59. #include <linux/nvram.h>
  60. #include <linux/proc_fs.h>
  61. #include <linux/seq_file.h>
  62. #include <linux/sysfs.h>
  63. #include <linux/backlight.h>
  64. #include <linux/fb.h>
  65. #include <linux/platform_device.h>
  66. #include <linux/hwmon.h>
  67. #include <linux/hwmon-sysfs.h>
  68. #include <linux/input.h>
  69. #include <linux/leds.h>
  70. #include <linux/rfkill.h>
  71. #include <asm/uaccess.h>
  72. #include <linux/dmi.h>
  73. #include <linux/jiffies.h>
  74. #include <linux/workqueue.h>
  75. #include <sound/core.h>
  76. #include <sound/control.h>
  77. #include <sound/initval.h>
  78. #include <acpi/acpi_drivers.h>
  79. #include <linux/pci_ids.h>
  80. /* ThinkPad CMOS commands */
  81. #define TP_CMOS_VOLUME_DOWN 0
  82. #define TP_CMOS_VOLUME_UP 1
  83. #define TP_CMOS_VOLUME_MUTE 2
  84. #define TP_CMOS_BRIGHTNESS_UP 4
  85. #define TP_CMOS_BRIGHTNESS_DOWN 5
  86. #define TP_CMOS_THINKLIGHT_ON 12
  87. #define TP_CMOS_THINKLIGHT_OFF 13
  88. /* NVRAM Addresses */
  89. enum tp_nvram_addr {
  90. TP_NVRAM_ADDR_HK2 = 0x57,
  91. TP_NVRAM_ADDR_THINKLIGHT = 0x58,
  92. TP_NVRAM_ADDR_VIDEO = 0x59,
  93. TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
  94. TP_NVRAM_ADDR_MIXER = 0x60,
  95. };
  96. /* NVRAM bit masks */
  97. enum {
  98. TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
  99. TP_NVRAM_MASK_HKT_ZOOM = 0x20,
  100. TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
  101. TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
  102. TP_NVRAM_MASK_THINKLIGHT = 0x10,
  103. TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
  104. TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
  105. TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
  106. TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
  107. TP_NVRAM_MASK_MUTE = 0x40,
  108. TP_NVRAM_MASK_HKT_VOLUME = 0x80,
  109. TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
  110. TP_NVRAM_POS_LEVEL_VOLUME = 0,
  111. };
  112. /* Misc NVRAM-related */
  113. enum {
  114. TP_NVRAM_LEVEL_VOLUME_MAX = 14,
  115. };
  116. /* ACPI HIDs */
  117. #define TPACPI_ACPI_HKEY_HID "IBM0068"
  118. #define TPACPI_ACPI_EC_HID "PNP0C09"
  119. /* Input IDs */
  120. #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
  121. #define TPACPI_HKEY_INPUT_VERSION 0x4101
  122. /* ACPI \WGSV commands */
  123. enum {
  124. TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
  125. TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
  126. TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
  127. TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
  128. };
  129. /* TP_ACPI_WGSV_GET_STATE bits */
  130. enum {
  131. TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
  132. TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
  133. TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
  134. TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
  135. TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
  136. TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
  137. TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
  138. TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
  139. TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
  140. TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
  141. };
  142. /* HKEY events */
  143. enum tpacpi_hkey_event_t {
  144. /* Hotkey-related */
  145. TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
  146. TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
  147. TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
  148. TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
  149. TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
  150. TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
  151. /* Reasons for waking up from S3/S4 */
  152. TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
  153. TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
  154. TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
  155. TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
  156. TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
  157. TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
  158. /* Auto-sleep after eject request */
  159. TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
  160. TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
  161. /* Misc bay events */
  162. TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
  163. /* User-interface events */
  164. TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
  165. TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
  166. TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
  167. TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
  168. TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
  169. TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
  170. TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
  171. /* Thermal events */
  172. TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
  173. TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
  174. TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
  175. TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
  176. TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
  177. /* Misc */
  178. TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
  179. };
  180. /****************************************************************************
  181. * Main driver
  182. */
  183. #define TPACPI_NAME "thinkpad"
  184. #define TPACPI_DESC "ThinkPad ACPI Extras"
  185. #define TPACPI_FILE TPACPI_NAME "_acpi"
  186. #define TPACPI_URL "http://ibm-acpi.sf.net/"
  187. #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
  188. #define TPACPI_PROC_DIR "ibm"
  189. #define TPACPI_ACPI_EVENT_PREFIX "ibm"
  190. #define TPACPI_DRVR_NAME TPACPI_FILE
  191. #define TPACPI_DRVR_SHORTNAME "tpacpi"
  192. #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
  193. #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
  194. #define TPACPI_WORKQUEUE_NAME "ktpacpid"
  195. #define TPACPI_MAX_ACPI_ARGS 3
  196. /* printk headers */
  197. #define TPACPI_LOG TPACPI_FILE ": "
  198. #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
  199. #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
  200. #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
  201. #define TPACPI_ERR KERN_ERR TPACPI_LOG
  202. #define TPACPI_WARN KERN_WARNING TPACPI_LOG
  203. #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
  204. #define TPACPI_INFO KERN_INFO TPACPI_LOG
  205. #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
  206. /* Debugging printk groups */
  207. #define TPACPI_DBG_ALL 0xffff
  208. #define TPACPI_DBG_DISCLOSETASK 0x8000
  209. #define TPACPI_DBG_INIT 0x0001
  210. #define TPACPI_DBG_EXIT 0x0002
  211. #define TPACPI_DBG_RFKILL 0x0004
  212. #define TPACPI_DBG_HKEY 0x0008
  213. #define TPACPI_DBG_FAN 0x0010
  214. #define TPACPI_DBG_BRGHT 0x0020
  215. #define TPACPI_DBG_MIXER 0x0040
  216. #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
  217. #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  218. #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
  219. /****************************************************************************
  220. * Driver-wide structs and misc. variables
  221. */
  222. struct ibm_struct;
  223. struct tp_acpi_drv_struct {
  224. const struct acpi_device_id *hid;
  225. struct acpi_driver *driver;
  226. void (*notify) (struct ibm_struct *, u32);
  227. acpi_handle *handle;
  228. u32 type;
  229. struct acpi_device *device;
  230. };
  231. struct ibm_struct {
  232. char *name;
  233. int (*read) (struct seq_file *);
  234. int (*write) (char *);
  235. void (*exit) (void);
  236. void (*resume) (void);
  237. void (*suspend) (pm_message_t state);
  238. void (*shutdown) (void);
  239. struct list_head all_drivers;
  240. struct tp_acpi_drv_struct *acpi;
  241. struct {
  242. u8 acpi_driver_registered:1;
  243. u8 acpi_notify_installed:1;
  244. u8 proc_created:1;
  245. u8 init_called:1;
  246. u8 experimental:1;
  247. } flags;
  248. };
  249. struct ibm_init_struct {
  250. char param[32];
  251. int (*init) (struct ibm_init_struct *);
  252. mode_t base_procfs_mode;
  253. struct ibm_struct *data;
  254. };
  255. static struct {
  256. u32 bluetooth:1;
  257. u32 hotkey:1;
  258. u32 hotkey_mask:1;
  259. u32 hotkey_wlsw:1;
  260. u32 hotkey_tablet:1;
  261. u32 light:1;
  262. u32 light_status:1;
  263. u32 bright_acpimode:1;
  264. u32 bright_unkfw:1;
  265. u32 wan:1;
  266. u32 uwb:1;
  267. u32 fan_ctrl_status_undef:1;
  268. u32 second_fan:1;
  269. u32 beep_needs_two_args:1;
  270. u32 mixer_no_level_control:1;
  271. u32 input_device_registered:1;
  272. u32 platform_drv_registered:1;
  273. u32 platform_drv_attrs_registered:1;
  274. u32 sensors_pdrv_registered:1;
  275. u32 sensors_pdrv_attrs_registered:1;
  276. u32 sensors_pdev_attrs_registered:1;
  277. u32 hotkey_poll_active:1;
  278. } tp_features;
  279. static struct {
  280. u16 hotkey_mask_ff:1;
  281. u16 volume_ctrl_forbidden:1;
  282. } tp_warned;
  283. struct thinkpad_id_data {
  284. unsigned int vendor; /* ThinkPad vendor:
  285. * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
  286. char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
  287. char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
  288. u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
  289. u16 ec_model;
  290. u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
  291. u16 ec_release;
  292. char *model_str; /* ThinkPad T43 */
  293. char *nummodel_str; /* 9384A9C for a 9384-A9C model */
  294. };
  295. static struct thinkpad_id_data thinkpad_id;
  296. static enum {
  297. TPACPI_LIFE_INIT = 0,
  298. TPACPI_LIFE_RUNNING,
  299. TPACPI_LIFE_EXITING,
  300. } tpacpi_lifecycle;
  301. static int experimental;
  302. static u32 dbg_level;
  303. static struct workqueue_struct *tpacpi_wq;
  304. enum led_status_t {
  305. TPACPI_LED_OFF = 0,
  306. TPACPI_LED_ON,
  307. TPACPI_LED_BLINK,
  308. };
  309. /* Special LED class that can defer work */
  310. struct tpacpi_led_classdev {
  311. struct led_classdev led_classdev;
  312. struct work_struct work;
  313. enum led_status_t new_state;
  314. unsigned int led;
  315. };
  316. /* brightness level capabilities */
  317. static unsigned int bright_maxlvl; /* 0 = unknown */
  318. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  319. static int dbg_wlswemul;
  320. static int tpacpi_wlsw_emulstate;
  321. static int dbg_bluetoothemul;
  322. static int tpacpi_bluetooth_emulstate;
  323. static int dbg_wwanemul;
  324. static int tpacpi_wwan_emulstate;
  325. static int dbg_uwbemul;
  326. static int tpacpi_uwb_emulstate;
  327. #endif
  328. /*************************************************************************
  329. * Debugging helpers
  330. */
  331. #define dbg_printk(a_dbg_level, format, arg...) \
  332. do { if (dbg_level & (a_dbg_level)) \
  333. printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
  334. } while (0)
  335. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  336. #define vdbg_printk dbg_printk
  337. static const char *str_supported(int is_supported);
  338. #else
  339. #define vdbg_printk(a_dbg_level, format, arg...) \
  340. do { } while (0)
  341. #endif
  342. static void tpacpi_log_usertask(const char * const what)
  343. {
  344. printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
  345. what, task_tgid_vnr(current));
  346. }
  347. #define tpacpi_disclose_usertask(what, format, arg...) \
  348. do { \
  349. if (unlikely( \
  350. (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
  351. (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
  352. printk(TPACPI_DEBUG "%s: PID %d: " format, \
  353. what, task_tgid_vnr(current), ## arg); \
  354. } \
  355. } while (0)
  356. /*
  357. * Quirk handling helpers
  358. *
  359. * ThinkPad IDs and versions seen in the field so far
  360. * are two-characters from the set [0-9A-Z], i.e. base 36.
  361. *
  362. * We use values well outside that range as specials.
  363. */
  364. #define TPACPI_MATCH_ANY 0xffffU
  365. #define TPACPI_MATCH_UNKNOWN 0U
  366. /* TPID('1', 'Y') == 0x5931 */
  367. #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
  368. #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
  369. { .vendor = PCI_VENDOR_ID_IBM, \
  370. .bios = TPID(__id1, __id2), \
  371. .ec = TPACPI_MATCH_ANY, \
  372. .quirks = (__quirk) }
  373. #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
  374. { .vendor = PCI_VENDOR_ID_LENOVO, \
  375. .bios = TPID(__id1, __id2), \
  376. .ec = TPACPI_MATCH_ANY, \
  377. .quirks = (__quirk) }
  378. #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
  379. { .vendor = PCI_VENDOR_ID_LENOVO, \
  380. .bios = TPACPI_MATCH_ANY, \
  381. .ec = TPID(__id1, __id2), \
  382. .quirks = (__quirk) }
  383. struct tpacpi_quirk {
  384. unsigned int vendor;
  385. u16 bios;
  386. u16 ec;
  387. unsigned long quirks;
  388. };
  389. /**
  390. * tpacpi_check_quirks() - search BIOS/EC version on a list
  391. * @qlist: array of &struct tpacpi_quirk
  392. * @qlist_size: number of elements in @qlist
  393. *
  394. * Iterates over a quirks list until one is found that matches the
  395. * ThinkPad's vendor, BIOS and EC model.
  396. *
  397. * Returns 0 if nothing matches, otherwise returns the quirks field of
  398. * the matching &struct tpacpi_quirk entry.
  399. *
  400. * The match criteria is: vendor, ec and bios much match.
  401. */
  402. static unsigned long __init tpacpi_check_quirks(
  403. const struct tpacpi_quirk *qlist,
  404. unsigned int qlist_size)
  405. {
  406. while (qlist_size) {
  407. if ((qlist->vendor == thinkpad_id.vendor ||
  408. qlist->vendor == TPACPI_MATCH_ANY) &&
  409. (qlist->bios == thinkpad_id.bios_model ||
  410. qlist->bios == TPACPI_MATCH_ANY) &&
  411. (qlist->ec == thinkpad_id.ec_model ||
  412. qlist->ec == TPACPI_MATCH_ANY))
  413. return qlist->quirks;
  414. qlist_size--;
  415. qlist++;
  416. }
  417. return 0;
  418. }
  419. static inline bool __pure __init tpacpi_is_lenovo(void)
  420. {
  421. return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
  422. }
  423. static inline bool __pure __init tpacpi_is_ibm(void)
  424. {
  425. return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
  426. }
  427. /****************************************************************************
  428. ****************************************************************************
  429. *
  430. * ACPI Helpers and device model
  431. *
  432. ****************************************************************************
  433. ****************************************************************************/
  434. /*************************************************************************
  435. * ACPI basic handles
  436. */
  437. static acpi_handle root_handle;
  438. static acpi_handle ec_handle;
  439. #define TPACPI_HANDLE(object, parent, paths...) \
  440. static acpi_handle object##_handle; \
  441. static const acpi_handle *object##_parent __initdata = \
  442. &parent##_handle; \
  443. static char *object##_paths[] __initdata = { paths }
  444. TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  445. TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  446. TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
  447. /* T4x, X31, X40 */
  448. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  449. "\\CMS", /* R40, R40e */
  450. ); /* all others */
  451. TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  452. "^HKEY", /* R30, R31 */
  453. "HKEY", /* all others */
  454. ); /* 570 */
  455. TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  456. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  457. "\\_SB.PCI0.VID0", /* 770e */
  458. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  459. "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
  460. "\\_SB.PCI0.AGP.VID", /* all others */
  461. ); /* R30, R31 */
  462. /*************************************************************************
  463. * ACPI helpers
  464. */
  465. static int acpi_evalf(acpi_handle handle,
  466. void *res, char *method, char *fmt, ...)
  467. {
  468. char *fmt0 = fmt;
  469. struct acpi_object_list params;
  470. union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
  471. struct acpi_buffer result, *resultp;
  472. union acpi_object out_obj;
  473. acpi_status status;
  474. va_list ap;
  475. char res_type;
  476. int success;
  477. int quiet;
  478. if (!*fmt) {
  479. printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
  480. return 0;
  481. }
  482. if (*fmt == 'q') {
  483. quiet = 1;
  484. fmt++;
  485. } else
  486. quiet = 0;
  487. res_type = *(fmt++);
  488. params.count = 0;
  489. params.pointer = &in_objs[0];
  490. va_start(ap, fmt);
  491. while (*fmt) {
  492. char c = *(fmt++);
  493. switch (c) {
  494. case 'd': /* int */
  495. in_objs[params.count].integer.value = va_arg(ap, int);
  496. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  497. break;
  498. /* add more types as needed */
  499. default:
  500. printk(TPACPI_ERR "acpi_evalf() called "
  501. "with invalid format character '%c'\n", c);
  502. va_end(ap);
  503. return 0;
  504. }
  505. }
  506. va_end(ap);
  507. if (res_type != 'v') {
  508. result.length = sizeof(out_obj);
  509. result.pointer = &out_obj;
  510. resultp = &result;
  511. } else
  512. resultp = NULL;
  513. status = acpi_evaluate_object(handle, method, &params, resultp);
  514. switch (res_type) {
  515. case 'd': /* int */
  516. success = (status == AE_OK &&
  517. out_obj.type == ACPI_TYPE_INTEGER);
  518. if (success && res)
  519. *(int *)res = out_obj.integer.value;
  520. break;
  521. case 'v': /* void */
  522. success = status == AE_OK;
  523. break;
  524. /* add more types as needed */
  525. default:
  526. printk(TPACPI_ERR "acpi_evalf() called "
  527. "with invalid format character '%c'\n", res_type);
  528. return 0;
  529. }
  530. if (!success && !quiet)
  531. printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
  532. method, fmt0, acpi_format_exception(status));
  533. return success;
  534. }
  535. static int acpi_ec_read(int i, u8 *p)
  536. {
  537. int v;
  538. if (ecrd_handle) {
  539. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  540. return 0;
  541. *p = v;
  542. } else {
  543. if (ec_read(i, p) < 0)
  544. return 0;
  545. }
  546. return 1;
  547. }
  548. static int acpi_ec_write(int i, u8 v)
  549. {
  550. if (ecwr_handle) {
  551. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  552. return 0;
  553. } else {
  554. if (ec_write(i, v) < 0)
  555. return 0;
  556. }
  557. return 1;
  558. }
  559. static int issue_thinkpad_cmos_command(int cmos_cmd)
  560. {
  561. if (!cmos_handle)
  562. return -ENXIO;
  563. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  564. return -EIO;
  565. return 0;
  566. }
  567. /*************************************************************************
  568. * ACPI device model
  569. */
  570. #define TPACPI_ACPIHANDLE_INIT(object) \
  571. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  572. object##_paths, ARRAY_SIZE(object##_paths))
  573. static void __init drv_acpi_handle_init(const char *name,
  574. acpi_handle *handle, const acpi_handle parent,
  575. char **paths, const int num_paths)
  576. {
  577. int i;
  578. acpi_status status;
  579. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  580. name);
  581. for (i = 0; i < num_paths; i++) {
  582. status = acpi_get_handle(parent, paths[i], handle);
  583. if (ACPI_SUCCESS(status)) {
  584. dbg_printk(TPACPI_DBG_INIT,
  585. "Found ACPI handle %s for %s\n",
  586. paths[i], name);
  587. return;
  588. }
  589. }
  590. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  591. name);
  592. *handle = NULL;
  593. }
  594. static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
  595. u32 level, void *context, void **return_value)
  596. {
  597. *(acpi_handle *)return_value = handle;
  598. return AE_CTRL_TERMINATE;
  599. }
  600. static void __init tpacpi_acpi_handle_locate(const char *name,
  601. const char *hid,
  602. acpi_handle *handle)
  603. {
  604. acpi_status status;
  605. acpi_handle device_found;
  606. BUG_ON(!name || !hid || !handle);
  607. vdbg_printk(TPACPI_DBG_INIT,
  608. "trying to locate ACPI handle for %s, using HID %s\n",
  609. name, hid);
  610. memset(&device_found, 0, sizeof(device_found));
  611. status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
  612. (void *)name, &device_found);
  613. *handle = NULL;
  614. if (ACPI_SUCCESS(status)) {
  615. *handle = device_found;
  616. dbg_printk(TPACPI_DBG_INIT,
  617. "Found ACPI handle for %s\n", name);
  618. } else {
  619. vdbg_printk(TPACPI_DBG_INIT,
  620. "Could not locate an ACPI handle for %s: %s\n",
  621. name, acpi_format_exception(status));
  622. }
  623. }
  624. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  625. {
  626. struct ibm_struct *ibm = data;
  627. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  628. return;
  629. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  630. return;
  631. ibm->acpi->notify(ibm, event);
  632. }
  633. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  634. {
  635. acpi_status status;
  636. int rc;
  637. BUG_ON(!ibm->acpi);
  638. if (!*ibm->acpi->handle)
  639. return 0;
  640. vdbg_printk(TPACPI_DBG_INIT,
  641. "setting up ACPI notify for %s\n", ibm->name);
  642. rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  643. if (rc < 0) {
  644. printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
  645. ibm->name, rc);
  646. return -ENODEV;
  647. }
  648. ibm->acpi->device->driver_data = ibm;
  649. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  650. TPACPI_ACPI_EVENT_PREFIX,
  651. ibm->name);
  652. status = acpi_install_notify_handler(*ibm->acpi->handle,
  653. ibm->acpi->type, dispatch_acpi_notify, ibm);
  654. if (ACPI_FAILURE(status)) {
  655. if (status == AE_ALREADY_EXISTS) {
  656. printk(TPACPI_NOTICE
  657. "another device driver is already "
  658. "handling %s events\n", ibm->name);
  659. } else {
  660. printk(TPACPI_ERR
  661. "acpi_install_notify_handler(%s) failed: %s\n",
  662. ibm->name, acpi_format_exception(status));
  663. }
  664. return -ENODEV;
  665. }
  666. ibm->flags.acpi_notify_installed = 1;
  667. return 0;
  668. }
  669. static int __init tpacpi_device_add(struct acpi_device *device)
  670. {
  671. return 0;
  672. }
  673. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  674. {
  675. int rc;
  676. dbg_printk(TPACPI_DBG_INIT,
  677. "registering %s as an ACPI driver\n", ibm->name);
  678. BUG_ON(!ibm->acpi);
  679. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  680. if (!ibm->acpi->driver) {
  681. printk(TPACPI_ERR
  682. "failed to allocate memory for ibm->acpi->driver\n");
  683. return -ENOMEM;
  684. }
  685. sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
  686. ibm->acpi->driver->ids = ibm->acpi->hid;
  687. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  688. rc = acpi_bus_register_driver(ibm->acpi->driver);
  689. if (rc < 0) {
  690. printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  691. ibm->name, rc);
  692. kfree(ibm->acpi->driver);
  693. ibm->acpi->driver = NULL;
  694. } else if (!rc)
  695. ibm->flags.acpi_driver_registered = 1;
  696. return rc;
  697. }
  698. /****************************************************************************
  699. ****************************************************************************
  700. *
  701. * Procfs Helpers
  702. *
  703. ****************************************************************************
  704. ****************************************************************************/
  705. static int dispatch_proc_show(struct seq_file *m, void *v)
  706. {
  707. struct ibm_struct *ibm = m->private;
  708. if (!ibm || !ibm->read)
  709. return -EINVAL;
  710. return ibm->read(m);
  711. }
  712. static int dispatch_proc_open(struct inode *inode, struct file *file)
  713. {
  714. return single_open(file, dispatch_proc_show, PDE(inode)->data);
  715. }
  716. static ssize_t dispatch_proc_write(struct file *file,
  717. const char __user *userbuf,
  718. size_t count, loff_t *pos)
  719. {
  720. struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
  721. char *kernbuf;
  722. int ret;
  723. if (!ibm || !ibm->write)
  724. return -EINVAL;
  725. if (count > PAGE_SIZE - 2)
  726. return -EINVAL;
  727. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  728. if (!kernbuf)
  729. return -ENOMEM;
  730. if (copy_from_user(kernbuf, userbuf, count)) {
  731. kfree(kernbuf);
  732. return -EFAULT;
  733. }
  734. kernbuf[count] = 0;
  735. strcat(kernbuf, ",");
  736. ret = ibm->write(kernbuf);
  737. if (ret == 0)
  738. ret = count;
  739. kfree(kernbuf);
  740. return ret;
  741. }
  742. static const struct file_operations dispatch_proc_fops = {
  743. .owner = THIS_MODULE,
  744. .open = dispatch_proc_open,
  745. .read = seq_read,
  746. .llseek = seq_lseek,
  747. .release = single_release,
  748. .write = dispatch_proc_write,
  749. };
  750. static char *next_cmd(char **cmds)
  751. {
  752. char *start = *cmds;
  753. char *end;
  754. while ((end = strchr(start, ',')) && end == start)
  755. start = end + 1;
  756. if (!end)
  757. return NULL;
  758. *end = 0;
  759. *cmds = end + 1;
  760. return start;
  761. }
  762. /****************************************************************************
  763. ****************************************************************************
  764. *
  765. * Device model: input, hwmon and platform
  766. *
  767. ****************************************************************************
  768. ****************************************************************************/
  769. static struct platform_device *tpacpi_pdev;
  770. static struct platform_device *tpacpi_sensors_pdev;
  771. static struct device *tpacpi_hwmon;
  772. static struct input_dev *tpacpi_inputdev;
  773. static struct mutex tpacpi_inputdev_send_mutex;
  774. static LIST_HEAD(tpacpi_all_drivers);
  775. static int tpacpi_suspend_handler(struct platform_device *pdev,
  776. pm_message_t state)
  777. {
  778. struct ibm_struct *ibm, *itmp;
  779. list_for_each_entry_safe(ibm, itmp,
  780. &tpacpi_all_drivers,
  781. all_drivers) {
  782. if (ibm->suspend)
  783. (ibm->suspend)(state);
  784. }
  785. return 0;
  786. }
  787. static int tpacpi_resume_handler(struct platform_device *pdev)
  788. {
  789. struct ibm_struct *ibm, *itmp;
  790. list_for_each_entry_safe(ibm, itmp,
  791. &tpacpi_all_drivers,
  792. all_drivers) {
  793. if (ibm->resume)
  794. (ibm->resume)();
  795. }
  796. return 0;
  797. }
  798. static void tpacpi_shutdown_handler(struct platform_device *pdev)
  799. {
  800. struct ibm_struct *ibm, *itmp;
  801. list_for_each_entry_safe(ibm, itmp,
  802. &tpacpi_all_drivers,
  803. all_drivers) {
  804. if (ibm->shutdown)
  805. (ibm->shutdown)();
  806. }
  807. }
  808. static struct platform_driver tpacpi_pdriver = {
  809. .driver = {
  810. .name = TPACPI_DRVR_NAME,
  811. .owner = THIS_MODULE,
  812. },
  813. .suspend = tpacpi_suspend_handler,
  814. .resume = tpacpi_resume_handler,
  815. .shutdown = tpacpi_shutdown_handler,
  816. };
  817. static struct platform_driver tpacpi_hwmon_pdriver = {
  818. .driver = {
  819. .name = TPACPI_HWMON_DRVR_NAME,
  820. .owner = THIS_MODULE,
  821. },
  822. };
  823. /*************************************************************************
  824. * sysfs support helpers
  825. */
  826. struct attribute_set {
  827. unsigned int members, max_members;
  828. struct attribute_group group;
  829. };
  830. struct attribute_set_obj {
  831. struct attribute_set s;
  832. struct attribute *a;
  833. } __attribute__((packed));
  834. static struct attribute_set *create_attr_set(unsigned int max_members,
  835. const char *name)
  836. {
  837. struct attribute_set_obj *sobj;
  838. if (max_members == 0)
  839. return NULL;
  840. /* Allocates space for implicit NULL at the end too */
  841. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  842. max_members * sizeof(struct attribute *),
  843. GFP_KERNEL);
  844. if (!sobj)
  845. return NULL;
  846. sobj->s.max_members = max_members;
  847. sobj->s.group.attrs = &sobj->a;
  848. sobj->s.group.name = name;
  849. return &sobj->s;
  850. }
  851. #define destroy_attr_set(_set) \
  852. kfree(_set);
  853. /* not multi-threaded safe, use it in a single thread per set */
  854. static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
  855. {
  856. if (!s || !attr)
  857. return -EINVAL;
  858. if (s->members >= s->max_members)
  859. return -ENOMEM;
  860. s->group.attrs[s->members] = attr;
  861. s->members++;
  862. return 0;
  863. }
  864. static int add_many_to_attr_set(struct attribute_set *s,
  865. struct attribute **attr,
  866. unsigned int count)
  867. {
  868. int i, res;
  869. for (i = 0; i < count; i++) {
  870. res = add_to_attr_set(s, attr[i]);
  871. if (res)
  872. return res;
  873. }
  874. return 0;
  875. }
  876. static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
  877. {
  878. sysfs_remove_group(kobj, &s->group);
  879. destroy_attr_set(s);
  880. }
  881. #define register_attr_set_with_sysfs(_attr_set, _kobj) \
  882. sysfs_create_group(_kobj, &_attr_set->group)
  883. static int parse_strtoul(const char *buf,
  884. unsigned long max, unsigned long *value)
  885. {
  886. char *endp;
  887. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  888. endp = skip_spaces(endp);
  889. if (*endp || *value > max)
  890. return -EINVAL;
  891. return 0;
  892. }
  893. static void tpacpi_disable_brightness_delay(void)
  894. {
  895. if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
  896. printk(TPACPI_NOTICE
  897. "ACPI backlight control delay disabled\n");
  898. }
  899. static void printk_deprecated_attribute(const char * const what,
  900. const char * const details)
  901. {
  902. tpacpi_log_usertask("deprecated sysfs attribute");
  903. printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
  904. "will be removed. %s\n",
  905. what, details);
  906. }
  907. /*************************************************************************
  908. * rfkill and radio control support helpers
  909. */
  910. /*
  911. * ThinkPad-ACPI firmware handling model:
  912. *
  913. * WLSW (master wireless switch) is event-driven, and is common to all
  914. * firmware-controlled radios. It cannot be controlled, just monitored,
  915. * as expected. It overrides all radio state in firmware
  916. *
  917. * The kernel, a masked-off hotkey, and WLSW can change the radio state
  918. * (TODO: verify how WLSW interacts with the returned radio state).
  919. *
  920. * The only time there are shadow radio state changes, is when
  921. * masked-off hotkeys are used.
  922. */
  923. /*
  924. * Internal driver API for radio state:
  925. *
  926. * int: < 0 = error, otherwise enum tpacpi_rfkill_state
  927. * bool: true means radio blocked (off)
  928. */
  929. enum tpacpi_rfkill_state {
  930. TPACPI_RFK_RADIO_OFF = 0,
  931. TPACPI_RFK_RADIO_ON
  932. };
  933. /* rfkill switches */
  934. enum tpacpi_rfk_id {
  935. TPACPI_RFK_BLUETOOTH_SW_ID = 0,
  936. TPACPI_RFK_WWAN_SW_ID,
  937. TPACPI_RFK_UWB_SW_ID,
  938. TPACPI_RFK_SW_MAX
  939. };
  940. static const char *tpacpi_rfkill_names[] = {
  941. [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
  942. [TPACPI_RFK_WWAN_SW_ID] = "wwan",
  943. [TPACPI_RFK_UWB_SW_ID] = "uwb",
  944. [TPACPI_RFK_SW_MAX] = NULL
  945. };
  946. /* ThinkPad-ACPI rfkill subdriver */
  947. struct tpacpi_rfk {
  948. struct rfkill *rfkill;
  949. enum tpacpi_rfk_id id;
  950. const struct tpacpi_rfk_ops *ops;
  951. };
  952. struct tpacpi_rfk_ops {
  953. /* firmware interface */
  954. int (*get_status)(void);
  955. int (*set_status)(const enum tpacpi_rfkill_state);
  956. };
  957. static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
  958. /* Query FW and update rfkill sw state for a given rfkill switch */
  959. static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
  960. {
  961. int status;
  962. if (!tp_rfk)
  963. return -ENODEV;
  964. status = (tp_rfk->ops->get_status)();
  965. if (status < 0)
  966. return status;
  967. rfkill_set_sw_state(tp_rfk->rfkill,
  968. (status == TPACPI_RFK_RADIO_OFF));
  969. return status;
  970. }
  971. /* Query FW and update rfkill sw state for all rfkill switches */
  972. static void tpacpi_rfk_update_swstate_all(void)
  973. {
  974. unsigned int i;
  975. for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
  976. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
  977. }
  978. /*
  979. * Sync the HW-blocking state of all rfkill switches,
  980. * do notice it causes the rfkill core to schedule uevents
  981. */
  982. static void tpacpi_rfk_update_hwblock_state(bool blocked)
  983. {
  984. unsigned int i;
  985. struct tpacpi_rfk *tp_rfk;
  986. for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
  987. tp_rfk = tpacpi_rfkill_switches[i];
  988. if (tp_rfk) {
  989. if (rfkill_set_hw_state(tp_rfk->rfkill,
  990. blocked)) {
  991. /* ignore -- we track sw block */
  992. }
  993. }
  994. }
  995. }
  996. /* Call to get the WLSW state from the firmware */
  997. static int hotkey_get_wlsw(void);
  998. /* Call to query WLSW state and update all rfkill switches */
  999. static bool tpacpi_rfk_check_hwblock_state(void)
  1000. {
  1001. int res = hotkey_get_wlsw();
  1002. int hw_blocked;
  1003. /* When unknown or unsupported, we have to assume it is unblocked */
  1004. if (res < 0)
  1005. return false;
  1006. hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
  1007. tpacpi_rfk_update_hwblock_state(hw_blocked);
  1008. return hw_blocked;
  1009. }
  1010. static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
  1011. {
  1012. struct tpacpi_rfk *tp_rfk = data;
  1013. int res;
  1014. dbg_printk(TPACPI_DBG_RFKILL,
  1015. "request to change radio state to %s\n",
  1016. blocked ? "blocked" : "unblocked");
  1017. /* try to set radio state */
  1018. res = (tp_rfk->ops->set_status)(blocked ?
  1019. TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
  1020. /* and update the rfkill core with whatever the FW really did */
  1021. tpacpi_rfk_update_swstate(tp_rfk);
  1022. return (res < 0) ? res : 0;
  1023. }
  1024. static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
  1025. .set_block = tpacpi_rfk_hook_set_block,
  1026. };
  1027. static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
  1028. const struct tpacpi_rfk_ops *tp_rfkops,
  1029. const enum rfkill_type rfktype,
  1030. const char *name,
  1031. const bool set_default)
  1032. {
  1033. struct tpacpi_rfk *atp_rfk;
  1034. int res;
  1035. bool sw_state = false;
  1036. bool hw_state;
  1037. int sw_status;
  1038. BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
  1039. atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
  1040. if (atp_rfk)
  1041. atp_rfk->rfkill = rfkill_alloc(name,
  1042. &tpacpi_pdev->dev,
  1043. rfktype,
  1044. &tpacpi_rfk_rfkill_ops,
  1045. atp_rfk);
  1046. if (!atp_rfk || !atp_rfk->rfkill) {
  1047. printk(TPACPI_ERR
  1048. "failed to allocate memory for rfkill class\n");
  1049. kfree(atp_rfk);
  1050. return -ENOMEM;
  1051. }
  1052. atp_rfk->id = id;
  1053. atp_rfk->ops = tp_rfkops;
  1054. sw_status = (tp_rfkops->get_status)();
  1055. if (sw_status < 0) {
  1056. printk(TPACPI_ERR
  1057. "failed to read initial state for %s, error %d\n",
  1058. name, sw_status);
  1059. } else {
  1060. sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
  1061. if (set_default) {
  1062. /* try to keep the initial state, since we ask the
  1063. * firmware to preserve it across S5 in NVRAM */
  1064. rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
  1065. }
  1066. }
  1067. hw_state = tpacpi_rfk_check_hwblock_state();
  1068. rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
  1069. res = rfkill_register(atp_rfk->rfkill);
  1070. if (res < 0) {
  1071. printk(TPACPI_ERR
  1072. "failed to register %s rfkill switch: %d\n",
  1073. name, res);
  1074. rfkill_destroy(atp_rfk->rfkill);
  1075. kfree(atp_rfk);
  1076. return res;
  1077. }
  1078. tpacpi_rfkill_switches[id] = atp_rfk;
  1079. printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
  1080. name, (sw_state || hw_state) ? "" : "un");
  1081. return 0;
  1082. }
  1083. static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
  1084. {
  1085. struct tpacpi_rfk *tp_rfk;
  1086. BUG_ON(id >= TPACPI_RFK_SW_MAX);
  1087. tp_rfk = tpacpi_rfkill_switches[id];
  1088. if (tp_rfk) {
  1089. rfkill_unregister(tp_rfk->rfkill);
  1090. rfkill_destroy(tp_rfk->rfkill);
  1091. tpacpi_rfkill_switches[id] = NULL;
  1092. kfree(tp_rfk);
  1093. }
  1094. }
  1095. static void printk_deprecated_rfkill_attribute(const char * const what)
  1096. {
  1097. printk_deprecated_attribute(what,
  1098. "Please switch to generic rfkill before year 2010");
  1099. }
  1100. /* sysfs <radio> enable ------------------------------------------------ */
  1101. static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
  1102. struct device_attribute *attr,
  1103. char *buf)
  1104. {
  1105. int status;
  1106. printk_deprecated_rfkill_attribute(attr->attr.name);
  1107. /* This is in the ABI... */
  1108. if (tpacpi_rfk_check_hwblock_state()) {
  1109. status = TPACPI_RFK_RADIO_OFF;
  1110. } else {
  1111. status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1112. if (status < 0)
  1113. return status;
  1114. }
  1115. return snprintf(buf, PAGE_SIZE, "%d\n",
  1116. (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
  1117. }
  1118. static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
  1119. struct device_attribute *attr,
  1120. const char *buf, size_t count)
  1121. {
  1122. unsigned long t;
  1123. int res;
  1124. printk_deprecated_rfkill_attribute(attr->attr.name);
  1125. if (parse_strtoul(buf, 1, &t))
  1126. return -EINVAL;
  1127. tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
  1128. /* This is in the ABI... */
  1129. if (tpacpi_rfk_check_hwblock_state() && !!t)
  1130. return -EPERM;
  1131. res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
  1132. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
  1133. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1134. return (res < 0) ? res : count;
  1135. }
  1136. /* procfs -------------------------------------------------------------- */
  1137. static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
  1138. {
  1139. if (id >= TPACPI_RFK_SW_MAX)
  1140. seq_printf(m, "status:\t\tnot supported\n");
  1141. else {
  1142. int status;
  1143. /* This is in the ABI... */
  1144. if (tpacpi_rfk_check_hwblock_state()) {
  1145. status = TPACPI_RFK_RADIO_OFF;
  1146. } else {
  1147. status = tpacpi_rfk_update_swstate(
  1148. tpacpi_rfkill_switches[id]);
  1149. if (status < 0)
  1150. return status;
  1151. }
  1152. seq_printf(m, "status:\t\t%s\n",
  1153. (status == TPACPI_RFK_RADIO_ON) ?
  1154. "enabled" : "disabled");
  1155. seq_printf(m, "commands:\tenable, disable\n");
  1156. }
  1157. return 0;
  1158. }
  1159. static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
  1160. {
  1161. char *cmd;
  1162. int status = -1;
  1163. int res = 0;
  1164. if (id >= TPACPI_RFK_SW_MAX)
  1165. return -ENODEV;
  1166. while ((cmd = next_cmd(&buf))) {
  1167. if (strlencmp(cmd, "enable") == 0)
  1168. status = TPACPI_RFK_RADIO_ON;
  1169. else if (strlencmp(cmd, "disable") == 0)
  1170. status = TPACPI_RFK_RADIO_OFF;
  1171. else
  1172. return -EINVAL;
  1173. }
  1174. if (status != -1) {
  1175. tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
  1176. (status == TPACPI_RFK_RADIO_ON) ?
  1177. "enable" : "disable",
  1178. tpacpi_rfkill_names[id]);
  1179. res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
  1180. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1181. }
  1182. return res;
  1183. }
  1184. /*************************************************************************
  1185. * thinkpad-acpi driver attributes
  1186. */
  1187. /* interface_version --------------------------------------------------- */
  1188. static ssize_t tpacpi_driver_interface_version_show(
  1189. struct device_driver *drv,
  1190. char *buf)
  1191. {
  1192. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  1193. }
  1194. static DRIVER_ATTR(interface_version, S_IRUGO,
  1195. tpacpi_driver_interface_version_show, NULL);
  1196. /* debug_level --------------------------------------------------------- */
  1197. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  1198. char *buf)
  1199. {
  1200. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  1201. }
  1202. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  1203. const char *buf, size_t count)
  1204. {
  1205. unsigned long t;
  1206. if (parse_strtoul(buf, 0xffff, &t))
  1207. return -EINVAL;
  1208. dbg_level = t;
  1209. return count;
  1210. }
  1211. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  1212. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  1213. /* version ------------------------------------------------------------- */
  1214. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  1215. char *buf)
  1216. {
  1217. return snprintf(buf, PAGE_SIZE, "%s v%s\n",
  1218. TPACPI_DESC, TPACPI_VERSION);
  1219. }
  1220. static DRIVER_ATTR(version, S_IRUGO,
  1221. tpacpi_driver_version_show, NULL);
  1222. /* --------------------------------------------------------------------- */
  1223. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1224. /* wlsw_emulstate ------------------------------------------------------ */
  1225. static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
  1226. char *buf)
  1227. {
  1228. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
  1229. }
  1230. static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
  1231. const char *buf, size_t count)
  1232. {
  1233. unsigned long t;
  1234. if (parse_strtoul(buf, 1, &t))
  1235. return -EINVAL;
  1236. if (tpacpi_wlsw_emulstate != !!t) {
  1237. tpacpi_wlsw_emulstate = !!t;
  1238. tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
  1239. }
  1240. return count;
  1241. }
  1242. static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
  1243. tpacpi_driver_wlsw_emulstate_show,
  1244. tpacpi_driver_wlsw_emulstate_store);
  1245. /* bluetooth_emulstate ------------------------------------------------- */
  1246. static ssize_t tpacpi_driver_bluetooth_emulstate_show(
  1247. struct device_driver *drv,
  1248. char *buf)
  1249. {
  1250. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
  1251. }
  1252. static ssize_t tpacpi_driver_bluetooth_emulstate_store(
  1253. struct device_driver *drv,
  1254. const char *buf, size_t count)
  1255. {
  1256. unsigned long t;
  1257. if (parse_strtoul(buf, 1, &t))
  1258. return -EINVAL;
  1259. tpacpi_bluetooth_emulstate = !!t;
  1260. return count;
  1261. }
  1262. static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
  1263. tpacpi_driver_bluetooth_emulstate_show,
  1264. tpacpi_driver_bluetooth_emulstate_store);
  1265. /* wwan_emulstate ------------------------------------------------- */
  1266. static ssize_t tpacpi_driver_wwan_emulstate_show(
  1267. struct device_driver *drv,
  1268. char *buf)
  1269. {
  1270. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
  1271. }
  1272. static ssize_t tpacpi_driver_wwan_emulstate_store(
  1273. struct device_driver *drv,
  1274. const char *buf, size_t count)
  1275. {
  1276. unsigned long t;
  1277. if (parse_strtoul(buf, 1, &t))
  1278. return -EINVAL;
  1279. tpacpi_wwan_emulstate = !!t;
  1280. return count;
  1281. }
  1282. static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
  1283. tpacpi_driver_wwan_emulstate_show,
  1284. tpacpi_driver_wwan_emulstate_store);
  1285. /* uwb_emulstate ------------------------------------------------- */
  1286. static ssize_t tpacpi_driver_uwb_emulstate_show(
  1287. struct device_driver *drv,
  1288. char *buf)
  1289. {
  1290. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
  1291. }
  1292. static ssize_t tpacpi_driver_uwb_emulstate_store(
  1293. struct device_driver *drv,
  1294. const char *buf, size_t count)
  1295. {
  1296. unsigned long t;
  1297. if (parse_strtoul(buf, 1, &t))
  1298. return -EINVAL;
  1299. tpacpi_uwb_emulstate = !!t;
  1300. return count;
  1301. }
  1302. static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
  1303. tpacpi_driver_uwb_emulstate_show,
  1304. tpacpi_driver_uwb_emulstate_store);
  1305. #endif
  1306. /* --------------------------------------------------------------------- */
  1307. static struct driver_attribute *tpacpi_driver_attributes[] = {
  1308. &driver_attr_debug_level, &driver_attr_version,
  1309. &driver_attr_interface_version,
  1310. };
  1311. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  1312. {
  1313. int i, res;
  1314. i = 0;
  1315. res = 0;
  1316. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  1317. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  1318. i++;
  1319. }
  1320. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1321. if (!res && dbg_wlswemul)
  1322. res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
  1323. if (!res && dbg_bluetoothemul)
  1324. res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
  1325. if (!res && dbg_wwanemul)
  1326. res = driver_create_file(drv, &driver_attr_wwan_emulstate);
  1327. if (!res && dbg_uwbemul)
  1328. res = driver_create_file(drv, &driver_attr_uwb_emulstate);
  1329. #endif
  1330. return res;
  1331. }
  1332. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  1333. {
  1334. int i;
  1335. for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  1336. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  1337. #ifdef THINKPAD_ACPI_DEBUGFACILITIES
  1338. driver_remove_file(drv, &driver_attr_wlsw_emulstate);
  1339. driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
  1340. driver_remove_file(drv, &driver_attr_wwan_emulstate);
  1341. driver_remove_file(drv, &driver_attr_uwb_emulstate);
  1342. #endif
  1343. }
  1344. /*************************************************************************
  1345. * Firmware Data
  1346. */
  1347. /*
  1348. * Table of recommended minimum BIOS versions
  1349. *
  1350. * Reasons for listing:
  1351. * 1. Stable BIOS, listed because the unknown amount of
  1352. * bugs and bad ACPI behaviour on older versions
  1353. *
  1354. * 2. BIOS or EC fw with known bugs that trigger on Linux
  1355. *
  1356. * 3. BIOS with known reduced functionality in older versions
  1357. *
  1358. * We recommend the latest BIOS and EC version.
  1359. * We only support the latest BIOS and EC fw version as a rule.
  1360. *
  1361. * Sources: IBM ThinkPad Public Web Documents (update changelogs),
  1362. * Information from users in ThinkWiki
  1363. *
  1364. * WARNING: we use this table also to detect that the machine is
  1365. * a ThinkPad in some cases, so don't remove entries lightly.
  1366. */
  1367. #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
  1368. { .vendor = (__v), \
  1369. .bios = TPID(__id1, __id2), \
  1370. .ec = TPACPI_MATCH_ANY, \
  1371. .quirks = TPACPI_MATCH_ANY << 16 \
  1372. | (__bv1) << 8 | (__bv2) }
  1373. #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
  1374. __eid, __ev1, __ev2) \
  1375. { .vendor = (__v), \
  1376. .bios = TPID(__bid1, __bid2), \
  1377. .ec = __eid, \
  1378. .quirks = (__ev1) << 24 | (__ev2) << 16 \
  1379. | (__bv1) << 8 | (__bv2) }
  1380. #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
  1381. TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
  1382. /* Outdated IBM BIOSes often lack the EC id string */
  1383. #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1384. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1385. __bv1, __bv2, TPID(__id1, __id2), \
  1386. __ev1, __ev2), \
  1387. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1388. __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
  1389. __ev1, __ev2)
  1390. /* Outdated IBM BIOSes often lack the EC id string */
  1391. #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
  1392. __eid1, __eid2, __ev1, __ev2) \
  1393. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1394. __bv1, __bv2, TPID(__eid1, __eid2), \
  1395. __ev1, __ev2), \
  1396. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1397. __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
  1398. __ev1, __ev2)
  1399. #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
  1400. TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
  1401. #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1402. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
  1403. __bv1, __bv2, TPID(__id1, __id2), \
  1404. __ev1, __ev2)
  1405. #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
  1406. __eid1, __eid2, __ev1, __ev2) \
  1407. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
  1408. __bv1, __bv2, TPID(__eid1, __eid2), \
  1409. __ev1, __ev2)
  1410. static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
  1411. /* Numeric models ------------------ */
  1412. /* FW MODEL BIOS VERS */
  1413. TPV_QI0('I', 'M', '6', '5'), /* 570 */
  1414. TPV_QI0('I', 'U', '2', '6'), /* 570E */
  1415. TPV_QI0('I', 'B', '5', '4'), /* 600 */
  1416. TPV_QI0('I', 'H', '4', '7'), /* 600E */
  1417. TPV_QI0('I', 'N', '3', '6'), /* 600E */
  1418. TPV_QI0('I', 'T', '5', '5'), /* 600X */
  1419. TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
  1420. TPV_QI0('I', 'I', '4', '2'), /* 770X */
  1421. TPV_QI0('I', 'O', '2', '3'), /* 770Z */
  1422. /* A-series ------------------------- */
  1423. /* FW MODEL BIOS VERS EC VERS */
  1424. TPV_QI0('I', 'W', '5', '9'), /* A20m */
  1425. TPV_QI0('I', 'V', '6', '9'), /* A20p */
  1426. TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
  1427. TPV_QI0('K', 'U', '3', '6'), /* A21e */
  1428. TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
  1429. TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
  1430. TPV_QI0('1', 'B', '1', '7'), /* A22e */
  1431. TPV_QI0('1', '3', '2', '0'), /* A22m */
  1432. TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
  1433. TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
  1434. TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
  1435. /* G-series ------------------------- */
  1436. /* FW MODEL BIOS VERS */
  1437. TPV_QI0('1', 'T', 'A', '6'), /* G40 */
  1438. TPV_QI0('1', 'X', '5', '7'), /* G41 */
  1439. /* R-series, T-series --------------- */
  1440. /* FW MODEL BIOS VERS EC VERS */
  1441. TPV_QI0('1', 'C', 'F', '0'), /* R30 */
  1442. TPV_QI0('1', 'F', 'F', '1'), /* R31 */
  1443. TPV_QI0('1', 'M', '9', '7'), /* R32 */
  1444. TPV_QI0('1', 'O', '6', '1'), /* R40 */
  1445. TPV_QI0('1', 'P', '6', '5'), /* R40 */
  1446. TPV_QI0('1', 'S', '7', '0'), /* R40e */
  1447. TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
  1448. T40/p, T41/p, T42/p (1) */
  1449. TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
  1450. TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
  1451. TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
  1452. TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
  1453. TPV_QI0('I', 'Y', '6', '1'), /* T20 */
  1454. TPV_QI0('K', 'Z', '3', '4'), /* T21 */
  1455. TPV_QI0('1', '6', '3', '2'), /* T22 */
  1456. TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
  1457. TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
  1458. TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
  1459. TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
  1460. TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
  1461. TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
  1462. /* BIOS FW BIOS VERS EC FW EC VERS */
  1463. TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
  1464. TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
  1465. /* X-series ------------------------- */
  1466. /* FW MODEL BIOS VERS EC VERS */
  1467. TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
  1468. TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
  1469. TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
  1470. TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
  1471. TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
  1472. TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
  1473. TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
  1474. TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
  1475. TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
  1476. /* (0) - older versions lack DMI EC fw string and functionality */
  1477. /* (1) - older versions known to lack functionality */
  1478. };
  1479. #undef TPV_QL1
  1480. #undef TPV_QL0
  1481. #undef TPV_QI2
  1482. #undef TPV_QI1
  1483. #undef TPV_QI0
  1484. #undef TPV_Q_X
  1485. #undef TPV_Q
  1486. static void __init tpacpi_check_outdated_fw(void)
  1487. {
  1488. unsigned long fwvers;
  1489. u16 ec_version, bios_version;
  1490. fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1491. ARRAY_SIZE(tpacpi_bios_version_qtable));
  1492. if (!fwvers)
  1493. return;
  1494. bios_version = fwvers & 0xffffU;
  1495. ec_version = (fwvers >> 16) & 0xffffU;
  1496. /* note that unknown versions are set to 0x0000 and we use that */
  1497. if ((bios_version > thinkpad_id.bios_release) ||
  1498. (ec_version > thinkpad_id.ec_release &&
  1499. ec_version != TPACPI_MATCH_ANY)) {
  1500. /*
  1501. * The changelogs would let us track down the exact
  1502. * reason, but it is just too much of a pain to track
  1503. * it. We only list BIOSes that are either really
  1504. * broken, or really stable to begin with, so it is
  1505. * best if the user upgrades the firmware anyway.
  1506. */
  1507. printk(TPACPI_WARN
  1508. "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
  1509. printk(TPACPI_WARN
  1510. "WARNING: This firmware may be missing critical bug "
  1511. "fixes and/or important features\n");
  1512. }
  1513. }
  1514. static bool __init tpacpi_is_fw_known(void)
  1515. {
  1516. return tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1517. ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
  1518. }
  1519. /****************************************************************************
  1520. ****************************************************************************
  1521. *
  1522. * Subdrivers
  1523. *
  1524. ****************************************************************************
  1525. ****************************************************************************/
  1526. /*************************************************************************
  1527. * thinkpad-acpi metadata subdriver
  1528. */
  1529. static int thinkpad_acpi_driver_read(struct seq_file *m)
  1530. {
  1531. seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
  1532. seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
  1533. return 0;
  1534. }
  1535. static struct ibm_struct thinkpad_acpi_driver_data = {
  1536. .name = "driver",
  1537. .read = thinkpad_acpi_driver_read,
  1538. };
  1539. /*************************************************************************
  1540. * Hotkey subdriver
  1541. */
  1542. /*
  1543. * ThinkPad firmware event model
  1544. *
  1545. * The ThinkPad firmware has two main event interfaces: normal ACPI
  1546. * notifications (which follow the ACPI standard), and a private event
  1547. * interface.
  1548. *
  1549. * The private event interface also issues events for the hotkeys. As
  1550. * the driver gained features, the event handling code ended up being
  1551. * built around the hotkey subdriver. This will need to be refactored
  1552. * to a more formal event API eventually.
  1553. *
  1554. * Some "hotkeys" are actually supposed to be used as event reports,
  1555. * such as "brightness has changed", "volume has changed", depending on
  1556. * the ThinkPad model and how the firmware is operating.
  1557. *
  1558. * Unlike other classes, hotkey-class events have mask/unmask control on
  1559. * non-ancient firmware. However, how it behaves changes a lot with the
  1560. * firmware model and version.
  1561. */
  1562. enum { /* hot key scan codes (derived from ACPI DSDT) */
  1563. TP_ACPI_HOTKEYSCAN_FNF1 = 0,
  1564. TP_ACPI_HOTKEYSCAN_FNF2,
  1565. TP_ACPI_HOTKEYSCAN_FNF3,
  1566. TP_ACPI_HOTKEYSCAN_FNF4,
  1567. TP_ACPI_HOTKEYSCAN_FNF5,
  1568. TP_ACPI_HOTKEYSCAN_FNF6,
  1569. TP_ACPI_HOTKEYSCAN_FNF7,
  1570. TP_ACPI_HOTKEYSCAN_FNF8,
  1571. TP_ACPI_HOTKEYSCAN_FNF9,
  1572. TP_ACPI_HOTKEYSCAN_FNF10,
  1573. TP_ACPI_HOTKEYSCAN_FNF11,
  1574. TP_ACPI_HOTKEYSCAN_FNF12,
  1575. TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
  1576. TP_ACPI_HOTKEYSCAN_FNINSERT,
  1577. TP_ACPI_HOTKEYSCAN_FNDELETE,
  1578. TP_ACPI_HOTKEYSCAN_FNHOME,
  1579. TP_ACPI_HOTKEYSCAN_FNEND,
  1580. TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1581. TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
  1582. TP_ACPI_HOTKEYSCAN_FNSPACE,
  1583. TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1584. TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1585. TP_ACPI_HOTKEYSCAN_MUTE,
  1586. TP_ACPI_HOTKEYSCAN_THINKPAD,
  1587. TP_ACPI_HOTKEYSCAN_UNK1,
  1588. TP_ACPI_HOTKEYSCAN_UNK2,
  1589. TP_ACPI_HOTKEYSCAN_UNK3,
  1590. TP_ACPI_HOTKEYSCAN_UNK4,
  1591. TP_ACPI_HOTKEYSCAN_UNK5,
  1592. TP_ACPI_HOTKEYSCAN_UNK6,
  1593. TP_ACPI_HOTKEYSCAN_UNK7,
  1594. TP_ACPI_HOTKEYSCAN_UNK8,
  1595. /* Hotkey keymap size */
  1596. TPACPI_HOTKEY_MAP_LEN
  1597. };
  1598. enum { /* Keys/events available through NVRAM polling */
  1599. TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
  1600. TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
  1601. };
  1602. enum { /* Positions of some of the keys in hotkey masks */
  1603. TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
  1604. TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
  1605. TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
  1606. TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
  1607. TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
  1608. TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1609. TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
  1610. TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1611. TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1612. TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
  1613. TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
  1614. };
  1615. enum { /* NVRAM to ACPI HKEY group map */
  1616. TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
  1617. TP_ACPI_HKEY_ZOOM_MASK |
  1618. TP_ACPI_HKEY_DISPSWTCH_MASK |
  1619. TP_ACPI_HKEY_HIBERNATE_MASK,
  1620. TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
  1621. TP_ACPI_HKEY_BRGHTDWN_MASK,
  1622. TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
  1623. TP_ACPI_HKEY_VOLDWN_MASK |
  1624. TP_ACPI_HKEY_MUTE_MASK,
  1625. };
  1626. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1627. struct tp_nvram_state {
  1628. u16 thinkpad_toggle:1;
  1629. u16 zoom_toggle:1;
  1630. u16 display_toggle:1;
  1631. u16 thinklight_toggle:1;
  1632. u16 hibernate_toggle:1;
  1633. u16 displayexp_toggle:1;
  1634. u16 display_state:1;
  1635. u16 brightness_toggle:1;
  1636. u16 volume_toggle:1;
  1637. u16 mute:1;
  1638. u8 brightness_level;
  1639. u8 volume_level;
  1640. };
  1641. /* kthread for the hotkey poller */
  1642. static struct task_struct *tpacpi_hotkey_task;
  1643. /* Acquired while the poller kthread is running, use to sync start/stop */
  1644. static struct mutex hotkey_thread_mutex;
  1645. /*
  1646. * Acquire mutex to write poller control variables as an
  1647. * atomic block.
  1648. *
  1649. * Increment hotkey_config_change when changing them if you
  1650. * want the kthread to forget old state.
  1651. *
  1652. * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1653. */
  1654. static struct mutex hotkey_thread_data_mutex;
  1655. static unsigned int hotkey_config_change;
  1656. /*
  1657. * hotkey poller control variables
  1658. *
  1659. * Must be atomic or readers will also need to acquire mutex
  1660. *
  1661. * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1662. * should be used only when the changes need to be taken as
  1663. * a block, OR when one needs to force the kthread to forget
  1664. * old state.
  1665. */
  1666. static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
  1667. static unsigned int hotkey_poll_freq = 10; /* Hz */
  1668. #define HOTKEY_CONFIG_CRITICAL_START \
  1669. do { \
  1670. mutex_lock(&hotkey_thread_data_mutex); \
  1671. hotkey_config_change++; \
  1672. } while (0);
  1673. #define HOTKEY_CONFIG_CRITICAL_END \
  1674. mutex_unlock(&hotkey_thread_data_mutex);
  1675. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1676. #define hotkey_source_mask 0U
  1677. #define HOTKEY_CONFIG_CRITICAL_START
  1678. #define HOTKEY_CONFIG_CRITICAL_END
  1679. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1680. static struct mutex hotkey_mutex;
  1681. static enum { /* Reasons for waking up */
  1682. TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
  1683. TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
  1684. TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
  1685. } hotkey_wakeup_reason;
  1686. static int hotkey_autosleep_ack;
  1687. static u32 hotkey_orig_mask; /* events the BIOS had enabled */
  1688. static u32 hotkey_all_mask; /* all events supported in fw */
  1689. static u32 hotkey_reserved_mask; /* events better left disabled */
  1690. static u32 hotkey_driver_mask; /* events needed by the driver */
  1691. static u32 hotkey_user_mask; /* events visible to userspace */
  1692. static u32 hotkey_acpi_mask; /* events enabled in firmware */
  1693. static unsigned int hotkey_report_mode;
  1694. static u16 *hotkey_keycode_map;
  1695. static struct attribute_set *hotkey_dev_attributes;
  1696. static void tpacpi_driver_event(const unsigned int hkey_event);
  1697. static void hotkey_driver_event(const unsigned int scancode);
  1698. static void hotkey_poll_setup(const bool may_warn);
  1699. /* HKEY.MHKG() return bits */
  1700. #define TP_HOTKEY_TABLET_MASK (1 << 3)
  1701. static int hotkey_get_wlsw(void)
  1702. {
  1703. int status;
  1704. if (!tp_features.hotkey_wlsw)
  1705. return -ENODEV;
  1706. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1707. if (dbg_wlswemul)
  1708. return (tpacpi_wlsw_emulstate) ?
  1709. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1710. #endif
  1711. if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
  1712. return -EIO;
  1713. return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1714. }
  1715. static int hotkey_get_tablet_mode(int *status)
  1716. {
  1717. int s;
  1718. if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
  1719. return -EIO;
  1720. *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
  1721. return 0;
  1722. }
  1723. /*
  1724. * Reads current event mask from firmware, and updates
  1725. * hotkey_acpi_mask accordingly. Also resets any bits
  1726. * from hotkey_user_mask that are unavailable to be
  1727. * delivered (shadow requirement of the userspace ABI).
  1728. *
  1729. * Call with hotkey_mutex held
  1730. */
  1731. static int hotkey_mask_get(void)
  1732. {
  1733. if (tp_features.hotkey_mask) {
  1734. u32 m = 0;
  1735. if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
  1736. return -EIO;
  1737. hotkey_acpi_mask = m;
  1738. } else {
  1739. /* no mask support doesn't mean no event support... */
  1740. hotkey_acpi_mask = hotkey_all_mask;
  1741. }
  1742. /* sync userspace-visible mask */
  1743. hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
  1744. return 0;
  1745. }
  1746. void static hotkey_mask_warn_incomplete_mask(void)
  1747. {
  1748. /* log only what the user can fix... */
  1749. const u32 wantedmask = hotkey_driver_mask &
  1750. ~(hotkey_acpi_mask | hotkey_source_mask) &
  1751. (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
  1752. if (wantedmask)
  1753. printk(TPACPI_NOTICE
  1754. "required events 0x%08x not enabled!\n",
  1755. wantedmask);
  1756. }
  1757. /*
  1758. * Set the firmware mask when supported
  1759. *
  1760. * Also calls hotkey_mask_get to update hotkey_acpi_mask.
  1761. *
  1762. * NOTE: does not set bits in hotkey_user_mask, but may reset them.
  1763. *
  1764. * Call with hotkey_mutex held
  1765. */
  1766. static int hotkey_mask_set(u32 mask)
  1767. {
  1768. int i;
  1769. int rc = 0;
  1770. const u32 fwmask = mask & ~hotkey_source_mask;
  1771. if (tp_features.hotkey_mask) {
  1772. for (i = 0; i < 32; i++) {
  1773. if (!acpi_evalf(hkey_handle,
  1774. NULL, "MHKM", "vdd", i + 1,
  1775. !!(mask & (1 << i)))) {
  1776. rc = -EIO;
  1777. break;
  1778. }
  1779. }
  1780. }
  1781. /*
  1782. * We *must* make an inconditional call to hotkey_mask_get to
  1783. * refresh hotkey_acpi_mask and update hotkey_user_mask
  1784. *
  1785. * Take the opportunity to also log when we cannot _enable_
  1786. * a given event.
  1787. */
  1788. if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
  1789. printk(TPACPI_NOTICE
  1790. "asked for hotkey mask 0x%08x, but "
  1791. "firmware forced it to 0x%08x\n",
  1792. fwmask, hotkey_acpi_mask);
  1793. }
  1794. if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
  1795. hotkey_mask_warn_incomplete_mask();
  1796. return rc;
  1797. }
  1798. /*
  1799. * Sets hotkey_user_mask and tries to set the firmware mask
  1800. *
  1801. * Call with hotkey_mutex held
  1802. */
  1803. static int hotkey_user_mask_set(const u32 mask)
  1804. {
  1805. int rc;
  1806. /* Give people a chance to notice they are doing something that
  1807. * is bound to go boom on their users sooner or later */
  1808. if (!tp_warned.hotkey_mask_ff &&
  1809. (mask == 0xffff || mask == 0xffffff ||
  1810. mask == 0xffffffff)) {
  1811. tp_warned.hotkey_mask_ff = 1;
  1812. printk(TPACPI_NOTICE
  1813. "setting the hotkey mask to 0x%08x is likely "
  1814. "not the best way to go about it\n", mask);
  1815. printk(TPACPI_NOTICE
  1816. "please consider using the driver defaults, "
  1817. "and refer to up-to-date thinkpad-acpi "
  1818. "documentation\n");
  1819. }
  1820. /* Try to enable what the user asked for, plus whatever we need.
  1821. * this syncs everything but won't enable bits in hotkey_user_mask */
  1822. rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
  1823. /* Enable the available bits in hotkey_user_mask */
  1824. hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
  1825. return rc;
  1826. }
  1827. /*
  1828. * Sets the driver hotkey mask.
  1829. *
  1830. * Can be called even if the hotkey subdriver is inactive
  1831. */
  1832. static int tpacpi_hotkey_driver_mask_set(const u32 mask)
  1833. {
  1834. int rc;
  1835. /* Do the right thing if hotkey_init has not been called yet */
  1836. if (!tp_features.hotkey) {
  1837. hotkey_driver_mask = mask;
  1838. return 0;
  1839. }
  1840. mutex_lock(&hotkey_mutex);
  1841. HOTKEY_CONFIG_CRITICAL_START
  1842. hotkey_driver_mask = mask;
  1843. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1844. hotkey_source_mask |= (mask & ~hotkey_all_mask);
  1845. #endif
  1846. HOTKEY_CONFIG_CRITICAL_END
  1847. rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
  1848. ~hotkey_source_mask);
  1849. hotkey_poll_setup(true);
  1850. mutex_unlock(&hotkey_mutex);
  1851. return rc;
  1852. }
  1853. static int hotkey_status_get(int *status)
  1854. {
  1855. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  1856. return -EIO;
  1857. return 0;
  1858. }
  1859. static int hotkey_status_set(bool enable)
  1860. {
  1861. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
  1862. return -EIO;
  1863. return 0;
  1864. }
  1865. static void tpacpi_input_send_tabletsw(void)
  1866. {
  1867. int state;
  1868. if (tp_features.hotkey_tablet &&
  1869. !hotkey_get_tablet_mode(&state)) {
  1870. mutex_lock(&tpacpi_inputdev_send_mutex);
  1871. input_report_switch(tpacpi_inputdev,
  1872. SW_TABLET_MODE, !!state);
  1873. input_sync(tpacpi_inputdev);
  1874. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1875. }
  1876. }
  1877. /* Do NOT call without validating scancode first */
  1878. static void tpacpi_input_send_key(const unsigned int scancode)
  1879. {
  1880. const unsigned int keycode = hotkey_keycode_map[scancode];
  1881. if (keycode != KEY_RESERVED) {
  1882. mutex_lock(&tpacpi_inputdev_send_mutex);
  1883. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
  1884. input_report_key(tpacpi_inputdev, keycode, 1);
  1885. input_sync(tpacpi_inputdev);
  1886. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
  1887. input_report_key(tpacpi_inputdev, keycode, 0);
  1888. input_sync(tpacpi_inputdev);
  1889. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1890. }
  1891. }
  1892. /* Do NOT call without validating scancode first */
  1893. static void tpacpi_input_send_key_masked(const unsigned int scancode)
  1894. {
  1895. hotkey_driver_event(scancode);
  1896. if (hotkey_user_mask & (1 << scancode))
  1897. tpacpi_input_send_key(scancode);
  1898. }
  1899. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1900. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1901. /* Do NOT call without validating scancode first */
  1902. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1903. {
  1904. tpacpi_input_send_key_masked(scancode);
  1905. if (hotkey_report_mode < 2) {
  1906. acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
  1907. 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
  1908. }
  1909. }
  1910. static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
  1911. {
  1912. u8 d;
  1913. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1914. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1915. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1916. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1917. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1918. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1919. }
  1920. if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
  1921. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1922. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1923. }
  1924. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1925. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1926. n->displayexp_toggle =
  1927. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1928. }
  1929. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1930. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1931. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1932. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1933. n->brightness_toggle =
  1934. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1935. }
  1936. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1937. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1938. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1939. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1940. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1941. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1942. }
  1943. }
  1944. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  1945. struct tp_nvram_state *newn,
  1946. const u32 event_mask)
  1947. {
  1948. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1949. do { \
  1950. if ((event_mask & (1 << __scancode)) && \
  1951. oldn->__member != newn->__member) \
  1952. tpacpi_hotkey_send_key(__scancode); \
  1953. } while (0)
  1954. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1955. do { \
  1956. if (event_mask & (1 << __scancode)) \
  1957. tpacpi_hotkey_send_key(__scancode); \
  1958. } while (0)
  1959. void issue_volchange(const unsigned int oldvol,
  1960. const unsigned int newvol)
  1961. {
  1962. unsigned int i = oldvol;
  1963. while (i > newvol) {
  1964. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1965. i--;
  1966. }
  1967. while (i < newvol) {
  1968. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1969. i++;
  1970. }
  1971. }
  1972. void issue_brightnesschange(const unsigned int oldbrt,
  1973. const unsigned int newbrt)
  1974. {
  1975. unsigned int i = oldbrt;
  1976. while (i > newbrt) {
  1977. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1978. i--;
  1979. }
  1980. while (i < newbrt) {
  1981. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1982. i++;
  1983. }
  1984. }
  1985. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  1986. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  1987. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  1988. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  1989. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  1990. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  1991. /*
  1992. * Handle volume
  1993. *
  1994. * This code is supposed to duplicate the IBM firmware behaviour:
  1995. * - Pressing MUTE issues mute hotkey message, even when already mute
  1996. * - Pressing Volume up/down issues volume up/down hotkey messages,
  1997. * even when already at maximum or minumum volume
  1998. * - The act of unmuting issues volume up/down notification,
  1999. * depending which key was used to unmute
  2000. *
  2001. * We are constrained to what the NVRAM can tell us, which is not much
  2002. * and certainly not enough if more than one volume hotkey was pressed
  2003. * since the last poll cycle.
  2004. *
  2005. * Just to make our life interesting, some newer Lenovo ThinkPads have
  2006. * bugs in the BIOS and may fail to update volume_toggle properly.
  2007. */
  2008. if (newn->mute) {
  2009. /* muted */
  2010. if (!oldn->mute ||
  2011. oldn->volume_toggle != newn->volume_toggle ||
  2012. oldn->volume_level != newn->volume_level) {
  2013. /* recently muted, or repeated mute keypress, or
  2014. * multiple presses ending in mute */
  2015. issue_volchange(oldn->volume_level, newn->volume_level);
  2016. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  2017. }
  2018. } else {
  2019. /* unmute */
  2020. if (oldn->mute) {
  2021. /* recently unmuted, issue 'unmute' keypress */
  2022. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  2023. }
  2024. if (oldn->volume_level != newn->volume_level) {
  2025. issue_volchange(oldn->volume_level, newn->volume_level);
  2026. } else if (oldn->volume_toggle != newn->volume_toggle) {
  2027. /* repeated vol up/down keypress at end of scale ? */
  2028. if (newn->volume_level == 0)
  2029. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  2030. else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
  2031. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  2032. }
  2033. }
  2034. /* handle brightness */
  2035. if (oldn->brightness_level != newn->brightness_level) {
  2036. issue_brightnesschange(oldn->brightness_level,
  2037. newn->brightness_level);
  2038. } else if (oldn->brightness_toggle != newn->brightness_toggle) {
  2039. /* repeated key presses that didn't change state */
  2040. if (newn->brightness_level == 0)
  2041. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  2042. else if (newn->brightness_level >= bright_maxlvl
  2043. && !tp_features.bright_unkfw)
  2044. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  2045. }
  2046. #undef TPACPI_COMPARE_KEY
  2047. #undef TPACPI_MAY_SEND_KEY
  2048. }
  2049. /*
  2050. * Polling driver
  2051. *
  2052. * We track all events in hotkey_source_mask all the time, since
  2053. * most of them are edge-based. We only issue those requested by
  2054. * hotkey_user_mask or hotkey_driver_mask, though.
  2055. */
  2056. static int hotkey_kthread(void *data)
  2057. {
  2058. struct tp_nvram_state s[2];
  2059. u32 poll_mask, event_mask;
  2060. unsigned int si, so;
  2061. unsigned long t;
  2062. unsigned int change_detector, must_reset;
  2063. unsigned int poll_freq;
  2064. mutex_lock(&hotkey_thread_mutex);
  2065. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  2066. goto exit;
  2067. set_freezable();
  2068. so = 0;
  2069. si = 1;
  2070. t = 0;
  2071. /* Initial state for compares */
  2072. mutex_lock(&hotkey_thread_data_mutex);
  2073. change_detector = hotkey_config_change;
  2074. poll_mask = hotkey_source_mask;
  2075. event_mask = hotkey_source_mask &
  2076. (hotkey_driver_mask | hotkey_user_mask);
  2077. poll_freq = hotkey_poll_freq;
  2078. mutex_unlock(&hotkey_thread_data_mutex);
  2079. hotkey_read_nvram(&s[so], poll_mask);
  2080. while (!kthread_should_stop()) {
  2081. if (t == 0) {
  2082. if (likely(poll_freq))
  2083. t = 1000/poll_freq;
  2084. else
  2085. t = 100; /* should never happen... */
  2086. }
  2087. t = msleep_interruptible(t);
  2088. if (unlikely(kthread_should_stop()))
  2089. break;
  2090. must_reset = try_to_freeze();
  2091. if (t > 0 && !must_reset)
  2092. continue;
  2093. mutex_lock(&hotkey_thread_data_mutex);
  2094. if (must_reset || hotkey_config_change != change_detector) {
  2095. /* forget old state on thaw or config change */
  2096. si = so;
  2097. t = 0;
  2098. change_detector = hotkey_config_change;
  2099. }
  2100. poll_mask = hotkey_source_mask;
  2101. event_mask = hotkey_source_mask &
  2102. (hotkey_driver_mask | hotkey_user_mask);
  2103. poll_freq = hotkey_poll_freq;
  2104. mutex_unlock(&hotkey_thread_data_mutex);
  2105. if (likely(poll_mask)) {
  2106. hotkey_read_nvram(&s[si], poll_mask);
  2107. if (likely(si != so)) {
  2108. hotkey_compare_and_issue_event(&s[so], &s[si],
  2109. event_mask);
  2110. }
  2111. }
  2112. so = si;
  2113. si ^= 1;
  2114. }
  2115. exit:
  2116. mutex_unlock(&hotkey_thread_mutex);
  2117. return 0;
  2118. }
  2119. /* call with hotkey_mutex held */
  2120. static void hotkey_poll_stop_sync(void)
  2121. {
  2122. if (tpacpi_hotkey_task) {
  2123. if (frozen(tpacpi_hotkey_task) ||
  2124. freezing(tpacpi_hotkey_task))
  2125. thaw_process(tpacpi_hotkey_task);
  2126. kthread_stop(tpacpi_hotkey_task);
  2127. tpacpi_hotkey_task = NULL;
  2128. mutex_lock(&hotkey_thread_mutex);
  2129. /* at this point, the thread did exit */
  2130. mutex_unlock(&hotkey_thread_mutex);
  2131. }
  2132. }
  2133. /* call with hotkey_mutex held */
  2134. static void hotkey_poll_setup(const bool may_warn)
  2135. {
  2136. const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
  2137. const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
  2138. if (hotkey_poll_freq > 0 &&
  2139. (poll_driver_mask ||
  2140. (poll_user_mask && tpacpi_inputdev->users > 0))) {
  2141. if (!tpacpi_hotkey_task) {
  2142. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  2143. NULL, TPACPI_NVRAM_KTHREAD_NAME);
  2144. if (IS_ERR(tpacpi_hotkey_task)) {
  2145. tpacpi_hotkey_task = NULL;
  2146. printk(TPACPI_ERR
  2147. "could not create kernel thread "
  2148. "for hotkey polling\n");
  2149. }
  2150. }
  2151. } else {
  2152. hotkey_poll_stop_sync();
  2153. if (may_warn && (poll_driver_mask || poll_user_mask) &&
  2154. hotkey_poll_freq == 0) {
  2155. printk(TPACPI_NOTICE
  2156. "hot keys 0x%08x and/or events 0x%08x "
  2157. "require polling, which is currently "
  2158. "disabled\n",
  2159. poll_user_mask, poll_driver_mask);
  2160. }
  2161. }
  2162. }
  2163. static void hotkey_poll_setup_safe(const bool may_warn)
  2164. {
  2165. mutex_lock(&hotkey_mutex);
  2166. hotkey_poll_setup(may_warn);
  2167. mutex_unlock(&hotkey_mutex);
  2168. }
  2169. /* call with hotkey_mutex held */
  2170. static void hotkey_poll_set_freq(unsigned int freq)
  2171. {
  2172. if (!freq)
  2173. hotkey_poll_stop_sync();
  2174. hotkey_poll_freq = freq;
  2175. }
  2176. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2177. static void hotkey_poll_setup(const bool __unused)
  2178. {
  2179. }
  2180. static void hotkey_poll_setup_safe(const bool __unused)
  2181. {
  2182. }
  2183. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2184. static int hotkey_inputdev_open(struct input_dev *dev)
  2185. {
  2186. switch (tpacpi_lifecycle) {
  2187. case TPACPI_LIFE_INIT:
  2188. case TPACPI_LIFE_RUNNING:
  2189. hotkey_poll_setup_safe(false);
  2190. return 0;
  2191. case TPACPI_LIFE_EXITING:
  2192. return -EBUSY;
  2193. }
  2194. /* Should only happen if tpacpi_lifecycle is corrupt */
  2195. BUG();
  2196. return -EBUSY;
  2197. }
  2198. static void hotkey_inputdev_close(struct input_dev *dev)
  2199. {
  2200. /* disable hotkey polling when possible */
  2201. if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
  2202. !(hotkey_source_mask & hotkey_driver_mask))
  2203. hotkey_poll_setup_safe(false);
  2204. }
  2205. /* sysfs hotkey enable ------------------------------------------------- */
  2206. static ssize_t hotkey_enable_show(struct device *dev,
  2207. struct device_attribute *attr,
  2208. char *buf)
  2209. {
  2210. int res, status;
  2211. printk_deprecated_attribute("hotkey_enable",
  2212. "Hotkey reporting is always enabled");
  2213. res = hotkey_status_get(&status);
  2214. if (res)
  2215. return res;
  2216. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  2217. }
  2218. static ssize_t hotkey_enable_store(struct device *dev,
  2219. struct device_attribute *attr,
  2220. const char *buf, size_t count)
  2221. {
  2222. unsigned long t;
  2223. printk_deprecated_attribute("hotkey_enable",
  2224. "Hotkeys can be disabled through hotkey_mask");
  2225. if (parse_strtoul(buf, 1, &t))
  2226. return -EINVAL;
  2227. if (t == 0)
  2228. return -EPERM;
  2229. return count;
  2230. }
  2231. static struct device_attribute dev_attr_hotkey_enable =
  2232. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  2233. hotkey_enable_show, hotkey_enable_store);
  2234. /* sysfs hotkey mask --------------------------------------------------- */
  2235. static ssize_t hotkey_mask_show(struct device *dev,
  2236. struct device_attribute *attr,
  2237. char *buf)
  2238. {
  2239. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
  2240. }
  2241. static ssize_t hotkey_mask_store(struct device *dev,
  2242. struct device_attribute *attr,
  2243. const char *buf, size_t count)
  2244. {
  2245. unsigned long t;
  2246. int res;
  2247. if (parse_strtoul(buf, 0xffffffffUL, &t))
  2248. return -EINVAL;
  2249. if (mutex_lock_killable(&hotkey_mutex))
  2250. return -ERESTARTSYS;
  2251. res = hotkey_user_mask_set(t);
  2252. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2253. hotkey_poll_setup(true);
  2254. #endif
  2255. mutex_unlock(&hotkey_mutex);
  2256. tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
  2257. return (res) ? res : count;
  2258. }
  2259. static struct device_attribute dev_attr_hotkey_mask =
  2260. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  2261. hotkey_mask_show, hotkey_mask_store);
  2262. /* sysfs hotkey bios_enabled ------------------------------------------- */
  2263. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  2264. struct device_attribute *attr,
  2265. char *buf)
  2266. {
  2267. return sprintf(buf, "0\n");
  2268. }
  2269. static struct device_attribute dev_attr_hotkey_bios_enabled =
  2270. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  2271. /* sysfs hotkey bios_mask ---------------------------------------------- */
  2272. static ssize_t hotkey_bios_mask_show(struct device *dev,
  2273. struct device_attribute *attr,
  2274. char *buf)
  2275. {
  2276. printk_deprecated_attribute("hotkey_bios_mask",
  2277. "This attribute is useless.");
  2278. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  2279. }
  2280. static struct device_attribute dev_attr_hotkey_bios_mask =
  2281. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  2282. /* sysfs hotkey all_mask ----------------------------------------------- */
  2283. static ssize_t hotkey_all_mask_show(struct device *dev,
  2284. struct device_attribute *attr,
  2285. char *buf)
  2286. {
  2287. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  2288. hotkey_all_mask | hotkey_source_mask);
  2289. }
  2290. static struct device_attribute dev_attr_hotkey_all_mask =
  2291. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  2292. /* sysfs hotkey recommended_mask --------------------------------------- */
  2293. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  2294. struct device_attribute *attr,
  2295. char *buf)
  2296. {
  2297. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  2298. (hotkey_all_mask | hotkey_source_mask)
  2299. & ~hotkey_reserved_mask);
  2300. }
  2301. static struct device_attribute dev_attr_hotkey_recommended_mask =
  2302. __ATTR(hotkey_recommended_mask, S_IRUGO,
  2303. hotkey_recommended_mask_show, NULL);
  2304. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2305. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  2306. static ssize_t hotkey_source_mask_show(struct device *dev,
  2307. struct device_attribute *attr,
  2308. char *buf)
  2309. {
  2310. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
  2311. }
  2312. static ssize_t hotkey_source_mask_store(struct device *dev,
  2313. struct device_attribute *attr,
  2314. const char *buf, size_t count)
  2315. {
  2316. unsigned long t;
  2317. u32 r_ev;
  2318. int rc;
  2319. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  2320. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  2321. return -EINVAL;
  2322. if (mutex_lock_killable(&hotkey_mutex))
  2323. return -ERESTARTSYS;
  2324. HOTKEY_CONFIG_CRITICAL_START
  2325. hotkey_source_mask = t;
  2326. HOTKEY_CONFIG_CRITICAL_END
  2327. rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
  2328. ~hotkey_source_mask);
  2329. hotkey_poll_setup(true);
  2330. /* check if events needed by the driver got disabled */
  2331. r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
  2332. & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
  2333. mutex_unlock(&hotkey_mutex);
  2334. if (rc < 0)
  2335. printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
  2336. "firmware event mask!\n");
  2337. if (r_ev)
  2338. printk(TPACPI_NOTICE "hotkey_source_mask: "
  2339. "some important events were disabled: "
  2340. "0x%04x\n", r_ev);
  2341. tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
  2342. return (rc < 0) ? rc : count;
  2343. }
  2344. static struct device_attribute dev_attr_hotkey_source_mask =
  2345. __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
  2346. hotkey_source_mask_show, hotkey_source_mask_store);
  2347. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  2348. static ssize_t hotkey_poll_freq_show(struct device *dev,
  2349. struct device_attribute *attr,
  2350. char *buf)
  2351. {
  2352. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
  2353. }
  2354. static ssize_t hotkey_poll_freq_store(struct device *dev,
  2355. struct device_attribute *attr,
  2356. const char *buf, size_t count)
  2357. {
  2358. unsigned long t;
  2359. if (parse_strtoul(buf, 25, &t))
  2360. return -EINVAL;
  2361. if (mutex_lock_killable(&hotkey_mutex))
  2362. return -ERESTARTSYS;
  2363. hotkey_poll_set_freq(t);
  2364. hotkey_poll_setup(true);
  2365. mutex_unlock(&hotkey_mutex);
  2366. tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
  2367. return count;
  2368. }
  2369. static struct device_attribute dev_attr_hotkey_poll_freq =
  2370. __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
  2371. hotkey_poll_freq_show, hotkey_poll_freq_store);
  2372. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2373. /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
  2374. static ssize_t hotkey_radio_sw_show(struct device *dev,
  2375. struct device_attribute *attr,
  2376. char *buf)
  2377. {
  2378. int res;
  2379. res = hotkey_get_wlsw();
  2380. if (res < 0)
  2381. return res;
  2382. /* Opportunistic update */
  2383. tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
  2384. return snprintf(buf, PAGE_SIZE, "%d\n",
  2385. (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
  2386. }
  2387. static struct device_attribute dev_attr_hotkey_radio_sw =
  2388. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  2389. static void hotkey_radio_sw_notify_change(void)
  2390. {
  2391. if (tp_features.hotkey_wlsw)
  2392. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2393. "hotkey_radio_sw");
  2394. }
  2395. /* sysfs hotkey tablet mode (pollable) --------------------------------- */
  2396. static ssize_t hotkey_tablet_mode_show(struct device *dev,
  2397. struct device_attribute *attr,
  2398. char *buf)
  2399. {
  2400. int res, s;
  2401. res = hotkey_get_tablet_mode(&s);
  2402. if (res < 0)
  2403. return res;
  2404. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  2405. }
  2406. static struct device_attribute dev_attr_hotkey_tablet_mode =
  2407. __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
  2408. static void hotkey_tablet_mode_notify_change(void)
  2409. {
  2410. if (tp_features.hotkey_tablet)
  2411. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2412. "hotkey_tablet_mode");
  2413. }
  2414. /* sysfs hotkey report_mode -------------------------------------------- */
  2415. static ssize_t hotkey_report_mode_show(struct device *dev,
  2416. struct device_attribute *attr,
  2417. char *buf)
  2418. {
  2419. return snprintf(buf, PAGE_SIZE, "%d\n",
  2420. (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
  2421. }
  2422. static struct device_attribute dev_attr_hotkey_report_mode =
  2423. __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
  2424. /* sysfs wakeup reason (pollable) -------------------------------------- */
  2425. static ssize_t hotkey_wakeup_reason_show(struct device *dev,
  2426. struct device_attribute *attr,
  2427. char *buf)
  2428. {
  2429. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
  2430. }
  2431. static struct device_attribute dev_attr_hotkey_wakeup_reason =
  2432. __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
  2433. static void hotkey_wakeup_reason_notify_change(void)
  2434. {
  2435. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2436. "wakeup_reason");
  2437. }
  2438. /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
  2439. static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
  2440. struct device_attribute *attr,
  2441. char *buf)
  2442. {
  2443. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
  2444. }
  2445. static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
  2446. __ATTR(wakeup_hotunplug_complete, S_IRUGO,
  2447. hotkey_wakeup_hotunplug_complete_show, NULL);
  2448. static void hotkey_wakeup_hotunplug_complete_notify_change(void)
  2449. {
  2450. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2451. "wakeup_hotunplug_complete");
  2452. }
  2453. /* --------------------------------------------------------------------- */
  2454. static struct attribute *hotkey_attributes[] __initdata = {
  2455. &dev_attr_hotkey_enable.attr,
  2456. &dev_attr_hotkey_bios_enabled.attr,
  2457. &dev_attr_hotkey_bios_mask.attr,
  2458. &dev_attr_hotkey_report_mode.attr,
  2459. &dev_attr_hotkey_wakeup_reason.attr,
  2460. &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
  2461. &dev_attr_hotkey_mask.attr,
  2462. &dev_attr_hotkey_all_mask.attr,
  2463. &dev_attr_hotkey_recommended_mask.attr,
  2464. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2465. &dev_attr_hotkey_source_mask.attr,
  2466. &dev_attr_hotkey_poll_freq.attr,
  2467. #endif
  2468. };
  2469. /*
  2470. * Sync both the hw and sw blocking state of all switches
  2471. */
  2472. static void tpacpi_send_radiosw_update(void)
  2473. {
  2474. int wlsw;
  2475. /*
  2476. * We must sync all rfkill controllers *before* issuing any
  2477. * rfkill input events, or we will race the rfkill core input
  2478. * handler.
  2479. *
  2480. * tpacpi_inputdev_send_mutex works as a syncronization point
  2481. * for the above.
  2482. *
  2483. * We optimize to avoid numerous calls to hotkey_get_wlsw.
  2484. */
  2485. wlsw = hotkey_get_wlsw();
  2486. /* Sync hw blocking state first if it is hw-blocked */
  2487. if (wlsw == TPACPI_RFK_RADIO_OFF)
  2488. tpacpi_rfk_update_hwblock_state(true);
  2489. /* Sync sw blocking state */
  2490. tpacpi_rfk_update_swstate_all();
  2491. /* Sync hw blocking state last if it is hw-unblocked */
  2492. if (wlsw == TPACPI_RFK_RADIO_ON)
  2493. tpacpi_rfk_update_hwblock_state(false);
  2494. /* Issue rfkill input event for WLSW switch */
  2495. if (!(wlsw < 0)) {
  2496. mutex_lock(&tpacpi_inputdev_send_mutex);
  2497. input_report_switch(tpacpi_inputdev,
  2498. SW_RFKILL_ALL, (wlsw > 0));
  2499. input_sync(tpacpi_inputdev);
  2500. mutex_unlock(&tpacpi_inputdev_send_mutex);
  2501. }
  2502. /*
  2503. * this can be unconditional, as we will poll state again
  2504. * if userspace uses the notify to read data
  2505. */
  2506. hotkey_radio_sw_notify_change();
  2507. }
  2508. static void hotkey_exit(void)
  2509. {
  2510. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2511. mutex_lock(&hotkey_mutex);
  2512. hotkey_poll_stop_sync();
  2513. mutex_unlock(&hotkey_mutex);
  2514. #endif
  2515. if (hotkey_dev_attributes)
  2516. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  2517. kfree(hotkey_keycode_map);
  2518. dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
  2519. "restoring original HKEY status and mask\n");
  2520. /* yes, there is a bitwise or below, we want the
  2521. * functions to be called even if one of them fail */
  2522. if (((tp_features.hotkey_mask &&
  2523. hotkey_mask_set(hotkey_orig_mask)) |
  2524. hotkey_status_set(false)) != 0)
  2525. printk(TPACPI_ERR
  2526. "failed to restore hot key mask "
  2527. "to BIOS defaults\n");
  2528. }
  2529. static void __init hotkey_unmap(const unsigned int scancode)
  2530. {
  2531. if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
  2532. clear_bit(hotkey_keycode_map[scancode],
  2533. tpacpi_inputdev->keybit);
  2534. hotkey_keycode_map[scancode] = KEY_RESERVED;
  2535. }
  2536. }
  2537. /*
  2538. * HKEY quirks:
  2539. * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
  2540. */
  2541. #define TPACPI_HK_Q_INIMASK 0x0001
  2542. static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
  2543. TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
  2544. TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
  2545. TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
  2546. TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
  2547. TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
  2548. TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
  2549. TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
  2550. TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
  2551. TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
  2552. TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
  2553. TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
  2554. TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
  2555. TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
  2556. TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
  2557. TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
  2558. TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
  2559. TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
  2560. TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
  2561. TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
  2562. };
  2563. typedef u16 tpacpi_keymap_entry_t;
  2564. typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
  2565. static int __init hotkey_init(struct ibm_init_struct *iibm)
  2566. {
  2567. /* Requirements for changing the default keymaps:
  2568. *
  2569. * 1. Many of the keys are mapped to KEY_RESERVED for very
  2570. * good reasons. Do not change them unless you have deep
  2571. * knowledge on the IBM and Lenovo ThinkPad firmware for
  2572. * the various ThinkPad models. The driver behaves
  2573. * differently for KEY_RESERVED: such keys have their
  2574. * hot key mask *unset* in mask_recommended, and also
  2575. * in the initial hot key mask programmed into the
  2576. * firmware at driver load time, which means the firm-
  2577. * ware may react very differently if you change them to
  2578. * something else;
  2579. *
  2580. * 2. You must be subscribed to the linux-thinkpad and
  2581. * ibm-acpi-devel mailing lists, and you should read the
  2582. * list archives since 2007 if you want to change the
  2583. * keymaps. This requirement exists so that you will
  2584. * know the past history of problems with the thinkpad-
  2585. * acpi driver keymaps, and also that you will be
  2586. * listening to any bug reports;
  2587. *
  2588. * 3. Do not send thinkpad-acpi specific patches directly to
  2589. * for merging, *ever*. Send them to the linux-acpi
  2590. * mailinglist for comments. Merging is to be done only
  2591. * through acpi-test and the ACPI maintainer.
  2592. *
  2593. * If the above is too much to ask, don't change the keymap.
  2594. * Ask the thinkpad-acpi maintainer to do it, instead.
  2595. */
  2596. enum keymap_index {
  2597. TPACPI_KEYMAP_IBM_GENERIC = 0,
  2598. TPACPI_KEYMAP_LENOVO_GENERIC,
  2599. };
  2600. static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
  2601. /* Generic keymap for IBM ThinkPads */
  2602. [TPACPI_KEYMAP_IBM_GENERIC] = {
  2603. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  2604. KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
  2605. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  2606. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  2607. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  2608. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  2609. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  2610. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  2611. /* brightness: firmware always reacts to them */
  2612. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  2613. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  2614. /* Thinklight: firmware always react to it */
  2615. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  2616. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  2617. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  2618. /* Volume: firmware always react to it and reprograms
  2619. * the built-in *extra* mixer. Never map it to control
  2620. * another mixer by default. */
  2621. KEY_RESERVED, /* 0x14: VOLUME UP */
  2622. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  2623. KEY_RESERVED, /* 0x16: MUTE */
  2624. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  2625. /* (assignments unknown, please report if found) */
  2626. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2627. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2628. },
  2629. /* Generic keymap for Lenovo ThinkPads */
  2630. [TPACPI_KEYMAP_LENOVO_GENERIC] = {
  2631. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  2632. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  2633. KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  2634. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  2635. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  2636. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  2637. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  2638. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  2639. /* These should be enabled --only-- when ACPI video
  2640. * is disabled (i.e. in "vendor" mode), and are handled
  2641. * in a special way by the init code */
  2642. KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
  2643. KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
  2644. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  2645. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  2646. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  2647. /* Volume: z60/z61, T60 (BIOS version?): firmware always
  2648. * react to it and reprograms the built-in *extra* mixer.
  2649. * Never map it to control another mixer by default.
  2650. *
  2651. * T60?, T61, R60?, R61: firmware and EC tries to send
  2652. * these over the regular keyboard, so these are no-ops,
  2653. * but there are still weird bugs re. MUTE, so do not
  2654. * change unless you get test reports from all Lenovo
  2655. * models. May cause the BIOS to interfere with the
  2656. * HDA mixer.
  2657. */
  2658. KEY_RESERVED, /* 0x14: VOLUME UP */
  2659. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  2660. KEY_RESERVED, /* 0x16: MUTE */
  2661. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  2662. /* (assignments unknown, please report if found) */
  2663. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2664. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2665. },
  2666. };
  2667. static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
  2668. /* Generic maps (fallback) */
  2669. {
  2670. .vendor = PCI_VENDOR_ID_IBM,
  2671. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  2672. .quirks = TPACPI_KEYMAP_IBM_GENERIC,
  2673. },
  2674. {
  2675. .vendor = PCI_VENDOR_ID_LENOVO,
  2676. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  2677. .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
  2678. },
  2679. };
  2680. #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
  2681. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
  2682. int res, i;
  2683. int status;
  2684. int hkeyv;
  2685. bool radiosw_state = false;
  2686. bool tabletsw_state = false;
  2687. unsigned long quirks;
  2688. unsigned long keymap_id;
  2689. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2690. "initializing hotkey subdriver\n");
  2691. BUG_ON(!tpacpi_inputdev);
  2692. BUG_ON(tpacpi_inputdev->open != NULL ||
  2693. tpacpi_inputdev->close != NULL);
  2694. TPACPI_ACPIHANDLE_INIT(hkey);
  2695. mutex_init(&hotkey_mutex);
  2696. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2697. mutex_init(&hotkey_thread_mutex);
  2698. mutex_init(&hotkey_thread_data_mutex);
  2699. #endif
  2700. /* hotkey not supported on 570 */
  2701. tp_features.hotkey = hkey_handle != NULL;
  2702. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2703. "hotkeys are %s\n",
  2704. str_supported(tp_features.hotkey));
  2705. if (!tp_features.hotkey)
  2706. return 1;
  2707. quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
  2708. ARRAY_SIZE(tpacpi_hotkey_qtable));
  2709. tpacpi_disable_brightness_delay();
  2710. /* MUST have enough space for all attributes to be added to
  2711. * hotkey_dev_attributes */
  2712. hotkey_dev_attributes = create_attr_set(
  2713. ARRAY_SIZE(hotkey_attributes) + 2,
  2714. NULL);
  2715. if (!hotkey_dev_attributes)
  2716. return -ENOMEM;
  2717. res = add_many_to_attr_set(hotkey_dev_attributes,
  2718. hotkey_attributes,
  2719. ARRAY_SIZE(hotkey_attributes));
  2720. if (res)
  2721. goto err_exit;
  2722. /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
  2723. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  2724. for HKEY interface version 0x100 */
  2725. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  2726. if ((hkeyv >> 8) != 1) {
  2727. printk(TPACPI_ERR "unknown version of the "
  2728. "HKEY interface: 0x%x\n", hkeyv);
  2729. printk(TPACPI_ERR "please report this to %s\n",
  2730. TPACPI_MAIL);
  2731. } else {
  2732. /*
  2733. * MHKV 0x100 in A31, R40, R40e,
  2734. * T4x, X31, and later
  2735. */
  2736. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2737. "firmware HKEY interface version: 0x%x\n",
  2738. hkeyv);
  2739. /* Paranoia check AND init hotkey_all_mask */
  2740. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  2741. "MHKA", "qd")) {
  2742. printk(TPACPI_ERR
  2743. "missing MHKA handler, "
  2744. "please report this to %s\n",
  2745. TPACPI_MAIL);
  2746. /* Fallback: pre-init for FN+F3,F4,F12 */
  2747. hotkey_all_mask = 0x080cU;
  2748. } else {
  2749. tp_features.hotkey_mask = 1;
  2750. }
  2751. }
  2752. }
  2753. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2754. "hotkey masks are %s\n",
  2755. str_supported(tp_features.hotkey_mask));
  2756. /* Init hotkey_all_mask if not initialized yet */
  2757. if (!tp_features.hotkey_mask && !hotkey_all_mask &&
  2758. (quirks & TPACPI_HK_Q_INIMASK))
  2759. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  2760. /* Init hotkey_acpi_mask and hotkey_orig_mask */
  2761. if (tp_features.hotkey_mask) {
  2762. /* hotkey_source_mask *must* be zero for
  2763. * the first hotkey_mask_get to return hotkey_orig_mask */
  2764. res = hotkey_mask_get();
  2765. if (res)
  2766. goto err_exit;
  2767. hotkey_orig_mask = hotkey_acpi_mask;
  2768. } else {
  2769. hotkey_orig_mask = hotkey_all_mask;
  2770. hotkey_acpi_mask = hotkey_all_mask;
  2771. }
  2772. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  2773. if (dbg_wlswemul) {
  2774. tp_features.hotkey_wlsw = 1;
  2775. radiosw_state = !!tpacpi_wlsw_emulstate;
  2776. printk(TPACPI_INFO
  2777. "radio switch emulation enabled\n");
  2778. } else
  2779. #endif
  2780. /* Not all thinkpads have a hardware radio switch */
  2781. if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  2782. tp_features.hotkey_wlsw = 1;
  2783. radiosw_state = !!status;
  2784. printk(TPACPI_INFO
  2785. "radio switch found; radios are %s\n",
  2786. enabled(status, 0));
  2787. }
  2788. if (tp_features.hotkey_wlsw)
  2789. res = add_to_attr_set(hotkey_dev_attributes,
  2790. &dev_attr_hotkey_radio_sw.attr);
  2791. /* For X41t, X60t, X61t Tablets... */
  2792. if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
  2793. tp_features.hotkey_tablet = 1;
  2794. tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
  2795. printk(TPACPI_INFO
  2796. "possible tablet mode switch found; "
  2797. "ThinkPad in %s mode\n",
  2798. (tabletsw_state) ? "tablet" : "laptop");
  2799. res = add_to_attr_set(hotkey_dev_attributes,
  2800. &dev_attr_hotkey_tablet_mode.attr);
  2801. }
  2802. if (!res)
  2803. res = register_attr_set_with_sysfs(
  2804. hotkey_dev_attributes,
  2805. &tpacpi_pdev->dev.kobj);
  2806. if (res)
  2807. goto err_exit;
  2808. /* Set up key map */
  2809. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  2810. GFP_KERNEL);
  2811. if (!hotkey_keycode_map) {
  2812. printk(TPACPI_ERR
  2813. "failed to allocate memory for key map\n");
  2814. res = -ENOMEM;
  2815. goto err_exit;
  2816. }
  2817. keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
  2818. ARRAY_SIZE(tpacpi_keymap_qtable));
  2819. BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
  2820. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2821. "using keymap number %lu\n", keymap_id);
  2822. memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
  2823. TPACPI_HOTKEY_MAP_SIZE);
  2824. input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
  2825. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  2826. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  2827. tpacpi_inputdev->keycode = hotkey_keycode_map;
  2828. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  2829. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  2830. input_set_capability(tpacpi_inputdev, EV_KEY,
  2831. hotkey_keycode_map[i]);
  2832. } else {
  2833. if (i < sizeof(hotkey_reserved_mask)*8)
  2834. hotkey_reserved_mask |= 1 << i;
  2835. }
  2836. }
  2837. if (tp_features.hotkey_wlsw) {
  2838. input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
  2839. input_report_switch(tpacpi_inputdev,
  2840. SW_RFKILL_ALL, radiosw_state);
  2841. }
  2842. if (tp_features.hotkey_tablet) {
  2843. input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
  2844. input_report_switch(tpacpi_inputdev,
  2845. SW_TABLET_MODE, tabletsw_state);
  2846. }
  2847. /* Do not issue duplicate brightness change events to
  2848. * userspace. tpacpi_detect_brightness_capabilities() must have
  2849. * been called before this point */
  2850. if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
  2851. printk(TPACPI_INFO
  2852. "This ThinkPad has standard ACPI backlight "
  2853. "brightness control, supported by the ACPI "
  2854. "video driver\n");
  2855. printk(TPACPI_NOTICE
  2856. "Disabling thinkpad-acpi brightness events "
  2857. "by default...\n");
  2858. /* Disable brightness up/down on Lenovo thinkpads when
  2859. * ACPI is handling them, otherwise it is plain impossible
  2860. * for userspace to do something even remotely sane */
  2861. hotkey_reserved_mask |=
  2862. (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
  2863. | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
  2864. hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
  2865. hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
  2866. }
  2867. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2868. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  2869. & ~hotkey_all_mask
  2870. & ~hotkey_reserved_mask;
  2871. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2872. "hotkey source mask 0x%08x, polling freq %u\n",
  2873. hotkey_source_mask, hotkey_poll_freq);
  2874. #endif
  2875. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2876. "enabling firmware HKEY event interface...\n");
  2877. res = hotkey_status_set(true);
  2878. if (res) {
  2879. hotkey_exit();
  2880. return res;
  2881. }
  2882. res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
  2883. | hotkey_driver_mask)
  2884. & ~hotkey_source_mask);
  2885. if (res < 0 && res != -ENXIO) {
  2886. hotkey_exit();
  2887. return res;
  2888. }
  2889. hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
  2890. & ~hotkey_reserved_mask;
  2891. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2892. "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
  2893. hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
  2894. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2895. "legacy ibm/hotkey event reporting over procfs %s\n",
  2896. (hotkey_report_mode < 2) ?
  2897. "enabled" : "disabled");
  2898. tpacpi_inputdev->open = &hotkey_inputdev_open;
  2899. tpacpi_inputdev->close = &hotkey_inputdev_close;
  2900. hotkey_poll_setup_safe(true);
  2901. return 0;
  2902. err_exit:
  2903. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  2904. hotkey_dev_attributes = NULL;
  2905. return (res < 0)? res : 1;
  2906. }
  2907. static bool hotkey_notify_hotkey(const u32 hkey,
  2908. bool *send_acpi_ev,
  2909. bool *ignore_acpi_ev)
  2910. {
  2911. /* 0x1000-0x1FFF: key presses */
  2912. unsigned int scancode = hkey & 0xfff;
  2913. *send_acpi_ev = true;
  2914. *ignore_acpi_ev = false;
  2915. /* HKEY event 0x1001 is scancode 0x00 */
  2916. if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
  2917. scancode--;
  2918. if (!(hotkey_source_mask & (1 << scancode))) {
  2919. tpacpi_input_send_key_masked(scancode);
  2920. *send_acpi_ev = false;
  2921. } else {
  2922. *ignore_acpi_ev = true;
  2923. }
  2924. return true;
  2925. }
  2926. return false;
  2927. }
  2928. static bool hotkey_notify_wakeup(const u32 hkey,
  2929. bool *send_acpi_ev,
  2930. bool *ignore_acpi_ev)
  2931. {
  2932. /* 0x2000-0x2FFF: Wakeup reason */
  2933. *send_acpi_ev = true;
  2934. *ignore_acpi_ev = false;
  2935. switch (hkey) {
  2936. case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
  2937. case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
  2938. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  2939. *ignore_acpi_ev = true;
  2940. break;
  2941. case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
  2942. case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
  2943. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  2944. *ignore_acpi_ev = true;
  2945. break;
  2946. case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
  2947. case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
  2948. printk(TPACPI_ALERT
  2949. "EMERGENCY WAKEUP: battery almost empty\n");
  2950. /* how to auto-heal: */
  2951. /* 2313: woke up from S3, go to S4/S5 */
  2952. /* 2413: woke up from S4, go to S5 */
  2953. break;
  2954. default:
  2955. return false;
  2956. }
  2957. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  2958. printk(TPACPI_INFO
  2959. "woke up due to a hot-unplug "
  2960. "request...\n");
  2961. hotkey_wakeup_reason_notify_change();
  2962. }
  2963. return true;
  2964. }
  2965. static bool hotkey_notify_usrevent(const u32 hkey,
  2966. bool *send_acpi_ev,
  2967. bool *ignore_acpi_ev)
  2968. {
  2969. /* 0x5000-0x5FFF: human interface helpers */
  2970. *send_acpi_ev = true;
  2971. *ignore_acpi_ev = false;
  2972. switch (hkey) {
  2973. case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
  2974. case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
  2975. return true;
  2976. case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
  2977. case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
  2978. tpacpi_input_send_tabletsw();
  2979. hotkey_tablet_mode_notify_change();
  2980. *send_acpi_ev = false;
  2981. return true;
  2982. case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
  2983. case TP_HKEY_EV_LID_OPEN: /* Lid opened */
  2984. case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
  2985. /* do not propagate these events */
  2986. *ignore_acpi_ev = true;
  2987. return true;
  2988. default:
  2989. return false;
  2990. }
  2991. }
  2992. static void thermal_dump_all_sensors(void);
  2993. static bool hotkey_notify_thermal(const u32 hkey,
  2994. bool *send_acpi_ev,
  2995. bool *ignore_acpi_ev)
  2996. {
  2997. bool known = true;
  2998. /* 0x6000-0x6FFF: thermal alarms */
  2999. *send_acpi_ev = true;
  3000. *ignore_acpi_ev = false;
  3001. switch (hkey) {
  3002. case TP_HKEY_EV_THM_TABLE_CHANGED:
  3003. printk(TPACPI_INFO
  3004. "EC reports that Thermal Table has changed\n");
  3005. /* recommended action: do nothing, we don't have
  3006. * Lenovo ATM information */
  3007. return true;
  3008. case TP_HKEY_EV_ALARM_BAT_HOT:
  3009. printk(TPACPI_CRIT
  3010. "THERMAL ALARM: battery is too hot!\n");
  3011. /* recommended action: warn user through gui */
  3012. break;
  3013. case TP_HKEY_EV_ALARM_BAT_XHOT:
  3014. printk(TPACPI_ALERT
  3015. "THERMAL EMERGENCY: battery is extremely hot!\n");
  3016. /* recommended action: immediate sleep/hibernate */
  3017. break;
  3018. case TP_HKEY_EV_ALARM_SENSOR_HOT:
  3019. printk(TPACPI_CRIT
  3020. "THERMAL ALARM: "
  3021. "a sensor reports something is too hot!\n");
  3022. /* recommended action: warn user through gui, that */
  3023. /* some internal component is too hot */
  3024. break;
  3025. case TP_HKEY_EV_ALARM_SENSOR_XHOT:
  3026. printk(TPACPI_ALERT
  3027. "THERMAL EMERGENCY: "
  3028. "a sensor reports something is extremely hot!\n");
  3029. /* recommended action: immediate sleep/hibernate */
  3030. break;
  3031. default:
  3032. printk(TPACPI_ALERT
  3033. "THERMAL ALERT: unknown thermal alarm received\n");
  3034. known = false;
  3035. }
  3036. thermal_dump_all_sensors();
  3037. return known;
  3038. }
  3039. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  3040. {
  3041. u32 hkey;
  3042. bool send_acpi_ev;
  3043. bool ignore_acpi_ev;
  3044. bool known_ev;
  3045. if (event != 0x80) {
  3046. printk(TPACPI_ERR
  3047. "unknown HKEY notification event %d\n", event);
  3048. /* forward it to userspace, maybe it knows how to handle it */
  3049. acpi_bus_generate_netlink_event(
  3050. ibm->acpi->device->pnp.device_class,
  3051. dev_name(&ibm->acpi->device->dev),
  3052. event, 0);
  3053. return;
  3054. }
  3055. while (1) {
  3056. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  3057. printk(TPACPI_ERR "failed to retrieve HKEY event\n");
  3058. return;
  3059. }
  3060. if (hkey == 0) {
  3061. /* queue empty */
  3062. return;
  3063. }
  3064. send_acpi_ev = true;
  3065. ignore_acpi_ev = false;
  3066. switch (hkey >> 12) {
  3067. case 1:
  3068. /* 0x1000-0x1FFF: key presses */
  3069. known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
  3070. &ignore_acpi_ev);
  3071. break;
  3072. case 2:
  3073. /* 0x2000-0x2FFF: Wakeup reason */
  3074. known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
  3075. &ignore_acpi_ev);
  3076. break;
  3077. case 3:
  3078. /* 0x3000-0x3FFF: bay-related wakeups */
  3079. switch (hkey) {
  3080. case TP_HKEY_EV_BAYEJ_ACK:
  3081. hotkey_autosleep_ack = 1;
  3082. printk(TPACPI_INFO
  3083. "bay ejected\n");
  3084. hotkey_wakeup_hotunplug_complete_notify_change();
  3085. known_ev = true;
  3086. break;
  3087. case TP_HKEY_EV_OPTDRV_EJ:
  3088. /* FIXME: kick libata if SATA link offline */
  3089. known_ev = true;
  3090. break;
  3091. default:
  3092. known_ev = false;
  3093. }
  3094. break;
  3095. case 4:
  3096. /* 0x4000-0x4FFF: dock-related wakeups */
  3097. if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
  3098. hotkey_autosleep_ack = 1;
  3099. printk(TPACPI_INFO
  3100. "undocked\n");
  3101. hotkey_wakeup_hotunplug_complete_notify_change();
  3102. known_ev = true;
  3103. } else {
  3104. known_ev = false;
  3105. }
  3106. break;
  3107. case 5:
  3108. /* 0x5000-0x5FFF: human interface helpers */
  3109. known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
  3110. &ignore_acpi_ev);
  3111. break;
  3112. case 6:
  3113. /* 0x6000-0x6FFF: thermal alarms */
  3114. known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
  3115. &ignore_acpi_ev);
  3116. break;
  3117. case 7:
  3118. /* 0x7000-0x7FFF: misc */
  3119. if (tp_features.hotkey_wlsw &&
  3120. hkey == TP_HKEY_EV_RFKILL_CHANGED) {
  3121. tpacpi_send_radiosw_update();
  3122. send_acpi_ev = 0;
  3123. known_ev = true;
  3124. break;
  3125. }
  3126. /* fallthrough to default */
  3127. default:
  3128. known_ev = false;
  3129. }
  3130. if (!known_ev) {
  3131. printk(TPACPI_NOTICE
  3132. "unhandled HKEY event 0x%04x\n", hkey);
  3133. printk(TPACPI_NOTICE
  3134. "please report the conditions when this "
  3135. "event happened to %s\n", TPACPI_MAIL);
  3136. }
  3137. /* Legacy events */
  3138. if (!ignore_acpi_ev &&
  3139. (send_acpi_ev || hotkey_report_mode < 2)) {
  3140. acpi_bus_generate_proc_event(ibm->acpi->device,
  3141. event, hkey);
  3142. }
  3143. /* netlink events */
  3144. if (!ignore_acpi_ev && send_acpi_ev) {
  3145. acpi_bus_generate_netlink_event(
  3146. ibm->acpi->device->pnp.device_class,
  3147. dev_name(&ibm->acpi->device->dev),
  3148. event, hkey);
  3149. }
  3150. }
  3151. }
  3152. static void hotkey_suspend(pm_message_t state)
  3153. {
  3154. /* Do these on suspend, we get the events on early resume! */
  3155. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  3156. hotkey_autosleep_ack = 0;
  3157. }
  3158. static void hotkey_resume(void)
  3159. {
  3160. tpacpi_disable_brightness_delay();
  3161. if (hotkey_status_set(true) < 0 ||
  3162. hotkey_mask_set(hotkey_acpi_mask) < 0)
  3163. printk(TPACPI_ERR
  3164. "error while attempting to reset the event "
  3165. "firmware interface\n");
  3166. tpacpi_send_radiosw_update();
  3167. hotkey_tablet_mode_notify_change();
  3168. hotkey_wakeup_reason_notify_change();
  3169. hotkey_wakeup_hotunplug_complete_notify_change();
  3170. hotkey_poll_setup_safe(false);
  3171. }
  3172. /* procfs -------------------------------------------------------------- */
  3173. static int hotkey_read(struct seq_file *m)
  3174. {
  3175. int res, status;
  3176. if (!tp_features.hotkey) {
  3177. seq_printf(m, "status:\t\tnot supported\n");
  3178. return 0;
  3179. }
  3180. if (mutex_lock_killable(&hotkey_mutex))
  3181. return -ERESTARTSYS;
  3182. res = hotkey_status_get(&status);
  3183. if (!res)
  3184. res = hotkey_mask_get();
  3185. mutex_unlock(&hotkey_mutex);
  3186. if (res)
  3187. return res;
  3188. seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
  3189. if (hotkey_all_mask) {
  3190. seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
  3191. seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
  3192. } else {
  3193. seq_printf(m, "mask:\t\tnot supported\n");
  3194. seq_printf(m, "commands:\tenable, disable, reset\n");
  3195. }
  3196. return 0;
  3197. }
  3198. static void hotkey_enabledisable_warn(bool enable)
  3199. {
  3200. tpacpi_log_usertask("procfs hotkey enable/disable");
  3201. if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
  3202. TPACPI_WARN
  3203. "hotkey enable/disable functionality has been "
  3204. "removed from the driver. Hotkeys are always "
  3205. "enabled\n"))
  3206. printk(TPACPI_ERR
  3207. "Please remove the hotkey=enable module "
  3208. "parameter, it is deprecated. Hotkeys are always "
  3209. "enabled\n");
  3210. }
  3211. static int hotkey_write(char *buf)
  3212. {
  3213. int res;
  3214. u32 mask;
  3215. char *cmd;
  3216. if (!tp_features.hotkey)
  3217. return -ENODEV;
  3218. if (mutex_lock_killable(&hotkey_mutex))
  3219. return -ERESTARTSYS;
  3220. mask = hotkey_user_mask;
  3221. res = 0;
  3222. while ((cmd = next_cmd(&buf))) {
  3223. if (strlencmp(cmd, "enable") == 0) {
  3224. hotkey_enabledisable_warn(1);
  3225. } else if (strlencmp(cmd, "disable") == 0) {
  3226. hotkey_enabledisable_warn(0);
  3227. res = -EPERM;
  3228. } else if (strlencmp(cmd, "reset") == 0) {
  3229. mask = (hotkey_all_mask | hotkey_source_mask)
  3230. & ~hotkey_reserved_mask;
  3231. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  3232. /* mask set */
  3233. } else if (sscanf(cmd, "%x", &mask) == 1) {
  3234. /* mask set */
  3235. } else {
  3236. res = -EINVAL;
  3237. goto errexit;
  3238. }
  3239. }
  3240. if (!res) {
  3241. tpacpi_disclose_usertask("procfs hotkey",
  3242. "set mask to 0x%08x\n", mask);
  3243. res = hotkey_user_mask_set(mask);
  3244. }
  3245. errexit:
  3246. mutex_unlock(&hotkey_mutex);
  3247. return res;
  3248. }
  3249. static const struct acpi_device_id ibm_htk_device_ids[] = {
  3250. {TPACPI_ACPI_HKEY_HID, 0},
  3251. {"", 0},
  3252. };
  3253. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  3254. .hid = ibm_htk_device_ids,
  3255. .notify = hotkey_notify,
  3256. .handle = &hkey_handle,
  3257. .type = ACPI_DEVICE_NOTIFY,
  3258. };
  3259. static struct ibm_struct hotkey_driver_data = {
  3260. .name = "hotkey",
  3261. .read = hotkey_read,
  3262. .write = hotkey_write,
  3263. .exit = hotkey_exit,
  3264. .resume = hotkey_resume,
  3265. .suspend = hotkey_suspend,
  3266. .acpi = &ibm_hotkey_acpidriver,
  3267. };
  3268. /*************************************************************************
  3269. * Bluetooth subdriver
  3270. */
  3271. enum {
  3272. /* ACPI GBDC/SBDC bits */
  3273. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  3274. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  3275. TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
  3276. 0 = disable, 1 = enable */
  3277. };
  3278. enum {
  3279. /* ACPI \BLTH commands */
  3280. TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
  3281. TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
  3282. TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
  3283. TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
  3284. TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
  3285. };
  3286. #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
  3287. static int bluetooth_get_status(void)
  3288. {
  3289. int status;
  3290. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3291. if (dbg_bluetoothemul)
  3292. return (tpacpi_bluetooth_emulstate) ?
  3293. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3294. #endif
  3295. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  3296. return -EIO;
  3297. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
  3298. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3299. }
  3300. static int bluetooth_set_status(enum tpacpi_rfkill_state state)
  3301. {
  3302. int status;
  3303. vdbg_printk(TPACPI_DBG_RFKILL,
  3304. "will attempt to %s bluetooth\n",
  3305. (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
  3306. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3307. if (dbg_bluetoothemul) {
  3308. tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3309. return 0;
  3310. }
  3311. #endif
  3312. if (state == TPACPI_RFK_RADIO_ON)
  3313. status = TP_ACPI_BLUETOOTH_RADIOSSW
  3314. | TP_ACPI_BLUETOOTH_RESUMECTRL;
  3315. else
  3316. status = 0;
  3317. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  3318. return -EIO;
  3319. return 0;
  3320. }
  3321. /* sysfs bluetooth enable ---------------------------------------------- */
  3322. static ssize_t bluetooth_enable_show(struct device *dev,
  3323. struct device_attribute *attr,
  3324. char *buf)
  3325. {
  3326. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
  3327. attr, buf);
  3328. }
  3329. static ssize_t bluetooth_enable_store(struct device *dev,
  3330. struct device_attribute *attr,
  3331. const char *buf, size_t count)
  3332. {
  3333. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
  3334. attr, buf, count);
  3335. }
  3336. static struct device_attribute dev_attr_bluetooth_enable =
  3337. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  3338. bluetooth_enable_show, bluetooth_enable_store);
  3339. /* --------------------------------------------------------------------- */
  3340. static struct attribute *bluetooth_attributes[] = {
  3341. &dev_attr_bluetooth_enable.attr,
  3342. NULL
  3343. };
  3344. static const struct attribute_group bluetooth_attr_group = {
  3345. .attrs = bluetooth_attributes,
  3346. };
  3347. static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
  3348. .get_status = bluetooth_get_status,
  3349. .set_status = bluetooth_set_status,
  3350. };
  3351. static void bluetooth_shutdown(void)
  3352. {
  3353. /* Order firmware to save current state to NVRAM */
  3354. if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
  3355. TP_ACPI_BLTH_SAVE_STATE))
  3356. printk(TPACPI_NOTICE
  3357. "failed to save bluetooth state to NVRAM\n");
  3358. else
  3359. vdbg_printk(TPACPI_DBG_RFKILL,
  3360. "bluestooth state saved to NVRAM\n");
  3361. }
  3362. static void bluetooth_exit(void)
  3363. {
  3364. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  3365. &bluetooth_attr_group);
  3366. tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
  3367. bluetooth_shutdown();
  3368. }
  3369. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  3370. {
  3371. int res;
  3372. int status = 0;
  3373. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3374. "initializing bluetooth subdriver\n");
  3375. TPACPI_ACPIHANDLE_INIT(hkey);
  3376. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  3377. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  3378. tp_features.bluetooth = hkey_handle &&
  3379. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  3380. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3381. "bluetooth is %s, status 0x%02x\n",
  3382. str_supported(tp_features.bluetooth),
  3383. status);
  3384. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3385. if (dbg_bluetoothemul) {
  3386. tp_features.bluetooth = 1;
  3387. printk(TPACPI_INFO
  3388. "bluetooth switch emulation enabled\n");
  3389. } else
  3390. #endif
  3391. if (tp_features.bluetooth &&
  3392. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  3393. /* no bluetooth hardware present in system */
  3394. tp_features.bluetooth = 0;
  3395. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3396. "bluetooth hardware not installed\n");
  3397. }
  3398. if (!tp_features.bluetooth)
  3399. return 1;
  3400. res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
  3401. &bluetooth_tprfk_ops,
  3402. RFKILL_TYPE_BLUETOOTH,
  3403. TPACPI_RFK_BLUETOOTH_SW_NAME,
  3404. true);
  3405. if (res)
  3406. return res;
  3407. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  3408. &bluetooth_attr_group);
  3409. if (res) {
  3410. tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
  3411. return res;
  3412. }
  3413. return 0;
  3414. }
  3415. /* procfs -------------------------------------------------------------- */
  3416. static int bluetooth_read(struct seq_file *m)
  3417. {
  3418. return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
  3419. }
  3420. static int bluetooth_write(char *buf)
  3421. {
  3422. return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
  3423. }
  3424. static struct ibm_struct bluetooth_driver_data = {
  3425. .name = "bluetooth",
  3426. .read = bluetooth_read,
  3427. .write = bluetooth_write,
  3428. .exit = bluetooth_exit,
  3429. .shutdown = bluetooth_shutdown,
  3430. };
  3431. /*************************************************************************
  3432. * Wan subdriver
  3433. */
  3434. enum {
  3435. /* ACPI GWAN/SWAN bits */
  3436. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  3437. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  3438. TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
  3439. 0 = disable, 1 = enable */
  3440. };
  3441. #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
  3442. static int wan_get_status(void)
  3443. {
  3444. int status;
  3445. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3446. if (dbg_wwanemul)
  3447. return (tpacpi_wwan_emulstate) ?
  3448. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3449. #endif
  3450. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  3451. return -EIO;
  3452. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
  3453. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3454. }
  3455. static int wan_set_status(enum tpacpi_rfkill_state state)
  3456. {
  3457. int status;
  3458. vdbg_printk(TPACPI_DBG_RFKILL,
  3459. "will attempt to %s wwan\n",
  3460. (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
  3461. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3462. if (dbg_wwanemul) {
  3463. tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3464. return 0;
  3465. }
  3466. #endif
  3467. if (state == TPACPI_RFK_RADIO_ON)
  3468. status = TP_ACPI_WANCARD_RADIOSSW
  3469. | TP_ACPI_WANCARD_RESUMECTRL;
  3470. else
  3471. status = 0;
  3472. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  3473. return -EIO;
  3474. return 0;
  3475. }
  3476. /* sysfs wan enable ---------------------------------------------------- */
  3477. static ssize_t wan_enable_show(struct device *dev,
  3478. struct device_attribute *attr,
  3479. char *buf)
  3480. {
  3481. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
  3482. attr, buf);
  3483. }
  3484. static ssize_t wan_enable_store(struct device *dev,
  3485. struct device_attribute *attr,
  3486. const char *buf, size_t count)
  3487. {
  3488. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
  3489. attr, buf, count);
  3490. }
  3491. static struct device_attribute dev_attr_wan_enable =
  3492. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  3493. wan_enable_show, wan_enable_store);
  3494. /* --------------------------------------------------------------------- */
  3495. static struct attribute *wan_attributes[] = {
  3496. &dev_attr_wan_enable.attr,
  3497. NULL
  3498. };
  3499. static const struct attribute_group wan_attr_group = {
  3500. .attrs = wan_attributes,
  3501. };
  3502. static const struct tpacpi_rfk_ops wan_tprfk_ops = {
  3503. .get_status = wan_get_status,
  3504. .set_status = wan_set_status,
  3505. };
  3506. static void wan_shutdown(void)
  3507. {
  3508. /* Order firmware to save current state to NVRAM */
  3509. if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
  3510. TP_ACPI_WGSV_SAVE_STATE))
  3511. printk(TPACPI_NOTICE
  3512. "failed to save WWAN state to NVRAM\n");
  3513. else
  3514. vdbg_printk(TPACPI_DBG_RFKILL,
  3515. "WWAN state saved to NVRAM\n");
  3516. }
  3517. static void wan_exit(void)
  3518. {
  3519. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  3520. &wan_attr_group);
  3521. tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
  3522. wan_shutdown();
  3523. }
  3524. static int __init wan_init(struct ibm_init_struct *iibm)
  3525. {
  3526. int res;
  3527. int status = 0;
  3528. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3529. "initializing wan subdriver\n");
  3530. TPACPI_ACPIHANDLE_INIT(hkey);
  3531. tp_features.wan = hkey_handle &&
  3532. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  3533. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3534. "wan is %s, status 0x%02x\n",
  3535. str_supported(tp_features.wan),
  3536. status);
  3537. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3538. if (dbg_wwanemul) {
  3539. tp_features.wan = 1;
  3540. printk(TPACPI_INFO
  3541. "wwan switch emulation enabled\n");
  3542. } else
  3543. #endif
  3544. if (tp_features.wan &&
  3545. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  3546. /* no wan hardware present in system */
  3547. tp_features.wan = 0;
  3548. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3549. "wan hardware not installed\n");
  3550. }
  3551. if (!tp_features.wan)
  3552. return 1;
  3553. res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
  3554. &wan_tprfk_ops,
  3555. RFKILL_TYPE_WWAN,
  3556. TPACPI_RFK_WWAN_SW_NAME,
  3557. true);
  3558. if (res)
  3559. return res;
  3560. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  3561. &wan_attr_group);
  3562. if (res) {
  3563. tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
  3564. return res;
  3565. }
  3566. return 0;
  3567. }
  3568. /* procfs -------------------------------------------------------------- */
  3569. static int wan_read(struct seq_file *m)
  3570. {
  3571. return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
  3572. }
  3573. static int wan_write(char *buf)
  3574. {
  3575. return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
  3576. }
  3577. static struct ibm_struct wan_driver_data = {
  3578. .name = "wan",
  3579. .read = wan_read,
  3580. .write = wan_write,
  3581. .exit = wan_exit,
  3582. .shutdown = wan_shutdown,
  3583. };
  3584. /*************************************************************************
  3585. * UWB subdriver
  3586. */
  3587. enum {
  3588. /* ACPI GUWB/SUWB bits */
  3589. TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
  3590. TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
  3591. };
  3592. #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
  3593. static int uwb_get_status(void)
  3594. {
  3595. int status;
  3596. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3597. if (dbg_uwbemul)
  3598. return (tpacpi_uwb_emulstate) ?
  3599. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3600. #endif
  3601. if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
  3602. return -EIO;
  3603. return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
  3604. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3605. }
  3606. static int uwb_set_status(enum tpacpi_rfkill_state state)
  3607. {
  3608. int status;
  3609. vdbg_printk(TPACPI_DBG_RFKILL,
  3610. "will attempt to %s UWB\n",
  3611. (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
  3612. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3613. if (dbg_uwbemul) {
  3614. tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3615. return 0;
  3616. }
  3617. #endif
  3618. if (state == TPACPI_RFK_RADIO_ON)
  3619. status = TP_ACPI_UWB_RADIOSSW;
  3620. else
  3621. status = 0;
  3622. if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
  3623. return -EIO;
  3624. return 0;
  3625. }
  3626. /* --------------------------------------------------------------------- */
  3627. static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
  3628. .get_status = uwb_get_status,
  3629. .set_status = uwb_set_status,
  3630. };
  3631. static void uwb_exit(void)
  3632. {
  3633. tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
  3634. }
  3635. static int __init uwb_init(struct ibm_init_struct *iibm)
  3636. {
  3637. int res;
  3638. int status = 0;
  3639. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3640. "initializing uwb subdriver\n");
  3641. TPACPI_ACPIHANDLE_INIT(hkey);
  3642. tp_features.uwb = hkey_handle &&
  3643. acpi_evalf(hkey_handle, &status, "GUWB", "qd");
  3644. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3645. "uwb is %s, status 0x%02x\n",
  3646. str_supported(tp_features.uwb),
  3647. status);
  3648. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3649. if (dbg_uwbemul) {
  3650. tp_features.uwb = 1;
  3651. printk(TPACPI_INFO
  3652. "uwb switch emulation enabled\n");
  3653. } else
  3654. #endif
  3655. if (tp_features.uwb &&
  3656. !(status & TP_ACPI_UWB_HWPRESENT)) {
  3657. /* no uwb hardware present in system */
  3658. tp_features.uwb = 0;
  3659. dbg_printk(TPACPI_DBG_INIT,
  3660. "uwb hardware not installed\n");
  3661. }
  3662. if (!tp_features.uwb)
  3663. return 1;
  3664. res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
  3665. &uwb_tprfk_ops,
  3666. RFKILL_TYPE_UWB,
  3667. TPACPI_RFK_UWB_SW_NAME,
  3668. false);
  3669. return res;
  3670. }
  3671. static struct ibm_struct uwb_driver_data = {
  3672. .name = "uwb",
  3673. .exit = uwb_exit,
  3674. .flags.experimental = 1,
  3675. };
  3676. /*************************************************************************
  3677. * Video subdriver
  3678. */
  3679. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  3680. enum video_access_mode {
  3681. TPACPI_VIDEO_NONE = 0,
  3682. TPACPI_VIDEO_570, /* 570 */
  3683. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  3684. TPACPI_VIDEO_NEW, /* all others */
  3685. };
  3686. enum { /* video status flags, based on VIDEO_570 */
  3687. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  3688. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  3689. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  3690. };
  3691. enum { /* TPACPI_VIDEO_570 constants */
  3692. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  3693. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  3694. * video_status_flags */
  3695. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  3696. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  3697. };
  3698. static enum video_access_mode video_supported;
  3699. static int video_orig_autosw;
  3700. static int video_autosw_get(void);
  3701. static int video_autosw_set(int enable);
  3702. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  3703. static int __init video_init(struct ibm_init_struct *iibm)
  3704. {
  3705. int ivga;
  3706. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  3707. TPACPI_ACPIHANDLE_INIT(vid);
  3708. if (tpacpi_is_ibm())
  3709. TPACPI_ACPIHANDLE_INIT(vid2);
  3710. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  3711. /* G41, assume IVGA doesn't change */
  3712. vid_handle = vid2_handle;
  3713. if (!vid_handle)
  3714. /* video switching not supported on R30, R31 */
  3715. video_supported = TPACPI_VIDEO_NONE;
  3716. else if (tpacpi_is_ibm() &&
  3717. acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  3718. /* 570 */
  3719. video_supported = TPACPI_VIDEO_570;
  3720. else if (tpacpi_is_ibm() &&
  3721. acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  3722. /* 600e/x, 770e, 770x */
  3723. video_supported = TPACPI_VIDEO_770;
  3724. else
  3725. /* all others */
  3726. video_supported = TPACPI_VIDEO_NEW;
  3727. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  3728. str_supported(video_supported != TPACPI_VIDEO_NONE),
  3729. video_supported);
  3730. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  3731. }
  3732. static void video_exit(void)
  3733. {
  3734. dbg_printk(TPACPI_DBG_EXIT,
  3735. "restoring original video autoswitch mode\n");
  3736. if (video_autosw_set(video_orig_autosw))
  3737. printk(TPACPI_ERR "error while trying to restore original "
  3738. "video autoswitch mode\n");
  3739. }
  3740. static int video_outputsw_get(void)
  3741. {
  3742. int status = 0;
  3743. int i;
  3744. switch (video_supported) {
  3745. case TPACPI_VIDEO_570:
  3746. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  3747. TP_ACPI_VIDEO_570_PHSCMD))
  3748. return -EIO;
  3749. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  3750. break;
  3751. case TPACPI_VIDEO_770:
  3752. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  3753. return -EIO;
  3754. if (i)
  3755. status |= TP_ACPI_VIDEO_S_LCD;
  3756. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  3757. return -EIO;
  3758. if (i)
  3759. status |= TP_ACPI_VIDEO_S_CRT;
  3760. break;
  3761. case TPACPI_VIDEO_NEW:
  3762. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  3763. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  3764. return -EIO;
  3765. if (i)
  3766. status |= TP_ACPI_VIDEO_S_CRT;
  3767. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  3768. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  3769. return -EIO;
  3770. if (i)
  3771. status |= TP_ACPI_VIDEO_S_LCD;
  3772. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  3773. return -EIO;
  3774. if (i)
  3775. status |= TP_ACPI_VIDEO_S_DVI;
  3776. break;
  3777. default:
  3778. return -ENOSYS;
  3779. }
  3780. return status;
  3781. }
  3782. static int video_outputsw_set(int status)
  3783. {
  3784. int autosw;
  3785. int res = 0;
  3786. switch (video_supported) {
  3787. case TPACPI_VIDEO_570:
  3788. res = acpi_evalf(NULL, NULL,
  3789. "\\_SB.PHS2", "vdd",
  3790. TP_ACPI_VIDEO_570_PHS2CMD,
  3791. status | TP_ACPI_VIDEO_570_PHS2SET);
  3792. break;
  3793. case TPACPI_VIDEO_770:
  3794. autosw = video_autosw_get();
  3795. if (autosw < 0)
  3796. return autosw;
  3797. res = video_autosw_set(1);
  3798. if (res)
  3799. return res;
  3800. res = acpi_evalf(vid_handle, NULL,
  3801. "ASWT", "vdd", status * 0x100, 0);
  3802. if (!autosw && video_autosw_set(autosw)) {
  3803. printk(TPACPI_ERR
  3804. "video auto-switch left enabled due to error\n");
  3805. return -EIO;
  3806. }
  3807. break;
  3808. case TPACPI_VIDEO_NEW:
  3809. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  3810. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  3811. break;
  3812. default:
  3813. return -ENOSYS;
  3814. }
  3815. return (res)? 0 : -EIO;
  3816. }
  3817. static int video_autosw_get(void)
  3818. {
  3819. int autosw = 0;
  3820. switch (video_supported) {
  3821. case TPACPI_VIDEO_570:
  3822. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  3823. return -EIO;
  3824. break;
  3825. case TPACPI_VIDEO_770:
  3826. case TPACPI_VIDEO_NEW:
  3827. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  3828. return -EIO;
  3829. break;
  3830. default:
  3831. return -ENOSYS;
  3832. }
  3833. return autosw & 1;
  3834. }
  3835. static int video_autosw_set(int enable)
  3836. {
  3837. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  3838. return -EIO;
  3839. return 0;
  3840. }
  3841. static int video_outputsw_cycle(void)
  3842. {
  3843. int autosw = video_autosw_get();
  3844. int res;
  3845. if (autosw < 0)
  3846. return autosw;
  3847. switch (video_supported) {
  3848. case TPACPI_VIDEO_570:
  3849. res = video_autosw_set(1);
  3850. if (res)
  3851. return res;
  3852. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  3853. break;
  3854. case TPACPI_VIDEO_770:
  3855. case TPACPI_VIDEO_NEW:
  3856. res = video_autosw_set(1);
  3857. if (res)
  3858. return res;
  3859. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  3860. break;
  3861. default:
  3862. return -ENOSYS;
  3863. }
  3864. if (!autosw && video_autosw_set(autosw)) {
  3865. printk(TPACPI_ERR
  3866. "video auto-switch left enabled due to error\n");
  3867. return -EIO;
  3868. }
  3869. return (res)? 0 : -EIO;
  3870. }
  3871. static int video_expand_toggle(void)
  3872. {
  3873. switch (video_supported) {
  3874. case TPACPI_VIDEO_570:
  3875. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  3876. 0 : -EIO;
  3877. case TPACPI_VIDEO_770:
  3878. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  3879. 0 : -EIO;
  3880. case TPACPI_VIDEO_NEW:
  3881. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  3882. 0 : -EIO;
  3883. default:
  3884. return -ENOSYS;
  3885. }
  3886. /* not reached */
  3887. }
  3888. static int video_read(struct seq_file *m)
  3889. {
  3890. int status, autosw;
  3891. if (video_supported == TPACPI_VIDEO_NONE) {
  3892. seq_printf(m, "status:\t\tnot supported\n");
  3893. return 0;
  3894. }
  3895. /* Even reads can crash X.org, so... */
  3896. if (!capable(CAP_SYS_ADMIN))
  3897. return -EPERM;
  3898. status = video_outputsw_get();
  3899. if (status < 0)
  3900. return status;
  3901. autosw = video_autosw_get();
  3902. if (autosw < 0)
  3903. return autosw;
  3904. seq_printf(m, "status:\t\tsupported\n");
  3905. seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
  3906. seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
  3907. if (video_supported == TPACPI_VIDEO_NEW)
  3908. seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
  3909. seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
  3910. seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
  3911. seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
  3912. if (video_supported == TPACPI_VIDEO_NEW)
  3913. seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
  3914. seq_printf(m, "commands:\tauto_enable, auto_disable\n");
  3915. seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
  3916. return 0;
  3917. }
  3918. static int video_write(char *buf)
  3919. {
  3920. char *cmd;
  3921. int enable, disable, status;
  3922. int res;
  3923. if (video_supported == TPACPI_VIDEO_NONE)
  3924. return -ENODEV;
  3925. /* Even reads can crash X.org, let alone writes... */
  3926. if (!capable(CAP_SYS_ADMIN))
  3927. return -EPERM;
  3928. enable = 0;
  3929. disable = 0;
  3930. while ((cmd = next_cmd(&buf))) {
  3931. if (strlencmp(cmd, "lcd_enable") == 0) {
  3932. enable |= TP_ACPI_VIDEO_S_LCD;
  3933. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  3934. disable |= TP_ACPI_VIDEO_S_LCD;
  3935. } else if (strlencmp(cmd, "crt_enable") == 0) {
  3936. enable |= TP_ACPI_VIDEO_S_CRT;
  3937. } else if (strlencmp(cmd, "crt_disable") == 0) {
  3938. disable |= TP_ACPI_VIDEO_S_CRT;
  3939. } else if (video_supported == TPACPI_VIDEO_NEW &&
  3940. strlencmp(cmd, "dvi_enable") == 0) {
  3941. enable |= TP_ACPI_VIDEO_S_DVI;
  3942. } else if (video_supported == TPACPI_VIDEO_NEW &&
  3943. strlencmp(cmd, "dvi_disable") == 0) {
  3944. disable |= TP_ACPI_VIDEO_S_DVI;
  3945. } else if (strlencmp(cmd, "auto_enable") == 0) {
  3946. res = video_autosw_set(1);
  3947. if (res)
  3948. return res;
  3949. } else if (strlencmp(cmd, "auto_disable") == 0) {
  3950. res = video_autosw_set(0);
  3951. if (res)
  3952. return res;
  3953. } else if (strlencmp(cmd, "video_switch") == 0) {
  3954. res = video_outputsw_cycle();
  3955. if (res)
  3956. return res;
  3957. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  3958. res = video_expand_toggle();
  3959. if (res)
  3960. return res;
  3961. } else
  3962. return -EINVAL;
  3963. }
  3964. if (enable || disable) {
  3965. status = video_outputsw_get();
  3966. if (status < 0)
  3967. return status;
  3968. res = video_outputsw_set((status & ~disable) | enable);
  3969. if (res)
  3970. return res;
  3971. }
  3972. return 0;
  3973. }
  3974. static struct ibm_struct video_driver_data = {
  3975. .name = "video",
  3976. .read = video_read,
  3977. .write = video_write,
  3978. .exit = video_exit,
  3979. };
  3980. #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
  3981. /*************************************************************************
  3982. * Light (thinklight) subdriver
  3983. */
  3984. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  3985. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  3986. static int light_get_status(void)
  3987. {
  3988. int status = 0;
  3989. if (tp_features.light_status) {
  3990. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  3991. return -EIO;
  3992. return (!!status);
  3993. }
  3994. return -ENXIO;
  3995. }
  3996. static int light_set_status(int status)
  3997. {
  3998. int rc;
  3999. if (tp_features.light) {
  4000. if (cmos_handle) {
  4001. rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
  4002. (status)?
  4003. TP_CMOS_THINKLIGHT_ON :
  4004. TP_CMOS_THINKLIGHT_OFF);
  4005. } else {
  4006. rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
  4007. (status)? 1 : 0);
  4008. }
  4009. return (rc)? 0 : -EIO;
  4010. }
  4011. return -ENXIO;
  4012. }
  4013. static void light_set_status_worker(struct work_struct *work)
  4014. {
  4015. struct tpacpi_led_classdev *data =
  4016. container_of(work, struct tpacpi_led_classdev, work);
  4017. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  4018. light_set_status((data->new_state != TPACPI_LED_OFF));
  4019. }
  4020. static void light_sysfs_set(struct led_classdev *led_cdev,
  4021. enum led_brightness brightness)
  4022. {
  4023. struct tpacpi_led_classdev *data =
  4024. container_of(led_cdev,
  4025. struct tpacpi_led_classdev,
  4026. led_classdev);
  4027. data->new_state = (brightness != LED_OFF) ?
  4028. TPACPI_LED_ON : TPACPI_LED_OFF;
  4029. queue_work(tpacpi_wq, &data->work);
  4030. }
  4031. static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
  4032. {
  4033. return (light_get_status() == 1)? LED_FULL : LED_OFF;
  4034. }
  4035. static struct tpacpi_led_classdev tpacpi_led_thinklight = {
  4036. .led_classdev = {
  4037. .name = "tpacpi::thinklight",
  4038. .brightness_set = &light_sysfs_set,
  4039. .brightness_get = &light_sysfs_get,
  4040. }
  4041. };
  4042. static int __init light_init(struct ibm_init_struct *iibm)
  4043. {
  4044. int rc;
  4045. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  4046. if (tpacpi_is_ibm()) {
  4047. TPACPI_ACPIHANDLE_INIT(ledb);
  4048. TPACPI_ACPIHANDLE_INIT(lght);
  4049. }
  4050. TPACPI_ACPIHANDLE_INIT(cmos);
  4051. INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
  4052. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  4053. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  4054. if (tp_features.light)
  4055. /* light status not supported on
  4056. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  4057. tp_features.light_status =
  4058. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  4059. vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
  4060. str_supported(tp_features.light),
  4061. str_supported(tp_features.light_status));
  4062. if (!tp_features.light)
  4063. return 1;
  4064. rc = led_classdev_register(&tpacpi_pdev->dev,
  4065. &tpacpi_led_thinklight.led_classdev);
  4066. if (rc < 0) {
  4067. tp_features.light = 0;
  4068. tp_features.light_status = 0;
  4069. } else {
  4070. rc = 0;
  4071. }
  4072. return rc;
  4073. }
  4074. static void light_exit(void)
  4075. {
  4076. led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
  4077. if (work_pending(&tpacpi_led_thinklight.work))
  4078. flush_workqueue(tpacpi_wq);
  4079. }
  4080. static int light_read(struct seq_file *m)
  4081. {
  4082. int status;
  4083. if (!tp_features.light) {
  4084. seq_printf(m, "status:\t\tnot supported\n");
  4085. } else if (!tp_features.light_status) {
  4086. seq_printf(m, "status:\t\tunknown\n");
  4087. seq_printf(m, "commands:\ton, off\n");
  4088. } else {
  4089. status = light_get_status();
  4090. if (status < 0)
  4091. return status;
  4092. seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
  4093. seq_printf(m, "commands:\ton, off\n");
  4094. }
  4095. return 0;
  4096. }
  4097. static int light_write(char *buf)
  4098. {
  4099. char *cmd;
  4100. int newstatus = 0;
  4101. if (!tp_features.light)
  4102. return -ENODEV;
  4103. while ((cmd = next_cmd(&buf))) {
  4104. if (strlencmp(cmd, "on") == 0) {
  4105. newstatus = 1;
  4106. } else if (strlencmp(cmd, "off") == 0) {
  4107. newstatus = 0;
  4108. } else
  4109. return -EINVAL;
  4110. }
  4111. return light_set_status(newstatus);
  4112. }
  4113. static struct ibm_struct light_driver_data = {
  4114. .name = "light",
  4115. .read = light_read,
  4116. .write = light_write,
  4117. .exit = light_exit,
  4118. };
  4119. /*************************************************************************
  4120. * CMOS subdriver
  4121. */
  4122. /* sysfs cmos_command -------------------------------------------------- */
  4123. static ssize_t cmos_command_store(struct device *dev,
  4124. struct device_attribute *attr,
  4125. const char *buf, size_t count)
  4126. {
  4127. unsigned long cmos_cmd;
  4128. int res;
  4129. if (parse_strtoul(buf, 21, &cmos_cmd))
  4130. return -EINVAL;
  4131. res = issue_thinkpad_cmos_command(cmos_cmd);
  4132. return (res)? res : count;
  4133. }
  4134. static struct device_attribute dev_attr_cmos_command =
  4135. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  4136. /* --------------------------------------------------------------------- */
  4137. static int __init cmos_init(struct ibm_init_struct *iibm)
  4138. {
  4139. int res;
  4140. vdbg_printk(TPACPI_DBG_INIT,
  4141. "initializing cmos commands subdriver\n");
  4142. TPACPI_ACPIHANDLE_INIT(cmos);
  4143. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  4144. str_supported(cmos_handle != NULL));
  4145. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  4146. if (res)
  4147. return res;
  4148. return (cmos_handle)? 0 : 1;
  4149. }
  4150. static void cmos_exit(void)
  4151. {
  4152. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  4153. }
  4154. static int cmos_read(struct seq_file *m)
  4155. {
  4156. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  4157. R30, R31, T20-22, X20-21 */
  4158. if (!cmos_handle)
  4159. seq_printf(m, "status:\t\tnot supported\n");
  4160. else {
  4161. seq_printf(m, "status:\t\tsupported\n");
  4162. seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
  4163. }
  4164. return 0;
  4165. }
  4166. static int cmos_write(char *buf)
  4167. {
  4168. char *cmd;
  4169. int cmos_cmd, res;
  4170. while ((cmd = next_cmd(&buf))) {
  4171. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  4172. cmos_cmd >= 0 && cmos_cmd <= 21) {
  4173. /* cmos_cmd set */
  4174. } else
  4175. return -EINVAL;
  4176. res = issue_thinkpad_cmos_command(cmos_cmd);
  4177. if (res)
  4178. return res;
  4179. }
  4180. return 0;
  4181. }
  4182. static struct ibm_struct cmos_driver_data = {
  4183. .name = "cmos",
  4184. .read = cmos_read,
  4185. .write = cmos_write,
  4186. .exit = cmos_exit,
  4187. };
  4188. /*************************************************************************
  4189. * LED subdriver
  4190. */
  4191. enum led_access_mode {
  4192. TPACPI_LED_NONE = 0,
  4193. TPACPI_LED_570, /* 570 */
  4194. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  4195. TPACPI_LED_NEW, /* all others */
  4196. };
  4197. enum { /* For TPACPI_LED_OLD */
  4198. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  4199. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  4200. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  4201. };
  4202. static enum led_access_mode led_supported;
  4203. static acpi_handle led_handle;
  4204. #define TPACPI_LED_NUMLEDS 16
  4205. static struct tpacpi_led_classdev *tpacpi_leds;
  4206. static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
  4207. static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
  4208. /* there's a limit of 19 chars + NULL before 2.6.26 */
  4209. "tpacpi::power",
  4210. "tpacpi:orange:batt",
  4211. "tpacpi:green:batt",
  4212. "tpacpi::dock_active",
  4213. "tpacpi::bay_active",
  4214. "tpacpi::dock_batt",
  4215. "tpacpi::unknown_led",
  4216. "tpacpi::standby",
  4217. "tpacpi::dock_status1",
  4218. "tpacpi::dock_status2",
  4219. "tpacpi::unknown_led2",
  4220. "tpacpi::unknown_led3",
  4221. "tpacpi::thinkvantage",
  4222. };
  4223. #define TPACPI_SAFE_LEDS 0x1081U
  4224. static inline bool tpacpi_is_led_restricted(const unsigned int led)
  4225. {
  4226. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  4227. return false;
  4228. #else
  4229. return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
  4230. #endif
  4231. }
  4232. static int led_get_status(const unsigned int led)
  4233. {
  4234. int status;
  4235. enum led_status_t led_s;
  4236. switch (led_supported) {
  4237. case TPACPI_LED_570:
  4238. if (!acpi_evalf(ec_handle,
  4239. &status, "GLED", "dd", 1 << led))
  4240. return -EIO;
  4241. led_s = (status == 0)?
  4242. TPACPI_LED_OFF :
  4243. ((status == 1)?
  4244. TPACPI_LED_ON :
  4245. TPACPI_LED_BLINK);
  4246. tpacpi_led_state_cache[led] = led_s;
  4247. return led_s;
  4248. default:
  4249. return -ENXIO;
  4250. }
  4251. /* not reached */
  4252. }
  4253. static int led_set_status(const unsigned int led,
  4254. const enum led_status_t ledstatus)
  4255. {
  4256. /* off, on, blink. Index is led_status_t */
  4257. static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
  4258. static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
  4259. int rc = 0;
  4260. switch (led_supported) {
  4261. case TPACPI_LED_570:
  4262. /* 570 */
  4263. if (unlikely(led > 7))
  4264. return -EINVAL;
  4265. if (unlikely(tpacpi_is_led_restricted(led)))
  4266. return -EPERM;
  4267. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4268. (1 << led), led_sled_arg1[ledstatus]))
  4269. rc = -EIO;
  4270. break;
  4271. case TPACPI_LED_OLD:
  4272. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  4273. if (unlikely(led > 7))
  4274. return -EINVAL;
  4275. if (unlikely(tpacpi_is_led_restricted(led)))
  4276. return -EPERM;
  4277. rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
  4278. if (rc >= 0)
  4279. rc = ec_write(TPACPI_LED_EC_HLBL,
  4280. (ledstatus == TPACPI_LED_BLINK) << led);
  4281. if (rc >= 0)
  4282. rc = ec_write(TPACPI_LED_EC_HLCL,
  4283. (ledstatus != TPACPI_LED_OFF) << led);
  4284. break;
  4285. case TPACPI_LED_NEW:
  4286. /* all others */
  4287. if (unlikely(led >= TPACPI_LED_NUMLEDS))
  4288. return -EINVAL;
  4289. if (unlikely(tpacpi_is_led_restricted(led)))
  4290. return -EPERM;
  4291. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4292. led, led_led_arg1[ledstatus]))
  4293. rc = -EIO;
  4294. break;
  4295. default:
  4296. rc = -ENXIO;
  4297. }
  4298. if (!rc)
  4299. tpacpi_led_state_cache[led] = ledstatus;
  4300. return rc;
  4301. }
  4302. static void led_set_status_worker(struct work_struct *work)
  4303. {
  4304. struct tpacpi_led_classdev *data =
  4305. container_of(work, struct tpacpi_led_classdev, work);
  4306. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  4307. led_set_status(data->led, data->new_state);
  4308. }
  4309. static void led_sysfs_set(struct led_classdev *led_cdev,
  4310. enum led_brightness brightness)
  4311. {
  4312. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4313. struct tpacpi_led_classdev, led_classdev);
  4314. if (brightness == LED_OFF)
  4315. data->new_state = TPACPI_LED_OFF;
  4316. else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
  4317. data->new_state = TPACPI_LED_ON;
  4318. else
  4319. data->new_state = TPACPI_LED_BLINK;
  4320. queue_work(tpacpi_wq, &data->work);
  4321. }
  4322. static int led_sysfs_blink_set(struct led_classdev *led_cdev,
  4323. unsigned long *delay_on, unsigned long *delay_off)
  4324. {
  4325. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4326. struct tpacpi_led_classdev, led_classdev);
  4327. /* Can we choose the flash rate? */
  4328. if (*delay_on == 0 && *delay_off == 0) {
  4329. /* yes. set them to the hardware blink rate (1 Hz) */
  4330. *delay_on = 500; /* ms */
  4331. *delay_off = 500; /* ms */
  4332. } else if ((*delay_on != 500) || (*delay_off != 500))
  4333. return -EINVAL;
  4334. data->new_state = TPACPI_LED_BLINK;
  4335. queue_work(tpacpi_wq, &data->work);
  4336. return 0;
  4337. }
  4338. static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
  4339. {
  4340. int rc;
  4341. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4342. struct tpacpi_led_classdev, led_classdev);
  4343. rc = led_get_status(data->led);
  4344. if (rc == TPACPI_LED_OFF || rc < 0)
  4345. rc = LED_OFF; /* no error handling in led class :( */
  4346. else
  4347. rc = LED_FULL;
  4348. return rc;
  4349. }
  4350. static void led_exit(void)
  4351. {
  4352. unsigned int i;
  4353. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  4354. if (tpacpi_leds[i].led_classdev.name)
  4355. led_classdev_unregister(&tpacpi_leds[i].led_classdev);
  4356. }
  4357. kfree(tpacpi_leds);
  4358. }
  4359. static int __init tpacpi_init_led(unsigned int led)
  4360. {
  4361. int rc;
  4362. tpacpi_leds[led].led = led;
  4363. /* LEDs with no name don't get registered */
  4364. if (!tpacpi_led_names[led])
  4365. return 0;
  4366. tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
  4367. tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
  4368. if (led_supported == TPACPI_LED_570)
  4369. tpacpi_leds[led].led_classdev.brightness_get =
  4370. &led_sysfs_get;
  4371. tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
  4372. INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
  4373. rc = led_classdev_register(&tpacpi_pdev->dev,
  4374. &tpacpi_leds[led].led_classdev);
  4375. if (rc < 0)
  4376. tpacpi_leds[led].led_classdev.name = NULL;
  4377. return rc;
  4378. }
  4379. static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
  4380. TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
  4381. TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
  4382. TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
  4383. TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
  4384. TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
  4385. TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
  4386. TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
  4387. TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
  4388. TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
  4389. TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
  4390. TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
  4391. TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
  4392. TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
  4393. TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
  4394. TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
  4395. TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
  4396. TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
  4397. TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
  4398. TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
  4399. TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
  4400. /* (1) - may have excess leds enabled on MSB */
  4401. /* Defaults (order matters, keep last, don't reorder!) */
  4402. { /* Lenovo */
  4403. .vendor = PCI_VENDOR_ID_LENOVO,
  4404. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  4405. .quirks = 0x1fffU,
  4406. },
  4407. { /* IBM ThinkPads with no EC version string */
  4408. .vendor = PCI_VENDOR_ID_IBM,
  4409. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
  4410. .quirks = 0x00ffU,
  4411. },
  4412. { /* IBM ThinkPads with EC version string */
  4413. .vendor = PCI_VENDOR_ID_IBM,
  4414. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  4415. .quirks = 0x00bfU,
  4416. },
  4417. };
  4418. #undef TPACPI_LEDQ_IBM
  4419. #undef TPACPI_LEDQ_LNV
  4420. static enum led_access_mode __init led_init_detect_mode(void)
  4421. {
  4422. acpi_status status;
  4423. if (tpacpi_is_ibm()) {
  4424. /* 570 */
  4425. status = acpi_get_handle(ec_handle, "SLED", &led_handle);
  4426. if (ACPI_SUCCESS(status))
  4427. return TPACPI_LED_570;
  4428. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  4429. status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
  4430. if (ACPI_SUCCESS(status))
  4431. return TPACPI_LED_OLD;
  4432. }
  4433. /* most others */
  4434. status = acpi_get_handle(ec_handle, "LED", &led_handle);
  4435. if (ACPI_SUCCESS(status))
  4436. return TPACPI_LED_NEW;
  4437. /* R30, R31, and unknown firmwares */
  4438. led_handle = NULL;
  4439. return TPACPI_LED_NONE;
  4440. }
  4441. static int __init led_init(struct ibm_init_struct *iibm)
  4442. {
  4443. unsigned int i;
  4444. int rc;
  4445. unsigned long useful_leds;
  4446. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  4447. led_supported = led_init_detect_mode();
  4448. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  4449. str_supported(led_supported), led_supported);
  4450. if (led_supported == TPACPI_LED_NONE)
  4451. return 1;
  4452. tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
  4453. GFP_KERNEL);
  4454. if (!tpacpi_leds) {
  4455. printk(TPACPI_ERR "Out of memory for LED data\n");
  4456. return -ENOMEM;
  4457. }
  4458. useful_leds = tpacpi_check_quirks(led_useful_qtable,
  4459. ARRAY_SIZE(led_useful_qtable));
  4460. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  4461. if (!tpacpi_is_led_restricted(i) &&
  4462. test_bit(i, &useful_leds)) {
  4463. rc = tpacpi_init_led(i);
  4464. if (rc < 0) {
  4465. led_exit();
  4466. return rc;
  4467. }
  4468. }
  4469. }
  4470. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  4471. printk(TPACPI_NOTICE
  4472. "warning: userspace override of important "
  4473. "firmware LEDs is enabled\n");
  4474. #endif
  4475. return 0;
  4476. }
  4477. #define str_led_status(s) \
  4478. ((s) == TPACPI_LED_OFF ? "off" : \
  4479. ((s) == TPACPI_LED_ON ? "on" : "blinking"))
  4480. static int led_read(struct seq_file *m)
  4481. {
  4482. if (!led_supported) {
  4483. seq_printf(m, "status:\t\tnot supported\n");
  4484. return 0;
  4485. }
  4486. seq_printf(m, "status:\t\tsupported\n");
  4487. if (led_supported == TPACPI_LED_570) {
  4488. /* 570 */
  4489. int i, status;
  4490. for (i = 0; i < 8; i++) {
  4491. status = led_get_status(i);
  4492. if (status < 0)
  4493. return -EIO;
  4494. seq_printf(m, "%d:\t\t%s\n",
  4495. i, str_led_status(status));
  4496. }
  4497. }
  4498. seq_printf(m, "commands:\t"
  4499. "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
  4500. return 0;
  4501. }
  4502. static int led_write(char *buf)
  4503. {
  4504. char *cmd;
  4505. int led, rc;
  4506. enum led_status_t s;
  4507. if (!led_supported)
  4508. return -ENODEV;
  4509. while ((cmd = next_cmd(&buf))) {
  4510. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
  4511. return -EINVAL;
  4512. if (strstr(cmd, "off")) {
  4513. s = TPACPI_LED_OFF;
  4514. } else if (strstr(cmd, "on")) {
  4515. s = TPACPI_LED_ON;
  4516. } else if (strstr(cmd, "blink")) {
  4517. s = TPACPI_LED_BLINK;
  4518. } else {
  4519. return -EINVAL;
  4520. }
  4521. rc = led_set_status(led, s);
  4522. if (rc < 0)
  4523. return rc;
  4524. }
  4525. return 0;
  4526. }
  4527. static struct ibm_struct led_driver_data = {
  4528. .name = "led",
  4529. .read = led_read,
  4530. .write = led_write,
  4531. .exit = led_exit,
  4532. };
  4533. /*************************************************************************
  4534. * Beep subdriver
  4535. */
  4536. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  4537. #define TPACPI_BEEP_Q1 0x0001
  4538. static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
  4539. TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
  4540. TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
  4541. };
  4542. static int __init beep_init(struct ibm_init_struct *iibm)
  4543. {
  4544. unsigned long quirks;
  4545. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  4546. TPACPI_ACPIHANDLE_INIT(beep);
  4547. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  4548. str_supported(beep_handle != NULL));
  4549. quirks = tpacpi_check_quirks(beep_quirk_table,
  4550. ARRAY_SIZE(beep_quirk_table));
  4551. tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
  4552. return (beep_handle)? 0 : 1;
  4553. }
  4554. static int beep_read(struct seq_file *m)
  4555. {
  4556. if (!beep_handle)
  4557. seq_printf(m, "status:\t\tnot supported\n");
  4558. else {
  4559. seq_printf(m, "status:\t\tsupported\n");
  4560. seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
  4561. }
  4562. return 0;
  4563. }
  4564. static int beep_write(char *buf)
  4565. {
  4566. char *cmd;
  4567. int beep_cmd;
  4568. if (!beep_handle)
  4569. return -ENODEV;
  4570. while ((cmd = next_cmd(&buf))) {
  4571. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  4572. beep_cmd >= 0 && beep_cmd <= 17) {
  4573. /* beep_cmd set */
  4574. } else
  4575. return -EINVAL;
  4576. if (tp_features.beep_needs_two_args) {
  4577. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
  4578. beep_cmd, 0))
  4579. return -EIO;
  4580. } else {
  4581. if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
  4582. beep_cmd))
  4583. return -EIO;
  4584. }
  4585. }
  4586. return 0;
  4587. }
  4588. static struct ibm_struct beep_driver_data = {
  4589. .name = "beep",
  4590. .read = beep_read,
  4591. .write = beep_write,
  4592. };
  4593. /*************************************************************************
  4594. * Thermal subdriver
  4595. */
  4596. enum thermal_access_mode {
  4597. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  4598. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  4599. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  4600. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  4601. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  4602. };
  4603. enum { /* TPACPI_THERMAL_TPEC_* */
  4604. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  4605. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  4606. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  4607. TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
  4608. };
  4609. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  4610. struct ibm_thermal_sensors_struct {
  4611. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  4612. };
  4613. static enum thermal_access_mode thermal_read_mode;
  4614. /* idx is zero-based */
  4615. static int thermal_get_sensor(int idx, s32 *value)
  4616. {
  4617. int t;
  4618. s8 tmp;
  4619. char tmpi[5];
  4620. t = TP_EC_THERMAL_TMP0;
  4621. switch (thermal_read_mode) {
  4622. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  4623. case TPACPI_THERMAL_TPEC_16:
  4624. if (idx >= 8 && idx <= 15) {
  4625. t = TP_EC_THERMAL_TMP8;
  4626. idx -= 8;
  4627. }
  4628. /* fallthrough */
  4629. #endif
  4630. case TPACPI_THERMAL_TPEC_8:
  4631. if (idx <= 7) {
  4632. if (!acpi_ec_read(t + idx, &tmp))
  4633. return -EIO;
  4634. *value = tmp * 1000;
  4635. return 0;
  4636. }
  4637. break;
  4638. case TPACPI_THERMAL_ACPI_UPDT:
  4639. if (idx <= 7) {
  4640. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  4641. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  4642. return -EIO;
  4643. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  4644. return -EIO;
  4645. *value = (t - 2732) * 100;
  4646. return 0;
  4647. }
  4648. break;
  4649. case TPACPI_THERMAL_ACPI_TMP07:
  4650. if (idx <= 7) {
  4651. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  4652. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  4653. return -EIO;
  4654. if (t > 127 || t < -127)
  4655. t = TP_EC_THERMAL_TMP_NA;
  4656. *value = t * 1000;
  4657. return 0;
  4658. }
  4659. break;
  4660. case TPACPI_THERMAL_NONE:
  4661. default:
  4662. return -ENOSYS;
  4663. }
  4664. return -EINVAL;
  4665. }
  4666. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  4667. {
  4668. int res, i;
  4669. int n;
  4670. n = 8;
  4671. i = 0;
  4672. if (!s)
  4673. return -EINVAL;
  4674. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  4675. n = 16;
  4676. for (i = 0 ; i < n; i++) {
  4677. res = thermal_get_sensor(i, &s->temp[i]);
  4678. if (res)
  4679. return res;
  4680. }
  4681. return n;
  4682. }
  4683. static void thermal_dump_all_sensors(void)
  4684. {
  4685. int n, i;
  4686. struct ibm_thermal_sensors_struct t;
  4687. n = thermal_get_sensors(&t);
  4688. if (n <= 0)
  4689. return;
  4690. printk(TPACPI_NOTICE
  4691. "temperatures (Celsius):");
  4692. for (i = 0; i < n; i++) {
  4693. if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
  4694. printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
  4695. else
  4696. printk(KERN_CONT " N/A");
  4697. }
  4698. printk(KERN_CONT "\n");
  4699. }
  4700. /* sysfs temp##_input -------------------------------------------------- */
  4701. static ssize_t thermal_temp_input_show(struct device *dev,
  4702. struct device_attribute *attr,
  4703. char *buf)
  4704. {
  4705. struct sensor_device_attribute *sensor_attr =
  4706. to_sensor_dev_attr(attr);
  4707. int idx = sensor_attr->index;
  4708. s32 value;
  4709. int res;
  4710. res = thermal_get_sensor(idx, &value);
  4711. if (res)
  4712. return res;
  4713. if (value == TPACPI_THERMAL_SENSOR_NA)
  4714. return -ENXIO;
  4715. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  4716. }
  4717. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  4718. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  4719. thermal_temp_input_show, NULL, _idxB)
  4720. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  4721. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  4722. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  4723. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  4724. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  4725. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  4726. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  4727. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  4728. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  4729. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  4730. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  4731. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  4732. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  4733. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  4734. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  4735. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  4736. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  4737. };
  4738. #define THERMAL_ATTRS(X) \
  4739. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  4740. static struct attribute *thermal_temp_input_attr[] = {
  4741. THERMAL_ATTRS(8),
  4742. THERMAL_ATTRS(9),
  4743. THERMAL_ATTRS(10),
  4744. THERMAL_ATTRS(11),
  4745. THERMAL_ATTRS(12),
  4746. THERMAL_ATTRS(13),
  4747. THERMAL_ATTRS(14),
  4748. THERMAL_ATTRS(15),
  4749. THERMAL_ATTRS(0),
  4750. THERMAL_ATTRS(1),
  4751. THERMAL_ATTRS(2),
  4752. THERMAL_ATTRS(3),
  4753. THERMAL_ATTRS(4),
  4754. THERMAL_ATTRS(5),
  4755. THERMAL_ATTRS(6),
  4756. THERMAL_ATTRS(7),
  4757. NULL
  4758. };
  4759. static const struct attribute_group thermal_temp_input16_group = {
  4760. .attrs = thermal_temp_input_attr
  4761. };
  4762. static const struct attribute_group thermal_temp_input8_group = {
  4763. .attrs = &thermal_temp_input_attr[8]
  4764. };
  4765. #undef THERMAL_SENSOR_ATTR_TEMP
  4766. #undef THERMAL_ATTRS
  4767. /* --------------------------------------------------------------------- */
  4768. static int __init thermal_init(struct ibm_init_struct *iibm)
  4769. {
  4770. u8 t, ta1, ta2;
  4771. int i;
  4772. int acpi_tmp7;
  4773. int res;
  4774. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  4775. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  4776. if (thinkpad_id.ec_model) {
  4777. /*
  4778. * Direct EC access mode: sensors at registers
  4779. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  4780. * non-implemented, thermal sensors return 0x80 when
  4781. * not available
  4782. */
  4783. ta1 = ta2 = 0;
  4784. for (i = 0; i < 8; i++) {
  4785. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  4786. ta1 |= t;
  4787. } else {
  4788. ta1 = 0;
  4789. break;
  4790. }
  4791. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  4792. ta2 |= t;
  4793. } else {
  4794. ta1 = 0;
  4795. break;
  4796. }
  4797. }
  4798. if (ta1 == 0) {
  4799. /* This is sheer paranoia, but we handle it anyway */
  4800. if (acpi_tmp7) {
  4801. printk(TPACPI_ERR
  4802. "ThinkPad ACPI EC access misbehaving, "
  4803. "falling back to ACPI TMPx access "
  4804. "mode\n");
  4805. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  4806. } else {
  4807. printk(TPACPI_ERR
  4808. "ThinkPad ACPI EC access misbehaving, "
  4809. "disabling thermal sensors access\n");
  4810. thermal_read_mode = TPACPI_THERMAL_NONE;
  4811. }
  4812. } else {
  4813. thermal_read_mode =
  4814. (ta2 != 0) ?
  4815. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  4816. }
  4817. } else if (acpi_tmp7) {
  4818. if (tpacpi_is_ibm() &&
  4819. acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  4820. /* 600e/x, 770e, 770x */
  4821. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  4822. } else {
  4823. /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
  4824. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  4825. }
  4826. } else {
  4827. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  4828. thermal_read_mode = TPACPI_THERMAL_NONE;
  4829. }
  4830. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  4831. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  4832. thermal_read_mode);
  4833. switch (thermal_read_mode) {
  4834. case TPACPI_THERMAL_TPEC_16:
  4835. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4836. &thermal_temp_input16_group);
  4837. if (res)
  4838. return res;
  4839. break;
  4840. case TPACPI_THERMAL_TPEC_8:
  4841. case TPACPI_THERMAL_ACPI_TMP07:
  4842. case TPACPI_THERMAL_ACPI_UPDT:
  4843. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4844. &thermal_temp_input8_group);
  4845. if (res)
  4846. return res;
  4847. break;
  4848. case TPACPI_THERMAL_NONE:
  4849. default:
  4850. return 1;
  4851. }
  4852. return 0;
  4853. }
  4854. static void thermal_exit(void)
  4855. {
  4856. switch (thermal_read_mode) {
  4857. case TPACPI_THERMAL_TPEC_16:
  4858. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  4859. &thermal_temp_input16_group);
  4860. break;
  4861. case TPACPI_THERMAL_TPEC_8:
  4862. case TPACPI_THERMAL_ACPI_TMP07:
  4863. case TPACPI_THERMAL_ACPI_UPDT:
  4864. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  4865. &thermal_temp_input8_group);
  4866. break;
  4867. case TPACPI_THERMAL_NONE:
  4868. default:
  4869. break;
  4870. }
  4871. }
  4872. static int thermal_read(struct seq_file *m)
  4873. {
  4874. int n, i;
  4875. struct ibm_thermal_sensors_struct t;
  4876. n = thermal_get_sensors(&t);
  4877. if (unlikely(n < 0))
  4878. return n;
  4879. seq_printf(m, "temperatures:\t");
  4880. if (n > 0) {
  4881. for (i = 0; i < (n - 1); i++)
  4882. seq_printf(m, "%d ", t.temp[i] / 1000);
  4883. seq_printf(m, "%d\n", t.temp[i] / 1000);
  4884. } else
  4885. seq_printf(m, "not supported\n");
  4886. return 0;
  4887. }
  4888. static struct ibm_struct thermal_driver_data = {
  4889. .name = "thermal",
  4890. .read = thermal_read,
  4891. .exit = thermal_exit,
  4892. };
  4893. /*************************************************************************
  4894. * Backlight/brightness subdriver
  4895. */
  4896. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  4897. /*
  4898. * ThinkPads can read brightness from two places: EC HBRV (0x31), or
  4899. * CMOS NVRAM byte 0x5E, bits 0-3.
  4900. *
  4901. * EC HBRV (0x31) has the following layout
  4902. * Bit 7: unknown function
  4903. * Bit 6: unknown function
  4904. * Bit 5: Z: honour scale changes, NZ: ignore scale changes
  4905. * Bit 4: must be set to zero to avoid problems
  4906. * Bit 3-0: backlight brightness level
  4907. *
  4908. * brightness_get_raw returns status data in the HBRV layout
  4909. *
  4910. * WARNING: The X61 has been verified to use HBRV for something else, so
  4911. * this should be used _only_ on IBM ThinkPads, and maybe with some careful
  4912. * testing on the very early *60 Lenovo models...
  4913. */
  4914. enum {
  4915. TP_EC_BACKLIGHT = 0x31,
  4916. /* TP_EC_BACKLIGHT bitmasks */
  4917. TP_EC_BACKLIGHT_LVLMSK = 0x1F,
  4918. TP_EC_BACKLIGHT_CMDMSK = 0xE0,
  4919. TP_EC_BACKLIGHT_MAPSW = 0x20,
  4920. };
  4921. enum tpacpi_brightness_access_mode {
  4922. TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
  4923. TPACPI_BRGHT_MODE_EC, /* EC control */
  4924. TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
  4925. TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  4926. TPACPI_BRGHT_MODE_MAX
  4927. };
  4928. static struct backlight_device *ibm_backlight_device;
  4929. static enum tpacpi_brightness_access_mode brightness_mode =
  4930. TPACPI_BRGHT_MODE_MAX;
  4931. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  4932. static struct mutex brightness_mutex;
  4933. /* NVRAM brightness access,
  4934. * call with brightness_mutex held! */
  4935. static unsigned int tpacpi_brightness_nvram_get(void)
  4936. {
  4937. u8 lnvram;
  4938. lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  4939. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  4940. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  4941. lnvram &= bright_maxlvl;
  4942. return lnvram;
  4943. }
  4944. static void tpacpi_brightness_checkpoint_nvram(void)
  4945. {
  4946. u8 lec = 0;
  4947. u8 b_nvram;
  4948. if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
  4949. return;
  4950. vdbg_printk(TPACPI_DBG_BRGHT,
  4951. "trying to checkpoint backlight level to NVRAM...\n");
  4952. if (mutex_lock_killable(&brightness_mutex) < 0)
  4953. return;
  4954. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  4955. goto unlock;
  4956. lec &= TP_EC_BACKLIGHT_LVLMSK;
  4957. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  4958. if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  4959. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
  4960. /* NVRAM needs update */
  4961. b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
  4962. TP_NVRAM_POS_LEVEL_BRIGHTNESS);
  4963. b_nvram |= lec;
  4964. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
  4965. dbg_printk(TPACPI_DBG_BRGHT,
  4966. "updated NVRAM backlight level to %u (0x%02x)\n",
  4967. (unsigned int) lec, (unsigned int) b_nvram);
  4968. } else
  4969. vdbg_printk(TPACPI_DBG_BRGHT,
  4970. "NVRAM backlight level already is %u (0x%02x)\n",
  4971. (unsigned int) lec, (unsigned int) b_nvram);
  4972. unlock:
  4973. mutex_unlock(&brightness_mutex);
  4974. }
  4975. /* call with brightness_mutex held! */
  4976. static int tpacpi_brightness_get_raw(int *status)
  4977. {
  4978. u8 lec = 0;
  4979. switch (brightness_mode) {
  4980. case TPACPI_BRGHT_MODE_UCMS_STEP:
  4981. *status = tpacpi_brightness_nvram_get();
  4982. return 0;
  4983. case TPACPI_BRGHT_MODE_EC:
  4984. case TPACPI_BRGHT_MODE_ECNVRAM:
  4985. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  4986. return -EIO;
  4987. *status = lec;
  4988. return 0;
  4989. default:
  4990. return -ENXIO;
  4991. }
  4992. }
  4993. /* call with brightness_mutex held! */
  4994. /* do NOT call with illegal backlight level value */
  4995. static int tpacpi_brightness_set_ec(unsigned int value)
  4996. {
  4997. u8 lec = 0;
  4998. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  4999. return -EIO;
  5000. if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
  5001. (lec & TP_EC_BACKLIGHT_CMDMSK) |
  5002. (value & TP_EC_BACKLIGHT_LVLMSK))))
  5003. return -EIO;
  5004. return 0;
  5005. }
  5006. /* call with brightness_mutex held! */
  5007. static int tpacpi_brightness_set_ucmsstep(unsigned int value)
  5008. {
  5009. int cmos_cmd, inc;
  5010. unsigned int current_value, i;
  5011. current_value = tpacpi_brightness_nvram_get();
  5012. if (value == current_value)
  5013. return 0;
  5014. cmos_cmd = (value > current_value) ?
  5015. TP_CMOS_BRIGHTNESS_UP :
  5016. TP_CMOS_BRIGHTNESS_DOWN;
  5017. inc = (value > current_value) ? 1 : -1;
  5018. for (i = current_value; i != value; i += inc)
  5019. if (issue_thinkpad_cmos_command(cmos_cmd))
  5020. return -EIO;
  5021. return 0;
  5022. }
  5023. /* May return EINTR which can always be mapped to ERESTARTSYS */
  5024. static int brightness_set(unsigned int value)
  5025. {
  5026. int res;
  5027. if (value > bright_maxlvl || value < 0)
  5028. return -EINVAL;
  5029. vdbg_printk(TPACPI_DBG_BRGHT,
  5030. "set backlight level to %d\n", value);
  5031. res = mutex_lock_killable(&brightness_mutex);
  5032. if (res < 0)
  5033. return res;
  5034. switch (brightness_mode) {
  5035. case TPACPI_BRGHT_MODE_EC:
  5036. case TPACPI_BRGHT_MODE_ECNVRAM:
  5037. res = tpacpi_brightness_set_ec(value);
  5038. break;
  5039. case TPACPI_BRGHT_MODE_UCMS_STEP:
  5040. res = tpacpi_brightness_set_ucmsstep(value);
  5041. break;
  5042. default:
  5043. res = -ENXIO;
  5044. }
  5045. mutex_unlock(&brightness_mutex);
  5046. return res;
  5047. }
  5048. /* sysfs backlight class ----------------------------------------------- */
  5049. static int brightness_update_status(struct backlight_device *bd)
  5050. {
  5051. unsigned int level =
  5052. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  5053. bd->props.power == FB_BLANK_UNBLANK) ?
  5054. bd->props.brightness : 0;
  5055. dbg_printk(TPACPI_DBG_BRGHT,
  5056. "backlight: attempt to set level to %d\n",
  5057. level);
  5058. /* it is the backlight class's job (caller) to handle
  5059. * EINTR and other errors properly */
  5060. return brightness_set(level);
  5061. }
  5062. static int brightness_get(struct backlight_device *bd)
  5063. {
  5064. int status, res;
  5065. res = mutex_lock_killable(&brightness_mutex);
  5066. if (res < 0)
  5067. return 0;
  5068. res = tpacpi_brightness_get_raw(&status);
  5069. mutex_unlock(&brightness_mutex);
  5070. if (res < 0)
  5071. return 0;
  5072. return status & TP_EC_BACKLIGHT_LVLMSK;
  5073. }
  5074. static void tpacpi_brightness_notify_change(void)
  5075. {
  5076. backlight_force_update(ibm_backlight_device,
  5077. BACKLIGHT_UPDATE_HOTKEY);
  5078. }
  5079. static const struct backlight_ops ibm_backlight_data = {
  5080. .get_brightness = brightness_get,
  5081. .update_status = brightness_update_status,
  5082. };
  5083. /* --------------------------------------------------------------------- */
  5084. /*
  5085. * Call _BCL method of video device. On some ThinkPads this will
  5086. * switch the firmware to the ACPI brightness control mode.
  5087. */
  5088. static int __init tpacpi_query_bcl_levels(acpi_handle handle)
  5089. {
  5090. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  5091. union acpi_object *obj;
  5092. int rc;
  5093. if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
  5094. obj = (union acpi_object *)buffer.pointer;
  5095. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  5096. printk(TPACPI_ERR "Unknown _BCL data, "
  5097. "please report this to %s\n", TPACPI_MAIL);
  5098. rc = 0;
  5099. } else {
  5100. rc = obj->package.count;
  5101. }
  5102. } else {
  5103. return 0;
  5104. }
  5105. kfree(buffer.pointer);
  5106. return rc;
  5107. }
  5108. /*
  5109. * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
  5110. */
  5111. static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
  5112. {
  5113. acpi_handle video_device;
  5114. int bcl_levels = 0;
  5115. tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
  5116. if (video_device)
  5117. bcl_levels = tpacpi_query_bcl_levels(video_device);
  5118. tp_features.bright_acpimode = (bcl_levels > 0);
  5119. return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
  5120. }
  5121. /*
  5122. * These are only useful for models that have only one possibility
  5123. * of GPU. If the BIOS model handles both ATI and Intel, don't use
  5124. * these quirks.
  5125. */
  5126. #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
  5127. #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
  5128. #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
  5129. static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
  5130. /* Models with ATI GPUs known to require ECNVRAM mode */
  5131. TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
  5132. /* Models with ATI GPUs that can use ECNVRAM */
  5133. TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
  5134. TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5135. TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
  5136. TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5137. /* Models with Intel Extreme Graphics 2 */
  5138. TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
  5139. TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5140. TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5141. /* Models with Intel GMA900 */
  5142. TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
  5143. TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
  5144. TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
  5145. };
  5146. /*
  5147. * Returns < 0 for error, otherwise sets tp_features.bright_*
  5148. * and bright_maxlvl.
  5149. */
  5150. static void __init tpacpi_detect_brightness_capabilities(void)
  5151. {
  5152. unsigned int b;
  5153. vdbg_printk(TPACPI_DBG_INIT,
  5154. "detecting firmware brightness interface capabilities\n");
  5155. /* we could run a quirks check here (same table used by
  5156. * brightness_init) if needed */
  5157. /*
  5158. * We always attempt to detect acpi support, so as to switch
  5159. * Lenovo Vista BIOS to ACPI brightness mode even if we are not
  5160. * going to publish a backlight interface
  5161. */
  5162. b = tpacpi_check_std_acpi_brightness_support();
  5163. switch (b) {
  5164. case 16:
  5165. bright_maxlvl = 15;
  5166. printk(TPACPI_INFO
  5167. "detected a 16-level brightness capable ThinkPad\n");
  5168. break;
  5169. case 8:
  5170. case 0:
  5171. bright_maxlvl = 7;
  5172. printk(TPACPI_INFO
  5173. "detected a 8-level brightness capable ThinkPad\n");
  5174. break;
  5175. default:
  5176. printk(TPACPI_ERR
  5177. "Unsupported brightness interface, "
  5178. "please contact %s\n", TPACPI_MAIL);
  5179. tp_features.bright_unkfw = 1;
  5180. bright_maxlvl = b - 1;
  5181. }
  5182. }
  5183. static int __init brightness_init(struct ibm_init_struct *iibm)
  5184. {
  5185. struct backlight_properties props;
  5186. int b;
  5187. unsigned long quirks;
  5188. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  5189. mutex_init(&brightness_mutex);
  5190. quirks = tpacpi_check_quirks(brightness_quirk_table,
  5191. ARRAY_SIZE(brightness_quirk_table));
  5192. /* tpacpi_detect_brightness_capabilities() must have run already */
  5193. /* if it is unknown, we don't handle it: it wouldn't be safe */
  5194. if (tp_features.bright_unkfw)
  5195. return 1;
  5196. if (!brightness_enable) {
  5197. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5198. "brightness support disabled by "
  5199. "module parameter\n");
  5200. return 1;
  5201. }
  5202. if (acpi_video_backlight_support()) {
  5203. if (brightness_enable > 1) {
  5204. printk(TPACPI_INFO
  5205. "Standard ACPI backlight interface "
  5206. "available, not loading native one.\n");
  5207. return 1;
  5208. } else if (brightness_enable == 1) {
  5209. printk(TPACPI_WARN
  5210. "Cannot enable backlight brightness support, "
  5211. "ACPI is already handling it. Refer to the "
  5212. "acpi_backlight kernel parameter\n");
  5213. return 1;
  5214. }
  5215. } else if (tp_features.bright_acpimode && brightness_enable > 1) {
  5216. printk(TPACPI_NOTICE
  5217. "Standard ACPI backlight interface not "
  5218. "available, thinkpad_acpi native "
  5219. "brightness control enabled\n");
  5220. }
  5221. /*
  5222. * Check for module parameter bogosity, note that we
  5223. * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
  5224. * able to detect "unspecified"
  5225. */
  5226. if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
  5227. return -EINVAL;
  5228. /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
  5229. if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
  5230. brightness_mode == TPACPI_BRGHT_MODE_MAX) {
  5231. if (quirks & TPACPI_BRGHT_Q_EC)
  5232. brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
  5233. else
  5234. brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
  5235. dbg_printk(TPACPI_DBG_BRGHT,
  5236. "driver auto-selected brightness_mode=%d\n",
  5237. brightness_mode);
  5238. }
  5239. /* Safety */
  5240. if (!tpacpi_is_ibm() &&
  5241. (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
  5242. brightness_mode == TPACPI_BRGHT_MODE_EC))
  5243. return -EINVAL;
  5244. if (tpacpi_brightness_get_raw(&b) < 0)
  5245. return 1;
  5246. memset(&props, 0, sizeof(struct backlight_properties));
  5247. props.type = BACKLIGHT_PLATFORM;
  5248. props.max_brightness = bright_maxlvl;
  5249. props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
  5250. ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
  5251. NULL, NULL,
  5252. &ibm_backlight_data,
  5253. &props);
  5254. if (IS_ERR(ibm_backlight_device)) {
  5255. int rc = PTR_ERR(ibm_backlight_device);
  5256. ibm_backlight_device = NULL;
  5257. printk(TPACPI_ERR "Could not register backlight device\n");
  5258. return rc;
  5259. }
  5260. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5261. "brightness is supported\n");
  5262. if (quirks & TPACPI_BRGHT_Q_ASK) {
  5263. printk(TPACPI_NOTICE
  5264. "brightness: will use unverified default: "
  5265. "brightness_mode=%d\n", brightness_mode);
  5266. printk(TPACPI_NOTICE
  5267. "brightness: please report to %s whether it works well "
  5268. "or not on your ThinkPad\n", TPACPI_MAIL);
  5269. }
  5270. /* Added by mistake in early 2007. Probably useless, but it could
  5271. * be working around some unknown firmware problem where the value
  5272. * read at startup doesn't match the real hardware state... so leave
  5273. * it in place just in case */
  5274. backlight_update_status(ibm_backlight_device);
  5275. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5276. "brightness: registering brightness hotkeys "
  5277. "as change notification\n");
  5278. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
  5279. | TP_ACPI_HKEY_BRGHTUP_MASK
  5280. | TP_ACPI_HKEY_BRGHTDWN_MASK);
  5281. return 0;
  5282. }
  5283. static void brightness_suspend(pm_message_t state)
  5284. {
  5285. tpacpi_brightness_checkpoint_nvram();
  5286. }
  5287. static void brightness_shutdown(void)
  5288. {
  5289. tpacpi_brightness_checkpoint_nvram();
  5290. }
  5291. static void brightness_exit(void)
  5292. {
  5293. if (ibm_backlight_device) {
  5294. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
  5295. "calling backlight_device_unregister()\n");
  5296. backlight_device_unregister(ibm_backlight_device);
  5297. }
  5298. tpacpi_brightness_checkpoint_nvram();
  5299. }
  5300. static int brightness_read(struct seq_file *m)
  5301. {
  5302. int level;
  5303. level = brightness_get(NULL);
  5304. if (level < 0) {
  5305. seq_printf(m, "level:\t\tunreadable\n");
  5306. } else {
  5307. seq_printf(m, "level:\t\t%d\n", level);
  5308. seq_printf(m, "commands:\tup, down\n");
  5309. seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
  5310. bright_maxlvl);
  5311. }
  5312. return 0;
  5313. }
  5314. static int brightness_write(char *buf)
  5315. {
  5316. int level;
  5317. int rc;
  5318. char *cmd;
  5319. level = brightness_get(NULL);
  5320. if (level < 0)
  5321. return level;
  5322. while ((cmd = next_cmd(&buf))) {
  5323. if (strlencmp(cmd, "up") == 0) {
  5324. if (level < bright_maxlvl)
  5325. level++;
  5326. } else if (strlencmp(cmd, "down") == 0) {
  5327. if (level > 0)
  5328. level--;
  5329. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  5330. level >= 0 && level <= bright_maxlvl) {
  5331. /* new level set */
  5332. } else
  5333. return -EINVAL;
  5334. }
  5335. tpacpi_disclose_usertask("procfs brightness",
  5336. "set level to %d\n", level);
  5337. /*
  5338. * Now we know what the final level should be, so we try to set it.
  5339. * Doing it this way makes the syscall restartable in case of EINTR
  5340. */
  5341. rc = brightness_set(level);
  5342. if (!rc && ibm_backlight_device)
  5343. backlight_force_update(ibm_backlight_device,
  5344. BACKLIGHT_UPDATE_SYSFS);
  5345. return (rc == -EINTR)? -ERESTARTSYS : rc;
  5346. }
  5347. static struct ibm_struct brightness_driver_data = {
  5348. .name = "brightness",
  5349. .read = brightness_read,
  5350. .write = brightness_write,
  5351. .exit = brightness_exit,
  5352. .suspend = brightness_suspend,
  5353. .shutdown = brightness_shutdown,
  5354. };
  5355. /*************************************************************************
  5356. * Volume subdriver
  5357. */
  5358. /*
  5359. * IBM ThinkPads have a simple volume controller with MUTE gating.
  5360. * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
  5361. *
  5362. * Since the *61 series (and probably also the later *60 series), Lenovo
  5363. * ThinkPads only implement the MUTE gate.
  5364. *
  5365. * EC register 0x30
  5366. * Bit 6: MUTE (1 mutes sound)
  5367. * Bit 3-0: Volume
  5368. * Other bits should be zero as far as we know.
  5369. *
  5370. * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
  5371. * bits 3-0 (volume). Other bits in NVRAM may have other functions,
  5372. * such as bit 7 which is used to detect repeated presses of MUTE,
  5373. * and we leave them unchanged.
  5374. */
  5375. #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
  5376. #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
  5377. #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
  5378. #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
  5379. static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
  5380. static char *alsa_id = "ThinkPadEC";
  5381. static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
  5382. struct tpacpi_alsa_data {
  5383. struct snd_card *card;
  5384. struct snd_ctl_elem_id *ctl_mute_id;
  5385. struct snd_ctl_elem_id *ctl_vol_id;
  5386. };
  5387. static struct snd_card *alsa_card;
  5388. enum {
  5389. TP_EC_AUDIO = 0x30,
  5390. /* TP_EC_AUDIO bits */
  5391. TP_EC_AUDIO_MUTESW = 6,
  5392. /* TP_EC_AUDIO bitmasks */
  5393. TP_EC_AUDIO_LVL_MSK = 0x0F,
  5394. TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
  5395. /* Maximum volume */
  5396. TP_EC_VOLUME_MAX = 14,
  5397. };
  5398. enum tpacpi_volume_access_mode {
  5399. TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
  5400. TPACPI_VOL_MODE_EC, /* Pure EC control */
  5401. TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
  5402. TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  5403. TPACPI_VOL_MODE_MAX
  5404. };
  5405. enum tpacpi_volume_capabilities {
  5406. TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
  5407. TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
  5408. TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
  5409. TPACPI_VOL_CAP_MAX
  5410. };
  5411. static enum tpacpi_volume_access_mode volume_mode =
  5412. TPACPI_VOL_MODE_MAX;
  5413. static enum tpacpi_volume_capabilities volume_capabilities;
  5414. static int volume_control_allowed;
  5415. /*
  5416. * Used to syncronize writers to TP_EC_AUDIO and
  5417. * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
  5418. */
  5419. static struct mutex volume_mutex;
  5420. static void tpacpi_volume_checkpoint_nvram(void)
  5421. {
  5422. u8 lec = 0;
  5423. u8 b_nvram;
  5424. u8 ec_mask;
  5425. if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
  5426. return;
  5427. if (!volume_control_allowed)
  5428. return;
  5429. vdbg_printk(TPACPI_DBG_MIXER,
  5430. "trying to checkpoint mixer state to NVRAM...\n");
  5431. if (tp_features.mixer_no_level_control)
  5432. ec_mask = TP_EC_AUDIO_MUTESW_MSK;
  5433. else
  5434. ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
  5435. if (mutex_lock_killable(&volume_mutex) < 0)
  5436. return;
  5437. if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
  5438. goto unlock;
  5439. lec &= ec_mask;
  5440. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  5441. if (lec != (b_nvram & ec_mask)) {
  5442. /* NVRAM needs update */
  5443. b_nvram &= ~ec_mask;
  5444. b_nvram |= lec;
  5445. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
  5446. dbg_printk(TPACPI_DBG_MIXER,
  5447. "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
  5448. (unsigned int) lec, (unsigned int) b_nvram);
  5449. } else {
  5450. vdbg_printk(TPACPI_DBG_MIXER,
  5451. "NVRAM mixer status already is 0x%02x (0x%02x)\n",
  5452. (unsigned int) lec, (unsigned int) b_nvram);
  5453. }
  5454. unlock:
  5455. mutex_unlock(&volume_mutex);
  5456. }
  5457. static int volume_get_status_ec(u8 *status)
  5458. {
  5459. u8 s;
  5460. if (!acpi_ec_read(TP_EC_AUDIO, &s))
  5461. return -EIO;
  5462. *status = s;
  5463. dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
  5464. return 0;
  5465. }
  5466. static int volume_get_status(u8 *status)
  5467. {
  5468. return volume_get_status_ec(status);
  5469. }
  5470. static int volume_set_status_ec(const u8 status)
  5471. {
  5472. if (!acpi_ec_write(TP_EC_AUDIO, status))
  5473. return -EIO;
  5474. dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
  5475. return 0;
  5476. }
  5477. static int volume_set_status(const u8 status)
  5478. {
  5479. return volume_set_status_ec(status);
  5480. }
  5481. /* returns < 0 on error, 0 on no change, 1 on change */
  5482. static int __volume_set_mute_ec(const bool mute)
  5483. {
  5484. int rc;
  5485. u8 s, n;
  5486. if (mutex_lock_killable(&volume_mutex) < 0)
  5487. return -EINTR;
  5488. rc = volume_get_status_ec(&s);
  5489. if (rc)
  5490. goto unlock;
  5491. n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
  5492. s & ~TP_EC_AUDIO_MUTESW_MSK;
  5493. if (n != s) {
  5494. rc = volume_set_status_ec(n);
  5495. if (!rc)
  5496. rc = 1;
  5497. }
  5498. unlock:
  5499. mutex_unlock(&volume_mutex);
  5500. return rc;
  5501. }
  5502. static int volume_alsa_set_mute(const bool mute)
  5503. {
  5504. dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
  5505. (mute) ? "" : "un");
  5506. return __volume_set_mute_ec(mute);
  5507. }
  5508. static int volume_set_mute(const bool mute)
  5509. {
  5510. int rc;
  5511. dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
  5512. (mute) ? "" : "un");
  5513. rc = __volume_set_mute_ec(mute);
  5514. return (rc < 0) ? rc : 0;
  5515. }
  5516. /* returns < 0 on error, 0 on no change, 1 on change */
  5517. static int __volume_set_volume_ec(const u8 vol)
  5518. {
  5519. int rc;
  5520. u8 s, n;
  5521. if (vol > TP_EC_VOLUME_MAX)
  5522. return -EINVAL;
  5523. if (mutex_lock_killable(&volume_mutex) < 0)
  5524. return -EINTR;
  5525. rc = volume_get_status_ec(&s);
  5526. if (rc)
  5527. goto unlock;
  5528. n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
  5529. if (n != s) {
  5530. rc = volume_set_status_ec(n);
  5531. if (!rc)
  5532. rc = 1;
  5533. }
  5534. unlock:
  5535. mutex_unlock(&volume_mutex);
  5536. return rc;
  5537. }
  5538. static int volume_alsa_set_volume(const u8 vol)
  5539. {
  5540. dbg_printk(TPACPI_DBG_MIXER,
  5541. "ALSA: trying to set volume level to %hu\n", vol);
  5542. return __volume_set_volume_ec(vol);
  5543. }
  5544. static void volume_alsa_notify_change(void)
  5545. {
  5546. struct tpacpi_alsa_data *d;
  5547. if (alsa_card && alsa_card->private_data) {
  5548. d = alsa_card->private_data;
  5549. if (d->ctl_mute_id)
  5550. snd_ctl_notify(alsa_card,
  5551. SNDRV_CTL_EVENT_MASK_VALUE,
  5552. d->ctl_mute_id);
  5553. if (d->ctl_vol_id)
  5554. snd_ctl_notify(alsa_card,
  5555. SNDRV_CTL_EVENT_MASK_VALUE,
  5556. d->ctl_vol_id);
  5557. }
  5558. }
  5559. static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
  5560. struct snd_ctl_elem_info *uinfo)
  5561. {
  5562. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  5563. uinfo->count = 1;
  5564. uinfo->value.integer.min = 0;
  5565. uinfo->value.integer.max = TP_EC_VOLUME_MAX;
  5566. return 0;
  5567. }
  5568. static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
  5569. struct snd_ctl_elem_value *ucontrol)
  5570. {
  5571. u8 s;
  5572. int rc;
  5573. rc = volume_get_status(&s);
  5574. if (rc < 0)
  5575. return rc;
  5576. ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
  5577. return 0;
  5578. }
  5579. static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
  5580. struct snd_ctl_elem_value *ucontrol)
  5581. {
  5582. tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
  5583. ucontrol->value.integer.value[0]);
  5584. return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
  5585. }
  5586. #define volume_alsa_mute_info snd_ctl_boolean_mono_info
  5587. static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
  5588. struct snd_ctl_elem_value *ucontrol)
  5589. {
  5590. u8 s;
  5591. int rc;
  5592. rc = volume_get_status(&s);
  5593. if (rc < 0)
  5594. return rc;
  5595. ucontrol->value.integer.value[0] =
  5596. (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
  5597. return 0;
  5598. }
  5599. static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
  5600. struct snd_ctl_elem_value *ucontrol)
  5601. {
  5602. tpacpi_disclose_usertask("ALSA", "%smute\n",
  5603. ucontrol->value.integer.value[0] ?
  5604. "un" : "");
  5605. return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
  5606. }
  5607. static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
  5608. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5609. .name = "Console Playback Volume",
  5610. .index = 0,
  5611. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  5612. .info = volume_alsa_vol_info,
  5613. .get = volume_alsa_vol_get,
  5614. };
  5615. static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
  5616. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5617. .name = "Console Playback Switch",
  5618. .index = 0,
  5619. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  5620. .info = volume_alsa_mute_info,
  5621. .get = volume_alsa_mute_get,
  5622. };
  5623. static void volume_suspend(pm_message_t state)
  5624. {
  5625. tpacpi_volume_checkpoint_nvram();
  5626. }
  5627. static void volume_resume(void)
  5628. {
  5629. volume_alsa_notify_change();
  5630. }
  5631. static void volume_shutdown(void)
  5632. {
  5633. tpacpi_volume_checkpoint_nvram();
  5634. }
  5635. static void volume_exit(void)
  5636. {
  5637. if (alsa_card) {
  5638. snd_card_free(alsa_card);
  5639. alsa_card = NULL;
  5640. }
  5641. tpacpi_volume_checkpoint_nvram();
  5642. }
  5643. static int __init volume_create_alsa_mixer(void)
  5644. {
  5645. struct snd_card *card;
  5646. struct tpacpi_alsa_data *data;
  5647. struct snd_kcontrol *ctl_vol;
  5648. struct snd_kcontrol *ctl_mute;
  5649. int rc;
  5650. rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
  5651. sizeof(struct tpacpi_alsa_data), &card);
  5652. if (rc < 0 || !card) {
  5653. printk(TPACPI_ERR
  5654. "Failed to create ALSA card structures: %d\n", rc);
  5655. return 1;
  5656. }
  5657. BUG_ON(!card->private_data);
  5658. data = card->private_data;
  5659. data->card = card;
  5660. strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
  5661. sizeof(card->driver));
  5662. strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
  5663. sizeof(card->shortname));
  5664. snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
  5665. (thinkpad_id.ec_version_str) ?
  5666. thinkpad_id.ec_version_str : "(unknown)");
  5667. snprintf(card->longname, sizeof(card->longname),
  5668. "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
  5669. (thinkpad_id.ec_version_str) ?
  5670. thinkpad_id.ec_version_str : "unknown");
  5671. if (volume_control_allowed) {
  5672. volume_alsa_control_vol.put = volume_alsa_vol_put;
  5673. volume_alsa_control_vol.access =
  5674. SNDRV_CTL_ELEM_ACCESS_READWRITE;
  5675. volume_alsa_control_mute.put = volume_alsa_mute_put;
  5676. volume_alsa_control_mute.access =
  5677. SNDRV_CTL_ELEM_ACCESS_READWRITE;
  5678. }
  5679. if (!tp_features.mixer_no_level_control) {
  5680. ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
  5681. rc = snd_ctl_add(card, ctl_vol);
  5682. if (rc < 0) {
  5683. printk(TPACPI_ERR
  5684. "Failed to create ALSA volume control: %d\n",
  5685. rc);
  5686. goto err_exit;
  5687. }
  5688. data->ctl_vol_id = &ctl_vol->id;
  5689. }
  5690. ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
  5691. rc = snd_ctl_add(card, ctl_mute);
  5692. if (rc < 0) {
  5693. printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
  5694. rc);
  5695. goto err_exit;
  5696. }
  5697. data->ctl_mute_id = &ctl_mute->id;
  5698. snd_card_set_dev(card, &tpacpi_pdev->dev);
  5699. rc = snd_card_register(card);
  5700. if (rc < 0) {
  5701. printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
  5702. goto err_exit;
  5703. }
  5704. alsa_card = card;
  5705. return 0;
  5706. err_exit:
  5707. snd_card_free(card);
  5708. return 1;
  5709. }
  5710. #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
  5711. #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
  5712. static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
  5713. /* Whitelist volume level on all IBM by default */
  5714. { .vendor = PCI_VENDOR_ID_IBM,
  5715. .bios = TPACPI_MATCH_ANY,
  5716. .ec = TPACPI_MATCH_ANY,
  5717. .quirks = TPACPI_VOL_Q_LEVEL },
  5718. /* Lenovo models with volume control (needs confirmation) */
  5719. TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
  5720. TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
  5721. TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
  5722. TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
  5723. TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
  5724. TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
  5725. TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
  5726. /* Whitelist mute-only on all Lenovo by default */
  5727. { .vendor = PCI_VENDOR_ID_LENOVO,
  5728. .bios = TPACPI_MATCH_ANY,
  5729. .ec = TPACPI_MATCH_ANY,
  5730. .quirks = TPACPI_VOL_Q_MUTEONLY }
  5731. };
  5732. static int __init volume_init(struct ibm_init_struct *iibm)
  5733. {
  5734. unsigned long quirks;
  5735. int rc;
  5736. vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
  5737. mutex_init(&volume_mutex);
  5738. /*
  5739. * Check for module parameter bogosity, note that we
  5740. * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
  5741. * able to detect "unspecified"
  5742. */
  5743. if (volume_mode > TPACPI_VOL_MODE_MAX)
  5744. return -EINVAL;
  5745. if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
  5746. printk(TPACPI_ERR
  5747. "UCMS step volume mode not implemented, "
  5748. "please contact %s\n", TPACPI_MAIL);
  5749. return 1;
  5750. }
  5751. if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
  5752. return -EINVAL;
  5753. /*
  5754. * The ALSA mixer is our primary interface.
  5755. * When disabled, don't install the subdriver at all
  5756. */
  5757. if (!alsa_enable) {
  5758. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  5759. "ALSA mixer disabled by parameter, "
  5760. "not loading volume subdriver...\n");
  5761. return 1;
  5762. }
  5763. quirks = tpacpi_check_quirks(volume_quirk_table,
  5764. ARRAY_SIZE(volume_quirk_table));
  5765. switch (volume_capabilities) {
  5766. case TPACPI_VOL_CAP_AUTO:
  5767. if (quirks & TPACPI_VOL_Q_MUTEONLY)
  5768. tp_features.mixer_no_level_control = 1;
  5769. else if (quirks & TPACPI_VOL_Q_LEVEL)
  5770. tp_features.mixer_no_level_control = 0;
  5771. else
  5772. return 1; /* no mixer */
  5773. break;
  5774. case TPACPI_VOL_CAP_VOLMUTE:
  5775. tp_features.mixer_no_level_control = 0;
  5776. break;
  5777. case TPACPI_VOL_CAP_MUTEONLY:
  5778. tp_features.mixer_no_level_control = 1;
  5779. break;
  5780. default:
  5781. return 1;
  5782. }
  5783. if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
  5784. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  5785. "using user-supplied volume_capabilities=%d\n",
  5786. volume_capabilities);
  5787. if (volume_mode == TPACPI_VOL_MODE_AUTO ||
  5788. volume_mode == TPACPI_VOL_MODE_MAX) {
  5789. volume_mode = TPACPI_VOL_MODE_ECNVRAM;
  5790. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  5791. "driver auto-selected volume_mode=%d\n",
  5792. volume_mode);
  5793. } else {
  5794. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  5795. "using user-supplied volume_mode=%d\n",
  5796. volume_mode);
  5797. }
  5798. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  5799. "mute is supported, volume control is %s\n",
  5800. str_supported(!tp_features.mixer_no_level_control));
  5801. rc = volume_create_alsa_mixer();
  5802. if (rc) {
  5803. printk(TPACPI_ERR
  5804. "Could not create the ALSA mixer interface\n");
  5805. return rc;
  5806. }
  5807. printk(TPACPI_INFO
  5808. "Console audio control enabled, mode: %s\n",
  5809. (volume_control_allowed) ?
  5810. "override (read/write)" :
  5811. "monitor (read only)");
  5812. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  5813. "registering volume hotkeys as change notification\n");
  5814. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
  5815. | TP_ACPI_HKEY_VOLUP_MASK
  5816. | TP_ACPI_HKEY_VOLDWN_MASK
  5817. | TP_ACPI_HKEY_MUTE_MASK);
  5818. return 0;
  5819. }
  5820. static int volume_read(struct seq_file *m)
  5821. {
  5822. u8 status;
  5823. if (volume_get_status(&status) < 0) {
  5824. seq_printf(m, "level:\t\tunreadable\n");
  5825. } else {
  5826. if (tp_features.mixer_no_level_control)
  5827. seq_printf(m, "level:\t\tunsupported\n");
  5828. else
  5829. seq_printf(m, "level:\t\t%d\n",
  5830. status & TP_EC_AUDIO_LVL_MSK);
  5831. seq_printf(m, "mute:\t\t%s\n",
  5832. onoff(status, TP_EC_AUDIO_MUTESW));
  5833. if (volume_control_allowed) {
  5834. seq_printf(m, "commands:\tunmute, mute\n");
  5835. if (!tp_features.mixer_no_level_control) {
  5836. seq_printf(m,
  5837. "commands:\tup, down\n");
  5838. seq_printf(m,
  5839. "commands:\tlevel <level>"
  5840. " (<level> is 0-%d)\n",
  5841. TP_EC_VOLUME_MAX);
  5842. }
  5843. }
  5844. }
  5845. return 0;
  5846. }
  5847. static int volume_write(char *buf)
  5848. {
  5849. u8 s;
  5850. u8 new_level, new_mute;
  5851. int l;
  5852. char *cmd;
  5853. int rc;
  5854. /*
  5855. * We do allow volume control at driver startup, so that the
  5856. * user can set initial state through the volume=... parameter hack.
  5857. */
  5858. if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
  5859. if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
  5860. tp_warned.volume_ctrl_forbidden = 1;
  5861. printk(TPACPI_NOTICE
  5862. "Console audio control in monitor mode, "
  5863. "changes are not allowed.\n");
  5864. printk(TPACPI_NOTICE
  5865. "Use the volume_control=1 module parameter "
  5866. "to enable volume control\n");
  5867. }
  5868. return -EPERM;
  5869. }
  5870. rc = volume_get_status(&s);
  5871. if (rc < 0)
  5872. return rc;
  5873. new_level = s & TP_EC_AUDIO_LVL_MSK;
  5874. new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
  5875. while ((cmd = next_cmd(&buf))) {
  5876. if (!tp_features.mixer_no_level_control) {
  5877. if (strlencmp(cmd, "up") == 0) {
  5878. if (new_mute)
  5879. new_mute = 0;
  5880. else if (new_level < TP_EC_VOLUME_MAX)
  5881. new_level++;
  5882. continue;
  5883. } else if (strlencmp(cmd, "down") == 0) {
  5884. if (new_mute)
  5885. new_mute = 0;
  5886. else if (new_level > 0)
  5887. new_level--;
  5888. continue;
  5889. } else if (sscanf(cmd, "level %u", &l) == 1 &&
  5890. l >= 0 && l <= TP_EC_VOLUME_MAX) {
  5891. new_level = l;
  5892. continue;
  5893. }
  5894. }
  5895. if (strlencmp(cmd, "mute") == 0)
  5896. new_mute = TP_EC_AUDIO_MUTESW_MSK;
  5897. else if (strlencmp(cmd, "unmute") == 0)
  5898. new_mute = 0;
  5899. else
  5900. return -EINVAL;
  5901. }
  5902. if (tp_features.mixer_no_level_control) {
  5903. tpacpi_disclose_usertask("procfs volume", "%smute\n",
  5904. new_mute ? "" : "un");
  5905. rc = volume_set_mute(!!new_mute);
  5906. } else {
  5907. tpacpi_disclose_usertask("procfs volume",
  5908. "%smute and set level to %d\n",
  5909. new_mute ? "" : "un", new_level);
  5910. rc = volume_set_status(new_mute | new_level);
  5911. }
  5912. volume_alsa_notify_change();
  5913. return (rc == -EINTR) ? -ERESTARTSYS : rc;
  5914. }
  5915. static struct ibm_struct volume_driver_data = {
  5916. .name = "volume",
  5917. .read = volume_read,
  5918. .write = volume_write,
  5919. .exit = volume_exit,
  5920. .suspend = volume_suspend,
  5921. .resume = volume_resume,
  5922. .shutdown = volume_shutdown,
  5923. };
  5924. #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  5925. #define alsa_card NULL
  5926. static void inline volume_alsa_notify_change(void)
  5927. {
  5928. }
  5929. static int __init volume_init(struct ibm_init_struct *iibm)
  5930. {
  5931. printk(TPACPI_INFO
  5932. "volume: disabled as there is no ALSA support in this kernel\n");
  5933. return 1;
  5934. }
  5935. static struct ibm_struct volume_driver_data = {
  5936. .name = "volume",
  5937. };
  5938. #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  5939. /*************************************************************************
  5940. * Fan subdriver
  5941. */
  5942. /*
  5943. * FAN ACCESS MODES
  5944. *
  5945. * TPACPI_FAN_RD_ACPI_GFAN:
  5946. * ACPI GFAN method: returns fan level
  5947. *
  5948. * see TPACPI_FAN_WR_ACPI_SFAN
  5949. * EC 0x2f (HFSP) not available if GFAN exists
  5950. *
  5951. * TPACPI_FAN_WR_ACPI_SFAN:
  5952. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  5953. *
  5954. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  5955. * it for writing.
  5956. *
  5957. * TPACPI_FAN_WR_TPEC:
  5958. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  5959. * Supported on almost all ThinkPads
  5960. *
  5961. * Fan speed changes of any sort (including those caused by the
  5962. * disengaged mode) are usually done slowly by the firmware as the
  5963. * maximum amount of fan duty cycle change per second seems to be
  5964. * limited.
  5965. *
  5966. * Reading is not available if GFAN exists.
  5967. * Writing is not available if SFAN exists.
  5968. *
  5969. * Bits
  5970. * 7 automatic mode engaged;
  5971. * (default operation mode of the ThinkPad)
  5972. * fan level is ignored in this mode.
  5973. * 6 full speed mode (takes precedence over bit 7);
  5974. * not available on all thinkpads. May disable
  5975. * the tachometer while the fan controller ramps up
  5976. * the speed (which can take up to a few *minutes*).
  5977. * Speeds up fan to 100% duty-cycle, which is far above
  5978. * the standard RPM levels. It is not impossible that
  5979. * it could cause hardware damage.
  5980. * 5-3 unused in some models. Extra bits for fan level
  5981. * in others, but still useless as all values above
  5982. * 7 map to the same speed as level 7 in these models.
  5983. * 2-0 fan level (0..7 usually)
  5984. * 0x00 = stop
  5985. * 0x07 = max (set when temperatures critical)
  5986. * Some ThinkPads may have other levels, see
  5987. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  5988. *
  5989. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  5990. * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
  5991. * does so, its initial value is meaningless (0x07).
  5992. *
  5993. * For firmware bugs, refer to:
  5994. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  5995. *
  5996. * ----
  5997. *
  5998. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  5999. * Main fan tachometer reading (in RPM)
  6000. *
  6001. * This register is present on all ThinkPads with a new-style EC, and
  6002. * it is known not to be present on the A21m/e, and T22, as there is
  6003. * something else in offset 0x84 according to the ACPI DSDT. Other
  6004. * ThinkPads from this same time period (and earlier) probably lack the
  6005. * tachometer as well.
  6006. *
  6007. * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
  6008. * was never fixed by IBM to report the EC firmware version string
  6009. * probably support the tachometer (like the early X models), so
  6010. * detecting it is quite hard. We need more data to know for sure.
  6011. *
  6012. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  6013. * might result.
  6014. *
  6015. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  6016. * mode.
  6017. *
  6018. * For firmware bugs, refer to:
  6019. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  6020. *
  6021. * ----
  6022. *
  6023. * ThinkPad EC register 0x31 bit 0 (only on select models)
  6024. *
  6025. * When bit 0 of EC register 0x31 is zero, the tachometer registers
  6026. * show the speed of the main fan. When bit 0 of EC register 0x31
  6027. * is one, the tachometer registers show the speed of the auxiliary
  6028. * fan.
  6029. *
  6030. * Fan control seems to affect both fans, regardless of the state
  6031. * of this bit.
  6032. *
  6033. * So far, only the firmware for the X60/X61 non-tablet versions
  6034. * seem to support this (firmware TP-7M).
  6035. *
  6036. * TPACPI_FAN_WR_ACPI_FANS:
  6037. * ThinkPad X31, X40, X41. Not available in the X60.
  6038. *
  6039. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  6040. * high speed. ACPI DSDT seems to map these three speeds to levels
  6041. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  6042. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  6043. *
  6044. * The speeds are stored on handles
  6045. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  6046. *
  6047. * There are three default speed sets, accessible as handles:
  6048. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  6049. *
  6050. * ACPI DSDT switches which set is in use depending on various
  6051. * factors.
  6052. *
  6053. * TPACPI_FAN_WR_TPEC is also available and should be used to
  6054. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  6055. * but the ACPI tables just mention level 7.
  6056. */
  6057. enum { /* Fan control constants */
  6058. fan_status_offset = 0x2f, /* EC register 0x2f */
  6059. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  6060. * 0x84 must be read before 0x85 */
  6061. fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
  6062. bit 0 selects which fan is active */
  6063. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  6064. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  6065. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  6066. };
  6067. enum fan_status_access_mode {
  6068. TPACPI_FAN_NONE = 0, /* No fan status or control */
  6069. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  6070. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  6071. };
  6072. enum fan_control_access_mode {
  6073. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  6074. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  6075. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  6076. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  6077. };
  6078. enum fan_control_commands {
  6079. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  6080. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  6081. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  6082. * and also watchdog cmd */
  6083. };
  6084. static int fan_control_allowed;
  6085. static enum fan_status_access_mode fan_status_access_mode;
  6086. static enum fan_control_access_mode fan_control_access_mode;
  6087. static enum fan_control_commands fan_control_commands;
  6088. static u8 fan_control_initial_status;
  6089. static u8 fan_control_desired_level;
  6090. static u8 fan_control_resume_level;
  6091. static int fan_watchdog_maxinterval;
  6092. static struct mutex fan_mutex;
  6093. static void fan_watchdog_fire(struct work_struct *ignored);
  6094. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  6095. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  6096. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  6097. "\\FSPD", /* 600e/x, 770e, 770x */
  6098. ); /* all others */
  6099. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  6100. "JFNS", /* 770x-JL */
  6101. ); /* all others */
  6102. /*
  6103. * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
  6104. * HFSP register at boot, so it contains 0x07 but the Thinkpad could
  6105. * be in auto mode (0x80).
  6106. *
  6107. * This is corrected by any write to HFSP either by the driver, or
  6108. * by the firmware.
  6109. *
  6110. * We assume 0x07 really means auto mode while this quirk is active,
  6111. * as this is far more likely than the ThinkPad being in level 7,
  6112. * which is only used by the firmware during thermal emergencies.
  6113. *
  6114. * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
  6115. * TP-70 (T43, R52), which are known to be buggy.
  6116. */
  6117. static void fan_quirk1_setup(void)
  6118. {
  6119. if (fan_control_initial_status == 0x07) {
  6120. printk(TPACPI_NOTICE
  6121. "fan_init: initial fan status is unknown, "
  6122. "assuming it is in auto mode\n");
  6123. tp_features.fan_ctrl_status_undef = 1;
  6124. }
  6125. }
  6126. static void fan_quirk1_handle(u8 *fan_status)
  6127. {
  6128. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  6129. if (*fan_status != fan_control_initial_status) {
  6130. /* something changed the HFSP regisnter since
  6131. * driver init time, so it is not undefined
  6132. * anymore */
  6133. tp_features.fan_ctrl_status_undef = 0;
  6134. } else {
  6135. /* Return most likely status. In fact, it
  6136. * might be the only possible status */
  6137. *fan_status = TP_EC_FAN_AUTO;
  6138. }
  6139. }
  6140. }
  6141. /* Select main fan on X60/X61, NOOP on others */
  6142. static bool fan_select_fan1(void)
  6143. {
  6144. if (tp_features.second_fan) {
  6145. u8 val;
  6146. if (ec_read(fan_select_offset, &val) < 0)
  6147. return false;
  6148. val &= 0xFEU;
  6149. if (ec_write(fan_select_offset, val) < 0)
  6150. return false;
  6151. }
  6152. return true;
  6153. }
  6154. /* Select secondary fan on X60/X61 */
  6155. static bool fan_select_fan2(void)
  6156. {
  6157. u8 val;
  6158. if (!tp_features.second_fan)
  6159. return false;
  6160. if (ec_read(fan_select_offset, &val) < 0)
  6161. return false;
  6162. val |= 0x01U;
  6163. if (ec_write(fan_select_offset, val) < 0)
  6164. return false;
  6165. return true;
  6166. }
  6167. /*
  6168. * Call with fan_mutex held
  6169. */
  6170. static void fan_update_desired_level(u8 status)
  6171. {
  6172. if ((status &
  6173. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  6174. if (status > 7)
  6175. fan_control_desired_level = 7;
  6176. else
  6177. fan_control_desired_level = status;
  6178. }
  6179. }
  6180. static int fan_get_status(u8 *status)
  6181. {
  6182. u8 s;
  6183. /* TODO:
  6184. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  6185. switch (fan_status_access_mode) {
  6186. case TPACPI_FAN_RD_ACPI_GFAN:
  6187. /* 570, 600e/x, 770e, 770x */
  6188. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  6189. return -EIO;
  6190. if (likely(status))
  6191. *status = s & 0x07;
  6192. break;
  6193. case TPACPI_FAN_RD_TPEC:
  6194. /* all except 570, 600e/x, 770e, 770x */
  6195. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  6196. return -EIO;
  6197. if (likely(status)) {
  6198. *status = s;
  6199. fan_quirk1_handle(status);
  6200. }
  6201. break;
  6202. default:
  6203. return -ENXIO;
  6204. }
  6205. return 0;
  6206. }
  6207. static int fan_get_status_safe(u8 *status)
  6208. {
  6209. int rc;
  6210. u8 s;
  6211. if (mutex_lock_killable(&fan_mutex))
  6212. return -ERESTARTSYS;
  6213. rc = fan_get_status(&s);
  6214. if (!rc)
  6215. fan_update_desired_level(s);
  6216. mutex_unlock(&fan_mutex);
  6217. if (status)
  6218. *status = s;
  6219. return rc;
  6220. }
  6221. static int fan_get_speed(unsigned int *speed)
  6222. {
  6223. u8 hi, lo;
  6224. switch (fan_status_access_mode) {
  6225. case TPACPI_FAN_RD_TPEC:
  6226. /* all except 570, 600e/x, 770e, 770x */
  6227. if (unlikely(!fan_select_fan1()))
  6228. return -EIO;
  6229. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  6230. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  6231. return -EIO;
  6232. if (likely(speed))
  6233. *speed = (hi << 8) | lo;
  6234. break;
  6235. default:
  6236. return -ENXIO;
  6237. }
  6238. return 0;
  6239. }
  6240. static int fan2_get_speed(unsigned int *speed)
  6241. {
  6242. u8 hi, lo;
  6243. bool rc;
  6244. switch (fan_status_access_mode) {
  6245. case TPACPI_FAN_RD_TPEC:
  6246. /* all except 570, 600e/x, 770e, 770x */
  6247. if (unlikely(!fan_select_fan2()))
  6248. return -EIO;
  6249. rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
  6250. !acpi_ec_read(fan_rpm_offset + 1, &hi);
  6251. fan_select_fan1(); /* play it safe */
  6252. if (rc)
  6253. return -EIO;
  6254. if (likely(speed))
  6255. *speed = (hi << 8) | lo;
  6256. break;
  6257. default:
  6258. return -ENXIO;
  6259. }
  6260. return 0;
  6261. }
  6262. static int fan_set_level(int level)
  6263. {
  6264. if (!fan_control_allowed)
  6265. return -EPERM;
  6266. switch (fan_control_access_mode) {
  6267. case TPACPI_FAN_WR_ACPI_SFAN:
  6268. if (level >= 0 && level <= 7) {
  6269. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  6270. return -EIO;
  6271. } else
  6272. return -EINVAL;
  6273. break;
  6274. case TPACPI_FAN_WR_ACPI_FANS:
  6275. case TPACPI_FAN_WR_TPEC:
  6276. if (!(level & TP_EC_FAN_AUTO) &&
  6277. !(level & TP_EC_FAN_FULLSPEED) &&
  6278. ((level < 0) || (level > 7)))
  6279. return -EINVAL;
  6280. /* safety net should the EC not support AUTO
  6281. * or FULLSPEED mode bits and just ignore them */
  6282. if (level & TP_EC_FAN_FULLSPEED)
  6283. level |= 7; /* safety min speed 7 */
  6284. else if (level & TP_EC_FAN_AUTO)
  6285. level |= 4; /* safety min speed 4 */
  6286. if (!acpi_ec_write(fan_status_offset, level))
  6287. return -EIO;
  6288. else
  6289. tp_features.fan_ctrl_status_undef = 0;
  6290. break;
  6291. default:
  6292. return -ENXIO;
  6293. }
  6294. vdbg_printk(TPACPI_DBG_FAN,
  6295. "fan control: set fan control register to 0x%02x\n", level);
  6296. return 0;
  6297. }
  6298. static int fan_set_level_safe(int level)
  6299. {
  6300. int rc;
  6301. if (!fan_control_allowed)
  6302. return -EPERM;
  6303. if (mutex_lock_killable(&fan_mutex))
  6304. return -ERESTARTSYS;
  6305. if (level == TPACPI_FAN_LAST_LEVEL)
  6306. level = fan_control_desired_level;
  6307. rc = fan_set_level(level);
  6308. if (!rc)
  6309. fan_update_desired_level(level);
  6310. mutex_unlock(&fan_mutex);
  6311. return rc;
  6312. }
  6313. static int fan_set_enable(void)
  6314. {
  6315. u8 s;
  6316. int rc;
  6317. if (!fan_control_allowed)
  6318. return -EPERM;
  6319. if (mutex_lock_killable(&fan_mutex))
  6320. return -ERESTARTSYS;
  6321. switch (fan_control_access_mode) {
  6322. case TPACPI_FAN_WR_ACPI_FANS:
  6323. case TPACPI_FAN_WR_TPEC:
  6324. rc = fan_get_status(&s);
  6325. if (rc < 0)
  6326. break;
  6327. /* Don't go out of emergency fan mode */
  6328. if (s != 7) {
  6329. s &= 0x07;
  6330. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  6331. }
  6332. if (!acpi_ec_write(fan_status_offset, s))
  6333. rc = -EIO;
  6334. else {
  6335. tp_features.fan_ctrl_status_undef = 0;
  6336. rc = 0;
  6337. }
  6338. break;
  6339. case TPACPI_FAN_WR_ACPI_SFAN:
  6340. rc = fan_get_status(&s);
  6341. if (rc < 0)
  6342. break;
  6343. s &= 0x07;
  6344. /* Set fan to at least level 4 */
  6345. s |= 4;
  6346. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  6347. rc = -EIO;
  6348. else
  6349. rc = 0;
  6350. break;
  6351. default:
  6352. rc = -ENXIO;
  6353. }
  6354. mutex_unlock(&fan_mutex);
  6355. if (!rc)
  6356. vdbg_printk(TPACPI_DBG_FAN,
  6357. "fan control: set fan control register to 0x%02x\n",
  6358. s);
  6359. return rc;
  6360. }
  6361. static int fan_set_disable(void)
  6362. {
  6363. int rc;
  6364. if (!fan_control_allowed)
  6365. return -EPERM;
  6366. if (mutex_lock_killable(&fan_mutex))
  6367. return -ERESTARTSYS;
  6368. rc = 0;
  6369. switch (fan_control_access_mode) {
  6370. case TPACPI_FAN_WR_ACPI_FANS:
  6371. case TPACPI_FAN_WR_TPEC:
  6372. if (!acpi_ec_write(fan_status_offset, 0x00))
  6373. rc = -EIO;
  6374. else {
  6375. fan_control_desired_level = 0;
  6376. tp_features.fan_ctrl_status_undef = 0;
  6377. }
  6378. break;
  6379. case TPACPI_FAN_WR_ACPI_SFAN:
  6380. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  6381. rc = -EIO;
  6382. else
  6383. fan_control_desired_level = 0;
  6384. break;
  6385. default:
  6386. rc = -ENXIO;
  6387. }
  6388. if (!rc)
  6389. vdbg_printk(TPACPI_DBG_FAN,
  6390. "fan control: set fan control register to 0\n");
  6391. mutex_unlock(&fan_mutex);
  6392. return rc;
  6393. }
  6394. static int fan_set_speed(int speed)
  6395. {
  6396. int rc;
  6397. if (!fan_control_allowed)
  6398. return -EPERM;
  6399. if (mutex_lock_killable(&fan_mutex))
  6400. return -ERESTARTSYS;
  6401. rc = 0;
  6402. switch (fan_control_access_mode) {
  6403. case TPACPI_FAN_WR_ACPI_FANS:
  6404. if (speed >= 0 && speed <= 65535) {
  6405. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  6406. speed, speed, speed))
  6407. rc = -EIO;
  6408. } else
  6409. rc = -EINVAL;
  6410. break;
  6411. default:
  6412. rc = -ENXIO;
  6413. }
  6414. mutex_unlock(&fan_mutex);
  6415. return rc;
  6416. }
  6417. static void fan_watchdog_reset(void)
  6418. {
  6419. static int fan_watchdog_active;
  6420. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  6421. return;
  6422. if (fan_watchdog_active)
  6423. cancel_delayed_work(&fan_watchdog_task);
  6424. if (fan_watchdog_maxinterval > 0 &&
  6425. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  6426. fan_watchdog_active = 1;
  6427. if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
  6428. msecs_to_jiffies(fan_watchdog_maxinterval
  6429. * 1000))) {
  6430. printk(TPACPI_ERR
  6431. "failed to queue the fan watchdog, "
  6432. "watchdog will not trigger\n");
  6433. }
  6434. } else
  6435. fan_watchdog_active = 0;
  6436. }
  6437. static void fan_watchdog_fire(struct work_struct *ignored)
  6438. {
  6439. int rc;
  6440. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  6441. return;
  6442. printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
  6443. rc = fan_set_enable();
  6444. if (rc < 0) {
  6445. printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
  6446. "will try again later...\n", -rc);
  6447. /* reschedule for later */
  6448. fan_watchdog_reset();
  6449. }
  6450. }
  6451. /*
  6452. * SYSFS fan layout: hwmon compatible (device)
  6453. *
  6454. * pwm*_enable:
  6455. * 0: "disengaged" mode
  6456. * 1: manual mode
  6457. * 2: native EC "auto" mode (recommended, hardware default)
  6458. *
  6459. * pwm*: set speed in manual mode, ignored otherwise.
  6460. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  6461. * interpolation.
  6462. *
  6463. * fan*_input: tachometer reading, RPM
  6464. *
  6465. *
  6466. * SYSFS fan layout: extensions
  6467. *
  6468. * fan_watchdog (driver):
  6469. * fan watchdog interval in seconds, 0 disables (default), max 120
  6470. */
  6471. /* sysfs fan pwm1_enable ----------------------------------------------- */
  6472. static ssize_t fan_pwm1_enable_show(struct device *dev,
  6473. struct device_attribute *attr,
  6474. char *buf)
  6475. {
  6476. int res, mode;
  6477. u8 status;
  6478. res = fan_get_status_safe(&status);
  6479. if (res)
  6480. return res;
  6481. if (status & TP_EC_FAN_FULLSPEED) {
  6482. mode = 0;
  6483. } else if (status & TP_EC_FAN_AUTO) {
  6484. mode = 2;
  6485. } else
  6486. mode = 1;
  6487. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  6488. }
  6489. static ssize_t fan_pwm1_enable_store(struct device *dev,
  6490. struct device_attribute *attr,
  6491. const char *buf, size_t count)
  6492. {
  6493. unsigned long t;
  6494. int res, level;
  6495. if (parse_strtoul(buf, 2, &t))
  6496. return -EINVAL;
  6497. tpacpi_disclose_usertask("hwmon pwm1_enable",
  6498. "set fan mode to %lu\n", t);
  6499. switch (t) {
  6500. case 0:
  6501. level = TP_EC_FAN_FULLSPEED;
  6502. break;
  6503. case 1:
  6504. level = TPACPI_FAN_LAST_LEVEL;
  6505. break;
  6506. case 2:
  6507. level = TP_EC_FAN_AUTO;
  6508. break;
  6509. case 3:
  6510. /* reserved for software-controlled auto mode */
  6511. return -ENOSYS;
  6512. default:
  6513. return -EINVAL;
  6514. }
  6515. res = fan_set_level_safe(level);
  6516. if (res == -ENXIO)
  6517. return -EINVAL;
  6518. else if (res < 0)
  6519. return res;
  6520. fan_watchdog_reset();
  6521. return count;
  6522. }
  6523. static struct device_attribute dev_attr_fan_pwm1_enable =
  6524. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  6525. fan_pwm1_enable_show, fan_pwm1_enable_store);
  6526. /* sysfs fan pwm1 ------------------------------------------------------ */
  6527. static ssize_t fan_pwm1_show(struct device *dev,
  6528. struct device_attribute *attr,
  6529. char *buf)
  6530. {
  6531. int res;
  6532. u8 status;
  6533. res = fan_get_status_safe(&status);
  6534. if (res)
  6535. return res;
  6536. if ((status &
  6537. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  6538. status = fan_control_desired_level;
  6539. if (status > 7)
  6540. status = 7;
  6541. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  6542. }
  6543. static ssize_t fan_pwm1_store(struct device *dev,
  6544. struct device_attribute *attr,
  6545. const char *buf, size_t count)
  6546. {
  6547. unsigned long s;
  6548. int rc;
  6549. u8 status, newlevel;
  6550. if (parse_strtoul(buf, 255, &s))
  6551. return -EINVAL;
  6552. tpacpi_disclose_usertask("hwmon pwm1",
  6553. "set fan speed to %lu\n", s);
  6554. /* scale down from 0-255 to 0-7 */
  6555. newlevel = (s >> 5) & 0x07;
  6556. if (mutex_lock_killable(&fan_mutex))
  6557. return -ERESTARTSYS;
  6558. rc = fan_get_status(&status);
  6559. if (!rc && (status &
  6560. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  6561. rc = fan_set_level(newlevel);
  6562. if (rc == -ENXIO)
  6563. rc = -EINVAL;
  6564. else if (!rc) {
  6565. fan_update_desired_level(newlevel);
  6566. fan_watchdog_reset();
  6567. }
  6568. }
  6569. mutex_unlock(&fan_mutex);
  6570. return (rc)? rc : count;
  6571. }
  6572. static struct device_attribute dev_attr_fan_pwm1 =
  6573. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  6574. fan_pwm1_show, fan_pwm1_store);
  6575. /* sysfs fan fan1_input ------------------------------------------------ */
  6576. static ssize_t fan_fan1_input_show(struct device *dev,
  6577. struct device_attribute *attr,
  6578. char *buf)
  6579. {
  6580. int res;
  6581. unsigned int speed;
  6582. res = fan_get_speed(&speed);
  6583. if (res < 0)
  6584. return res;
  6585. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  6586. }
  6587. static struct device_attribute dev_attr_fan_fan1_input =
  6588. __ATTR(fan1_input, S_IRUGO,
  6589. fan_fan1_input_show, NULL);
  6590. /* sysfs fan fan2_input ------------------------------------------------ */
  6591. static ssize_t fan_fan2_input_show(struct device *dev,
  6592. struct device_attribute *attr,
  6593. char *buf)
  6594. {
  6595. int res;
  6596. unsigned int speed;
  6597. res = fan2_get_speed(&speed);
  6598. if (res < 0)
  6599. return res;
  6600. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  6601. }
  6602. static struct device_attribute dev_attr_fan_fan2_input =
  6603. __ATTR(fan2_input, S_IRUGO,
  6604. fan_fan2_input_show, NULL);
  6605. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  6606. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  6607. char *buf)
  6608. {
  6609. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  6610. }
  6611. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  6612. const char *buf, size_t count)
  6613. {
  6614. unsigned long t;
  6615. if (parse_strtoul(buf, 120, &t))
  6616. return -EINVAL;
  6617. if (!fan_control_allowed)
  6618. return -EPERM;
  6619. fan_watchdog_maxinterval = t;
  6620. fan_watchdog_reset();
  6621. tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
  6622. return count;
  6623. }
  6624. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  6625. fan_fan_watchdog_show, fan_fan_watchdog_store);
  6626. /* --------------------------------------------------------------------- */
  6627. static struct attribute *fan_attributes[] = {
  6628. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  6629. &dev_attr_fan_fan1_input.attr,
  6630. NULL, /* for fan2_input */
  6631. NULL
  6632. };
  6633. static const struct attribute_group fan_attr_group = {
  6634. .attrs = fan_attributes,
  6635. };
  6636. #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
  6637. #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
  6638. #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
  6639. { .vendor = PCI_VENDOR_ID_IBM, \
  6640. .bios = TPACPI_MATCH_ANY, \
  6641. .ec = TPID(__id1, __id2), \
  6642. .quirks = __quirks }
  6643. #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
  6644. { .vendor = PCI_VENDOR_ID_LENOVO, \
  6645. .bios = TPACPI_MATCH_ANY, \
  6646. .ec = TPID(__id1, __id2), \
  6647. .quirks = __quirks }
  6648. static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
  6649. TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
  6650. TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
  6651. TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
  6652. TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
  6653. TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
  6654. };
  6655. #undef TPACPI_FAN_QL
  6656. #undef TPACPI_FAN_QI
  6657. static int __init fan_init(struct ibm_init_struct *iibm)
  6658. {
  6659. int rc;
  6660. unsigned long quirks;
  6661. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  6662. "initializing fan subdriver\n");
  6663. mutex_init(&fan_mutex);
  6664. fan_status_access_mode = TPACPI_FAN_NONE;
  6665. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  6666. fan_control_commands = 0;
  6667. fan_watchdog_maxinterval = 0;
  6668. tp_features.fan_ctrl_status_undef = 0;
  6669. tp_features.second_fan = 0;
  6670. fan_control_desired_level = 7;
  6671. if (tpacpi_is_ibm()) {
  6672. TPACPI_ACPIHANDLE_INIT(fans);
  6673. TPACPI_ACPIHANDLE_INIT(gfan);
  6674. TPACPI_ACPIHANDLE_INIT(sfan);
  6675. }
  6676. quirks = tpacpi_check_quirks(fan_quirk_table,
  6677. ARRAY_SIZE(fan_quirk_table));
  6678. if (gfan_handle) {
  6679. /* 570, 600e/x, 770e, 770x */
  6680. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  6681. } else {
  6682. /* all other ThinkPads: note that even old-style
  6683. * ThinkPad ECs supports the fan control register */
  6684. if (likely(acpi_ec_read(fan_status_offset,
  6685. &fan_control_initial_status))) {
  6686. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  6687. if (quirks & TPACPI_FAN_Q1)
  6688. fan_quirk1_setup();
  6689. if (quirks & TPACPI_FAN_2FAN) {
  6690. tp_features.second_fan = 1;
  6691. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  6692. "secondary fan support enabled\n");
  6693. }
  6694. } else {
  6695. printk(TPACPI_ERR
  6696. "ThinkPad ACPI EC access misbehaving, "
  6697. "fan status and control unavailable\n");
  6698. return 1;
  6699. }
  6700. }
  6701. if (sfan_handle) {
  6702. /* 570, 770x-JL */
  6703. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  6704. fan_control_commands |=
  6705. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  6706. } else {
  6707. if (!gfan_handle) {
  6708. /* gfan without sfan means no fan control */
  6709. /* all other models implement TP EC 0x2f control */
  6710. if (fans_handle) {
  6711. /* X31, X40, X41 */
  6712. fan_control_access_mode =
  6713. TPACPI_FAN_WR_ACPI_FANS;
  6714. fan_control_commands |=
  6715. TPACPI_FAN_CMD_SPEED |
  6716. TPACPI_FAN_CMD_LEVEL |
  6717. TPACPI_FAN_CMD_ENABLE;
  6718. } else {
  6719. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  6720. fan_control_commands |=
  6721. TPACPI_FAN_CMD_LEVEL |
  6722. TPACPI_FAN_CMD_ENABLE;
  6723. }
  6724. }
  6725. }
  6726. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  6727. "fan is %s, modes %d, %d\n",
  6728. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  6729. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  6730. fan_status_access_mode, fan_control_access_mode);
  6731. /* fan control master switch */
  6732. if (!fan_control_allowed) {
  6733. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  6734. fan_control_commands = 0;
  6735. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  6736. "fan control features disabled by parameter\n");
  6737. }
  6738. /* update fan_control_desired_level */
  6739. if (fan_status_access_mode != TPACPI_FAN_NONE)
  6740. fan_get_status_safe(NULL);
  6741. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  6742. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  6743. if (tp_features.second_fan) {
  6744. /* attach second fan tachometer */
  6745. fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
  6746. &dev_attr_fan_fan2_input.attr;
  6747. }
  6748. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  6749. &fan_attr_group);
  6750. if (rc < 0)
  6751. return rc;
  6752. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  6753. &driver_attr_fan_watchdog);
  6754. if (rc < 0) {
  6755. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  6756. &fan_attr_group);
  6757. return rc;
  6758. }
  6759. return 0;
  6760. } else
  6761. return 1;
  6762. }
  6763. static void fan_exit(void)
  6764. {
  6765. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
  6766. "cancelling any pending fan watchdog tasks\n");
  6767. /* FIXME: can we really do this unconditionally? */
  6768. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  6769. driver_remove_file(&tpacpi_hwmon_pdriver.driver,
  6770. &driver_attr_fan_watchdog);
  6771. cancel_delayed_work(&fan_watchdog_task);
  6772. flush_workqueue(tpacpi_wq);
  6773. }
  6774. static void fan_suspend(pm_message_t state)
  6775. {
  6776. int rc;
  6777. if (!fan_control_allowed)
  6778. return;
  6779. /* Store fan status in cache */
  6780. fan_control_resume_level = 0;
  6781. rc = fan_get_status_safe(&fan_control_resume_level);
  6782. if (rc < 0)
  6783. printk(TPACPI_NOTICE
  6784. "failed to read fan level for later "
  6785. "restore during resume: %d\n", rc);
  6786. /* if it is undefined, don't attempt to restore it.
  6787. * KEEP THIS LAST */
  6788. if (tp_features.fan_ctrl_status_undef)
  6789. fan_control_resume_level = 0;
  6790. }
  6791. static void fan_resume(void)
  6792. {
  6793. u8 current_level = 7;
  6794. bool do_set = false;
  6795. int rc;
  6796. /* DSDT *always* updates status on resume */
  6797. tp_features.fan_ctrl_status_undef = 0;
  6798. if (!fan_control_allowed ||
  6799. !fan_control_resume_level ||
  6800. (fan_get_status_safe(&current_level) < 0))
  6801. return;
  6802. switch (fan_control_access_mode) {
  6803. case TPACPI_FAN_WR_ACPI_SFAN:
  6804. /* never decrease fan level */
  6805. do_set = (fan_control_resume_level > current_level);
  6806. break;
  6807. case TPACPI_FAN_WR_ACPI_FANS:
  6808. case TPACPI_FAN_WR_TPEC:
  6809. /* never decrease fan level, scale is:
  6810. * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
  6811. *
  6812. * We expect the firmware to set either 7 or AUTO, but we
  6813. * handle FULLSPEED out of paranoia.
  6814. *
  6815. * So, we can safely only restore FULLSPEED or 7, anything
  6816. * else could slow the fan. Restoring AUTO is useless, at
  6817. * best that's exactly what the DSDT already set (it is the
  6818. * slower it uses).
  6819. *
  6820. * Always keep in mind that the DSDT *will* have set the
  6821. * fans to what the vendor supposes is the best level. We
  6822. * muck with it only to speed the fan up.
  6823. */
  6824. if (fan_control_resume_level != 7 &&
  6825. !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
  6826. return;
  6827. else
  6828. do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
  6829. (current_level != fan_control_resume_level);
  6830. break;
  6831. default:
  6832. return;
  6833. }
  6834. if (do_set) {
  6835. printk(TPACPI_NOTICE
  6836. "restoring fan level to 0x%02x\n",
  6837. fan_control_resume_level);
  6838. rc = fan_set_level_safe(fan_control_resume_level);
  6839. if (rc < 0)
  6840. printk(TPACPI_NOTICE
  6841. "failed to restore fan level: %d\n", rc);
  6842. }
  6843. }
  6844. static int fan_read(struct seq_file *m)
  6845. {
  6846. int rc;
  6847. u8 status;
  6848. unsigned int speed = 0;
  6849. switch (fan_status_access_mode) {
  6850. case TPACPI_FAN_RD_ACPI_GFAN:
  6851. /* 570, 600e/x, 770e, 770x */
  6852. rc = fan_get_status_safe(&status);
  6853. if (rc < 0)
  6854. return rc;
  6855. seq_printf(m, "status:\t\t%s\n"
  6856. "level:\t\t%d\n",
  6857. (status != 0) ? "enabled" : "disabled", status);
  6858. break;
  6859. case TPACPI_FAN_RD_TPEC:
  6860. /* all except 570, 600e/x, 770e, 770x */
  6861. rc = fan_get_status_safe(&status);
  6862. if (rc < 0)
  6863. return rc;
  6864. seq_printf(m, "status:\t\t%s\n",
  6865. (status != 0) ? "enabled" : "disabled");
  6866. rc = fan_get_speed(&speed);
  6867. if (rc < 0)
  6868. return rc;
  6869. seq_printf(m, "speed:\t\t%d\n", speed);
  6870. if (status & TP_EC_FAN_FULLSPEED)
  6871. /* Disengaged mode takes precedence */
  6872. seq_printf(m, "level:\t\tdisengaged\n");
  6873. else if (status & TP_EC_FAN_AUTO)
  6874. seq_printf(m, "level:\t\tauto\n");
  6875. else
  6876. seq_printf(m, "level:\t\t%d\n", status);
  6877. break;
  6878. case TPACPI_FAN_NONE:
  6879. default:
  6880. seq_printf(m, "status:\t\tnot supported\n");
  6881. }
  6882. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  6883. seq_printf(m, "commands:\tlevel <level>");
  6884. switch (fan_control_access_mode) {
  6885. case TPACPI_FAN_WR_ACPI_SFAN:
  6886. seq_printf(m, " (<level> is 0-7)\n");
  6887. break;
  6888. default:
  6889. seq_printf(m, " (<level> is 0-7, "
  6890. "auto, disengaged, full-speed)\n");
  6891. break;
  6892. }
  6893. }
  6894. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  6895. seq_printf(m, "commands:\tenable, disable\n"
  6896. "commands:\twatchdog <timeout> (<timeout> "
  6897. "is 0 (off), 1-120 (seconds))\n");
  6898. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  6899. seq_printf(m, "commands:\tspeed <speed>"
  6900. " (<speed> is 0-65535)\n");
  6901. return 0;
  6902. }
  6903. static int fan_write_cmd_level(const char *cmd, int *rc)
  6904. {
  6905. int level;
  6906. if (strlencmp(cmd, "level auto") == 0)
  6907. level = TP_EC_FAN_AUTO;
  6908. else if ((strlencmp(cmd, "level disengaged") == 0) |
  6909. (strlencmp(cmd, "level full-speed") == 0))
  6910. level = TP_EC_FAN_FULLSPEED;
  6911. else if (sscanf(cmd, "level %d", &level) != 1)
  6912. return 0;
  6913. *rc = fan_set_level_safe(level);
  6914. if (*rc == -ENXIO)
  6915. printk(TPACPI_ERR "level command accepted for unsupported "
  6916. "access mode %d", fan_control_access_mode);
  6917. else if (!*rc)
  6918. tpacpi_disclose_usertask("procfs fan",
  6919. "set level to %d\n", level);
  6920. return 1;
  6921. }
  6922. static int fan_write_cmd_enable(const char *cmd, int *rc)
  6923. {
  6924. if (strlencmp(cmd, "enable") != 0)
  6925. return 0;
  6926. *rc = fan_set_enable();
  6927. if (*rc == -ENXIO)
  6928. printk(TPACPI_ERR "enable command accepted for unsupported "
  6929. "access mode %d", fan_control_access_mode);
  6930. else if (!*rc)
  6931. tpacpi_disclose_usertask("procfs fan", "enable\n");
  6932. return 1;
  6933. }
  6934. static int fan_write_cmd_disable(const char *cmd, int *rc)
  6935. {
  6936. if (strlencmp(cmd, "disable") != 0)
  6937. return 0;
  6938. *rc = fan_set_disable();
  6939. if (*rc == -ENXIO)
  6940. printk(TPACPI_ERR "disable command accepted for unsupported "
  6941. "access mode %d", fan_control_access_mode);
  6942. else if (!*rc)
  6943. tpacpi_disclose_usertask("procfs fan", "disable\n");
  6944. return 1;
  6945. }
  6946. static int fan_write_cmd_speed(const char *cmd, int *rc)
  6947. {
  6948. int speed;
  6949. /* TODO:
  6950. * Support speed <low> <medium> <high> ? */
  6951. if (sscanf(cmd, "speed %d", &speed) != 1)
  6952. return 0;
  6953. *rc = fan_set_speed(speed);
  6954. if (*rc == -ENXIO)
  6955. printk(TPACPI_ERR "speed command accepted for unsupported "
  6956. "access mode %d", fan_control_access_mode);
  6957. else if (!*rc)
  6958. tpacpi_disclose_usertask("procfs fan",
  6959. "set speed to %d\n", speed);
  6960. return 1;
  6961. }
  6962. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  6963. {
  6964. int interval;
  6965. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  6966. return 0;
  6967. if (interval < 0 || interval > 120)
  6968. *rc = -EINVAL;
  6969. else {
  6970. fan_watchdog_maxinterval = interval;
  6971. tpacpi_disclose_usertask("procfs fan",
  6972. "set watchdog timer to %d\n",
  6973. interval);
  6974. }
  6975. return 1;
  6976. }
  6977. static int fan_write(char *buf)
  6978. {
  6979. char *cmd;
  6980. int rc = 0;
  6981. while (!rc && (cmd = next_cmd(&buf))) {
  6982. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  6983. fan_write_cmd_level(cmd, &rc)) &&
  6984. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  6985. (fan_write_cmd_enable(cmd, &rc) ||
  6986. fan_write_cmd_disable(cmd, &rc) ||
  6987. fan_write_cmd_watchdog(cmd, &rc))) &&
  6988. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  6989. fan_write_cmd_speed(cmd, &rc))
  6990. )
  6991. rc = -EINVAL;
  6992. else if (!rc)
  6993. fan_watchdog_reset();
  6994. }
  6995. return rc;
  6996. }
  6997. static struct ibm_struct fan_driver_data = {
  6998. .name = "fan",
  6999. .read = fan_read,
  7000. .write = fan_write,
  7001. .exit = fan_exit,
  7002. .suspend = fan_suspend,
  7003. .resume = fan_resume,
  7004. };
  7005. /****************************************************************************
  7006. ****************************************************************************
  7007. *
  7008. * Infrastructure
  7009. *
  7010. ****************************************************************************
  7011. ****************************************************************************/
  7012. /*
  7013. * HKEY event callout for other subdrivers go here
  7014. * (yes, it is ugly, but it is quick, safe, and gets the job done
  7015. */
  7016. static void tpacpi_driver_event(const unsigned int hkey_event)
  7017. {
  7018. if (ibm_backlight_device) {
  7019. switch (hkey_event) {
  7020. case TP_HKEY_EV_BRGHT_UP:
  7021. case TP_HKEY_EV_BRGHT_DOWN:
  7022. tpacpi_brightness_notify_change();
  7023. }
  7024. }
  7025. if (alsa_card) {
  7026. switch (hkey_event) {
  7027. case TP_HKEY_EV_VOL_UP:
  7028. case TP_HKEY_EV_VOL_DOWN:
  7029. case TP_HKEY_EV_VOL_MUTE:
  7030. volume_alsa_notify_change();
  7031. }
  7032. }
  7033. }
  7034. static void hotkey_driver_event(const unsigned int scancode)
  7035. {
  7036. tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
  7037. }
  7038. /* sysfs name ---------------------------------------------------------- */
  7039. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  7040. struct device_attribute *attr,
  7041. char *buf)
  7042. {
  7043. return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
  7044. }
  7045. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  7046. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  7047. /* --------------------------------------------------------------------- */
  7048. /* /proc support */
  7049. static struct proc_dir_entry *proc_dir;
  7050. /*
  7051. * Module and infrastructure proble, init and exit handling
  7052. */
  7053. static int force_load;
  7054. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  7055. static const char * __init str_supported(int is_supported)
  7056. {
  7057. static char text_unsupported[] __initdata = "not supported";
  7058. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  7059. }
  7060. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  7061. static void ibm_exit(struct ibm_struct *ibm)
  7062. {
  7063. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  7064. list_del_init(&ibm->all_drivers);
  7065. if (ibm->flags.acpi_notify_installed) {
  7066. dbg_printk(TPACPI_DBG_EXIT,
  7067. "%s: acpi_remove_notify_handler\n", ibm->name);
  7068. BUG_ON(!ibm->acpi);
  7069. acpi_remove_notify_handler(*ibm->acpi->handle,
  7070. ibm->acpi->type,
  7071. dispatch_acpi_notify);
  7072. ibm->flags.acpi_notify_installed = 0;
  7073. }
  7074. if (ibm->flags.proc_created) {
  7075. dbg_printk(TPACPI_DBG_EXIT,
  7076. "%s: remove_proc_entry\n", ibm->name);
  7077. remove_proc_entry(ibm->name, proc_dir);
  7078. ibm->flags.proc_created = 0;
  7079. }
  7080. if (ibm->flags.acpi_driver_registered) {
  7081. dbg_printk(TPACPI_DBG_EXIT,
  7082. "%s: acpi_bus_unregister_driver\n", ibm->name);
  7083. BUG_ON(!ibm->acpi);
  7084. acpi_bus_unregister_driver(ibm->acpi->driver);
  7085. kfree(ibm->acpi->driver);
  7086. ibm->acpi->driver = NULL;
  7087. ibm->flags.acpi_driver_registered = 0;
  7088. }
  7089. if (ibm->flags.init_called && ibm->exit) {
  7090. ibm->exit();
  7091. ibm->flags.init_called = 0;
  7092. }
  7093. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  7094. }
  7095. static int __init ibm_init(struct ibm_init_struct *iibm)
  7096. {
  7097. int ret;
  7098. struct ibm_struct *ibm = iibm->data;
  7099. struct proc_dir_entry *entry;
  7100. BUG_ON(ibm == NULL);
  7101. INIT_LIST_HEAD(&ibm->all_drivers);
  7102. if (ibm->flags.experimental && !experimental)
  7103. return 0;
  7104. dbg_printk(TPACPI_DBG_INIT,
  7105. "probing for %s\n", ibm->name);
  7106. if (iibm->init) {
  7107. ret = iibm->init(iibm);
  7108. if (ret > 0)
  7109. return 0; /* probe failed */
  7110. if (ret)
  7111. return ret;
  7112. ibm->flags.init_called = 1;
  7113. }
  7114. if (ibm->acpi) {
  7115. if (ibm->acpi->hid) {
  7116. ret = register_tpacpi_subdriver(ibm);
  7117. if (ret)
  7118. goto err_out;
  7119. }
  7120. if (ibm->acpi->notify) {
  7121. ret = setup_acpi_notify(ibm);
  7122. if (ret == -ENODEV) {
  7123. printk(TPACPI_NOTICE "disabling subdriver %s\n",
  7124. ibm->name);
  7125. ret = 0;
  7126. goto err_out;
  7127. }
  7128. if (ret < 0)
  7129. goto err_out;
  7130. }
  7131. }
  7132. dbg_printk(TPACPI_DBG_INIT,
  7133. "%s installed\n", ibm->name);
  7134. if (ibm->read) {
  7135. mode_t mode = iibm->base_procfs_mode;
  7136. if (!mode)
  7137. mode = S_IRUGO;
  7138. if (ibm->write)
  7139. mode |= S_IWUSR;
  7140. entry = proc_create_data(ibm->name, mode, proc_dir,
  7141. &dispatch_proc_fops, ibm);
  7142. if (!entry) {
  7143. printk(TPACPI_ERR "unable to create proc entry %s\n",
  7144. ibm->name);
  7145. ret = -ENODEV;
  7146. goto err_out;
  7147. }
  7148. ibm->flags.proc_created = 1;
  7149. }
  7150. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  7151. return 0;
  7152. err_out:
  7153. dbg_printk(TPACPI_DBG_INIT,
  7154. "%s: at error exit path with result %d\n",
  7155. ibm->name, ret);
  7156. ibm_exit(ibm);
  7157. return (ret < 0)? ret : 0;
  7158. }
  7159. /* Probing */
  7160. static bool __pure __init tpacpi_is_fw_digit(const char c)
  7161. {
  7162. return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
  7163. }
  7164. /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
  7165. static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
  7166. const char t)
  7167. {
  7168. return s && strlen(s) >= 8 &&
  7169. tpacpi_is_fw_digit(s[0]) &&
  7170. tpacpi_is_fw_digit(s[1]) &&
  7171. s[2] == t && s[3] == 'T' &&
  7172. tpacpi_is_fw_digit(s[4]) &&
  7173. tpacpi_is_fw_digit(s[5]) &&
  7174. s[6] == 'W' && s[7] == 'W';
  7175. }
  7176. /* returns 0 - probe ok, or < 0 - probe error.
  7177. * Probe ok doesn't mean thinkpad found.
  7178. * On error, kfree() cleanup on tp->* is not performed, caller must do it */
  7179. static int __must_check __init get_thinkpad_model_data(
  7180. struct thinkpad_id_data *tp)
  7181. {
  7182. const struct dmi_device *dev = NULL;
  7183. char ec_fw_string[18];
  7184. char const *s;
  7185. if (!tp)
  7186. return -EINVAL;
  7187. memset(tp, 0, sizeof(*tp));
  7188. if (dmi_name_in_vendors("IBM"))
  7189. tp->vendor = PCI_VENDOR_ID_IBM;
  7190. else if (dmi_name_in_vendors("LENOVO"))
  7191. tp->vendor = PCI_VENDOR_ID_LENOVO;
  7192. else
  7193. return 0;
  7194. s = dmi_get_system_info(DMI_BIOS_VERSION);
  7195. tp->bios_version_str = kstrdup(s, GFP_KERNEL);
  7196. if (s && !tp->bios_version_str)
  7197. return -ENOMEM;
  7198. /* Really ancient ThinkPad 240X will fail this, which is fine */
  7199. if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
  7200. return 0;
  7201. tp->bios_model = tp->bios_version_str[0]
  7202. | (tp->bios_version_str[1] << 8);
  7203. tp->bios_release = (tp->bios_version_str[4] << 8)
  7204. | tp->bios_version_str[5];
  7205. /*
  7206. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  7207. * X32 or newer, all Z series; Some models must have an
  7208. * up-to-date BIOS or they will not be detected.
  7209. *
  7210. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  7211. */
  7212. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  7213. if (sscanf(dev->name,
  7214. "IBM ThinkPad Embedded Controller -[%17c",
  7215. ec_fw_string) == 1) {
  7216. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  7217. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  7218. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  7219. if (!tp->ec_version_str)
  7220. return -ENOMEM;
  7221. if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
  7222. tp->ec_model = ec_fw_string[0]
  7223. | (ec_fw_string[1] << 8);
  7224. tp->ec_release = (ec_fw_string[4] << 8)
  7225. | ec_fw_string[5];
  7226. } else {
  7227. printk(TPACPI_NOTICE
  7228. "ThinkPad firmware release %s "
  7229. "doesn't match the known patterns\n",
  7230. ec_fw_string);
  7231. printk(TPACPI_NOTICE
  7232. "please report this to %s\n",
  7233. TPACPI_MAIL);
  7234. }
  7235. break;
  7236. }
  7237. }
  7238. s = dmi_get_system_info(DMI_PRODUCT_VERSION);
  7239. if (s && !strnicmp(s, "ThinkPad", 8)) {
  7240. tp->model_str = kstrdup(s, GFP_KERNEL);
  7241. if (!tp->model_str)
  7242. return -ENOMEM;
  7243. }
  7244. s = dmi_get_system_info(DMI_PRODUCT_NAME);
  7245. tp->nummodel_str = kstrdup(s, GFP_KERNEL);
  7246. if (s && !tp->nummodel_str)
  7247. return -ENOMEM;
  7248. return 0;
  7249. }
  7250. static int __init probe_for_thinkpad(void)
  7251. {
  7252. int is_thinkpad;
  7253. if (acpi_disabled)
  7254. return -ENODEV;
  7255. /* It would be dangerous to run the driver in this case */
  7256. if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
  7257. return -ENODEV;
  7258. /*
  7259. * Non-ancient models have better DMI tagging, but very old models
  7260. * don't. tpacpi_is_fw_known() is a cheat to help in that case.
  7261. */
  7262. is_thinkpad = (thinkpad_id.model_str != NULL) ||
  7263. (thinkpad_id.ec_model != 0) ||
  7264. tpacpi_is_fw_known();
  7265. /* The EC handler is required */
  7266. tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
  7267. if (!ec_handle) {
  7268. if (is_thinkpad)
  7269. printk(TPACPI_ERR
  7270. "Not yet supported ThinkPad detected!\n");
  7271. return -ENODEV;
  7272. }
  7273. if (!is_thinkpad && !force_load)
  7274. return -ENODEV;
  7275. return 0;
  7276. }
  7277. static void __init thinkpad_acpi_init_banner(void)
  7278. {
  7279. printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
  7280. printk(TPACPI_INFO "%s\n", TPACPI_URL);
  7281. printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
  7282. (thinkpad_id.bios_version_str) ?
  7283. thinkpad_id.bios_version_str : "unknown",
  7284. (thinkpad_id.ec_version_str) ?
  7285. thinkpad_id.ec_version_str : "unknown");
  7286. BUG_ON(!thinkpad_id.vendor);
  7287. if (thinkpad_id.model_str)
  7288. printk(TPACPI_INFO "%s %s, model %s\n",
  7289. (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
  7290. "IBM" : ((thinkpad_id.vendor ==
  7291. PCI_VENDOR_ID_LENOVO) ?
  7292. "Lenovo" : "Unknown vendor"),
  7293. thinkpad_id.model_str,
  7294. (thinkpad_id.nummodel_str) ?
  7295. thinkpad_id.nummodel_str : "unknown");
  7296. }
  7297. /* Module init, exit, parameters */
  7298. static struct ibm_init_struct ibms_init[] __initdata = {
  7299. {
  7300. .data = &thinkpad_acpi_driver_data,
  7301. },
  7302. {
  7303. .init = hotkey_init,
  7304. .data = &hotkey_driver_data,
  7305. },
  7306. {
  7307. .init = bluetooth_init,
  7308. .data = &bluetooth_driver_data,
  7309. },
  7310. {
  7311. .init = wan_init,
  7312. .data = &wan_driver_data,
  7313. },
  7314. {
  7315. .init = uwb_init,
  7316. .data = &uwb_driver_data,
  7317. },
  7318. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  7319. {
  7320. .init = video_init,
  7321. .base_procfs_mode = S_IRUSR,
  7322. .data = &video_driver_data,
  7323. },
  7324. #endif
  7325. {
  7326. .init = light_init,
  7327. .data = &light_driver_data,
  7328. },
  7329. {
  7330. .init = cmos_init,
  7331. .data = &cmos_driver_data,
  7332. },
  7333. {
  7334. .init = led_init,
  7335. .data = &led_driver_data,
  7336. },
  7337. {
  7338. .init = beep_init,
  7339. .data = &beep_driver_data,
  7340. },
  7341. {
  7342. .init = thermal_init,
  7343. .data = &thermal_driver_data,
  7344. },
  7345. {
  7346. .init = brightness_init,
  7347. .data = &brightness_driver_data,
  7348. },
  7349. {
  7350. .init = volume_init,
  7351. .data = &volume_driver_data,
  7352. },
  7353. {
  7354. .init = fan_init,
  7355. .data = &fan_driver_data,
  7356. },
  7357. };
  7358. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  7359. {
  7360. unsigned int i;
  7361. struct ibm_struct *ibm;
  7362. if (!kp || !kp->name || !val)
  7363. return -EINVAL;
  7364. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  7365. ibm = ibms_init[i].data;
  7366. WARN_ON(ibm == NULL);
  7367. if (!ibm || !ibm->name)
  7368. continue;
  7369. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  7370. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  7371. return -ENOSPC;
  7372. strcpy(ibms_init[i].param, val);
  7373. strcat(ibms_init[i].param, ",");
  7374. return 0;
  7375. }
  7376. }
  7377. return -EINVAL;
  7378. }
  7379. module_param(experimental, int, 0444);
  7380. MODULE_PARM_DESC(experimental,
  7381. "Enables experimental features when non-zero");
  7382. module_param_named(debug, dbg_level, uint, 0);
  7383. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  7384. module_param(force_load, bool, 0444);
  7385. MODULE_PARM_DESC(force_load,
  7386. "Attempts to load the driver even on a "
  7387. "mis-identified ThinkPad when true");
  7388. module_param_named(fan_control, fan_control_allowed, bool, 0444);
  7389. MODULE_PARM_DESC(fan_control,
  7390. "Enables setting fan parameters features when true");
  7391. module_param_named(brightness_mode, brightness_mode, uint, 0444);
  7392. MODULE_PARM_DESC(brightness_mode,
  7393. "Selects brightness control strategy: "
  7394. "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
  7395. module_param(brightness_enable, uint, 0444);
  7396. MODULE_PARM_DESC(brightness_enable,
  7397. "Enables backlight control when 1, disables when 0");
  7398. module_param(hotkey_report_mode, uint, 0444);
  7399. MODULE_PARM_DESC(hotkey_report_mode,
  7400. "used for backwards compatibility with userspace, "
  7401. "see documentation");
  7402. #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
  7403. module_param_named(volume_mode, volume_mode, uint, 0444);
  7404. MODULE_PARM_DESC(volume_mode,
  7405. "Selects volume control strategy: "
  7406. "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
  7407. module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
  7408. MODULE_PARM_DESC(volume_capabilities,
  7409. "Selects the mixer capabilites: "
  7410. "0=auto, 1=volume and mute, 2=mute only");
  7411. module_param_named(volume_control, volume_control_allowed, bool, 0444);
  7412. MODULE_PARM_DESC(volume_control,
  7413. "Enables software override for the console audio "
  7414. "control when true");
  7415. /* ALSA module API parameters */
  7416. module_param_named(index, alsa_index, int, 0444);
  7417. MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
  7418. module_param_named(id, alsa_id, charp, 0444);
  7419. MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
  7420. module_param_named(enable, alsa_enable, bool, 0444);
  7421. MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
  7422. #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  7423. #define TPACPI_PARAM(feature) \
  7424. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  7425. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
  7426. "at module load, see documentation")
  7427. TPACPI_PARAM(hotkey);
  7428. TPACPI_PARAM(bluetooth);
  7429. TPACPI_PARAM(video);
  7430. TPACPI_PARAM(light);
  7431. TPACPI_PARAM(cmos);
  7432. TPACPI_PARAM(led);
  7433. TPACPI_PARAM(beep);
  7434. TPACPI_PARAM(brightness);
  7435. TPACPI_PARAM(volume);
  7436. TPACPI_PARAM(fan);
  7437. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  7438. module_param(dbg_wlswemul, uint, 0444);
  7439. MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
  7440. module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
  7441. MODULE_PARM_DESC(wlsw_state,
  7442. "Initial state of the emulated WLSW switch");
  7443. module_param(dbg_bluetoothemul, uint, 0444);
  7444. MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
  7445. module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
  7446. MODULE_PARM_DESC(bluetooth_state,
  7447. "Initial state of the emulated bluetooth switch");
  7448. module_param(dbg_wwanemul, uint, 0444);
  7449. MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
  7450. module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
  7451. MODULE_PARM_DESC(wwan_state,
  7452. "Initial state of the emulated WWAN switch");
  7453. module_param(dbg_uwbemul, uint, 0444);
  7454. MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
  7455. module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
  7456. MODULE_PARM_DESC(uwb_state,
  7457. "Initial state of the emulated UWB switch");
  7458. #endif
  7459. static void thinkpad_acpi_module_exit(void)
  7460. {
  7461. struct ibm_struct *ibm, *itmp;
  7462. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  7463. list_for_each_entry_safe_reverse(ibm, itmp,
  7464. &tpacpi_all_drivers,
  7465. all_drivers) {
  7466. ibm_exit(ibm);
  7467. }
  7468. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  7469. if (tpacpi_inputdev) {
  7470. if (tp_features.input_device_registered)
  7471. input_unregister_device(tpacpi_inputdev);
  7472. else
  7473. input_free_device(tpacpi_inputdev);
  7474. }
  7475. if (tpacpi_hwmon)
  7476. hwmon_device_unregister(tpacpi_hwmon);
  7477. if (tp_features.sensors_pdev_attrs_registered)
  7478. device_remove_file(&tpacpi_sensors_pdev->dev,
  7479. &dev_attr_thinkpad_acpi_pdev_name);
  7480. if (tpacpi_sensors_pdev)
  7481. platform_device_unregister(tpacpi_sensors_pdev);
  7482. if (tpacpi_pdev)
  7483. platform_device_unregister(tpacpi_pdev);
  7484. if (tp_features.sensors_pdrv_attrs_registered)
  7485. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  7486. if (tp_features.platform_drv_attrs_registered)
  7487. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  7488. if (tp_features.sensors_pdrv_registered)
  7489. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  7490. if (tp_features.platform_drv_registered)
  7491. platform_driver_unregister(&tpacpi_pdriver);
  7492. if (proc_dir)
  7493. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  7494. if (tpacpi_wq)
  7495. destroy_workqueue(tpacpi_wq);
  7496. kfree(thinkpad_id.bios_version_str);
  7497. kfree(thinkpad_id.ec_version_str);
  7498. kfree(thinkpad_id.model_str);
  7499. }
  7500. static int __init thinkpad_acpi_module_init(void)
  7501. {
  7502. int ret, i;
  7503. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  7504. /* Parameter checking */
  7505. if (hotkey_report_mode > 2)
  7506. return -EINVAL;
  7507. /* Driver-level probe */
  7508. ret = get_thinkpad_model_data(&thinkpad_id);
  7509. if (ret) {
  7510. printk(TPACPI_ERR
  7511. "unable to get DMI data: %d\n", ret);
  7512. thinkpad_acpi_module_exit();
  7513. return ret;
  7514. }
  7515. ret = probe_for_thinkpad();
  7516. if (ret) {
  7517. thinkpad_acpi_module_exit();
  7518. return ret;
  7519. }
  7520. /* Driver initialization */
  7521. thinkpad_acpi_init_banner();
  7522. tpacpi_check_outdated_fw();
  7523. TPACPI_ACPIHANDLE_INIT(ecrd);
  7524. TPACPI_ACPIHANDLE_INIT(ecwr);
  7525. tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
  7526. if (!tpacpi_wq) {
  7527. thinkpad_acpi_module_exit();
  7528. return -ENOMEM;
  7529. }
  7530. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  7531. if (!proc_dir) {
  7532. printk(TPACPI_ERR
  7533. "unable to create proc dir " TPACPI_PROC_DIR);
  7534. thinkpad_acpi_module_exit();
  7535. return -ENODEV;
  7536. }
  7537. ret = platform_driver_register(&tpacpi_pdriver);
  7538. if (ret) {
  7539. printk(TPACPI_ERR
  7540. "unable to register main platform driver\n");
  7541. thinkpad_acpi_module_exit();
  7542. return ret;
  7543. }
  7544. tp_features.platform_drv_registered = 1;
  7545. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  7546. if (ret) {
  7547. printk(TPACPI_ERR
  7548. "unable to register hwmon platform driver\n");
  7549. thinkpad_acpi_module_exit();
  7550. return ret;
  7551. }
  7552. tp_features.sensors_pdrv_registered = 1;
  7553. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  7554. if (!ret) {
  7555. tp_features.platform_drv_attrs_registered = 1;
  7556. ret = tpacpi_create_driver_attributes(
  7557. &tpacpi_hwmon_pdriver.driver);
  7558. }
  7559. if (ret) {
  7560. printk(TPACPI_ERR
  7561. "unable to create sysfs driver attributes\n");
  7562. thinkpad_acpi_module_exit();
  7563. return ret;
  7564. }
  7565. tp_features.sensors_pdrv_attrs_registered = 1;
  7566. /* Device initialization */
  7567. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
  7568. NULL, 0);
  7569. if (IS_ERR(tpacpi_pdev)) {
  7570. ret = PTR_ERR(tpacpi_pdev);
  7571. tpacpi_pdev = NULL;
  7572. printk(TPACPI_ERR "unable to register platform device\n");
  7573. thinkpad_acpi_module_exit();
  7574. return ret;
  7575. }
  7576. tpacpi_sensors_pdev = platform_device_register_simple(
  7577. TPACPI_HWMON_DRVR_NAME,
  7578. -1, NULL, 0);
  7579. if (IS_ERR(tpacpi_sensors_pdev)) {
  7580. ret = PTR_ERR(tpacpi_sensors_pdev);
  7581. tpacpi_sensors_pdev = NULL;
  7582. printk(TPACPI_ERR
  7583. "unable to register hwmon platform device\n");
  7584. thinkpad_acpi_module_exit();
  7585. return ret;
  7586. }
  7587. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  7588. &dev_attr_thinkpad_acpi_pdev_name);
  7589. if (ret) {
  7590. printk(TPACPI_ERR
  7591. "unable to create sysfs hwmon device attributes\n");
  7592. thinkpad_acpi_module_exit();
  7593. return ret;
  7594. }
  7595. tp_features.sensors_pdev_attrs_registered = 1;
  7596. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  7597. if (IS_ERR(tpacpi_hwmon)) {
  7598. ret = PTR_ERR(tpacpi_hwmon);
  7599. tpacpi_hwmon = NULL;
  7600. printk(TPACPI_ERR "unable to register hwmon device\n");
  7601. thinkpad_acpi_module_exit();
  7602. return ret;
  7603. }
  7604. mutex_init(&tpacpi_inputdev_send_mutex);
  7605. tpacpi_inputdev = input_allocate_device();
  7606. if (!tpacpi_inputdev) {
  7607. printk(TPACPI_ERR "unable to allocate input device\n");
  7608. thinkpad_acpi_module_exit();
  7609. return -ENOMEM;
  7610. } else {
  7611. /* Prepare input device, but don't register */
  7612. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  7613. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  7614. tpacpi_inputdev->id.bustype = BUS_HOST;
  7615. tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
  7616. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  7617. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  7618. tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
  7619. }
  7620. /* Init subdriver dependencies */
  7621. tpacpi_detect_brightness_capabilities();
  7622. /* Init subdrivers */
  7623. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  7624. ret = ibm_init(&ibms_init[i]);
  7625. if (ret >= 0 && *ibms_init[i].param)
  7626. ret = ibms_init[i].data->write(ibms_init[i].param);
  7627. if (ret < 0) {
  7628. thinkpad_acpi_module_exit();
  7629. return ret;
  7630. }
  7631. }
  7632. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  7633. ret = input_register_device(tpacpi_inputdev);
  7634. if (ret < 0) {
  7635. printk(TPACPI_ERR "unable to register input device\n");
  7636. thinkpad_acpi_module_exit();
  7637. return ret;
  7638. } else {
  7639. tp_features.input_device_registered = 1;
  7640. }
  7641. return 0;
  7642. }
  7643. MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
  7644. /*
  7645. * This will autoload the driver in almost every ThinkPad
  7646. * in widespread use.
  7647. *
  7648. * Only _VERY_ old models, like the 240, 240x and 570 lack
  7649. * the HKEY event interface.
  7650. */
  7651. MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
  7652. /*
  7653. * DMI matching for module autoloading
  7654. *
  7655. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  7656. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  7657. *
  7658. * Only models listed in thinkwiki will be supported, so add yours
  7659. * if it is not there yet.
  7660. */
  7661. #define IBM_BIOS_MODULE_ALIAS(__type) \
  7662. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
  7663. /* Ancient thinkpad BIOSes have to be identified by
  7664. * BIOS type or model number, and there are far less
  7665. * BIOS types than model numbers... */
  7666. IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
  7667. MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
  7668. MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
  7669. MODULE_DESCRIPTION(TPACPI_DESC);
  7670. MODULE_VERSION(TPACPI_VERSION);
  7671. MODULE_LICENSE("GPL");
  7672. module_init(thinkpad_acpi_module_init);
  7673. module_exit(thinkpad_acpi_module_exit);