thinkpad_acpi.c 113 KB

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