thinkpad_acpi.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073
  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.17"
  24. #define TPACPI_SYSFS_VERSION 0x020000
  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. static int hotkey_orig_status;
  632. static u32 hotkey_orig_mask;
  633. static u32 hotkey_all_mask;
  634. static u32 hotkey_reserved_mask;
  635. static u16 *hotkey_keycode_map;
  636. static struct attribute_set *hotkey_dev_attributes;
  637. static int hotkey_get_wlsw(int *status)
  638. {
  639. if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
  640. return -EIO;
  641. return 0;
  642. }
  643. /* sysfs hotkey enable ------------------------------------------------- */
  644. static ssize_t hotkey_enable_show(struct device *dev,
  645. struct device_attribute *attr,
  646. char *buf)
  647. {
  648. int res, status;
  649. u32 mask;
  650. res = hotkey_get(&status, &mask);
  651. if (res)
  652. return res;
  653. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  654. }
  655. static ssize_t hotkey_enable_store(struct device *dev,
  656. struct device_attribute *attr,
  657. const char *buf, size_t count)
  658. {
  659. unsigned long t;
  660. int res, status;
  661. u32 mask;
  662. if (parse_strtoul(buf, 1, &t))
  663. return -EINVAL;
  664. res = hotkey_get(&status, &mask);
  665. if (!res)
  666. res = hotkey_set(t, mask);
  667. return (res) ? res : count;
  668. }
  669. static struct device_attribute dev_attr_hotkey_enable =
  670. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  671. hotkey_enable_show, hotkey_enable_store);
  672. /* sysfs hotkey mask --------------------------------------------------- */
  673. static ssize_t hotkey_mask_show(struct device *dev,
  674. struct device_attribute *attr,
  675. char *buf)
  676. {
  677. int res, status;
  678. u32 mask;
  679. res = hotkey_get(&status, &mask);
  680. if (res)
  681. return res;
  682. return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
  683. }
  684. static ssize_t hotkey_mask_store(struct device *dev,
  685. struct device_attribute *attr,
  686. const char *buf, size_t count)
  687. {
  688. unsigned long t;
  689. int res, status;
  690. u32 mask;
  691. if (parse_strtoul(buf, 0xffffffffUL, &t))
  692. return -EINVAL;
  693. res = hotkey_get(&status, &mask);
  694. if (!res)
  695. hotkey_set(status, t);
  696. return (res) ? res : count;
  697. }
  698. static struct device_attribute dev_attr_hotkey_mask =
  699. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  700. hotkey_mask_show, hotkey_mask_store);
  701. /* sysfs hotkey bios_enabled ------------------------------------------- */
  702. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  703. struct device_attribute *attr,
  704. char *buf)
  705. {
  706. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
  707. }
  708. static struct device_attribute dev_attr_hotkey_bios_enabled =
  709. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  710. /* sysfs hotkey bios_mask ---------------------------------------------- */
  711. static ssize_t hotkey_bios_mask_show(struct device *dev,
  712. struct device_attribute *attr,
  713. char *buf)
  714. {
  715. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  716. }
  717. static struct device_attribute dev_attr_hotkey_bios_mask =
  718. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  719. /* sysfs hotkey all_mask ----------------------------------------------- */
  720. static ssize_t hotkey_all_mask_show(struct device *dev,
  721. struct device_attribute *attr,
  722. char *buf)
  723. {
  724. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
  725. }
  726. static struct device_attribute dev_attr_hotkey_all_mask =
  727. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  728. /* sysfs hotkey recommended_mask --------------------------------------- */
  729. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  730. struct device_attribute *attr,
  731. char *buf)
  732. {
  733. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  734. hotkey_all_mask & ~hotkey_reserved_mask);
  735. }
  736. static struct device_attribute dev_attr_hotkey_recommended_mask =
  737. __ATTR(hotkey_recommended_mask, S_IRUGO,
  738. hotkey_recommended_mask_show, NULL);
  739. /* sysfs hotkey radio_sw ----------------------------------------------- */
  740. static ssize_t hotkey_radio_sw_show(struct device *dev,
  741. struct device_attribute *attr,
  742. char *buf)
  743. {
  744. int res, s;
  745. res = hotkey_get_wlsw(&s);
  746. if (res < 0)
  747. return res;
  748. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  749. }
  750. static struct device_attribute dev_attr_hotkey_radio_sw =
  751. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  752. /* sysfs hotkey report_mode -------------------------------------------- */
  753. static ssize_t hotkey_report_mode_show(struct device *dev,
  754. struct device_attribute *attr,
  755. char *buf)
  756. {
  757. return snprintf(buf, PAGE_SIZE, "%d\n",
  758. (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
  759. }
  760. static struct device_attribute dev_attr_hotkey_report_mode =
  761. __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
  762. /* --------------------------------------------------------------------- */
  763. static struct attribute *hotkey_attributes[] __initdata = {
  764. &dev_attr_hotkey_enable.attr,
  765. &dev_attr_hotkey_report_mode.attr,
  766. };
  767. static struct attribute *hotkey_mask_attributes[] __initdata = {
  768. &dev_attr_hotkey_mask.attr,
  769. &dev_attr_hotkey_bios_enabled.attr,
  770. &dev_attr_hotkey_bios_mask.attr,
  771. &dev_attr_hotkey_all_mask.attr,
  772. &dev_attr_hotkey_recommended_mask.attr,
  773. };
  774. static int __init hotkey_init(struct ibm_init_struct *iibm)
  775. {
  776. static u16 ibm_keycode_map[] __initdata = {
  777. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  778. KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
  779. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  780. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  781. /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
  782. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  783. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  784. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  785. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  786. /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
  787. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  788. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  789. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  790. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  791. KEY_RESERVED, /* 0x14: VOLUME UP */
  792. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  793. KEY_RESERVED, /* 0x16: MUTE */
  794. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  795. /* (assignments unknown, please report if found) */
  796. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  797. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  798. };
  799. static u16 lenovo_keycode_map[] __initdata = {
  800. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  801. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  802. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  803. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  804. /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
  805. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  806. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  807. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  808. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  809. /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
  810. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  811. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  812. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  813. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  814. KEY_RESERVED, /* 0x14: VOLUME UP */
  815. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  816. KEY_RESERVED, /* 0x16: MUTE */
  817. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  818. /* (assignments unknown, please report if found) */
  819. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  820. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  821. };
  822. #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
  823. #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
  824. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
  825. int res, i;
  826. int status;
  827. int hkeyv;
  828. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  829. BUG_ON(!tpacpi_inputdev);
  830. IBM_ACPIHANDLE_INIT(hkey);
  831. mutex_init(&hotkey_mutex);
  832. /* hotkey not supported on 570 */
  833. tp_features.hotkey = hkey_handle != NULL;
  834. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  835. str_supported(tp_features.hotkey));
  836. if (tp_features.hotkey) {
  837. hotkey_dev_attributes = create_attr_set(8, NULL);
  838. if (!hotkey_dev_attributes)
  839. return -ENOMEM;
  840. res = add_many_to_attr_set(hotkey_dev_attributes,
  841. hotkey_attributes,
  842. ARRAY_SIZE(hotkey_attributes));
  843. if (res)
  844. return res;
  845. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  846. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  847. for HKEY interface version 0x100 */
  848. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  849. if ((hkeyv >> 8) != 1) {
  850. printk(IBM_ERR "unknown version of the "
  851. "HKEY interface: 0x%x\n", hkeyv);
  852. printk(IBM_ERR "please report this to %s\n",
  853. IBM_MAIL);
  854. } else {
  855. /*
  856. * MHKV 0x100 in A31, R40, R40e,
  857. * T4x, X31, and later
  858. * */
  859. tp_features.hotkey_mask = 1;
  860. }
  861. }
  862. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  863. str_supported(tp_features.hotkey_mask));
  864. if (tp_features.hotkey_mask) {
  865. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  866. "MHKA", "qd")) {
  867. printk(IBM_ERR
  868. "missing MHKA handler, "
  869. "please report this to %s\n",
  870. IBM_MAIL);
  871. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  872. }
  873. }
  874. res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
  875. if (!res && tp_features.hotkey_mask) {
  876. res = add_many_to_attr_set(hotkey_dev_attributes,
  877. hotkey_mask_attributes,
  878. ARRAY_SIZE(hotkey_mask_attributes));
  879. }
  880. /* Not all thinkpads have a hardware radio switch */
  881. if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  882. tp_features.hotkey_wlsw = 1;
  883. printk(IBM_INFO
  884. "radio switch found; radios are %s\n",
  885. enabled(status, 0));
  886. res = add_to_attr_set(hotkey_dev_attributes,
  887. &dev_attr_hotkey_radio_sw.attr);
  888. }
  889. if (!res)
  890. res = register_attr_set_with_sysfs(
  891. hotkey_dev_attributes,
  892. &tpacpi_pdev->dev.kobj);
  893. if (res)
  894. return res;
  895. /* Set up key map */
  896. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  897. GFP_KERNEL);
  898. if (!hotkey_keycode_map) {
  899. printk(IBM_ERR "failed to allocate memory for key map\n");
  900. return -ENOMEM;
  901. }
  902. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  903. dbg_printk(TPACPI_DBG_INIT,
  904. "using Lenovo default hot key map\n");
  905. memcpy(hotkey_keycode_map, &lenovo_keycode_map,
  906. TPACPI_HOTKEY_MAP_SIZE);
  907. } else {
  908. dbg_printk(TPACPI_DBG_INIT,
  909. "using IBM default hot key map\n");
  910. memcpy(hotkey_keycode_map, &ibm_keycode_map,
  911. TPACPI_HOTKEY_MAP_SIZE);
  912. }
  913. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  914. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  915. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  916. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  917. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  918. tpacpi_inputdev->keycode = hotkey_keycode_map;
  919. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  920. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  921. set_bit(hotkey_keycode_map[i],
  922. tpacpi_inputdev->keybit);
  923. } else {
  924. if (i < sizeof(hotkey_reserved_mask)*8)
  925. hotkey_reserved_mask |= 1 << i;
  926. }
  927. }
  928. if (tp_features.hotkey_wlsw) {
  929. set_bit(EV_SW, tpacpi_inputdev->evbit);
  930. set_bit(SW_RADIO, tpacpi_inputdev->swbit);
  931. }
  932. dbg_printk(TPACPI_DBG_INIT,
  933. "enabling hot key handling\n");
  934. res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
  935. | hotkey_orig_mask);
  936. if (res)
  937. return res;
  938. dbg_printk(TPACPI_DBG_INIT,
  939. "legacy hot key reporting over procfs %s\n",
  940. (hotkey_report_mode < 2) ?
  941. "enabled" : "disabled");
  942. }
  943. return (tp_features.hotkey)? 0 : 1;
  944. }
  945. static void hotkey_exit(void)
  946. {
  947. int res;
  948. if (tp_features.hotkey) {
  949. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  950. res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  951. if (res)
  952. printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
  953. }
  954. if (hotkey_dev_attributes) {
  955. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  956. hotkey_dev_attributes = NULL;
  957. }
  958. }
  959. static void tpacpi_input_send_key(unsigned int scancode,
  960. unsigned int keycode)
  961. {
  962. if (keycode != KEY_RESERVED) {
  963. mutex_lock(&tpacpi_inputdev_send_mutex);
  964. input_report_key(tpacpi_inputdev, keycode, 1);
  965. if (keycode == KEY_UNKNOWN)
  966. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  967. scancode);
  968. input_sync(tpacpi_inputdev);
  969. input_report_key(tpacpi_inputdev, keycode, 0);
  970. if (keycode == KEY_UNKNOWN)
  971. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  972. scancode);
  973. input_sync(tpacpi_inputdev);
  974. mutex_unlock(&tpacpi_inputdev_send_mutex);
  975. }
  976. }
  977. static void tpacpi_input_send_radiosw(void)
  978. {
  979. int wlsw;
  980. mutex_lock(&tpacpi_inputdev_send_mutex);
  981. if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
  982. input_report_switch(tpacpi_inputdev,
  983. SW_RADIO, !!wlsw);
  984. input_sync(tpacpi_inputdev);
  985. }
  986. mutex_unlock(&tpacpi_inputdev_send_mutex);
  987. }
  988. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  989. {
  990. u32 hkey;
  991. unsigned int keycode, scancode;
  992. int send_acpi_ev;
  993. int ignore_acpi_ev;
  994. if (event != 0x80) {
  995. printk(IBM_ERR "unknown HKEY notification event %d\n", event);
  996. /* forward it to userspace, maybe it knows how to handle it */
  997. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  998. ibm->acpi->device->dev.bus_id,
  999. event, 0);
  1000. return;
  1001. }
  1002. while (1) {
  1003. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  1004. printk(IBM_ERR "failed to retrieve HKEY event\n");
  1005. return;
  1006. }
  1007. if (hkey == 0) {
  1008. /* queue empty */
  1009. return;
  1010. }
  1011. send_acpi_ev = 0;
  1012. ignore_acpi_ev = 0;
  1013. switch (hkey >> 12) {
  1014. case 1:
  1015. /* 0x1000-0x1FFF: key presses */
  1016. scancode = hkey & 0xfff;
  1017. if (scancode > 0 && scancode < 0x21) {
  1018. scancode--;
  1019. keycode = hotkey_keycode_map[scancode];
  1020. tpacpi_input_send_key(scancode, keycode);
  1021. } else {
  1022. printk(IBM_ERR
  1023. "hotkey 0x%04x out of range for keyboard map\n",
  1024. hkey);
  1025. send_acpi_ev = 1;
  1026. }
  1027. break;
  1028. case 5:
  1029. /* 0x5000-0x5FFF: LID */
  1030. /* we don't handle it through this path, just
  1031. * eat up known LID events */
  1032. if (hkey != 0x5001 && hkey != 0x5002) {
  1033. printk(IBM_ERR
  1034. "unknown LID-related HKEY event: 0x%04x\n",
  1035. hkey);
  1036. send_acpi_ev = 1;
  1037. } else {
  1038. ignore_acpi_ev = 1;
  1039. }
  1040. break;
  1041. case 7:
  1042. /* 0x7000-0x7FFF: misc */
  1043. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  1044. tpacpi_input_send_radiosw();
  1045. break;
  1046. }
  1047. /* fallthrough to default */
  1048. default:
  1049. /* case 2: dock-related */
  1050. /* 0x2305 - T43 waking up due to bay lever eject while aslept */
  1051. /* case 3: ultra-bay related. maybe bay in dock? */
  1052. /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
  1053. printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
  1054. send_acpi_ev = 1;
  1055. }
  1056. /* Legacy events */
  1057. if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
  1058. acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
  1059. }
  1060. /* netlink events */
  1061. if (!ignore_acpi_ev && send_acpi_ev) {
  1062. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  1063. ibm->acpi->device->dev.bus_id,
  1064. event, hkey);
  1065. }
  1066. }
  1067. }
  1068. static void hotkey_resume(void)
  1069. {
  1070. tpacpi_input_send_radiosw();
  1071. }
  1072. /*
  1073. * Call with hotkey_mutex held
  1074. */
  1075. static int hotkey_get(int *status, u32 *mask)
  1076. {
  1077. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  1078. return -EIO;
  1079. if (tp_features.hotkey_mask)
  1080. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  1081. return -EIO;
  1082. return 0;
  1083. }
  1084. /*
  1085. * Call with hotkey_mutex held
  1086. */
  1087. static int hotkey_set(int status, u32 mask)
  1088. {
  1089. int i;
  1090. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  1091. return -EIO;
  1092. if (tp_features.hotkey_mask)
  1093. for (i = 0; i < 32; i++) {
  1094. int bit = ((1 << i) & mask) != 0;
  1095. if (!acpi_evalf(hkey_handle,
  1096. NULL, "MHKM", "vdd", i + 1, bit))
  1097. return -EIO;
  1098. }
  1099. return 0;
  1100. }
  1101. /* procfs -------------------------------------------------------------- */
  1102. static int hotkey_read(char *p)
  1103. {
  1104. int res, status;
  1105. u32 mask;
  1106. int len = 0;
  1107. if (!tp_features.hotkey) {
  1108. len += sprintf(p + len, "status:\t\tnot supported\n");
  1109. return len;
  1110. }
  1111. if (mutex_lock_interruptible(&hotkey_mutex))
  1112. return -ERESTARTSYS;
  1113. res = hotkey_get(&status, &mask);
  1114. mutex_unlock(&hotkey_mutex);
  1115. if (res)
  1116. return res;
  1117. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  1118. if (tp_features.hotkey_mask) {
  1119. len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
  1120. len += sprintf(p + len,
  1121. "commands:\tenable, disable, reset, <mask>\n");
  1122. } else {
  1123. len += sprintf(p + len, "mask:\t\tnot supported\n");
  1124. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  1125. }
  1126. return len;
  1127. }
  1128. static int hotkey_write(char *buf)
  1129. {
  1130. int res, status;
  1131. u32 mask;
  1132. char *cmd;
  1133. int do_cmd = 0;
  1134. if (!tp_features.hotkey)
  1135. return -ENODEV;
  1136. if (mutex_lock_interruptible(&hotkey_mutex))
  1137. return -ERESTARTSYS;
  1138. res = hotkey_get(&status, &mask);
  1139. if (res)
  1140. goto errexit;
  1141. res = 0;
  1142. while ((cmd = next_cmd(&buf))) {
  1143. if (strlencmp(cmd, "enable") == 0) {
  1144. status = 1;
  1145. } else if (strlencmp(cmd, "disable") == 0) {
  1146. status = 0;
  1147. } else if (strlencmp(cmd, "reset") == 0) {
  1148. status = hotkey_orig_status;
  1149. mask = hotkey_orig_mask;
  1150. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  1151. /* mask set */
  1152. } else if (sscanf(cmd, "%x", &mask) == 1) {
  1153. /* mask set */
  1154. } else {
  1155. res = -EINVAL;
  1156. goto errexit;
  1157. }
  1158. do_cmd = 1;
  1159. }
  1160. if (do_cmd)
  1161. res = hotkey_set(status, mask);
  1162. errexit:
  1163. mutex_unlock(&hotkey_mutex);
  1164. return res;
  1165. }
  1166. static const struct acpi_device_id ibm_htk_device_ids[] = {
  1167. {IBM_HKEY_HID, 0},
  1168. {"", 0},
  1169. };
  1170. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  1171. .hid = ibm_htk_device_ids,
  1172. .notify = hotkey_notify,
  1173. .handle = &hkey_handle,
  1174. .type = ACPI_DEVICE_NOTIFY,
  1175. };
  1176. static struct ibm_struct hotkey_driver_data = {
  1177. .name = "hotkey",
  1178. .read = hotkey_read,
  1179. .write = hotkey_write,
  1180. .exit = hotkey_exit,
  1181. .resume = hotkey_resume,
  1182. .acpi = &ibm_hotkey_acpidriver,
  1183. };
  1184. /*************************************************************************
  1185. * Bluetooth subdriver
  1186. */
  1187. /* sysfs bluetooth enable ---------------------------------------------- */
  1188. static ssize_t bluetooth_enable_show(struct device *dev,
  1189. struct device_attribute *attr,
  1190. char *buf)
  1191. {
  1192. int status;
  1193. status = bluetooth_get_radiosw();
  1194. if (status < 0)
  1195. return status;
  1196. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1197. }
  1198. static ssize_t bluetooth_enable_store(struct device *dev,
  1199. struct device_attribute *attr,
  1200. const char *buf, size_t count)
  1201. {
  1202. unsigned long t;
  1203. int res;
  1204. if (parse_strtoul(buf, 1, &t))
  1205. return -EINVAL;
  1206. res = bluetooth_set_radiosw(t);
  1207. return (res) ? res : count;
  1208. }
  1209. static struct device_attribute dev_attr_bluetooth_enable =
  1210. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  1211. bluetooth_enable_show, bluetooth_enable_store);
  1212. /* --------------------------------------------------------------------- */
  1213. static struct attribute *bluetooth_attributes[] = {
  1214. &dev_attr_bluetooth_enable.attr,
  1215. NULL
  1216. };
  1217. static const struct attribute_group bluetooth_attr_group = {
  1218. .attrs = bluetooth_attributes,
  1219. };
  1220. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  1221. {
  1222. int res;
  1223. int status = 0;
  1224. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  1225. IBM_ACPIHANDLE_INIT(hkey);
  1226. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1227. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  1228. tp_features.bluetooth = hkey_handle &&
  1229. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  1230. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  1231. str_supported(tp_features.bluetooth),
  1232. status);
  1233. if (tp_features.bluetooth) {
  1234. if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  1235. /* no bluetooth hardware present in system */
  1236. tp_features.bluetooth = 0;
  1237. dbg_printk(TPACPI_DBG_INIT,
  1238. "bluetooth hardware not installed\n");
  1239. } else {
  1240. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1241. &bluetooth_attr_group);
  1242. if (res)
  1243. return res;
  1244. }
  1245. }
  1246. return (tp_features.bluetooth)? 0 : 1;
  1247. }
  1248. static void bluetooth_exit(void)
  1249. {
  1250. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1251. &bluetooth_attr_group);
  1252. }
  1253. static int bluetooth_get_radiosw(void)
  1254. {
  1255. int status;
  1256. if (!tp_features.bluetooth)
  1257. return -ENODEV;
  1258. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1259. return -EIO;
  1260. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  1261. }
  1262. static int bluetooth_set_radiosw(int radio_on)
  1263. {
  1264. int status;
  1265. if (!tp_features.bluetooth)
  1266. return -ENODEV;
  1267. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1268. return -EIO;
  1269. if (radio_on)
  1270. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  1271. else
  1272. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  1273. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  1274. return -EIO;
  1275. return 0;
  1276. }
  1277. /* procfs -------------------------------------------------------------- */
  1278. static int bluetooth_read(char *p)
  1279. {
  1280. int len = 0;
  1281. int status = bluetooth_get_radiosw();
  1282. if (!tp_features.bluetooth)
  1283. len += sprintf(p + len, "status:\t\tnot supported\n");
  1284. else {
  1285. len += sprintf(p + len, "status:\t\t%s\n",
  1286. (status)? "enabled" : "disabled");
  1287. len += sprintf(p + len, "commands:\tenable, disable\n");
  1288. }
  1289. return len;
  1290. }
  1291. static int bluetooth_write(char *buf)
  1292. {
  1293. char *cmd;
  1294. if (!tp_features.bluetooth)
  1295. return -ENODEV;
  1296. while ((cmd = next_cmd(&buf))) {
  1297. if (strlencmp(cmd, "enable") == 0) {
  1298. bluetooth_set_radiosw(1);
  1299. } else if (strlencmp(cmd, "disable") == 0) {
  1300. bluetooth_set_radiosw(0);
  1301. } else
  1302. return -EINVAL;
  1303. }
  1304. return 0;
  1305. }
  1306. static struct ibm_struct bluetooth_driver_data = {
  1307. .name = "bluetooth",
  1308. .read = bluetooth_read,
  1309. .write = bluetooth_write,
  1310. .exit = bluetooth_exit,
  1311. };
  1312. /*************************************************************************
  1313. * Wan subdriver
  1314. */
  1315. /* sysfs wan enable ---------------------------------------------------- */
  1316. static ssize_t wan_enable_show(struct device *dev,
  1317. struct device_attribute *attr,
  1318. char *buf)
  1319. {
  1320. int status;
  1321. status = wan_get_radiosw();
  1322. if (status < 0)
  1323. return status;
  1324. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1325. }
  1326. static ssize_t wan_enable_store(struct device *dev,
  1327. struct device_attribute *attr,
  1328. const char *buf, size_t count)
  1329. {
  1330. unsigned long t;
  1331. int res;
  1332. if (parse_strtoul(buf, 1, &t))
  1333. return -EINVAL;
  1334. res = wan_set_radiosw(t);
  1335. return (res) ? res : count;
  1336. }
  1337. static struct device_attribute dev_attr_wan_enable =
  1338. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  1339. wan_enable_show, wan_enable_store);
  1340. /* --------------------------------------------------------------------- */
  1341. static struct attribute *wan_attributes[] = {
  1342. &dev_attr_wan_enable.attr,
  1343. NULL
  1344. };
  1345. static const struct attribute_group wan_attr_group = {
  1346. .attrs = wan_attributes,
  1347. };
  1348. static int __init wan_init(struct ibm_init_struct *iibm)
  1349. {
  1350. int res;
  1351. int status = 0;
  1352. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  1353. IBM_ACPIHANDLE_INIT(hkey);
  1354. tp_features.wan = hkey_handle &&
  1355. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  1356. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  1357. str_supported(tp_features.wan),
  1358. status);
  1359. if (tp_features.wan) {
  1360. if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
  1361. /* no wan hardware present in system */
  1362. tp_features.wan = 0;
  1363. dbg_printk(TPACPI_DBG_INIT,
  1364. "wan hardware not installed\n");
  1365. } else {
  1366. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1367. &wan_attr_group);
  1368. if (res)
  1369. return res;
  1370. }
  1371. }
  1372. return (tp_features.wan)? 0 : 1;
  1373. }
  1374. static void wan_exit(void)
  1375. {
  1376. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1377. &wan_attr_group);
  1378. }
  1379. static int wan_get_radiosw(void)
  1380. {
  1381. int status;
  1382. if (!tp_features.wan)
  1383. return -ENODEV;
  1384. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  1385. return -EIO;
  1386. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  1387. }
  1388. static int wan_set_radiosw(int radio_on)
  1389. {
  1390. int status;
  1391. if (!tp_features.wan)
  1392. return -ENODEV;
  1393. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  1394. return -EIO;
  1395. if (radio_on)
  1396. status |= TP_ACPI_WANCARD_RADIOSSW;
  1397. else
  1398. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  1399. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  1400. return -EIO;
  1401. return 0;
  1402. }
  1403. /* procfs -------------------------------------------------------------- */
  1404. static int wan_read(char *p)
  1405. {
  1406. int len = 0;
  1407. int status = wan_get_radiosw();
  1408. if (!tp_features.wan)
  1409. len += sprintf(p + len, "status:\t\tnot supported\n");
  1410. else {
  1411. len += sprintf(p + len, "status:\t\t%s\n",
  1412. (status)? "enabled" : "disabled");
  1413. len += sprintf(p + len, "commands:\tenable, disable\n");
  1414. }
  1415. return len;
  1416. }
  1417. static int wan_write(char *buf)
  1418. {
  1419. char *cmd;
  1420. if (!tp_features.wan)
  1421. return -ENODEV;
  1422. while ((cmd = next_cmd(&buf))) {
  1423. if (strlencmp(cmd, "enable") == 0) {
  1424. wan_set_radiosw(1);
  1425. } else if (strlencmp(cmd, "disable") == 0) {
  1426. wan_set_radiosw(0);
  1427. } else
  1428. return -EINVAL;
  1429. }
  1430. return 0;
  1431. }
  1432. static struct ibm_struct wan_driver_data = {
  1433. .name = "wan",
  1434. .read = wan_read,
  1435. .write = wan_write,
  1436. .exit = wan_exit,
  1437. .flags.experimental = 1,
  1438. };
  1439. /*************************************************************************
  1440. * Video subdriver
  1441. */
  1442. static enum video_access_mode video_supported;
  1443. static int video_orig_autosw;
  1444. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  1445. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  1446. "\\_SB.PCI0.VID0", /* 770e */
  1447. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  1448. "\\_SB.PCI0.AGP.VID", /* all others */
  1449. ); /* R30, R31 */
  1450. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  1451. static int __init video_init(struct ibm_init_struct *iibm)
  1452. {
  1453. int ivga;
  1454. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  1455. IBM_ACPIHANDLE_INIT(vid);
  1456. IBM_ACPIHANDLE_INIT(vid2);
  1457. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  1458. /* G41, assume IVGA doesn't change */
  1459. vid_handle = vid2_handle;
  1460. if (!vid_handle)
  1461. /* video switching not supported on R30, R31 */
  1462. video_supported = TPACPI_VIDEO_NONE;
  1463. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  1464. /* 570 */
  1465. video_supported = TPACPI_VIDEO_570;
  1466. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  1467. /* 600e/x, 770e, 770x */
  1468. video_supported = TPACPI_VIDEO_770;
  1469. else
  1470. /* all others */
  1471. video_supported = TPACPI_VIDEO_NEW;
  1472. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  1473. str_supported(video_supported != TPACPI_VIDEO_NONE),
  1474. video_supported);
  1475. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  1476. }
  1477. static void video_exit(void)
  1478. {
  1479. dbg_printk(TPACPI_DBG_EXIT,
  1480. "restoring original video autoswitch mode\n");
  1481. if (video_autosw_set(video_orig_autosw))
  1482. printk(IBM_ERR "error while trying to restore original "
  1483. "video autoswitch mode\n");
  1484. }
  1485. static int video_outputsw_get(void)
  1486. {
  1487. int status = 0;
  1488. int i;
  1489. switch (video_supported) {
  1490. case TPACPI_VIDEO_570:
  1491. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  1492. TP_ACPI_VIDEO_570_PHSCMD))
  1493. return -EIO;
  1494. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  1495. break;
  1496. case TPACPI_VIDEO_770:
  1497. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  1498. return -EIO;
  1499. if (i)
  1500. status |= TP_ACPI_VIDEO_S_LCD;
  1501. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  1502. return -EIO;
  1503. if (i)
  1504. status |= TP_ACPI_VIDEO_S_CRT;
  1505. break;
  1506. case TPACPI_VIDEO_NEW:
  1507. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  1508. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  1509. return -EIO;
  1510. if (i)
  1511. status |= TP_ACPI_VIDEO_S_CRT;
  1512. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  1513. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  1514. return -EIO;
  1515. if (i)
  1516. status |= TP_ACPI_VIDEO_S_LCD;
  1517. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  1518. return -EIO;
  1519. if (i)
  1520. status |= TP_ACPI_VIDEO_S_DVI;
  1521. break;
  1522. default:
  1523. return -ENOSYS;
  1524. }
  1525. return status;
  1526. }
  1527. static int video_outputsw_set(int status)
  1528. {
  1529. int autosw;
  1530. int res = 0;
  1531. switch (video_supported) {
  1532. case TPACPI_VIDEO_570:
  1533. res = acpi_evalf(NULL, NULL,
  1534. "\\_SB.PHS2", "vdd",
  1535. TP_ACPI_VIDEO_570_PHS2CMD,
  1536. status | TP_ACPI_VIDEO_570_PHS2SET);
  1537. break;
  1538. case TPACPI_VIDEO_770:
  1539. autosw = video_autosw_get();
  1540. if (autosw < 0)
  1541. return autosw;
  1542. res = video_autosw_set(1);
  1543. if (res)
  1544. return res;
  1545. res = acpi_evalf(vid_handle, NULL,
  1546. "ASWT", "vdd", status * 0x100, 0);
  1547. if (!autosw && video_autosw_set(autosw)) {
  1548. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1549. return -EIO;
  1550. }
  1551. break;
  1552. case TPACPI_VIDEO_NEW:
  1553. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  1554. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  1555. break;
  1556. default:
  1557. return -ENOSYS;
  1558. }
  1559. return (res)? 0 : -EIO;
  1560. }
  1561. static int video_autosw_get(void)
  1562. {
  1563. int autosw = 0;
  1564. switch (video_supported) {
  1565. case TPACPI_VIDEO_570:
  1566. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  1567. return -EIO;
  1568. break;
  1569. case TPACPI_VIDEO_770:
  1570. case TPACPI_VIDEO_NEW:
  1571. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  1572. return -EIO;
  1573. break;
  1574. default:
  1575. return -ENOSYS;
  1576. }
  1577. return autosw & 1;
  1578. }
  1579. static int video_autosw_set(int enable)
  1580. {
  1581. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  1582. return -EIO;
  1583. return 0;
  1584. }
  1585. static int video_outputsw_cycle(void)
  1586. {
  1587. int autosw = video_autosw_get();
  1588. int res;
  1589. if (autosw < 0)
  1590. return autosw;
  1591. switch (video_supported) {
  1592. case TPACPI_VIDEO_570:
  1593. res = video_autosw_set(1);
  1594. if (res)
  1595. return res;
  1596. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  1597. break;
  1598. case TPACPI_VIDEO_770:
  1599. case TPACPI_VIDEO_NEW:
  1600. res = video_autosw_set(1);
  1601. if (res)
  1602. return res;
  1603. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  1604. break;
  1605. default:
  1606. return -ENOSYS;
  1607. }
  1608. if (!autosw && video_autosw_set(autosw)) {
  1609. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1610. return -EIO;
  1611. }
  1612. return (res)? 0 : -EIO;
  1613. }
  1614. static int video_expand_toggle(void)
  1615. {
  1616. switch (video_supported) {
  1617. case TPACPI_VIDEO_570:
  1618. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  1619. 0 : -EIO;
  1620. case TPACPI_VIDEO_770:
  1621. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  1622. 0 : -EIO;
  1623. case TPACPI_VIDEO_NEW:
  1624. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  1625. 0 : -EIO;
  1626. default:
  1627. return -ENOSYS;
  1628. }
  1629. /* not reached */
  1630. }
  1631. static int video_read(char *p)
  1632. {
  1633. int status, autosw;
  1634. int len = 0;
  1635. if (video_supported == TPACPI_VIDEO_NONE) {
  1636. len += sprintf(p + len, "status:\t\tnot supported\n");
  1637. return len;
  1638. }
  1639. status = video_outputsw_get();
  1640. if (status < 0)
  1641. return status;
  1642. autosw = video_autosw_get();
  1643. if (autosw < 0)
  1644. return autosw;
  1645. len += sprintf(p + len, "status:\t\tsupported\n");
  1646. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  1647. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  1648. if (video_supported == TPACPI_VIDEO_NEW)
  1649. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  1650. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  1651. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  1652. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  1653. if (video_supported == TPACPI_VIDEO_NEW)
  1654. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  1655. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  1656. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  1657. return len;
  1658. }
  1659. static int video_write(char *buf)
  1660. {
  1661. char *cmd;
  1662. int enable, disable, status;
  1663. int res;
  1664. if (video_supported == TPACPI_VIDEO_NONE)
  1665. return -ENODEV;
  1666. enable = 0;
  1667. disable = 0;
  1668. while ((cmd = next_cmd(&buf))) {
  1669. if (strlencmp(cmd, "lcd_enable") == 0) {
  1670. enable |= TP_ACPI_VIDEO_S_LCD;
  1671. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  1672. disable |= TP_ACPI_VIDEO_S_LCD;
  1673. } else if (strlencmp(cmd, "crt_enable") == 0) {
  1674. enable |= TP_ACPI_VIDEO_S_CRT;
  1675. } else if (strlencmp(cmd, "crt_disable") == 0) {
  1676. disable |= TP_ACPI_VIDEO_S_CRT;
  1677. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1678. strlencmp(cmd, "dvi_enable") == 0) {
  1679. enable |= TP_ACPI_VIDEO_S_DVI;
  1680. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1681. strlencmp(cmd, "dvi_disable") == 0) {
  1682. disable |= TP_ACPI_VIDEO_S_DVI;
  1683. } else if (strlencmp(cmd, "auto_enable") == 0) {
  1684. res = video_autosw_set(1);
  1685. if (res)
  1686. return res;
  1687. } else if (strlencmp(cmd, "auto_disable") == 0) {
  1688. res = video_autosw_set(0);
  1689. if (res)
  1690. return res;
  1691. } else if (strlencmp(cmd, "video_switch") == 0) {
  1692. res = video_outputsw_cycle();
  1693. if (res)
  1694. return res;
  1695. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  1696. res = video_expand_toggle();
  1697. if (res)
  1698. return res;
  1699. } else
  1700. return -EINVAL;
  1701. }
  1702. if (enable || disable) {
  1703. status = video_outputsw_get();
  1704. if (status < 0)
  1705. return status;
  1706. res = video_outputsw_set((status & ~disable) | enable);
  1707. if (res)
  1708. return res;
  1709. }
  1710. return 0;
  1711. }
  1712. static struct ibm_struct video_driver_data = {
  1713. .name = "video",
  1714. .read = video_read,
  1715. .write = video_write,
  1716. .exit = video_exit,
  1717. };
  1718. /*************************************************************************
  1719. * Light (thinklight) subdriver
  1720. */
  1721. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  1722. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  1723. static int __init light_init(struct ibm_init_struct *iibm)
  1724. {
  1725. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  1726. IBM_ACPIHANDLE_INIT(ledb);
  1727. IBM_ACPIHANDLE_INIT(lght);
  1728. IBM_ACPIHANDLE_INIT(cmos);
  1729. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  1730. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  1731. if (tp_features.light)
  1732. /* light status not supported on
  1733. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  1734. tp_features.light_status =
  1735. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  1736. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  1737. str_supported(tp_features.light));
  1738. return (tp_features.light)? 0 : 1;
  1739. }
  1740. static int light_read(char *p)
  1741. {
  1742. int len = 0;
  1743. int status = 0;
  1744. if (!tp_features.light) {
  1745. len += sprintf(p + len, "status:\t\tnot supported\n");
  1746. } else if (!tp_features.light_status) {
  1747. len += sprintf(p + len, "status:\t\tunknown\n");
  1748. len += sprintf(p + len, "commands:\ton, off\n");
  1749. } else {
  1750. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  1751. return -EIO;
  1752. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  1753. len += sprintf(p + len, "commands:\ton, off\n");
  1754. }
  1755. return len;
  1756. }
  1757. static int light_write(char *buf)
  1758. {
  1759. int cmos_cmd, lght_cmd;
  1760. char *cmd;
  1761. int success;
  1762. if (!tp_features.light)
  1763. return -ENODEV;
  1764. while ((cmd = next_cmd(&buf))) {
  1765. if (strlencmp(cmd, "on") == 0) {
  1766. cmos_cmd = 0x0c;
  1767. lght_cmd = 1;
  1768. } else if (strlencmp(cmd, "off") == 0) {
  1769. cmos_cmd = 0x0d;
  1770. lght_cmd = 0;
  1771. } else
  1772. return -EINVAL;
  1773. success = cmos_handle ?
  1774. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  1775. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  1776. if (!success)
  1777. return -EIO;
  1778. }
  1779. return 0;
  1780. }
  1781. static struct ibm_struct light_driver_data = {
  1782. .name = "light",
  1783. .read = light_read,
  1784. .write = light_write,
  1785. };
  1786. /*************************************************************************
  1787. * Dock subdriver
  1788. */
  1789. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  1790. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  1791. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  1792. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  1793. "\\_SB.PCI.ISA.SLCE", /* 570 */
  1794. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  1795. /* don't list other alternatives as we install a notify handler on the 570 */
  1796. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  1797. static const struct acpi_device_id ibm_pci_device_ids[] = {
  1798. {PCI_ROOT_HID_STRING, 0},
  1799. {"", 0},
  1800. };
  1801. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  1802. {
  1803. .notify = dock_notify,
  1804. .handle = &dock_handle,
  1805. .type = ACPI_SYSTEM_NOTIFY,
  1806. },
  1807. {
  1808. /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
  1809. * We just use it to get notifications of dock hotplug
  1810. * in very old thinkpads */
  1811. .hid = ibm_pci_device_ids,
  1812. .notify = dock_notify,
  1813. .handle = &pci_handle,
  1814. .type = ACPI_SYSTEM_NOTIFY,
  1815. },
  1816. };
  1817. static struct ibm_struct dock_driver_data[2] = {
  1818. {
  1819. .name = "dock",
  1820. .read = dock_read,
  1821. .write = dock_write,
  1822. .acpi = &ibm_dock_acpidriver[0],
  1823. },
  1824. {
  1825. .name = "dock",
  1826. .acpi = &ibm_dock_acpidriver[1],
  1827. },
  1828. };
  1829. #define dock_docked() (_sta(dock_handle) & 1)
  1830. static int __init dock_init(struct ibm_init_struct *iibm)
  1831. {
  1832. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  1833. IBM_ACPIHANDLE_INIT(dock);
  1834. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  1835. str_supported(dock_handle != NULL));
  1836. return (dock_handle)? 0 : 1;
  1837. }
  1838. static int __init dock_init2(struct ibm_init_struct *iibm)
  1839. {
  1840. int dock2_needed;
  1841. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  1842. if (dock_driver_data[0].flags.acpi_driver_registered &&
  1843. dock_driver_data[0].flags.acpi_notify_installed) {
  1844. IBM_ACPIHANDLE_INIT(pci);
  1845. dock2_needed = (pci_handle != NULL);
  1846. vdbg_printk(TPACPI_DBG_INIT,
  1847. "dock PCI handler for the TP 570 is %s\n",
  1848. str_supported(dock2_needed));
  1849. } else {
  1850. vdbg_printk(TPACPI_DBG_INIT,
  1851. "dock subdriver part 2 not required\n");
  1852. dock2_needed = 0;
  1853. }
  1854. return (dock2_needed)? 0 : 1;
  1855. }
  1856. static void dock_notify(struct ibm_struct *ibm, u32 event)
  1857. {
  1858. int docked = dock_docked();
  1859. int pci = ibm->acpi->hid && ibm->acpi->device &&
  1860. acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
  1861. int data;
  1862. if (event == 1 && !pci) /* 570 */
  1863. data = 1; /* button */
  1864. else if (event == 1 && pci) /* 570 */
  1865. data = 3; /* dock */
  1866. else if (event == 3 && docked)
  1867. data = 1; /* button */
  1868. else if (event == 3 && !docked)
  1869. data = 2; /* undock */
  1870. else if (event == 0 && docked)
  1871. data = 3; /* dock */
  1872. else {
  1873. printk(IBM_ERR "unknown dock event %d, status %d\n",
  1874. event, _sta(dock_handle));
  1875. data = 0; /* unknown */
  1876. }
  1877. acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
  1878. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  1879. ibm->acpi->device->dev.bus_id,
  1880. event, data);
  1881. }
  1882. static int dock_read(char *p)
  1883. {
  1884. int len = 0;
  1885. int docked = dock_docked();
  1886. if (!dock_handle)
  1887. len += sprintf(p + len, "status:\t\tnot supported\n");
  1888. else if (!docked)
  1889. len += sprintf(p + len, "status:\t\tundocked\n");
  1890. else {
  1891. len += sprintf(p + len, "status:\t\tdocked\n");
  1892. len += sprintf(p + len, "commands:\tdock, undock\n");
  1893. }
  1894. return len;
  1895. }
  1896. static int dock_write(char *buf)
  1897. {
  1898. char *cmd;
  1899. if (!dock_docked())
  1900. return -ENODEV;
  1901. while ((cmd = next_cmd(&buf))) {
  1902. if (strlencmp(cmd, "undock") == 0) {
  1903. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  1904. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  1905. return -EIO;
  1906. } else if (strlencmp(cmd, "dock") == 0) {
  1907. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  1908. return -EIO;
  1909. } else
  1910. return -EINVAL;
  1911. }
  1912. return 0;
  1913. }
  1914. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1915. /*************************************************************************
  1916. * Bay subdriver
  1917. */
  1918. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1919. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1920. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1921. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1922. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1923. ); /* A21e, R30, R31 */
  1924. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1925. "_EJ0", /* all others */
  1926. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1927. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1928. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1929. ); /* all others */
  1930. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1931. "_EJ0", /* 770x */
  1932. ); /* all others */
  1933. static int __init bay_init(struct ibm_init_struct *iibm)
  1934. {
  1935. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1936. IBM_ACPIHANDLE_INIT(bay);
  1937. if (bay_handle)
  1938. IBM_ACPIHANDLE_INIT(bay_ej);
  1939. IBM_ACPIHANDLE_INIT(bay2);
  1940. if (bay2_handle)
  1941. IBM_ACPIHANDLE_INIT(bay2_ej);
  1942. tp_features.bay_status = bay_handle &&
  1943. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1944. tp_features.bay_status2 = bay2_handle &&
  1945. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1946. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  1947. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1948. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  1949. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1950. vdbg_printk(TPACPI_DBG_INIT,
  1951. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1952. str_supported(tp_features.bay_status),
  1953. str_supported(tp_features.bay_eject),
  1954. str_supported(tp_features.bay_status2),
  1955. str_supported(tp_features.bay_eject2));
  1956. return (tp_features.bay_status || tp_features.bay_eject ||
  1957. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  1958. }
  1959. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1960. {
  1961. acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
  1962. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  1963. ibm->acpi->device->dev.bus_id,
  1964. event, 0);
  1965. }
  1966. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1967. static int bay_read(char *p)
  1968. {
  1969. int len = 0;
  1970. int occupied = bay_occupied(bay);
  1971. int occupied2 = bay_occupied(bay2);
  1972. int eject, eject2;
  1973. len += sprintf(p + len, "status:\t\t%s\n",
  1974. tp_features.bay_status ?
  1975. (occupied ? "occupied" : "unoccupied") :
  1976. "not supported");
  1977. if (tp_features.bay_status2)
  1978. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1979. "occupied" : "unoccupied");
  1980. eject = tp_features.bay_eject && occupied;
  1981. eject2 = tp_features.bay_eject2 && occupied2;
  1982. if (eject && eject2)
  1983. len += sprintf(p + len, "commands:\teject, eject2\n");
  1984. else if (eject)
  1985. len += sprintf(p + len, "commands:\teject\n");
  1986. else if (eject2)
  1987. len += sprintf(p + len, "commands:\teject2\n");
  1988. return len;
  1989. }
  1990. static int bay_write(char *buf)
  1991. {
  1992. char *cmd;
  1993. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  1994. return -ENODEV;
  1995. while ((cmd = next_cmd(&buf))) {
  1996. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  1997. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1998. return -EIO;
  1999. } else if (tp_features.bay_eject2 &&
  2000. strlencmp(cmd, "eject2") == 0) {
  2001. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  2002. return -EIO;
  2003. } else
  2004. return -EINVAL;
  2005. }
  2006. return 0;
  2007. }
  2008. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  2009. .notify = bay_notify,
  2010. .handle = &bay_handle,
  2011. .type = ACPI_SYSTEM_NOTIFY,
  2012. };
  2013. static struct ibm_struct bay_driver_data = {
  2014. .name = "bay",
  2015. .read = bay_read,
  2016. .write = bay_write,
  2017. .acpi = &ibm_bay_acpidriver,
  2018. };
  2019. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  2020. /*************************************************************************
  2021. * CMOS subdriver
  2022. */
  2023. /* sysfs cmos_command -------------------------------------------------- */
  2024. static ssize_t cmos_command_store(struct device *dev,
  2025. struct device_attribute *attr,
  2026. const char *buf, size_t count)
  2027. {
  2028. unsigned long cmos_cmd;
  2029. int res;
  2030. if (parse_strtoul(buf, 21, &cmos_cmd))
  2031. return -EINVAL;
  2032. res = issue_thinkpad_cmos_command(cmos_cmd);
  2033. return (res)? res : count;
  2034. }
  2035. static struct device_attribute dev_attr_cmos_command =
  2036. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  2037. /* --------------------------------------------------------------------- */
  2038. static int __init cmos_init(struct ibm_init_struct *iibm)
  2039. {
  2040. int res;
  2041. vdbg_printk(TPACPI_DBG_INIT,
  2042. "initializing cmos commands subdriver\n");
  2043. IBM_ACPIHANDLE_INIT(cmos);
  2044. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  2045. str_supported(cmos_handle != NULL));
  2046. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2047. if (res)
  2048. return res;
  2049. return (cmos_handle)? 0 : 1;
  2050. }
  2051. static void cmos_exit(void)
  2052. {
  2053. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2054. }
  2055. static int cmos_read(char *p)
  2056. {
  2057. int len = 0;
  2058. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2059. R30, R31, T20-22, X20-21 */
  2060. if (!cmos_handle)
  2061. len += sprintf(p + len, "status:\t\tnot supported\n");
  2062. else {
  2063. len += sprintf(p + len, "status:\t\tsupported\n");
  2064. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  2065. }
  2066. return len;
  2067. }
  2068. static int cmos_write(char *buf)
  2069. {
  2070. char *cmd;
  2071. int cmos_cmd, res;
  2072. while ((cmd = next_cmd(&buf))) {
  2073. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  2074. cmos_cmd >= 0 && cmos_cmd <= 21) {
  2075. /* cmos_cmd set */
  2076. } else
  2077. return -EINVAL;
  2078. res = issue_thinkpad_cmos_command(cmos_cmd);
  2079. if (res)
  2080. return res;
  2081. }
  2082. return 0;
  2083. }
  2084. static struct ibm_struct cmos_driver_data = {
  2085. .name = "cmos",
  2086. .read = cmos_read,
  2087. .write = cmos_write,
  2088. .exit = cmos_exit,
  2089. };
  2090. /*************************************************************************
  2091. * LED subdriver
  2092. */
  2093. static enum led_access_mode led_supported;
  2094. IBM_HANDLE(led, ec, "SLED", /* 570 */
  2095. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2096. "LED", /* all others */
  2097. ); /* R30, R31 */
  2098. static int __init led_init(struct ibm_init_struct *iibm)
  2099. {
  2100. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  2101. IBM_ACPIHANDLE_INIT(led);
  2102. if (!led_handle)
  2103. /* led not supported on R30, R31 */
  2104. led_supported = TPACPI_LED_NONE;
  2105. else if (strlencmp(led_path, "SLED") == 0)
  2106. /* 570 */
  2107. led_supported = TPACPI_LED_570;
  2108. else if (strlencmp(led_path, "SYSL") == 0)
  2109. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2110. led_supported = TPACPI_LED_OLD;
  2111. else
  2112. /* all others */
  2113. led_supported = TPACPI_LED_NEW;
  2114. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  2115. str_supported(led_supported), led_supported);
  2116. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  2117. }
  2118. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  2119. static int led_read(char *p)
  2120. {
  2121. int len = 0;
  2122. if (!led_supported) {
  2123. len += sprintf(p + len, "status:\t\tnot supported\n");
  2124. return len;
  2125. }
  2126. len += sprintf(p + len, "status:\t\tsupported\n");
  2127. if (led_supported == TPACPI_LED_570) {
  2128. /* 570 */
  2129. int i, status;
  2130. for (i = 0; i < 8; i++) {
  2131. if (!acpi_evalf(ec_handle,
  2132. &status, "GLED", "dd", 1 << i))
  2133. return -EIO;
  2134. len += sprintf(p + len, "%d:\t\t%s\n",
  2135. i, led_status(status));
  2136. }
  2137. }
  2138. len += sprintf(p + len, "commands:\t"
  2139. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  2140. return len;
  2141. }
  2142. /* off, on, blink */
  2143. static const int led_sled_arg1[] = { 0, 1, 3 };
  2144. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  2145. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  2146. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  2147. static int led_write(char *buf)
  2148. {
  2149. char *cmd;
  2150. int led, ind, ret;
  2151. if (!led_supported)
  2152. return -ENODEV;
  2153. while ((cmd = next_cmd(&buf))) {
  2154. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  2155. return -EINVAL;
  2156. if (strstr(cmd, "off")) {
  2157. ind = 0;
  2158. } else if (strstr(cmd, "on")) {
  2159. ind = 1;
  2160. } else if (strstr(cmd, "blink")) {
  2161. ind = 2;
  2162. } else
  2163. return -EINVAL;
  2164. if (led_supported == TPACPI_LED_570) {
  2165. /* 570 */
  2166. led = 1 << led;
  2167. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2168. led, led_sled_arg1[ind]))
  2169. return -EIO;
  2170. } else if (led_supported == TPACPI_LED_OLD) {
  2171. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  2172. led = 1 << led;
  2173. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  2174. if (ret >= 0)
  2175. ret =
  2176. ec_write(TPACPI_LED_EC_HLBL,
  2177. led * led_exp_hlbl[ind]);
  2178. if (ret >= 0)
  2179. ret =
  2180. ec_write(TPACPI_LED_EC_HLCL,
  2181. led * led_exp_hlcl[ind]);
  2182. if (ret < 0)
  2183. return ret;
  2184. } else {
  2185. /* all others */
  2186. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2187. led, led_led_arg1[ind]))
  2188. return -EIO;
  2189. }
  2190. }
  2191. return 0;
  2192. }
  2193. static struct ibm_struct led_driver_data = {
  2194. .name = "led",
  2195. .read = led_read,
  2196. .write = led_write,
  2197. };
  2198. /*************************************************************************
  2199. * Beep subdriver
  2200. */
  2201. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  2202. static int __init beep_init(struct ibm_init_struct *iibm)
  2203. {
  2204. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  2205. IBM_ACPIHANDLE_INIT(beep);
  2206. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  2207. str_supported(beep_handle != NULL));
  2208. return (beep_handle)? 0 : 1;
  2209. }
  2210. static int beep_read(char *p)
  2211. {
  2212. int len = 0;
  2213. if (!beep_handle)
  2214. len += sprintf(p + len, "status:\t\tnot supported\n");
  2215. else {
  2216. len += sprintf(p + len, "status:\t\tsupported\n");
  2217. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  2218. }
  2219. return len;
  2220. }
  2221. static int beep_write(char *buf)
  2222. {
  2223. char *cmd;
  2224. int beep_cmd;
  2225. if (!beep_handle)
  2226. return -ENODEV;
  2227. while ((cmd = next_cmd(&buf))) {
  2228. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  2229. beep_cmd >= 0 && beep_cmd <= 17) {
  2230. /* beep_cmd set */
  2231. } else
  2232. return -EINVAL;
  2233. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  2234. return -EIO;
  2235. }
  2236. return 0;
  2237. }
  2238. static struct ibm_struct beep_driver_data = {
  2239. .name = "beep",
  2240. .read = beep_read,
  2241. .write = beep_write,
  2242. };
  2243. /*************************************************************************
  2244. * Thermal subdriver
  2245. */
  2246. static enum thermal_access_mode thermal_read_mode;
  2247. /* sysfs temp##_input -------------------------------------------------- */
  2248. static ssize_t thermal_temp_input_show(struct device *dev,
  2249. struct device_attribute *attr,
  2250. char *buf)
  2251. {
  2252. struct sensor_device_attribute *sensor_attr =
  2253. to_sensor_dev_attr(attr);
  2254. int idx = sensor_attr->index;
  2255. s32 value;
  2256. int res;
  2257. res = thermal_get_sensor(idx, &value);
  2258. if (res)
  2259. return res;
  2260. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  2261. return -ENXIO;
  2262. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  2263. }
  2264. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  2265. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  2266. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  2267. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  2268. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  2269. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  2270. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  2271. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  2272. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  2273. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  2274. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  2275. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  2276. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  2277. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  2278. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  2279. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  2280. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  2281. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  2282. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  2283. };
  2284. #define THERMAL_ATTRS(X) \
  2285. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  2286. static struct attribute *thermal_temp_input_attr[] = {
  2287. THERMAL_ATTRS(8),
  2288. THERMAL_ATTRS(9),
  2289. THERMAL_ATTRS(10),
  2290. THERMAL_ATTRS(11),
  2291. THERMAL_ATTRS(12),
  2292. THERMAL_ATTRS(13),
  2293. THERMAL_ATTRS(14),
  2294. THERMAL_ATTRS(15),
  2295. THERMAL_ATTRS(0),
  2296. THERMAL_ATTRS(1),
  2297. THERMAL_ATTRS(2),
  2298. THERMAL_ATTRS(3),
  2299. THERMAL_ATTRS(4),
  2300. THERMAL_ATTRS(5),
  2301. THERMAL_ATTRS(6),
  2302. THERMAL_ATTRS(7),
  2303. NULL
  2304. };
  2305. static const struct attribute_group thermal_temp_input16_group = {
  2306. .attrs = thermal_temp_input_attr
  2307. };
  2308. static const struct attribute_group thermal_temp_input8_group = {
  2309. .attrs = &thermal_temp_input_attr[8]
  2310. };
  2311. #undef THERMAL_SENSOR_ATTR_TEMP
  2312. #undef THERMAL_ATTRS
  2313. /* --------------------------------------------------------------------- */
  2314. static int __init thermal_init(struct ibm_init_struct *iibm)
  2315. {
  2316. u8 t, ta1, ta2;
  2317. int i;
  2318. int acpi_tmp7;
  2319. int res;
  2320. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  2321. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  2322. if (thinkpad_id.ec_model) {
  2323. /*
  2324. * Direct EC access mode: sensors at registers
  2325. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  2326. * non-implemented, thermal sensors return 0x80 when
  2327. * not available
  2328. */
  2329. ta1 = ta2 = 0;
  2330. for (i = 0; i < 8; i++) {
  2331. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  2332. ta1 |= t;
  2333. } else {
  2334. ta1 = 0;
  2335. break;
  2336. }
  2337. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  2338. ta2 |= t;
  2339. } else {
  2340. ta1 = 0;
  2341. break;
  2342. }
  2343. }
  2344. if (ta1 == 0) {
  2345. /* This is sheer paranoia, but we handle it anyway */
  2346. if (acpi_tmp7) {
  2347. printk(IBM_ERR
  2348. "ThinkPad ACPI EC access misbehaving, "
  2349. "falling back to ACPI TMPx access mode\n");
  2350. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  2351. } else {
  2352. printk(IBM_ERR
  2353. "ThinkPad ACPI EC access misbehaving, "
  2354. "disabling thermal sensors access\n");
  2355. thermal_read_mode = TPACPI_THERMAL_NONE;
  2356. }
  2357. } else {
  2358. thermal_read_mode =
  2359. (ta2 != 0) ?
  2360. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  2361. }
  2362. } else if (acpi_tmp7) {
  2363. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  2364. /* 600e/x, 770e, 770x */
  2365. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  2366. } else {
  2367. /* Standard ACPI TMPx access, max 8 sensors */
  2368. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  2369. }
  2370. } else {
  2371. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  2372. thermal_read_mode = TPACPI_THERMAL_NONE;
  2373. }
  2374. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  2375. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  2376. thermal_read_mode);
  2377. switch(thermal_read_mode) {
  2378. case TPACPI_THERMAL_TPEC_16:
  2379. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  2380. &thermal_temp_input16_group);
  2381. if (res)
  2382. return res;
  2383. break;
  2384. case TPACPI_THERMAL_TPEC_8:
  2385. case TPACPI_THERMAL_ACPI_TMP07:
  2386. case TPACPI_THERMAL_ACPI_UPDT:
  2387. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  2388. &thermal_temp_input8_group);
  2389. if (res)
  2390. return res;
  2391. break;
  2392. case TPACPI_THERMAL_NONE:
  2393. default:
  2394. return 1;
  2395. }
  2396. return 0;
  2397. }
  2398. static void thermal_exit(void)
  2399. {
  2400. switch(thermal_read_mode) {
  2401. case TPACPI_THERMAL_TPEC_16:
  2402. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  2403. &thermal_temp_input16_group);
  2404. break;
  2405. case TPACPI_THERMAL_TPEC_8:
  2406. case TPACPI_THERMAL_ACPI_TMP07:
  2407. case TPACPI_THERMAL_ACPI_UPDT:
  2408. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  2409. &thermal_temp_input16_group);
  2410. break;
  2411. case TPACPI_THERMAL_NONE:
  2412. default:
  2413. break;
  2414. }
  2415. }
  2416. /* idx is zero-based */
  2417. static int thermal_get_sensor(int idx, s32 *value)
  2418. {
  2419. int t;
  2420. s8 tmp;
  2421. char tmpi[5];
  2422. t = TP_EC_THERMAL_TMP0;
  2423. switch (thermal_read_mode) {
  2424. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  2425. case TPACPI_THERMAL_TPEC_16:
  2426. if (idx >= 8 && idx <= 15) {
  2427. t = TP_EC_THERMAL_TMP8;
  2428. idx -= 8;
  2429. }
  2430. /* fallthrough */
  2431. #endif
  2432. case TPACPI_THERMAL_TPEC_8:
  2433. if (idx <= 7) {
  2434. if (!acpi_ec_read(t + idx, &tmp))
  2435. return -EIO;
  2436. *value = tmp * 1000;
  2437. return 0;
  2438. }
  2439. break;
  2440. case TPACPI_THERMAL_ACPI_UPDT:
  2441. if (idx <= 7) {
  2442. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  2443. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  2444. return -EIO;
  2445. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  2446. return -EIO;
  2447. *value = (t - 2732) * 100;
  2448. return 0;
  2449. }
  2450. break;
  2451. case TPACPI_THERMAL_ACPI_TMP07:
  2452. if (idx <= 7) {
  2453. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  2454. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  2455. return -EIO;
  2456. if (t > 127 || t < -127)
  2457. t = TP_EC_THERMAL_TMP_NA;
  2458. *value = t * 1000;
  2459. return 0;
  2460. }
  2461. break;
  2462. case TPACPI_THERMAL_NONE:
  2463. default:
  2464. return -ENOSYS;
  2465. }
  2466. return -EINVAL;
  2467. }
  2468. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  2469. {
  2470. int res, i;
  2471. int n;
  2472. n = 8;
  2473. i = 0;
  2474. if (!s)
  2475. return -EINVAL;
  2476. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  2477. n = 16;
  2478. for(i = 0 ; i < n; i++) {
  2479. res = thermal_get_sensor(i, &s->temp[i]);
  2480. if (res)
  2481. return res;
  2482. }
  2483. return n;
  2484. }
  2485. static int thermal_read(char *p)
  2486. {
  2487. int len = 0;
  2488. int n, i;
  2489. struct ibm_thermal_sensors_struct t;
  2490. n = thermal_get_sensors(&t);
  2491. if (unlikely(n < 0))
  2492. return n;
  2493. len += sprintf(p + len, "temperatures:\t");
  2494. if (n > 0) {
  2495. for (i = 0; i < (n - 1); i++)
  2496. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  2497. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  2498. } else
  2499. len += sprintf(p + len, "not supported\n");
  2500. return len;
  2501. }
  2502. static struct ibm_struct thermal_driver_data = {
  2503. .name = "thermal",
  2504. .read = thermal_read,
  2505. .exit = thermal_exit,
  2506. };
  2507. /*************************************************************************
  2508. * EC Dump subdriver
  2509. */
  2510. static u8 ecdump_regs[256];
  2511. static int ecdump_read(char *p)
  2512. {
  2513. int len = 0;
  2514. int i, j;
  2515. u8 v;
  2516. len += sprintf(p + len, "EC "
  2517. " +00 +01 +02 +03 +04 +05 +06 +07"
  2518. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  2519. for (i = 0; i < 256; i += 16) {
  2520. len += sprintf(p + len, "EC 0x%02x:", i);
  2521. for (j = 0; j < 16; j++) {
  2522. if (!acpi_ec_read(i + j, &v))
  2523. break;
  2524. if (v != ecdump_regs[i + j])
  2525. len += sprintf(p + len, " *%02x", v);
  2526. else
  2527. len += sprintf(p + len, " %02x", v);
  2528. ecdump_regs[i + j] = v;
  2529. }
  2530. len += sprintf(p + len, "\n");
  2531. if (j != 16)
  2532. break;
  2533. }
  2534. /* These are way too dangerous to advertise openly... */
  2535. #if 0
  2536. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  2537. " (<offset> is 00-ff, <value> is 00-ff)\n");
  2538. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  2539. " (<offset> is 00-ff, <value> is 0-255)\n");
  2540. #endif
  2541. return len;
  2542. }
  2543. static int ecdump_write(char *buf)
  2544. {
  2545. char *cmd;
  2546. int i, v;
  2547. while ((cmd = next_cmd(&buf))) {
  2548. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  2549. /* i and v set */
  2550. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  2551. /* i and v set */
  2552. } else
  2553. return -EINVAL;
  2554. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  2555. if (!acpi_ec_write(i, v))
  2556. return -EIO;
  2557. } else
  2558. return -EINVAL;
  2559. }
  2560. return 0;
  2561. }
  2562. static struct ibm_struct ecdump_driver_data = {
  2563. .name = "ecdump",
  2564. .read = ecdump_read,
  2565. .write = ecdump_write,
  2566. .flags.experimental = 1,
  2567. };
  2568. /*************************************************************************
  2569. * Backlight/brightness subdriver
  2570. */
  2571. static struct backlight_device *ibm_backlight_device;
  2572. static struct backlight_ops ibm_backlight_data = {
  2573. .get_brightness = brightness_get,
  2574. .update_status = brightness_update_status,
  2575. };
  2576. static struct mutex brightness_mutex;
  2577. static int __init tpacpi_query_bcll_levels(acpi_handle handle)
  2578. {
  2579. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  2580. union acpi_object *obj;
  2581. int rc;
  2582. if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
  2583. obj = (union acpi_object *)buffer.pointer;
  2584. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  2585. printk(IBM_ERR "Unknown BCLL data, "
  2586. "please report this to %s\n", IBM_MAIL);
  2587. rc = 0;
  2588. } else {
  2589. rc = obj->package.count;
  2590. }
  2591. } else {
  2592. return 0;
  2593. }
  2594. kfree(buffer.pointer);
  2595. return rc;
  2596. }
  2597. static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
  2598. void *context, void **rv)
  2599. {
  2600. char name[ACPI_PATH_SEGMENT_LENGTH];
  2601. struct acpi_buffer buffer = { sizeof(name), &name };
  2602. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  2603. !strncmp("BCLL", name, sizeof(name) - 1)) {
  2604. if (tpacpi_query_bcll_levels(handle) == 16) {
  2605. *rv = handle;
  2606. return AE_CTRL_TERMINATE;
  2607. } else {
  2608. return AE_OK;
  2609. }
  2610. } else {
  2611. return AE_OK;
  2612. }
  2613. }
  2614. static int __init brightness_check_levels(void)
  2615. {
  2616. int status;
  2617. void *found_node = NULL;
  2618. if (!vid_handle) {
  2619. IBM_ACPIHANDLE_INIT(vid);
  2620. }
  2621. if (!vid_handle)
  2622. return 0;
  2623. /* Search for a BCLL package with 16 levels */
  2624. status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
  2625. brightness_find_bcll, NULL, &found_node);
  2626. return (ACPI_SUCCESS(status) && found_node != NULL);
  2627. }
  2628. static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
  2629. void *context, void **rv)
  2630. {
  2631. char name[ACPI_PATH_SEGMENT_LENGTH];
  2632. struct acpi_buffer buffer = { sizeof(name), &name };
  2633. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  2634. !strncmp("_BCL", name, sizeof(name) - 1)) {
  2635. *rv = handle;
  2636. return AE_CTRL_TERMINATE;
  2637. } else {
  2638. return AE_OK;
  2639. }
  2640. }
  2641. static int __init brightness_check_std_acpi_support(void)
  2642. {
  2643. int status;
  2644. void *found_node = NULL;
  2645. if (!vid_handle) {
  2646. IBM_ACPIHANDLE_INIT(vid);
  2647. }
  2648. if (!vid_handle)
  2649. return 0;
  2650. /* Search for a _BCL method, but don't execute it */
  2651. status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
  2652. brightness_find_bcl, NULL, &found_node);
  2653. return (ACPI_SUCCESS(status) && found_node != NULL);
  2654. }
  2655. static int __init brightness_init(struct ibm_init_struct *iibm)
  2656. {
  2657. int b;
  2658. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  2659. mutex_init(&brightness_mutex);
  2660. if (!brightness_enable) {
  2661. dbg_printk(TPACPI_DBG_INIT,
  2662. "brightness support disabled by module parameter\n");
  2663. return 1;
  2664. } else if (brightness_enable > 1) {
  2665. if (brightness_check_std_acpi_support()) {
  2666. printk(IBM_NOTICE
  2667. "standard ACPI backlight interface available, not loading native one...\n");
  2668. return 1;
  2669. }
  2670. }
  2671. if (!brightness_mode) {
  2672. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
  2673. brightness_mode = 2;
  2674. else
  2675. brightness_mode = 3;
  2676. dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
  2677. brightness_mode);
  2678. }
  2679. if (brightness_mode > 3)
  2680. return -EINVAL;
  2681. tp_features.bright_16levels =
  2682. thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
  2683. brightness_check_levels();
  2684. b = brightness_get(NULL);
  2685. if (b < 0)
  2686. return 1;
  2687. if (tp_features.bright_16levels)
  2688. printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
  2689. ibm_backlight_device = backlight_device_register(
  2690. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  2691. &ibm_backlight_data);
  2692. if (IS_ERR(ibm_backlight_device)) {
  2693. printk(IBM_ERR "Could not register backlight device\n");
  2694. return PTR_ERR(ibm_backlight_device);
  2695. }
  2696. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  2697. ibm_backlight_device->props.max_brightness =
  2698. (tp_features.bright_16levels)? 15 : 7;
  2699. ibm_backlight_device->props.brightness = b;
  2700. backlight_update_status(ibm_backlight_device);
  2701. return 0;
  2702. }
  2703. static void brightness_exit(void)
  2704. {
  2705. if (ibm_backlight_device) {
  2706. vdbg_printk(TPACPI_DBG_EXIT,
  2707. "calling backlight_device_unregister()\n");
  2708. backlight_device_unregister(ibm_backlight_device);
  2709. ibm_backlight_device = NULL;
  2710. }
  2711. }
  2712. static int brightness_update_status(struct backlight_device *bd)
  2713. {
  2714. /* it is the backlight class's job (caller) to handle
  2715. * EINTR and other errors properly */
  2716. return brightness_set(
  2717. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  2718. bd->props.power == FB_BLANK_UNBLANK) ?
  2719. bd->props.brightness : 0);
  2720. }
  2721. /*
  2722. * ThinkPads can read brightness from two places: EC 0x31, or
  2723. * CMOS NVRAM byte 0x5E, bits 0-3.
  2724. */
  2725. static int brightness_get(struct backlight_device *bd)
  2726. {
  2727. u8 lec = 0, lcmos = 0, level = 0;
  2728. if (brightness_mode & 1) {
  2729. if (!acpi_ec_read(brightness_offset, &lec))
  2730. return -EIO;
  2731. lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
  2732. level = lec;
  2733. };
  2734. if (brightness_mode & 2) {
  2735. lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  2736. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  2737. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  2738. lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
  2739. level = lcmos;
  2740. }
  2741. if (brightness_mode == 3 && lec != lcmos) {
  2742. printk(IBM_ERR
  2743. "CMOS NVRAM (%u) and EC (%u) do not agree "
  2744. "on display brightness level\n",
  2745. (unsigned int) lcmos,
  2746. (unsigned int) lec);
  2747. return -EIO;
  2748. }
  2749. return level;
  2750. }
  2751. /* May return EINTR which can always be mapped to ERESTARTSYS */
  2752. static int brightness_set(int value)
  2753. {
  2754. int cmos_cmd, inc, i, res;
  2755. int current_value;
  2756. if (value > ((tp_features.bright_16levels)? 15 : 7))
  2757. return -EINVAL;
  2758. res = mutex_lock_interruptible(&brightness_mutex);
  2759. if (res < 0)
  2760. return res;
  2761. current_value = brightness_get(NULL);
  2762. if (current_value < 0) {
  2763. res = current_value;
  2764. goto errout;
  2765. }
  2766. cmos_cmd = value > current_value ?
  2767. TP_CMOS_BRIGHTNESS_UP :
  2768. TP_CMOS_BRIGHTNESS_DOWN;
  2769. inc = (value > current_value)? 1 : -1;
  2770. res = 0;
  2771. for (i = current_value; i != value; i += inc) {
  2772. if ((brightness_mode & 2) &&
  2773. issue_thinkpad_cmos_command(cmos_cmd)) {
  2774. res = -EIO;
  2775. goto errout;
  2776. }
  2777. if ((brightness_mode & 1) &&
  2778. !acpi_ec_write(brightness_offset, i + inc)) {
  2779. res = -EIO;
  2780. goto errout;;
  2781. }
  2782. }
  2783. errout:
  2784. mutex_unlock(&brightness_mutex);
  2785. return res;
  2786. }
  2787. static int brightness_read(char *p)
  2788. {
  2789. int len = 0;
  2790. int level;
  2791. if ((level = brightness_get(NULL)) < 0) {
  2792. len += sprintf(p + len, "level:\t\tunreadable\n");
  2793. } else {
  2794. len += sprintf(p + len, "level:\t\t%d\n", level);
  2795. len += sprintf(p + len, "commands:\tup, down\n");
  2796. len += sprintf(p + len, "commands:\tlevel <level>"
  2797. " (<level> is 0-%d)\n",
  2798. (tp_features.bright_16levels) ? 15 : 7);
  2799. }
  2800. return len;
  2801. }
  2802. static int brightness_write(char *buf)
  2803. {
  2804. int level;
  2805. int rc;
  2806. char *cmd;
  2807. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  2808. level = brightness_get(NULL);
  2809. if (level < 0)
  2810. return level;
  2811. while ((cmd = next_cmd(&buf))) {
  2812. if (strlencmp(cmd, "up") == 0) {
  2813. if (level < max_level)
  2814. level++;
  2815. } else if (strlencmp(cmd, "down") == 0) {
  2816. if (level > 0)
  2817. level--;
  2818. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  2819. level >= 0 && level <= max_level) {
  2820. /* new level set */
  2821. } else
  2822. return -EINVAL;
  2823. }
  2824. /*
  2825. * Now we know what the final level should be, so we try to set it.
  2826. * Doing it this way makes the syscall restartable in case of EINTR
  2827. */
  2828. rc = brightness_set(level);
  2829. return (rc == -EINTR)? ERESTARTSYS : rc;
  2830. }
  2831. static struct ibm_struct brightness_driver_data = {
  2832. .name = "brightness",
  2833. .read = brightness_read,
  2834. .write = brightness_write,
  2835. .exit = brightness_exit,
  2836. };
  2837. /*************************************************************************
  2838. * Volume subdriver
  2839. */
  2840. static int volume_read(char *p)
  2841. {
  2842. int len = 0;
  2843. u8 level;
  2844. if (!acpi_ec_read(volume_offset, &level)) {
  2845. len += sprintf(p + len, "level:\t\tunreadable\n");
  2846. } else {
  2847. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  2848. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  2849. len += sprintf(p + len, "commands:\tup, down, mute\n");
  2850. len += sprintf(p + len, "commands:\tlevel <level>"
  2851. " (<level> is 0-15)\n");
  2852. }
  2853. return len;
  2854. }
  2855. static int volume_write(char *buf)
  2856. {
  2857. int cmos_cmd, inc, i;
  2858. u8 level, mute;
  2859. int new_level, new_mute;
  2860. char *cmd;
  2861. while ((cmd = next_cmd(&buf))) {
  2862. if (!acpi_ec_read(volume_offset, &level))
  2863. return -EIO;
  2864. new_mute = mute = level & 0x40;
  2865. new_level = level = level & 0xf;
  2866. if (strlencmp(cmd, "up") == 0) {
  2867. if (mute)
  2868. new_mute = 0;
  2869. else
  2870. new_level = level == 15 ? 15 : level + 1;
  2871. } else if (strlencmp(cmd, "down") == 0) {
  2872. if (mute)
  2873. new_mute = 0;
  2874. else
  2875. new_level = level == 0 ? 0 : level - 1;
  2876. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2877. new_level >= 0 && new_level <= 15) {
  2878. /* new_level set */
  2879. } else if (strlencmp(cmd, "mute") == 0) {
  2880. new_mute = 0x40;
  2881. } else
  2882. return -EINVAL;
  2883. if (new_level != level) { /* mute doesn't change */
  2884. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  2885. inc = new_level > level ? 1 : -1;
  2886. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  2887. !acpi_ec_write(volume_offset, level)))
  2888. return -EIO;
  2889. for (i = level; i != new_level; i += inc)
  2890. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2891. !acpi_ec_write(volume_offset, i + inc))
  2892. return -EIO;
  2893. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  2894. !acpi_ec_write(volume_offset,
  2895. new_level + mute)))
  2896. return -EIO;
  2897. }
  2898. if (new_mute != mute) { /* level doesn't change */
  2899. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  2900. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2901. !acpi_ec_write(volume_offset, level + new_mute))
  2902. return -EIO;
  2903. }
  2904. }
  2905. return 0;
  2906. }
  2907. static struct ibm_struct volume_driver_data = {
  2908. .name = "volume",
  2909. .read = volume_read,
  2910. .write = volume_write,
  2911. };
  2912. /*************************************************************************
  2913. * Fan subdriver
  2914. */
  2915. /*
  2916. * FAN ACCESS MODES
  2917. *
  2918. * TPACPI_FAN_RD_ACPI_GFAN:
  2919. * ACPI GFAN method: returns fan level
  2920. *
  2921. * see TPACPI_FAN_WR_ACPI_SFAN
  2922. * EC 0x2f (HFSP) not available if GFAN exists
  2923. *
  2924. * TPACPI_FAN_WR_ACPI_SFAN:
  2925. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2926. *
  2927. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2928. * it for writing.
  2929. *
  2930. * TPACPI_FAN_WR_TPEC:
  2931. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2932. * Supported on almost all ThinkPads
  2933. *
  2934. * Fan speed changes of any sort (including those caused by the
  2935. * disengaged mode) are usually done slowly by the firmware as the
  2936. * maximum ammount of fan duty cycle change per second seems to be
  2937. * limited.
  2938. *
  2939. * Reading is not available if GFAN exists.
  2940. * Writing is not available if SFAN exists.
  2941. *
  2942. * Bits
  2943. * 7 automatic mode engaged;
  2944. * (default operation mode of the ThinkPad)
  2945. * fan level is ignored in this mode.
  2946. * 6 full speed mode (takes precedence over bit 7);
  2947. * not available on all thinkpads. May disable
  2948. * the tachometer while the fan controller ramps up
  2949. * the speed (which can take up to a few *minutes*).
  2950. * Speeds up fan to 100% duty-cycle, which is far above
  2951. * the standard RPM levels. It is not impossible that
  2952. * it could cause hardware damage.
  2953. * 5-3 unused in some models. Extra bits for fan level
  2954. * in others, but still useless as all values above
  2955. * 7 map to the same speed as level 7 in these models.
  2956. * 2-0 fan level (0..7 usually)
  2957. * 0x00 = stop
  2958. * 0x07 = max (set when temperatures critical)
  2959. * Some ThinkPads may have other levels, see
  2960. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2961. *
  2962. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2963. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2964. * does so, its initial value is meaningless (0x07).
  2965. *
  2966. * For firmware bugs, refer to:
  2967. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2968. *
  2969. * ----
  2970. *
  2971. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2972. * Main fan tachometer reading (in RPM)
  2973. *
  2974. * This register is present on all ThinkPads with a new-style EC, and
  2975. * it is known not to be present on the A21m/e, and T22, as there is
  2976. * something else in offset 0x84 according to the ACPI DSDT. Other
  2977. * ThinkPads from this same time period (and earlier) probably lack the
  2978. * tachometer as well.
  2979. *
  2980. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2981. * was never fixed by IBM to report the EC firmware version string
  2982. * probably support the tachometer (like the early X models), so
  2983. * detecting it is quite hard. We need more data to know for sure.
  2984. *
  2985. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2986. * might result.
  2987. *
  2988. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2989. * mode.
  2990. *
  2991. * For firmware bugs, refer to:
  2992. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2993. *
  2994. * TPACPI_FAN_WR_ACPI_FANS:
  2995. * ThinkPad X31, X40, X41. Not available in the X60.
  2996. *
  2997. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2998. * high speed. ACPI DSDT seems to map these three speeds to levels
  2999. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  3000. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  3001. *
  3002. * The speeds are stored on handles
  3003. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  3004. *
  3005. * There are three default speed sets, acessible as handles:
  3006. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  3007. *
  3008. * ACPI DSDT switches which set is in use depending on various
  3009. * factors.
  3010. *
  3011. * TPACPI_FAN_WR_TPEC is also available and should be used to
  3012. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  3013. * but the ACPI tables just mention level 7.
  3014. */
  3015. static enum fan_status_access_mode fan_status_access_mode;
  3016. static enum fan_control_access_mode fan_control_access_mode;
  3017. static enum fan_control_commands fan_control_commands;
  3018. static u8 fan_control_initial_status;
  3019. static u8 fan_control_desired_level;
  3020. static void fan_watchdog_fire(struct work_struct *ignored);
  3021. static int fan_watchdog_maxinterval;
  3022. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  3023. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  3024. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  3025. "\\FSPD", /* 600e/x, 770e, 770x */
  3026. ); /* all others */
  3027. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  3028. "JFNS", /* 770x-JL */
  3029. ); /* all others */
  3030. /*
  3031. * SYSFS fan layout: hwmon compatible (device)
  3032. *
  3033. * pwm*_enable:
  3034. * 0: "disengaged" mode
  3035. * 1: manual mode
  3036. * 2: native EC "auto" mode (recommended, hardware default)
  3037. *
  3038. * pwm*: set speed in manual mode, ignored otherwise.
  3039. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  3040. * interpolation.
  3041. *
  3042. * fan*_input: tachometer reading, RPM
  3043. *
  3044. *
  3045. * SYSFS fan layout: extensions
  3046. *
  3047. * fan_watchdog (driver):
  3048. * fan watchdog interval in seconds, 0 disables (default), max 120
  3049. */
  3050. /* sysfs fan pwm1_enable ----------------------------------------------- */
  3051. static ssize_t fan_pwm1_enable_show(struct device *dev,
  3052. struct device_attribute *attr,
  3053. char *buf)
  3054. {
  3055. int res, mode;
  3056. u8 status;
  3057. res = fan_get_status_safe(&status);
  3058. if (res)
  3059. return res;
  3060. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3061. if (status != fan_control_initial_status) {
  3062. tp_features.fan_ctrl_status_undef = 0;
  3063. } else {
  3064. /* Return most likely status. In fact, it
  3065. * might be the only possible status */
  3066. status = TP_EC_FAN_AUTO;
  3067. }
  3068. }
  3069. if (status & TP_EC_FAN_FULLSPEED) {
  3070. mode = 0;
  3071. } else if (status & TP_EC_FAN_AUTO) {
  3072. mode = 2;
  3073. } else
  3074. mode = 1;
  3075. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  3076. }
  3077. static ssize_t fan_pwm1_enable_store(struct device *dev,
  3078. struct device_attribute *attr,
  3079. const char *buf, size_t count)
  3080. {
  3081. unsigned long t;
  3082. int res, level;
  3083. if (parse_strtoul(buf, 2, &t))
  3084. return -EINVAL;
  3085. switch (t) {
  3086. case 0:
  3087. level = TP_EC_FAN_FULLSPEED;
  3088. break;
  3089. case 1:
  3090. level = TPACPI_FAN_LAST_LEVEL;
  3091. break;
  3092. case 2:
  3093. level = TP_EC_FAN_AUTO;
  3094. break;
  3095. case 3:
  3096. /* reserved for software-controlled auto mode */
  3097. return -ENOSYS;
  3098. default:
  3099. return -EINVAL;
  3100. }
  3101. res = fan_set_level_safe(level);
  3102. if (res == -ENXIO)
  3103. return -EINVAL;
  3104. else if (res < 0)
  3105. return res;
  3106. fan_watchdog_reset();
  3107. return count;
  3108. }
  3109. static struct device_attribute dev_attr_fan_pwm1_enable =
  3110. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  3111. fan_pwm1_enable_show, fan_pwm1_enable_store);
  3112. /* sysfs fan pwm1 ------------------------------------------------------ */
  3113. static ssize_t fan_pwm1_show(struct device *dev,
  3114. struct device_attribute *attr,
  3115. char *buf)
  3116. {
  3117. int res;
  3118. u8 status;
  3119. res = fan_get_status_safe(&status);
  3120. if (res)
  3121. return res;
  3122. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3123. if (status != fan_control_initial_status) {
  3124. tp_features.fan_ctrl_status_undef = 0;
  3125. } else {
  3126. status = TP_EC_FAN_AUTO;
  3127. }
  3128. }
  3129. if ((status &
  3130. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  3131. status = fan_control_desired_level;
  3132. if (status > 7)
  3133. status = 7;
  3134. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  3135. }
  3136. static ssize_t fan_pwm1_store(struct device *dev,
  3137. struct device_attribute *attr,
  3138. const char *buf, size_t count)
  3139. {
  3140. unsigned long s;
  3141. int rc;
  3142. u8 status, newlevel;
  3143. if (parse_strtoul(buf, 255, &s))
  3144. return -EINVAL;
  3145. /* scale down from 0-255 to 0-7 */
  3146. newlevel = (s >> 5) & 0x07;
  3147. if (mutex_lock_interruptible(&fan_mutex))
  3148. return -ERESTARTSYS;
  3149. rc = fan_get_status(&status);
  3150. if (!rc && (status &
  3151. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  3152. rc = fan_set_level(newlevel);
  3153. if (rc == -ENXIO)
  3154. rc = -EINVAL;
  3155. else if (!rc) {
  3156. fan_update_desired_level(newlevel);
  3157. fan_watchdog_reset();
  3158. }
  3159. }
  3160. mutex_unlock(&fan_mutex);
  3161. return (rc)? rc : count;
  3162. }
  3163. static struct device_attribute dev_attr_fan_pwm1 =
  3164. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  3165. fan_pwm1_show, fan_pwm1_store);
  3166. /* sysfs fan fan1_input ------------------------------------------------ */
  3167. static ssize_t fan_fan1_input_show(struct device *dev,
  3168. struct device_attribute *attr,
  3169. char *buf)
  3170. {
  3171. int res;
  3172. unsigned int speed;
  3173. res = fan_get_speed(&speed);
  3174. if (res < 0)
  3175. return res;
  3176. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  3177. }
  3178. static struct device_attribute dev_attr_fan_fan1_input =
  3179. __ATTR(fan1_input, S_IRUGO,
  3180. fan_fan1_input_show, NULL);
  3181. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  3182. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  3183. char *buf)
  3184. {
  3185. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  3186. }
  3187. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  3188. const char *buf, size_t count)
  3189. {
  3190. unsigned long t;
  3191. if (parse_strtoul(buf, 120, &t))
  3192. return -EINVAL;
  3193. if (!fan_control_allowed)
  3194. return -EPERM;
  3195. fan_watchdog_maxinterval = t;
  3196. fan_watchdog_reset();
  3197. return count;
  3198. }
  3199. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  3200. fan_fan_watchdog_show, fan_fan_watchdog_store);
  3201. /* --------------------------------------------------------------------- */
  3202. static struct attribute *fan_attributes[] = {
  3203. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  3204. &dev_attr_fan_fan1_input.attr,
  3205. NULL
  3206. };
  3207. static const struct attribute_group fan_attr_group = {
  3208. .attrs = fan_attributes,
  3209. };
  3210. static int __init fan_init(struct ibm_init_struct *iibm)
  3211. {
  3212. int rc;
  3213. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  3214. mutex_init(&fan_mutex);
  3215. fan_status_access_mode = TPACPI_FAN_NONE;
  3216. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  3217. fan_control_commands = 0;
  3218. fan_watchdog_maxinterval = 0;
  3219. tp_features.fan_ctrl_status_undef = 0;
  3220. fan_control_desired_level = 7;
  3221. IBM_ACPIHANDLE_INIT(fans);
  3222. IBM_ACPIHANDLE_INIT(gfan);
  3223. IBM_ACPIHANDLE_INIT(sfan);
  3224. if (gfan_handle) {
  3225. /* 570, 600e/x, 770e, 770x */
  3226. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  3227. } else {
  3228. /* all other ThinkPads: note that even old-style
  3229. * ThinkPad ECs supports the fan control register */
  3230. if (likely(acpi_ec_read(fan_status_offset,
  3231. &fan_control_initial_status))) {
  3232. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  3233. /* In some ThinkPads, neither the EC nor the ACPI
  3234. * DSDT initialize the fan status, and it ends up
  3235. * being set to 0x07 when it *could* be either
  3236. * 0x07 or 0x80.
  3237. *
  3238. * Enable for TP-1Y (T43), TP-78 (R51e),
  3239. * TP-76 (R52), TP-70 (T43, R52), which are known
  3240. * to be buggy. */
  3241. if (fan_control_initial_status == 0x07) {
  3242. switch (thinkpad_id.ec_model) {
  3243. case 0x5931: /* TP-1Y */
  3244. case 0x3837: /* TP-78 */
  3245. case 0x3637: /* TP-76 */
  3246. case 0x3037: /* TP-70 */
  3247. printk(IBM_NOTICE
  3248. "fan_init: initial fan status is "
  3249. "unknown, assuming it is in auto "
  3250. "mode\n");
  3251. tp_features.fan_ctrl_status_undef = 1;
  3252. ;;
  3253. }
  3254. }
  3255. } else {
  3256. printk(IBM_ERR
  3257. "ThinkPad ACPI EC access misbehaving, "
  3258. "fan status and control unavailable\n");
  3259. return 1;
  3260. }
  3261. }
  3262. if (sfan_handle) {
  3263. /* 570, 770x-JL */
  3264. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  3265. fan_control_commands |=
  3266. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  3267. } else {
  3268. if (!gfan_handle) {
  3269. /* gfan without sfan means no fan control */
  3270. /* all other models implement TP EC 0x2f control */
  3271. if (fans_handle) {
  3272. /* X31, X40, X41 */
  3273. fan_control_access_mode =
  3274. TPACPI_FAN_WR_ACPI_FANS;
  3275. fan_control_commands |=
  3276. TPACPI_FAN_CMD_SPEED |
  3277. TPACPI_FAN_CMD_LEVEL |
  3278. TPACPI_FAN_CMD_ENABLE;
  3279. } else {
  3280. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  3281. fan_control_commands |=
  3282. TPACPI_FAN_CMD_LEVEL |
  3283. TPACPI_FAN_CMD_ENABLE;
  3284. }
  3285. }
  3286. }
  3287. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  3288. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  3289. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  3290. fan_status_access_mode, fan_control_access_mode);
  3291. /* fan control master switch */
  3292. if (!fan_control_allowed) {
  3293. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  3294. fan_control_commands = 0;
  3295. dbg_printk(TPACPI_DBG_INIT,
  3296. "fan control features disabled by parameter\n");
  3297. }
  3298. /* update fan_control_desired_level */
  3299. if (fan_status_access_mode != TPACPI_FAN_NONE)
  3300. fan_get_status_safe(NULL);
  3301. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  3302. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  3303. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3304. &fan_attr_group);
  3305. if (!(rc < 0))
  3306. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  3307. &driver_attr_fan_watchdog);
  3308. if (rc < 0)
  3309. return rc;
  3310. return 0;
  3311. } else
  3312. return 1;
  3313. }
  3314. /*
  3315. * Call with fan_mutex held
  3316. */
  3317. static void fan_update_desired_level(u8 status)
  3318. {
  3319. if ((status &
  3320. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  3321. if (status > 7)
  3322. fan_control_desired_level = 7;
  3323. else
  3324. fan_control_desired_level = status;
  3325. }
  3326. }
  3327. static int fan_get_status(u8 *status)
  3328. {
  3329. u8 s;
  3330. /* TODO:
  3331. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  3332. switch (fan_status_access_mode) {
  3333. case TPACPI_FAN_RD_ACPI_GFAN:
  3334. /* 570, 600e/x, 770e, 770x */
  3335. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  3336. return -EIO;
  3337. if (likely(status))
  3338. *status = s & 0x07;
  3339. break;
  3340. case TPACPI_FAN_RD_TPEC:
  3341. /* all except 570, 600e/x, 770e, 770x */
  3342. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  3343. return -EIO;
  3344. if (likely(status))
  3345. *status = s;
  3346. break;
  3347. default:
  3348. return -ENXIO;
  3349. }
  3350. return 0;
  3351. }
  3352. static int fan_get_status_safe(u8 *status)
  3353. {
  3354. int rc;
  3355. u8 s;
  3356. if (mutex_lock_interruptible(&fan_mutex))
  3357. return -ERESTARTSYS;
  3358. rc = fan_get_status(&s);
  3359. if (!rc)
  3360. fan_update_desired_level(s);
  3361. mutex_unlock(&fan_mutex);
  3362. if (status)
  3363. *status = s;
  3364. return rc;
  3365. }
  3366. static void fan_exit(void)
  3367. {
  3368. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  3369. /* FIXME: can we really do this unconditionally? */
  3370. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  3371. driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
  3372. cancel_delayed_work(&fan_watchdog_task);
  3373. flush_scheduled_work();
  3374. }
  3375. static int fan_get_speed(unsigned int *speed)
  3376. {
  3377. u8 hi, lo;
  3378. switch (fan_status_access_mode) {
  3379. case TPACPI_FAN_RD_TPEC:
  3380. /* all except 570, 600e/x, 770e, 770x */
  3381. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  3382. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  3383. return -EIO;
  3384. if (likely(speed))
  3385. *speed = (hi << 8) | lo;
  3386. break;
  3387. default:
  3388. return -ENXIO;
  3389. }
  3390. return 0;
  3391. }
  3392. static void fan_watchdog_fire(struct work_struct *ignored)
  3393. {
  3394. int rc;
  3395. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  3396. return;
  3397. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  3398. rc = fan_set_enable();
  3399. if (rc < 0) {
  3400. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  3401. "will try again later...\n", -rc);
  3402. /* reschedule for later */
  3403. fan_watchdog_reset();
  3404. }
  3405. }
  3406. static void fan_watchdog_reset(void)
  3407. {
  3408. static int fan_watchdog_active;
  3409. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  3410. return;
  3411. if (fan_watchdog_active)
  3412. cancel_delayed_work(&fan_watchdog_task);
  3413. if (fan_watchdog_maxinterval > 0 &&
  3414. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  3415. fan_watchdog_active = 1;
  3416. if (!schedule_delayed_work(&fan_watchdog_task,
  3417. msecs_to_jiffies(fan_watchdog_maxinterval
  3418. * 1000))) {
  3419. printk(IBM_ERR "failed to schedule the fan watchdog, "
  3420. "watchdog will not trigger\n");
  3421. }
  3422. } else
  3423. fan_watchdog_active = 0;
  3424. }
  3425. static int fan_set_level(int level)
  3426. {
  3427. if (!fan_control_allowed)
  3428. return -EPERM;
  3429. switch (fan_control_access_mode) {
  3430. case TPACPI_FAN_WR_ACPI_SFAN:
  3431. if (level >= 0 && level <= 7) {
  3432. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  3433. return -EIO;
  3434. } else
  3435. return -EINVAL;
  3436. break;
  3437. case TPACPI_FAN_WR_ACPI_FANS:
  3438. case TPACPI_FAN_WR_TPEC:
  3439. if ((level != TP_EC_FAN_AUTO) &&
  3440. (level != TP_EC_FAN_FULLSPEED) &&
  3441. ((level < 0) || (level > 7)))
  3442. return -EINVAL;
  3443. /* safety net should the EC not support AUTO
  3444. * or FULLSPEED mode bits and just ignore them */
  3445. if (level & TP_EC_FAN_FULLSPEED)
  3446. level |= 7; /* safety min speed 7 */
  3447. else if (level & TP_EC_FAN_FULLSPEED)
  3448. level |= 4; /* safety min speed 4 */
  3449. if (!acpi_ec_write(fan_status_offset, level))
  3450. return -EIO;
  3451. else
  3452. tp_features.fan_ctrl_status_undef = 0;
  3453. break;
  3454. default:
  3455. return -ENXIO;
  3456. }
  3457. return 0;
  3458. }
  3459. static int fan_set_level_safe(int level)
  3460. {
  3461. int rc;
  3462. if (!fan_control_allowed)
  3463. return -EPERM;
  3464. if (mutex_lock_interruptible(&fan_mutex))
  3465. return -ERESTARTSYS;
  3466. if (level == TPACPI_FAN_LAST_LEVEL)
  3467. level = fan_control_desired_level;
  3468. rc = fan_set_level(level);
  3469. if (!rc)
  3470. fan_update_desired_level(level);
  3471. mutex_unlock(&fan_mutex);
  3472. return rc;
  3473. }
  3474. static int fan_set_enable(void)
  3475. {
  3476. u8 s;
  3477. int rc;
  3478. if (!fan_control_allowed)
  3479. return -EPERM;
  3480. if (mutex_lock_interruptible(&fan_mutex))
  3481. return -ERESTARTSYS;
  3482. switch (fan_control_access_mode) {
  3483. case TPACPI_FAN_WR_ACPI_FANS:
  3484. case TPACPI_FAN_WR_TPEC:
  3485. rc = fan_get_status(&s);
  3486. if (rc < 0)
  3487. break;
  3488. /* Don't go out of emergency fan mode */
  3489. if (s != 7) {
  3490. s &= 0x07;
  3491. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  3492. }
  3493. if (!acpi_ec_write(fan_status_offset, s))
  3494. rc = -EIO;
  3495. else {
  3496. tp_features.fan_ctrl_status_undef = 0;
  3497. rc = 0;
  3498. }
  3499. break;
  3500. case TPACPI_FAN_WR_ACPI_SFAN:
  3501. rc = fan_get_status(&s);
  3502. if (rc < 0)
  3503. break;
  3504. s &= 0x07;
  3505. /* Set fan to at least level 4 */
  3506. s |= 4;
  3507. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  3508. rc= -EIO;
  3509. else
  3510. rc = 0;
  3511. break;
  3512. default:
  3513. rc = -ENXIO;
  3514. }
  3515. mutex_unlock(&fan_mutex);
  3516. return rc;
  3517. }
  3518. static int fan_set_disable(void)
  3519. {
  3520. int rc;
  3521. if (!fan_control_allowed)
  3522. return -EPERM;
  3523. if (mutex_lock_interruptible(&fan_mutex))
  3524. return -ERESTARTSYS;
  3525. rc = 0;
  3526. switch (fan_control_access_mode) {
  3527. case TPACPI_FAN_WR_ACPI_FANS:
  3528. case TPACPI_FAN_WR_TPEC:
  3529. if (!acpi_ec_write(fan_status_offset, 0x00))
  3530. rc = -EIO;
  3531. else {
  3532. fan_control_desired_level = 0;
  3533. tp_features.fan_ctrl_status_undef = 0;
  3534. }
  3535. break;
  3536. case TPACPI_FAN_WR_ACPI_SFAN:
  3537. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  3538. rc = -EIO;
  3539. else
  3540. fan_control_desired_level = 0;
  3541. break;
  3542. default:
  3543. rc = -ENXIO;
  3544. }
  3545. mutex_unlock(&fan_mutex);
  3546. return rc;
  3547. }
  3548. static int fan_set_speed(int speed)
  3549. {
  3550. int rc;
  3551. if (!fan_control_allowed)
  3552. return -EPERM;
  3553. if (mutex_lock_interruptible(&fan_mutex))
  3554. return -ERESTARTSYS;
  3555. rc = 0;
  3556. switch (fan_control_access_mode) {
  3557. case TPACPI_FAN_WR_ACPI_FANS:
  3558. if (speed >= 0 && speed <= 65535) {
  3559. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  3560. speed, speed, speed))
  3561. rc = -EIO;
  3562. } else
  3563. rc = -EINVAL;
  3564. break;
  3565. default:
  3566. rc = -ENXIO;
  3567. }
  3568. mutex_unlock(&fan_mutex);
  3569. return rc;
  3570. }
  3571. static int fan_read(char *p)
  3572. {
  3573. int len = 0;
  3574. int rc;
  3575. u8 status;
  3576. unsigned int speed = 0;
  3577. switch (fan_status_access_mode) {
  3578. case TPACPI_FAN_RD_ACPI_GFAN:
  3579. /* 570, 600e/x, 770e, 770x */
  3580. if ((rc = fan_get_status_safe(&status)) < 0)
  3581. return rc;
  3582. len += sprintf(p + len, "status:\t\t%s\n"
  3583. "level:\t\t%d\n",
  3584. (status != 0) ? "enabled" : "disabled", status);
  3585. break;
  3586. case TPACPI_FAN_RD_TPEC:
  3587. /* all except 570, 600e/x, 770e, 770x */
  3588. if ((rc = fan_get_status_safe(&status)) < 0)
  3589. return rc;
  3590. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3591. if (status != fan_control_initial_status)
  3592. tp_features.fan_ctrl_status_undef = 0;
  3593. else
  3594. /* Return most likely status. In fact, it
  3595. * might be the only possible status */
  3596. status = TP_EC_FAN_AUTO;
  3597. }
  3598. len += sprintf(p + len, "status:\t\t%s\n",
  3599. (status != 0) ? "enabled" : "disabled");
  3600. if ((rc = fan_get_speed(&speed)) < 0)
  3601. return rc;
  3602. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  3603. if (status & TP_EC_FAN_FULLSPEED)
  3604. /* Disengaged mode takes precedence */
  3605. len += sprintf(p + len, "level:\t\tdisengaged\n");
  3606. else if (status & TP_EC_FAN_AUTO)
  3607. len += sprintf(p + len, "level:\t\tauto\n");
  3608. else
  3609. len += sprintf(p + len, "level:\t\t%d\n", status);
  3610. break;
  3611. case TPACPI_FAN_NONE:
  3612. default:
  3613. len += sprintf(p + len, "status:\t\tnot supported\n");
  3614. }
  3615. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  3616. len += sprintf(p + len, "commands:\tlevel <level>");
  3617. switch (fan_control_access_mode) {
  3618. case TPACPI_FAN_WR_ACPI_SFAN:
  3619. len += sprintf(p + len, " (<level> is 0-7)\n");
  3620. break;
  3621. default:
  3622. len += sprintf(p + len, " (<level> is 0-7, "
  3623. "auto, disengaged, full-speed)\n");
  3624. break;
  3625. }
  3626. }
  3627. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  3628. len += sprintf(p + len, "commands:\tenable, disable\n"
  3629. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  3630. "1-120 (seconds))\n");
  3631. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  3632. len += sprintf(p + len, "commands:\tspeed <speed>"
  3633. " (<speed> is 0-65535)\n");
  3634. return len;
  3635. }
  3636. static int fan_write_cmd_level(const char *cmd, int *rc)
  3637. {
  3638. int level;
  3639. if (strlencmp(cmd, "level auto") == 0)
  3640. level = TP_EC_FAN_AUTO;
  3641. else if ((strlencmp(cmd, "level disengaged") == 0) |
  3642. (strlencmp(cmd, "level full-speed") == 0))
  3643. level = TP_EC_FAN_FULLSPEED;
  3644. else if (sscanf(cmd, "level %d", &level) != 1)
  3645. return 0;
  3646. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  3647. printk(IBM_ERR "level command accepted for unsupported "
  3648. "access mode %d", fan_control_access_mode);
  3649. return 1;
  3650. }
  3651. static int fan_write_cmd_enable(const char *cmd, int *rc)
  3652. {
  3653. if (strlencmp(cmd, "enable") != 0)
  3654. return 0;
  3655. if ((*rc = fan_set_enable()) == -ENXIO)
  3656. printk(IBM_ERR "enable command accepted for unsupported "
  3657. "access mode %d", fan_control_access_mode);
  3658. return 1;
  3659. }
  3660. static int fan_write_cmd_disable(const char *cmd, int *rc)
  3661. {
  3662. if (strlencmp(cmd, "disable") != 0)
  3663. return 0;
  3664. if ((*rc = fan_set_disable()) == -ENXIO)
  3665. printk(IBM_ERR "disable command accepted for unsupported "
  3666. "access mode %d", fan_control_access_mode);
  3667. return 1;
  3668. }
  3669. static int fan_write_cmd_speed(const char *cmd, int *rc)
  3670. {
  3671. int speed;
  3672. /* TODO:
  3673. * Support speed <low> <medium> <high> ? */
  3674. if (sscanf(cmd, "speed %d", &speed) != 1)
  3675. return 0;
  3676. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  3677. printk(IBM_ERR "speed command accepted for unsupported "
  3678. "access mode %d", fan_control_access_mode);
  3679. return 1;
  3680. }
  3681. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  3682. {
  3683. int interval;
  3684. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  3685. return 0;
  3686. if (interval < 0 || interval > 120)
  3687. *rc = -EINVAL;
  3688. else
  3689. fan_watchdog_maxinterval = interval;
  3690. return 1;
  3691. }
  3692. static int fan_write(char *buf)
  3693. {
  3694. char *cmd;
  3695. int rc = 0;
  3696. while (!rc && (cmd = next_cmd(&buf))) {
  3697. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  3698. fan_write_cmd_level(cmd, &rc)) &&
  3699. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  3700. (fan_write_cmd_enable(cmd, &rc) ||
  3701. fan_write_cmd_disable(cmd, &rc) ||
  3702. fan_write_cmd_watchdog(cmd, &rc))) &&
  3703. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  3704. fan_write_cmd_speed(cmd, &rc))
  3705. )
  3706. rc = -EINVAL;
  3707. else if (!rc)
  3708. fan_watchdog_reset();
  3709. }
  3710. return rc;
  3711. }
  3712. static struct ibm_struct fan_driver_data = {
  3713. .name = "fan",
  3714. .read = fan_read,
  3715. .write = fan_write,
  3716. .exit = fan_exit,
  3717. };
  3718. /****************************************************************************
  3719. ****************************************************************************
  3720. *
  3721. * Infrastructure
  3722. *
  3723. ****************************************************************************
  3724. ****************************************************************************/
  3725. /* sysfs name ---------------------------------------------------------- */
  3726. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  3727. struct device_attribute *attr,
  3728. char *buf)
  3729. {
  3730. return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
  3731. }
  3732. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  3733. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  3734. /* --------------------------------------------------------------------- */
  3735. /* /proc support */
  3736. static struct proc_dir_entry *proc_dir;
  3737. /* Subdriver registry */
  3738. static LIST_HEAD(tpacpi_all_drivers);
  3739. /*
  3740. * Module and infrastructure proble, init and exit handling
  3741. */
  3742. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  3743. static const char * __init str_supported(int is_supported)
  3744. {
  3745. static char text_unsupported[] __initdata = "not supported";
  3746. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  3747. }
  3748. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  3749. static int __init ibm_init(struct ibm_init_struct *iibm)
  3750. {
  3751. int ret;
  3752. struct ibm_struct *ibm = iibm->data;
  3753. struct proc_dir_entry *entry;
  3754. BUG_ON(ibm == NULL);
  3755. INIT_LIST_HEAD(&ibm->all_drivers);
  3756. if (ibm->flags.experimental && !experimental)
  3757. return 0;
  3758. dbg_printk(TPACPI_DBG_INIT,
  3759. "probing for %s\n", ibm->name);
  3760. if (iibm->init) {
  3761. ret = iibm->init(iibm);
  3762. if (ret > 0)
  3763. return 0; /* probe failed */
  3764. if (ret)
  3765. return ret;
  3766. ibm->flags.init_called = 1;
  3767. }
  3768. if (ibm->acpi) {
  3769. if (ibm->acpi->hid) {
  3770. ret = register_tpacpi_subdriver(ibm);
  3771. if (ret)
  3772. goto err_out;
  3773. }
  3774. if (ibm->acpi->notify) {
  3775. ret = setup_acpi_notify(ibm);
  3776. if (ret == -ENODEV) {
  3777. printk(IBM_NOTICE "disabling subdriver %s\n",
  3778. ibm->name);
  3779. ret = 0;
  3780. goto err_out;
  3781. }
  3782. if (ret < 0)
  3783. goto err_out;
  3784. }
  3785. }
  3786. dbg_printk(TPACPI_DBG_INIT,
  3787. "%s installed\n", ibm->name);
  3788. if (ibm->read) {
  3789. entry = create_proc_entry(ibm->name,
  3790. S_IFREG | S_IRUGO | S_IWUSR,
  3791. proc_dir);
  3792. if (!entry) {
  3793. printk(IBM_ERR "unable to create proc entry %s\n",
  3794. ibm->name);
  3795. ret = -ENODEV;
  3796. goto err_out;
  3797. }
  3798. entry->owner = THIS_MODULE;
  3799. entry->data = ibm;
  3800. entry->read_proc = &dispatch_procfs_read;
  3801. if (ibm->write)
  3802. entry->write_proc = &dispatch_procfs_write;
  3803. ibm->flags.proc_created = 1;
  3804. }
  3805. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  3806. return 0;
  3807. err_out:
  3808. dbg_printk(TPACPI_DBG_INIT,
  3809. "%s: at error exit path with result %d\n",
  3810. ibm->name, ret);
  3811. ibm_exit(ibm);
  3812. return (ret < 0)? ret : 0;
  3813. }
  3814. static void ibm_exit(struct ibm_struct *ibm)
  3815. {
  3816. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  3817. list_del_init(&ibm->all_drivers);
  3818. if (ibm->flags.acpi_notify_installed) {
  3819. dbg_printk(TPACPI_DBG_EXIT,
  3820. "%s: acpi_remove_notify_handler\n", ibm->name);
  3821. BUG_ON(!ibm->acpi);
  3822. acpi_remove_notify_handler(*ibm->acpi->handle,
  3823. ibm->acpi->type,
  3824. dispatch_acpi_notify);
  3825. ibm->flags.acpi_notify_installed = 0;
  3826. ibm->flags.acpi_notify_installed = 0;
  3827. }
  3828. if (ibm->flags.proc_created) {
  3829. dbg_printk(TPACPI_DBG_EXIT,
  3830. "%s: remove_proc_entry\n", ibm->name);
  3831. remove_proc_entry(ibm->name, proc_dir);
  3832. ibm->flags.proc_created = 0;
  3833. }
  3834. if (ibm->flags.acpi_driver_registered) {
  3835. dbg_printk(TPACPI_DBG_EXIT,
  3836. "%s: acpi_bus_unregister_driver\n", ibm->name);
  3837. BUG_ON(!ibm->acpi);
  3838. acpi_bus_unregister_driver(ibm->acpi->driver);
  3839. kfree(ibm->acpi->driver);
  3840. ibm->acpi->driver = NULL;
  3841. ibm->flags.acpi_driver_registered = 0;
  3842. }
  3843. if (ibm->flags.init_called && ibm->exit) {
  3844. ibm->exit();
  3845. ibm->flags.init_called = 0;
  3846. }
  3847. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  3848. }
  3849. /* Probing */
  3850. static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
  3851. {
  3852. const struct dmi_device *dev = NULL;
  3853. char ec_fw_string[18];
  3854. if (!tp)
  3855. return;
  3856. memset(tp, 0, sizeof(*tp));
  3857. if (dmi_name_in_vendors("IBM"))
  3858. tp->vendor = PCI_VENDOR_ID_IBM;
  3859. else if (dmi_name_in_vendors("LENOVO"))
  3860. tp->vendor = PCI_VENDOR_ID_LENOVO;
  3861. else
  3862. return;
  3863. tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
  3864. GFP_KERNEL);
  3865. if (!tp->bios_version_str)
  3866. return;
  3867. tp->bios_model = tp->bios_version_str[0]
  3868. | (tp->bios_version_str[1] << 8);
  3869. /*
  3870. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  3871. * X32 or newer, all Z series; Some models must have an
  3872. * up-to-date BIOS or they will not be detected.
  3873. *
  3874. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  3875. */
  3876. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  3877. if (sscanf(dev->name,
  3878. "IBM ThinkPad Embedded Controller -[%17c",
  3879. ec_fw_string) == 1) {
  3880. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  3881. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  3882. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  3883. tp->ec_model = ec_fw_string[0]
  3884. | (ec_fw_string[1] << 8);
  3885. break;
  3886. }
  3887. }
  3888. tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
  3889. GFP_KERNEL);
  3890. if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
  3891. kfree(tp->model_str);
  3892. tp->model_str = NULL;
  3893. }
  3894. }
  3895. static int __init probe_for_thinkpad(void)
  3896. {
  3897. int is_thinkpad;
  3898. if (acpi_disabled)
  3899. return -ENODEV;
  3900. /*
  3901. * Non-ancient models have better DMI tagging, but very old models
  3902. * don't.
  3903. */
  3904. is_thinkpad = (thinkpad_id.model_str != NULL);
  3905. /* ec is required because many other handles are relative to it */
  3906. IBM_ACPIHANDLE_INIT(ec);
  3907. if (!ec_handle) {
  3908. if (is_thinkpad)
  3909. printk(IBM_ERR
  3910. "Not yet supported ThinkPad detected!\n");
  3911. return -ENODEV;
  3912. }
  3913. /*
  3914. * Risks a regression on very old machines, but reduces potential
  3915. * false positives a damn great deal
  3916. */
  3917. if (!is_thinkpad)
  3918. is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
  3919. if (!is_thinkpad && !force_load)
  3920. return -ENODEV;
  3921. return 0;
  3922. }
  3923. /* Module init, exit, parameters */
  3924. static struct ibm_init_struct ibms_init[] __initdata = {
  3925. {
  3926. .init = thinkpad_acpi_driver_init,
  3927. .data = &thinkpad_acpi_driver_data,
  3928. },
  3929. {
  3930. .init = hotkey_init,
  3931. .data = &hotkey_driver_data,
  3932. },
  3933. {
  3934. .init = bluetooth_init,
  3935. .data = &bluetooth_driver_data,
  3936. },
  3937. {
  3938. .init = wan_init,
  3939. .data = &wan_driver_data,
  3940. },
  3941. {
  3942. .init = video_init,
  3943. .data = &video_driver_data,
  3944. },
  3945. {
  3946. .init = light_init,
  3947. .data = &light_driver_data,
  3948. },
  3949. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3950. {
  3951. .init = dock_init,
  3952. .data = &dock_driver_data[0],
  3953. },
  3954. {
  3955. .init = dock_init2,
  3956. .data = &dock_driver_data[1],
  3957. },
  3958. #endif
  3959. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3960. {
  3961. .init = bay_init,
  3962. .data = &bay_driver_data,
  3963. },
  3964. #endif
  3965. {
  3966. .init = cmos_init,
  3967. .data = &cmos_driver_data,
  3968. },
  3969. {
  3970. .init = led_init,
  3971. .data = &led_driver_data,
  3972. },
  3973. {
  3974. .init = beep_init,
  3975. .data = &beep_driver_data,
  3976. },
  3977. {
  3978. .init = thermal_init,
  3979. .data = &thermal_driver_data,
  3980. },
  3981. {
  3982. .data = &ecdump_driver_data,
  3983. },
  3984. {
  3985. .init = brightness_init,
  3986. .data = &brightness_driver_data,
  3987. },
  3988. {
  3989. .data = &volume_driver_data,
  3990. },
  3991. {
  3992. .init = fan_init,
  3993. .data = &fan_driver_data,
  3994. },
  3995. };
  3996. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  3997. {
  3998. unsigned int i;
  3999. struct ibm_struct *ibm;
  4000. if (!kp || !kp->name || !val)
  4001. return -EINVAL;
  4002. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  4003. ibm = ibms_init[i].data;
  4004. WARN_ON(ibm == NULL);
  4005. if (!ibm || !ibm->name)
  4006. continue;
  4007. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  4008. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  4009. return -ENOSPC;
  4010. strcpy(ibms_init[i].param, val);
  4011. strcat(ibms_init[i].param, ",");
  4012. return 0;
  4013. }
  4014. }
  4015. return -EINVAL;
  4016. }
  4017. static int experimental;
  4018. module_param(experimental, int, 0);
  4019. static u32 dbg_level;
  4020. module_param_named(debug, dbg_level, uint, 0);
  4021. static int force_load;
  4022. module_param(force_load, bool, 0);
  4023. static int fan_control_allowed;
  4024. module_param_named(fan_control, fan_control_allowed, bool, 0);
  4025. static int brightness_mode;
  4026. module_param_named(brightness_mode, brightness_mode, int, 0);
  4027. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  4028. module_param(brightness_enable, uint, 0);
  4029. static unsigned int hotkey_report_mode;
  4030. module_param(hotkey_report_mode, uint, 0);
  4031. #define IBM_PARAM(feature) \
  4032. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  4033. IBM_PARAM(hotkey);
  4034. IBM_PARAM(bluetooth);
  4035. IBM_PARAM(video);
  4036. IBM_PARAM(light);
  4037. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  4038. IBM_PARAM(dock);
  4039. #endif
  4040. #ifdef CONFIG_THINKPAD_ACPI_BAY
  4041. IBM_PARAM(bay);
  4042. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  4043. IBM_PARAM(cmos);
  4044. IBM_PARAM(led);
  4045. IBM_PARAM(beep);
  4046. IBM_PARAM(ecdump);
  4047. IBM_PARAM(brightness);
  4048. IBM_PARAM(volume);
  4049. IBM_PARAM(fan);
  4050. static int __init thinkpad_acpi_module_init(void)
  4051. {
  4052. int ret, i;
  4053. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  4054. /* Parameter checking */
  4055. if (hotkey_report_mode > 2)
  4056. return -EINVAL;
  4057. /* Driver-level probe */
  4058. get_thinkpad_model_data(&thinkpad_id);
  4059. ret = probe_for_thinkpad();
  4060. if (ret) {
  4061. thinkpad_acpi_module_exit();
  4062. return ret;
  4063. }
  4064. /* Driver initialization */
  4065. IBM_ACPIHANDLE_INIT(ecrd);
  4066. IBM_ACPIHANDLE_INIT(ecwr);
  4067. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  4068. if (!proc_dir) {
  4069. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  4070. thinkpad_acpi_module_exit();
  4071. return -ENODEV;
  4072. }
  4073. proc_dir->owner = THIS_MODULE;
  4074. ret = platform_driver_register(&tpacpi_pdriver);
  4075. if (ret) {
  4076. printk(IBM_ERR "unable to register main platform driver\n");
  4077. thinkpad_acpi_module_exit();
  4078. return ret;
  4079. }
  4080. tp_features.platform_drv_registered = 1;
  4081. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  4082. if (ret) {
  4083. printk(IBM_ERR "unable to register hwmon platform driver\n");
  4084. thinkpad_acpi_module_exit();
  4085. return ret;
  4086. }
  4087. tp_features.sensors_pdrv_registered = 1;
  4088. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  4089. if (!ret) {
  4090. tp_features.platform_drv_attrs_registered = 1;
  4091. ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  4092. }
  4093. if (ret) {
  4094. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  4095. thinkpad_acpi_module_exit();
  4096. return ret;
  4097. }
  4098. tp_features.sensors_pdrv_attrs_registered = 1;
  4099. /* Device initialization */
  4100. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  4101. NULL, 0);
  4102. if (IS_ERR(tpacpi_pdev)) {
  4103. ret = PTR_ERR(tpacpi_pdev);
  4104. tpacpi_pdev = NULL;
  4105. printk(IBM_ERR "unable to register platform device\n");
  4106. thinkpad_acpi_module_exit();
  4107. return ret;
  4108. }
  4109. tpacpi_sensors_pdev = platform_device_register_simple(
  4110. IBM_HWMON_DRVR_NAME,
  4111. -1, NULL, 0);
  4112. if (IS_ERR(tpacpi_sensors_pdev)) {
  4113. ret = PTR_ERR(tpacpi_sensors_pdev);
  4114. tpacpi_sensors_pdev = NULL;
  4115. printk(IBM_ERR "unable to register hwmon platform device\n");
  4116. thinkpad_acpi_module_exit();
  4117. return ret;
  4118. }
  4119. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  4120. &dev_attr_thinkpad_acpi_pdev_name);
  4121. if (ret) {
  4122. printk(IBM_ERR
  4123. "unable to create sysfs hwmon device attributes\n");
  4124. thinkpad_acpi_module_exit();
  4125. return ret;
  4126. }
  4127. tp_features.sensors_pdev_attrs_registered = 1;
  4128. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  4129. if (IS_ERR(tpacpi_hwmon)) {
  4130. ret = PTR_ERR(tpacpi_hwmon);
  4131. tpacpi_hwmon = NULL;
  4132. printk(IBM_ERR "unable to register hwmon device\n");
  4133. thinkpad_acpi_module_exit();
  4134. return ret;
  4135. }
  4136. mutex_init(&tpacpi_inputdev_send_mutex);
  4137. tpacpi_inputdev = input_allocate_device();
  4138. if (!tpacpi_inputdev) {
  4139. printk(IBM_ERR "unable to allocate input device\n");
  4140. thinkpad_acpi_module_exit();
  4141. return -ENOMEM;
  4142. } else {
  4143. /* Prepare input device, but don't register */
  4144. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  4145. tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
  4146. tpacpi_inputdev->id.bustype = BUS_HOST;
  4147. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  4148. thinkpad_id.vendor :
  4149. PCI_VENDOR_ID_IBM;
  4150. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  4151. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  4152. }
  4153. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  4154. ret = ibm_init(&ibms_init[i]);
  4155. if (ret >= 0 && *ibms_init[i].param)
  4156. ret = ibms_init[i].data->write(ibms_init[i].param);
  4157. if (ret < 0) {
  4158. thinkpad_acpi_module_exit();
  4159. return ret;
  4160. }
  4161. }
  4162. ret = input_register_device(tpacpi_inputdev);
  4163. if (ret < 0) {
  4164. printk(IBM_ERR "unable to register input device\n");
  4165. thinkpad_acpi_module_exit();
  4166. return ret;
  4167. } else {
  4168. tp_features.input_device_registered = 1;
  4169. }
  4170. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  4171. return 0;
  4172. }
  4173. static void thinkpad_acpi_module_exit(void)
  4174. {
  4175. struct ibm_struct *ibm, *itmp;
  4176. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  4177. list_for_each_entry_safe_reverse(ibm, itmp,
  4178. &tpacpi_all_drivers,
  4179. all_drivers) {
  4180. ibm_exit(ibm);
  4181. }
  4182. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  4183. if (tpacpi_inputdev) {
  4184. if (tp_features.input_device_registered)
  4185. input_unregister_device(tpacpi_inputdev);
  4186. else
  4187. input_free_device(tpacpi_inputdev);
  4188. }
  4189. if (tpacpi_hwmon)
  4190. hwmon_device_unregister(tpacpi_hwmon);
  4191. if (tp_features.sensors_pdev_attrs_registered)
  4192. device_remove_file(&tpacpi_sensors_pdev->dev,
  4193. &dev_attr_thinkpad_acpi_pdev_name);
  4194. if (tpacpi_sensors_pdev)
  4195. platform_device_unregister(tpacpi_sensors_pdev);
  4196. if (tpacpi_pdev)
  4197. platform_device_unregister(tpacpi_pdev);
  4198. if (tp_features.sensors_pdrv_attrs_registered)
  4199. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  4200. if (tp_features.platform_drv_attrs_registered)
  4201. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  4202. if (tp_features.sensors_pdrv_registered)
  4203. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  4204. if (tp_features.platform_drv_registered)
  4205. platform_driver_unregister(&tpacpi_pdriver);
  4206. if (proc_dir)
  4207. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  4208. kfree(thinkpad_id.bios_version_str);
  4209. kfree(thinkpad_id.ec_version_str);
  4210. kfree(thinkpad_id.model_str);
  4211. }
  4212. module_init(thinkpad_acpi_module_init);
  4213. module_exit(thinkpad_acpi_module_exit);