thinkpad_acpi.c 110 KB

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