thinkpad_acpi.c 100 KB

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