thinkpad_acpi.c 103 KB

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