thinkpad_acpi.c 144 KB

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