thinkpad_acpi.c 112 KB

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