thinkpad_acpi.c 113 KB

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