thinkpad_acpi.c 112 KB

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