thinkpad_acpi.c 120 KB

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