thinkpad_acpi.c 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033
  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-2007 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 IBM_VERSION "0.18"
  24. #define TPACPI_SYSFS_VERSION 0x020101
  25. /*
  26. * Changelog:
  27. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  28. * drivers/misc.
  29. *
  30. * 2006-11-22 0.13 new maintainer
  31. * changelog now lives in git commit history, and will
  32. * not be updated further in-file.
  33. *
  34. * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
  35. * 2005-03-17 0.11 support for 600e, 770x
  36. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  37. * support for 770e, G41
  38. * G40 and G41 don't have a thinklight
  39. * temperatures no longer experimental
  40. * experimental brightness control
  41. * experimental volume control
  42. * experimental fan enable/disable
  43. * 2005-01-16 0.10 fix module loading on R30, R31
  44. * 2005-01-16 0.9 support for 570, R30, R31
  45. * ultrabay support on A22p, A3x
  46. * limit arg for cmos, led, beep, drop experimental status
  47. * more capable led control on A21e, A22p, T20-22, X20
  48. * experimental temperatures and fan speed
  49. * experimental embedded controller register dump
  50. * mark more functions as __init, drop incorrect __exit
  51. * use MODULE_VERSION
  52. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  53. * fix parameter passing on module loading
  54. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  55. * thanks to Jim Radford <radford@blackbean.org>
  56. * 2004-11-08 0.8 fix init error case, don't return from a macro
  57. * thanks to Chris Wright <chrisw@osdl.org>
  58. * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
  59. * fix led control on A21e
  60. * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
  61. * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
  62. * proc file format changed
  63. * video_switch command
  64. * experimental cmos control
  65. * experimental led control
  66. * experimental acpi sounds
  67. * 2004-09-16 0.4 support for module parameters
  68. * hotkey mask can be prefixed by 0x
  69. * video output switching
  70. * video expansion control
  71. * ultrabay eject support
  72. * removed lcd brightness/on/off control, didn't work
  73. * 2004-08-17 0.3 support for R40
  74. * lcd off, brightness control
  75. * thinklight on/off
  76. * 2004-08-14 0.2 support for T series, X20
  77. * bluetooth enable/disable
  78. * hotkey events disabled by default
  79. * removed fan control, currently useless
  80. * 2004-08-09 0.1 initial release, support for X series
  81. */
  82. #include <linux/kernel.h>
  83. #include <linux/module.h>
  84. #include <linux/init.h>
  85. #include <linux/types.h>
  86. #include <linux/string.h>
  87. #include <linux/list.h>
  88. #include <linux/mutex.h>
  89. #include <linux/kthread.h>
  90. #include <linux/freezer.h>
  91. #include <linux/delay.h>
  92. #include <linux/nvram.h>
  93. #include <linux/proc_fs.h>
  94. #include <linux/sysfs.h>
  95. #include <linux/backlight.h>
  96. #include <linux/fb.h>
  97. #include <linux/platform_device.h>
  98. #include <linux/hwmon.h>
  99. #include <linux/hwmon-sysfs.h>
  100. #include <linux/input.h>
  101. #include <asm/uaccess.h>
  102. #include <linux/dmi.h>
  103. #include <linux/jiffies.h>
  104. #include <linux/workqueue.h>
  105. #include <acpi/acpi_drivers.h>
  106. #include <acpi/acnamesp.h>
  107. #include <linux/pci_ids.h>
  108. /****************************************************************************
  109. * Main driver
  110. */
  111. #define IBM_NAME "thinkpad"
  112. #define IBM_DESC "ThinkPad ACPI Extras"
  113. #define IBM_FILE IBM_NAME "_acpi"
  114. #define IBM_URL "http://ibm-acpi.sf.net/"
  115. #define IBM_MAIL "ibm-acpi-devel@lists.sourceforge.net"
  116. #define IBM_PROC_DIR "ibm"
  117. #define IBM_ACPI_EVENT_PREFIX "ibm"
  118. #define IBM_DRVR_NAME IBM_FILE
  119. #define IBM_HWMON_DRVR_NAME IBM_NAME "_hwmon"
  120. #define IBM_LOG IBM_FILE ": "
  121. #define IBM_ERR KERN_ERR IBM_LOG
  122. #define IBM_NOTICE KERN_NOTICE IBM_LOG
  123. #define IBM_INFO KERN_INFO IBM_LOG
  124. #define IBM_DEBUG KERN_DEBUG IBM_LOG
  125. #define IBM_MAX_ACPI_ARGS 3
  126. /* ThinkPad CMOS commands */
  127. #define TP_CMOS_VOLUME_DOWN 0
  128. #define TP_CMOS_VOLUME_UP 1
  129. #define TP_CMOS_VOLUME_MUTE 2
  130. #define TP_CMOS_BRIGHTNESS_UP 4
  131. #define TP_CMOS_BRIGHTNESS_DOWN 5
  132. /* NVRAM Addresses */
  133. enum tp_nvram_addr {
  134. TP_NVRAM_ADDR_HK2 = 0x57,
  135. TP_NVRAM_ADDR_THINKLIGHT = 0x58,
  136. TP_NVRAM_ADDR_VIDEO = 0x59,
  137. TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
  138. TP_NVRAM_ADDR_MIXER = 0x60,
  139. };
  140. /* NVRAM bit masks */
  141. enum {
  142. TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
  143. TP_NVRAM_MASK_HKT_ZOOM = 0x20,
  144. TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
  145. TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
  146. TP_NVRAM_MASK_THINKLIGHT = 0x10,
  147. TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
  148. TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
  149. TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
  150. TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
  151. TP_NVRAM_MASK_MUTE = 0x40,
  152. TP_NVRAM_MASK_HKT_VOLUME = 0x80,
  153. TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
  154. TP_NVRAM_POS_LEVEL_VOLUME = 0,
  155. };
  156. #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
  157. #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  158. #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
  159. /* Debugging */
  160. #define TPACPI_DBG_ALL 0xffff
  161. #define TPACPI_DBG_ALL 0xffff
  162. #define TPACPI_DBG_INIT 0x0001
  163. #define TPACPI_DBG_EXIT 0x0002
  164. #define dbg_printk(a_dbg_level, format, arg...) \
  165. do { if (dbg_level & a_dbg_level) \
  166. printk(IBM_DEBUG "%s: " format, __func__ , ## arg); } while (0)
  167. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  168. #define vdbg_printk(a_dbg_level, format, arg...) \
  169. dbg_printk(a_dbg_level, format, ## arg)
  170. static const char *str_supported(int is_supported);
  171. #else
  172. #define vdbg_printk(a_dbg_level, format, arg...)
  173. #endif
  174. /* Input IDs */
  175. #define TPACPI_HKEY_INPUT_VENDOR PCI_VENDOR_ID_IBM
  176. #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
  177. #define TPACPI_HKEY_INPUT_VERSION 0x4101
  178. /* ACPI HIDs */
  179. #define IBM_HKEY_HID "IBM0068"
  180. /* sysfs support */
  181. struct attribute_set {
  182. unsigned int members, max_members;
  183. struct attribute_group group;
  184. };
  185. /* Helpers */
  186. static int parse_strtoul(const char *buf, unsigned long max,
  187. unsigned long *value);
  188. /* Module */
  189. static int experimental;
  190. static u32 dbg_level;
  191. static int force_load;
  192. static unsigned int hotkey_report_mode;
  193. /****************************************************************************
  194. * Subdrivers
  195. */
  196. struct ibm_struct;
  197. struct tp_acpi_drv_struct {
  198. const struct acpi_device_id *hid;
  199. struct acpi_driver *driver;
  200. void (*notify) (struct ibm_struct *, u32);
  201. acpi_handle *handle;
  202. u32 type;
  203. struct acpi_device *device;
  204. };
  205. struct ibm_struct {
  206. char *name;
  207. int (*read) (char *);
  208. int (*write) (char *);
  209. void (*exit) (void);
  210. void (*resume) (void);
  211. struct list_head all_drivers;
  212. struct tp_acpi_drv_struct *acpi;
  213. struct {
  214. u8 acpi_driver_registered:1;
  215. u8 acpi_notify_installed:1;
  216. u8 proc_created:1;
  217. u8 init_called:1;
  218. u8 experimental:1;
  219. } flags;
  220. };
  221. struct ibm_init_struct {
  222. char param[32];
  223. int (*init) (struct ibm_init_struct *);
  224. struct ibm_struct *data;
  225. };
  226. static struct {
  227. #ifdef CONFIG_THINKPAD_ACPI_BAY
  228. u32 bay_status:1;
  229. u32 bay_eject:1;
  230. u32 bay_status2:1;
  231. u32 bay_eject2:1;
  232. #endif
  233. u32 bluetooth:1;
  234. u32 hotkey:1;
  235. u32 hotkey_mask:1;
  236. u32 hotkey_wlsw:1;
  237. u32 light:1;
  238. u32 light_status:1;
  239. u32 bright_16levels:1;
  240. u32 wan:1;
  241. u32 fan_ctrl_status_undef:1;
  242. u32 input_device_registered:1;
  243. u32 platform_drv_registered:1;
  244. u32 platform_drv_attrs_registered:1;
  245. u32 sensors_pdrv_registered:1;
  246. u32 sensors_pdrv_attrs_registered:1;
  247. u32 sensors_pdev_attrs_registered:1;
  248. u32 hotkey_poll_active:1;
  249. } tp_features;
  250. struct thinkpad_id_data {
  251. unsigned int vendor; /* ThinkPad vendor:
  252. * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
  253. char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
  254. char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
  255. u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
  256. u16 ec_model;
  257. char *model_str;
  258. };
  259. static struct thinkpad_id_data thinkpad_id;
  260. static LIST_HEAD(tpacpi_all_drivers);
  261. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  262. MODULE_DESCRIPTION(IBM_DESC);
  263. MODULE_VERSION(IBM_VERSION);
  264. MODULE_LICENSE("GPL");
  265. /* Please remove this in year 2009 */
  266. MODULE_ALIAS("ibm_acpi");
  267. /*
  268. * DMI matching for module autoloading
  269. *
  270. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  271. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  272. *
  273. * Only models listed in thinkwiki will be supported, so add yours
  274. * if it is not there yet.
  275. */
  276. #define IBM_BIOS_MODULE_ALIAS(__type) \
  277. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
  278. /* Non-ancient thinkpads */
  279. MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
  280. MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
  281. /* Ancient thinkpad BIOSes have to be identified by
  282. * BIOS type or model number, and there are far less
  283. * BIOS types than model numbers... */
  284. IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
  285. IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
  286. IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
  287. #define __unused __attribute__ ((unused))
  288. static enum {
  289. TPACPI_LIFE_INIT = 0,
  290. TPACPI_LIFE_RUNNING,
  291. TPACPI_LIFE_EXITING,
  292. } tpacpi_lifecycle;
  293. /****************************************************************************
  294. ****************************************************************************
  295. *
  296. * ACPI Helpers and device model
  297. *
  298. ****************************************************************************
  299. ****************************************************************************/
  300. /*************************************************************************
  301. * ACPI basic handles
  302. */
  303. static acpi_handle root_handle;
  304. #define IBM_HANDLE(object, parent, paths...) \
  305. static acpi_handle object##_handle; \
  306. static acpi_handle *object##_parent = &parent##_handle; \
  307. static char *object##_path; \
  308. static char *object##_paths[] = { paths }
  309. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  310. "\\_SB.PCI.ISA.EC", /* 570 */
  311. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  312. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  313. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  314. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  315. "\\_SB.PCI0.LPC.EC", /* all others */
  316. );
  317. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  318. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  319. /*************************************************************************
  320. * Misc ACPI handles
  321. */
  322. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  323. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  324. "\\CMS", /* R40, R40e */
  325. ); /* all others */
  326. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  327. "^HKEY", /* R30, R31 */
  328. "HKEY", /* all others */
  329. ); /* 570 */
  330. /*************************************************************************
  331. * ACPI helpers
  332. */
  333. static int acpi_evalf(acpi_handle handle,
  334. void *res, char *method, char *fmt, ...)
  335. {
  336. char *fmt0 = fmt;
  337. struct acpi_object_list params;
  338. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  339. struct acpi_buffer result, *resultp;
  340. union acpi_object out_obj;
  341. acpi_status status;
  342. va_list ap;
  343. char res_type;
  344. int success;
  345. int quiet;
  346. if (!*fmt) {
  347. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  348. return 0;
  349. }
  350. if (*fmt == 'q') {
  351. quiet = 1;
  352. fmt++;
  353. } else
  354. quiet = 0;
  355. res_type = *(fmt++);
  356. params.count = 0;
  357. params.pointer = &in_objs[0];
  358. va_start(ap, fmt);
  359. while (*fmt) {
  360. char c = *(fmt++);
  361. switch (c) {
  362. case 'd': /* int */
  363. in_objs[params.count].integer.value = va_arg(ap, int);
  364. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  365. break;
  366. /* add more types as needed */
  367. default:
  368. printk(IBM_ERR "acpi_evalf() called "
  369. "with invalid format character '%c'\n", c);
  370. return 0;
  371. }
  372. }
  373. va_end(ap);
  374. if (res_type != 'v') {
  375. result.length = sizeof(out_obj);
  376. result.pointer = &out_obj;
  377. resultp = &result;
  378. } else
  379. resultp = NULL;
  380. status = acpi_evaluate_object(handle, method, &params, resultp);
  381. switch (res_type) {
  382. case 'd': /* int */
  383. if (res)
  384. *(int *)res = out_obj.integer.value;
  385. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  386. break;
  387. case 'v': /* void */
  388. success = status == AE_OK;
  389. break;
  390. /* add more types as needed */
  391. default:
  392. printk(IBM_ERR "acpi_evalf() called "
  393. "with invalid format character '%c'\n", res_type);
  394. return 0;
  395. }
  396. if (!success && !quiet)
  397. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  398. method, fmt0, status);
  399. return success;
  400. }
  401. static void __unused acpi_print_int(acpi_handle handle, char *method)
  402. {
  403. int i;
  404. if (acpi_evalf(handle, &i, method, "d"))
  405. printk(IBM_INFO "%s = 0x%x\n", method, i);
  406. else
  407. printk(IBM_ERR "error calling %s\n", method);
  408. }
  409. static int acpi_ec_read(int i, u8 * p)
  410. {
  411. int v;
  412. if (ecrd_handle) {
  413. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  414. return 0;
  415. *p = v;
  416. } else {
  417. if (ec_read(i, p) < 0)
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. static int acpi_ec_write(int i, u8 v)
  423. {
  424. if (ecwr_handle) {
  425. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  426. return 0;
  427. } else {
  428. if (ec_write(i, v) < 0)
  429. return 0;
  430. }
  431. return 1;
  432. }
  433. static int _sta(acpi_handle handle)
  434. {
  435. int status;
  436. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  437. status = 0;
  438. return status;
  439. }
  440. static int issue_thinkpad_cmos_command(int cmos_cmd)
  441. {
  442. if (!cmos_handle)
  443. return -ENXIO;
  444. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  445. return -EIO;
  446. return 0;
  447. }
  448. /*************************************************************************
  449. * ACPI device model
  450. */
  451. #define IBM_ACPIHANDLE_INIT(object) \
  452. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  453. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  454. static void drv_acpi_handle_init(char *name,
  455. acpi_handle *handle, acpi_handle parent,
  456. char **paths, int num_paths, char **path)
  457. {
  458. int i;
  459. acpi_status status;
  460. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  461. name);
  462. for (i = 0; i < num_paths; i++) {
  463. status = acpi_get_handle(parent, paths[i], handle);
  464. if (ACPI_SUCCESS(status)) {
  465. *path = paths[i];
  466. dbg_printk(TPACPI_DBG_INIT,
  467. "Found ACPI handle %s for %s\n",
  468. *path, name);
  469. return;
  470. }
  471. }
  472. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  473. name);
  474. *handle = NULL;
  475. }
  476. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  477. {
  478. struct ibm_struct *ibm = data;
  479. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  480. return;
  481. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  482. return;
  483. ibm->acpi->notify(ibm, event);
  484. }
  485. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  486. {
  487. acpi_status status;
  488. int rc;
  489. BUG_ON(!ibm->acpi);
  490. if (!*ibm->acpi->handle)
  491. return 0;
  492. vdbg_printk(TPACPI_DBG_INIT,
  493. "setting up ACPI notify for %s\n", ibm->name);
  494. rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  495. if (rc < 0) {
  496. printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
  497. ibm->name, rc);
  498. return -ENODEV;
  499. }
  500. acpi_driver_data(ibm->acpi->device) = ibm;
  501. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  502. IBM_ACPI_EVENT_PREFIX,
  503. ibm->name);
  504. status = acpi_install_notify_handler(*ibm->acpi->handle,
  505. ibm->acpi->type, dispatch_acpi_notify, ibm);
  506. if (ACPI_FAILURE(status)) {
  507. if (status == AE_ALREADY_EXISTS) {
  508. printk(IBM_NOTICE "another device driver is already handling %s events\n",
  509. ibm->name);
  510. } else {
  511. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  512. ibm->name, status);
  513. }
  514. return -ENODEV;
  515. }
  516. ibm->flags.acpi_notify_installed = 1;
  517. return 0;
  518. }
  519. static int __init tpacpi_device_add(struct acpi_device *device)
  520. {
  521. return 0;
  522. }
  523. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  524. {
  525. int rc;
  526. dbg_printk(TPACPI_DBG_INIT,
  527. "registering %s as an ACPI driver\n", ibm->name);
  528. BUG_ON(!ibm->acpi);
  529. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  530. if (!ibm->acpi->driver) {
  531. printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
  532. return -ENOMEM;
  533. }
  534. sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
  535. ibm->acpi->driver->ids = ibm->acpi->hid;
  536. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  537. rc = acpi_bus_register_driver(ibm->acpi->driver);
  538. if (rc < 0) {
  539. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  540. ibm->name, rc);
  541. kfree(ibm->acpi->driver);
  542. ibm->acpi->driver = NULL;
  543. } else if (!rc)
  544. ibm->flags.acpi_driver_registered = 1;
  545. return rc;
  546. }
  547. /****************************************************************************
  548. ****************************************************************************
  549. *
  550. * Procfs Helpers
  551. *
  552. ****************************************************************************
  553. ****************************************************************************/
  554. static int dispatch_procfs_read(char *page, char **start, off_t off,
  555. int count, int *eof, void *data)
  556. {
  557. struct ibm_struct *ibm = data;
  558. int len;
  559. if (!ibm || !ibm->read)
  560. return -EINVAL;
  561. len = ibm->read(page);
  562. if (len < 0)
  563. return len;
  564. if (len <= off + count)
  565. *eof = 1;
  566. *start = page + off;
  567. len -= off;
  568. if (len > count)
  569. len = count;
  570. if (len < 0)
  571. len = 0;
  572. return len;
  573. }
  574. static int dispatch_procfs_write(struct file *file,
  575. const char __user * userbuf,
  576. unsigned long count, void *data)
  577. {
  578. struct ibm_struct *ibm = data;
  579. char *kernbuf;
  580. int ret;
  581. if (!ibm || !ibm->write)
  582. return -EINVAL;
  583. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  584. if (!kernbuf)
  585. return -ENOMEM;
  586. if (copy_from_user(kernbuf, userbuf, count)) {
  587. kfree(kernbuf);
  588. return -EFAULT;
  589. }
  590. kernbuf[count] = 0;
  591. strcat(kernbuf, ",");
  592. ret = ibm->write(kernbuf);
  593. if (ret == 0)
  594. ret = count;
  595. kfree(kernbuf);
  596. return ret;
  597. }
  598. static char *next_cmd(char **cmds)
  599. {
  600. char *start = *cmds;
  601. char *end;
  602. while ((end = strchr(start, ',')) && end == start)
  603. start = end + 1;
  604. if (!end)
  605. return NULL;
  606. *end = 0;
  607. *cmds = end + 1;
  608. return start;
  609. }
  610. /****************************************************************************
  611. ****************************************************************************
  612. *
  613. * Device model: input, hwmon and platform
  614. *
  615. ****************************************************************************
  616. ****************************************************************************/
  617. static struct platform_device *tpacpi_pdev;
  618. static struct platform_device *tpacpi_sensors_pdev;
  619. static struct device *tpacpi_hwmon;
  620. static struct input_dev *tpacpi_inputdev;
  621. static struct mutex tpacpi_inputdev_send_mutex;
  622. static int tpacpi_resume_handler(struct platform_device *pdev)
  623. {
  624. struct ibm_struct *ibm, *itmp;
  625. list_for_each_entry_safe(ibm, itmp,
  626. &tpacpi_all_drivers,
  627. all_drivers) {
  628. if (ibm->resume)
  629. (ibm->resume)();
  630. }
  631. return 0;
  632. }
  633. static struct platform_driver tpacpi_pdriver = {
  634. .driver = {
  635. .name = IBM_DRVR_NAME,
  636. .owner = THIS_MODULE,
  637. },
  638. .resume = tpacpi_resume_handler,
  639. };
  640. static struct platform_driver tpacpi_hwmon_pdriver = {
  641. .driver = {
  642. .name = IBM_HWMON_DRVR_NAME,
  643. .owner = THIS_MODULE,
  644. },
  645. };
  646. /*************************************************************************
  647. * thinkpad-acpi driver attributes
  648. */
  649. /* interface_version --------------------------------------------------- */
  650. static ssize_t tpacpi_driver_interface_version_show(
  651. struct device_driver *drv,
  652. char *buf)
  653. {
  654. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  655. }
  656. static DRIVER_ATTR(interface_version, S_IRUGO,
  657. tpacpi_driver_interface_version_show, NULL);
  658. /* debug_level --------------------------------------------------------- */
  659. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  660. char *buf)
  661. {
  662. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  663. }
  664. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  665. const char *buf, size_t count)
  666. {
  667. unsigned long t;
  668. if (parse_strtoul(buf, 0xffff, &t))
  669. return -EINVAL;
  670. dbg_level = t;
  671. return count;
  672. }
  673. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  674. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  675. /* version ------------------------------------------------------------- */
  676. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  677. char *buf)
  678. {
  679. return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
  680. }
  681. static DRIVER_ATTR(version, S_IRUGO,
  682. tpacpi_driver_version_show, NULL);
  683. /* --------------------------------------------------------------------- */
  684. static struct driver_attribute* tpacpi_driver_attributes[] = {
  685. &driver_attr_debug_level, &driver_attr_version,
  686. &driver_attr_interface_version,
  687. };
  688. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  689. {
  690. int i, res;
  691. i = 0;
  692. res = 0;
  693. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  694. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  695. i++;
  696. }
  697. return res;
  698. }
  699. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  700. {
  701. int i;
  702. for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  703. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  704. }
  705. /*************************************************************************
  706. * sysfs support helpers
  707. */
  708. struct attribute_set_obj {
  709. struct attribute_set s;
  710. struct attribute *a;
  711. } __attribute__((packed));
  712. static struct attribute_set *create_attr_set(unsigned int max_members,
  713. const char* name)
  714. {
  715. struct attribute_set_obj *sobj;
  716. if (max_members == 0)
  717. return NULL;
  718. /* Allocates space for implicit NULL at the end too */
  719. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  720. max_members * sizeof(struct attribute *),
  721. GFP_KERNEL);
  722. if (!sobj)
  723. return NULL;
  724. sobj->s.max_members = max_members;
  725. sobj->s.group.attrs = &sobj->a;
  726. sobj->s.group.name = name;
  727. return &sobj->s;
  728. }
  729. #define destroy_attr_set(_set) \
  730. kfree(_set);
  731. /* not multi-threaded safe, use it in a single thread per set */
  732. static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
  733. {
  734. if (!s || !attr)
  735. return -EINVAL;
  736. if (s->members >= s->max_members)
  737. return -ENOMEM;
  738. s->group.attrs[s->members] = attr;
  739. s->members++;
  740. return 0;
  741. }
  742. static int add_many_to_attr_set(struct attribute_set* s,
  743. struct attribute **attr,
  744. unsigned int count)
  745. {
  746. int i, res;
  747. for (i = 0; i < count; i++) {
  748. res = add_to_attr_set(s, attr[i]);
  749. if (res)
  750. return res;
  751. }
  752. return 0;
  753. }
  754. static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
  755. {
  756. sysfs_remove_group(kobj, &s->group);
  757. destroy_attr_set(s);
  758. }
  759. #define register_attr_set_with_sysfs(_attr_set, _kobj) \
  760. sysfs_create_group(_kobj, &_attr_set->group)
  761. static int parse_strtoul(const char *buf,
  762. unsigned long max, unsigned long *value)
  763. {
  764. char *endp;
  765. while (*buf && isspace(*buf))
  766. buf++;
  767. *value = simple_strtoul(buf, &endp, 0);
  768. while (*endp && isspace(*endp))
  769. endp++;
  770. if (*endp || *value > max)
  771. return -EINVAL;
  772. return 0;
  773. }
  774. /****************************************************************************
  775. ****************************************************************************
  776. *
  777. * Subdrivers
  778. *
  779. ****************************************************************************
  780. ****************************************************************************/
  781. /*************************************************************************
  782. * thinkpad-acpi init subdriver
  783. */
  784. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  785. {
  786. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  787. printk(IBM_INFO "%s\n", IBM_URL);
  788. printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
  789. (thinkpad_id.bios_version_str) ?
  790. thinkpad_id.bios_version_str : "unknown",
  791. (thinkpad_id.ec_version_str) ?
  792. thinkpad_id.ec_version_str : "unknown");
  793. if (thinkpad_id.vendor && thinkpad_id.model_str)
  794. printk(IBM_INFO "%s %s\n",
  795. (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
  796. "IBM" : ((thinkpad_id.vendor ==
  797. PCI_VENDOR_ID_LENOVO) ?
  798. "Lenovo" : "Unknown vendor"),
  799. thinkpad_id.model_str);
  800. return 0;
  801. }
  802. static int thinkpad_acpi_driver_read(char *p)
  803. {
  804. int len = 0;
  805. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  806. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  807. return len;
  808. }
  809. static struct ibm_struct thinkpad_acpi_driver_data = {
  810. .name = "driver",
  811. .read = thinkpad_acpi_driver_read,
  812. };
  813. /*************************************************************************
  814. * Hotkey subdriver
  815. */
  816. enum { /* hot key scan codes (derived from ACPI DSDT) */
  817. TP_ACPI_HOTKEYSCAN_FNF1 = 0,
  818. TP_ACPI_HOTKEYSCAN_FNF2,
  819. TP_ACPI_HOTKEYSCAN_FNF3,
  820. TP_ACPI_HOTKEYSCAN_FNF4,
  821. TP_ACPI_HOTKEYSCAN_FNF5,
  822. TP_ACPI_HOTKEYSCAN_FNF6,
  823. TP_ACPI_HOTKEYSCAN_FNF7,
  824. TP_ACPI_HOTKEYSCAN_FNF8,
  825. TP_ACPI_HOTKEYSCAN_FNF9,
  826. TP_ACPI_HOTKEYSCAN_FNF10,
  827. TP_ACPI_HOTKEYSCAN_FNF11,
  828. TP_ACPI_HOTKEYSCAN_FNF12,
  829. TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
  830. TP_ACPI_HOTKEYSCAN_FNINSERT,
  831. TP_ACPI_HOTKEYSCAN_FNDELETE,
  832. TP_ACPI_HOTKEYSCAN_FNHOME,
  833. TP_ACPI_HOTKEYSCAN_FNEND,
  834. TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  835. TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
  836. TP_ACPI_HOTKEYSCAN_FNSPACE,
  837. TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  838. TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  839. TP_ACPI_HOTKEYSCAN_MUTE,
  840. TP_ACPI_HOTKEYSCAN_THINKPAD,
  841. };
  842. enum { /* Keys available through NVRAM polling */
  843. TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
  844. TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
  845. };
  846. enum { /* Positions of some of the keys in hotkey masks */
  847. TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
  848. TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
  849. TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
  850. TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
  851. TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
  852. TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  853. TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
  854. TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  855. TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  856. TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
  857. TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
  858. };
  859. enum { /* NVRAM to ACPI HKEY group map */
  860. TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
  861. TP_ACPI_HKEY_ZOOM_MASK |
  862. TP_ACPI_HKEY_DISPSWTCH_MASK |
  863. TP_ACPI_HKEY_HIBERNATE_MASK,
  864. TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
  865. TP_ACPI_HKEY_BRGHTDWN_MASK,
  866. TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
  867. TP_ACPI_HKEY_VOLDWN_MASK |
  868. TP_ACPI_HKEY_MUTE_MASK,
  869. };
  870. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  871. struct tp_nvram_state {
  872. u16 thinkpad_toggle:1;
  873. u16 zoom_toggle:1;
  874. u16 display_toggle:1;
  875. u16 thinklight_toggle:1;
  876. u16 hibernate_toggle:1;
  877. u16 displayexp_toggle:1;
  878. u16 display_state:1;
  879. u16 brightness_toggle:1;
  880. u16 volume_toggle:1;
  881. u16 mute:1;
  882. u8 brightness_level;
  883. u8 volume_level;
  884. };
  885. static struct task_struct *tpacpi_hotkey_task;
  886. static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
  887. static int hotkey_poll_freq = 10; /* Hz */
  888. static struct mutex hotkey_thread_mutex;
  889. static struct mutex hotkey_thread_data_mutex;
  890. static unsigned int hotkey_config_change;
  891. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  892. #define hotkey_source_mask 0U
  893. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  894. static struct mutex hotkey_mutex;
  895. static int hotkey_orig_status;
  896. static u32 hotkey_orig_mask;
  897. static u32 hotkey_all_mask;
  898. static u32 hotkey_reserved_mask;
  899. static u32 hotkey_mask;
  900. static u16 *hotkey_keycode_map;
  901. static struct attribute_set *hotkey_dev_attributes;
  902. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  903. #define HOTKEY_CONFIG_CRITICAL_START \
  904. mutex_lock(&hotkey_thread_data_mutex); \
  905. hotkey_config_change++;
  906. #define HOTKEY_CONFIG_CRITICAL_END \
  907. mutex_unlock(&hotkey_thread_data_mutex);
  908. #else
  909. #define HOTKEY_CONFIG_CRITICAL_START
  910. #define HOTKEY_CONFIG_CRITICAL_END
  911. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  912. static int hotkey_get_wlsw(int *status)
  913. {
  914. if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
  915. return -EIO;
  916. return 0;
  917. }
  918. /*
  919. * Call with hotkey_mutex held
  920. */
  921. static int hotkey_mask_get(void)
  922. {
  923. u32 m = 0;
  924. if (tp_features.hotkey_mask) {
  925. if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
  926. return -EIO;
  927. }
  928. hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
  929. return 0;
  930. }
  931. /*
  932. * Call with hotkey_mutex held
  933. */
  934. static int hotkey_mask_set(u32 mask)
  935. {
  936. int i;
  937. int rc = 0;
  938. if (tp_features.hotkey_mask) {
  939. HOTKEY_CONFIG_CRITICAL_START
  940. for (i = 0; i < 32; i++) {
  941. u32 m = 1 << i;
  942. /* enable in firmware mask only keys not in NVRAM
  943. * mode, but enable the key in the cached hotkey_mask
  944. * regardless of mode, or the key will end up
  945. * disabled by hotkey_mask_get() */
  946. if (!acpi_evalf(hkey_handle,
  947. NULL, "MHKM", "vdd", i + 1,
  948. !!((mask & ~hotkey_source_mask) & m))) {
  949. rc = -EIO;
  950. break;
  951. } else {
  952. hotkey_mask = (hotkey_mask & ~m) | (mask & m);
  953. }
  954. }
  955. HOTKEY_CONFIG_CRITICAL_END
  956. /* hotkey_mask_get must be called unconditionally below */
  957. if (!hotkey_mask_get() && !rc &&
  958. (hotkey_mask & ~hotkey_source_mask) !=
  959. (mask & ~hotkey_source_mask)) {
  960. printk(IBM_NOTICE
  961. "requested hot key mask 0x%08x, but "
  962. "firmware forced it to 0x%08x\n",
  963. mask, hotkey_mask);
  964. }
  965. } else {
  966. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  967. HOTKEY_CONFIG_CRITICAL_START
  968. hotkey_mask = mask & hotkey_source_mask;
  969. HOTKEY_CONFIG_CRITICAL_END
  970. hotkey_mask_get();
  971. if (hotkey_mask != mask) {
  972. printk(IBM_NOTICE
  973. "requested hot key mask 0x%08x, "
  974. "forced to 0x%08x (NVRAM poll mask is "
  975. "0x%08x): no firmware mask support\n",
  976. mask, hotkey_mask, hotkey_source_mask);
  977. }
  978. #else
  979. hotkey_mask_get();
  980. rc = -ENXIO;
  981. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  982. }
  983. return rc;
  984. }
  985. static int hotkey_status_get(int *status)
  986. {
  987. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  988. return -EIO;
  989. return 0;
  990. }
  991. static int hotkey_status_set(int status)
  992. {
  993. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  994. return -EIO;
  995. return 0;
  996. }
  997. static void tpacpi_input_send_radiosw(void)
  998. {
  999. int wlsw;
  1000. mutex_lock(&tpacpi_inputdev_send_mutex);
  1001. if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
  1002. input_report_switch(tpacpi_inputdev,
  1003. SW_RADIO, !!wlsw);
  1004. input_sync(tpacpi_inputdev);
  1005. }
  1006. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1007. }
  1008. static void tpacpi_input_send_key(unsigned int scancode)
  1009. {
  1010. unsigned int keycode;
  1011. keycode = hotkey_keycode_map[scancode];
  1012. if (keycode != KEY_RESERVED) {
  1013. mutex_lock(&tpacpi_inputdev_send_mutex);
  1014. input_report_key(tpacpi_inputdev, keycode, 1);
  1015. if (keycode == KEY_UNKNOWN)
  1016. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1017. scancode);
  1018. input_sync(tpacpi_inputdev);
  1019. input_report_key(tpacpi_inputdev, keycode, 0);
  1020. if (keycode == KEY_UNKNOWN)
  1021. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1022. scancode);
  1023. input_sync(tpacpi_inputdev);
  1024. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1025. }
  1026. }
  1027. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1028. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1029. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1030. {
  1031. tpacpi_input_send_key(scancode);
  1032. if (hotkey_report_mode < 2) {
  1033. acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
  1034. 0x80, 0x1001 + scancode);
  1035. }
  1036. }
  1037. static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
  1038. {
  1039. u8 d;
  1040. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1041. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1042. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1043. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1044. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1045. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1046. }
  1047. if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
  1048. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1049. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1050. }
  1051. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1052. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1053. n->displayexp_toggle =
  1054. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1055. }
  1056. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1057. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1058. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1059. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1060. n->brightness_toggle =
  1061. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1062. }
  1063. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1064. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1065. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1066. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1067. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1068. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1069. }
  1070. }
  1071. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1072. do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
  1073. tpacpi_hotkey_send_key(__scancode); } while (0)
  1074. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1075. do { if (mask & (1 << __scancode)) \
  1076. tpacpi_hotkey_send_key(__scancode); } while (0)
  1077. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  1078. struct tp_nvram_state *newn,
  1079. u32 mask)
  1080. {
  1081. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  1082. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  1083. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  1084. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  1085. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  1086. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  1087. /* handle volume */
  1088. if (oldn->volume_toggle != newn->volume_toggle) {
  1089. if (oldn->mute != newn->mute) {
  1090. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1091. }
  1092. if (oldn->volume_level > newn->volume_level) {
  1093. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1094. } else if (oldn->volume_level < newn->volume_level) {
  1095. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1096. } else if (oldn->mute == newn->mute) {
  1097. /* repeated key presses that didn't change state */
  1098. if (newn->mute) {
  1099. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1100. } else if (newn->volume_level != 0) {
  1101. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1102. } else {
  1103. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1104. }
  1105. }
  1106. }
  1107. /* handle brightness */
  1108. if (oldn->brightness_toggle != newn->brightness_toggle) {
  1109. if (oldn->brightness_level < newn->brightness_level) {
  1110. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1111. } else if (oldn->brightness_level > newn->brightness_level) {
  1112. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1113. } else {
  1114. /* repeated key presses that didn't change state */
  1115. if (newn->brightness_level != 0) {
  1116. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1117. } else {
  1118. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1119. }
  1120. }
  1121. }
  1122. }
  1123. #undef TPACPI_COMPARE_KEY
  1124. #undef TPACPI_MAY_SEND_KEY
  1125. static int hotkey_kthread(void *data)
  1126. {
  1127. struct tp_nvram_state s[2];
  1128. u32 mask;
  1129. unsigned int si, so;
  1130. unsigned long t;
  1131. unsigned int change_detector, must_reset;
  1132. mutex_lock(&hotkey_thread_mutex);
  1133. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  1134. goto exit;
  1135. set_freezable();
  1136. so = 0;
  1137. si = 1;
  1138. t = 0;
  1139. /* Initial state for compares */
  1140. mutex_lock(&hotkey_thread_data_mutex);
  1141. change_detector = hotkey_config_change;
  1142. mask = hotkey_source_mask & hotkey_mask;
  1143. mutex_unlock(&hotkey_thread_data_mutex);
  1144. hotkey_read_nvram(&s[so], mask);
  1145. while (!kthread_should_stop() && hotkey_poll_freq) {
  1146. if (t == 0)
  1147. t = 1000/hotkey_poll_freq;
  1148. t = msleep_interruptible(t);
  1149. if (unlikely(kthread_should_stop()))
  1150. break;
  1151. must_reset = try_to_freeze();
  1152. if (t > 0 && !must_reset)
  1153. continue;
  1154. mutex_lock(&hotkey_thread_data_mutex);
  1155. if (must_reset || hotkey_config_change != change_detector) {
  1156. /* forget old state on thaw or config change */
  1157. si = so;
  1158. t = 0;
  1159. change_detector = hotkey_config_change;
  1160. }
  1161. mask = hotkey_source_mask & hotkey_mask;
  1162. mutex_unlock(&hotkey_thread_data_mutex);
  1163. if (likely(mask)) {
  1164. hotkey_read_nvram(&s[si], mask);
  1165. if (likely(si != so)) {
  1166. hotkey_compare_and_issue_event(&s[so], &s[si],
  1167. mask);
  1168. }
  1169. }
  1170. so = si;
  1171. si ^= 1;
  1172. }
  1173. exit:
  1174. mutex_unlock(&hotkey_thread_mutex);
  1175. return 0;
  1176. }
  1177. static void hotkey_poll_stop_sync(void)
  1178. {
  1179. if (tpacpi_hotkey_task) {
  1180. if (frozen(tpacpi_hotkey_task) ||
  1181. freezing(tpacpi_hotkey_task))
  1182. thaw_process(tpacpi_hotkey_task);
  1183. kthread_stop(tpacpi_hotkey_task);
  1184. tpacpi_hotkey_task = NULL;
  1185. mutex_lock(&hotkey_thread_mutex);
  1186. /* at this point, the thread did exit */
  1187. mutex_unlock(&hotkey_thread_mutex);
  1188. }
  1189. }
  1190. /* call with hotkey_mutex held */
  1191. static void hotkey_poll_setup(int may_warn)
  1192. {
  1193. if ((hotkey_source_mask & hotkey_mask) != 0 &&
  1194. hotkey_poll_freq > 0 &&
  1195. (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
  1196. if (!tpacpi_hotkey_task) {
  1197. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  1198. NULL, IBM_FILE "d");
  1199. if (IS_ERR(tpacpi_hotkey_task)) {
  1200. tpacpi_hotkey_task = NULL;
  1201. printk(IBM_ERR "could not create kernel thread "
  1202. "for hotkey polling\n");
  1203. }
  1204. }
  1205. } else {
  1206. hotkey_poll_stop_sync();
  1207. if (may_warn &&
  1208. hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
  1209. printk(IBM_NOTICE "hot keys 0x%08x require polling, "
  1210. "which is currently disabled\n",
  1211. hotkey_source_mask);
  1212. }
  1213. }
  1214. }
  1215. static void hotkey_poll_setup_safe(int may_warn)
  1216. {
  1217. mutex_lock(&hotkey_mutex);
  1218. hotkey_poll_setup(may_warn);
  1219. mutex_unlock(&hotkey_mutex);
  1220. }
  1221. static int hotkey_inputdev_open(struct input_dev *dev)
  1222. {
  1223. switch (tpacpi_lifecycle) {
  1224. case TPACPI_LIFE_INIT:
  1225. /*
  1226. * hotkey_init will call hotkey_poll_setup_safe
  1227. * at the appropriate moment
  1228. */
  1229. return 0;
  1230. case TPACPI_LIFE_EXITING:
  1231. return -EBUSY;
  1232. case TPACPI_LIFE_RUNNING:
  1233. hotkey_poll_setup_safe(0);
  1234. return 0;
  1235. }
  1236. /* Should only happen if tpacpi_lifecycle is corrupt */
  1237. BUG();
  1238. return -EBUSY;
  1239. }
  1240. static void hotkey_inputdev_close(struct input_dev *dev)
  1241. {
  1242. /* disable hotkey polling when possible */
  1243. if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
  1244. hotkey_poll_setup_safe(0);
  1245. }
  1246. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1247. /* sysfs hotkey enable ------------------------------------------------- */
  1248. static ssize_t hotkey_enable_show(struct device *dev,
  1249. struct device_attribute *attr,
  1250. char *buf)
  1251. {
  1252. int res, status;
  1253. res = hotkey_status_get(&status);
  1254. if (res)
  1255. return res;
  1256. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  1257. }
  1258. static ssize_t hotkey_enable_store(struct device *dev,
  1259. struct device_attribute *attr,
  1260. const char *buf, size_t count)
  1261. {
  1262. unsigned long t;
  1263. int res;
  1264. if (parse_strtoul(buf, 1, &t))
  1265. return -EINVAL;
  1266. res = hotkey_status_set(t);
  1267. return (res) ? res : count;
  1268. }
  1269. static struct device_attribute dev_attr_hotkey_enable =
  1270. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  1271. hotkey_enable_show, hotkey_enable_store);
  1272. /* sysfs hotkey mask --------------------------------------------------- */
  1273. static ssize_t hotkey_mask_show(struct device *dev,
  1274. struct device_attribute *attr,
  1275. char *buf)
  1276. {
  1277. int res;
  1278. if (mutex_lock_interruptible(&hotkey_mutex))
  1279. return -ERESTARTSYS;
  1280. res = hotkey_mask_get();
  1281. mutex_unlock(&hotkey_mutex);
  1282. return (res)?
  1283. res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
  1284. }
  1285. static ssize_t hotkey_mask_store(struct device *dev,
  1286. struct device_attribute *attr,
  1287. const char *buf, size_t count)
  1288. {
  1289. unsigned long t;
  1290. int res;
  1291. if (parse_strtoul(buf, 0xffffffffUL, &t))
  1292. return -EINVAL;
  1293. if (mutex_lock_interruptible(&hotkey_mutex))
  1294. return -ERESTARTSYS;
  1295. res = hotkey_mask_set(t);
  1296. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1297. hotkey_poll_setup(1);
  1298. #endif
  1299. mutex_unlock(&hotkey_mutex);
  1300. return (res) ? res : count;
  1301. }
  1302. static struct device_attribute dev_attr_hotkey_mask =
  1303. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  1304. hotkey_mask_show, hotkey_mask_store);
  1305. /* sysfs hotkey bios_enabled ------------------------------------------- */
  1306. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  1307. struct device_attribute *attr,
  1308. char *buf)
  1309. {
  1310. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
  1311. }
  1312. static struct device_attribute dev_attr_hotkey_bios_enabled =
  1313. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  1314. /* sysfs hotkey bios_mask ---------------------------------------------- */
  1315. static ssize_t hotkey_bios_mask_show(struct device *dev,
  1316. struct device_attribute *attr,
  1317. char *buf)
  1318. {
  1319. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  1320. }
  1321. static struct device_attribute dev_attr_hotkey_bios_mask =
  1322. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  1323. /* sysfs hotkey all_mask ----------------------------------------------- */
  1324. static ssize_t hotkey_all_mask_show(struct device *dev,
  1325. struct device_attribute *attr,
  1326. char *buf)
  1327. {
  1328. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  1329. hotkey_all_mask | hotkey_source_mask);
  1330. }
  1331. static struct device_attribute dev_attr_hotkey_all_mask =
  1332. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  1333. /* sysfs hotkey recommended_mask --------------------------------------- */
  1334. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  1335. struct device_attribute *attr,
  1336. char *buf)
  1337. {
  1338. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  1339. (hotkey_all_mask | hotkey_source_mask)
  1340. & ~hotkey_reserved_mask);
  1341. }
  1342. static struct device_attribute dev_attr_hotkey_recommended_mask =
  1343. __ATTR(hotkey_recommended_mask, S_IRUGO,
  1344. hotkey_recommended_mask_show, NULL);
  1345. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1346. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  1347. static ssize_t hotkey_source_mask_show(struct device *dev,
  1348. struct device_attribute *attr,
  1349. char *buf)
  1350. {
  1351. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
  1352. }
  1353. static ssize_t hotkey_source_mask_store(struct device *dev,
  1354. struct device_attribute *attr,
  1355. const char *buf, size_t count)
  1356. {
  1357. unsigned long t;
  1358. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  1359. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  1360. return -EINVAL;
  1361. if (mutex_lock_interruptible(&hotkey_mutex))
  1362. return -ERESTARTSYS;
  1363. HOTKEY_CONFIG_CRITICAL_START
  1364. hotkey_source_mask = t;
  1365. HOTKEY_CONFIG_CRITICAL_END
  1366. hotkey_poll_setup(1);
  1367. mutex_unlock(&hotkey_mutex);
  1368. return count;
  1369. }
  1370. static struct device_attribute dev_attr_hotkey_source_mask =
  1371. __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
  1372. hotkey_source_mask_show, hotkey_source_mask_store);
  1373. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  1374. static ssize_t hotkey_poll_freq_show(struct device *dev,
  1375. struct device_attribute *attr,
  1376. char *buf)
  1377. {
  1378. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
  1379. }
  1380. static ssize_t hotkey_poll_freq_store(struct device *dev,
  1381. struct device_attribute *attr,
  1382. const char *buf, size_t count)
  1383. {
  1384. unsigned long t;
  1385. if (parse_strtoul(buf, 25, &t))
  1386. return -EINVAL;
  1387. if (mutex_lock_interruptible(&hotkey_mutex))
  1388. return -ERESTARTSYS;
  1389. hotkey_poll_freq = t;
  1390. hotkey_poll_setup(1);
  1391. mutex_unlock(&hotkey_mutex);
  1392. return count;
  1393. }
  1394. static struct device_attribute dev_attr_hotkey_poll_freq =
  1395. __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
  1396. hotkey_poll_freq_show, hotkey_poll_freq_store);
  1397. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1398. /* sysfs hotkey radio_sw ----------------------------------------------- */
  1399. static ssize_t hotkey_radio_sw_show(struct device *dev,
  1400. struct device_attribute *attr,
  1401. char *buf)
  1402. {
  1403. int res, s;
  1404. res = hotkey_get_wlsw(&s);
  1405. if (res < 0)
  1406. return res;
  1407. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  1408. }
  1409. static struct device_attribute dev_attr_hotkey_radio_sw =
  1410. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  1411. /* sysfs hotkey report_mode -------------------------------------------- */
  1412. static ssize_t hotkey_report_mode_show(struct device *dev,
  1413. struct device_attribute *attr,
  1414. char *buf)
  1415. {
  1416. return snprintf(buf, PAGE_SIZE, "%d\n",
  1417. (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
  1418. }
  1419. static struct device_attribute dev_attr_hotkey_report_mode =
  1420. __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
  1421. /* --------------------------------------------------------------------- */
  1422. static struct attribute *hotkey_attributes[] __initdata = {
  1423. &dev_attr_hotkey_enable.attr,
  1424. &dev_attr_hotkey_bios_enabled.attr,
  1425. &dev_attr_hotkey_report_mode.attr,
  1426. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1427. &dev_attr_hotkey_mask.attr,
  1428. &dev_attr_hotkey_all_mask.attr,
  1429. &dev_attr_hotkey_recommended_mask.attr,
  1430. &dev_attr_hotkey_source_mask.attr,
  1431. &dev_attr_hotkey_poll_freq.attr,
  1432. #endif
  1433. };
  1434. static struct attribute *hotkey_mask_attributes[] __initdata = {
  1435. &dev_attr_hotkey_bios_mask.attr,
  1436. #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1437. &dev_attr_hotkey_mask.attr,
  1438. &dev_attr_hotkey_all_mask.attr,
  1439. &dev_attr_hotkey_recommended_mask.attr,
  1440. #endif
  1441. };
  1442. static int __init hotkey_init(struct ibm_init_struct *iibm)
  1443. {
  1444. /* Requirements for changing the default keymaps:
  1445. *
  1446. * 1. Many of the keys are mapped to KEY_RESERVED for very
  1447. * good reasons. Do not change them unless you have deep
  1448. * knowledge on the IBM and Lenovo ThinkPad firmware for
  1449. * the various ThinkPad models. The driver behaves
  1450. * differently for KEY_RESERVED: such keys have their
  1451. * hot key mask *unset* in mask_recommended, and also
  1452. * in the initial hot key mask programmed into the
  1453. * firmware at driver load time, which means the firm-
  1454. * ware may react very differently if you change them to
  1455. * something else;
  1456. *
  1457. * 2. You must be subscribed to the linux-thinkpad and
  1458. * ibm-acpi-devel mailing lists, and you should read the
  1459. * list archives since 2007 if you want to change the
  1460. * keymaps. This requirement exists so that you will
  1461. * know the past history of problems with the thinkpad-
  1462. * acpi driver keymaps, and also that you will be
  1463. * listening to any bug reports;
  1464. *
  1465. * 3. Do not send thinkpad-acpi specific patches directly to
  1466. * for merging, *ever*. Send them to the linux-acpi
  1467. * mailinglist for comments. Merging is to be done only
  1468. * through acpi-test and the ACPI maintainer.
  1469. *
  1470. * If the above is too much to ask, don't change the keymap.
  1471. * Ask the thinkpad-acpi maintainer to do it, instead.
  1472. */
  1473. static u16 ibm_keycode_map[] __initdata = {
  1474. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  1475. KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
  1476. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  1477. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  1478. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  1479. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  1480. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  1481. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  1482. /* brightness: firmware always reacts to them, unless
  1483. * X.org did some tricks in the radeon BIOS scratch
  1484. * registers of *some* models */
  1485. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  1486. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  1487. /* Thinklight: firmware always react to it */
  1488. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  1489. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  1490. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  1491. /* Volume: firmware always react to it and reprograms
  1492. * the built-in *extra* mixer. Never map it to control
  1493. * another mixer by default. */
  1494. KEY_RESERVED, /* 0x14: VOLUME UP */
  1495. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  1496. KEY_RESERVED, /* 0x16: MUTE */
  1497. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  1498. /* (assignments unknown, please report if found) */
  1499. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1500. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1501. };
  1502. static u16 lenovo_keycode_map[] __initdata = {
  1503. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  1504. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  1505. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  1506. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  1507. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  1508. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  1509. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  1510. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  1511. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  1512. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  1513. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  1514. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  1515. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  1516. /* Volume: z60/z61, T60 (BIOS version?): firmware always
  1517. * react to it and reprograms the built-in *extra* mixer.
  1518. * Never map it to control another mixer by default.
  1519. *
  1520. * T60?, T61, R60?, R61: firmware and EC tries to send
  1521. * these over the regular keyboard, so these are no-ops,
  1522. * but there are still weird bugs re. MUTE, so do not
  1523. * change unless you get test reports from all Lenovo
  1524. * models. May cause the BIOS to interfere with the
  1525. * HDA mixer.
  1526. */
  1527. KEY_RESERVED, /* 0x14: VOLUME UP */
  1528. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  1529. KEY_RESERVED, /* 0x16: MUTE */
  1530. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  1531. /* (assignments unknown, please report if found) */
  1532. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1533. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1534. };
  1535. #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
  1536. #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
  1537. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
  1538. int res, i;
  1539. int status;
  1540. int hkeyv;
  1541. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  1542. BUG_ON(!tpacpi_inputdev);
  1543. BUG_ON(tpacpi_inputdev->open != NULL ||
  1544. tpacpi_inputdev->close != NULL);
  1545. IBM_ACPIHANDLE_INIT(hkey);
  1546. mutex_init(&hotkey_mutex);
  1547. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1548. mutex_init(&hotkey_thread_mutex);
  1549. mutex_init(&hotkey_thread_data_mutex);
  1550. #endif
  1551. /* hotkey not supported on 570 */
  1552. tp_features.hotkey = hkey_handle != NULL;
  1553. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  1554. str_supported(tp_features.hotkey));
  1555. if (tp_features.hotkey) {
  1556. hotkey_dev_attributes = create_attr_set(10, NULL);
  1557. if (!hotkey_dev_attributes)
  1558. return -ENOMEM;
  1559. res = add_many_to_attr_set(hotkey_dev_attributes,
  1560. hotkey_attributes,
  1561. ARRAY_SIZE(hotkey_attributes));
  1562. if (res)
  1563. return res;
  1564. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1565. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  1566. for HKEY interface version 0x100 */
  1567. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  1568. if ((hkeyv >> 8) != 1) {
  1569. printk(IBM_ERR "unknown version of the "
  1570. "HKEY interface: 0x%x\n", hkeyv);
  1571. printk(IBM_ERR "please report this to %s\n",
  1572. IBM_MAIL);
  1573. } else {
  1574. /*
  1575. * MHKV 0x100 in A31, R40, R40e,
  1576. * T4x, X31, and later
  1577. */
  1578. tp_features.hotkey_mask = 1;
  1579. }
  1580. }
  1581. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  1582. str_supported(tp_features.hotkey_mask));
  1583. if (tp_features.hotkey_mask) {
  1584. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  1585. "MHKA", "qd")) {
  1586. printk(IBM_ERR
  1587. "missing MHKA handler, "
  1588. "please report this to %s\n",
  1589. IBM_MAIL);
  1590. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  1591. }
  1592. }
  1593. /* hotkey_source_mask *must* be zero for
  1594. * the first hotkey_mask_get */
  1595. res = hotkey_status_get(&hotkey_orig_status);
  1596. if (!res && tp_features.hotkey_mask) {
  1597. res = hotkey_mask_get();
  1598. hotkey_orig_mask = hotkey_mask;
  1599. if (!res) {
  1600. res = add_many_to_attr_set(
  1601. hotkey_dev_attributes,
  1602. hotkey_mask_attributes,
  1603. ARRAY_SIZE(hotkey_mask_attributes));
  1604. }
  1605. }
  1606. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1607. if (tp_features.hotkey_mask) {
  1608. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  1609. & ~hotkey_all_mask;
  1610. } else {
  1611. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
  1612. }
  1613. vdbg_printk(TPACPI_DBG_INIT,
  1614. "hotkey source mask 0x%08x, polling freq %d\n",
  1615. hotkey_source_mask, hotkey_poll_freq);
  1616. #endif
  1617. /* Not all thinkpads have a hardware radio switch */
  1618. if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  1619. tp_features.hotkey_wlsw = 1;
  1620. printk(IBM_INFO
  1621. "radio switch found; radios are %s\n",
  1622. enabled(status, 0));
  1623. res = add_to_attr_set(hotkey_dev_attributes,
  1624. &dev_attr_hotkey_radio_sw.attr);
  1625. }
  1626. if (!res)
  1627. res = register_attr_set_with_sysfs(
  1628. hotkey_dev_attributes,
  1629. &tpacpi_pdev->dev.kobj);
  1630. if (res)
  1631. return res;
  1632. /* Set up key map */
  1633. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  1634. GFP_KERNEL);
  1635. if (!hotkey_keycode_map) {
  1636. printk(IBM_ERR "failed to allocate memory for key map\n");
  1637. return -ENOMEM;
  1638. }
  1639. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  1640. dbg_printk(TPACPI_DBG_INIT,
  1641. "using Lenovo default hot key map\n");
  1642. memcpy(hotkey_keycode_map, &lenovo_keycode_map,
  1643. TPACPI_HOTKEY_MAP_SIZE);
  1644. } else {
  1645. dbg_printk(TPACPI_DBG_INIT,
  1646. "using IBM default hot key map\n");
  1647. memcpy(hotkey_keycode_map, &ibm_keycode_map,
  1648. TPACPI_HOTKEY_MAP_SIZE);
  1649. }
  1650. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  1651. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  1652. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  1653. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  1654. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  1655. tpacpi_inputdev->keycode = hotkey_keycode_map;
  1656. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  1657. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  1658. set_bit(hotkey_keycode_map[i],
  1659. tpacpi_inputdev->keybit);
  1660. } else {
  1661. if (i < sizeof(hotkey_reserved_mask)*8)
  1662. hotkey_reserved_mask |= 1 << i;
  1663. }
  1664. }
  1665. if (tp_features.hotkey_wlsw) {
  1666. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1667. set_bit(SW_RADIO, tpacpi_inputdev->swbit);
  1668. }
  1669. dbg_printk(TPACPI_DBG_INIT,
  1670. "enabling hot key handling\n");
  1671. res = hotkey_status_set(1);
  1672. if (res)
  1673. return res;
  1674. res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
  1675. & ~hotkey_reserved_mask)
  1676. | hotkey_orig_mask);
  1677. if (res < 0 && res != -ENXIO)
  1678. return res;
  1679. dbg_printk(TPACPI_DBG_INIT,
  1680. "legacy hot key reporting over procfs %s\n",
  1681. (hotkey_report_mode < 2) ?
  1682. "enabled" : "disabled");
  1683. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1684. tpacpi_inputdev->open = &hotkey_inputdev_open;
  1685. tpacpi_inputdev->close = &hotkey_inputdev_close;
  1686. hotkey_poll_setup_safe(1);
  1687. #endif
  1688. }
  1689. return (tp_features.hotkey)? 0 : 1;
  1690. }
  1691. static void hotkey_exit(void)
  1692. {
  1693. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1694. hotkey_poll_stop_sync();
  1695. #endif
  1696. if (tp_features.hotkey) {
  1697. dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
  1698. /* no short-circuit boolean operator below! */
  1699. if ((hotkey_mask_set(hotkey_orig_mask) |
  1700. hotkey_status_set(hotkey_orig_status)) != 0)
  1701. printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
  1702. }
  1703. if (hotkey_dev_attributes) {
  1704. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  1705. hotkey_dev_attributes = NULL;
  1706. }
  1707. }
  1708. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  1709. {
  1710. u32 hkey;
  1711. unsigned int scancode;
  1712. int send_acpi_ev;
  1713. int ignore_acpi_ev;
  1714. if (event != 0x80) {
  1715. printk(IBM_ERR "unknown HKEY notification event %d\n", event);
  1716. /* forward it to userspace, maybe it knows how to handle it */
  1717. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  1718. ibm->acpi->device->dev.bus_id,
  1719. event, 0);
  1720. return;
  1721. }
  1722. while (1) {
  1723. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  1724. printk(IBM_ERR "failed to retrieve HKEY event\n");
  1725. return;
  1726. }
  1727. if (hkey == 0) {
  1728. /* queue empty */
  1729. return;
  1730. }
  1731. send_acpi_ev = 0;
  1732. ignore_acpi_ev = 0;
  1733. switch (hkey >> 12) {
  1734. case 1:
  1735. /* 0x1000-0x1FFF: key presses */
  1736. scancode = hkey & 0xfff;
  1737. if (scancode > 0 && scancode < 0x21) {
  1738. scancode--;
  1739. if (!(hotkey_source_mask & (1 << scancode))) {
  1740. tpacpi_input_send_key(scancode);
  1741. } else {
  1742. ignore_acpi_ev = 1;
  1743. }
  1744. } else {
  1745. printk(IBM_ERR
  1746. "hotkey 0x%04x out of range for keyboard map\n",
  1747. hkey);
  1748. send_acpi_ev = 1;
  1749. }
  1750. break;
  1751. case 5:
  1752. /* 0x5000-0x5FFF: LID */
  1753. /* we don't handle it through this path, just
  1754. * eat up known LID events */
  1755. if (hkey != 0x5001 && hkey != 0x5002) {
  1756. printk(IBM_ERR
  1757. "unknown LID-related HKEY event: 0x%04x\n",
  1758. hkey);
  1759. send_acpi_ev = 1;
  1760. } else {
  1761. ignore_acpi_ev = 1;
  1762. }
  1763. break;
  1764. case 7:
  1765. /* 0x7000-0x7FFF: misc */
  1766. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  1767. tpacpi_input_send_radiosw();
  1768. break;
  1769. }
  1770. /* fallthrough to default */
  1771. default:
  1772. /* case 2: dock-related */
  1773. /* 0x2305 - T43 waking up due to bay lever eject while aslept */
  1774. /* case 3: ultra-bay related. maybe bay in dock? */
  1775. /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
  1776. printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
  1777. send_acpi_ev = 1;
  1778. }
  1779. /* Legacy events */
  1780. if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
  1781. acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
  1782. }
  1783. /* netlink events */
  1784. if (!ignore_acpi_ev && send_acpi_ev) {
  1785. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  1786. ibm->acpi->device->dev.bus_id,
  1787. event, hkey);
  1788. }
  1789. }
  1790. }
  1791. static void hotkey_resume(void)
  1792. {
  1793. if (hotkey_mask_get())
  1794. printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
  1795. tpacpi_input_send_radiosw();
  1796. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1797. hotkey_poll_setup_safe(0);
  1798. #endif
  1799. }
  1800. /* procfs -------------------------------------------------------------- */
  1801. static int hotkey_read(char *p)
  1802. {
  1803. int res, status;
  1804. int len = 0;
  1805. if (!tp_features.hotkey) {
  1806. len += sprintf(p + len, "status:\t\tnot supported\n");
  1807. return len;
  1808. }
  1809. if (mutex_lock_interruptible(&hotkey_mutex))
  1810. return -ERESTARTSYS;
  1811. res = hotkey_status_get(&status);
  1812. if (!res)
  1813. res = hotkey_mask_get();
  1814. mutex_unlock(&hotkey_mutex);
  1815. if (res)
  1816. return res;
  1817. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  1818. if (tp_features.hotkey_mask) {
  1819. len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
  1820. len += sprintf(p + len,
  1821. "commands:\tenable, disable, reset, <mask>\n");
  1822. } else {
  1823. len += sprintf(p + len, "mask:\t\tnot supported\n");
  1824. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  1825. }
  1826. return len;
  1827. }
  1828. static int hotkey_write(char *buf)
  1829. {
  1830. int res, status;
  1831. u32 mask;
  1832. char *cmd;
  1833. if (!tp_features.hotkey)
  1834. return -ENODEV;
  1835. if (mutex_lock_interruptible(&hotkey_mutex))
  1836. return -ERESTARTSYS;
  1837. status = -1;
  1838. mask = hotkey_mask;
  1839. res = 0;
  1840. while ((cmd = next_cmd(&buf))) {
  1841. if (strlencmp(cmd, "enable") == 0) {
  1842. status = 1;
  1843. } else if (strlencmp(cmd, "disable") == 0) {
  1844. status = 0;
  1845. } else if (strlencmp(cmd, "reset") == 0) {
  1846. status = hotkey_orig_status;
  1847. mask = hotkey_orig_mask;
  1848. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  1849. /* mask set */
  1850. } else if (sscanf(cmd, "%x", &mask) == 1) {
  1851. /* mask set */
  1852. } else {
  1853. res = -EINVAL;
  1854. goto errexit;
  1855. }
  1856. }
  1857. if (status != -1)
  1858. res = hotkey_status_set(status);
  1859. if (!res && mask != hotkey_mask)
  1860. res = hotkey_mask_set(mask);
  1861. errexit:
  1862. mutex_unlock(&hotkey_mutex);
  1863. return res;
  1864. }
  1865. static const struct acpi_device_id ibm_htk_device_ids[] = {
  1866. {IBM_HKEY_HID, 0},
  1867. {"", 0},
  1868. };
  1869. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  1870. .hid = ibm_htk_device_ids,
  1871. .notify = hotkey_notify,
  1872. .handle = &hkey_handle,
  1873. .type = ACPI_DEVICE_NOTIFY,
  1874. };
  1875. static struct ibm_struct hotkey_driver_data = {
  1876. .name = "hotkey",
  1877. .read = hotkey_read,
  1878. .write = hotkey_write,
  1879. .exit = hotkey_exit,
  1880. .resume = hotkey_resume,
  1881. .acpi = &ibm_hotkey_acpidriver,
  1882. };
  1883. /*************************************************************************
  1884. * Bluetooth subdriver
  1885. */
  1886. enum {
  1887. /* ACPI GBDC/SBDC bits */
  1888. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  1889. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  1890. TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
  1891. };
  1892. static int bluetooth_get_radiosw(void);
  1893. static int bluetooth_set_radiosw(int radio_on);
  1894. /* sysfs bluetooth enable ---------------------------------------------- */
  1895. static ssize_t bluetooth_enable_show(struct device *dev,
  1896. struct device_attribute *attr,
  1897. char *buf)
  1898. {
  1899. int status;
  1900. status = bluetooth_get_radiosw();
  1901. if (status < 0)
  1902. return status;
  1903. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1904. }
  1905. static ssize_t bluetooth_enable_store(struct device *dev,
  1906. struct device_attribute *attr,
  1907. const char *buf, size_t count)
  1908. {
  1909. unsigned long t;
  1910. int res;
  1911. if (parse_strtoul(buf, 1, &t))
  1912. return -EINVAL;
  1913. res = bluetooth_set_radiosw(t);
  1914. return (res) ? res : count;
  1915. }
  1916. static struct device_attribute dev_attr_bluetooth_enable =
  1917. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  1918. bluetooth_enable_show, bluetooth_enable_store);
  1919. /* --------------------------------------------------------------------- */
  1920. static struct attribute *bluetooth_attributes[] = {
  1921. &dev_attr_bluetooth_enable.attr,
  1922. NULL
  1923. };
  1924. static const struct attribute_group bluetooth_attr_group = {
  1925. .attrs = bluetooth_attributes,
  1926. };
  1927. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  1928. {
  1929. int res;
  1930. int status = 0;
  1931. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  1932. IBM_ACPIHANDLE_INIT(hkey);
  1933. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1934. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  1935. tp_features.bluetooth = hkey_handle &&
  1936. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  1937. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  1938. str_supported(tp_features.bluetooth),
  1939. status);
  1940. if (tp_features.bluetooth) {
  1941. if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  1942. /* no bluetooth hardware present in system */
  1943. tp_features.bluetooth = 0;
  1944. dbg_printk(TPACPI_DBG_INIT,
  1945. "bluetooth hardware not installed\n");
  1946. } else {
  1947. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1948. &bluetooth_attr_group);
  1949. if (res)
  1950. return res;
  1951. }
  1952. }
  1953. return (tp_features.bluetooth)? 0 : 1;
  1954. }
  1955. static void bluetooth_exit(void)
  1956. {
  1957. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1958. &bluetooth_attr_group);
  1959. }
  1960. static int bluetooth_get_radiosw(void)
  1961. {
  1962. int status;
  1963. if (!tp_features.bluetooth)
  1964. return -ENODEV;
  1965. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1966. return -EIO;
  1967. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  1968. }
  1969. static int bluetooth_set_radiosw(int radio_on)
  1970. {
  1971. int status;
  1972. if (!tp_features.bluetooth)
  1973. return -ENODEV;
  1974. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1975. return -EIO;
  1976. if (radio_on)
  1977. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  1978. else
  1979. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  1980. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  1981. return -EIO;
  1982. return 0;
  1983. }
  1984. /* procfs -------------------------------------------------------------- */
  1985. static int bluetooth_read(char *p)
  1986. {
  1987. int len = 0;
  1988. int status = bluetooth_get_radiosw();
  1989. if (!tp_features.bluetooth)
  1990. len += sprintf(p + len, "status:\t\tnot supported\n");
  1991. else {
  1992. len += sprintf(p + len, "status:\t\t%s\n",
  1993. (status)? "enabled" : "disabled");
  1994. len += sprintf(p + len, "commands:\tenable, disable\n");
  1995. }
  1996. return len;
  1997. }
  1998. static int bluetooth_write(char *buf)
  1999. {
  2000. char *cmd;
  2001. if (!tp_features.bluetooth)
  2002. return -ENODEV;
  2003. while ((cmd = next_cmd(&buf))) {
  2004. if (strlencmp(cmd, "enable") == 0) {
  2005. bluetooth_set_radiosw(1);
  2006. } else if (strlencmp(cmd, "disable") == 0) {
  2007. bluetooth_set_radiosw(0);
  2008. } else
  2009. return -EINVAL;
  2010. }
  2011. return 0;
  2012. }
  2013. static struct ibm_struct bluetooth_driver_data = {
  2014. .name = "bluetooth",
  2015. .read = bluetooth_read,
  2016. .write = bluetooth_write,
  2017. .exit = bluetooth_exit,
  2018. };
  2019. /*************************************************************************
  2020. * Wan subdriver
  2021. */
  2022. enum {
  2023. /* ACPI GWAN/SWAN bits */
  2024. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  2025. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  2026. TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
  2027. };
  2028. static int wan_get_radiosw(void);
  2029. static int wan_set_radiosw(int radio_on);
  2030. /* sysfs wan enable ---------------------------------------------------- */
  2031. static ssize_t wan_enable_show(struct device *dev,
  2032. struct device_attribute *attr,
  2033. char *buf)
  2034. {
  2035. int status;
  2036. status = wan_get_radiosw();
  2037. if (status < 0)
  2038. return status;
  2039. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  2040. }
  2041. static ssize_t wan_enable_store(struct device *dev,
  2042. struct device_attribute *attr,
  2043. const char *buf, size_t count)
  2044. {
  2045. unsigned long t;
  2046. int res;
  2047. if (parse_strtoul(buf, 1, &t))
  2048. return -EINVAL;
  2049. res = wan_set_radiosw(t);
  2050. return (res) ? res : count;
  2051. }
  2052. static struct device_attribute dev_attr_wan_enable =
  2053. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  2054. wan_enable_show, wan_enable_store);
  2055. /* --------------------------------------------------------------------- */
  2056. static struct attribute *wan_attributes[] = {
  2057. &dev_attr_wan_enable.attr,
  2058. NULL
  2059. };
  2060. static const struct attribute_group wan_attr_group = {
  2061. .attrs = wan_attributes,
  2062. };
  2063. static int __init wan_init(struct ibm_init_struct *iibm)
  2064. {
  2065. int res;
  2066. int status = 0;
  2067. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  2068. IBM_ACPIHANDLE_INIT(hkey);
  2069. tp_features.wan = hkey_handle &&
  2070. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  2071. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  2072. str_supported(tp_features.wan),
  2073. status);
  2074. if (tp_features.wan) {
  2075. if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
  2076. /* no wan hardware present in system */
  2077. tp_features.wan = 0;
  2078. dbg_printk(TPACPI_DBG_INIT,
  2079. "wan hardware not installed\n");
  2080. } else {
  2081. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2082. &wan_attr_group);
  2083. if (res)
  2084. return res;
  2085. }
  2086. }
  2087. return (tp_features.wan)? 0 : 1;
  2088. }
  2089. static void wan_exit(void)
  2090. {
  2091. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2092. &wan_attr_group);
  2093. }
  2094. static int wan_get_radiosw(void)
  2095. {
  2096. int status;
  2097. if (!tp_features.wan)
  2098. return -ENODEV;
  2099. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2100. return -EIO;
  2101. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  2102. }
  2103. static int wan_set_radiosw(int radio_on)
  2104. {
  2105. int status;
  2106. if (!tp_features.wan)
  2107. return -ENODEV;
  2108. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2109. return -EIO;
  2110. if (radio_on)
  2111. status |= TP_ACPI_WANCARD_RADIOSSW;
  2112. else
  2113. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  2114. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  2115. return -EIO;
  2116. return 0;
  2117. }
  2118. /* procfs -------------------------------------------------------------- */
  2119. static int wan_read(char *p)
  2120. {
  2121. int len = 0;
  2122. int status = wan_get_radiosw();
  2123. if (!tp_features.wan)
  2124. len += sprintf(p + len, "status:\t\tnot supported\n");
  2125. else {
  2126. len += sprintf(p + len, "status:\t\t%s\n",
  2127. (status)? "enabled" : "disabled");
  2128. len += sprintf(p + len, "commands:\tenable, disable\n");
  2129. }
  2130. return len;
  2131. }
  2132. static int wan_write(char *buf)
  2133. {
  2134. char *cmd;
  2135. if (!tp_features.wan)
  2136. return -ENODEV;
  2137. while ((cmd = next_cmd(&buf))) {
  2138. if (strlencmp(cmd, "enable") == 0) {
  2139. wan_set_radiosw(1);
  2140. } else if (strlencmp(cmd, "disable") == 0) {
  2141. wan_set_radiosw(0);
  2142. } else
  2143. return -EINVAL;
  2144. }
  2145. return 0;
  2146. }
  2147. static struct ibm_struct wan_driver_data = {
  2148. .name = "wan",
  2149. .read = wan_read,
  2150. .write = wan_write,
  2151. .exit = wan_exit,
  2152. .flags.experimental = 1,
  2153. };
  2154. /*************************************************************************
  2155. * Video subdriver
  2156. */
  2157. enum video_access_mode {
  2158. TPACPI_VIDEO_NONE = 0,
  2159. TPACPI_VIDEO_570, /* 570 */
  2160. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  2161. TPACPI_VIDEO_NEW, /* all others */
  2162. };
  2163. enum { /* video status flags, based on VIDEO_570 */
  2164. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  2165. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  2166. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  2167. };
  2168. enum { /* TPACPI_VIDEO_570 constants */
  2169. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  2170. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  2171. * video_status_flags */
  2172. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  2173. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  2174. };
  2175. static enum video_access_mode video_supported;
  2176. static int video_orig_autosw;
  2177. static int video_autosw_get(void);
  2178. static int video_autosw_set(int enable);
  2179. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  2180. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  2181. "\\_SB.PCI0.VID0", /* 770e */
  2182. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  2183. "\\_SB.PCI0.AGP.VID", /* all others */
  2184. ); /* R30, R31 */
  2185. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  2186. static int __init video_init(struct ibm_init_struct *iibm)
  2187. {
  2188. int ivga;
  2189. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  2190. IBM_ACPIHANDLE_INIT(vid);
  2191. IBM_ACPIHANDLE_INIT(vid2);
  2192. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  2193. /* G41, assume IVGA doesn't change */
  2194. vid_handle = vid2_handle;
  2195. if (!vid_handle)
  2196. /* video switching not supported on R30, R31 */
  2197. video_supported = TPACPI_VIDEO_NONE;
  2198. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  2199. /* 570 */
  2200. video_supported = TPACPI_VIDEO_570;
  2201. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  2202. /* 600e/x, 770e, 770x */
  2203. video_supported = TPACPI_VIDEO_770;
  2204. else
  2205. /* all others */
  2206. video_supported = TPACPI_VIDEO_NEW;
  2207. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  2208. str_supported(video_supported != TPACPI_VIDEO_NONE),
  2209. video_supported);
  2210. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  2211. }
  2212. static void video_exit(void)
  2213. {
  2214. dbg_printk(TPACPI_DBG_EXIT,
  2215. "restoring original video autoswitch mode\n");
  2216. if (video_autosw_set(video_orig_autosw))
  2217. printk(IBM_ERR "error while trying to restore original "
  2218. "video autoswitch mode\n");
  2219. }
  2220. static int video_outputsw_get(void)
  2221. {
  2222. int status = 0;
  2223. int i;
  2224. switch (video_supported) {
  2225. case TPACPI_VIDEO_570:
  2226. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  2227. TP_ACPI_VIDEO_570_PHSCMD))
  2228. return -EIO;
  2229. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  2230. break;
  2231. case TPACPI_VIDEO_770:
  2232. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2233. return -EIO;
  2234. if (i)
  2235. status |= TP_ACPI_VIDEO_S_LCD;
  2236. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2237. return -EIO;
  2238. if (i)
  2239. status |= TP_ACPI_VIDEO_S_CRT;
  2240. break;
  2241. case TPACPI_VIDEO_NEW:
  2242. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  2243. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2244. return -EIO;
  2245. if (i)
  2246. status |= TP_ACPI_VIDEO_S_CRT;
  2247. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  2248. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2249. return -EIO;
  2250. if (i)
  2251. status |= TP_ACPI_VIDEO_S_LCD;
  2252. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  2253. return -EIO;
  2254. if (i)
  2255. status |= TP_ACPI_VIDEO_S_DVI;
  2256. break;
  2257. default:
  2258. return -ENOSYS;
  2259. }
  2260. return status;
  2261. }
  2262. static int video_outputsw_set(int status)
  2263. {
  2264. int autosw;
  2265. int res = 0;
  2266. switch (video_supported) {
  2267. case TPACPI_VIDEO_570:
  2268. res = acpi_evalf(NULL, NULL,
  2269. "\\_SB.PHS2", "vdd",
  2270. TP_ACPI_VIDEO_570_PHS2CMD,
  2271. status | TP_ACPI_VIDEO_570_PHS2SET);
  2272. break;
  2273. case TPACPI_VIDEO_770:
  2274. autosw = video_autosw_get();
  2275. if (autosw < 0)
  2276. return autosw;
  2277. res = video_autosw_set(1);
  2278. if (res)
  2279. return res;
  2280. res = acpi_evalf(vid_handle, NULL,
  2281. "ASWT", "vdd", status * 0x100, 0);
  2282. if (!autosw && video_autosw_set(autosw)) {
  2283. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  2284. return -EIO;
  2285. }
  2286. break;
  2287. case TPACPI_VIDEO_NEW:
  2288. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  2289. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  2290. break;
  2291. default:
  2292. return -ENOSYS;
  2293. }
  2294. return (res)? 0 : -EIO;
  2295. }
  2296. static int video_autosw_get(void)
  2297. {
  2298. int autosw = 0;
  2299. switch (video_supported) {
  2300. case TPACPI_VIDEO_570:
  2301. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  2302. return -EIO;
  2303. break;
  2304. case TPACPI_VIDEO_770:
  2305. case TPACPI_VIDEO_NEW:
  2306. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  2307. return -EIO;
  2308. break;
  2309. default:
  2310. return -ENOSYS;
  2311. }
  2312. return autosw & 1;
  2313. }
  2314. static int video_autosw_set(int enable)
  2315. {
  2316. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  2317. return -EIO;
  2318. return 0;
  2319. }
  2320. static int video_outputsw_cycle(void)
  2321. {
  2322. int autosw = video_autosw_get();
  2323. int res;
  2324. if (autosw < 0)
  2325. return autosw;
  2326. switch (video_supported) {
  2327. case TPACPI_VIDEO_570:
  2328. res = video_autosw_set(1);
  2329. if (res)
  2330. return res;
  2331. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  2332. break;
  2333. case TPACPI_VIDEO_770:
  2334. case TPACPI_VIDEO_NEW:
  2335. res = video_autosw_set(1);
  2336. if (res)
  2337. return res;
  2338. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  2339. break;
  2340. default:
  2341. return -ENOSYS;
  2342. }
  2343. if (!autosw && video_autosw_set(autosw)) {
  2344. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  2345. return -EIO;
  2346. }
  2347. return (res)? 0 : -EIO;
  2348. }
  2349. static int video_expand_toggle(void)
  2350. {
  2351. switch (video_supported) {
  2352. case TPACPI_VIDEO_570:
  2353. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  2354. 0 : -EIO;
  2355. case TPACPI_VIDEO_770:
  2356. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  2357. 0 : -EIO;
  2358. case TPACPI_VIDEO_NEW:
  2359. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  2360. 0 : -EIO;
  2361. default:
  2362. return -ENOSYS;
  2363. }
  2364. /* not reached */
  2365. }
  2366. static int video_read(char *p)
  2367. {
  2368. int status, autosw;
  2369. int len = 0;
  2370. if (video_supported == TPACPI_VIDEO_NONE) {
  2371. len += sprintf(p + len, "status:\t\tnot supported\n");
  2372. return len;
  2373. }
  2374. status = video_outputsw_get();
  2375. if (status < 0)
  2376. return status;
  2377. autosw = video_autosw_get();
  2378. if (autosw < 0)
  2379. return autosw;
  2380. len += sprintf(p + len, "status:\t\tsupported\n");
  2381. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  2382. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  2383. if (video_supported == TPACPI_VIDEO_NEW)
  2384. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  2385. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  2386. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  2387. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  2388. if (video_supported == TPACPI_VIDEO_NEW)
  2389. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  2390. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  2391. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  2392. return len;
  2393. }
  2394. static int video_write(char *buf)
  2395. {
  2396. char *cmd;
  2397. int enable, disable, status;
  2398. int res;
  2399. if (video_supported == TPACPI_VIDEO_NONE)
  2400. return -ENODEV;
  2401. enable = 0;
  2402. disable = 0;
  2403. while ((cmd = next_cmd(&buf))) {
  2404. if (strlencmp(cmd, "lcd_enable") == 0) {
  2405. enable |= TP_ACPI_VIDEO_S_LCD;
  2406. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  2407. disable |= TP_ACPI_VIDEO_S_LCD;
  2408. } else if (strlencmp(cmd, "crt_enable") == 0) {
  2409. enable |= TP_ACPI_VIDEO_S_CRT;
  2410. } else if (strlencmp(cmd, "crt_disable") == 0) {
  2411. disable |= TP_ACPI_VIDEO_S_CRT;
  2412. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2413. strlencmp(cmd, "dvi_enable") == 0) {
  2414. enable |= TP_ACPI_VIDEO_S_DVI;
  2415. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2416. strlencmp(cmd, "dvi_disable") == 0) {
  2417. disable |= TP_ACPI_VIDEO_S_DVI;
  2418. } else if (strlencmp(cmd, "auto_enable") == 0) {
  2419. res = video_autosw_set(1);
  2420. if (res)
  2421. return res;
  2422. } else if (strlencmp(cmd, "auto_disable") == 0) {
  2423. res = video_autosw_set(0);
  2424. if (res)
  2425. return res;
  2426. } else if (strlencmp(cmd, "video_switch") == 0) {
  2427. res = video_outputsw_cycle();
  2428. if (res)
  2429. return res;
  2430. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  2431. res = video_expand_toggle();
  2432. if (res)
  2433. return res;
  2434. } else
  2435. return -EINVAL;
  2436. }
  2437. if (enable || disable) {
  2438. status = video_outputsw_get();
  2439. if (status < 0)
  2440. return status;
  2441. res = video_outputsw_set((status & ~disable) | enable);
  2442. if (res)
  2443. return res;
  2444. }
  2445. return 0;
  2446. }
  2447. static struct ibm_struct video_driver_data = {
  2448. .name = "video",
  2449. .read = video_read,
  2450. .write = video_write,
  2451. .exit = video_exit,
  2452. };
  2453. /*************************************************************************
  2454. * Light (thinklight) subdriver
  2455. */
  2456. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  2457. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  2458. static int __init light_init(struct ibm_init_struct *iibm)
  2459. {
  2460. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  2461. IBM_ACPIHANDLE_INIT(ledb);
  2462. IBM_ACPIHANDLE_INIT(lght);
  2463. IBM_ACPIHANDLE_INIT(cmos);
  2464. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  2465. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  2466. if (tp_features.light)
  2467. /* light status not supported on
  2468. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  2469. tp_features.light_status =
  2470. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  2471. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  2472. str_supported(tp_features.light));
  2473. return (tp_features.light)? 0 : 1;
  2474. }
  2475. static int light_read(char *p)
  2476. {
  2477. int len = 0;
  2478. int status = 0;
  2479. if (!tp_features.light) {
  2480. len += sprintf(p + len, "status:\t\tnot supported\n");
  2481. } else if (!tp_features.light_status) {
  2482. len += sprintf(p + len, "status:\t\tunknown\n");
  2483. len += sprintf(p + len, "commands:\ton, off\n");
  2484. } else {
  2485. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  2486. return -EIO;
  2487. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  2488. len += sprintf(p + len, "commands:\ton, off\n");
  2489. }
  2490. return len;
  2491. }
  2492. static int light_write(char *buf)
  2493. {
  2494. int cmos_cmd, lght_cmd;
  2495. char *cmd;
  2496. int success;
  2497. if (!tp_features.light)
  2498. return -ENODEV;
  2499. while ((cmd = next_cmd(&buf))) {
  2500. if (strlencmp(cmd, "on") == 0) {
  2501. cmos_cmd = 0x0c;
  2502. lght_cmd = 1;
  2503. } else if (strlencmp(cmd, "off") == 0) {
  2504. cmos_cmd = 0x0d;
  2505. lght_cmd = 0;
  2506. } else
  2507. return -EINVAL;
  2508. success = cmos_handle ?
  2509. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  2510. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  2511. if (!success)
  2512. return -EIO;
  2513. }
  2514. return 0;
  2515. }
  2516. static struct ibm_struct light_driver_data = {
  2517. .name = "light",
  2518. .read = light_read,
  2519. .write = light_write,
  2520. };
  2521. /*************************************************************************
  2522. * Dock subdriver
  2523. */
  2524. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2525. static void dock_notify(struct ibm_struct *ibm, u32 event);
  2526. static int dock_read(char *p);
  2527. static int dock_write(char *buf);
  2528. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  2529. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  2530. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  2531. "\\_SB.PCI.ISA.SLCE", /* 570 */
  2532. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  2533. /* don't list other alternatives as we install a notify handler on the 570 */
  2534. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  2535. static const struct acpi_device_id ibm_pci_device_ids[] = {
  2536. {PCI_ROOT_HID_STRING, 0},
  2537. {"", 0},
  2538. };
  2539. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  2540. {
  2541. .notify = dock_notify,
  2542. .handle = &dock_handle,
  2543. .type = ACPI_SYSTEM_NOTIFY,
  2544. },
  2545. {
  2546. /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
  2547. * We just use it to get notifications of dock hotplug
  2548. * in very old thinkpads */
  2549. .hid = ibm_pci_device_ids,
  2550. .notify = dock_notify,
  2551. .handle = &pci_handle,
  2552. .type = ACPI_SYSTEM_NOTIFY,
  2553. },
  2554. };
  2555. static struct ibm_struct dock_driver_data[2] = {
  2556. {
  2557. .name = "dock",
  2558. .read = dock_read,
  2559. .write = dock_write,
  2560. .acpi = &ibm_dock_acpidriver[0],
  2561. },
  2562. {
  2563. .name = "dock",
  2564. .acpi = &ibm_dock_acpidriver[1],
  2565. },
  2566. };
  2567. #define dock_docked() (_sta(dock_handle) & 1)
  2568. static int __init dock_init(struct ibm_init_struct *iibm)
  2569. {
  2570. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  2571. IBM_ACPIHANDLE_INIT(dock);
  2572. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  2573. str_supported(dock_handle != NULL));
  2574. return (dock_handle)? 0 : 1;
  2575. }
  2576. static int __init dock_init2(struct ibm_init_struct *iibm)
  2577. {
  2578. int dock2_needed;
  2579. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  2580. if (dock_driver_data[0].flags.acpi_driver_registered &&
  2581. dock_driver_data[0].flags.acpi_notify_installed) {
  2582. IBM_ACPIHANDLE_INIT(pci);
  2583. dock2_needed = (pci_handle != NULL);
  2584. vdbg_printk(TPACPI_DBG_INIT,
  2585. "dock PCI handler for the TP 570 is %s\n",
  2586. str_supported(dock2_needed));
  2587. } else {
  2588. vdbg_printk(TPACPI_DBG_INIT,
  2589. "dock subdriver part 2 not required\n");
  2590. dock2_needed = 0;
  2591. }
  2592. return (dock2_needed)? 0 : 1;
  2593. }
  2594. static void dock_notify(struct ibm_struct *ibm, u32 event)
  2595. {
  2596. int docked = dock_docked();
  2597. int pci = ibm->acpi->hid && ibm->acpi->device &&
  2598. acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
  2599. int data;
  2600. if (event == 1 && !pci) /* 570 */
  2601. data = 1; /* button */
  2602. else if (event == 1 && pci) /* 570 */
  2603. data = 3; /* dock */
  2604. else if (event == 3 && docked)
  2605. data = 1; /* button */
  2606. else if (event == 3 && !docked)
  2607. data = 2; /* undock */
  2608. else if (event == 0 && docked)
  2609. data = 3; /* dock */
  2610. else {
  2611. printk(IBM_ERR "unknown dock event %d, status %d\n",
  2612. event, _sta(dock_handle));
  2613. data = 0; /* unknown */
  2614. }
  2615. acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
  2616. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2617. ibm->acpi->device->dev.bus_id,
  2618. event, data);
  2619. }
  2620. static int dock_read(char *p)
  2621. {
  2622. int len = 0;
  2623. int docked = dock_docked();
  2624. if (!dock_handle)
  2625. len += sprintf(p + len, "status:\t\tnot supported\n");
  2626. else if (!docked)
  2627. len += sprintf(p + len, "status:\t\tundocked\n");
  2628. else {
  2629. len += sprintf(p + len, "status:\t\tdocked\n");
  2630. len += sprintf(p + len, "commands:\tdock, undock\n");
  2631. }
  2632. return len;
  2633. }
  2634. static int dock_write(char *buf)
  2635. {
  2636. char *cmd;
  2637. if (!dock_docked())
  2638. return -ENODEV;
  2639. while ((cmd = next_cmd(&buf))) {
  2640. if (strlencmp(cmd, "undock") == 0) {
  2641. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  2642. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  2643. return -EIO;
  2644. } else if (strlencmp(cmd, "dock") == 0) {
  2645. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  2646. return -EIO;
  2647. } else
  2648. return -EINVAL;
  2649. }
  2650. return 0;
  2651. }
  2652. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  2653. /*************************************************************************
  2654. * Bay subdriver
  2655. */
  2656. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2657. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  2658. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  2659. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  2660. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  2661. ); /* A21e, R30, R31 */
  2662. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  2663. "_EJ0", /* all others */
  2664. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  2665. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  2666. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  2667. ); /* all others */
  2668. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  2669. "_EJ0", /* 770x */
  2670. ); /* all others */
  2671. static int __init bay_init(struct ibm_init_struct *iibm)
  2672. {
  2673. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  2674. IBM_ACPIHANDLE_INIT(bay);
  2675. if (bay_handle)
  2676. IBM_ACPIHANDLE_INIT(bay_ej);
  2677. IBM_ACPIHANDLE_INIT(bay2);
  2678. if (bay2_handle)
  2679. IBM_ACPIHANDLE_INIT(bay2_ej);
  2680. tp_features.bay_status = bay_handle &&
  2681. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  2682. tp_features.bay_status2 = bay2_handle &&
  2683. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  2684. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  2685. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  2686. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  2687. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  2688. vdbg_printk(TPACPI_DBG_INIT,
  2689. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  2690. str_supported(tp_features.bay_status),
  2691. str_supported(tp_features.bay_eject),
  2692. str_supported(tp_features.bay_status2),
  2693. str_supported(tp_features.bay_eject2));
  2694. return (tp_features.bay_status || tp_features.bay_eject ||
  2695. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  2696. }
  2697. static void bay_notify(struct ibm_struct *ibm, u32 event)
  2698. {
  2699. acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
  2700. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2701. ibm->acpi->device->dev.bus_id,
  2702. event, 0);
  2703. }
  2704. #define bay_occupied(b) (_sta(b##_handle) & 1)
  2705. static int bay_read(char *p)
  2706. {
  2707. int len = 0;
  2708. int occupied = bay_occupied(bay);
  2709. int occupied2 = bay_occupied(bay2);
  2710. int eject, eject2;
  2711. len += sprintf(p + len, "status:\t\t%s\n",
  2712. tp_features.bay_status ?
  2713. (occupied ? "occupied" : "unoccupied") :
  2714. "not supported");
  2715. if (tp_features.bay_status2)
  2716. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  2717. "occupied" : "unoccupied");
  2718. eject = tp_features.bay_eject && occupied;
  2719. eject2 = tp_features.bay_eject2 && occupied2;
  2720. if (eject && eject2)
  2721. len += sprintf(p + len, "commands:\teject, eject2\n");
  2722. else if (eject)
  2723. len += sprintf(p + len, "commands:\teject\n");
  2724. else if (eject2)
  2725. len += sprintf(p + len, "commands:\teject2\n");
  2726. return len;
  2727. }
  2728. static int bay_write(char *buf)
  2729. {
  2730. char *cmd;
  2731. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  2732. return -ENODEV;
  2733. while ((cmd = next_cmd(&buf))) {
  2734. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  2735. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  2736. return -EIO;
  2737. } else if (tp_features.bay_eject2 &&
  2738. strlencmp(cmd, "eject2") == 0) {
  2739. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  2740. return -EIO;
  2741. } else
  2742. return -EINVAL;
  2743. }
  2744. return 0;
  2745. }
  2746. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  2747. .notify = bay_notify,
  2748. .handle = &bay_handle,
  2749. .type = ACPI_SYSTEM_NOTIFY,
  2750. };
  2751. static struct ibm_struct bay_driver_data = {
  2752. .name = "bay",
  2753. .read = bay_read,
  2754. .write = bay_write,
  2755. .acpi = &ibm_bay_acpidriver,
  2756. };
  2757. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  2758. /*************************************************************************
  2759. * CMOS subdriver
  2760. */
  2761. /* sysfs cmos_command -------------------------------------------------- */
  2762. static ssize_t cmos_command_store(struct device *dev,
  2763. struct device_attribute *attr,
  2764. const char *buf, size_t count)
  2765. {
  2766. unsigned long cmos_cmd;
  2767. int res;
  2768. if (parse_strtoul(buf, 21, &cmos_cmd))
  2769. return -EINVAL;
  2770. res = issue_thinkpad_cmos_command(cmos_cmd);
  2771. return (res)? res : count;
  2772. }
  2773. static struct device_attribute dev_attr_cmos_command =
  2774. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  2775. /* --------------------------------------------------------------------- */
  2776. static int __init cmos_init(struct ibm_init_struct *iibm)
  2777. {
  2778. int res;
  2779. vdbg_printk(TPACPI_DBG_INIT,
  2780. "initializing cmos commands subdriver\n");
  2781. IBM_ACPIHANDLE_INIT(cmos);
  2782. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  2783. str_supported(cmos_handle != NULL));
  2784. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2785. if (res)
  2786. return res;
  2787. return (cmos_handle)? 0 : 1;
  2788. }
  2789. static void cmos_exit(void)
  2790. {
  2791. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2792. }
  2793. static int cmos_read(char *p)
  2794. {
  2795. int len = 0;
  2796. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2797. R30, R31, T20-22, X20-21 */
  2798. if (!cmos_handle)
  2799. len += sprintf(p + len, "status:\t\tnot supported\n");
  2800. else {
  2801. len += sprintf(p + len, "status:\t\tsupported\n");
  2802. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  2803. }
  2804. return len;
  2805. }
  2806. static int cmos_write(char *buf)
  2807. {
  2808. char *cmd;
  2809. int cmos_cmd, res;
  2810. while ((cmd = next_cmd(&buf))) {
  2811. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  2812. cmos_cmd >= 0 && cmos_cmd <= 21) {
  2813. /* cmos_cmd set */
  2814. } else
  2815. return -EINVAL;
  2816. res = issue_thinkpad_cmos_command(cmos_cmd);
  2817. if (res)
  2818. return res;
  2819. }
  2820. return 0;
  2821. }
  2822. static struct ibm_struct cmos_driver_data = {
  2823. .name = "cmos",
  2824. .read = cmos_read,
  2825. .write = cmos_write,
  2826. .exit = cmos_exit,
  2827. };
  2828. /*************************************************************************
  2829. * LED subdriver
  2830. */
  2831. enum led_access_mode {
  2832. TPACPI_LED_NONE = 0,
  2833. TPACPI_LED_570, /* 570 */
  2834. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2835. TPACPI_LED_NEW, /* all others */
  2836. };
  2837. enum { /* For TPACPI_LED_OLD */
  2838. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  2839. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  2840. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  2841. };
  2842. static enum led_access_mode led_supported;
  2843. IBM_HANDLE(led, ec, "SLED", /* 570 */
  2844. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2845. "LED", /* all others */
  2846. ); /* R30, R31 */
  2847. static int __init led_init(struct ibm_init_struct *iibm)
  2848. {
  2849. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  2850. IBM_ACPIHANDLE_INIT(led);
  2851. if (!led_handle)
  2852. /* led not supported on R30, R31 */
  2853. led_supported = TPACPI_LED_NONE;
  2854. else if (strlencmp(led_path, "SLED") == 0)
  2855. /* 570 */
  2856. led_supported = TPACPI_LED_570;
  2857. else if (strlencmp(led_path, "SYSL") == 0)
  2858. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2859. led_supported = TPACPI_LED_OLD;
  2860. else
  2861. /* all others */
  2862. led_supported = TPACPI_LED_NEW;
  2863. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  2864. str_supported(led_supported), led_supported);
  2865. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  2866. }
  2867. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  2868. static int led_read(char *p)
  2869. {
  2870. int len = 0;
  2871. if (!led_supported) {
  2872. len += sprintf(p + len, "status:\t\tnot supported\n");
  2873. return len;
  2874. }
  2875. len += sprintf(p + len, "status:\t\tsupported\n");
  2876. if (led_supported == TPACPI_LED_570) {
  2877. /* 570 */
  2878. int i, status;
  2879. for (i = 0; i < 8; i++) {
  2880. if (!acpi_evalf(ec_handle,
  2881. &status, "GLED", "dd", 1 << i))
  2882. return -EIO;
  2883. len += sprintf(p + len, "%d:\t\t%s\n",
  2884. i, led_status(status));
  2885. }
  2886. }
  2887. len += sprintf(p + len, "commands:\t"
  2888. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  2889. return len;
  2890. }
  2891. /* off, on, blink */
  2892. static const int led_sled_arg1[] = { 0, 1, 3 };
  2893. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  2894. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  2895. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  2896. static int led_write(char *buf)
  2897. {
  2898. char *cmd;
  2899. int led, ind, ret;
  2900. if (!led_supported)
  2901. return -ENODEV;
  2902. while ((cmd = next_cmd(&buf))) {
  2903. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  2904. return -EINVAL;
  2905. if (strstr(cmd, "off")) {
  2906. ind = 0;
  2907. } else if (strstr(cmd, "on")) {
  2908. ind = 1;
  2909. } else if (strstr(cmd, "blink")) {
  2910. ind = 2;
  2911. } else
  2912. return -EINVAL;
  2913. if (led_supported == TPACPI_LED_570) {
  2914. /* 570 */
  2915. led = 1 << led;
  2916. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2917. led, led_sled_arg1[ind]))
  2918. return -EIO;
  2919. } else if (led_supported == TPACPI_LED_OLD) {
  2920. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  2921. led = 1 << led;
  2922. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  2923. if (ret >= 0)
  2924. ret =
  2925. ec_write(TPACPI_LED_EC_HLBL,
  2926. led * led_exp_hlbl[ind]);
  2927. if (ret >= 0)
  2928. ret =
  2929. ec_write(TPACPI_LED_EC_HLCL,
  2930. led * led_exp_hlcl[ind]);
  2931. if (ret < 0)
  2932. return ret;
  2933. } else {
  2934. /* all others */
  2935. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2936. led, led_led_arg1[ind]))
  2937. return -EIO;
  2938. }
  2939. }
  2940. return 0;
  2941. }
  2942. static struct ibm_struct led_driver_data = {
  2943. .name = "led",
  2944. .read = led_read,
  2945. .write = led_write,
  2946. };
  2947. /*************************************************************************
  2948. * Beep subdriver
  2949. */
  2950. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  2951. static int __init beep_init(struct ibm_init_struct *iibm)
  2952. {
  2953. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  2954. IBM_ACPIHANDLE_INIT(beep);
  2955. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  2956. str_supported(beep_handle != NULL));
  2957. return (beep_handle)? 0 : 1;
  2958. }
  2959. static int beep_read(char *p)
  2960. {
  2961. int len = 0;
  2962. if (!beep_handle)
  2963. len += sprintf(p + len, "status:\t\tnot supported\n");
  2964. else {
  2965. len += sprintf(p + len, "status:\t\tsupported\n");
  2966. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  2967. }
  2968. return len;
  2969. }
  2970. static int beep_write(char *buf)
  2971. {
  2972. char *cmd;
  2973. int beep_cmd;
  2974. if (!beep_handle)
  2975. return -ENODEV;
  2976. while ((cmd = next_cmd(&buf))) {
  2977. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  2978. beep_cmd >= 0 && beep_cmd <= 17) {
  2979. /* beep_cmd set */
  2980. } else
  2981. return -EINVAL;
  2982. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  2983. return -EIO;
  2984. }
  2985. return 0;
  2986. }
  2987. static struct ibm_struct beep_driver_data = {
  2988. .name = "beep",
  2989. .read = beep_read,
  2990. .write = beep_write,
  2991. };
  2992. /*************************************************************************
  2993. * Thermal subdriver
  2994. */
  2995. enum thermal_access_mode {
  2996. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  2997. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  2998. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  2999. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  3000. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  3001. };
  3002. enum { /* TPACPI_THERMAL_TPEC_* */
  3003. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  3004. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  3005. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  3006. };
  3007. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  3008. struct ibm_thermal_sensors_struct {
  3009. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  3010. };
  3011. static enum thermal_access_mode thermal_read_mode;
  3012. static int thermal_get_sensor(int idx, s32 *value);
  3013. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s);
  3014. /* sysfs temp##_input -------------------------------------------------- */
  3015. static ssize_t thermal_temp_input_show(struct device *dev,
  3016. struct device_attribute *attr,
  3017. char *buf)
  3018. {
  3019. struct sensor_device_attribute *sensor_attr =
  3020. to_sensor_dev_attr(attr);
  3021. int idx = sensor_attr->index;
  3022. s32 value;
  3023. int res;
  3024. res = thermal_get_sensor(idx, &value);
  3025. if (res)
  3026. return res;
  3027. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  3028. return -ENXIO;
  3029. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  3030. }
  3031. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  3032. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  3033. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  3034. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  3035. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  3036. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  3037. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  3038. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  3039. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  3040. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  3041. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  3042. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  3043. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  3044. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  3045. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  3046. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  3047. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  3048. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  3049. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  3050. };
  3051. #define THERMAL_ATTRS(X) \
  3052. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  3053. static struct attribute *thermal_temp_input_attr[] = {
  3054. THERMAL_ATTRS(8),
  3055. THERMAL_ATTRS(9),
  3056. THERMAL_ATTRS(10),
  3057. THERMAL_ATTRS(11),
  3058. THERMAL_ATTRS(12),
  3059. THERMAL_ATTRS(13),
  3060. THERMAL_ATTRS(14),
  3061. THERMAL_ATTRS(15),
  3062. THERMAL_ATTRS(0),
  3063. THERMAL_ATTRS(1),
  3064. THERMAL_ATTRS(2),
  3065. THERMAL_ATTRS(3),
  3066. THERMAL_ATTRS(4),
  3067. THERMAL_ATTRS(5),
  3068. THERMAL_ATTRS(6),
  3069. THERMAL_ATTRS(7),
  3070. NULL
  3071. };
  3072. static const struct attribute_group thermal_temp_input16_group = {
  3073. .attrs = thermal_temp_input_attr
  3074. };
  3075. static const struct attribute_group thermal_temp_input8_group = {
  3076. .attrs = &thermal_temp_input_attr[8]
  3077. };
  3078. #undef THERMAL_SENSOR_ATTR_TEMP
  3079. #undef THERMAL_ATTRS
  3080. /* --------------------------------------------------------------------- */
  3081. static int __init thermal_init(struct ibm_init_struct *iibm)
  3082. {
  3083. u8 t, ta1, ta2;
  3084. int i;
  3085. int acpi_tmp7;
  3086. int res;
  3087. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  3088. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  3089. if (thinkpad_id.ec_model) {
  3090. /*
  3091. * Direct EC access mode: sensors at registers
  3092. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  3093. * non-implemented, thermal sensors return 0x80 when
  3094. * not available
  3095. */
  3096. ta1 = ta2 = 0;
  3097. for (i = 0; i < 8; i++) {
  3098. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  3099. ta1 |= t;
  3100. } else {
  3101. ta1 = 0;
  3102. break;
  3103. }
  3104. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  3105. ta2 |= t;
  3106. } else {
  3107. ta1 = 0;
  3108. break;
  3109. }
  3110. }
  3111. if (ta1 == 0) {
  3112. /* This is sheer paranoia, but we handle it anyway */
  3113. if (acpi_tmp7) {
  3114. printk(IBM_ERR
  3115. "ThinkPad ACPI EC access misbehaving, "
  3116. "falling back to ACPI TMPx access mode\n");
  3117. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3118. } else {
  3119. printk(IBM_ERR
  3120. "ThinkPad ACPI EC access misbehaving, "
  3121. "disabling thermal sensors access\n");
  3122. thermal_read_mode = TPACPI_THERMAL_NONE;
  3123. }
  3124. } else {
  3125. thermal_read_mode =
  3126. (ta2 != 0) ?
  3127. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  3128. }
  3129. } else if (acpi_tmp7) {
  3130. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  3131. /* 600e/x, 770e, 770x */
  3132. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  3133. } else {
  3134. /* Standard ACPI TMPx access, max 8 sensors */
  3135. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3136. }
  3137. } else {
  3138. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  3139. thermal_read_mode = TPACPI_THERMAL_NONE;
  3140. }
  3141. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  3142. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  3143. thermal_read_mode);
  3144. switch(thermal_read_mode) {
  3145. case TPACPI_THERMAL_TPEC_16:
  3146. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3147. &thermal_temp_input16_group);
  3148. if (res)
  3149. return res;
  3150. break;
  3151. case TPACPI_THERMAL_TPEC_8:
  3152. case TPACPI_THERMAL_ACPI_TMP07:
  3153. case TPACPI_THERMAL_ACPI_UPDT:
  3154. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3155. &thermal_temp_input8_group);
  3156. if (res)
  3157. return res;
  3158. break;
  3159. case TPACPI_THERMAL_NONE:
  3160. default:
  3161. return 1;
  3162. }
  3163. return 0;
  3164. }
  3165. static void thermal_exit(void)
  3166. {
  3167. switch(thermal_read_mode) {
  3168. case TPACPI_THERMAL_TPEC_16:
  3169. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3170. &thermal_temp_input16_group);
  3171. break;
  3172. case TPACPI_THERMAL_TPEC_8:
  3173. case TPACPI_THERMAL_ACPI_TMP07:
  3174. case TPACPI_THERMAL_ACPI_UPDT:
  3175. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3176. &thermal_temp_input16_group);
  3177. break;
  3178. case TPACPI_THERMAL_NONE:
  3179. default:
  3180. break;
  3181. }
  3182. }
  3183. /* idx is zero-based */
  3184. static int thermal_get_sensor(int idx, s32 *value)
  3185. {
  3186. int t;
  3187. s8 tmp;
  3188. char tmpi[5];
  3189. t = TP_EC_THERMAL_TMP0;
  3190. switch (thermal_read_mode) {
  3191. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  3192. case TPACPI_THERMAL_TPEC_16:
  3193. if (idx >= 8 && idx <= 15) {
  3194. t = TP_EC_THERMAL_TMP8;
  3195. idx -= 8;
  3196. }
  3197. /* fallthrough */
  3198. #endif
  3199. case TPACPI_THERMAL_TPEC_8:
  3200. if (idx <= 7) {
  3201. if (!acpi_ec_read(t + idx, &tmp))
  3202. return -EIO;
  3203. *value = tmp * 1000;
  3204. return 0;
  3205. }
  3206. break;
  3207. case TPACPI_THERMAL_ACPI_UPDT:
  3208. if (idx <= 7) {
  3209. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3210. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  3211. return -EIO;
  3212. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3213. return -EIO;
  3214. *value = (t - 2732) * 100;
  3215. return 0;
  3216. }
  3217. break;
  3218. case TPACPI_THERMAL_ACPI_TMP07:
  3219. if (idx <= 7) {
  3220. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3221. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3222. return -EIO;
  3223. if (t > 127 || t < -127)
  3224. t = TP_EC_THERMAL_TMP_NA;
  3225. *value = t * 1000;
  3226. return 0;
  3227. }
  3228. break;
  3229. case TPACPI_THERMAL_NONE:
  3230. default:
  3231. return -ENOSYS;
  3232. }
  3233. return -EINVAL;
  3234. }
  3235. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  3236. {
  3237. int res, i;
  3238. int n;
  3239. n = 8;
  3240. i = 0;
  3241. if (!s)
  3242. return -EINVAL;
  3243. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  3244. n = 16;
  3245. for(i = 0 ; i < n; i++) {
  3246. res = thermal_get_sensor(i, &s->temp[i]);
  3247. if (res)
  3248. return res;
  3249. }
  3250. return n;
  3251. }
  3252. static int thermal_read(char *p)
  3253. {
  3254. int len = 0;
  3255. int n, i;
  3256. struct ibm_thermal_sensors_struct t;
  3257. n = thermal_get_sensors(&t);
  3258. if (unlikely(n < 0))
  3259. return n;
  3260. len += sprintf(p + len, "temperatures:\t");
  3261. if (n > 0) {
  3262. for (i = 0; i < (n - 1); i++)
  3263. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  3264. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  3265. } else
  3266. len += sprintf(p + len, "not supported\n");
  3267. return len;
  3268. }
  3269. static struct ibm_struct thermal_driver_data = {
  3270. .name = "thermal",
  3271. .read = thermal_read,
  3272. .exit = thermal_exit,
  3273. };
  3274. /*************************************************************************
  3275. * EC Dump subdriver
  3276. */
  3277. static u8 ecdump_regs[256];
  3278. static int ecdump_read(char *p)
  3279. {
  3280. int len = 0;
  3281. int i, j;
  3282. u8 v;
  3283. len += sprintf(p + len, "EC "
  3284. " +00 +01 +02 +03 +04 +05 +06 +07"
  3285. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  3286. for (i = 0; i < 256; i += 16) {
  3287. len += sprintf(p + len, "EC 0x%02x:", i);
  3288. for (j = 0; j < 16; j++) {
  3289. if (!acpi_ec_read(i + j, &v))
  3290. break;
  3291. if (v != ecdump_regs[i + j])
  3292. len += sprintf(p + len, " *%02x", v);
  3293. else
  3294. len += sprintf(p + len, " %02x", v);
  3295. ecdump_regs[i + j] = v;
  3296. }
  3297. len += sprintf(p + len, "\n");
  3298. if (j != 16)
  3299. break;
  3300. }
  3301. /* These are way too dangerous to advertise openly... */
  3302. #if 0
  3303. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  3304. " (<offset> is 00-ff, <value> is 00-ff)\n");
  3305. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  3306. " (<offset> is 00-ff, <value> is 0-255)\n");
  3307. #endif
  3308. return len;
  3309. }
  3310. static int ecdump_write(char *buf)
  3311. {
  3312. char *cmd;
  3313. int i, v;
  3314. while ((cmd = next_cmd(&buf))) {
  3315. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  3316. /* i and v set */
  3317. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  3318. /* i and v set */
  3319. } else
  3320. return -EINVAL;
  3321. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  3322. if (!acpi_ec_write(i, v))
  3323. return -EIO;
  3324. } else
  3325. return -EINVAL;
  3326. }
  3327. return 0;
  3328. }
  3329. static struct ibm_struct ecdump_driver_data = {
  3330. .name = "ecdump",
  3331. .read = ecdump_read,
  3332. .write = ecdump_write,
  3333. .flags.experimental = 1,
  3334. };
  3335. /*************************************************************************
  3336. * Backlight/brightness subdriver
  3337. */
  3338. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  3339. static struct backlight_device *ibm_backlight_device;
  3340. static int brightness_offset = 0x31;
  3341. static int brightness_mode;
  3342. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  3343. static int brightness_get(struct backlight_device *bd);
  3344. static int brightness_set(int value);
  3345. static int brightness_update_status(struct backlight_device *bd);
  3346. static struct backlight_ops ibm_backlight_data = {
  3347. .get_brightness = brightness_get,
  3348. .update_status = brightness_update_status,
  3349. };
  3350. static struct mutex brightness_mutex;
  3351. static int __init tpacpi_query_bcll_levels(acpi_handle handle)
  3352. {
  3353. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  3354. union acpi_object *obj;
  3355. int rc;
  3356. if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
  3357. obj = (union acpi_object *)buffer.pointer;
  3358. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  3359. printk(IBM_ERR "Unknown BCLL data, "
  3360. "please report this to %s\n", IBM_MAIL);
  3361. rc = 0;
  3362. } else {
  3363. rc = obj->package.count;
  3364. }
  3365. } else {
  3366. return 0;
  3367. }
  3368. kfree(buffer.pointer);
  3369. return rc;
  3370. }
  3371. static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
  3372. void *context, void **rv)
  3373. {
  3374. char name[ACPI_PATH_SEGMENT_LENGTH];
  3375. struct acpi_buffer buffer = { sizeof(name), &name };
  3376. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  3377. !strncmp("BCLL", name, sizeof(name) - 1)) {
  3378. if (tpacpi_query_bcll_levels(handle) == 16) {
  3379. *rv = handle;
  3380. return AE_CTRL_TERMINATE;
  3381. } else {
  3382. return AE_OK;
  3383. }
  3384. } else {
  3385. return AE_OK;
  3386. }
  3387. }
  3388. static int __init brightness_check_levels(void)
  3389. {
  3390. int status;
  3391. void *found_node = NULL;
  3392. if (!vid_handle) {
  3393. IBM_ACPIHANDLE_INIT(vid);
  3394. }
  3395. if (!vid_handle)
  3396. return 0;
  3397. /* Search for a BCLL package with 16 levels */
  3398. status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
  3399. brightness_find_bcll, NULL, &found_node);
  3400. return (ACPI_SUCCESS(status) && found_node != NULL);
  3401. }
  3402. static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
  3403. void *context, void **rv)
  3404. {
  3405. char name[ACPI_PATH_SEGMENT_LENGTH];
  3406. struct acpi_buffer buffer = { sizeof(name), &name };
  3407. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  3408. !strncmp("_BCL", name, sizeof(name) - 1)) {
  3409. *rv = handle;
  3410. return AE_CTRL_TERMINATE;
  3411. } else {
  3412. return AE_OK;
  3413. }
  3414. }
  3415. static int __init brightness_check_std_acpi_support(void)
  3416. {
  3417. int status;
  3418. void *found_node = NULL;
  3419. if (!vid_handle) {
  3420. IBM_ACPIHANDLE_INIT(vid);
  3421. }
  3422. if (!vid_handle)
  3423. return 0;
  3424. /* Search for a _BCL method, but don't execute it */
  3425. status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
  3426. brightness_find_bcl, NULL, &found_node);
  3427. return (ACPI_SUCCESS(status) && found_node != NULL);
  3428. }
  3429. static int __init brightness_init(struct ibm_init_struct *iibm)
  3430. {
  3431. int b;
  3432. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  3433. mutex_init(&brightness_mutex);
  3434. if (!brightness_enable) {
  3435. dbg_printk(TPACPI_DBG_INIT,
  3436. "brightness support disabled by module parameter\n");
  3437. return 1;
  3438. } else if (brightness_enable > 1) {
  3439. if (brightness_check_std_acpi_support()) {
  3440. printk(IBM_NOTICE
  3441. "standard ACPI backlight interface available, not loading native one...\n");
  3442. return 1;
  3443. }
  3444. }
  3445. if (!brightness_mode) {
  3446. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
  3447. brightness_mode = 2;
  3448. else
  3449. brightness_mode = 3;
  3450. dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
  3451. brightness_mode);
  3452. }
  3453. if (brightness_mode > 3)
  3454. return -EINVAL;
  3455. tp_features.bright_16levels =
  3456. thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
  3457. brightness_check_levels();
  3458. b = brightness_get(NULL);
  3459. if (b < 0)
  3460. return 1;
  3461. if (tp_features.bright_16levels)
  3462. printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
  3463. ibm_backlight_device = backlight_device_register(
  3464. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  3465. &ibm_backlight_data);
  3466. if (IS_ERR(ibm_backlight_device)) {
  3467. printk(IBM_ERR "Could not register backlight device\n");
  3468. return PTR_ERR(ibm_backlight_device);
  3469. }
  3470. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  3471. ibm_backlight_device->props.max_brightness =
  3472. (tp_features.bright_16levels)? 15 : 7;
  3473. ibm_backlight_device->props.brightness = b;
  3474. backlight_update_status(ibm_backlight_device);
  3475. return 0;
  3476. }
  3477. static void brightness_exit(void)
  3478. {
  3479. if (ibm_backlight_device) {
  3480. vdbg_printk(TPACPI_DBG_EXIT,
  3481. "calling backlight_device_unregister()\n");
  3482. backlight_device_unregister(ibm_backlight_device);
  3483. ibm_backlight_device = NULL;
  3484. }
  3485. }
  3486. static int brightness_update_status(struct backlight_device *bd)
  3487. {
  3488. /* it is the backlight class's job (caller) to handle
  3489. * EINTR and other errors properly */
  3490. return brightness_set(
  3491. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  3492. bd->props.power == FB_BLANK_UNBLANK) ?
  3493. bd->props.brightness : 0);
  3494. }
  3495. /*
  3496. * ThinkPads can read brightness from two places: EC 0x31, or
  3497. * CMOS NVRAM byte 0x5E, bits 0-3.
  3498. */
  3499. static int brightness_get(struct backlight_device *bd)
  3500. {
  3501. u8 lec = 0, lcmos = 0, level = 0;
  3502. if (brightness_mode & 1) {
  3503. if (!acpi_ec_read(brightness_offset, &lec))
  3504. return -EIO;
  3505. lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3506. level = lec;
  3507. };
  3508. if (brightness_mode & 2) {
  3509. lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  3510. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  3511. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  3512. lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3513. level = lcmos;
  3514. }
  3515. if (brightness_mode == 3 && lec != lcmos) {
  3516. printk(IBM_ERR
  3517. "CMOS NVRAM (%u) and EC (%u) do not agree "
  3518. "on display brightness level\n",
  3519. (unsigned int) lcmos,
  3520. (unsigned int) lec);
  3521. return -EIO;
  3522. }
  3523. return level;
  3524. }
  3525. /* May return EINTR which can always be mapped to ERESTARTSYS */
  3526. static int brightness_set(int value)
  3527. {
  3528. int cmos_cmd, inc, i, res;
  3529. int current_value;
  3530. if (value > ((tp_features.bright_16levels)? 15 : 7))
  3531. return -EINVAL;
  3532. res = mutex_lock_interruptible(&brightness_mutex);
  3533. if (res < 0)
  3534. return res;
  3535. current_value = brightness_get(NULL);
  3536. if (current_value < 0) {
  3537. res = current_value;
  3538. goto errout;
  3539. }
  3540. cmos_cmd = value > current_value ?
  3541. TP_CMOS_BRIGHTNESS_UP :
  3542. TP_CMOS_BRIGHTNESS_DOWN;
  3543. inc = (value > current_value)? 1 : -1;
  3544. res = 0;
  3545. for (i = current_value; i != value; i += inc) {
  3546. if ((brightness_mode & 2) &&
  3547. issue_thinkpad_cmos_command(cmos_cmd)) {
  3548. res = -EIO;
  3549. goto errout;
  3550. }
  3551. if ((brightness_mode & 1) &&
  3552. !acpi_ec_write(brightness_offset, i + inc)) {
  3553. res = -EIO;
  3554. goto errout;;
  3555. }
  3556. }
  3557. errout:
  3558. mutex_unlock(&brightness_mutex);
  3559. return res;
  3560. }
  3561. static int brightness_read(char *p)
  3562. {
  3563. int len = 0;
  3564. int level;
  3565. if ((level = brightness_get(NULL)) < 0) {
  3566. len += sprintf(p + len, "level:\t\tunreadable\n");
  3567. } else {
  3568. len += sprintf(p + len, "level:\t\t%d\n", level);
  3569. len += sprintf(p + len, "commands:\tup, down\n");
  3570. len += sprintf(p + len, "commands:\tlevel <level>"
  3571. " (<level> is 0-%d)\n",
  3572. (tp_features.bright_16levels) ? 15 : 7);
  3573. }
  3574. return len;
  3575. }
  3576. static int brightness_write(char *buf)
  3577. {
  3578. int level;
  3579. int rc;
  3580. char *cmd;
  3581. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  3582. level = brightness_get(NULL);
  3583. if (level < 0)
  3584. return level;
  3585. while ((cmd = next_cmd(&buf))) {
  3586. if (strlencmp(cmd, "up") == 0) {
  3587. if (level < max_level)
  3588. level++;
  3589. } else if (strlencmp(cmd, "down") == 0) {
  3590. if (level > 0)
  3591. level--;
  3592. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  3593. level >= 0 && level <= max_level) {
  3594. /* new level set */
  3595. } else
  3596. return -EINVAL;
  3597. }
  3598. /*
  3599. * Now we know what the final level should be, so we try to set it.
  3600. * Doing it this way makes the syscall restartable in case of EINTR
  3601. */
  3602. rc = brightness_set(level);
  3603. return (rc == -EINTR)? ERESTARTSYS : rc;
  3604. }
  3605. static struct ibm_struct brightness_driver_data = {
  3606. .name = "brightness",
  3607. .read = brightness_read,
  3608. .write = brightness_write,
  3609. .exit = brightness_exit,
  3610. };
  3611. /*************************************************************************
  3612. * Volume subdriver
  3613. */
  3614. static int volume_offset = 0x30;
  3615. static int volume_read(char *p)
  3616. {
  3617. int len = 0;
  3618. u8 level;
  3619. if (!acpi_ec_read(volume_offset, &level)) {
  3620. len += sprintf(p + len, "level:\t\tunreadable\n");
  3621. } else {
  3622. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  3623. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  3624. len += sprintf(p + len, "commands:\tup, down, mute\n");
  3625. len += sprintf(p + len, "commands:\tlevel <level>"
  3626. " (<level> is 0-15)\n");
  3627. }
  3628. return len;
  3629. }
  3630. static int volume_write(char *buf)
  3631. {
  3632. int cmos_cmd, inc, i;
  3633. u8 level, mute;
  3634. int new_level, new_mute;
  3635. char *cmd;
  3636. while ((cmd = next_cmd(&buf))) {
  3637. if (!acpi_ec_read(volume_offset, &level))
  3638. return -EIO;
  3639. new_mute = mute = level & 0x40;
  3640. new_level = level = level & 0xf;
  3641. if (strlencmp(cmd, "up") == 0) {
  3642. if (mute)
  3643. new_mute = 0;
  3644. else
  3645. new_level = level == 15 ? 15 : level + 1;
  3646. } else if (strlencmp(cmd, "down") == 0) {
  3647. if (mute)
  3648. new_mute = 0;
  3649. else
  3650. new_level = level == 0 ? 0 : level - 1;
  3651. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  3652. new_level >= 0 && new_level <= 15) {
  3653. /* new_level set */
  3654. } else if (strlencmp(cmd, "mute") == 0) {
  3655. new_mute = 0x40;
  3656. } else
  3657. return -EINVAL;
  3658. if (new_level != level) { /* mute doesn't change */
  3659. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  3660. inc = new_level > level ? 1 : -1;
  3661. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  3662. !acpi_ec_write(volume_offset, level)))
  3663. return -EIO;
  3664. for (i = level; i != new_level; i += inc)
  3665. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  3666. !acpi_ec_write(volume_offset, i + inc))
  3667. return -EIO;
  3668. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  3669. !acpi_ec_write(volume_offset,
  3670. new_level + mute)))
  3671. return -EIO;
  3672. }
  3673. if (new_mute != mute) { /* level doesn't change */
  3674. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  3675. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  3676. !acpi_ec_write(volume_offset, level + new_mute))
  3677. return -EIO;
  3678. }
  3679. }
  3680. return 0;
  3681. }
  3682. static struct ibm_struct volume_driver_data = {
  3683. .name = "volume",
  3684. .read = volume_read,
  3685. .write = volume_write,
  3686. };
  3687. /*************************************************************************
  3688. * Fan subdriver
  3689. */
  3690. /*
  3691. * FAN ACCESS MODES
  3692. *
  3693. * TPACPI_FAN_RD_ACPI_GFAN:
  3694. * ACPI GFAN method: returns fan level
  3695. *
  3696. * see TPACPI_FAN_WR_ACPI_SFAN
  3697. * EC 0x2f (HFSP) not available if GFAN exists
  3698. *
  3699. * TPACPI_FAN_WR_ACPI_SFAN:
  3700. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  3701. *
  3702. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  3703. * it for writing.
  3704. *
  3705. * TPACPI_FAN_WR_TPEC:
  3706. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  3707. * Supported on almost all ThinkPads
  3708. *
  3709. * Fan speed changes of any sort (including those caused by the
  3710. * disengaged mode) are usually done slowly by the firmware as the
  3711. * maximum ammount of fan duty cycle change per second seems to be
  3712. * limited.
  3713. *
  3714. * Reading is not available if GFAN exists.
  3715. * Writing is not available if SFAN exists.
  3716. *
  3717. * Bits
  3718. * 7 automatic mode engaged;
  3719. * (default operation mode of the ThinkPad)
  3720. * fan level is ignored in this mode.
  3721. * 6 full speed mode (takes precedence over bit 7);
  3722. * not available on all thinkpads. May disable
  3723. * the tachometer while the fan controller ramps up
  3724. * the speed (which can take up to a few *minutes*).
  3725. * Speeds up fan to 100% duty-cycle, which is far above
  3726. * the standard RPM levels. It is not impossible that
  3727. * it could cause hardware damage.
  3728. * 5-3 unused in some models. Extra bits for fan level
  3729. * in others, but still useless as all values above
  3730. * 7 map to the same speed as level 7 in these models.
  3731. * 2-0 fan level (0..7 usually)
  3732. * 0x00 = stop
  3733. * 0x07 = max (set when temperatures critical)
  3734. * Some ThinkPads may have other levels, see
  3735. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  3736. *
  3737. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  3738. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  3739. * does so, its initial value is meaningless (0x07).
  3740. *
  3741. * For firmware bugs, refer to:
  3742. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  3743. *
  3744. * ----
  3745. *
  3746. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  3747. * Main fan tachometer reading (in RPM)
  3748. *
  3749. * This register is present on all ThinkPads with a new-style EC, and
  3750. * it is known not to be present on the A21m/e, and T22, as there is
  3751. * something else in offset 0x84 according to the ACPI DSDT. Other
  3752. * ThinkPads from this same time period (and earlier) probably lack the
  3753. * tachometer as well.
  3754. *
  3755. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  3756. * was never fixed by IBM to report the EC firmware version string
  3757. * probably support the tachometer (like the early X models), so
  3758. * detecting it is quite hard. We need more data to know for sure.
  3759. *
  3760. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  3761. * might result.
  3762. *
  3763. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  3764. * mode.
  3765. *
  3766. * For firmware bugs, refer to:
  3767. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  3768. *
  3769. * TPACPI_FAN_WR_ACPI_FANS:
  3770. * ThinkPad X31, X40, X41. Not available in the X60.
  3771. *
  3772. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  3773. * high speed. ACPI DSDT seems to map these three speeds to levels
  3774. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  3775. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  3776. *
  3777. * The speeds are stored on handles
  3778. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  3779. *
  3780. * There are three default speed sets, acessible as handles:
  3781. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  3782. *
  3783. * ACPI DSDT switches which set is in use depending on various
  3784. * factors.
  3785. *
  3786. * TPACPI_FAN_WR_TPEC is also available and should be used to
  3787. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  3788. * but the ACPI tables just mention level 7.
  3789. */
  3790. enum { /* Fan control constants */
  3791. fan_status_offset = 0x2f, /* EC register 0x2f */
  3792. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  3793. * 0x84 must be read before 0x85 */
  3794. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  3795. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  3796. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  3797. };
  3798. enum fan_status_access_mode {
  3799. TPACPI_FAN_NONE = 0, /* No fan status or control */
  3800. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  3801. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  3802. };
  3803. enum fan_control_access_mode {
  3804. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  3805. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  3806. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  3807. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  3808. };
  3809. enum fan_control_commands {
  3810. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  3811. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  3812. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  3813. * and also watchdog cmd */
  3814. };
  3815. static int fan_control_allowed;
  3816. static enum fan_status_access_mode fan_status_access_mode;
  3817. static enum fan_control_access_mode fan_control_access_mode;
  3818. static enum fan_control_commands fan_control_commands;
  3819. static u8 fan_control_initial_status;
  3820. static u8 fan_control_desired_level;
  3821. static int fan_watchdog_maxinterval;
  3822. static struct mutex fan_mutex;
  3823. static int fan_get_status(u8 *status);
  3824. static int fan_get_status_safe(u8 *status);
  3825. static int fan_get_speed(unsigned int *speed);
  3826. static void fan_update_desired_level(u8 status);
  3827. static void fan_watchdog_fire(struct work_struct *ignored);
  3828. static void fan_watchdog_reset(void);
  3829. static int fan_set_level(int level);
  3830. static int fan_set_level_safe(int level);
  3831. static int fan_set_enable(void);
  3832. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  3833. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  3834. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  3835. "\\FSPD", /* 600e/x, 770e, 770x */
  3836. ); /* all others */
  3837. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  3838. "JFNS", /* 770x-JL */
  3839. ); /* all others */
  3840. /*
  3841. * SYSFS fan layout: hwmon compatible (device)
  3842. *
  3843. * pwm*_enable:
  3844. * 0: "disengaged" mode
  3845. * 1: manual mode
  3846. * 2: native EC "auto" mode (recommended, hardware default)
  3847. *
  3848. * pwm*: set speed in manual mode, ignored otherwise.
  3849. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  3850. * interpolation.
  3851. *
  3852. * fan*_input: tachometer reading, RPM
  3853. *
  3854. *
  3855. * SYSFS fan layout: extensions
  3856. *
  3857. * fan_watchdog (driver):
  3858. * fan watchdog interval in seconds, 0 disables (default), max 120
  3859. */
  3860. /* sysfs fan pwm1_enable ----------------------------------------------- */
  3861. static ssize_t fan_pwm1_enable_show(struct device *dev,
  3862. struct device_attribute *attr,
  3863. char *buf)
  3864. {
  3865. int res, mode;
  3866. u8 status;
  3867. res = fan_get_status_safe(&status);
  3868. if (res)
  3869. return res;
  3870. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3871. if (status != fan_control_initial_status) {
  3872. tp_features.fan_ctrl_status_undef = 0;
  3873. } else {
  3874. /* Return most likely status. In fact, it
  3875. * might be the only possible status */
  3876. status = TP_EC_FAN_AUTO;
  3877. }
  3878. }
  3879. if (status & TP_EC_FAN_FULLSPEED) {
  3880. mode = 0;
  3881. } else if (status & TP_EC_FAN_AUTO) {
  3882. mode = 2;
  3883. } else
  3884. mode = 1;
  3885. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  3886. }
  3887. static ssize_t fan_pwm1_enable_store(struct device *dev,
  3888. struct device_attribute *attr,
  3889. const char *buf, size_t count)
  3890. {
  3891. unsigned long t;
  3892. int res, level;
  3893. if (parse_strtoul(buf, 2, &t))
  3894. return -EINVAL;
  3895. switch (t) {
  3896. case 0:
  3897. level = TP_EC_FAN_FULLSPEED;
  3898. break;
  3899. case 1:
  3900. level = TPACPI_FAN_LAST_LEVEL;
  3901. break;
  3902. case 2:
  3903. level = TP_EC_FAN_AUTO;
  3904. break;
  3905. case 3:
  3906. /* reserved for software-controlled auto mode */
  3907. return -ENOSYS;
  3908. default:
  3909. return -EINVAL;
  3910. }
  3911. res = fan_set_level_safe(level);
  3912. if (res == -ENXIO)
  3913. return -EINVAL;
  3914. else if (res < 0)
  3915. return res;
  3916. fan_watchdog_reset();
  3917. return count;
  3918. }
  3919. static struct device_attribute dev_attr_fan_pwm1_enable =
  3920. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  3921. fan_pwm1_enable_show, fan_pwm1_enable_store);
  3922. /* sysfs fan pwm1 ------------------------------------------------------ */
  3923. static ssize_t fan_pwm1_show(struct device *dev,
  3924. struct device_attribute *attr,
  3925. char *buf)
  3926. {
  3927. int res;
  3928. u8 status;
  3929. res = fan_get_status_safe(&status);
  3930. if (res)
  3931. return res;
  3932. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3933. if (status != fan_control_initial_status) {
  3934. tp_features.fan_ctrl_status_undef = 0;
  3935. } else {
  3936. status = TP_EC_FAN_AUTO;
  3937. }
  3938. }
  3939. if ((status &
  3940. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  3941. status = fan_control_desired_level;
  3942. if (status > 7)
  3943. status = 7;
  3944. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  3945. }
  3946. static ssize_t fan_pwm1_store(struct device *dev,
  3947. struct device_attribute *attr,
  3948. const char *buf, size_t count)
  3949. {
  3950. unsigned long s;
  3951. int rc;
  3952. u8 status, newlevel;
  3953. if (parse_strtoul(buf, 255, &s))
  3954. return -EINVAL;
  3955. /* scale down from 0-255 to 0-7 */
  3956. newlevel = (s >> 5) & 0x07;
  3957. if (mutex_lock_interruptible(&fan_mutex))
  3958. return -ERESTARTSYS;
  3959. rc = fan_get_status(&status);
  3960. if (!rc && (status &
  3961. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  3962. rc = fan_set_level(newlevel);
  3963. if (rc == -ENXIO)
  3964. rc = -EINVAL;
  3965. else if (!rc) {
  3966. fan_update_desired_level(newlevel);
  3967. fan_watchdog_reset();
  3968. }
  3969. }
  3970. mutex_unlock(&fan_mutex);
  3971. return (rc)? rc : count;
  3972. }
  3973. static struct device_attribute dev_attr_fan_pwm1 =
  3974. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  3975. fan_pwm1_show, fan_pwm1_store);
  3976. /* sysfs fan fan1_input ------------------------------------------------ */
  3977. static ssize_t fan_fan1_input_show(struct device *dev,
  3978. struct device_attribute *attr,
  3979. char *buf)
  3980. {
  3981. int res;
  3982. unsigned int speed;
  3983. res = fan_get_speed(&speed);
  3984. if (res < 0)
  3985. return res;
  3986. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  3987. }
  3988. static struct device_attribute dev_attr_fan_fan1_input =
  3989. __ATTR(fan1_input, S_IRUGO,
  3990. fan_fan1_input_show, NULL);
  3991. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  3992. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  3993. char *buf)
  3994. {
  3995. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  3996. }
  3997. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  3998. const char *buf, size_t count)
  3999. {
  4000. unsigned long t;
  4001. if (parse_strtoul(buf, 120, &t))
  4002. return -EINVAL;
  4003. if (!fan_control_allowed)
  4004. return -EPERM;
  4005. fan_watchdog_maxinterval = t;
  4006. fan_watchdog_reset();
  4007. return count;
  4008. }
  4009. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  4010. fan_fan_watchdog_show, fan_fan_watchdog_store);
  4011. /* --------------------------------------------------------------------- */
  4012. static struct attribute *fan_attributes[] = {
  4013. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  4014. &dev_attr_fan_fan1_input.attr,
  4015. NULL
  4016. };
  4017. static const struct attribute_group fan_attr_group = {
  4018. .attrs = fan_attributes,
  4019. };
  4020. static int __init fan_init(struct ibm_init_struct *iibm)
  4021. {
  4022. int rc;
  4023. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  4024. mutex_init(&fan_mutex);
  4025. fan_status_access_mode = TPACPI_FAN_NONE;
  4026. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4027. fan_control_commands = 0;
  4028. fan_watchdog_maxinterval = 0;
  4029. tp_features.fan_ctrl_status_undef = 0;
  4030. fan_control_desired_level = 7;
  4031. IBM_ACPIHANDLE_INIT(fans);
  4032. IBM_ACPIHANDLE_INIT(gfan);
  4033. IBM_ACPIHANDLE_INIT(sfan);
  4034. if (gfan_handle) {
  4035. /* 570, 600e/x, 770e, 770x */
  4036. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  4037. } else {
  4038. /* all other ThinkPads: note that even old-style
  4039. * ThinkPad ECs supports the fan control register */
  4040. if (likely(acpi_ec_read(fan_status_offset,
  4041. &fan_control_initial_status))) {
  4042. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  4043. /* In some ThinkPads, neither the EC nor the ACPI
  4044. * DSDT initialize the fan status, and it ends up
  4045. * being set to 0x07 when it *could* be either
  4046. * 0x07 or 0x80.
  4047. *
  4048. * Enable for TP-1Y (T43), TP-78 (R51e),
  4049. * TP-76 (R52), TP-70 (T43, R52), which are known
  4050. * to be buggy. */
  4051. if (fan_control_initial_status == 0x07) {
  4052. switch (thinkpad_id.ec_model) {
  4053. case 0x5931: /* TP-1Y */
  4054. case 0x3837: /* TP-78 */
  4055. case 0x3637: /* TP-76 */
  4056. case 0x3037: /* TP-70 */
  4057. printk(IBM_NOTICE
  4058. "fan_init: initial fan status is "
  4059. "unknown, assuming it is in auto "
  4060. "mode\n");
  4061. tp_features.fan_ctrl_status_undef = 1;
  4062. ;;
  4063. }
  4064. }
  4065. } else {
  4066. printk(IBM_ERR
  4067. "ThinkPad ACPI EC access misbehaving, "
  4068. "fan status and control unavailable\n");
  4069. return 1;
  4070. }
  4071. }
  4072. if (sfan_handle) {
  4073. /* 570, 770x-JL */
  4074. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  4075. fan_control_commands |=
  4076. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  4077. } else {
  4078. if (!gfan_handle) {
  4079. /* gfan without sfan means no fan control */
  4080. /* all other models implement TP EC 0x2f control */
  4081. if (fans_handle) {
  4082. /* X31, X40, X41 */
  4083. fan_control_access_mode =
  4084. TPACPI_FAN_WR_ACPI_FANS;
  4085. fan_control_commands |=
  4086. TPACPI_FAN_CMD_SPEED |
  4087. TPACPI_FAN_CMD_LEVEL |
  4088. TPACPI_FAN_CMD_ENABLE;
  4089. } else {
  4090. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  4091. fan_control_commands |=
  4092. TPACPI_FAN_CMD_LEVEL |
  4093. TPACPI_FAN_CMD_ENABLE;
  4094. }
  4095. }
  4096. }
  4097. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  4098. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  4099. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  4100. fan_status_access_mode, fan_control_access_mode);
  4101. /* fan control master switch */
  4102. if (!fan_control_allowed) {
  4103. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4104. fan_control_commands = 0;
  4105. dbg_printk(TPACPI_DBG_INIT,
  4106. "fan control features disabled by parameter\n");
  4107. }
  4108. /* update fan_control_desired_level */
  4109. if (fan_status_access_mode != TPACPI_FAN_NONE)
  4110. fan_get_status_safe(NULL);
  4111. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  4112. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  4113. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4114. &fan_attr_group);
  4115. if (!(rc < 0))
  4116. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  4117. &driver_attr_fan_watchdog);
  4118. if (rc < 0)
  4119. return rc;
  4120. return 0;
  4121. } else
  4122. return 1;
  4123. }
  4124. /*
  4125. * Call with fan_mutex held
  4126. */
  4127. static void fan_update_desired_level(u8 status)
  4128. {
  4129. if ((status &
  4130. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4131. if (status > 7)
  4132. fan_control_desired_level = 7;
  4133. else
  4134. fan_control_desired_level = status;
  4135. }
  4136. }
  4137. static int fan_get_status(u8 *status)
  4138. {
  4139. u8 s;
  4140. /* TODO:
  4141. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  4142. switch (fan_status_access_mode) {
  4143. case TPACPI_FAN_RD_ACPI_GFAN:
  4144. /* 570, 600e/x, 770e, 770x */
  4145. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  4146. return -EIO;
  4147. if (likely(status))
  4148. *status = s & 0x07;
  4149. break;
  4150. case TPACPI_FAN_RD_TPEC:
  4151. /* all except 570, 600e/x, 770e, 770x */
  4152. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  4153. return -EIO;
  4154. if (likely(status))
  4155. *status = s;
  4156. break;
  4157. default:
  4158. return -ENXIO;
  4159. }
  4160. return 0;
  4161. }
  4162. static int fan_get_status_safe(u8 *status)
  4163. {
  4164. int rc;
  4165. u8 s;
  4166. if (mutex_lock_interruptible(&fan_mutex))
  4167. return -ERESTARTSYS;
  4168. rc = fan_get_status(&s);
  4169. if (!rc)
  4170. fan_update_desired_level(s);
  4171. mutex_unlock(&fan_mutex);
  4172. if (status)
  4173. *status = s;
  4174. return rc;
  4175. }
  4176. static void fan_exit(void)
  4177. {
  4178. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  4179. /* FIXME: can we really do this unconditionally? */
  4180. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  4181. driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
  4182. cancel_delayed_work(&fan_watchdog_task);
  4183. flush_scheduled_work();
  4184. }
  4185. static int fan_get_speed(unsigned int *speed)
  4186. {
  4187. u8 hi, lo;
  4188. switch (fan_status_access_mode) {
  4189. case TPACPI_FAN_RD_TPEC:
  4190. /* all except 570, 600e/x, 770e, 770x */
  4191. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  4192. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  4193. return -EIO;
  4194. if (likely(speed))
  4195. *speed = (hi << 8) | lo;
  4196. break;
  4197. default:
  4198. return -ENXIO;
  4199. }
  4200. return 0;
  4201. }
  4202. static void fan_watchdog_fire(struct work_struct *ignored)
  4203. {
  4204. int rc;
  4205. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  4206. return;
  4207. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  4208. rc = fan_set_enable();
  4209. if (rc < 0) {
  4210. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  4211. "will try again later...\n", -rc);
  4212. /* reschedule for later */
  4213. fan_watchdog_reset();
  4214. }
  4215. }
  4216. static void fan_watchdog_reset(void)
  4217. {
  4218. static int fan_watchdog_active;
  4219. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  4220. return;
  4221. if (fan_watchdog_active)
  4222. cancel_delayed_work(&fan_watchdog_task);
  4223. if (fan_watchdog_maxinterval > 0 &&
  4224. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  4225. fan_watchdog_active = 1;
  4226. if (!schedule_delayed_work(&fan_watchdog_task,
  4227. msecs_to_jiffies(fan_watchdog_maxinterval
  4228. * 1000))) {
  4229. printk(IBM_ERR "failed to schedule the fan watchdog, "
  4230. "watchdog will not trigger\n");
  4231. }
  4232. } else
  4233. fan_watchdog_active = 0;
  4234. }
  4235. static int fan_set_level(int level)
  4236. {
  4237. if (!fan_control_allowed)
  4238. return -EPERM;
  4239. switch (fan_control_access_mode) {
  4240. case TPACPI_FAN_WR_ACPI_SFAN:
  4241. if (level >= 0 && level <= 7) {
  4242. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  4243. return -EIO;
  4244. } else
  4245. return -EINVAL;
  4246. break;
  4247. case TPACPI_FAN_WR_ACPI_FANS:
  4248. case TPACPI_FAN_WR_TPEC:
  4249. if ((level != TP_EC_FAN_AUTO) &&
  4250. (level != TP_EC_FAN_FULLSPEED) &&
  4251. ((level < 0) || (level > 7)))
  4252. return -EINVAL;
  4253. /* safety net should the EC not support AUTO
  4254. * or FULLSPEED mode bits and just ignore them */
  4255. if (level & TP_EC_FAN_FULLSPEED)
  4256. level |= 7; /* safety min speed 7 */
  4257. else if (level & TP_EC_FAN_FULLSPEED)
  4258. level |= 4; /* safety min speed 4 */
  4259. if (!acpi_ec_write(fan_status_offset, level))
  4260. return -EIO;
  4261. else
  4262. tp_features.fan_ctrl_status_undef = 0;
  4263. break;
  4264. default:
  4265. return -ENXIO;
  4266. }
  4267. return 0;
  4268. }
  4269. static int fan_set_level_safe(int level)
  4270. {
  4271. int rc;
  4272. if (!fan_control_allowed)
  4273. return -EPERM;
  4274. if (mutex_lock_interruptible(&fan_mutex))
  4275. return -ERESTARTSYS;
  4276. if (level == TPACPI_FAN_LAST_LEVEL)
  4277. level = fan_control_desired_level;
  4278. rc = fan_set_level(level);
  4279. if (!rc)
  4280. fan_update_desired_level(level);
  4281. mutex_unlock(&fan_mutex);
  4282. return rc;
  4283. }
  4284. static int fan_set_enable(void)
  4285. {
  4286. u8 s;
  4287. int rc;
  4288. if (!fan_control_allowed)
  4289. return -EPERM;
  4290. if (mutex_lock_interruptible(&fan_mutex))
  4291. return -ERESTARTSYS;
  4292. switch (fan_control_access_mode) {
  4293. case TPACPI_FAN_WR_ACPI_FANS:
  4294. case TPACPI_FAN_WR_TPEC:
  4295. rc = fan_get_status(&s);
  4296. if (rc < 0)
  4297. break;
  4298. /* Don't go out of emergency fan mode */
  4299. if (s != 7) {
  4300. s &= 0x07;
  4301. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  4302. }
  4303. if (!acpi_ec_write(fan_status_offset, s))
  4304. rc = -EIO;
  4305. else {
  4306. tp_features.fan_ctrl_status_undef = 0;
  4307. rc = 0;
  4308. }
  4309. break;
  4310. case TPACPI_FAN_WR_ACPI_SFAN:
  4311. rc = fan_get_status(&s);
  4312. if (rc < 0)
  4313. break;
  4314. s &= 0x07;
  4315. /* Set fan to at least level 4 */
  4316. s |= 4;
  4317. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  4318. rc= -EIO;
  4319. else
  4320. rc = 0;
  4321. break;
  4322. default:
  4323. rc = -ENXIO;
  4324. }
  4325. mutex_unlock(&fan_mutex);
  4326. return rc;
  4327. }
  4328. static int fan_set_disable(void)
  4329. {
  4330. int rc;
  4331. if (!fan_control_allowed)
  4332. return -EPERM;
  4333. if (mutex_lock_interruptible(&fan_mutex))
  4334. return -ERESTARTSYS;
  4335. rc = 0;
  4336. switch (fan_control_access_mode) {
  4337. case TPACPI_FAN_WR_ACPI_FANS:
  4338. case TPACPI_FAN_WR_TPEC:
  4339. if (!acpi_ec_write(fan_status_offset, 0x00))
  4340. rc = -EIO;
  4341. else {
  4342. fan_control_desired_level = 0;
  4343. tp_features.fan_ctrl_status_undef = 0;
  4344. }
  4345. break;
  4346. case TPACPI_FAN_WR_ACPI_SFAN:
  4347. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  4348. rc = -EIO;
  4349. else
  4350. fan_control_desired_level = 0;
  4351. break;
  4352. default:
  4353. rc = -ENXIO;
  4354. }
  4355. mutex_unlock(&fan_mutex);
  4356. return rc;
  4357. }
  4358. static int fan_set_speed(int speed)
  4359. {
  4360. int rc;
  4361. if (!fan_control_allowed)
  4362. return -EPERM;
  4363. if (mutex_lock_interruptible(&fan_mutex))
  4364. return -ERESTARTSYS;
  4365. rc = 0;
  4366. switch (fan_control_access_mode) {
  4367. case TPACPI_FAN_WR_ACPI_FANS:
  4368. if (speed >= 0 && speed <= 65535) {
  4369. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  4370. speed, speed, speed))
  4371. rc = -EIO;
  4372. } else
  4373. rc = -EINVAL;
  4374. break;
  4375. default:
  4376. rc = -ENXIO;
  4377. }
  4378. mutex_unlock(&fan_mutex);
  4379. return rc;
  4380. }
  4381. static int fan_read(char *p)
  4382. {
  4383. int len = 0;
  4384. int rc;
  4385. u8 status;
  4386. unsigned int speed = 0;
  4387. switch (fan_status_access_mode) {
  4388. case TPACPI_FAN_RD_ACPI_GFAN:
  4389. /* 570, 600e/x, 770e, 770x */
  4390. if ((rc = fan_get_status_safe(&status)) < 0)
  4391. return rc;
  4392. len += sprintf(p + len, "status:\t\t%s\n"
  4393. "level:\t\t%d\n",
  4394. (status != 0) ? "enabled" : "disabled", status);
  4395. break;
  4396. case TPACPI_FAN_RD_TPEC:
  4397. /* all except 570, 600e/x, 770e, 770x */
  4398. if ((rc = fan_get_status_safe(&status)) < 0)
  4399. return rc;
  4400. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4401. if (status != fan_control_initial_status)
  4402. tp_features.fan_ctrl_status_undef = 0;
  4403. else
  4404. /* Return most likely status. In fact, it
  4405. * might be the only possible status */
  4406. status = TP_EC_FAN_AUTO;
  4407. }
  4408. len += sprintf(p + len, "status:\t\t%s\n",
  4409. (status != 0) ? "enabled" : "disabled");
  4410. if ((rc = fan_get_speed(&speed)) < 0)
  4411. return rc;
  4412. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  4413. if (status & TP_EC_FAN_FULLSPEED)
  4414. /* Disengaged mode takes precedence */
  4415. len += sprintf(p + len, "level:\t\tdisengaged\n");
  4416. else if (status & TP_EC_FAN_AUTO)
  4417. len += sprintf(p + len, "level:\t\tauto\n");
  4418. else
  4419. len += sprintf(p + len, "level:\t\t%d\n", status);
  4420. break;
  4421. case TPACPI_FAN_NONE:
  4422. default:
  4423. len += sprintf(p + len, "status:\t\tnot supported\n");
  4424. }
  4425. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  4426. len += sprintf(p + len, "commands:\tlevel <level>");
  4427. switch (fan_control_access_mode) {
  4428. case TPACPI_FAN_WR_ACPI_SFAN:
  4429. len += sprintf(p + len, " (<level> is 0-7)\n");
  4430. break;
  4431. default:
  4432. len += sprintf(p + len, " (<level> is 0-7, "
  4433. "auto, disengaged, full-speed)\n");
  4434. break;
  4435. }
  4436. }
  4437. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  4438. len += sprintf(p + len, "commands:\tenable, disable\n"
  4439. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  4440. "1-120 (seconds))\n");
  4441. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  4442. len += sprintf(p + len, "commands:\tspeed <speed>"
  4443. " (<speed> is 0-65535)\n");
  4444. return len;
  4445. }
  4446. static int fan_write_cmd_level(const char *cmd, int *rc)
  4447. {
  4448. int level;
  4449. if (strlencmp(cmd, "level auto") == 0)
  4450. level = TP_EC_FAN_AUTO;
  4451. else if ((strlencmp(cmd, "level disengaged") == 0) |
  4452. (strlencmp(cmd, "level full-speed") == 0))
  4453. level = TP_EC_FAN_FULLSPEED;
  4454. else if (sscanf(cmd, "level %d", &level) != 1)
  4455. return 0;
  4456. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  4457. printk(IBM_ERR "level command accepted for unsupported "
  4458. "access mode %d", fan_control_access_mode);
  4459. return 1;
  4460. }
  4461. static int fan_write_cmd_enable(const char *cmd, int *rc)
  4462. {
  4463. if (strlencmp(cmd, "enable") != 0)
  4464. return 0;
  4465. if ((*rc = fan_set_enable()) == -ENXIO)
  4466. printk(IBM_ERR "enable command accepted for unsupported "
  4467. "access mode %d", fan_control_access_mode);
  4468. return 1;
  4469. }
  4470. static int fan_write_cmd_disable(const char *cmd, int *rc)
  4471. {
  4472. if (strlencmp(cmd, "disable") != 0)
  4473. return 0;
  4474. if ((*rc = fan_set_disable()) == -ENXIO)
  4475. printk(IBM_ERR "disable command accepted for unsupported "
  4476. "access mode %d", fan_control_access_mode);
  4477. return 1;
  4478. }
  4479. static int fan_write_cmd_speed(const char *cmd, int *rc)
  4480. {
  4481. int speed;
  4482. /* TODO:
  4483. * Support speed <low> <medium> <high> ? */
  4484. if (sscanf(cmd, "speed %d", &speed) != 1)
  4485. return 0;
  4486. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  4487. printk(IBM_ERR "speed command accepted for unsupported "
  4488. "access mode %d", fan_control_access_mode);
  4489. return 1;
  4490. }
  4491. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  4492. {
  4493. int interval;
  4494. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  4495. return 0;
  4496. if (interval < 0 || interval > 120)
  4497. *rc = -EINVAL;
  4498. else
  4499. fan_watchdog_maxinterval = interval;
  4500. return 1;
  4501. }
  4502. static int fan_write(char *buf)
  4503. {
  4504. char *cmd;
  4505. int rc = 0;
  4506. while (!rc && (cmd = next_cmd(&buf))) {
  4507. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  4508. fan_write_cmd_level(cmd, &rc)) &&
  4509. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  4510. (fan_write_cmd_enable(cmd, &rc) ||
  4511. fan_write_cmd_disable(cmd, &rc) ||
  4512. fan_write_cmd_watchdog(cmd, &rc))) &&
  4513. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  4514. fan_write_cmd_speed(cmd, &rc))
  4515. )
  4516. rc = -EINVAL;
  4517. else if (!rc)
  4518. fan_watchdog_reset();
  4519. }
  4520. return rc;
  4521. }
  4522. static struct ibm_struct fan_driver_data = {
  4523. .name = "fan",
  4524. .read = fan_read,
  4525. .write = fan_write,
  4526. .exit = fan_exit,
  4527. };
  4528. /****************************************************************************
  4529. ****************************************************************************
  4530. *
  4531. * Infrastructure
  4532. *
  4533. ****************************************************************************
  4534. ****************************************************************************/
  4535. /* sysfs name ---------------------------------------------------------- */
  4536. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  4537. struct device_attribute *attr,
  4538. char *buf)
  4539. {
  4540. return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
  4541. }
  4542. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  4543. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  4544. /* --------------------------------------------------------------------- */
  4545. /* /proc support */
  4546. static struct proc_dir_entry *proc_dir;
  4547. /*
  4548. * Module and infrastructure proble, init and exit handling
  4549. */
  4550. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  4551. static const char * __init str_supported(int is_supported)
  4552. {
  4553. static char text_unsupported[] __initdata = "not supported";
  4554. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  4555. }
  4556. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  4557. static void ibm_exit(struct ibm_struct *ibm);
  4558. static int __init ibm_init(struct ibm_init_struct *iibm)
  4559. {
  4560. int ret;
  4561. struct ibm_struct *ibm = iibm->data;
  4562. struct proc_dir_entry *entry;
  4563. BUG_ON(ibm == NULL);
  4564. INIT_LIST_HEAD(&ibm->all_drivers);
  4565. if (ibm->flags.experimental && !experimental)
  4566. return 0;
  4567. dbg_printk(TPACPI_DBG_INIT,
  4568. "probing for %s\n", ibm->name);
  4569. if (iibm->init) {
  4570. ret = iibm->init(iibm);
  4571. if (ret > 0)
  4572. return 0; /* probe failed */
  4573. if (ret)
  4574. return ret;
  4575. ibm->flags.init_called = 1;
  4576. }
  4577. if (ibm->acpi) {
  4578. if (ibm->acpi->hid) {
  4579. ret = register_tpacpi_subdriver(ibm);
  4580. if (ret)
  4581. goto err_out;
  4582. }
  4583. if (ibm->acpi->notify) {
  4584. ret = setup_acpi_notify(ibm);
  4585. if (ret == -ENODEV) {
  4586. printk(IBM_NOTICE "disabling subdriver %s\n",
  4587. ibm->name);
  4588. ret = 0;
  4589. goto err_out;
  4590. }
  4591. if (ret < 0)
  4592. goto err_out;
  4593. }
  4594. }
  4595. dbg_printk(TPACPI_DBG_INIT,
  4596. "%s installed\n", ibm->name);
  4597. if (ibm->read) {
  4598. entry = create_proc_entry(ibm->name,
  4599. S_IFREG | S_IRUGO | S_IWUSR,
  4600. proc_dir);
  4601. if (!entry) {
  4602. printk(IBM_ERR "unable to create proc entry %s\n",
  4603. ibm->name);
  4604. ret = -ENODEV;
  4605. goto err_out;
  4606. }
  4607. entry->owner = THIS_MODULE;
  4608. entry->data = ibm;
  4609. entry->read_proc = &dispatch_procfs_read;
  4610. if (ibm->write)
  4611. entry->write_proc = &dispatch_procfs_write;
  4612. ibm->flags.proc_created = 1;
  4613. }
  4614. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  4615. return 0;
  4616. err_out:
  4617. dbg_printk(TPACPI_DBG_INIT,
  4618. "%s: at error exit path with result %d\n",
  4619. ibm->name, ret);
  4620. ibm_exit(ibm);
  4621. return (ret < 0)? ret : 0;
  4622. }
  4623. static void ibm_exit(struct ibm_struct *ibm)
  4624. {
  4625. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  4626. list_del_init(&ibm->all_drivers);
  4627. if (ibm->flags.acpi_notify_installed) {
  4628. dbg_printk(TPACPI_DBG_EXIT,
  4629. "%s: acpi_remove_notify_handler\n", ibm->name);
  4630. BUG_ON(!ibm->acpi);
  4631. acpi_remove_notify_handler(*ibm->acpi->handle,
  4632. ibm->acpi->type,
  4633. dispatch_acpi_notify);
  4634. ibm->flags.acpi_notify_installed = 0;
  4635. ibm->flags.acpi_notify_installed = 0;
  4636. }
  4637. if (ibm->flags.proc_created) {
  4638. dbg_printk(TPACPI_DBG_EXIT,
  4639. "%s: remove_proc_entry\n", ibm->name);
  4640. remove_proc_entry(ibm->name, proc_dir);
  4641. ibm->flags.proc_created = 0;
  4642. }
  4643. if (ibm->flags.acpi_driver_registered) {
  4644. dbg_printk(TPACPI_DBG_EXIT,
  4645. "%s: acpi_bus_unregister_driver\n", ibm->name);
  4646. BUG_ON(!ibm->acpi);
  4647. acpi_bus_unregister_driver(ibm->acpi->driver);
  4648. kfree(ibm->acpi->driver);
  4649. ibm->acpi->driver = NULL;
  4650. ibm->flags.acpi_driver_registered = 0;
  4651. }
  4652. if (ibm->flags.init_called && ibm->exit) {
  4653. ibm->exit();
  4654. ibm->flags.init_called = 0;
  4655. }
  4656. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  4657. }
  4658. /* Probing */
  4659. static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
  4660. {
  4661. const struct dmi_device *dev = NULL;
  4662. char ec_fw_string[18];
  4663. if (!tp)
  4664. return;
  4665. memset(tp, 0, sizeof(*tp));
  4666. if (dmi_name_in_vendors("IBM"))
  4667. tp->vendor = PCI_VENDOR_ID_IBM;
  4668. else if (dmi_name_in_vendors("LENOVO"))
  4669. tp->vendor = PCI_VENDOR_ID_LENOVO;
  4670. else
  4671. return;
  4672. tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
  4673. GFP_KERNEL);
  4674. if (!tp->bios_version_str)
  4675. return;
  4676. tp->bios_model = tp->bios_version_str[0]
  4677. | (tp->bios_version_str[1] << 8);
  4678. /*
  4679. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  4680. * X32 or newer, all Z series; Some models must have an
  4681. * up-to-date BIOS or they will not be detected.
  4682. *
  4683. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  4684. */
  4685. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  4686. if (sscanf(dev->name,
  4687. "IBM ThinkPad Embedded Controller -[%17c",
  4688. ec_fw_string) == 1) {
  4689. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  4690. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  4691. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  4692. tp->ec_model = ec_fw_string[0]
  4693. | (ec_fw_string[1] << 8);
  4694. break;
  4695. }
  4696. }
  4697. tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
  4698. GFP_KERNEL);
  4699. if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
  4700. kfree(tp->model_str);
  4701. tp->model_str = NULL;
  4702. }
  4703. }
  4704. static int __init probe_for_thinkpad(void)
  4705. {
  4706. int is_thinkpad;
  4707. if (acpi_disabled)
  4708. return -ENODEV;
  4709. /*
  4710. * Non-ancient models have better DMI tagging, but very old models
  4711. * don't.
  4712. */
  4713. is_thinkpad = (thinkpad_id.model_str != NULL);
  4714. /* ec is required because many other handles are relative to it */
  4715. IBM_ACPIHANDLE_INIT(ec);
  4716. if (!ec_handle) {
  4717. if (is_thinkpad)
  4718. printk(IBM_ERR
  4719. "Not yet supported ThinkPad detected!\n");
  4720. return -ENODEV;
  4721. }
  4722. /*
  4723. * Risks a regression on very old machines, but reduces potential
  4724. * false positives a damn great deal
  4725. */
  4726. if (!is_thinkpad)
  4727. is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
  4728. if (!is_thinkpad && !force_load)
  4729. return -ENODEV;
  4730. return 0;
  4731. }
  4732. /* Module init, exit, parameters */
  4733. static struct ibm_init_struct ibms_init[] __initdata = {
  4734. {
  4735. .init = thinkpad_acpi_driver_init,
  4736. .data = &thinkpad_acpi_driver_data,
  4737. },
  4738. {
  4739. .init = hotkey_init,
  4740. .data = &hotkey_driver_data,
  4741. },
  4742. {
  4743. .init = bluetooth_init,
  4744. .data = &bluetooth_driver_data,
  4745. },
  4746. {
  4747. .init = wan_init,
  4748. .data = &wan_driver_data,
  4749. },
  4750. {
  4751. .init = video_init,
  4752. .data = &video_driver_data,
  4753. },
  4754. {
  4755. .init = light_init,
  4756. .data = &light_driver_data,
  4757. },
  4758. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  4759. {
  4760. .init = dock_init,
  4761. .data = &dock_driver_data[0],
  4762. },
  4763. {
  4764. .init = dock_init2,
  4765. .data = &dock_driver_data[1],
  4766. },
  4767. #endif
  4768. #ifdef CONFIG_THINKPAD_ACPI_BAY
  4769. {
  4770. .init = bay_init,
  4771. .data = &bay_driver_data,
  4772. },
  4773. #endif
  4774. {
  4775. .init = cmos_init,
  4776. .data = &cmos_driver_data,
  4777. },
  4778. {
  4779. .init = led_init,
  4780. .data = &led_driver_data,
  4781. },
  4782. {
  4783. .init = beep_init,
  4784. .data = &beep_driver_data,
  4785. },
  4786. {
  4787. .init = thermal_init,
  4788. .data = &thermal_driver_data,
  4789. },
  4790. {
  4791. .data = &ecdump_driver_data,
  4792. },
  4793. {
  4794. .init = brightness_init,
  4795. .data = &brightness_driver_data,
  4796. },
  4797. {
  4798. .data = &volume_driver_data,
  4799. },
  4800. {
  4801. .init = fan_init,
  4802. .data = &fan_driver_data,
  4803. },
  4804. };
  4805. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  4806. {
  4807. unsigned int i;
  4808. struct ibm_struct *ibm;
  4809. if (!kp || !kp->name || !val)
  4810. return -EINVAL;
  4811. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  4812. ibm = ibms_init[i].data;
  4813. WARN_ON(ibm == NULL);
  4814. if (!ibm || !ibm->name)
  4815. continue;
  4816. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  4817. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  4818. return -ENOSPC;
  4819. strcpy(ibms_init[i].param, val);
  4820. strcat(ibms_init[i].param, ",");
  4821. return 0;
  4822. }
  4823. }
  4824. return -EINVAL;
  4825. }
  4826. module_param(experimental, int, 0);
  4827. module_param_named(debug, dbg_level, uint, 0);
  4828. module_param(force_load, bool, 0);
  4829. module_param_named(fan_control, fan_control_allowed, bool, 0);
  4830. module_param_named(brightness_mode, brightness_mode, int, 0);
  4831. module_param(brightness_enable, uint, 0);
  4832. module_param(hotkey_report_mode, uint, 0);
  4833. #define IBM_PARAM(feature) \
  4834. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  4835. IBM_PARAM(hotkey);
  4836. IBM_PARAM(bluetooth);
  4837. IBM_PARAM(video);
  4838. IBM_PARAM(light);
  4839. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  4840. IBM_PARAM(dock);
  4841. #endif
  4842. #ifdef CONFIG_THINKPAD_ACPI_BAY
  4843. IBM_PARAM(bay);
  4844. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  4845. IBM_PARAM(cmos);
  4846. IBM_PARAM(led);
  4847. IBM_PARAM(beep);
  4848. IBM_PARAM(ecdump);
  4849. IBM_PARAM(brightness);
  4850. IBM_PARAM(volume);
  4851. IBM_PARAM(fan);
  4852. static void thinkpad_acpi_module_exit(void);
  4853. static int __init thinkpad_acpi_module_init(void)
  4854. {
  4855. int ret, i;
  4856. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  4857. /* Parameter checking */
  4858. if (hotkey_report_mode > 2)
  4859. return -EINVAL;
  4860. /* Driver-level probe */
  4861. get_thinkpad_model_data(&thinkpad_id);
  4862. ret = probe_for_thinkpad();
  4863. if (ret) {
  4864. thinkpad_acpi_module_exit();
  4865. return ret;
  4866. }
  4867. /* Driver initialization */
  4868. IBM_ACPIHANDLE_INIT(ecrd);
  4869. IBM_ACPIHANDLE_INIT(ecwr);
  4870. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  4871. if (!proc_dir) {
  4872. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  4873. thinkpad_acpi_module_exit();
  4874. return -ENODEV;
  4875. }
  4876. proc_dir->owner = THIS_MODULE;
  4877. ret = platform_driver_register(&tpacpi_pdriver);
  4878. if (ret) {
  4879. printk(IBM_ERR "unable to register main platform driver\n");
  4880. thinkpad_acpi_module_exit();
  4881. return ret;
  4882. }
  4883. tp_features.platform_drv_registered = 1;
  4884. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  4885. if (ret) {
  4886. printk(IBM_ERR "unable to register hwmon platform driver\n");
  4887. thinkpad_acpi_module_exit();
  4888. return ret;
  4889. }
  4890. tp_features.sensors_pdrv_registered = 1;
  4891. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  4892. if (!ret) {
  4893. tp_features.platform_drv_attrs_registered = 1;
  4894. ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  4895. }
  4896. if (ret) {
  4897. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  4898. thinkpad_acpi_module_exit();
  4899. return ret;
  4900. }
  4901. tp_features.sensors_pdrv_attrs_registered = 1;
  4902. /* Device initialization */
  4903. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  4904. NULL, 0);
  4905. if (IS_ERR(tpacpi_pdev)) {
  4906. ret = PTR_ERR(tpacpi_pdev);
  4907. tpacpi_pdev = NULL;
  4908. printk(IBM_ERR "unable to register platform device\n");
  4909. thinkpad_acpi_module_exit();
  4910. return ret;
  4911. }
  4912. tpacpi_sensors_pdev = platform_device_register_simple(
  4913. IBM_HWMON_DRVR_NAME,
  4914. -1, NULL, 0);
  4915. if (IS_ERR(tpacpi_sensors_pdev)) {
  4916. ret = PTR_ERR(tpacpi_sensors_pdev);
  4917. tpacpi_sensors_pdev = NULL;
  4918. printk(IBM_ERR "unable to register hwmon platform device\n");
  4919. thinkpad_acpi_module_exit();
  4920. return ret;
  4921. }
  4922. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  4923. &dev_attr_thinkpad_acpi_pdev_name);
  4924. if (ret) {
  4925. printk(IBM_ERR
  4926. "unable to create sysfs hwmon device attributes\n");
  4927. thinkpad_acpi_module_exit();
  4928. return ret;
  4929. }
  4930. tp_features.sensors_pdev_attrs_registered = 1;
  4931. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  4932. if (IS_ERR(tpacpi_hwmon)) {
  4933. ret = PTR_ERR(tpacpi_hwmon);
  4934. tpacpi_hwmon = NULL;
  4935. printk(IBM_ERR "unable to register hwmon device\n");
  4936. thinkpad_acpi_module_exit();
  4937. return ret;
  4938. }
  4939. mutex_init(&tpacpi_inputdev_send_mutex);
  4940. tpacpi_inputdev = input_allocate_device();
  4941. if (!tpacpi_inputdev) {
  4942. printk(IBM_ERR "unable to allocate input device\n");
  4943. thinkpad_acpi_module_exit();
  4944. return -ENOMEM;
  4945. } else {
  4946. /* Prepare input device, but don't register */
  4947. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  4948. tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
  4949. tpacpi_inputdev->id.bustype = BUS_HOST;
  4950. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  4951. thinkpad_id.vendor :
  4952. PCI_VENDOR_ID_IBM;
  4953. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  4954. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  4955. }
  4956. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  4957. ret = ibm_init(&ibms_init[i]);
  4958. if (ret >= 0 && *ibms_init[i].param)
  4959. ret = ibms_init[i].data->write(ibms_init[i].param);
  4960. if (ret < 0) {
  4961. thinkpad_acpi_module_exit();
  4962. return ret;
  4963. }
  4964. }
  4965. ret = input_register_device(tpacpi_inputdev);
  4966. if (ret < 0) {
  4967. printk(IBM_ERR "unable to register input device\n");
  4968. thinkpad_acpi_module_exit();
  4969. return ret;
  4970. } else {
  4971. tp_features.input_device_registered = 1;
  4972. }
  4973. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  4974. return 0;
  4975. }
  4976. static void thinkpad_acpi_module_exit(void)
  4977. {
  4978. struct ibm_struct *ibm, *itmp;
  4979. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  4980. list_for_each_entry_safe_reverse(ibm, itmp,
  4981. &tpacpi_all_drivers,
  4982. all_drivers) {
  4983. ibm_exit(ibm);
  4984. }
  4985. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  4986. if (tpacpi_inputdev) {
  4987. if (tp_features.input_device_registered)
  4988. input_unregister_device(tpacpi_inputdev);
  4989. else
  4990. input_free_device(tpacpi_inputdev);
  4991. }
  4992. if (tpacpi_hwmon)
  4993. hwmon_device_unregister(tpacpi_hwmon);
  4994. if (tp_features.sensors_pdev_attrs_registered)
  4995. device_remove_file(&tpacpi_sensors_pdev->dev,
  4996. &dev_attr_thinkpad_acpi_pdev_name);
  4997. if (tpacpi_sensors_pdev)
  4998. platform_device_unregister(tpacpi_sensors_pdev);
  4999. if (tpacpi_pdev)
  5000. platform_device_unregister(tpacpi_pdev);
  5001. if (tp_features.sensors_pdrv_attrs_registered)
  5002. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  5003. if (tp_features.platform_drv_attrs_registered)
  5004. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  5005. if (tp_features.sensors_pdrv_registered)
  5006. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  5007. if (tp_features.platform_drv_registered)
  5008. platform_driver_unregister(&tpacpi_pdriver);
  5009. if (proc_dir)
  5010. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  5011. kfree(thinkpad_id.bios_version_str);
  5012. kfree(thinkpad_id.ec_version_str);
  5013. kfree(thinkpad_id.model_str);
  5014. }
  5015. module_init(thinkpad_acpi_module_init);
  5016. module_exit(thinkpad_acpi_module_exit);