thinkpad_acpi.c 136 KB

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