thinkpad_acpi.c 123 KB

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