thinkpad_acpi.c 107 KB

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