thinkpad_acpi.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566
  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.14"
  24. #define TPACPI_SYSFS_VERSION 0x000200
  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. /****************************************************************************
  111. ****************************************************************************
  112. *
  113. * ACPI Helpers and device model
  114. *
  115. ****************************************************************************
  116. ****************************************************************************/
  117. /*************************************************************************
  118. * ACPI basic handles
  119. */
  120. static acpi_handle root_handle;
  121. #define IBM_HANDLE(object, parent, paths...) \
  122. static acpi_handle object##_handle; \
  123. static acpi_handle *object##_parent = &parent##_handle; \
  124. static char *object##_path; \
  125. static char *object##_paths[] = { paths }
  126. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  127. "\\_SB.PCI.ISA.EC", /* 570 */
  128. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  129. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  130. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  131. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  132. "\\_SB.PCI0.LPC.EC", /* all others */
  133. );
  134. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  135. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  136. /*************************************************************************
  137. * Misc ACPI handles
  138. */
  139. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  140. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  141. "\\CMS", /* R40, R40e */
  142. ); /* all others */
  143. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  144. "^HKEY", /* R30, R31 */
  145. "HKEY", /* all others */
  146. ); /* 570 */
  147. /*************************************************************************
  148. * ACPI helpers
  149. */
  150. static int acpi_evalf(acpi_handle handle,
  151. void *res, char *method, char *fmt, ...)
  152. {
  153. char *fmt0 = fmt;
  154. struct acpi_object_list params;
  155. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  156. struct acpi_buffer result, *resultp;
  157. union acpi_object out_obj;
  158. acpi_status status;
  159. va_list ap;
  160. char res_type;
  161. int success;
  162. int quiet;
  163. if (!*fmt) {
  164. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  165. return 0;
  166. }
  167. if (*fmt == 'q') {
  168. quiet = 1;
  169. fmt++;
  170. } else
  171. quiet = 0;
  172. res_type = *(fmt++);
  173. params.count = 0;
  174. params.pointer = &in_objs[0];
  175. va_start(ap, fmt);
  176. while (*fmt) {
  177. char c = *(fmt++);
  178. switch (c) {
  179. case 'd': /* int */
  180. in_objs[params.count].integer.value = va_arg(ap, int);
  181. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  182. break;
  183. /* add more types as needed */
  184. default:
  185. printk(IBM_ERR "acpi_evalf() called "
  186. "with invalid format character '%c'\n", c);
  187. return 0;
  188. }
  189. }
  190. va_end(ap);
  191. if (res_type != 'v') {
  192. result.length = sizeof(out_obj);
  193. result.pointer = &out_obj;
  194. resultp = &result;
  195. } else
  196. resultp = NULL;
  197. status = acpi_evaluate_object(handle, method, &params, resultp);
  198. switch (res_type) {
  199. case 'd': /* int */
  200. if (res)
  201. *(int *)res = out_obj.integer.value;
  202. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  203. break;
  204. case 'v': /* void */
  205. success = status == AE_OK;
  206. break;
  207. /* add more types as needed */
  208. default:
  209. printk(IBM_ERR "acpi_evalf() called "
  210. "with invalid format character '%c'\n", res_type);
  211. return 0;
  212. }
  213. if (!success && !quiet)
  214. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  215. method, fmt0, status);
  216. return success;
  217. }
  218. static void __unused acpi_print_int(acpi_handle handle, char *method)
  219. {
  220. int i;
  221. if (acpi_evalf(handle, &i, method, "d"))
  222. printk(IBM_INFO "%s = 0x%x\n", method, i);
  223. else
  224. printk(IBM_ERR "error calling %s\n", method);
  225. }
  226. static int acpi_ec_read(int i, u8 * p)
  227. {
  228. int v;
  229. if (ecrd_handle) {
  230. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  231. return 0;
  232. *p = v;
  233. } else {
  234. if (ec_read(i, p) < 0)
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. static int acpi_ec_write(int i, u8 v)
  240. {
  241. if (ecwr_handle) {
  242. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  243. return 0;
  244. } else {
  245. if (ec_write(i, v) < 0)
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. static int _sta(acpi_handle handle)
  251. {
  252. int status;
  253. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  254. status = 0;
  255. return status;
  256. }
  257. static int issue_thinkpad_cmos_command(int cmos_cmd)
  258. {
  259. if (!cmos_handle)
  260. return -ENXIO;
  261. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  262. return -EIO;
  263. return 0;
  264. }
  265. /*************************************************************************
  266. * ACPI device model
  267. */
  268. static void drv_acpi_handle_init(char *name,
  269. acpi_handle *handle, acpi_handle parent,
  270. char **paths, int num_paths, char **path)
  271. {
  272. int i;
  273. acpi_status status;
  274. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  275. name);
  276. for (i = 0; i < num_paths; i++) {
  277. status = acpi_get_handle(parent, paths[i], handle);
  278. if (ACPI_SUCCESS(status)) {
  279. *path = paths[i];
  280. dbg_printk(TPACPI_DBG_INIT,
  281. "Found ACPI handle %s for %s\n",
  282. *path, name);
  283. return;
  284. }
  285. }
  286. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  287. name);
  288. *handle = NULL;
  289. }
  290. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  291. {
  292. struct ibm_struct *ibm = data;
  293. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  294. return;
  295. ibm->acpi->notify(ibm, event);
  296. }
  297. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  298. {
  299. acpi_status status;
  300. int rc;
  301. BUG_ON(!ibm->acpi);
  302. if (!*ibm->acpi->handle)
  303. return 0;
  304. vdbg_printk(TPACPI_DBG_INIT,
  305. "setting up ACPI notify for %s\n", ibm->name);
  306. rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  307. if (rc < 0) {
  308. printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
  309. ibm->name, rc);
  310. return -ENODEV;
  311. }
  312. acpi_driver_data(ibm->acpi->device) = ibm;
  313. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  314. IBM_ACPI_EVENT_PREFIX,
  315. ibm->name);
  316. status = acpi_install_notify_handler(*ibm->acpi->handle,
  317. ibm->acpi->type, dispatch_acpi_notify, ibm);
  318. if (ACPI_FAILURE(status)) {
  319. if (status == AE_ALREADY_EXISTS) {
  320. printk(IBM_NOTICE "another device driver is already handling %s events\n",
  321. ibm->name);
  322. } else {
  323. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  324. ibm->name, status);
  325. }
  326. return -ENODEV;
  327. }
  328. ibm->flags.acpi_notify_installed = 1;
  329. return 0;
  330. }
  331. static int __init tpacpi_device_add(struct acpi_device *device)
  332. {
  333. return 0;
  334. }
  335. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  336. {
  337. int rc;
  338. dbg_printk(TPACPI_DBG_INIT,
  339. "registering %s as an ACPI driver\n", ibm->name);
  340. BUG_ON(!ibm->acpi);
  341. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  342. if (!ibm->acpi->driver) {
  343. printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
  344. return -ENOMEM;
  345. }
  346. sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
  347. ibm->acpi->driver->ids = ibm->acpi->hid;
  348. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  349. rc = acpi_bus_register_driver(ibm->acpi->driver);
  350. if (rc < 0) {
  351. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  352. ibm->acpi->hid, rc);
  353. kfree(ibm->acpi->driver);
  354. ibm->acpi->driver = NULL;
  355. } else if (!rc)
  356. ibm->flags.acpi_driver_registered = 1;
  357. return rc;
  358. }
  359. /****************************************************************************
  360. ****************************************************************************
  361. *
  362. * Procfs Helpers
  363. *
  364. ****************************************************************************
  365. ****************************************************************************/
  366. static int dispatch_procfs_read(char *page, char **start, off_t off,
  367. int count, int *eof, void *data)
  368. {
  369. struct ibm_struct *ibm = data;
  370. int len;
  371. if (!ibm || !ibm->read)
  372. return -EINVAL;
  373. len = ibm->read(page);
  374. if (len < 0)
  375. return len;
  376. if (len <= off + count)
  377. *eof = 1;
  378. *start = page + off;
  379. len -= off;
  380. if (len > count)
  381. len = count;
  382. if (len < 0)
  383. len = 0;
  384. return len;
  385. }
  386. static int dispatch_procfs_write(struct file *file,
  387. const char __user * userbuf,
  388. unsigned long count, void *data)
  389. {
  390. struct ibm_struct *ibm = data;
  391. char *kernbuf;
  392. int ret;
  393. if (!ibm || !ibm->write)
  394. return -EINVAL;
  395. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  396. if (!kernbuf)
  397. return -ENOMEM;
  398. if (copy_from_user(kernbuf, userbuf, count)) {
  399. kfree(kernbuf);
  400. return -EFAULT;
  401. }
  402. kernbuf[count] = 0;
  403. strcat(kernbuf, ",");
  404. ret = ibm->write(kernbuf);
  405. if (ret == 0)
  406. ret = count;
  407. kfree(kernbuf);
  408. return ret;
  409. }
  410. static char *next_cmd(char **cmds)
  411. {
  412. char *start = *cmds;
  413. char *end;
  414. while ((end = strchr(start, ',')) && end == start)
  415. start = end + 1;
  416. if (!end)
  417. return NULL;
  418. *end = 0;
  419. *cmds = end + 1;
  420. return start;
  421. }
  422. /****************************************************************************
  423. ****************************************************************************
  424. *
  425. * Device model: input, hwmon and platform
  426. *
  427. ****************************************************************************
  428. ****************************************************************************/
  429. static struct platform_device *tpacpi_pdev;
  430. static struct class_device *tpacpi_hwmon;
  431. static struct input_dev *tpacpi_inputdev;
  432. static int tpacpi_resume_handler(struct platform_device *pdev)
  433. {
  434. struct ibm_struct *ibm, *itmp;
  435. list_for_each_entry_safe(ibm, itmp,
  436. &tpacpi_all_drivers,
  437. all_drivers) {
  438. if (ibm->resume)
  439. (ibm->resume)();
  440. }
  441. return 0;
  442. }
  443. static struct platform_driver tpacpi_pdriver = {
  444. .driver = {
  445. .name = IBM_DRVR_NAME,
  446. .owner = THIS_MODULE,
  447. },
  448. .resume = tpacpi_resume_handler,
  449. };
  450. /*************************************************************************
  451. * thinkpad-acpi driver attributes
  452. */
  453. /* interface_version --------------------------------------------------- */
  454. static ssize_t tpacpi_driver_interface_version_show(
  455. struct device_driver *drv,
  456. char *buf)
  457. {
  458. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  459. }
  460. static DRIVER_ATTR(interface_version, S_IRUGO,
  461. tpacpi_driver_interface_version_show, NULL);
  462. /* debug_level --------------------------------------------------------- */
  463. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  464. char *buf)
  465. {
  466. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  467. }
  468. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  469. const char *buf, size_t count)
  470. {
  471. unsigned long t;
  472. if (parse_strtoul(buf, 0xffff, &t))
  473. return -EINVAL;
  474. dbg_level = t;
  475. return count;
  476. }
  477. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  478. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  479. /* version ------------------------------------------------------------- */
  480. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  481. char *buf)
  482. {
  483. return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
  484. }
  485. static DRIVER_ATTR(version, S_IRUGO,
  486. tpacpi_driver_version_show, NULL);
  487. /* --------------------------------------------------------------------- */
  488. static struct driver_attribute* tpacpi_driver_attributes[] = {
  489. &driver_attr_debug_level, &driver_attr_version,
  490. &driver_attr_interface_version,
  491. };
  492. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  493. {
  494. int i, res;
  495. i = 0;
  496. res = 0;
  497. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  498. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  499. i++;
  500. }
  501. return res;
  502. }
  503. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  504. {
  505. int i;
  506. for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  507. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  508. }
  509. /*************************************************************************
  510. * sysfs support helpers
  511. */
  512. struct attribute_set_obj {
  513. struct attribute_set s;
  514. struct attribute *a;
  515. } __attribute__((packed));
  516. static struct attribute_set *create_attr_set(unsigned int max_members,
  517. const char* name)
  518. {
  519. struct attribute_set_obj *sobj;
  520. if (max_members == 0)
  521. return NULL;
  522. /* Allocates space for implicit NULL at the end too */
  523. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  524. max_members * sizeof(struct attribute *),
  525. GFP_KERNEL);
  526. if (!sobj)
  527. return NULL;
  528. sobj->s.max_members = max_members;
  529. sobj->s.group.attrs = &sobj->a;
  530. sobj->s.group.name = name;
  531. return &sobj->s;
  532. }
  533. /* not multi-threaded safe, use it in a single thread per set */
  534. static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
  535. {
  536. if (!s || !attr)
  537. return -EINVAL;
  538. if (s->members >= s->max_members)
  539. return -ENOMEM;
  540. s->group.attrs[s->members] = attr;
  541. s->members++;
  542. return 0;
  543. }
  544. static int add_many_to_attr_set(struct attribute_set* s,
  545. struct attribute **attr,
  546. unsigned int count)
  547. {
  548. int i, res;
  549. for (i = 0; i < count; i++) {
  550. res = add_to_attr_set(s, attr[i]);
  551. if (res)
  552. return res;
  553. }
  554. return 0;
  555. }
  556. static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
  557. {
  558. sysfs_remove_group(kobj, &s->group);
  559. destroy_attr_set(s);
  560. }
  561. static int parse_strtoul(const char *buf,
  562. unsigned long max, unsigned long *value)
  563. {
  564. char *endp;
  565. *value = simple_strtoul(buf, &endp, 0);
  566. while (*endp && isspace(*endp))
  567. endp++;
  568. if (*endp || *value > max)
  569. return -EINVAL;
  570. return 0;
  571. }
  572. /****************************************************************************
  573. ****************************************************************************
  574. *
  575. * Subdrivers
  576. *
  577. ****************************************************************************
  578. ****************************************************************************/
  579. /*************************************************************************
  580. * thinkpad-acpi init subdriver
  581. */
  582. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  583. {
  584. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  585. printk(IBM_INFO "%s\n", IBM_URL);
  586. if (ibm_thinkpad_ec_found)
  587. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  588. ibm_thinkpad_ec_found);
  589. return 0;
  590. }
  591. static int thinkpad_acpi_driver_read(char *p)
  592. {
  593. int len = 0;
  594. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  595. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  596. return len;
  597. }
  598. static struct ibm_struct thinkpad_acpi_driver_data = {
  599. .name = "driver",
  600. .read = thinkpad_acpi_driver_read,
  601. };
  602. /*************************************************************************
  603. * Hotkey subdriver
  604. */
  605. static int hotkey_orig_status;
  606. static u32 hotkey_orig_mask;
  607. static u32 hotkey_all_mask;
  608. static u32 hotkey_reserved_mask;
  609. static u16 hotkey_keycode_map[] = {
  610. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  611. KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
  612. KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
  613. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  614. /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
  615. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  616. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  617. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  618. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  619. /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
  620. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  621. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  622. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  623. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  624. KEY_RESERVED, /* 0x14: VOLUME UP */
  625. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  626. KEY_RESERVED, /* 0x16: MUTE */
  627. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  628. /* (assignments unknown, please report if found) */
  629. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  630. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  631. };
  632. static struct attribute_set *hotkey_dev_attributes;
  633. static int hotkey_get_wlsw(int *status)
  634. {
  635. if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
  636. return -EIO;
  637. return 0;
  638. }
  639. /* sysfs hotkey enable ------------------------------------------------- */
  640. static ssize_t hotkey_enable_show(struct device *dev,
  641. struct device_attribute *attr,
  642. char *buf)
  643. {
  644. int res, status;
  645. u32 mask;
  646. res = hotkey_get(&status, &mask);
  647. if (res)
  648. return res;
  649. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  650. }
  651. static ssize_t hotkey_enable_store(struct device *dev,
  652. struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. unsigned long t;
  656. int res, status;
  657. u32 mask;
  658. if (parse_strtoul(buf, 1, &t))
  659. return -EINVAL;
  660. res = hotkey_get(&status, &mask);
  661. if (!res)
  662. res = hotkey_set(t, mask);
  663. return (res) ? res : count;
  664. }
  665. static struct device_attribute dev_attr_hotkey_enable =
  666. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  667. hotkey_enable_show, hotkey_enable_store);
  668. /* sysfs hotkey mask --------------------------------------------------- */
  669. static ssize_t hotkey_mask_show(struct device *dev,
  670. struct device_attribute *attr,
  671. char *buf)
  672. {
  673. int res, status;
  674. u32 mask;
  675. res = hotkey_get(&status, &mask);
  676. if (res)
  677. return res;
  678. return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
  679. }
  680. static ssize_t hotkey_mask_store(struct device *dev,
  681. struct device_attribute *attr,
  682. const char *buf, size_t count)
  683. {
  684. unsigned long t;
  685. int res, status;
  686. u32 mask;
  687. if (parse_strtoul(buf, 0xffffffffUL, &t))
  688. return -EINVAL;
  689. res = hotkey_get(&status, &mask);
  690. if (!res)
  691. hotkey_set(status, t);
  692. return (res) ? res : count;
  693. }
  694. static struct device_attribute dev_attr_hotkey_mask =
  695. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  696. hotkey_mask_show, hotkey_mask_store);
  697. /* sysfs hotkey bios_enabled ------------------------------------------- */
  698. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  699. struct device_attribute *attr,
  700. char *buf)
  701. {
  702. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
  703. }
  704. static struct device_attribute dev_attr_hotkey_bios_enabled =
  705. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  706. /* sysfs hotkey bios_mask ---------------------------------------------- */
  707. static ssize_t hotkey_bios_mask_show(struct device *dev,
  708. struct device_attribute *attr,
  709. char *buf)
  710. {
  711. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  712. }
  713. static struct device_attribute dev_attr_hotkey_bios_mask =
  714. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  715. /* sysfs hotkey all_mask ----------------------------------------------- */
  716. static ssize_t hotkey_all_mask_show(struct device *dev,
  717. struct device_attribute *attr,
  718. char *buf)
  719. {
  720. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
  721. }
  722. static struct device_attribute dev_attr_hotkey_all_mask =
  723. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  724. /* sysfs hotkey recommended_mask --------------------------------------- */
  725. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  726. struct device_attribute *attr,
  727. char *buf)
  728. {
  729. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  730. hotkey_all_mask & ~hotkey_reserved_mask);
  731. }
  732. static struct device_attribute dev_attr_hotkey_recommended_mask =
  733. __ATTR(hotkey_recommended_mask, S_IRUGO,
  734. hotkey_recommended_mask_show, NULL);
  735. /* sysfs hotkey radio_sw ----------------------------------------------- */
  736. static ssize_t hotkey_radio_sw_show(struct device *dev,
  737. struct device_attribute *attr,
  738. char *buf)
  739. {
  740. int res, s;
  741. res = hotkey_get_wlsw(&s);
  742. if (res < 0)
  743. return res;
  744. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  745. }
  746. static struct device_attribute dev_attr_hotkey_radio_sw =
  747. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  748. /* --------------------------------------------------------------------- */
  749. static struct attribute *hotkey_mask_attributes[] = {
  750. &dev_attr_hotkey_mask.attr,
  751. &dev_attr_hotkey_bios_enabled.attr,
  752. &dev_attr_hotkey_bios_mask.attr,
  753. &dev_attr_hotkey_all_mask.attr,
  754. &dev_attr_hotkey_recommended_mask.attr,
  755. };
  756. static int __init hotkey_init(struct ibm_init_struct *iibm)
  757. {
  758. int res, i;
  759. int status;
  760. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  761. BUG_ON(!tpacpi_inputdev);
  762. IBM_ACPIHANDLE_INIT(hkey);
  763. mutex_init(&hotkey_mutex);
  764. /* hotkey not supported on 570 */
  765. tp_features.hotkey = hkey_handle != NULL;
  766. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  767. str_supported(tp_features.hotkey));
  768. if (tp_features.hotkey) {
  769. hotkey_dev_attributes = create_attr_set(7, NULL);
  770. if (!hotkey_dev_attributes)
  771. return -ENOMEM;
  772. res = add_to_attr_set(hotkey_dev_attributes,
  773. &dev_attr_hotkey_enable.attr);
  774. if (res)
  775. return res;
  776. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  777. A30, R30, R31, T20-22, X20-21, X22-24 */
  778. tp_features.hotkey_mask =
  779. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  780. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  781. str_supported(tp_features.hotkey_mask));
  782. if (tp_features.hotkey_mask) {
  783. /* MHKA available in A31, R40, R40e, T4x, X31, and later */
  784. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  785. "MHKA", "qd"))
  786. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  787. }
  788. res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
  789. if (!res && tp_features.hotkey_mask) {
  790. res = add_many_to_attr_set(hotkey_dev_attributes,
  791. hotkey_mask_attributes,
  792. ARRAY_SIZE(hotkey_mask_attributes));
  793. }
  794. /* Not all thinkpads have a hardware radio switch */
  795. if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  796. tp_features.hotkey_wlsw = 1;
  797. printk(IBM_INFO
  798. "radio switch found; radios are %s\n",
  799. enabled(status, 0));
  800. res = add_to_attr_set(hotkey_dev_attributes,
  801. &dev_attr_hotkey_radio_sw.attr);
  802. }
  803. if (!res)
  804. res = register_attr_set_with_sysfs(
  805. hotkey_dev_attributes,
  806. &tpacpi_pdev->dev.kobj);
  807. if (res)
  808. return res;
  809. #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
  810. for (i = 0; i < 12; i++)
  811. hotkey_keycode_map[i] = KEY_UNKNOWN;
  812. #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
  813. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  814. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  815. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  816. tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
  817. tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
  818. tpacpi_inputdev->keycode = &hotkey_keycode_map;
  819. for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
  820. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  821. set_bit(hotkey_keycode_map[i],
  822. tpacpi_inputdev->keybit);
  823. } else {
  824. if (i < sizeof(hotkey_reserved_mask)*8)
  825. hotkey_reserved_mask |= 1 << i;
  826. }
  827. }
  828. #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
  829. dbg_printk(TPACPI_DBG_INIT,
  830. "enabling hot key handling\n");
  831. res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
  832. | hotkey_orig_mask);
  833. if (res)
  834. return res;
  835. #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
  836. }
  837. return (tp_features.hotkey)? 0 : 1;
  838. }
  839. static void hotkey_exit(void)
  840. {
  841. int res;
  842. if (tp_features.hotkey) {
  843. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  844. res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  845. if (res)
  846. printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
  847. }
  848. if (hotkey_dev_attributes) {
  849. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  850. hotkey_dev_attributes = NULL;
  851. }
  852. }
  853. static void tpacpi_input_send_key(unsigned int scancode,
  854. unsigned int keycode)
  855. {
  856. if (keycode != KEY_RESERVED) {
  857. input_report_key(tpacpi_inputdev, keycode, 1);
  858. if (keycode == KEY_UNKNOWN)
  859. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  860. scancode);
  861. input_sync(tpacpi_inputdev);
  862. input_report_key(tpacpi_inputdev, keycode, 0);
  863. if (keycode == KEY_UNKNOWN)
  864. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  865. scancode);
  866. input_sync(tpacpi_inputdev);
  867. }
  868. }
  869. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  870. {
  871. u32 hkey;
  872. unsigned int keycode, scancode;
  873. int sendacpi = 1;
  874. if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  875. if (tpacpi_inputdev->users > 0) {
  876. switch (hkey >> 12) {
  877. case 1:
  878. /* 0x1000-0x1FFF: key presses */
  879. scancode = hkey & 0xfff;
  880. if (scancode > 0 && scancode < 0x21) {
  881. scancode--;
  882. keycode = hotkey_keycode_map[scancode];
  883. tpacpi_input_send_key(scancode, keycode);
  884. sendacpi = (keycode == KEY_RESERVED
  885. || keycode == KEY_UNKNOWN);
  886. } else {
  887. printk(IBM_ERR
  888. "hotkey 0x%04x out of range for keyboard map\n",
  889. hkey);
  890. }
  891. break;
  892. case 5:
  893. /* 0x5000-0x5FFF: LID */
  894. /* we don't handle it through this path, just
  895. * eat up known LID events */
  896. if (hkey != 0x5001 && hkey != 0x5002) {
  897. printk(IBM_ERR
  898. "unknown LID-related hotkey event: 0x%04x\n",
  899. hkey);
  900. }
  901. break;
  902. default:
  903. /* case 2: dock-related */
  904. /* 0x2305 - T43 waking up due to bay lever eject while aslept */
  905. /* case 3: ultra-bay related. maybe bay in dock? */
  906. /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
  907. printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
  908. }
  909. }
  910. if (sendacpi)
  911. acpi_bus_generate_event(ibm->acpi->device, event, hkey);
  912. } else {
  913. printk(IBM_ERR "unknown hotkey notification event %d\n", event);
  914. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  915. }
  916. }
  917. /*
  918. * Call with hotkey_mutex held
  919. */
  920. static int hotkey_get(int *status, u32 *mask)
  921. {
  922. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  923. return -EIO;
  924. if (tp_features.hotkey_mask)
  925. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  926. return -EIO;
  927. return 0;
  928. }
  929. /*
  930. * Call with hotkey_mutex held
  931. */
  932. static int hotkey_set(int status, u32 mask)
  933. {
  934. int i;
  935. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  936. return -EIO;
  937. if (tp_features.hotkey_mask)
  938. for (i = 0; i < 32; i++) {
  939. int bit = ((1 << i) & mask) != 0;
  940. if (!acpi_evalf(hkey_handle,
  941. NULL, "MHKM", "vdd", i + 1, bit))
  942. return -EIO;
  943. }
  944. return 0;
  945. }
  946. /* procfs -------------------------------------------------------------- */
  947. static int hotkey_read(char *p)
  948. {
  949. int res, status;
  950. u32 mask;
  951. int len = 0;
  952. if (!tp_features.hotkey) {
  953. len += sprintf(p + len, "status:\t\tnot supported\n");
  954. return len;
  955. }
  956. res = mutex_lock_interruptible(&hotkey_mutex);
  957. if (res < 0)
  958. return res;
  959. res = hotkey_get(&status, &mask);
  960. mutex_unlock(&hotkey_mutex);
  961. if (res)
  962. return res;
  963. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  964. if (tp_features.hotkey_mask) {
  965. len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
  966. len += sprintf(p + len,
  967. "commands:\tenable, disable, reset, <mask>\n");
  968. } else {
  969. len += sprintf(p + len, "mask:\t\tnot supported\n");
  970. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  971. }
  972. return len;
  973. }
  974. static int hotkey_write(char *buf)
  975. {
  976. int res, status;
  977. u32 mask;
  978. char *cmd;
  979. int do_cmd = 0;
  980. if (!tp_features.hotkey)
  981. return -ENODEV;
  982. res = mutex_lock_interruptible(&hotkey_mutex);
  983. if (res < 0)
  984. return res;
  985. res = hotkey_get(&status, &mask);
  986. if (res)
  987. goto errexit;
  988. res = 0;
  989. while ((cmd = next_cmd(&buf))) {
  990. if (strlencmp(cmd, "enable") == 0) {
  991. status = 1;
  992. } else if (strlencmp(cmd, "disable") == 0) {
  993. status = 0;
  994. } else if (strlencmp(cmd, "reset") == 0) {
  995. status = hotkey_orig_status;
  996. mask = hotkey_orig_mask;
  997. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  998. /* mask set */
  999. } else if (sscanf(cmd, "%x", &mask) == 1) {
  1000. /* mask set */
  1001. } else {
  1002. res = -EINVAL;
  1003. goto errexit;
  1004. }
  1005. do_cmd = 1;
  1006. }
  1007. if (do_cmd)
  1008. res = hotkey_set(status, mask);
  1009. errexit:
  1010. mutex_unlock(&hotkey_mutex);
  1011. return res;
  1012. }
  1013. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  1014. .hid = IBM_HKEY_HID,
  1015. .notify = hotkey_notify,
  1016. .handle = &hkey_handle,
  1017. .type = ACPI_DEVICE_NOTIFY,
  1018. };
  1019. static struct ibm_struct hotkey_driver_data = {
  1020. .name = "hotkey",
  1021. .read = hotkey_read,
  1022. .write = hotkey_write,
  1023. .exit = hotkey_exit,
  1024. .acpi = &ibm_hotkey_acpidriver,
  1025. };
  1026. /*************************************************************************
  1027. * Bluetooth subdriver
  1028. */
  1029. /* sysfs bluetooth enable ---------------------------------------------- */
  1030. static ssize_t bluetooth_enable_show(struct device *dev,
  1031. struct device_attribute *attr,
  1032. char *buf)
  1033. {
  1034. int status;
  1035. status = bluetooth_get_radiosw();
  1036. if (status < 0)
  1037. return status;
  1038. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1039. }
  1040. static ssize_t bluetooth_enable_store(struct device *dev,
  1041. struct device_attribute *attr,
  1042. const char *buf, size_t count)
  1043. {
  1044. unsigned long t;
  1045. int res;
  1046. if (parse_strtoul(buf, 1, &t))
  1047. return -EINVAL;
  1048. res = bluetooth_set_radiosw(t);
  1049. return (res) ? res : count;
  1050. }
  1051. static struct device_attribute dev_attr_bluetooth_enable =
  1052. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  1053. bluetooth_enable_show, bluetooth_enable_store);
  1054. /* --------------------------------------------------------------------- */
  1055. static struct attribute *bluetooth_attributes[] = {
  1056. &dev_attr_bluetooth_enable.attr,
  1057. NULL
  1058. };
  1059. static const struct attribute_group bluetooth_attr_group = {
  1060. .attrs = bluetooth_attributes,
  1061. };
  1062. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  1063. {
  1064. int res;
  1065. int status = 0;
  1066. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  1067. IBM_ACPIHANDLE_INIT(hkey);
  1068. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1069. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  1070. tp_features.bluetooth = hkey_handle &&
  1071. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  1072. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  1073. str_supported(tp_features.bluetooth),
  1074. status);
  1075. if (tp_features.bluetooth) {
  1076. if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  1077. /* no bluetooth hardware present in system */
  1078. tp_features.bluetooth = 0;
  1079. dbg_printk(TPACPI_DBG_INIT,
  1080. "bluetooth hardware not installed\n");
  1081. } else {
  1082. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1083. &bluetooth_attr_group);
  1084. if (res)
  1085. return res;
  1086. }
  1087. }
  1088. return (tp_features.bluetooth)? 0 : 1;
  1089. }
  1090. static void bluetooth_exit(void)
  1091. {
  1092. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1093. &bluetooth_attr_group);
  1094. }
  1095. static int bluetooth_get_radiosw(void)
  1096. {
  1097. int status;
  1098. if (!tp_features.bluetooth)
  1099. return -ENODEV;
  1100. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1101. return -EIO;
  1102. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  1103. }
  1104. static int bluetooth_set_radiosw(int radio_on)
  1105. {
  1106. int status;
  1107. if (!tp_features.bluetooth)
  1108. return -ENODEV;
  1109. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1110. return -EIO;
  1111. if (radio_on)
  1112. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  1113. else
  1114. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  1115. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  1116. return -EIO;
  1117. return 0;
  1118. }
  1119. /* procfs -------------------------------------------------------------- */
  1120. static int bluetooth_read(char *p)
  1121. {
  1122. int len = 0;
  1123. int status = bluetooth_get_radiosw();
  1124. if (!tp_features.bluetooth)
  1125. len += sprintf(p + len, "status:\t\tnot supported\n");
  1126. else {
  1127. len += sprintf(p + len, "status:\t\t%s\n",
  1128. (status)? "enabled" : "disabled");
  1129. len += sprintf(p + len, "commands:\tenable, disable\n");
  1130. }
  1131. return len;
  1132. }
  1133. static int bluetooth_write(char *buf)
  1134. {
  1135. char *cmd;
  1136. if (!tp_features.bluetooth)
  1137. return -ENODEV;
  1138. while ((cmd = next_cmd(&buf))) {
  1139. if (strlencmp(cmd, "enable") == 0) {
  1140. bluetooth_set_radiosw(1);
  1141. } else if (strlencmp(cmd, "disable") == 0) {
  1142. bluetooth_set_radiosw(0);
  1143. } else
  1144. return -EINVAL;
  1145. }
  1146. return 0;
  1147. }
  1148. static struct ibm_struct bluetooth_driver_data = {
  1149. .name = "bluetooth",
  1150. .read = bluetooth_read,
  1151. .write = bluetooth_write,
  1152. .exit = bluetooth_exit,
  1153. };
  1154. /*************************************************************************
  1155. * Wan subdriver
  1156. */
  1157. /* sysfs wan enable ---------------------------------------------------- */
  1158. static ssize_t wan_enable_show(struct device *dev,
  1159. struct device_attribute *attr,
  1160. char *buf)
  1161. {
  1162. int status;
  1163. status = wan_get_radiosw();
  1164. if (status < 0)
  1165. return status;
  1166. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1167. }
  1168. static ssize_t wan_enable_store(struct device *dev,
  1169. struct device_attribute *attr,
  1170. const char *buf, size_t count)
  1171. {
  1172. unsigned long t;
  1173. int res;
  1174. if (parse_strtoul(buf, 1, &t))
  1175. return -EINVAL;
  1176. res = wan_set_radiosw(t);
  1177. return (res) ? res : count;
  1178. }
  1179. static struct device_attribute dev_attr_wan_enable =
  1180. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  1181. wan_enable_show, wan_enable_store);
  1182. /* --------------------------------------------------------------------- */
  1183. static struct attribute *wan_attributes[] = {
  1184. &dev_attr_wan_enable.attr,
  1185. NULL
  1186. };
  1187. static const struct attribute_group wan_attr_group = {
  1188. .attrs = wan_attributes,
  1189. };
  1190. static int __init wan_init(struct ibm_init_struct *iibm)
  1191. {
  1192. int res;
  1193. int status = 0;
  1194. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  1195. IBM_ACPIHANDLE_INIT(hkey);
  1196. tp_features.wan = hkey_handle &&
  1197. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  1198. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  1199. str_supported(tp_features.wan),
  1200. status);
  1201. if (tp_features.wan) {
  1202. if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
  1203. /* no wan hardware present in system */
  1204. tp_features.wan = 0;
  1205. dbg_printk(TPACPI_DBG_INIT,
  1206. "wan hardware not installed\n");
  1207. } else {
  1208. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1209. &wan_attr_group);
  1210. if (res)
  1211. return res;
  1212. }
  1213. }
  1214. return (tp_features.wan)? 0 : 1;
  1215. }
  1216. static void wan_exit(void)
  1217. {
  1218. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1219. &wan_attr_group);
  1220. }
  1221. static int wan_get_radiosw(void)
  1222. {
  1223. int status;
  1224. if (!tp_features.wan)
  1225. return -ENODEV;
  1226. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  1227. return -EIO;
  1228. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  1229. }
  1230. static int wan_set_radiosw(int radio_on)
  1231. {
  1232. int status;
  1233. if (!tp_features.wan)
  1234. return -ENODEV;
  1235. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  1236. return -EIO;
  1237. if (radio_on)
  1238. status |= TP_ACPI_WANCARD_RADIOSSW;
  1239. else
  1240. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  1241. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  1242. return -EIO;
  1243. return 0;
  1244. }
  1245. /* procfs -------------------------------------------------------------- */
  1246. static int wan_read(char *p)
  1247. {
  1248. int len = 0;
  1249. int status = wan_get_radiosw();
  1250. if (!tp_features.wan)
  1251. len += sprintf(p + len, "status:\t\tnot supported\n");
  1252. else {
  1253. len += sprintf(p + len, "status:\t\t%s\n",
  1254. (status)? "enabled" : "disabled");
  1255. len += sprintf(p + len, "commands:\tenable, disable\n");
  1256. }
  1257. return len;
  1258. }
  1259. static int wan_write(char *buf)
  1260. {
  1261. char *cmd;
  1262. if (!tp_features.wan)
  1263. return -ENODEV;
  1264. while ((cmd = next_cmd(&buf))) {
  1265. if (strlencmp(cmd, "enable") == 0) {
  1266. wan_set_radiosw(1);
  1267. } else if (strlencmp(cmd, "disable") == 0) {
  1268. wan_set_radiosw(0);
  1269. } else
  1270. return -EINVAL;
  1271. }
  1272. return 0;
  1273. }
  1274. static struct ibm_struct wan_driver_data = {
  1275. .name = "wan",
  1276. .read = wan_read,
  1277. .write = wan_write,
  1278. .exit = wan_exit,
  1279. .flags.experimental = 1,
  1280. };
  1281. /*************************************************************************
  1282. * Video subdriver
  1283. */
  1284. static enum video_access_mode video_supported;
  1285. static int video_orig_autosw;
  1286. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  1287. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  1288. "\\_SB.PCI0.VID0", /* 770e */
  1289. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  1290. "\\_SB.PCI0.AGP.VID", /* all others */
  1291. ); /* R30, R31 */
  1292. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  1293. static int __init video_init(struct ibm_init_struct *iibm)
  1294. {
  1295. int ivga;
  1296. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  1297. IBM_ACPIHANDLE_INIT(vid);
  1298. IBM_ACPIHANDLE_INIT(vid2);
  1299. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  1300. /* G41, assume IVGA doesn't change */
  1301. vid_handle = vid2_handle;
  1302. if (!vid_handle)
  1303. /* video switching not supported on R30, R31 */
  1304. video_supported = TPACPI_VIDEO_NONE;
  1305. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  1306. /* 570 */
  1307. video_supported = TPACPI_VIDEO_570;
  1308. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  1309. /* 600e/x, 770e, 770x */
  1310. video_supported = TPACPI_VIDEO_770;
  1311. else
  1312. /* all others */
  1313. video_supported = TPACPI_VIDEO_NEW;
  1314. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  1315. str_supported(video_supported != TPACPI_VIDEO_NONE),
  1316. video_supported);
  1317. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  1318. }
  1319. static void video_exit(void)
  1320. {
  1321. dbg_printk(TPACPI_DBG_EXIT,
  1322. "restoring original video autoswitch mode\n");
  1323. if (video_autosw_set(video_orig_autosw))
  1324. printk(IBM_ERR "error while trying to restore original "
  1325. "video autoswitch mode\n");
  1326. }
  1327. static int video_outputsw_get(void)
  1328. {
  1329. int status = 0;
  1330. int i;
  1331. switch (video_supported) {
  1332. case TPACPI_VIDEO_570:
  1333. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  1334. TP_ACPI_VIDEO_570_PHSCMD))
  1335. return -EIO;
  1336. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  1337. break;
  1338. case TPACPI_VIDEO_770:
  1339. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  1340. return -EIO;
  1341. if (i)
  1342. status |= TP_ACPI_VIDEO_S_LCD;
  1343. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  1344. return -EIO;
  1345. if (i)
  1346. status |= TP_ACPI_VIDEO_S_CRT;
  1347. break;
  1348. case TPACPI_VIDEO_NEW:
  1349. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  1350. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  1351. return -EIO;
  1352. if (i)
  1353. status |= TP_ACPI_VIDEO_S_CRT;
  1354. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  1355. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  1356. return -EIO;
  1357. if (i)
  1358. status |= TP_ACPI_VIDEO_S_LCD;
  1359. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  1360. return -EIO;
  1361. if (i)
  1362. status |= TP_ACPI_VIDEO_S_DVI;
  1363. break;
  1364. default:
  1365. return -ENOSYS;
  1366. }
  1367. return status;
  1368. }
  1369. static int video_outputsw_set(int status)
  1370. {
  1371. int autosw;
  1372. int res = 0;
  1373. switch (video_supported) {
  1374. case TPACPI_VIDEO_570:
  1375. res = acpi_evalf(NULL, NULL,
  1376. "\\_SB.PHS2", "vdd",
  1377. TP_ACPI_VIDEO_570_PHS2CMD,
  1378. status | TP_ACPI_VIDEO_570_PHS2SET);
  1379. break;
  1380. case TPACPI_VIDEO_770:
  1381. autosw = video_autosw_get();
  1382. if (autosw < 0)
  1383. return autosw;
  1384. res = video_autosw_set(1);
  1385. if (res)
  1386. return res;
  1387. res = acpi_evalf(vid_handle, NULL,
  1388. "ASWT", "vdd", status * 0x100, 0);
  1389. if (!autosw && video_autosw_set(autosw)) {
  1390. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1391. return -EIO;
  1392. }
  1393. break;
  1394. case TPACPI_VIDEO_NEW:
  1395. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  1396. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  1397. break;
  1398. default:
  1399. return -ENOSYS;
  1400. }
  1401. return (res)? 0 : -EIO;
  1402. }
  1403. static int video_autosw_get(void)
  1404. {
  1405. int autosw = 0;
  1406. switch (video_supported) {
  1407. case TPACPI_VIDEO_570:
  1408. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  1409. return -EIO;
  1410. break;
  1411. case TPACPI_VIDEO_770:
  1412. case TPACPI_VIDEO_NEW:
  1413. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  1414. return -EIO;
  1415. break;
  1416. default:
  1417. return -ENOSYS;
  1418. }
  1419. return autosw & 1;
  1420. }
  1421. static int video_autosw_set(int enable)
  1422. {
  1423. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  1424. return -EIO;
  1425. return 0;
  1426. }
  1427. static int video_outputsw_cycle(void)
  1428. {
  1429. int autosw = video_autosw_get();
  1430. int res;
  1431. if (autosw < 0)
  1432. return autosw;
  1433. switch (video_supported) {
  1434. case TPACPI_VIDEO_570:
  1435. res = video_autosw_set(1);
  1436. if (res)
  1437. return res;
  1438. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  1439. break;
  1440. case TPACPI_VIDEO_770:
  1441. case TPACPI_VIDEO_NEW:
  1442. res = video_autosw_set(1);
  1443. if (res)
  1444. return res;
  1445. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  1446. break;
  1447. default:
  1448. return -ENOSYS;
  1449. }
  1450. if (!autosw && video_autosw_set(autosw)) {
  1451. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1452. return -EIO;
  1453. }
  1454. return (res)? 0 : -EIO;
  1455. }
  1456. static int video_expand_toggle(void)
  1457. {
  1458. switch (video_supported) {
  1459. case TPACPI_VIDEO_570:
  1460. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  1461. 0 : -EIO;
  1462. case TPACPI_VIDEO_770:
  1463. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  1464. 0 : -EIO;
  1465. case TPACPI_VIDEO_NEW:
  1466. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  1467. 0 : -EIO;
  1468. default:
  1469. return -ENOSYS;
  1470. }
  1471. /* not reached */
  1472. }
  1473. static int video_read(char *p)
  1474. {
  1475. int status, autosw;
  1476. int len = 0;
  1477. if (video_supported == TPACPI_VIDEO_NONE) {
  1478. len += sprintf(p + len, "status:\t\tnot supported\n");
  1479. return len;
  1480. }
  1481. status = video_outputsw_get();
  1482. if (status < 0)
  1483. return status;
  1484. autosw = video_autosw_get();
  1485. if (autosw < 0)
  1486. return autosw;
  1487. len += sprintf(p + len, "status:\t\tsupported\n");
  1488. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  1489. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  1490. if (video_supported == TPACPI_VIDEO_NEW)
  1491. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  1492. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  1493. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  1494. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  1495. if (video_supported == TPACPI_VIDEO_NEW)
  1496. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  1497. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  1498. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  1499. return len;
  1500. }
  1501. static int video_write(char *buf)
  1502. {
  1503. char *cmd;
  1504. int enable, disable, status;
  1505. int res;
  1506. if (video_supported == TPACPI_VIDEO_NONE)
  1507. return -ENODEV;
  1508. enable = 0;
  1509. disable = 0;
  1510. while ((cmd = next_cmd(&buf))) {
  1511. if (strlencmp(cmd, "lcd_enable") == 0) {
  1512. enable |= TP_ACPI_VIDEO_S_LCD;
  1513. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  1514. disable |= TP_ACPI_VIDEO_S_LCD;
  1515. } else if (strlencmp(cmd, "crt_enable") == 0) {
  1516. enable |= TP_ACPI_VIDEO_S_CRT;
  1517. } else if (strlencmp(cmd, "crt_disable") == 0) {
  1518. disable |= TP_ACPI_VIDEO_S_CRT;
  1519. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1520. strlencmp(cmd, "dvi_enable") == 0) {
  1521. enable |= TP_ACPI_VIDEO_S_DVI;
  1522. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1523. strlencmp(cmd, "dvi_disable") == 0) {
  1524. disable |= TP_ACPI_VIDEO_S_DVI;
  1525. } else if (strlencmp(cmd, "auto_enable") == 0) {
  1526. res = video_autosw_set(1);
  1527. if (res)
  1528. return res;
  1529. } else if (strlencmp(cmd, "auto_disable") == 0) {
  1530. res = video_autosw_set(0);
  1531. if (res)
  1532. return res;
  1533. } else if (strlencmp(cmd, "video_switch") == 0) {
  1534. res = video_outputsw_cycle();
  1535. if (res)
  1536. return res;
  1537. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  1538. res = video_expand_toggle();
  1539. if (res)
  1540. return res;
  1541. } else
  1542. return -EINVAL;
  1543. }
  1544. if (enable || disable) {
  1545. status = video_outputsw_get();
  1546. if (status < 0)
  1547. return status;
  1548. res = video_outputsw_set((status & ~disable) | enable);
  1549. if (res)
  1550. return res;
  1551. }
  1552. return 0;
  1553. }
  1554. static struct ibm_struct video_driver_data = {
  1555. .name = "video",
  1556. .read = video_read,
  1557. .write = video_write,
  1558. .exit = video_exit,
  1559. };
  1560. /*************************************************************************
  1561. * Light (thinklight) subdriver
  1562. */
  1563. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  1564. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  1565. static int __init light_init(struct ibm_init_struct *iibm)
  1566. {
  1567. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  1568. IBM_ACPIHANDLE_INIT(ledb);
  1569. IBM_ACPIHANDLE_INIT(lght);
  1570. IBM_ACPIHANDLE_INIT(cmos);
  1571. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  1572. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  1573. if (tp_features.light)
  1574. /* light status not supported on
  1575. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  1576. tp_features.light_status =
  1577. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  1578. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  1579. str_supported(tp_features.light));
  1580. return (tp_features.light)? 0 : 1;
  1581. }
  1582. static int light_read(char *p)
  1583. {
  1584. int len = 0;
  1585. int status = 0;
  1586. if (!tp_features.light) {
  1587. len += sprintf(p + len, "status:\t\tnot supported\n");
  1588. } else if (!tp_features.light_status) {
  1589. len += sprintf(p + len, "status:\t\tunknown\n");
  1590. len += sprintf(p + len, "commands:\ton, off\n");
  1591. } else {
  1592. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  1593. return -EIO;
  1594. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  1595. len += sprintf(p + len, "commands:\ton, off\n");
  1596. }
  1597. return len;
  1598. }
  1599. static int light_write(char *buf)
  1600. {
  1601. int cmos_cmd, lght_cmd;
  1602. char *cmd;
  1603. int success;
  1604. if (!tp_features.light)
  1605. return -ENODEV;
  1606. while ((cmd = next_cmd(&buf))) {
  1607. if (strlencmp(cmd, "on") == 0) {
  1608. cmos_cmd = 0x0c;
  1609. lght_cmd = 1;
  1610. } else if (strlencmp(cmd, "off") == 0) {
  1611. cmos_cmd = 0x0d;
  1612. lght_cmd = 0;
  1613. } else
  1614. return -EINVAL;
  1615. success = cmos_handle ?
  1616. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  1617. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  1618. if (!success)
  1619. return -EIO;
  1620. }
  1621. return 0;
  1622. }
  1623. static struct ibm_struct light_driver_data = {
  1624. .name = "light",
  1625. .read = light_read,
  1626. .write = light_write,
  1627. };
  1628. /*************************************************************************
  1629. * Dock subdriver
  1630. */
  1631. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  1632. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  1633. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  1634. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  1635. "\\_SB.PCI.ISA.SLCE", /* 570 */
  1636. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  1637. /* don't list other alternatives as we install a notify handler on the 570 */
  1638. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  1639. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  1640. {
  1641. .notify = dock_notify,
  1642. .handle = &dock_handle,
  1643. .type = ACPI_SYSTEM_NOTIFY,
  1644. },
  1645. {
  1646. .hid = IBM_PCI_HID,
  1647. .notify = dock_notify,
  1648. .handle = &pci_handle,
  1649. .type = ACPI_SYSTEM_NOTIFY,
  1650. },
  1651. };
  1652. static struct ibm_struct dock_driver_data[2] = {
  1653. {
  1654. .name = "dock",
  1655. .read = dock_read,
  1656. .write = dock_write,
  1657. .acpi = &ibm_dock_acpidriver[0],
  1658. },
  1659. {
  1660. .name = "dock",
  1661. .acpi = &ibm_dock_acpidriver[1],
  1662. },
  1663. };
  1664. #define dock_docked() (_sta(dock_handle) & 1)
  1665. static int __init dock_init(struct ibm_init_struct *iibm)
  1666. {
  1667. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  1668. IBM_ACPIHANDLE_INIT(dock);
  1669. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  1670. str_supported(dock_handle != NULL));
  1671. return (dock_handle)? 0 : 1;
  1672. }
  1673. static int __init dock_init2(struct ibm_init_struct *iibm)
  1674. {
  1675. int dock2_needed;
  1676. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  1677. if (dock_driver_data[0].flags.acpi_driver_registered &&
  1678. dock_driver_data[0].flags.acpi_notify_installed) {
  1679. IBM_ACPIHANDLE_INIT(pci);
  1680. dock2_needed = (pci_handle != NULL);
  1681. vdbg_printk(TPACPI_DBG_INIT,
  1682. "dock PCI handler for the TP 570 is %s\n",
  1683. str_supported(dock2_needed));
  1684. } else {
  1685. vdbg_printk(TPACPI_DBG_INIT,
  1686. "dock subdriver part 2 not required\n");
  1687. dock2_needed = 0;
  1688. }
  1689. return (dock2_needed)? 0 : 1;
  1690. }
  1691. static void dock_notify(struct ibm_struct *ibm, u32 event)
  1692. {
  1693. int docked = dock_docked();
  1694. int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
  1695. if (event == 1 && !pci) /* 570 */
  1696. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1697. else if (event == 1 && pci) /* 570 */
  1698. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1699. else if (event == 3 && docked)
  1700. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1701. else if (event == 3 && !docked)
  1702. acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
  1703. else if (event == 0 && docked)
  1704. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1705. else {
  1706. printk(IBM_ERR "unknown dock event %d, status %d\n",
  1707. event, _sta(dock_handle));
  1708. acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
  1709. }
  1710. }
  1711. static int dock_read(char *p)
  1712. {
  1713. int len = 0;
  1714. int docked = dock_docked();
  1715. if (!dock_handle)
  1716. len += sprintf(p + len, "status:\t\tnot supported\n");
  1717. else if (!docked)
  1718. len += sprintf(p + len, "status:\t\tundocked\n");
  1719. else {
  1720. len += sprintf(p + len, "status:\t\tdocked\n");
  1721. len += sprintf(p + len, "commands:\tdock, undock\n");
  1722. }
  1723. return len;
  1724. }
  1725. static int dock_write(char *buf)
  1726. {
  1727. char *cmd;
  1728. if (!dock_docked())
  1729. return -ENODEV;
  1730. while ((cmd = next_cmd(&buf))) {
  1731. if (strlencmp(cmd, "undock") == 0) {
  1732. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  1733. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  1734. return -EIO;
  1735. } else if (strlencmp(cmd, "dock") == 0) {
  1736. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  1737. return -EIO;
  1738. } else
  1739. return -EINVAL;
  1740. }
  1741. return 0;
  1742. }
  1743. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1744. /*************************************************************************
  1745. * Bay subdriver
  1746. */
  1747. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1748. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1749. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1750. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1751. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1752. ); /* A21e, R30, R31 */
  1753. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1754. "_EJ0", /* all others */
  1755. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1756. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1757. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1758. ); /* all others */
  1759. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1760. "_EJ0", /* 770x */
  1761. ); /* all others */
  1762. static int __init bay_init(struct ibm_init_struct *iibm)
  1763. {
  1764. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1765. IBM_ACPIHANDLE_INIT(bay);
  1766. if (bay_handle)
  1767. IBM_ACPIHANDLE_INIT(bay_ej);
  1768. IBM_ACPIHANDLE_INIT(bay2);
  1769. if (bay2_handle)
  1770. IBM_ACPIHANDLE_INIT(bay2_ej);
  1771. tp_features.bay_status = bay_handle &&
  1772. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1773. tp_features.bay_status2 = bay2_handle &&
  1774. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1775. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  1776. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1777. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  1778. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1779. vdbg_printk(TPACPI_DBG_INIT,
  1780. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1781. str_supported(tp_features.bay_status),
  1782. str_supported(tp_features.bay_eject),
  1783. str_supported(tp_features.bay_status2),
  1784. str_supported(tp_features.bay_eject2));
  1785. return (tp_features.bay_status || tp_features.bay_eject ||
  1786. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  1787. }
  1788. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1789. {
  1790. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  1791. }
  1792. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1793. static int bay_read(char *p)
  1794. {
  1795. int len = 0;
  1796. int occupied = bay_occupied(bay);
  1797. int occupied2 = bay_occupied(bay2);
  1798. int eject, eject2;
  1799. len += sprintf(p + len, "status:\t\t%s\n",
  1800. tp_features.bay_status ?
  1801. (occupied ? "occupied" : "unoccupied") :
  1802. "not supported");
  1803. if (tp_features.bay_status2)
  1804. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1805. "occupied" : "unoccupied");
  1806. eject = tp_features.bay_eject && occupied;
  1807. eject2 = tp_features.bay_eject2 && occupied2;
  1808. if (eject && eject2)
  1809. len += sprintf(p + len, "commands:\teject, eject2\n");
  1810. else if (eject)
  1811. len += sprintf(p + len, "commands:\teject\n");
  1812. else if (eject2)
  1813. len += sprintf(p + len, "commands:\teject2\n");
  1814. return len;
  1815. }
  1816. static int bay_write(char *buf)
  1817. {
  1818. char *cmd;
  1819. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  1820. return -ENODEV;
  1821. while ((cmd = next_cmd(&buf))) {
  1822. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  1823. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1824. return -EIO;
  1825. } else if (tp_features.bay_eject2 &&
  1826. strlencmp(cmd, "eject2") == 0) {
  1827. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  1828. return -EIO;
  1829. } else
  1830. return -EINVAL;
  1831. }
  1832. return 0;
  1833. }
  1834. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  1835. .notify = bay_notify,
  1836. .handle = &bay_handle,
  1837. .type = ACPI_SYSTEM_NOTIFY,
  1838. };
  1839. static struct ibm_struct bay_driver_data = {
  1840. .name = "bay",
  1841. .read = bay_read,
  1842. .write = bay_write,
  1843. .acpi = &ibm_bay_acpidriver,
  1844. };
  1845. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  1846. /*************************************************************************
  1847. * CMOS subdriver
  1848. */
  1849. /* sysfs cmos_command -------------------------------------------------- */
  1850. static ssize_t cmos_command_store(struct device *dev,
  1851. struct device_attribute *attr,
  1852. const char *buf, size_t count)
  1853. {
  1854. unsigned long cmos_cmd;
  1855. int res;
  1856. if (parse_strtoul(buf, 21, &cmos_cmd))
  1857. return -EINVAL;
  1858. res = issue_thinkpad_cmos_command(cmos_cmd);
  1859. return (res)? res : count;
  1860. }
  1861. static struct device_attribute dev_attr_cmos_command =
  1862. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  1863. /* --------------------------------------------------------------------- */
  1864. static int __init cmos_init(struct ibm_init_struct *iibm)
  1865. {
  1866. int res;
  1867. vdbg_printk(TPACPI_DBG_INIT,
  1868. "initializing cmos commands subdriver\n");
  1869. IBM_ACPIHANDLE_INIT(cmos);
  1870. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1871. str_supported(cmos_handle != NULL));
  1872. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  1873. if (res)
  1874. return res;
  1875. return (cmos_handle)? 0 : 1;
  1876. }
  1877. static void cmos_exit(void)
  1878. {
  1879. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  1880. }
  1881. static int cmos_read(char *p)
  1882. {
  1883. int len = 0;
  1884. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1885. R30, R31, T20-22, X20-21 */
  1886. if (!cmos_handle)
  1887. len += sprintf(p + len, "status:\t\tnot supported\n");
  1888. else {
  1889. len += sprintf(p + len, "status:\t\tsupported\n");
  1890. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1891. }
  1892. return len;
  1893. }
  1894. static int cmos_write(char *buf)
  1895. {
  1896. char *cmd;
  1897. int cmos_cmd, res;
  1898. while ((cmd = next_cmd(&buf))) {
  1899. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1900. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1901. /* cmos_cmd set */
  1902. } else
  1903. return -EINVAL;
  1904. res = issue_thinkpad_cmos_command(cmos_cmd);
  1905. if (res)
  1906. return res;
  1907. }
  1908. return 0;
  1909. }
  1910. static struct ibm_struct cmos_driver_data = {
  1911. .name = "cmos",
  1912. .read = cmos_read,
  1913. .write = cmos_write,
  1914. .exit = cmos_exit,
  1915. };
  1916. /*************************************************************************
  1917. * LED subdriver
  1918. */
  1919. static enum led_access_mode led_supported;
  1920. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1921. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1922. "LED", /* all others */
  1923. ); /* R30, R31 */
  1924. static int __init led_init(struct ibm_init_struct *iibm)
  1925. {
  1926. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1927. IBM_ACPIHANDLE_INIT(led);
  1928. if (!led_handle)
  1929. /* led not supported on R30, R31 */
  1930. led_supported = TPACPI_LED_NONE;
  1931. else if (strlencmp(led_path, "SLED") == 0)
  1932. /* 570 */
  1933. led_supported = TPACPI_LED_570;
  1934. else if (strlencmp(led_path, "SYSL") == 0)
  1935. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1936. led_supported = TPACPI_LED_OLD;
  1937. else
  1938. /* all others */
  1939. led_supported = TPACPI_LED_NEW;
  1940. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1941. str_supported(led_supported), led_supported);
  1942. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1943. }
  1944. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1945. static int led_read(char *p)
  1946. {
  1947. int len = 0;
  1948. if (!led_supported) {
  1949. len += sprintf(p + len, "status:\t\tnot supported\n");
  1950. return len;
  1951. }
  1952. len += sprintf(p + len, "status:\t\tsupported\n");
  1953. if (led_supported == TPACPI_LED_570) {
  1954. /* 570 */
  1955. int i, status;
  1956. for (i = 0; i < 8; i++) {
  1957. if (!acpi_evalf(ec_handle,
  1958. &status, "GLED", "dd", 1 << i))
  1959. return -EIO;
  1960. len += sprintf(p + len, "%d:\t\t%s\n",
  1961. i, led_status(status));
  1962. }
  1963. }
  1964. len += sprintf(p + len, "commands:\t"
  1965. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1966. return len;
  1967. }
  1968. /* off, on, blink */
  1969. static const int led_sled_arg1[] = { 0, 1, 3 };
  1970. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1971. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1972. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1973. static int led_write(char *buf)
  1974. {
  1975. char *cmd;
  1976. int led, ind, ret;
  1977. if (!led_supported)
  1978. return -ENODEV;
  1979. while ((cmd = next_cmd(&buf))) {
  1980. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1981. return -EINVAL;
  1982. if (strstr(cmd, "off")) {
  1983. ind = 0;
  1984. } else if (strstr(cmd, "on")) {
  1985. ind = 1;
  1986. } else if (strstr(cmd, "blink")) {
  1987. ind = 2;
  1988. } else
  1989. return -EINVAL;
  1990. if (led_supported == TPACPI_LED_570) {
  1991. /* 570 */
  1992. led = 1 << led;
  1993. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1994. led, led_sled_arg1[ind]))
  1995. return -EIO;
  1996. } else if (led_supported == TPACPI_LED_OLD) {
  1997. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1998. led = 1 << led;
  1999. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  2000. if (ret >= 0)
  2001. ret =
  2002. ec_write(TPACPI_LED_EC_HLBL,
  2003. led * led_exp_hlbl[ind]);
  2004. if (ret >= 0)
  2005. ret =
  2006. ec_write(TPACPI_LED_EC_HLCL,
  2007. led * led_exp_hlcl[ind]);
  2008. if (ret < 0)
  2009. return ret;
  2010. } else {
  2011. /* all others */
  2012. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2013. led, led_led_arg1[ind]))
  2014. return -EIO;
  2015. }
  2016. }
  2017. return 0;
  2018. }
  2019. static struct ibm_struct led_driver_data = {
  2020. .name = "led",
  2021. .read = led_read,
  2022. .write = led_write,
  2023. };
  2024. /*************************************************************************
  2025. * Beep subdriver
  2026. */
  2027. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  2028. static int __init beep_init(struct ibm_init_struct *iibm)
  2029. {
  2030. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  2031. IBM_ACPIHANDLE_INIT(beep);
  2032. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  2033. str_supported(beep_handle != NULL));
  2034. return (beep_handle)? 0 : 1;
  2035. }
  2036. static int beep_read(char *p)
  2037. {
  2038. int len = 0;
  2039. if (!beep_handle)
  2040. len += sprintf(p + len, "status:\t\tnot supported\n");
  2041. else {
  2042. len += sprintf(p + len, "status:\t\tsupported\n");
  2043. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  2044. }
  2045. return len;
  2046. }
  2047. static int beep_write(char *buf)
  2048. {
  2049. char *cmd;
  2050. int beep_cmd;
  2051. if (!beep_handle)
  2052. return -ENODEV;
  2053. while ((cmd = next_cmd(&buf))) {
  2054. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  2055. beep_cmd >= 0 && beep_cmd <= 17) {
  2056. /* beep_cmd set */
  2057. } else
  2058. return -EINVAL;
  2059. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  2060. return -EIO;
  2061. }
  2062. return 0;
  2063. }
  2064. static struct ibm_struct beep_driver_data = {
  2065. .name = "beep",
  2066. .read = beep_read,
  2067. .write = beep_write,
  2068. };
  2069. /*************************************************************************
  2070. * Thermal subdriver
  2071. */
  2072. static enum thermal_access_mode thermal_read_mode;
  2073. /* sysfs temp##_input -------------------------------------------------- */
  2074. static ssize_t thermal_temp_input_show(struct device *dev,
  2075. struct device_attribute *attr,
  2076. char *buf)
  2077. {
  2078. struct sensor_device_attribute *sensor_attr =
  2079. to_sensor_dev_attr(attr);
  2080. int idx = sensor_attr->index;
  2081. s32 value;
  2082. int res;
  2083. res = thermal_get_sensor(idx, &value);
  2084. if (res)
  2085. return res;
  2086. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  2087. return -ENXIO;
  2088. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  2089. }
  2090. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  2091. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  2092. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  2093. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  2094. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  2095. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  2096. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  2097. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  2098. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  2099. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  2100. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  2101. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  2102. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  2103. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  2104. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  2105. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  2106. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  2107. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  2108. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  2109. };
  2110. #define THERMAL_ATTRS(X) \
  2111. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  2112. static struct attribute *thermal_temp_input_attr[] = {
  2113. THERMAL_ATTRS(8),
  2114. THERMAL_ATTRS(9),
  2115. THERMAL_ATTRS(10),
  2116. THERMAL_ATTRS(11),
  2117. THERMAL_ATTRS(12),
  2118. THERMAL_ATTRS(13),
  2119. THERMAL_ATTRS(14),
  2120. THERMAL_ATTRS(15),
  2121. THERMAL_ATTRS(0),
  2122. THERMAL_ATTRS(1),
  2123. THERMAL_ATTRS(2),
  2124. THERMAL_ATTRS(3),
  2125. THERMAL_ATTRS(4),
  2126. THERMAL_ATTRS(5),
  2127. THERMAL_ATTRS(6),
  2128. THERMAL_ATTRS(7),
  2129. NULL
  2130. };
  2131. static const struct attribute_group thermal_temp_input16_group = {
  2132. .attrs = thermal_temp_input_attr
  2133. };
  2134. static const struct attribute_group thermal_temp_input8_group = {
  2135. .attrs = &thermal_temp_input_attr[8]
  2136. };
  2137. #undef THERMAL_SENSOR_ATTR_TEMP
  2138. #undef THERMAL_ATTRS
  2139. /* --------------------------------------------------------------------- */
  2140. static int __init thermal_init(struct ibm_init_struct *iibm)
  2141. {
  2142. u8 t, ta1, ta2;
  2143. int i;
  2144. int acpi_tmp7;
  2145. int res;
  2146. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  2147. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  2148. if (ibm_thinkpad_ec_found && experimental) {
  2149. /*
  2150. * Direct EC access mode: sensors at registers
  2151. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  2152. * non-implemented, thermal sensors return 0x80 when
  2153. * not available
  2154. */
  2155. ta1 = ta2 = 0;
  2156. for (i = 0; i < 8; i++) {
  2157. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  2158. ta1 |= t;
  2159. } else {
  2160. ta1 = 0;
  2161. break;
  2162. }
  2163. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  2164. ta2 |= t;
  2165. } else {
  2166. ta1 = 0;
  2167. break;
  2168. }
  2169. }
  2170. if (ta1 == 0) {
  2171. /* This is sheer paranoia, but we handle it anyway */
  2172. if (acpi_tmp7) {
  2173. printk(IBM_ERR
  2174. "ThinkPad ACPI EC access misbehaving, "
  2175. "falling back to ACPI TMPx access mode\n");
  2176. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  2177. } else {
  2178. printk(IBM_ERR
  2179. "ThinkPad ACPI EC access misbehaving, "
  2180. "disabling thermal sensors access\n");
  2181. thermal_read_mode = TPACPI_THERMAL_NONE;
  2182. }
  2183. } else {
  2184. thermal_read_mode =
  2185. (ta2 != 0) ?
  2186. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  2187. }
  2188. } else if (acpi_tmp7) {
  2189. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  2190. /* 600e/x, 770e, 770x */
  2191. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  2192. } else {
  2193. /* Standard ACPI TMPx access, max 8 sensors */
  2194. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  2195. }
  2196. } else {
  2197. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  2198. thermal_read_mode = TPACPI_THERMAL_NONE;
  2199. }
  2200. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  2201. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  2202. thermal_read_mode);
  2203. switch(thermal_read_mode) {
  2204. case TPACPI_THERMAL_TPEC_16:
  2205. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2206. &thermal_temp_input16_group);
  2207. if (res)
  2208. return res;
  2209. break;
  2210. case TPACPI_THERMAL_TPEC_8:
  2211. case TPACPI_THERMAL_ACPI_TMP07:
  2212. case TPACPI_THERMAL_ACPI_UPDT:
  2213. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2214. &thermal_temp_input8_group);
  2215. if (res)
  2216. return res;
  2217. break;
  2218. case TPACPI_THERMAL_NONE:
  2219. default:
  2220. return 1;
  2221. }
  2222. return 0;
  2223. }
  2224. static void thermal_exit(void)
  2225. {
  2226. switch(thermal_read_mode) {
  2227. case TPACPI_THERMAL_TPEC_16:
  2228. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2229. &thermal_temp_input16_group);
  2230. break;
  2231. case TPACPI_THERMAL_TPEC_8:
  2232. case TPACPI_THERMAL_ACPI_TMP07:
  2233. case TPACPI_THERMAL_ACPI_UPDT:
  2234. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2235. &thermal_temp_input16_group);
  2236. break;
  2237. case TPACPI_THERMAL_NONE:
  2238. default:
  2239. break;
  2240. }
  2241. }
  2242. /* idx is zero-based */
  2243. static int thermal_get_sensor(int idx, s32 *value)
  2244. {
  2245. int t;
  2246. s8 tmp;
  2247. char tmpi[5];
  2248. t = TP_EC_THERMAL_TMP0;
  2249. switch (thermal_read_mode) {
  2250. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  2251. case TPACPI_THERMAL_TPEC_16:
  2252. if (idx >= 8 && idx <= 15) {
  2253. t = TP_EC_THERMAL_TMP8;
  2254. idx -= 8;
  2255. }
  2256. /* fallthrough */
  2257. #endif
  2258. case TPACPI_THERMAL_TPEC_8:
  2259. if (idx <= 7) {
  2260. if (!acpi_ec_read(t + idx, &tmp))
  2261. return -EIO;
  2262. *value = tmp * 1000;
  2263. return 0;
  2264. }
  2265. break;
  2266. case TPACPI_THERMAL_ACPI_UPDT:
  2267. if (idx <= 7) {
  2268. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  2269. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  2270. return -EIO;
  2271. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  2272. return -EIO;
  2273. *value = (t - 2732) * 100;
  2274. return 0;
  2275. }
  2276. break;
  2277. case TPACPI_THERMAL_ACPI_TMP07:
  2278. if (idx <= 7) {
  2279. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  2280. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  2281. return -EIO;
  2282. *value = t * 1000;
  2283. return 0;
  2284. }
  2285. break;
  2286. case TPACPI_THERMAL_NONE:
  2287. default:
  2288. return -ENOSYS;
  2289. }
  2290. return -EINVAL;
  2291. }
  2292. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  2293. {
  2294. int res, i;
  2295. int n;
  2296. n = 8;
  2297. i = 0;
  2298. if (!s)
  2299. return -EINVAL;
  2300. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  2301. n = 16;
  2302. for(i = 0 ; i < n; i++) {
  2303. res = thermal_get_sensor(i, &s->temp[i]);
  2304. if (res)
  2305. return res;
  2306. }
  2307. return n;
  2308. }
  2309. static int thermal_read(char *p)
  2310. {
  2311. int len = 0;
  2312. int n, i;
  2313. struct ibm_thermal_sensors_struct t;
  2314. n = thermal_get_sensors(&t);
  2315. if (unlikely(n < 0))
  2316. return n;
  2317. len += sprintf(p + len, "temperatures:\t");
  2318. if (n > 0) {
  2319. for (i = 0; i < (n - 1); i++)
  2320. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  2321. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  2322. } else
  2323. len += sprintf(p + len, "not supported\n");
  2324. return len;
  2325. }
  2326. static struct ibm_struct thermal_driver_data = {
  2327. .name = "thermal",
  2328. .read = thermal_read,
  2329. .exit = thermal_exit,
  2330. };
  2331. /*************************************************************************
  2332. * EC Dump subdriver
  2333. */
  2334. static u8 ecdump_regs[256];
  2335. static int ecdump_read(char *p)
  2336. {
  2337. int len = 0;
  2338. int i, j;
  2339. u8 v;
  2340. len += sprintf(p + len, "EC "
  2341. " +00 +01 +02 +03 +04 +05 +06 +07"
  2342. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  2343. for (i = 0; i < 256; i += 16) {
  2344. len += sprintf(p + len, "EC 0x%02x:", i);
  2345. for (j = 0; j < 16; j++) {
  2346. if (!acpi_ec_read(i + j, &v))
  2347. break;
  2348. if (v != ecdump_regs[i + j])
  2349. len += sprintf(p + len, " *%02x", v);
  2350. else
  2351. len += sprintf(p + len, " %02x", v);
  2352. ecdump_regs[i + j] = v;
  2353. }
  2354. len += sprintf(p + len, "\n");
  2355. if (j != 16)
  2356. break;
  2357. }
  2358. /* These are way too dangerous to advertise openly... */
  2359. #if 0
  2360. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  2361. " (<offset> is 00-ff, <value> is 00-ff)\n");
  2362. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  2363. " (<offset> is 00-ff, <value> is 0-255)\n");
  2364. #endif
  2365. return len;
  2366. }
  2367. static int ecdump_write(char *buf)
  2368. {
  2369. char *cmd;
  2370. int i, v;
  2371. while ((cmd = next_cmd(&buf))) {
  2372. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  2373. /* i and v set */
  2374. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  2375. /* i and v set */
  2376. } else
  2377. return -EINVAL;
  2378. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  2379. if (!acpi_ec_write(i, v))
  2380. return -EIO;
  2381. } else
  2382. return -EINVAL;
  2383. }
  2384. return 0;
  2385. }
  2386. static struct ibm_struct ecdump_driver_data = {
  2387. .name = "ecdump",
  2388. .read = ecdump_read,
  2389. .write = ecdump_write,
  2390. .flags.experimental = 1,
  2391. };
  2392. /*************************************************************************
  2393. * Backlight/brightness subdriver
  2394. */
  2395. static struct backlight_device *ibm_backlight_device;
  2396. static struct backlight_ops ibm_backlight_data = {
  2397. .get_brightness = brightness_get,
  2398. .update_status = brightness_update_status,
  2399. };
  2400. static int __init brightness_init(struct ibm_init_struct *iibm)
  2401. {
  2402. int b;
  2403. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  2404. b = brightness_get(NULL);
  2405. if (b < 0)
  2406. return b;
  2407. ibm_backlight_device = backlight_device_register(
  2408. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  2409. &ibm_backlight_data);
  2410. if (IS_ERR(ibm_backlight_device)) {
  2411. printk(IBM_ERR "Could not register backlight device\n");
  2412. return PTR_ERR(ibm_backlight_device);
  2413. }
  2414. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  2415. ibm_backlight_device->props.max_brightness = 7;
  2416. ibm_backlight_device->props.brightness = b;
  2417. backlight_update_status(ibm_backlight_device);
  2418. return 0;
  2419. }
  2420. static void brightness_exit(void)
  2421. {
  2422. if (ibm_backlight_device) {
  2423. vdbg_printk(TPACPI_DBG_EXIT,
  2424. "calling backlight_device_unregister()\n");
  2425. backlight_device_unregister(ibm_backlight_device);
  2426. ibm_backlight_device = NULL;
  2427. }
  2428. }
  2429. static int brightness_update_status(struct backlight_device *bd)
  2430. {
  2431. return brightness_set(
  2432. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  2433. bd->props.power == FB_BLANK_UNBLANK) ?
  2434. bd->props.brightness : 0);
  2435. }
  2436. static int brightness_get(struct backlight_device *bd)
  2437. {
  2438. u8 level;
  2439. if (!acpi_ec_read(brightness_offset, &level))
  2440. return -EIO;
  2441. level &= 0x7;
  2442. return level;
  2443. }
  2444. static int brightness_set(int value)
  2445. {
  2446. int cmos_cmd, inc, i;
  2447. int current_value = brightness_get(NULL);
  2448. value &= 7;
  2449. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  2450. inc = value > current_value ? 1 : -1;
  2451. for (i = current_value; i != value; i += inc) {
  2452. if (issue_thinkpad_cmos_command(cmos_cmd))
  2453. return -EIO;
  2454. if (!acpi_ec_write(brightness_offset, i + inc))
  2455. return -EIO;
  2456. }
  2457. return 0;
  2458. }
  2459. static int brightness_read(char *p)
  2460. {
  2461. int len = 0;
  2462. int level;
  2463. if ((level = brightness_get(NULL)) < 0) {
  2464. len += sprintf(p + len, "level:\t\tunreadable\n");
  2465. } else {
  2466. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  2467. len += sprintf(p + len, "commands:\tup, down\n");
  2468. len += sprintf(p + len, "commands:\tlevel <level>"
  2469. " (<level> is 0-7)\n");
  2470. }
  2471. return len;
  2472. }
  2473. static int brightness_write(char *buf)
  2474. {
  2475. int level;
  2476. int new_level;
  2477. char *cmd;
  2478. while ((cmd = next_cmd(&buf))) {
  2479. if ((level = brightness_get(NULL)) < 0)
  2480. return level;
  2481. level &= 7;
  2482. if (strlencmp(cmd, "up") == 0) {
  2483. new_level = level == 7 ? 7 : level + 1;
  2484. } else if (strlencmp(cmd, "down") == 0) {
  2485. new_level = level == 0 ? 0 : level - 1;
  2486. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2487. new_level >= 0 && new_level <= 7) {
  2488. /* new_level set */
  2489. } else
  2490. return -EINVAL;
  2491. brightness_set(new_level);
  2492. }
  2493. return 0;
  2494. }
  2495. static struct ibm_struct brightness_driver_data = {
  2496. .name = "brightness",
  2497. .read = brightness_read,
  2498. .write = brightness_write,
  2499. .exit = brightness_exit,
  2500. };
  2501. /*************************************************************************
  2502. * Volume subdriver
  2503. */
  2504. static int volume_read(char *p)
  2505. {
  2506. int len = 0;
  2507. u8 level;
  2508. if (!acpi_ec_read(volume_offset, &level)) {
  2509. len += sprintf(p + len, "level:\t\tunreadable\n");
  2510. } else {
  2511. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  2512. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  2513. len += sprintf(p + len, "commands:\tup, down, mute\n");
  2514. len += sprintf(p + len, "commands:\tlevel <level>"
  2515. " (<level> is 0-15)\n");
  2516. }
  2517. return len;
  2518. }
  2519. static int volume_write(char *buf)
  2520. {
  2521. int cmos_cmd, inc, i;
  2522. u8 level, mute;
  2523. int new_level, new_mute;
  2524. char *cmd;
  2525. while ((cmd = next_cmd(&buf))) {
  2526. if (!acpi_ec_read(volume_offset, &level))
  2527. return -EIO;
  2528. new_mute = mute = level & 0x40;
  2529. new_level = level = level & 0xf;
  2530. if (strlencmp(cmd, "up") == 0) {
  2531. if (mute)
  2532. new_mute = 0;
  2533. else
  2534. new_level = level == 15 ? 15 : level + 1;
  2535. } else if (strlencmp(cmd, "down") == 0) {
  2536. if (mute)
  2537. new_mute = 0;
  2538. else
  2539. new_level = level == 0 ? 0 : level - 1;
  2540. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2541. new_level >= 0 && new_level <= 15) {
  2542. /* new_level set */
  2543. } else if (strlencmp(cmd, "mute") == 0) {
  2544. new_mute = 0x40;
  2545. } else
  2546. return -EINVAL;
  2547. if (new_level != level) { /* mute doesn't change */
  2548. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  2549. inc = new_level > level ? 1 : -1;
  2550. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  2551. !acpi_ec_write(volume_offset, level)))
  2552. return -EIO;
  2553. for (i = level; i != new_level; i += inc)
  2554. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2555. !acpi_ec_write(volume_offset, i + inc))
  2556. return -EIO;
  2557. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  2558. !acpi_ec_write(volume_offset,
  2559. new_level + mute)))
  2560. return -EIO;
  2561. }
  2562. if (new_mute != mute) { /* level doesn't change */
  2563. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  2564. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2565. !acpi_ec_write(volume_offset, level + new_mute))
  2566. return -EIO;
  2567. }
  2568. }
  2569. return 0;
  2570. }
  2571. static struct ibm_struct volume_driver_data = {
  2572. .name = "volume",
  2573. .read = volume_read,
  2574. .write = volume_write,
  2575. };
  2576. /*************************************************************************
  2577. * Fan subdriver
  2578. */
  2579. /*
  2580. * FAN ACCESS MODES
  2581. *
  2582. * TPACPI_FAN_RD_ACPI_GFAN:
  2583. * ACPI GFAN method: returns fan level
  2584. *
  2585. * see TPACPI_FAN_WR_ACPI_SFAN
  2586. * EC 0x2f (HFSP) not available if GFAN exists
  2587. *
  2588. * TPACPI_FAN_WR_ACPI_SFAN:
  2589. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2590. *
  2591. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2592. * it for writing.
  2593. *
  2594. * TPACPI_FAN_WR_TPEC:
  2595. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2596. * Supported on almost all ThinkPads
  2597. *
  2598. * Fan speed changes of any sort (including those caused by the
  2599. * disengaged mode) are usually done slowly by the firmware as the
  2600. * maximum ammount of fan duty cycle change per second seems to be
  2601. * limited.
  2602. *
  2603. * Reading is not available if GFAN exists.
  2604. * Writing is not available if SFAN exists.
  2605. *
  2606. * Bits
  2607. * 7 automatic mode engaged;
  2608. * (default operation mode of the ThinkPad)
  2609. * fan level is ignored in this mode.
  2610. * 6 full speed mode (takes precedence over bit 7);
  2611. * not available on all thinkpads. May disable
  2612. * the tachometer while the fan controller ramps up
  2613. * the speed (which can take up to a few *minutes*).
  2614. * Speeds up fan to 100% duty-cycle, which is far above
  2615. * the standard RPM levels. It is not impossible that
  2616. * it could cause hardware damage.
  2617. * 5-3 unused in some models. Extra bits for fan level
  2618. * in others, but still useless as all values above
  2619. * 7 map to the same speed as level 7 in these models.
  2620. * 2-0 fan level (0..7 usually)
  2621. * 0x00 = stop
  2622. * 0x07 = max (set when temperatures critical)
  2623. * Some ThinkPads may have other levels, see
  2624. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2625. *
  2626. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2627. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2628. * does so, its initial value is meaningless (0x07).
  2629. *
  2630. * For firmware bugs, refer to:
  2631. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2632. *
  2633. * ----
  2634. *
  2635. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2636. * Main fan tachometer reading (in RPM)
  2637. *
  2638. * This register is present on all ThinkPads with a new-style EC, and
  2639. * it is known not to be present on the A21m/e, and T22, as there is
  2640. * something else in offset 0x84 according to the ACPI DSDT. Other
  2641. * ThinkPads from this same time period (and earlier) probably lack the
  2642. * tachometer as well.
  2643. *
  2644. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2645. * was never fixed by IBM to report the EC firmware version string
  2646. * probably support the tachometer (like the early X models), so
  2647. * detecting it is quite hard. We need more data to know for sure.
  2648. *
  2649. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2650. * might result.
  2651. *
  2652. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2653. * mode.
  2654. *
  2655. * For firmware bugs, refer to:
  2656. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2657. *
  2658. * TPACPI_FAN_WR_ACPI_FANS:
  2659. * ThinkPad X31, X40, X41. Not available in the X60.
  2660. *
  2661. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2662. * high speed. ACPI DSDT seems to map these three speeds to levels
  2663. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  2664. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  2665. *
  2666. * The speeds are stored on handles
  2667. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  2668. *
  2669. * There are three default speed sets, acessible as handles:
  2670. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  2671. *
  2672. * ACPI DSDT switches which set is in use depending on various
  2673. * factors.
  2674. *
  2675. * TPACPI_FAN_WR_TPEC is also available and should be used to
  2676. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  2677. * but the ACPI tables just mention level 7.
  2678. */
  2679. static enum fan_status_access_mode fan_status_access_mode;
  2680. static enum fan_control_access_mode fan_control_access_mode;
  2681. static enum fan_control_commands fan_control_commands;
  2682. static u8 fan_control_initial_status;
  2683. static u8 fan_control_desired_level;
  2684. static void fan_watchdog_fire(struct work_struct *ignored);
  2685. static int fan_watchdog_maxinterval;
  2686. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  2687. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  2688. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  2689. "\\FSPD", /* 600e/x, 770e, 770x */
  2690. ); /* all others */
  2691. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  2692. "JFNS", /* 770x-JL */
  2693. ); /* all others */
  2694. /*
  2695. * SYSFS fan layout: hwmon compatible (device)
  2696. *
  2697. * pwm*_enable:
  2698. * 0: "disengaged" mode
  2699. * 1: manual mode
  2700. * 2: native EC "auto" mode (recommended, hardware default)
  2701. *
  2702. * pwm*: set speed in manual mode, ignored otherwise.
  2703. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  2704. * interpolation.
  2705. *
  2706. * fan*_input: tachometer reading, RPM
  2707. *
  2708. *
  2709. * SYSFS fan layout: extensions
  2710. *
  2711. * fan_watchdog (driver):
  2712. * fan watchdog interval in seconds, 0 disables (default), max 120
  2713. */
  2714. /* sysfs fan pwm1_enable ----------------------------------------------- */
  2715. static ssize_t fan_pwm1_enable_show(struct device *dev,
  2716. struct device_attribute *attr,
  2717. char *buf)
  2718. {
  2719. int res, mode;
  2720. u8 status;
  2721. res = fan_get_status_safe(&status);
  2722. if (res)
  2723. return res;
  2724. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2725. if (status != fan_control_initial_status) {
  2726. tp_features.fan_ctrl_status_undef = 0;
  2727. } else {
  2728. /* Return most likely status. In fact, it
  2729. * might be the only possible status */
  2730. status = TP_EC_FAN_AUTO;
  2731. }
  2732. }
  2733. if (status & TP_EC_FAN_FULLSPEED) {
  2734. mode = 0;
  2735. } else if (status & TP_EC_FAN_AUTO) {
  2736. mode = 2;
  2737. } else
  2738. mode = 1;
  2739. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  2740. }
  2741. static ssize_t fan_pwm1_enable_store(struct device *dev,
  2742. struct device_attribute *attr,
  2743. const char *buf, size_t count)
  2744. {
  2745. unsigned long t;
  2746. int res, level;
  2747. if (parse_strtoul(buf, 2, &t))
  2748. return -EINVAL;
  2749. switch (t) {
  2750. case 0:
  2751. level = TP_EC_FAN_FULLSPEED;
  2752. break;
  2753. case 1:
  2754. level = TPACPI_FAN_LAST_LEVEL;
  2755. break;
  2756. case 2:
  2757. level = TP_EC_FAN_AUTO;
  2758. break;
  2759. case 3:
  2760. /* reserved for software-controlled auto mode */
  2761. return -ENOSYS;
  2762. default:
  2763. return -EINVAL;
  2764. }
  2765. res = fan_set_level_safe(level);
  2766. if (res == -ENXIO)
  2767. return -EINVAL;
  2768. else if (res < 0)
  2769. return res;
  2770. fan_watchdog_reset();
  2771. return count;
  2772. }
  2773. static struct device_attribute dev_attr_fan_pwm1_enable =
  2774. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  2775. fan_pwm1_enable_show, fan_pwm1_enable_store);
  2776. /* sysfs fan pwm1 ------------------------------------------------------ */
  2777. static ssize_t fan_pwm1_show(struct device *dev,
  2778. struct device_attribute *attr,
  2779. char *buf)
  2780. {
  2781. int res;
  2782. u8 status;
  2783. res = fan_get_status_safe(&status);
  2784. if (res)
  2785. return res;
  2786. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2787. if (status != fan_control_initial_status) {
  2788. tp_features.fan_ctrl_status_undef = 0;
  2789. } else {
  2790. status = TP_EC_FAN_AUTO;
  2791. }
  2792. }
  2793. if ((status &
  2794. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  2795. status = fan_control_desired_level;
  2796. if (status > 7)
  2797. status = 7;
  2798. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  2799. }
  2800. static ssize_t fan_pwm1_store(struct device *dev,
  2801. struct device_attribute *attr,
  2802. const char *buf, size_t count)
  2803. {
  2804. unsigned long s;
  2805. int rc;
  2806. u8 status, newlevel;
  2807. if (parse_strtoul(buf, 255, &s))
  2808. return -EINVAL;
  2809. /* scale down from 0-255 to 0-7 */
  2810. newlevel = (s >> 5) & 0x07;
  2811. rc = mutex_lock_interruptible(&fan_mutex);
  2812. if (rc < 0)
  2813. return rc;
  2814. rc = fan_get_status(&status);
  2815. if (!rc && (status &
  2816. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2817. rc = fan_set_level(newlevel);
  2818. if (rc == -ENXIO)
  2819. rc = -EINVAL;
  2820. else if (!rc) {
  2821. fan_update_desired_level(newlevel);
  2822. fan_watchdog_reset();
  2823. }
  2824. }
  2825. mutex_unlock(&fan_mutex);
  2826. return (rc)? rc : count;
  2827. }
  2828. static struct device_attribute dev_attr_fan_pwm1 =
  2829. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  2830. fan_pwm1_show, fan_pwm1_store);
  2831. /* sysfs fan fan1_input ------------------------------------------------ */
  2832. static ssize_t fan_fan1_input_show(struct device *dev,
  2833. struct device_attribute *attr,
  2834. char *buf)
  2835. {
  2836. int res;
  2837. unsigned int speed;
  2838. res = fan_get_speed(&speed);
  2839. if (res < 0)
  2840. return res;
  2841. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  2842. }
  2843. static struct device_attribute dev_attr_fan_fan1_input =
  2844. __ATTR(fan1_input, S_IRUGO,
  2845. fan_fan1_input_show, NULL);
  2846. /* sysfs fan fan_watchdog (driver) ------------------------------------- */
  2847. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  2848. char *buf)
  2849. {
  2850. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  2851. }
  2852. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  2853. const char *buf, size_t count)
  2854. {
  2855. unsigned long t;
  2856. if (parse_strtoul(buf, 120, &t))
  2857. return -EINVAL;
  2858. if (!fan_control_allowed)
  2859. return -EPERM;
  2860. fan_watchdog_maxinterval = t;
  2861. fan_watchdog_reset();
  2862. return count;
  2863. }
  2864. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  2865. fan_fan_watchdog_show, fan_fan_watchdog_store);
  2866. /* --------------------------------------------------------------------- */
  2867. static struct attribute *fan_attributes[] = {
  2868. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  2869. &dev_attr_fan_fan1_input.attr,
  2870. NULL
  2871. };
  2872. static const struct attribute_group fan_attr_group = {
  2873. .attrs = fan_attributes,
  2874. };
  2875. static int __init fan_init(struct ibm_init_struct *iibm)
  2876. {
  2877. int rc;
  2878. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  2879. mutex_init(&fan_mutex);
  2880. fan_status_access_mode = TPACPI_FAN_NONE;
  2881. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2882. fan_control_commands = 0;
  2883. fan_watchdog_maxinterval = 0;
  2884. tp_features.fan_ctrl_status_undef = 0;
  2885. fan_control_desired_level = 7;
  2886. IBM_ACPIHANDLE_INIT(fans);
  2887. IBM_ACPIHANDLE_INIT(gfan);
  2888. IBM_ACPIHANDLE_INIT(sfan);
  2889. if (gfan_handle) {
  2890. /* 570, 600e/x, 770e, 770x */
  2891. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  2892. } else {
  2893. /* all other ThinkPads: note that even old-style
  2894. * ThinkPad ECs supports the fan control register */
  2895. if (likely(acpi_ec_read(fan_status_offset,
  2896. &fan_control_initial_status))) {
  2897. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  2898. /* In some ThinkPads, neither the EC nor the ACPI
  2899. * DSDT initialize the fan status, and it ends up
  2900. * being set to 0x07 when it *could* be either
  2901. * 0x07 or 0x80.
  2902. *
  2903. * Enable for TP-1Y (T43), TP-78 (R51e),
  2904. * TP-76 (R52), TP-70 (T43, R52), which are known
  2905. * to be buggy. */
  2906. if (fan_control_initial_status == 0x07 &&
  2907. ibm_thinkpad_ec_found &&
  2908. ((ibm_thinkpad_ec_found[0] == '1' &&
  2909. ibm_thinkpad_ec_found[1] == 'Y') ||
  2910. (ibm_thinkpad_ec_found[0] == '7' &&
  2911. (ibm_thinkpad_ec_found[1] == '6' ||
  2912. ibm_thinkpad_ec_found[1] == '8' ||
  2913. ibm_thinkpad_ec_found[1] == '0'))
  2914. )) {
  2915. printk(IBM_NOTICE
  2916. "fan_init: initial fan status is "
  2917. "unknown, assuming it is in auto "
  2918. "mode\n");
  2919. tp_features.fan_ctrl_status_undef = 1;
  2920. }
  2921. } else {
  2922. printk(IBM_ERR
  2923. "ThinkPad ACPI EC access misbehaving, "
  2924. "fan status and control unavailable\n");
  2925. return 1;
  2926. }
  2927. }
  2928. if (sfan_handle) {
  2929. /* 570, 770x-JL */
  2930. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  2931. fan_control_commands |=
  2932. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  2933. } else {
  2934. if (!gfan_handle) {
  2935. /* gfan without sfan means no fan control */
  2936. /* all other models implement TP EC 0x2f control */
  2937. if (fans_handle) {
  2938. /* X31, X40, X41 */
  2939. fan_control_access_mode =
  2940. TPACPI_FAN_WR_ACPI_FANS;
  2941. fan_control_commands |=
  2942. TPACPI_FAN_CMD_SPEED |
  2943. TPACPI_FAN_CMD_LEVEL |
  2944. TPACPI_FAN_CMD_ENABLE;
  2945. } else {
  2946. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  2947. fan_control_commands |=
  2948. TPACPI_FAN_CMD_LEVEL |
  2949. TPACPI_FAN_CMD_ENABLE;
  2950. }
  2951. }
  2952. }
  2953. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  2954. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  2955. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  2956. fan_status_access_mode, fan_control_access_mode);
  2957. /* fan control master switch */
  2958. if (!fan_control_allowed) {
  2959. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2960. fan_control_commands = 0;
  2961. dbg_printk(TPACPI_DBG_INIT,
  2962. "fan control features disabled by parameter\n");
  2963. }
  2964. /* update fan_control_desired_level */
  2965. if (fan_status_access_mode != TPACPI_FAN_NONE)
  2966. fan_get_status_safe(NULL);
  2967. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  2968. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  2969. rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2970. &fan_attr_group);
  2971. if (!(rc < 0))
  2972. rc = driver_create_file(&tpacpi_pdriver.driver,
  2973. &driver_attr_fan_watchdog);
  2974. if (rc < 0)
  2975. return rc;
  2976. return 0;
  2977. } else
  2978. return 1;
  2979. }
  2980. /*
  2981. * Call with fan_mutex held
  2982. */
  2983. static void fan_update_desired_level(u8 status)
  2984. {
  2985. if ((status &
  2986. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2987. if (status > 7)
  2988. fan_control_desired_level = 7;
  2989. else
  2990. fan_control_desired_level = status;
  2991. }
  2992. }
  2993. static int fan_get_status(u8 *status)
  2994. {
  2995. u8 s;
  2996. /* TODO:
  2997. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  2998. switch (fan_status_access_mode) {
  2999. case TPACPI_FAN_RD_ACPI_GFAN:
  3000. /* 570, 600e/x, 770e, 770x */
  3001. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  3002. return -EIO;
  3003. if (likely(status))
  3004. *status = s & 0x07;
  3005. break;
  3006. case TPACPI_FAN_RD_TPEC:
  3007. /* all except 570, 600e/x, 770e, 770x */
  3008. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  3009. return -EIO;
  3010. if (likely(status))
  3011. *status = s;
  3012. break;
  3013. default:
  3014. return -ENXIO;
  3015. }
  3016. return 0;
  3017. }
  3018. static int fan_get_status_safe(u8 *status)
  3019. {
  3020. int rc;
  3021. u8 s;
  3022. rc = mutex_lock_interruptible(&fan_mutex);
  3023. if (rc < 0)
  3024. return rc;
  3025. rc = fan_get_status(&s);
  3026. if (!rc)
  3027. fan_update_desired_level(s);
  3028. mutex_unlock(&fan_mutex);
  3029. if (status)
  3030. *status = s;
  3031. return rc;
  3032. }
  3033. static void fan_exit(void)
  3034. {
  3035. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  3036. /* FIXME: can we really do this unconditionally? */
  3037. sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
  3038. driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
  3039. cancel_delayed_work(&fan_watchdog_task);
  3040. flush_scheduled_work();
  3041. }
  3042. static int fan_get_speed(unsigned int *speed)
  3043. {
  3044. u8 hi, lo;
  3045. switch (fan_status_access_mode) {
  3046. case TPACPI_FAN_RD_TPEC:
  3047. /* all except 570, 600e/x, 770e, 770x */
  3048. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  3049. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  3050. return -EIO;
  3051. if (likely(speed))
  3052. *speed = (hi << 8) | lo;
  3053. break;
  3054. default:
  3055. return -ENXIO;
  3056. }
  3057. return 0;
  3058. }
  3059. static void fan_watchdog_fire(struct work_struct *ignored)
  3060. {
  3061. int rc;
  3062. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  3063. rc = fan_set_enable();
  3064. if (rc < 0) {
  3065. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  3066. "will try again later...\n", -rc);
  3067. /* reschedule for later */
  3068. fan_watchdog_reset();
  3069. }
  3070. }
  3071. static void fan_watchdog_reset(void)
  3072. {
  3073. static int fan_watchdog_active;
  3074. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  3075. return;
  3076. if (fan_watchdog_active)
  3077. cancel_delayed_work(&fan_watchdog_task);
  3078. if (fan_watchdog_maxinterval > 0) {
  3079. fan_watchdog_active = 1;
  3080. if (!schedule_delayed_work(&fan_watchdog_task,
  3081. msecs_to_jiffies(fan_watchdog_maxinterval
  3082. * 1000))) {
  3083. printk(IBM_ERR "failed to schedule the fan watchdog, "
  3084. "watchdog will not trigger\n");
  3085. }
  3086. } else
  3087. fan_watchdog_active = 0;
  3088. }
  3089. static int fan_set_level(int level)
  3090. {
  3091. if (!fan_control_allowed)
  3092. return -EPERM;
  3093. switch (fan_control_access_mode) {
  3094. case TPACPI_FAN_WR_ACPI_SFAN:
  3095. if (level >= 0 && level <= 7) {
  3096. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  3097. return -EIO;
  3098. } else
  3099. return -EINVAL;
  3100. break;
  3101. case TPACPI_FAN_WR_ACPI_FANS:
  3102. case TPACPI_FAN_WR_TPEC:
  3103. if ((level != TP_EC_FAN_AUTO) &&
  3104. (level != TP_EC_FAN_FULLSPEED) &&
  3105. ((level < 0) || (level > 7)))
  3106. return -EINVAL;
  3107. /* safety net should the EC not support AUTO
  3108. * or FULLSPEED mode bits and just ignore them */
  3109. if (level & TP_EC_FAN_FULLSPEED)
  3110. level |= 7; /* safety min speed 7 */
  3111. else if (level & TP_EC_FAN_FULLSPEED)
  3112. level |= 4; /* safety min speed 4 */
  3113. if (!acpi_ec_write(fan_status_offset, level))
  3114. return -EIO;
  3115. else
  3116. tp_features.fan_ctrl_status_undef = 0;
  3117. break;
  3118. default:
  3119. return -ENXIO;
  3120. }
  3121. return 0;
  3122. }
  3123. static int fan_set_level_safe(int level)
  3124. {
  3125. int rc;
  3126. if (!fan_control_allowed)
  3127. return -EPERM;
  3128. rc = mutex_lock_interruptible(&fan_mutex);
  3129. if (rc < 0)
  3130. return rc;
  3131. if (level == TPACPI_FAN_LAST_LEVEL)
  3132. level = fan_control_desired_level;
  3133. rc = fan_set_level(level);
  3134. if (!rc)
  3135. fan_update_desired_level(level);
  3136. mutex_unlock(&fan_mutex);
  3137. return rc;
  3138. }
  3139. static int fan_set_enable(void)
  3140. {
  3141. u8 s;
  3142. int rc;
  3143. if (!fan_control_allowed)
  3144. return -EPERM;
  3145. rc = mutex_lock_interruptible(&fan_mutex);
  3146. if (rc < 0)
  3147. return rc;
  3148. switch (fan_control_access_mode) {
  3149. case TPACPI_FAN_WR_ACPI_FANS:
  3150. case TPACPI_FAN_WR_TPEC:
  3151. rc = fan_get_status(&s);
  3152. if (rc < 0)
  3153. break;
  3154. /* Don't go out of emergency fan mode */
  3155. if (s != 7) {
  3156. s &= 0x07;
  3157. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  3158. }
  3159. if (!acpi_ec_write(fan_status_offset, s))
  3160. rc = -EIO;
  3161. else {
  3162. tp_features.fan_ctrl_status_undef = 0;
  3163. rc = 0;
  3164. }
  3165. break;
  3166. case TPACPI_FAN_WR_ACPI_SFAN:
  3167. rc = fan_get_status(&s);
  3168. if (rc < 0)
  3169. break;
  3170. s &= 0x07;
  3171. /* Set fan to at least level 4 */
  3172. s |= 4;
  3173. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  3174. rc= -EIO;
  3175. else
  3176. rc = 0;
  3177. break;
  3178. default:
  3179. rc = -ENXIO;
  3180. }
  3181. mutex_unlock(&fan_mutex);
  3182. return rc;
  3183. }
  3184. static int fan_set_disable(void)
  3185. {
  3186. int rc;
  3187. if (!fan_control_allowed)
  3188. return -EPERM;
  3189. rc = mutex_lock_interruptible(&fan_mutex);
  3190. if (rc < 0)
  3191. return rc;
  3192. rc = 0;
  3193. switch (fan_control_access_mode) {
  3194. case TPACPI_FAN_WR_ACPI_FANS:
  3195. case TPACPI_FAN_WR_TPEC:
  3196. if (!acpi_ec_write(fan_status_offset, 0x00))
  3197. rc = -EIO;
  3198. else {
  3199. fan_control_desired_level = 0;
  3200. tp_features.fan_ctrl_status_undef = 0;
  3201. }
  3202. break;
  3203. case TPACPI_FAN_WR_ACPI_SFAN:
  3204. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  3205. rc = -EIO;
  3206. else
  3207. fan_control_desired_level = 0;
  3208. break;
  3209. default:
  3210. rc = -ENXIO;
  3211. }
  3212. mutex_unlock(&fan_mutex);
  3213. return rc;
  3214. }
  3215. static int fan_set_speed(int speed)
  3216. {
  3217. int rc;
  3218. if (!fan_control_allowed)
  3219. return -EPERM;
  3220. rc = mutex_lock_interruptible(&fan_mutex);
  3221. if (rc < 0)
  3222. return rc;
  3223. rc = 0;
  3224. switch (fan_control_access_mode) {
  3225. case TPACPI_FAN_WR_ACPI_FANS:
  3226. if (speed >= 0 && speed <= 65535) {
  3227. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  3228. speed, speed, speed))
  3229. rc = -EIO;
  3230. } else
  3231. rc = -EINVAL;
  3232. break;
  3233. default:
  3234. rc = -ENXIO;
  3235. }
  3236. mutex_unlock(&fan_mutex);
  3237. return rc;
  3238. }
  3239. static int fan_read(char *p)
  3240. {
  3241. int len = 0;
  3242. int rc;
  3243. u8 status;
  3244. unsigned int speed = 0;
  3245. switch (fan_status_access_mode) {
  3246. case TPACPI_FAN_RD_ACPI_GFAN:
  3247. /* 570, 600e/x, 770e, 770x */
  3248. if ((rc = fan_get_status_safe(&status)) < 0)
  3249. return rc;
  3250. len += sprintf(p + len, "status:\t\t%s\n"
  3251. "level:\t\t%d\n",
  3252. (status != 0) ? "enabled" : "disabled", status);
  3253. break;
  3254. case TPACPI_FAN_RD_TPEC:
  3255. /* all except 570, 600e/x, 770e, 770x */
  3256. if ((rc = fan_get_status_safe(&status)) < 0)
  3257. return rc;
  3258. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3259. if (status != fan_control_initial_status)
  3260. tp_features.fan_ctrl_status_undef = 0;
  3261. else
  3262. /* Return most likely status. In fact, it
  3263. * might be the only possible status */
  3264. status = TP_EC_FAN_AUTO;
  3265. }
  3266. len += sprintf(p + len, "status:\t\t%s\n",
  3267. (status != 0) ? "enabled" : "disabled");
  3268. if ((rc = fan_get_speed(&speed)) < 0)
  3269. return rc;
  3270. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  3271. if (status & TP_EC_FAN_FULLSPEED)
  3272. /* Disengaged mode takes precedence */
  3273. len += sprintf(p + len, "level:\t\tdisengaged\n");
  3274. else if (status & TP_EC_FAN_AUTO)
  3275. len += sprintf(p + len, "level:\t\tauto\n");
  3276. else
  3277. len += sprintf(p + len, "level:\t\t%d\n", status);
  3278. break;
  3279. case TPACPI_FAN_NONE:
  3280. default:
  3281. len += sprintf(p + len, "status:\t\tnot supported\n");
  3282. }
  3283. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  3284. len += sprintf(p + len, "commands:\tlevel <level>");
  3285. switch (fan_control_access_mode) {
  3286. case TPACPI_FAN_WR_ACPI_SFAN:
  3287. len += sprintf(p + len, " (<level> is 0-7)\n");
  3288. break;
  3289. default:
  3290. len += sprintf(p + len, " (<level> is 0-7, "
  3291. "auto, disengaged, full-speed)\n");
  3292. break;
  3293. }
  3294. }
  3295. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  3296. len += sprintf(p + len, "commands:\tenable, disable\n"
  3297. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  3298. "1-120 (seconds))\n");
  3299. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  3300. len += sprintf(p + len, "commands:\tspeed <speed>"
  3301. " (<speed> is 0-65535)\n");
  3302. return len;
  3303. }
  3304. static int fan_write_cmd_level(const char *cmd, int *rc)
  3305. {
  3306. int level;
  3307. if (strlencmp(cmd, "level auto") == 0)
  3308. level = TP_EC_FAN_AUTO;
  3309. else if ((strlencmp(cmd, "level disengaged") == 0) |
  3310. (strlencmp(cmd, "level full-speed") == 0))
  3311. level = TP_EC_FAN_FULLSPEED;
  3312. else if (sscanf(cmd, "level %d", &level) != 1)
  3313. return 0;
  3314. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  3315. printk(IBM_ERR "level command accepted for unsupported "
  3316. "access mode %d", fan_control_access_mode);
  3317. return 1;
  3318. }
  3319. static int fan_write_cmd_enable(const char *cmd, int *rc)
  3320. {
  3321. if (strlencmp(cmd, "enable") != 0)
  3322. return 0;
  3323. if ((*rc = fan_set_enable()) == -ENXIO)
  3324. printk(IBM_ERR "enable command accepted for unsupported "
  3325. "access mode %d", fan_control_access_mode);
  3326. return 1;
  3327. }
  3328. static int fan_write_cmd_disable(const char *cmd, int *rc)
  3329. {
  3330. if (strlencmp(cmd, "disable") != 0)
  3331. return 0;
  3332. if ((*rc = fan_set_disable()) == -ENXIO)
  3333. printk(IBM_ERR "disable command accepted for unsupported "
  3334. "access mode %d", fan_control_access_mode);
  3335. return 1;
  3336. }
  3337. static int fan_write_cmd_speed(const char *cmd, int *rc)
  3338. {
  3339. int speed;
  3340. /* TODO:
  3341. * Support speed <low> <medium> <high> ? */
  3342. if (sscanf(cmd, "speed %d", &speed) != 1)
  3343. return 0;
  3344. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  3345. printk(IBM_ERR "speed command accepted for unsupported "
  3346. "access mode %d", fan_control_access_mode);
  3347. return 1;
  3348. }
  3349. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  3350. {
  3351. int interval;
  3352. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  3353. return 0;
  3354. if (interval < 0 || interval > 120)
  3355. *rc = -EINVAL;
  3356. else
  3357. fan_watchdog_maxinterval = interval;
  3358. return 1;
  3359. }
  3360. static int fan_write(char *buf)
  3361. {
  3362. char *cmd;
  3363. int rc = 0;
  3364. while (!rc && (cmd = next_cmd(&buf))) {
  3365. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  3366. fan_write_cmd_level(cmd, &rc)) &&
  3367. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  3368. (fan_write_cmd_enable(cmd, &rc) ||
  3369. fan_write_cmd_disable(cmd, &rc) ||
  3370. fan_write_cmd_watchdog(cmd, &rc))) &&
  3371. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  3372. fan_write_cmd_speed(cmd, &rc))
  3373. )
  3374. rc = -EINVAL;
  3375. else if (!rc)
  3376. fan_watchdog_reset();
  3377. }
  3378. return rc;
  3379. }
  3380. static struct ibm_struct fan_driver_data = {
  3381. .name = "fan",
  3382. .read = fan_read,
  3383. .write = fan_write,
  3384. .exit = fan_exit,
  3385. };
  3386. /****************************************************************************
  3387. ****************************************************************************
  3388. *
  3389. * Infrastructure
  3390. *
  3391. ****************************************************************************
  3392. ****************************************************************************/
  3393. /* /proc support */
  3394. static struct proc_dir_entry *proc_dir;
  3395. /* Subdriver registry */
  3396. static LIST_HEAD(tpacpi_all_drivers);
  3397. /*
  3398. * Module and infrastructure proble, init and exit handling
  3399. */
  3400. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  3401. static const char * __init str_supported(int is_supported)
  3402. {
  3403. static char text_unsupported[] __initdata = "not supported";
  3404. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  3405. }
  3406. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  3407. static int __init ibm_init(struct ibm_init_struct *iibm)
  3408. {
  3409. int ret;
  3410. struct ibm_struct *ibm = iibm->data;
  3411. struct proc_dir_entry *entry;
  3412. BUG_ON(ibm == NULL);
  3413. INIT_LIST_HEAD(&ibm->all_drivers);
  3414. if (ibm->flags.experimental && !experimental)
  3415. return 0;
  3416. dbg_printk(TPACPI_DBG_INIT,
  3417. "probing for %s\n", ibm->name);
  3418. if (iibm->init) {
  3419. ret = iibm->init(iibm);
  3420. if (ret > 0)
  3421. return 0; /* probe failed */
  3422. if (ret)
  3423. return ret;
  3424. ibm->flags.init_called = 1;
  3425. }
  3426. if (ibm->acpi) {
  3427. if (ibm->acpi->hid) {
  3428. ret = register_tpacpi_subdriver(ibm);
  3429. if (ret)
  3430. goto err_out;
  3431. }
  3432. if (ibm->acpi->notify) {
  3433. ret = setup_acpi_notify(ibm);
  3434. if (ret == -ENODEV) {
  3435. printk(IBM_NOTICE "disabling subdriver %s\n",
  3436. ibm->name);
  3437. ret = 0;
  3438. goto err_out;
  3439. }
  3440. if (ret < 0)
  3441. goto err_out;
  3442. }
  3443. }
  3444. dbg_printk(TPACPI_DBG_INIT,
  3445. "%s installed\n", ibm->name);
  3446. if (ibm->read) {
  3447. entry = create_proc_entry(ibm->name,
  3448. S_IFREG | S_IRUGO | S_IWUSR,
  3449. proc_dir);
  3450. if (!entry) {
  3451. printk(IBM_ERR "unable to create proc entry %s\n",
  3452. ibm->name);
  3453. ret = -ENODEV;
  3454. goto err_out;
  3455. }
  3456. entry->owner = THIS_MODULE;
  3457. entry->data = ibm;
  3458. entry->read_proc = &dispatch_procfs_read;
  3459. if (ibm->write)
  3460. entry->write_proc = &dispatch_procfs_write;
  3461. ibm->flags.proc_created = 1;
  3462. }
  3463. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  3464. return 0;
  3465. err_out:
  3466. dbg_printk(TPACPI_DBG_INIT,
  3467. "%s: at error exit path with result %d\n",
  3468. ibm->name, ret);
  3469. ibm_exit(ibm);
  3470. return (ret < 0)? ret : 0;
  3471. }
  3472. static void ibm_exit(struct ibm_struct *ibm)
  3473. {
  3474. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  3475. list_del_init(&ibm->all_drivers);
  3476. if (ibm->flags.acpi_notify_installed) {
  3477. dbg_printk(TPACPI_DBG_EXIT,
  3478. "%s: acpi_remove_notify_handler\n", ibm->name);
  3479. BUG_ON(!ibm->acpi);
  3480. acpi_remove_notify_handler(*ibm->acpi->handle,
  3481. ibm->acpi->type,
  3482. dispatch_acpi_notify);
  3483. ibm->flags.acpi_notify_installed = 0;
  3484. ibm->flags.acpi_notify_installed = 0;
  3485. }
  3486. if (ibm->flags.proc_created) {
  3487. dbg_printk(TPACPI_DBG_EXIT,
  3488. "%s: remove_proc_entry\n", ibm->name);
  3489. remove_proc_entry(ibm->name, proc_dir);
  3490. ibm->flags.proc_created = 0;
  3491. }
  3492. if (ibm->flags.acpi_driver_registered) {
  3493. dbg_printk(TPACPI_DBG_EXIT,
  3494. "%s: acpi_bus_unregister_driver\n", ibm->name);
  3495. BUG_ON(!ibm->acpi);
  3496. acpi_bus_unregister_driver(ibm->acpi->driver);
  3497. kfree(ibm->acpi->driver);
  3498. ibm->acpi->driver = NULL;
  3499. ibm->flags.acpi_driver_registered = 0;
  3500. }
  3501. if (ibm->flags.init_called && ibm->exit) {
  3502. ibm->exit();
  3503. ibm->flags.init_called = 0;
  3504. }
  3505. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  3506. }
  3507. /* Probing */
  3508. static char *ibm_thinkpad_ec_found;
  3509. static char* __init check_dmi_for_ec(void)
  3510. {
  3511. struct dmi_device *dev = NULL;
  3512. char ec_fw_string[18];
  3513. /*
  3514. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  3515. * X32 or newer, all Z series; Some models must have an
  3516. * up-to-date BIOS or they will not be detected.
  3517. *
  3518. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  3519. */
  3520. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  3521. if (sscanf(dev->name,
  3522. "IBM ThinkPad Embedded Controller -[%17c",
  3523. ec_fw_string) == 1) {
  3524. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  3525. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  3526. return kstrdup(ec_fw_string, GFP_KERNEL);
  3527. }
  3528. }
  3529. return NULL;
  3530. }
  3531. static int __init probe_for_thinkpad(void)
  3532. {
  3533. int is_thinkpad;
  3534. if (acpi_disabled)
  3535. return -ENODEV;
  3536. /*
  3537. * Non-ancient models have better DMI tagging, but very old models
  3538. * don't.
  3539. */
  3540. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  3541. /* ec is required because many other handles are relative to it */
  3542. IBM_ACPIHANDLE_INIT(ec);
  3543. if (!ec_handle) {
  3544. if (is_thinkpad)
  3545. printk(IBM_ERR
  3546. "Not yet supported ThinkPad detected!\n");
  3547. return -ENODEV;
  3548. }
  3549. /*
  3550. * Risks a regression on very old machines, but reduces potential
  3551. * false positives a damn great deal
  3552. */
  3553. if (!is_thinkpad)
  3554. is_thinkpad = dmi_name_in_vendors("IBM");
  3555. if (!is_thinkpad && !force_load)
  3556. return -ENODEV;
  3557. return 0;
  3558. }
  3559. /* Module init, exit, parameters */
  3560. static struct ibm_init_struct ibms_init[] __initdata = {
  3561. {
  3562. .init = thinkpad_acpi_driver_init,
  3563. .data = &thinkpad_acpi_driver_data,
  3564. },
  3565. {
  3566. .init = hotkey_init,
  3567. .data = &hotkey_driver_data,
  3568. },
  3569. {
  3570. .init = bluetooth_init,
  3571. .data = &bluetooth_driver_data,
  3572. },
  3573. {
  3574. .init = wan_init,
  3575. .data = &wan_driver_data,
  3576. },
  3577. {
  3578. .init = video_init,
  3579. .data = &video_driver_data,
  3580. },
  3581. {
  3582. .init = light_init,
  3583. .data = &light_driver_data,
  3584. },
  3585. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3586. {
  3587. .init = dock_init,
  3588. .data = &dock_driver_data[0],
  3589. },
  3590. {
  3591. .init = dock_init2,
  3592. .data = &dock_driver_data[1],
  3593. },
  3594. #endif
  3595. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3596. {
  3597. .init = bay_init,
  3598. .data = &bay_driver_data,
  3599. },
  3600. #endif
  3601. {
  3602. .init = cmos_init,
  3603. .data = &cmos_driver_data,
  3604. },
  3605. {
  3606. .init = led_init,
  3607. .data = &led_driver_data,
  3608. },
  3609. {
  3610. .init = beep_init,
  3611. .data = &beep_driver_data,
  3612. },
  3613. {
  3614. .init = thermal_init,
  3615. .data = &thermal_driver_data,
  3616. },
  3617. {
  3618. .data = &ecdump_driver_data,
  3619. },
  3620. {
  3621. .init = brightness_init,
  3622. .data = &brightness_driver_data,
  3623. },
  3624. {
  3625. .data = &volume_driver_data,
  3626. },
  3627. {
  3628. .init = fan_init,
  3629. .data = &fan_driver_data,
  3630. },
  3631. };
  3632. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  3633. {
  3634. unsigned int i;
  3635. struct ibm_struct *ibm;
  3636. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3637. ibm = ibms_init[i].data;
  3638. BUG_ON(ibm == NULL);
  3639. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  3640. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  3641. return -ENOSPC;
  3642. strcpy(ibms_init[i].param, val);
  3643. strcat(ibms_init[i].param, ",");
  3644. return 0;
  3645. }
  3646. }
  3647. return -EINVAL;
  3648. }
  3649. static int experimental;
  3650. module_param(experimental, int, 0);
  3651. static u32 dbg_level;
  3652. module_param_named(debug, dbg_level, uint, 0);
  3653. static int force_load;
  3654. module_param(force_load, int, 0);
  3655. static int fan_control_allowed;
  3656. module_param_named(fan_control, fan_control_allowed, int, 0);
  3657. #define IBM_PARAM(feature) \
  3658. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  3659. IBM_PARAM(hotkey);
  3660. IBM_PARAM(bluetooth);
  3661. IBM_PARAM(video);
  3662. IBM_PARAM(light);
  3663. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3664. IBM_PARAM(dock);
  3665. #endif
  3666. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3667. IBM_PARAM(bay);
  3668. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  3669. IBM_PARAM(cmos);
  3670. IBM_PARAM(led);
  3671. IBM_PARAM(beep);
  3672. IBM_PARAM(ecdump);
  3673. IBM_PARAM(brightness);
  3674. IBM_PARAM(volume);
  3675. IBM_PARAM(fan);
  3676. static int __init thinkpad_acpi_module_init(void)
  3677. {
  3678. int ret, i;
  3679. /* Driver-level probe */
  3680. ret = probe_for_thinkpad();
  3681. if (ret)
  3682. return ret;
  3683. /* Driver initialization */
  3684. ibm_thinkpad_ec_found = check_dmi_for_ec();
  3685. IBM_ACPIHANDLE_INIT(ecrd);
  3686. IBM_ACPIHANDLE_INIT(ecwr);
  3687. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  3688. if (!proc_dir) {
  3689. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  3690. thinkpad_acpi_module_exit();
  3691. return -ENODEV;
  3692. }
  3693. proc_dir->owner = THIS_MODULE;
  3694. ret = platform_driver_register(&tpacpi_pdriver);
  3695. if (ret) {
  3696. printk(IBM_ERR "unable to register platform driver\n");
  3697. thinkpad_acpi_module_exit();
  3698. return ret;
  3699. }
  3700. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  3701. if (ret) {
  3702. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  3703. thinkpad_acpi_module_exit();
  3704. return ret;
  3705. }
  3706. /* Device initialization */
  3707. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  3708. NULL, 0);
  3709. if (IS_ERR(tpacpi_pdev)) {
  3710. ret = PTR_ERR(tpacpi_pdev);
  3711. tpacpi_pdev = NULL;
  3712. printk(IBM_ERR "unable to register platform device\n");
  3713. thinkpad_acpi_module_exit();
  3714. return ret;
  3715. }
  3716. tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
  3717. if (IS_ERR(tpacpi_hwmon)) {
  3718. ret = PTR_ERR(tpacpi_hwmon);
  3719. tpacpi_hwmon = NULL;
  3720. printk(IBM_ERR "unable to register hwmon device\n");
  3721. thinkpad_acpi_module_exit();
  3722. return ret;
  3723. }
  3724. tpacpi_inputdev = input_allocate_device();
  3725. if (!tpacpi_inputdev) {
  3726. printk(IBM_ERR "unable to allocate input device\n");
  3727. thinkpad_acpi_module_exit();
  3728. return -ENOMEM;
  3729. } else {
  3730. /* Prepare input device, but don't register */
  3731. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  3732. tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
  3733. tpacpi_inputdev->id.bustype = BUS_HOST;
  3734. tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
  3735. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  3736. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  3737. }
  3738. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3739. ret = ibm_init(&ibms_init[i]);
  3740. if (ret >= 0 && *ibms_init[i].param)
  3741. ret = ibms_init[i].data->write(ibms_init[i].param);
  3742. if (ret < 0) {
  3743. thinkpad_acpi_module_exit();
  3744. return ret;
  3745. }
  3746. }
  3747. ret = input_register_device(tpacpi_inputdev);
  3748. if (ret < 0) {
  3749. printk(IBM_ERR "unable to register input device\n");
  3750. thinkpad_acpi_module_exit();
  3751. return ret;
  3752. } else {
  3753. tp_features.input_device_registered = 1;
  3754. }
  3755. return 0;
  3756. }
  3757. static void thinkpad_acpi_module_exit(void)
  3758. {
  3759. struct ibm_struct *ibm, *itmp;
  3760. list_for_each_entry_safe_reverse(ibm, itmp,
  3761. &tpacpi_all_drivers,
  3762. all_drivers) {
  3763. ibm_exit(ibm);
  3764. }
  3765. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  3766. if (tpacpi_inputdev) {
  3767. if (tp_features.input_device_registered)
  3768. input_unregister_device(tpacpi_inputdev);
  3769. else
  3770. input_free_device(tpacpi_inputdev);
  3771. }
  3772. if (tpacpi_hwmon)
  3773. hwmon_device_unregister(tpacpi_hwmon);
  3774. if (tpacpi_pdev)
  3775. platform_device_unregister(tpacpi_pdev);
  3776. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  3777. platform_driver_unregister(&tpacpi_pdriver);
  3778. if (proc_dir)
  3779. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  3780. kfree(ibm_thinkpad_ec_found);
  3781. }
  3782. module_init(thinkpad_acpi_module_init);
  3783. module_exit(thinkpad_acpi_module_exit);