thinkpad_acpi.c 115 KB

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