thinkpad_acpi.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255
  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-2008 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 TPACPI_VERSION "0.19"
  24. #define TPACPI_SYSFS_VERSION 0x020200
  25. /*
  26. * Changelog:
  27. * 2007-10-20 changelog trimmed down
  28. *
  29. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  30. * drivers/misc.
  31. *
  32. * 2006-11-22 0.13 new maintainer
  33. * changelog now lives in git commit history, and will
  34. * not be updated further in-file.
  35. *
  36. * 2005-03-17 0.11 support for 600e, 770x
  37. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  38. *
  39. * 2005-01-16 0.9 use MODULE_VERSION
  40. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  41. * fix parameter passing on module loading
  42. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  43. * thanks to Jim Radford <radford@blackbean.org>
  44. * 2004-11-08 0.8 fix init error case, don't return from a macro
  45. * thanks to Chris Wright <chrisw@osdl.org>
  46. */
  47. #include <linux/kernel.h>
  48. #include <linux/module.h>
  49. #include <linux/init.h>
  50. #include <linux/types.h>
  51. #include <linux/string.h>
  52. #include <linux/list.h>
  53. #include <linux/mutex.h>
  54. #include <linux/kthread.h>
  55. #include <linux/freezer.h>
  56. #include <linux/delay.h>
  57. #include <linux/nvram.h>
  58. #include <linux/proc_fs.h>
  59. #include <linux/sysfs.h>
  60. #include <linux/backlight.h>
  61. #include <linux/fb.h>
  62. #include <linux/platform_device.h>
  63. #include <linux/hwmon.h>
  64. #include <linux/hwmon-sysfs.h>
  65. #include <linux/input.h>
  66. #include <asm/uaccess.h>
  67. #include <linux/dmi.h>
  68. #include <linux/jiffies.h>
  69. #include <linux/workqueue.h>
  70. #include <acpi/acpi_drivers.h>
  71. #include <acpi/acnamesp.h>
  72. #include <linux/pci_ids.h>
  73. /* ThinkPad CMOS commands */
  74. #define TP_CMOS_VOLUME_DOWN 0
  75. #define TP_CMOS_VOLUME_UP 1
  76. #define TP_CMOS_VOLUME_MUTE 2
  77. #define TP_CMOS_BRIGHTNESS_UP 4
  78. #define TP_CMOS_BRIGHTNESS_DOWN 5
  79. /* NVRAM Addresses */
  80. enum tp_nvram_addr {
  81. TP_NVRAM_ADDR_HK2 = 0x57,
  82. TP_NVRAM_ADDR_THINKLIGHT = 0x58,
  83. TP_NVRAM_ADDR_VIDEO = 0x59,
  84. TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
  85. TP_NVRAM_ADDR_MIXER = 0x60,
  86. };
  87. /* NVRAM bit masks */
  88. enum {
  89. TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
  90. TP_NVRAM_MASK_HKT_ZOOM = 0x20,
  91. TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
  92. TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
  93. TP_NVRAM_MASK_THINKLIGHT = 0x10,
  94. TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
  95. TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
  96. TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
  97. TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
  98. TP_NVRAM_MASK_MUTE = 0x40,
  99. TP_NVRAM_MASK_HKT_VOLUME = 0x80,
  100. TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
  101. TP_NVRAM_POS_LEVEL_VOLUME = 0,
  102. };
  103. /* ACPI HIDs */
  104. #define TPACPI_ACPI_HKEY_HID "IBM0068"
  105. /* Input IDs */
  106. #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
  107. #define TPACPI_HKEY_INPUT_VERSION 0x4101
  108. /****************************************************************************
  109. * Main driver
  110. */
  111. #define TPACPI_NAME "thinkpad"
  112. #define TPACPI_DESC "ThinkPad ACPI Extras"
  113. #define TPACPI_FILE TPACPI_NAME "_acpi"
  114. #define TPACPI_URL "http://ibm-acpi.sf.net/"
  115. #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
  116. #define TPACPI_PROC_DIR "ibm"
  117. #define TPACPI_ACPI_EVENT_PREFIX "ibm"
  118. #define TPACPI_DRVR_NAME TPACPI_FILE
  119. #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
  120. #define TPACPI_MAX_ACPI_ARGS 3
  121. /* Debugging */
  122. #define TPACPI_LOG TPACPI_FILE ": "
  123. #define TPACPI_ERR KERN_ERR TPACPI_LOG
  124. #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
  125. #define TPACPI_INFO KERN_INFO TPACPI_LOG
  126. #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
  127. #define TPACPI_DBG_ALL 0xffff
  128. #define TPACPI_DBG_ALL 0xffff
  129. #define TPACPI_DBG_INIT 0x0001
  130. #define TPACPI_DBG_EXIT 0x0002
  131. #define dbg_printk(a_dbg_level, format, arg...) \
  132. do { if (dbg_level & a_dbg_level) \
  133. printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
  134. } while (0)
  135. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  136. #define vdbg_printk(a_dbg_level, format, arg...) \
  137. dbg_printk(a_dbg_level, format, ## arg)
  138. static const char *str_supported(int is_supported);
  139. #else
  140. #define vdbg_printk(a_dbg_level, format, arg...)
  141. #endif
  142. #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
  143. #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  144. #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
  145. /****************************************************************************
  146. * Driver-wide structs and misc. variables
  147. */
  148. struct ibm_struct;
  149. struct tp_acpi_drv_struct {
  150. const struct acpi_device_id *hid;
  151. struct acpi_driver *driver;
  152. void (*notify) (struct ibm_struct *, u32);
  153. acpi_handle *handle;
  154. u32 type;
  155. struct acpi_device *device;
  156. };
  157. struct ibm_struct {
  158. char *name;
  159. int (*read) (char *);
  160. int (*write) (char *);
  161. void (*exit) (void);
  162. void (*resume) (void);
  163. void (*suspend) (pm_message_t state);
  164. struct list_head all_drivers;
  165. struct tp_acpi_drv_struct *acpi;
  166. struct {
  167. u8 acpi_driver_registered:1;
  168. u8 acpi_notify_installed:1;
  169. u8 proc_created:1;
  170. u8 init_called:1;
  171. u8 experimental:1;
  172. } flags;
  173. };
  174. struct ibm_init_struct {
  175. char param[32];
  176. int (*init) (struct ibm_init_struct *);
  177. struct ibm_struct *data;
  178. };
  179. static struct {
  180. #ifdef CONFIG_THINKPAD_ACPI_BAY
  181. u32 bay_status:1;
  182. u32 bay_eject:1;
  183. u32 bay_status2:1;
  184. u32 bay_eject2:1;
  185. #endif
  186. u32 bluetooth:1;
  187. u32 hotkey:1;
  188. u32 hotkey_mask:1;
  189. u32 hotkey_wlsw:1;
  190. u32 hotkey_tablet:1;
  191. u32 light:1;
  192. u32 light_status:1;
  193. u32 bright_16levels:1;
  194. u32 wan:1;
  195. u32 fan_ctrl_status_undef:1;
  196. u32 input_device_registered:1;
  197. u32 platform_drv_registered:1;
  198. u32 platform_drv_attrs_registered:1;
  199. u32 sensors_pdrv_registered:1;
  200. u32 sensors_pdrv_attrs_registered:1;
  201. u32 sensors_pdev_attrs_registered:1;
  202. u32 hotkey_poll_active:1;
  203. } tp_features;
  204. struct thinkpad_id_data {
  205. unsigned int vendor; /* ThinkPad vendor:
  206. * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
  207. char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
  208. char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
  209. u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
  210. u16 ec_model;
  211. char *model_str;
  212. };
  213. static struct thinkpad_id_data thinkpad_id;
  214. static enum {
  215. TPACPI_LIFE_INIT = 0,
  216. TPACPI_LIFE_RUNNING,
  217. TPACPI_LIFE_EXITING,
  218. } tpacpi_lifecycle;
  219. static int experimental;
  220. static u32 dbg_level;
  221. /****************************************************************************
  222. ****************************************************************************
  223. *
  224. * ACPI Helpers and device model
  225. *
  226. ****************************************************************************
  227. ****************************************************************************/
  228. /*************************************************************************
  229. * ACPI basic handles
  230. */
  231. static acpi_handle root_handle;
  232. #define TPACPI_HANDLE(object, parent, paths...) \
  233. static acpi_handle object##_handle; \
  234. static acpi_handle *object##_parent = &parent##_handle; \
  235. static char *object##_path; \
  236. static char *object##_paths[] = { paths }
  237. TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  238. "\\_SB.PCI.ISA.EC", /* 570 */
  239. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  240. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  241. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  242. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  243. "\\_SB.PCI0.LPC.EC", /* all others */
  244. );
  245. TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  246. TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  247. TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
  248. /* T4x, X31, X40 */
  249. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  250. "\\CMS", /* R40, R40e */
  251. ); /* all others */
  252. TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  253. "^HKEY", /* R30, R31 */
  254. "HKEY", /* all others */
  255. ); /* 570 */
  256. TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  257. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  258. "\\_SB.PCI0.VID0", /* 770e */
  259. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  260. "\\_SB.PCI0.AGP.VID", /* all others */
  261. ); /* R30, R31 */
  262. /*************************************************************************
  263. * ACPI helpers
  264. */
  265. static int acpi_evalf(acpi_handle handle,
  266. void *res, char *method, char *fmt, ...)
  267. {
  268. char *fmt0 = fmt;
  269. struct acpi_object_list params;
  270. union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
  271. struct acpi_buffer result, *resultp;
  272. union acpi_object out_obj;
  273. acpi_status status;
  274. va_list ap;
  275. char res_type;
  276. int success;
  277. int quiet;
  278. if (!*fmt) {
  279. printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
  280. return 0;
  281. }
  282. if (*fmt == 'q') {
  283. quiet = 1;
  284. fmt++;
  285. } else
  286. quiet = 0;
  287. res_type = *(fmt++);
  288. params.count = 0;
  289. params.pointer = &in_objs[0];
  290. va_start(ap, fmt);
  291. while (*fmt) {
  292. char c = *(fmt++);
  293. switch (c) {
  294. case 'd': /* int */
  295. in_objs[params.count].integer.value = va_arg(ap, int);
  296. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  297. break;
  298. /* add more types as needed */
  299. default:
  300. printk(TPACPI_ERR "acpi_evalf() called "
  301. "with invalid format character '%c'\n", c);
  302. return 0;
  303. }
  304. }
  305. va_end(ap);
  306. if (res_type != 'v') {
  307. result.length = sizeof(out_obj);
  308. result.pointer = &out_obj;
  309. resultp = &result;
  310. } else
  311. resultp = NULL;
  312. status = acpi_evaluate_object(handle, method, &params, resultp);
  313. switch (res_type) {
  314. case 'd': /* int */
  315. if (res)
  316. *(int *)res = out_obj.integer.value;
  317. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  318. break;
  319. case 'v': /* void */
  320. success = status == AE_OK;
  321. break;
  322. /* add more types as needed */
  323. default:
  324. printk(TPACPI_ERR "acpi_evalf() called "
  325. "with invalid format character '%c'\n", res_type);
  326. return 0;
  327. }
  328. if (!success && !quiet)
  329. printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  330. method, fmt0, status);
  331. return success;
  332. }
  333. static int acpi_ec_read(int i, u8 *p)
  334. {
  335. int v;
  336. if (ecrd_handle) {
  337. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  338. return 0;
  339. *p = v;
  340. } else {
  341. if (ec_read(i, p) < 0)
  342. return 0;
  343. }
  344. return 1;
  345. }
  346. static int acpi_ec_write(int i, u8 v)
  347. {
  348. if (ecwr_handle) {
  349. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  350. return 0;
  351. } else {
  352. if (ec_write(i, v) < 0)
  353. return 0;
  354. }
  355. return 1;
  356. }
  357. #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
  358. static int _sta(acpi_handle handle)
  359. {
  360. int status;
  361. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  362. status = 0;
  363. return status;
  364. }
  365. #endif
  366. static int issue_thinkpad_cmos_command(int cmos_cmd)
  367. {
  368. if (!cmos_handle)
  369. return -ENXIO;
  370. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  371. return -EIO;
  372. return 0;
  373. }
  374. /*************************************************************************
  375. * ACPI device model
  376. */
  377. #define TPACPI_ACPIHANDLE_INIT(object) \
  378. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  379. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  380. static void drv_acpi_handle_init(char *name,
  381. acpi_handle *handle, acpi_handle parent,
  382. char **paths, int num_paths, char **path)
  383. {
  384. int i;
  385. acpi_status status;
  386. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  387. name);
  388. for (i = 0; i < num_paths; i++) {
  389. status = acpi_get_handle(parent, paths[i], handle);
  390. if (ACPI_SUCCESS(status)) {
  391. *path = paths[i];
  392. dbg_printk(TPACPI_DBG_INIT,
  393. "Found ACPI handle %s for %s\n",
  394. *path, name);
  395. return;
  396. }
  397. }
  398. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  399. name);
  400. *handle = NULL;
  401. }
  402. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  403. {
  404. struct ibm_struct *ibm = data;
  405. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  406. return;
  407. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  408. return;
  409. ibm->acpi->notify(ibm, event);
  410. }
  411. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  412. {
  413. acpi_status status;
  414. int rc;
  415. BUG_ON(!ibm->acpi);
  416. if (!*ibm->acpi->handle)
  417. return 0;
  418. vdbg_printk(TPACPI_DBG_INIT,
  419. "setting up ACPI notify for %s\n", ibm->name);
  420. rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  421. if (rc < 0) {
  422. printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
  423. ibm->name, rc);
  424. return -ENODEV;
  425. }
  426. acpi_driver_data(ibm->acpi->device) = ibm;
  427. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  428. TPACPI_ACPI_EVENT_PREFIX,
  429. ibm->name);
  430. status = acpi_install_notify_handler(*ibm->acpi->handle,
  431. ibm->acpi->type, dispatch_acpi_notify, ibm);
  432. if (ACPI_FAILURE(status)) {
  433. if (status == AE_ALREADY_EXISTS) {
  434. printk(TPACPI_NOTICE
  435. "another device driver is already "
  436. "handling %s events\n", ibm->name);
  437. } else {
  438. printk(TPACPI_ERR
  439. "acpi_install_notify_handler(%s) failed: %d\n",
  440. ibm->name, status);
  441. }
  442. return -ENODEV;
  443. }
  444. ibm->flags.acpi_notify_installed = 1;
  445. return 0;
  446. }
  447. static int __init tpacpi_device_add(struct acpi_device *device)
  448. {
  449. return 0;
  450. }
  451. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  452. {
  453. int rc;
  454. dbg_printk(TPACPI_DBG_INIT,
  455. "registering %s as an ACPI driver\n", ibm->name);
  456. BUG_ON(!ibm->acpi);
  457. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  458. if (!ibm->acpi->driver) {
  459. printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
  460. return -ENOMEM;
  461. }
  462. sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
  463. ibm->acpi->driver->ids = ibm->acpi->hid;
  464. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  465. rc = acpi_bus_register_driver(ibm->acpi->driver);
  466. if (rc < 0) {
  467. printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  468. ibm->name, rc);
  469. kfree(ibm->acpi->driver);
  470. ibm->acpi->driver = NULL;
  471. } else if (!rc)
  472. ibm->flags.acpi_driver_registered = 1;
  473. return rc;
  474. }
  475. /****************************************************************************
  476. ****************************************************************************
  477. *
  478. * Procfs Helpers
  479. *
  480. ****************************************************************************
  481. ****************************************************************************/
  482. static int dispatch_procfs_read(char *page, char **start, off_t off,
  483. int count, int *eof, void *data)
  484. {
  485. struct ibm_struct *ibm = data;
  486. int len;
  487. if (!ibm || !ibm->read)
  488. return -EINVAL;
  489. len = ibm->read(page);
  490. if (len < 0)
  491. return len;
  492. if (len <= off + count)
  493. *eof = 1;
  494. *start = page + off;
  495. len -= off;
  496. if (len > count)
  497. len = count;
  498. if (len < 0)
  499. len = 0;
  500. return len;
  501. }
  502. static int dispatch_procfs_write(struct file *file,
  503. const char __user *userbuf,
  504. unsigned long count, void *data)
  505. {
  506. struct ibm_struct *ibm = data;
  507. char *kernbuf;
  508. int ret;
  509. if (!ibm || !ibm->write)
  510. return -EINVAL;
  511. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  512. if (!kernbuf)
  513. return -ENOMEM;
  514. if (copy_from_user(kernbuf, userbuf, count)) {
  515. kfree(kernbuf);
  516. return -EFAULT;
  517. }
  518. kernbuf[count] = 0;
  519. strcat(kernbuf, ",");
  520. ret = ibm->write(kernbuf);
  521. if (ret == 0)
  522. ret = count;
  523. kfree(kernbuf);
  524. return ret;
  525. }
  526. static char *next_cmd(char **cmds)
  527. {
  528. char *start = *cmds;
  529. char *end;
  530. while ((end = strchr(start, ',')) && end == start)
  531. start = end + 1;
  532. if (!end)
  533. return NULL;
  534. *end = 0;
  535. *cmds = end + 1;
  536. return start;
  537. }
  538. /****************************************************************************
  539. ****************************************************************************
  540. *
  541. * Device model: input, hwmon and platform
  542. *
  543. ****************************************************************************
  544. ****************************************************************************/
  545. static struct platform_device *tpacpi_pdev;
  546. static struct platform_device *tpacpi_sensors_pdev;
  547. static struct device *tpacpi_hwmon;
  548. static struct input_dev *tpacpi_inputdev;
  549. static struct mutex tpacpi_inputdev_send_mutex;
  550. static LIST_HEAD(tpacpi_all_drivers);
  551. static int tpacpi_suspend_handler(struct platform_device *pdev,
  552. pm_message_t state)
  553. {
  554. struct ibm_struct *ibm, *itmp;
  555. list_for_each_entry_safe(ibm, itmp,
  556. &tpacpi_all_drivers,
  557. all_drivers) {
  558. if (ibm->suspend)
  559. (ibm->suspend)(state);
  560. }
  561. return 0;
  562. }
  563. static int tpacpi_resume_handler(struct platform_device *pdev)
  564. {
  565. struct ibm_struct *ibm, *itmp;
  566. list_for_each_entry_safe(ibm, itmp,
  567. &tpacpi_all_drivers,
  568. all_drivers) {
  569. if (ibm->resume)
  570. (ibm->resume)();
  571. }
  572. return 0;
  573. }
  574. static struct platform_driver tpacpi_pdriver = {
  575. .driver = {
  576. .name = TPACPI_DRVR_NAME,
  577. .owner = THIS_MODULE,
  578. },
  579. .suspend = tpacpi_suspend_handler,
  580. .resume = tpacpi_resume_handler,
  581. };
  582. static struct platform_driver tpacpi_hwmon_pdriver = {
  583. .driver = {
  584. .name = TPACPI_HWMON_DRVR_NAME,
  585. .owner = THIS_MODULE,
  586. },
  587. };
  588. /*************************************************************************
  589. * sysfs support helpers
  590. */
  591. struct attribute_set {
  592. unsigned int members, max_members;
  593. struct attribute_group group;
  594. };
  595. struct attribute_set_obj {
  596. struct attribute_set s;
  597. struct attribute *a;
  598. } __attribute__((packed));
  599. static struct attribute_set *create_attr_set(unsigned int max_members,
  600. const char *name)
  601. {
  602. struct attribute_set_obj *sobj;
  603. if (max_members == 0)
  604. return NULL;
  605. /* Allocates space for implicit NULL at the end too */
  606. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  607. max_members * sizeof(struct attribute *),
  608. GFP_KERNEL);
  609. if (!sobj)
  610. return NULL;
  611. sobj->s.max_members = max_members;
  612. sobj->s.group.attrs = &sobj->a;
  613. sobj->s.group.name = name;
  614. return &sobj->s;
  615. }
  616. #define destroy_attr_set(_set) \
  617. kfree(_set);
  618. /* not multi-threaded safe, use it in a single thread per set */
  619. static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
  620. {
  621. if (!s || !attr)
  622. return -EINVAL;
  623. if (s->members >= s->max_members)
  624. return -ENOMEM;
  625. s->group.attrs[s->members] = attr;
  626. s->members++;
  627. return 0;
  628. }
  629. static int add_many_to_attr_set(struct attribute_set *s,
  630. struct attribute **attr,
  631. unsigned int count)
  632. {
  633. int i, res;
  634. for (i = 0; i < count; i++) {
  635. res = add_to_attr_set(s, attr[i]);
  636. if (res)
  637. return res;
  638. }
  639. return 0;
  640. }
  641. static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
  642. {
  643. sysfs_remove_group(kobj, &s->group);
  644. destroy_attr_set(s);
  645. }
  646. #define register_attr_set_with_sysfs(_attr_set, _kobj) \
  647. sysfs_create_group(_kobj, &_attr_set->group)
  648. static int parse_strtoul(const char *buf,
  649. unsigned long max, unsigned long *value)
  650. {
  651. char *endp;
  652. while (*buf && isspace(*buf))
  653. buf++;
  654. *value = simple_strtoul(buf, &endp, 0);
  655. while (*endp && isspace(*endp))
  656. endp++;
  657. if (*endp || *value > max)
  658. return -EINVAL;
  659. return 0;
  660. }
  661. /*************************************************************************
  662. * thinkpad-acpi driver attributes
  663. */
  664. /* interface_version --------------------------------------------------- */
  665. static ssize_t tpacpi_driver_interface_version_show(
  666. struct device_driver *drv,
  667. char *buf)
  668. {
  669. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  670. }
  671. static DRIVER_ATTR(interface_version, S_IRUGO,
  672. tpacpi_driver_interface_version_show, NULL);
  673. /* debug_level --------------------------------------------------------- */
  674. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  675. char *buf)
  676. {
  677. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  678. }
  679. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  680. const char *buf, size_t count)
  681. {
  682. unsigned long t;
  683. if (parse_strtoul(buf, 0xffff, &t))
  684. return -EINVAL;
  685. dbg_level = t;
  686. return count;
  687. }
  688. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  689. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  690. /* version ------------------------------------------------------------- */
  691. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  692. char *buf)
  693. {
  694. return snprintf(buf, PAGE_SIZE, "%s v%s\n",
  695. TPACPI_DESC, TPACPI_VERSION);
  696. }
  697. static DRIVER_ATTR(version, S_IRUGO,
  698. tpacpi_driver_version_show, NULL);
  699. /* --------------------------------------------------------------------- */
  700. static struct driver_attribute *tpacpi_driver_attributes[] = {
  701. &driver_attr_debug_level, &driver_attr_version,
  702. &driver_attr_interface_version,
  703. };
  704. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  705. {
  706. int i, res;
  707. i = 0;
  708. res = 0;
  709. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  710. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  711. i++;
  712. }
  713. return res;
  714. }
  715. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  716. {
  717. int i;
  718. for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  719. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  720. }
  721. /****************************************************************************
  722. ****************************************************************************
  723. *
  724. * Subdrivers
  725. *
  726. ****************************************************************************
  727. ****************************************************************************/
  728. /*************************************************************************
  729. * thinkpad-acpi init subdriver
  730. */
  731. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  732. {
  733. printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
  734. printk(TPACPI_INFO "%s\n", TPACPI_URL);
  735. printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
  736. (thinkpad_id.bios_version_str) ?
  737. thinkpad_id.bios_version_str : "unknown",
  738. (thinkpad_id.ec_version_str) ?
  739. thinkpad_id.ec_version_str : "unknown");
  740. if (thinkpad_id.vendor && thinkpad_id.model_str)
  741. printk(TPACPI_INFO "%s %s\n",
  742. (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
  743. "IBM" : ((thinkpad_id.vendor ==
  744. PCI_VENDOR_ID_LENOVO) ?
  745. "Lenovo" : "Unknown vendor"),
  746. thinkpad_id.model_str);
  747. return 0;
  748. }
  749. static int thinkpad_acpi_driver_read(char *p)
  750. {
  751. int len = 0;
  752. len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
  753. len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
  754. return len;
  755. }
  756. static struct ibm_struct thinkpad_acpi_driver_data = {
  757. .name = "driver",
  758. .read = thinkpad_acpi_driver_read,
  759. };
  760. /*************************************************************************
  761. * Hotkey subdriver
  762. */
  763. enum { /* hot key scan codes (derived from ACPI DSDT) */
  764. TP_ACPI_HOTKEYSCAN_FNF1 = 0,
  765. TP_ACPI_HOTKEYSCAN_FNF2,
  766. TP_ACPI_HOTKEYSCAN_FNF3,
  767. TP_ACPI_HOTKEYSCAN_FNF4,
  768. TP_ACPI_HOTKEYSCAN_FNF5,
  769. TP_ACPI_HOTKEYSCAN_FNF6,
  770. TP_ACPI_HOTKEYSCAN_FNF7,
  771. TP_ACPI_HOTKEYSCAN_FNF8,
  772. TP_ACPI_HOTKEYSCAN_FNF9,
  773. TP_ACPI_HOTKEYSCAN_FNF10,
  774. TP_ACPI_HOTKEYSCAN_FNF11,
  775. TP_ACPI_HOTKEYSCAN_FNF12,
  776. TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
  777. TP_ACPI_HOTKEYSCAN_FNINSERT,
  778. TP_ACPI_HOTKEYSCAN_FNDELETE,
  779. TP_ACPI_HOTKEYSCAN_FNHOME,
  780. TP_ACPI_HOTKEYSCAN_FNEND,
  781. TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  782. TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
  783. TP_ACPI_HOTKEYSCAN_FNSPACE,
  784. TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  785. TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  786. TP_ACPI_HOTKEYSCAN_MUTE,
  787. TP_ACPI_HOTKEYSCAN_THINKPAD,
  788. };
  789. enum { /* Keys available through NVRAM polling */
  790. TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
  791. TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
  792. };
  793. enum { /* Positions of some of the keys in hotkey masks */
  794. TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
  795. TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
  796. TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
  797. TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
  798. TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
  799. TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  800. TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
  801. TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  802. TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  803. TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
  804. TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
  805. };
  806. enum { /* NVRAM to ACPI HKEY group map */
  807. TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
  808. TP_ACPI_HKEY_ZOOM_MASK |
  809. TP_ACPI_HKEY_DISPSWTCH_MASK |
  810. TP_ACPI_HKEY_HIBERNATE_MASK,
  811. TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
  812. TP_ACPI_HKEY_BRGHTDWN_MASK,
  813. TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
  814. TP_ACPI_HKEY_VOLDWN_MASK |
  815. TP_ACPI_HKEY_MUTE_MASK,
  816. };
  817. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  818. struct tp_nvram_state {
  819. u16 thinkpad_toggle:1;
  820. u16 zoom_toggle:1;
  821. u16 display_toggle:1;
  822. u16 thinklight_toggle:1;
  823. u16 hibernate_toggle:1;
  824. u16 displayexp_toggle:1;
  825. u16 display_state:1;
  826. u16 brightness_toggle:1;
  827. u16 volume_toggle:1;
  828. u16 mute:1;
  829. u8 brightness_level;
  830. u8 volume_level;
  831. };
  832. static struct task_struct *tpacpi_hotkey_task;
  833. static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
  834. static int hotkey_poll_freq = 10; /* Hz */
  835. static struct mutex hotkey_thread_mutex;
  836. static struct mutex hotkey_thread_data_mutex;
  837. static unsigned int hotkey_config_change;
  838. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  839. #define hotkey_source_mask 0U
  840. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  841. static struct mutex hotkey_mutex;
  842. static enum { /* Reasons for waking up */
  843. TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
  844. TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
  845. TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
  846. } hotkey_wakeup_reason;
  847. static int hotkey_autosleep_ack;
  848. static int hotkey_orig_status;
  849. static u32 hotkey_orig_mask;
  850. static u32 hotkey_all_mask;
  851. static u32 hotkey_reserved_mask;
  852. static u32 hotkey_mask;
  853. static unsigned int hotkey_report_mode;
  854. static u16 *hotkey_keycode_map;
  855. static struct attribute_set *hotkey_dev_attributes;
  856. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  857. #define HOTKEY_CONFIG_CRITICAL_START \
  858. do { \
  859. mutex_lock(&hotkey_thread_data_mutex); \
  860. hotkey_config_change++; \
  861. } while (0);
  862. #define HOTKEY_CONFIG_CRITICAL_END \
  863. mutex_unlock(&hotkey_thread_data_mutex);
  864. #else
  865. #define HOTKEY_CONFIG_CRITICAL_START
  866. #define HOTKEY_CONFIG_CRITICAL_END
  867. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  868. /* HKEY.MHKG() return bits */
  869. #define TP_HOTKEY_TABLET_MASK (1 << 3)
  870. static int hotkey_get_wlsw(int *status)
  871. {
  872. if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
  873. return -EIO;
  874. return 0;
  875. }
  876. static int hotkey_get_tablet_mode(int *status)
  877. {
  878. int s;
  879. if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
  880. return -EIO;
  881. *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
  882. return 0;
  883. }
  884. /*
  885. * Call with hotkey_mutex held
  886. */
  887. static int hotkey_mask_get(void)
  888. {
  889. u32 m = 0;
  890. if (tp_features.hotkey_mask) {
  891. if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
  892. return -EIO;
  893. }
  894. hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
  895. return 0;
  896. }
  897. /*
  898. * Call with hotkey_mutex held
  899. */
  900. static int hotkey_mask_set(u32 mask)
  901. {
  902. int i;
  903. int rc = 0;
  904. if (tp_features.hotkey_mask) {
  905. HOTKEY_CONFIG_CRITICAL_START
  906. for (i = 0; i < 32; i++) {
  907. u32 m = 1 << i;
  908. /* enable in firmware mask only keys not in NVRAM
  909. * mode, but enable the key in the cached hotkey_mask
  910. * regardless of mode, or the key will end up
  911. * disabled by hotkey_mask_get() */
  912. if (!acpi_evalf(hkey_handle,
  913. NULL, "MHKM", "vdd", i + 1,
  914. !!((mask & ~hotkey_source_mask) & m))) {
  915. rc = -EIO;
  916. break;
  917. } else {
  918. hotkey_mask = (hotkey_mask & ~m) | (mask & m);
  919. }
  920. }
  921. HOTKEY_CONFIG_CRITICAL_END
  922. /* hotkey_mask_get must be called unconditionally below */
  923. if (!hotkey_mask_get() && !rc &&
  924. (hotkey_mask & ~hotkey_source_mask) !=
  925. (mask & ~hotkey_source_mask)) {
  926. printk(TPACPI_NOTICE
  927. "requested hot key mask 0x%08x, but "
  928. "firmware forced it to 0x%08x\n",
  929. mask, hotkey_mask);
  930. }
  931. } else {
  932. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  933. HOTKEY_CONFIG_CRITICAL_START
  934. hotkey_mask = mask & hotkey_source_mask;
  935. HOTKEY_CONFIG_CRITICAL_END
  936. hotkey_mask_get();
  937. if (hotkey_mask != mask) {
  938. printk(TPACPI_NOTICE
  939. "requested hot key mask 0x%08x, "
  940. "forced to 0x%08x (NVRAM poll mask is "
  941. "0x%08x): no firmware mask support\n",
  942. mask, hotkey_mask, hotkey_source_mask);
  943. }
  944. #else
  945. hotkey_mask_get();
  946. rc = -ENXIO;
  947. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  948. }
  949. return rc;
  950. }
  951. static int hotkey_status_get(int *status)
  952. {
  953. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  954. return -EIO;
  955. return 0;
  956. }
  957. static int hotkey_status_set(int status)
  958. {
  959. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  960. return -EIO;
  961. return 0;
  962. }
  963. static void tpacpi_input_send_radiosw(void)
  964. {
  965. int wlsw;
  966. if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
  967. mutex_lock(&tpacpi_inputdev_send_mutex);
  968. input_report_switch(tpacpi_inputdev,
  969. SW_RADIO, !!wlsw);
  970. input_sync(tpacpi_inputdev);
  971. mutex_unlock(&tpacpi_inputdev_send_mutex);
  972. }
  973. }
  974. static void tpacpi_input_send_tabletsw(void)
  975. {
  976. int state;
  977. if (tp_features.hotkey_tablet &&
  978. !hotkey_get_tablet_mode(&state)) {
  979. mutex_lock(&tpacpi_inputdev_send_mutex);
  980. input_report_switch(tpacpi_inputdev,
  981. SW_TABLET_MODE, !!state);
  982. input_sync(tpacpi_inputdev);
  983. mutex_unlock(&tpacpi_inputdev_send_mutex);
  984. }
  985. }
  986. static void tpacpi_input_send_key(unsigned int scancode)
  987. {
  988. unsigned int keycode;
  989. keycode = hotkey_keycode_map[scancode];
  990. if (keycode != KEY_RESERVED) {
  991. mutex_lock(&tpacpi_inputdev_send_mutex);
  992. input_report_key(tpacpi_inputdev, keycode, 1);
  993. if (keycode == KEY_UNKNOWN)
  994. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  995. scancode);
  996. input_sync(tpacpi_inputdev);
  997. input_report_key(tpacpi_inputdev, keycode, 0);
  998. if (keycode == KEY_UNKNOWN)
  999. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1000. scancode);
  1001. input_sync(tpacpi_inputdev);
  1002. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1003. }
  1004. }
  1005. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1006. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1007. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1008. {
  1009. tpacpi_input_send_key(scancode);
  1010. if (hotkey_report_mode < 2) {
  1011. acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
  1012. 0x80, 0x1001 + scancode);
  1013. }
  1014. }
  1015. static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
  1016. {
  1017. u8 d;
  1018. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1019. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1020. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1021. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1022. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1023. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1024. }
  1025. if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
  1026. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1027. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1028. }
  1029. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1030. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1031. n->displayexp_toggle =
  1032. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1033. }
  1034. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1035. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1036. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1037. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1038. n->brightness_toggle =
  1039. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1040. }
  1041. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1042. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1043. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1044. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1045. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1046. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1047. }
  1048. }
  1049. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1050. do { \
  1051. if ((mask & (1 << __scancode)) && \
  1052. oldn->__member != newn->__member) \
  1053. tpacpi_hotkey_send_key(__scancode); \
  1054. } while (0)
  1055. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1056. do { if (mask & (1 << __scancode)) \
  1057. tpacpi_hotkey_send_key(__scancode); } while (0)
  1058. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  1059. struct tp_nvram_state *newn,
  1060. u32 mask)
  1061. {
  1062. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  1063. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  1064. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  1065. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  1066. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  1067. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  1068. /* handle volume */
  1069. if (oldn->volume_toggle != newn->volume_toggle) {
  1070. if (oldn->mute != newn->mute) {
  1071. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1072. }
  1073. if (oldn->volume_level > newn->volume_level) {
  1074. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1075. } else if (oldn->volume_level < newn->volume_level) {
  1076. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1077. } else if (oldn->mute == newn->mute) {
  1078. /* repeated key presses that didn't change state */
  1079. if (newn->mute) {
  1080. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1081. } else if (newn->volume_level != 0) {
  1082. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1083. } else {
  1084. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1085. }
  1086. }
  1087. }
  1088. /* handle brightness */
  1089. if (oldn->brightness_toggle != newn->brightness_toggle) {
  1090. if (oldn->brightness_level < newn->brightness_level) {
  1091. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1092. } else if (oldn->brightness_level > newn->brightness_level) {
  1093. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1094. } else {
  1095. /* repeated key presses that didn't change state */
  1096. if (newn->brightness_level != 0) {
  1097. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1098. } else {
  1099. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1100. }
  1101. }
  1102. }
  1103. }
  1104. #undef TPACPI_COMPARE_KEY
  1105. #undef TPACPI_MAY_SEND_KEY
  1106. static int hotkey_kthread(void *data)
  1107. {
  1108. struct tp_nvram_state s[2];
  1109. u32 mask;
  1110. unsigned int si, so;
  1111. unsigned long t;
  1112. unsigned int change_detector, must_reset;
  1113. mutex_lock(&hotkey_thread_mutex);
  1114. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  1115. goto exit;
  1116. set_freezable();
  1117. so = 0;
  1118. si = 1;
  1119. t = 0;
  1120. /* Initial state for compares */
  1121. mutex_lock(&hotkey_thread_data_mutex);
  1122. change_detector = hotkey_config_change;
  1123. mask = hotkey_source_mask & hotkey_mask;
  1124. mutex_unlock(&hotkey_thread_data_mutex);
  1125. hotkey_read_nvram(&s[so], mask);
  1126. while (!kthread_should_stop() && hotkey_poll_freq) {
  1127. if (t == 0)
  1128. t = 1000/hotkey_poll_freq;
  1129. t = msleep_interruptible(t);
  1130. if (unlikely(kthread_should_stop()))
  1131. break;
  1132. must_reset = try_to_freeze();
  1133. if (t > 0 && !must_reset)
  1134. continue;
  1135. mutex_lock(&hotkey_thread_data_mutex);
  1136. if (must_reset || hotkey_config_change != change_detector) {
  1137. /* forget old state on thaw or config change */
  1138. si = so;
  1139. t = 0;
  1140. change_detector = hotkey_config_change;
  1141. }
  1142. mask = hotkey_source_mask & hotkey_mask;
  1143. mutex_unlock(&hotkey_thread_data_mutex);
  1144. if (likely(mask)) {
  1145. hotkey_read_nvram(&s[si], mask);
  1146. if (likely(si != so)) {
  1147. hotkey_compare_and_issue_event(&s[so], &s[si],
  1148. mask);
  1149. }
  1150. }
  1151. so = si;
  1152. si ^= 1;
  1153. }
  1154. exit:
  1155. mutex_unlock(&hotkey_thread_mutex);
  1156. return 0;
  1157. }
  1158. static void hotkey_poll_stop_sync(void)
  1159. {
  1160. if (tpacpi_hotkey_task) {
  1161. if (frozen(tpacpi_hotkey_task) ||
  1162. freezing(tpacpi_hotkey_task))
  1163. thaw_process(tpacpi_hotkey_task);
  1164. kthread_stop(tpacpi_hotkey_task);
  1165. tpacpi_hotkey_task = NULL;
  1166. mutex_lock(&hotkey_thread_mutex);
  1167. /* at this point, the thread did exit */
  1168. mutex_unlock(&hotkey_thread_mutex);
  1169. }
  1170. }
  1171. /* call with hotkey_mutex held */
  1172. static void hotkey_poll_setup(int may_warn)
  1173. {
  1174. if ((hotkey_source_mask & hotkey_mask) != 0 &&
  1175. hotkey_poll_freq > 0 &&
  1176. (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
  1177. if (!tpacpi_hotkey_task) {
  1178. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  1179. NULL,
  1180. TPACPI_FILE "d");
  1181. if (IS_ERR(tpacpi_hotkey_task)) {
  1182. tpacpi_hotkey_task = NULL;
  1183. printk(TPACPI_ERR
  1184. "could not create kernel thread "
  1185. "for hotkey polling\n");
  1186. }
  1187. }
  1188. } else {
  1189. hotkey_poll_stop_sync();
  1190. if (may_warn &&
  1191. hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
  1192. printk(TPACPI_NOTICE
  1193. "hot keys 0x%08x require polling, "
  1194. "which is currently disabled\n",
  1195. hotkey_source_mask);
  1196. }
  1197. }
  1198. }
  1199. static void hotkey_poll_setup_safe(int may_warn)
  1200. {
  1201. mutex_lock(&hotkey_mutex);
  1202. hotkey_poll_setup(may_warn);
  1203. mutex_unlock(&hotkey_mutex);
  1204. }
  1205. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1206. static void hotkey_poll_setup_safe(int __unused)
  1207. {
  1208. }
  1209. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1210. static int hotkey_inputdev_open(struct input_dev *dev)
  1211. {
  1212. switch (tpacpi_lifecycle) {
  1213. case TPACPI_LIFE_INIT:
  1214. /*
  1215. * hotkey_init will call hotkey_poll_setup_safe
  1216. * at the appropriate moment
  1217. */
  1218. return 0;
  1219. case TPACPI_LIFE_EXITING:
  1220. return -EBUSY;
  1221. case TPACPI_LIFE_RUNNING:
  1222. hotkey_poll_setup_safe(0);
  1223. return 0;
  1224. }
  1225. /* Should only happen if tpacpi_lifecycle is corrupt */
  1226. BUG();
  1227. return -EBUSY;
  1228. }
  1229. static void hotkey_inputdev_close(struct input_dev *dev)
  1230. {
  1231. /* disable hotkey polling when possible */
  1232. if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
  1233. hotkey_poll_setup_safe(0);
  1234. }
  1235. /* sysfs hotkey enable ------------------------------------------------- */
  1236. static ssize_t hotkey_enable_show(struct device *dev,
  1237. struct device_attribute *attr,
  1238. char *buf)
  1239. {
  1240. int res, status;
  1241. res = hotkey_status_get(&status);
  1242. if (res)
  1243. return res;
  1244. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  1245. }
  1246. static ssize_t hotkey_enable_store(struct device *dev,
  1247. struct device_attribute *attr,
  1248. const char *buf, size_t count)
  1249. {
  1250. unsigned long t;
  1251. int res;
  1252. if (parse_strtoul(buf, 1, &t))
  1253. return -EINVAL;
  1254. res = hotkey_status_set(t);
  1255. return (res) ? res : count;
  1256. }
  1257. static struct device_attribute dev_attr_hotkey_enable =
  1258. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  1259. hotkey_enable_show, hotkey_enable_store);
  1260. /* sysfs hotkey mask --------------------------------------------------- */
  1261. static ssize_t hotkey_mask_show(struct device *dev,
  1262. struct device_attribute *attr,
  1263. char *buf)
  1264. {
  1265. int res;
  1266. if (mutex_lock_interruptible(&hotkey_mutex))
  1267. return -ERESTARTSYS;
  1268. res = hotkey_mask_get();
  1269. mutex_unlock(&hotkey_mutex);
  1270. return (res)?
  1271. res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
  1272. }
  1273. static ssize_t hotkey_mask_store(struct device *dev,
  1274. struct device_attribute *attr,
  1275. const char *buf, size_t count)
  1276. {
  1277. unsigned long t;
  1278. int res;
  1279. if (parse_strtoul(buf, 0xffffffffUL, &t))
  1280. return -EINVAL;
  1281. if (mutex_lock_interruptible(&hotkey_mutex))
  1282. return -ERESTARTSYS;
  1283. res = hotkey_mask_set(t);
  1284. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1285. hotkey_poll_setup(1);
  1286. #endif
  1287. mutex_unlock(&hotkey_mutex);
  1288. return (res) ? res : count;
  1289. }
  1290. static struct device_attribute dev_attr_hotkey_mask =
  1291. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  1292. hotkey_mask_show, hotkey_mask_store);
  1293. /* sysfs hotkey bios_enabled ------------------------------------------- */
  1294. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  1295. struct device_attribute *attr,
  1296. char *buf)
  1297. {
  1298. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
  1299. }
  1300. static struct device_attribute dev_attr_hotkey_bios_enabled =
  1301. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  1302. /* sysfs hotkey bios_mask ---------------------------------------------- */
  1303. static ssize_t hotkey_bios_mask_show(struct device *dev,
  1304. struct device_attribute *attr,
  1305. char *buf)
  1306. {
  1307. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  1308. }
  1309. static struct device_attribute dev_attr_hotkey_bios_mask =
  1310. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  1311. /* sysfs hotkey all_mask ----------------------------------------------- */
  1312. static ssize_t hotkey_all_mask_show(struct device *dev,
  1313. struct device_attribute *attr,
  1314. char *buf)
  1315. {
  1316. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  1317. hotkey_all_mask | hotkey_source_mask);
  1318. }
  1319. static struct device_attribute dev_attr_hotkey_all_mask =
  1320. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  1321. /* sysfs hotkey recommended_mask --------------------------------------- */
  1322. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  1323. struct device_attribute *attr,
  1324. char *buf)
  1325. {
  1326. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  1327. (hotkey_all_mask | hotkey_source_mask)
  1328. & ~hotkey_reserved_mask);
  1329. }
  1330. static struct device_attribute dev_attr_hotkey_recommended_mask =
  1331. __ATTR(hotkey_recommended_mask, S_IRUGO,
  1332. hotkey_recommended_mask_show, NULL);
  1333. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1334. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  1335. static ssize_t hotkey_source_mask_show(struct device *dev,
  1336. struct device_attribute *attr,
  1337. char *buf)
  1338. {
  1339. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
  1340. }
  1341. static ssize_t hotkey_source_mask_store(struct device *dev,
  1342. struct device_attribute *attr,
  1343. const char *buf, size_t count)
  1344. {
  1345. unsigned long t;
  1346. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  1347. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  1348. return -EINVAL;
  1349. if (mutex_lock_interruptible(&hotkey_mutex))
  1350. return -ERESTARTSYS;
  1351. HOTKEY_CONFIG_CRITICAL_START
  1352. hotkey_source_mask = t;
  1353. HOTKEY_CONFIG_CRITICAL_END
  1354. hotkey_poll_setup(1);
  1355. mutex_unlock(&hotkey_mutex);
  1356. return count;
  1357. }
  1358. static struct device_attribute dev_attr_hotkey_source_mask =
  1359. __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
  1360. hotkey_source_mask_show, hotkey_source_mask_store);
  1361. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  1362. static ssize_t hotkey_poll_freq_show(struct device *dev,
  1363. struct device_attribute *attr,
  1364. char *buf)
  1365. {
  1366. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
  1367. }
  1368. static ssize_t hotkey_poll_freq_store(struct device *dev,
  1369. struct device_attribute *attr,
  1370. const char *buf, size_t count)
  1371. {
  1372. unsigned long t;
  1373. if (parse_strtoul(buf, 25, &t))
  1374. return -EINVAL;
  1375. if (mutex_lock_interruptible(&hotkey_mutex))
  1376. return -ERESTARTSYS;
  1377. hotkey_poll_freq = t;
  1378. hotkey_poll_setup(1);
  1379. mutex_unlock(&hotkey_mutex);
  1380. return count;
  1381. }
  1382. static struct device_attribute dev_attr_hotkey_poll_freq =
  1383. __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
  1384. hotkey_poll_freq_show, hotkey_poll_freq_store);
  1385. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1386. /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
  1387. static ssize_t hotkey_radio_sw_show(struct device *dev,
  1388. struct device_attribute *attr,
  1389. char *buf)
  1390. {
  1391. int res, s;
  1392. res = hotkey_get_wlsw(&s);
  1393. if (res < 0)
  1394. return res;
  1395. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  1396. }
  1397. static struct device_attribute dev_attr_hotkey_radio_sw =
  1398. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  1399. static void hotkey_radio_sw_notify_change(void)
  1400. {
  1401. if (tp_features.hotkey_wlsw)
  1402. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1403. "hotkey_radio_sw");
  1404. }
  1405. /* sysfs hotkey tablet mode (pollable) --------------------------------- */
  1406. static ssize_t hotkey_tablet_mode_show(struct device *dev,
  1407. struct device_attribute *attr,
  1408. char *buf)
  1409. {
  1410. int res, s;
  1411. res = hotkey_get_tablet_mode(&s);
  1412. if (res < 0)
  1413. return res;
  1414. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  1415. }
  1416. static struct device_attribute dev_attr_hotkey_tablet_mode =
  1417. __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
  1418. static void hotkey_tablet_mode_notify_change(void)
  1419. {
  1420. if (tp_features.hotkey_tablet)
  1421. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1422. "hotkey_tablet_mode");
  1423. }
  1424. /* sysfs hotkey report_mode -------------------------------------------- */
  1425. static ssize_t hotkey_report_mode_show(struct device *dev,
  1426. struct device_attribute *attr,
  1427. char *buf)
  1428. {
  1429. return snprintf(buf, PAGE_SIZE, "%d\n",
  1430. (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
  1431. }
  1432. static struct device_attribute dev_attr_hotkey_report_mode =
  1433. __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
  1434. /* sysfs wakeup reason (pollable) -------------------------------------- */
  1435. static ssize_t hotkey_wakeup_reason_show(struct device *dev,
  1436. struct device_attribute *attr,
  1437. char *buf)
  1438. {
  1439. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
  1440. }
  1441. static struct device_attribute dev_attr_hotkey_wakeup_reason =
  1442. __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
  1443. static void hotkey_wakeup_reason_notify_change(void)
  1444. {
  1445. if (tp_features.hotkey_mask)
  1446. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1447. "wakeup_reason");
  1448. }
  1449. /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
  1450. static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
  1451. struct device_attribute *attr,
  1452. char *buf)
  1453. {
  1454. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
  1455. }
  1456. static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
  1457. __ATTR(wakeup_hotunplug_complete, S_IRUGO,
  1458. hotkey_wakeup_hotunplug_complete_show, NULL);
  1459. static void hotkey_wakeup_hotunplug_complete_notify_change(void)
  1460. {
  1461. if (tp_features.hotkey_mask)
  1462. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1463. "wakeup_hotunplug_complete");
  1464. }
  1465. /* --------------------------------------------------------------------- */
  1466. static struct attribute *hotkey_attributes[] __initdata = {
  1467. &dev_attr_hotkey_enable.attr,
  1468. &dev_attr_hotkey_bios_enabled.attr,
  1469. &dev_attr_hotkey_report_mode.attr,
  1470. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1471. &dev_attr_hotkey_mask.attr,
  1472. &dev_attr_hotkey_all_mask.attr,
  1473. &dev_attr_hotkey_recommended_mask.attr,
  1474. &dev_attr_hotkey_source_mask.attr,
  1475. &dev_attr_hotkey_poll_freq.attr,
  1476. #endif
  1477. };
  1478. static struct attribute *hotkey_mask_attributes[] __initdata = {
  1479. &dev_attr_hotkey_bios_mask.attr,
  1480. #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1481. &dev_attr_hotkey_mask.attr,
  1482. &dev_attr_hotkey_all_mask.attr,
  1483. &dev_attr_hotkey_recommended_mask.attr,
  1484. #endif
  1485. &dev_attr_hotkey_wakeup_reason.attr,
  1486. &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
  1487. };
  1488. static int __init hotkey_init(struct ibm_init_struct *iibm)
  1489. {
  1490. /* Requirements for changing the default keymaps:
  1491. *
  1492. * 1. Many of the keys are mapped to KEY_RESERVED for very
  1493. * good reasons. Do not change them unless you have deep
  1494. * knowledge on the IBM and Lenovo ThinkPad firmware for
  1495. * the various ThinkPad models. The driver behaves
  1496. * differently for KEY_RESERVED: such keys have their
  1497. * hot key mask *unset* in mask_recommended, and also
  1498. * in the initial hot key mask programmed into the
  1499. * firmware at driver load time, which means the firm-
  1500. * ware may react very differently if you change them to
  1501. * something else;
  1502. *
  1503. * 2. You must be subscribed to the linux-thinkpad and
  1504. * ibm-acpi-devel mailing lists, and you should read the
  1505. * list archives since 2007 if you want to change the
  1506. * keymaps. This requirement exists so that you will
  1507. * know the past history of problems with the thinkpad-
  1508. * acpi driver keymaps, and also that you will be
  1509. * listening to any bug reports;
  1510. *
  1511. * 3. Do not send thinkpad-acpi specific patches directly to
  1512. * for merging, *ever*. Send them to the linux-acpi
  1513. * mailinglist for comments. Merging is to be done only
  1514. * through acpi-test and the ACPI maintainer.
  1515. *
  1516. * If the above is too much to ask, don't change the keymap.
  1517. * Ask the thinkpad-acpi maintainer to do it, instead.
  1518. */
  1519. static u16 ibm_keycode_map[] __initdata = {
  1520. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  1521. KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
  1522. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  1523. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  1524. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  1525. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  1526. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  1527. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  1528. /* brightness: firmware always reacts to them, unless
  1529. * X.org did some tricks in the radeon BIOS scratch
  1530. * registers of *some* models */
  1531. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  1532. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  1533. /* Thinklight: firmware always react to it */
  1534. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  1535. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  1536. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  1537. /* Volume: firmware always react to it and reprograms
  1538. * the built-in *extra* mixer. Never map it to control
  1539. * another mixer by default. */
  1540. KEY_RESERVED, /* 0x14: VOLUME UP */
  1541. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  1542. KEY_RESERVED, /* 0x16: MUTE */
  1543. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  1544. /* (assignments unknown, please report if found) */
  1545. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1546. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1547. };
  1548. static u16 lenovo_keycode_map[] __initdata = {
  1549. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  1550. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  1551. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  1552. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  1553. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  1554. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  1555. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  1556. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  1557. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  1558. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  1559. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  1560. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  1561. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  1562. /* Volume: z60/z61, T60 (BIOS version?): firmware always
  1563. * react to it and reprograms the built-in *extra* mixer.
  1564. * Never map it to control another mixer by default.
  1565. *
  1566. * T60?, T61, R60?, R61: firmware and EC tries to send
  1567. * these over the regular keyboard, so these are no-ops,
  1568. * but there are still weird bugs re. MUTE, so do not
  1569. * change unless you get test reports from all Lenovo
  1570. * models. May cause the BIOS to interfere with the
  1571. * HDA mixer.
  1572. */
  1573. KEY_RESERVED, /* 0x14: VOLUME UP */
  1574. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  1575. KEY_RESERVED, /* 0x16: MUTE */
  1576. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  1577. /* (assignments unknown, please report if found) */
  1578. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1579. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1580. };
  1581. #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
  1582. #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
  1583. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
  1584. int res, i;
  1585. int status;
  1586. int hkeyv;
  1587. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  1588. BUG_ON(!tpacpi_inputdev);
  1589. BUG_ON(tpacpi_inputdev->open != NULL ||
  1590. tpacpi_inputdev->close != NULL);
  1591. TPACPI_ACPIHANDLE_INIT(hkey);
  1592. mutex_init(&hotkey_mutex);
  1593. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1594. mutex_init(&hotkey_thread_mutex);
  1595. mutex_init(&hotkey_thread_data_mutex);
  1596. #endif
  1597. /* hotkey not supported on 570 */
  1598. tp_features.hotkey = hkey_handle != NULL;
  1599. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  1600. str_supported(tp_features.hotkey));
  1601. if (tp_features.hotkey) {
  1602. hotkey_dev_attributes = create_attr_set(13, NULL);
  1603. if (!hotkey_dev_attributes)
  1604. return -ENOMEM;
  1605. res = add_many_to_attr_set(hotkey_dev_attributes,
  1606. hotkey_attributes,
  1607. ARRAY_SIZE(hotkey_attributes));
  1608. if (res)
  1609. return res;
  1610. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1611. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  1612. for HKEY interface version 0x100 */
  1613. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  1614. if ((hkeyv >> 8) != 1) {
  1615. printk(TPACPI_ERR "unknown version of the "
  1616. "HKEY interface: 0x%x\n", hkeyv);
  1617. printk(TPACPI_ERR "please report this to %s\n",
  1618. TPACPI_MAIL);
  1619. } else {
  1620. /*
  1621. * MHKV 0x100 in A31, R40, R40e,
  1622. * T4x, X31, and later
  1623. */
  1624. tp_features.hotkey_mask = 1;
  1625. }
  1626. }
  1627. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  1628. str_supported(tp_features.hotkey_mask));
  1629. if (tp_features.hotkey_mask) {
  1630. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  1631. "MHKA", "qd")) {
  1632. printk(TPACPI_ERR
  1633. "missing MHKA handler, "
  1634. "please report this to %s\n",
  1635. TPACPI_MAIL);
  1636. /* FN+F12, FN+F4, FN+F3 */
  1637. hotkey_all_mask = 0x080cU;
  1638. }
  1639. }
  1640. /* hotkey_source_mask *must* be zero for
  1641. * the first hotkey_mask_get */
  1642. res = hotkey_status_get(&hotkey_orig_status);
  1643. if (!res && tp_features.hotkey_mask) {
  1644. res = hotkey_mask_get();
  1645. hotkey_orig_mask = hotkey_mask;
  1646. if (!res) {
  1647. res = add_many_to_attr_set(
  1648. hotkey_dev_attributes,
  1649. hotkey_mask_attributes,
  1650. ARRAY_SIZE(hotkey_mask_attributes));
  1651. }
  1652. }
  1653. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1654. if (tp_features.hotkey_mask) {
  1655. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  1656. & ~hotkey_all_mask;
  1657. } else {
  1658. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
  1659. }
  1660. vdbg_printk(TPACPI_DBG_INIT,
  1661. "hotkey source mask 0x%08x, polling freq %d\n",
  1662. hotkey_source_mask, hotkey_poll_freq);
  1663. #endif
  1664. /* Not all thinkpads have a hardware radio switch */
  1665. if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  1666. tp_features.hotkey_wlsw = 1;
  1667. printk(TPACPI_INFO
  1668. "radio switch found; radios are %s\n",
  1669. enabled(status, 0));
  1670. res = add_to_attr_set(hotkey_dev_attributes,
  1671. &dev_attr_hotkey_radio_sw.attr);
  1672. }
  1673. /* For X41t, X60t, X61t Tablets... */
  1674. if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
  1675. tp_features.hotkey_tablet = 1;
  1676. printk(TPACPI_INFO
  1677. "possible tablet mode switch found; "
  1678. "ThinkPad in %s mode\n",
  1679. (status & TP_HOTKEY_TABLET_MASK)?
  1680. "tablet" : "laptop");
  1681. res = add_to_attr_set(hotkey_dev_attributes,
  1682. &dev_attr_hotkey_tablet_mode.attr);
  1683. }
  1684. if (!res)
  1685. res = register_attr_set_with_sysfs(
  1686. hotkey_dev_attributes,
  1687. &tpacpi_pdev->dev.kobj);
  1688. if (res)
  1689. return res;
  1690. /* Set up key map */
  1691. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  1692. GFP_KERNEL);
  1693. if (!hotkey_keycode_map) {
  1694. printk(TPACPI_ERR
  1695. "failed to allocate memory for key map\n");
  1696. return -ENOMEM;
  1697. }
  1698. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  1699. dbg_printk(TPACPI_DBG_INIT,
  1700. "using Lenovo default hot key map\n");
  1701. memcpy(hotkey_keycode_map, &lenovo_keycode_map,
  1702. TPACPI_HOTKEY_MAP_SIZE);
  1703. } else {
  1704. dbg_printk(TPACPI_DBG_INIT,
  1705. "using IBM default hot key map\n");
  1706. memcpy(hotkey_keycode_map, &ibm_keycode_map,
  1707. TPACPI_HOTKEY_MAP_SIZE);
  1708. }
  1709. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  1710. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  1711. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  1712. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  1713. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  1714. tpacpi_inputdev->keycode = hotkey_keycode_map;
  1715. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  1716. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  1717. set_bit(hotkey_keycode_map[i],
  1718. tpacpi_inputdev->keybit);
  1719. } else {
  1720. if (i < sizeof(hotkey_reserved_mask)*8)
  1721. hotkey_reserved_mask |= 1 << i;
  1722. }
  1723. }
  1724. if (tp_features.hotkey_wlsw) {
  1725. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1726. set_bit(SW_RADIO, tpacpi_inputdev->swbit);
  1727. }
  1728. if (tp_features.hotkey_tablet) {
  1729. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1730. set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
  1731. }
  1732. dbg_printk(TPACPI_DBG_INIT,
  1733. "enabling hot key handling\n");
  1734. res = hotkey_status_set(1);
  1735. if (res)
  1736. return res;
  1737. res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
  1738. & ~hotkey_reserved_mask)
  1739. | hotkey_orig_mask);
  1740. if (res < 0 && res != -ENXIO)
  1741. return res;
  1742. dbg_printk(TPACPI_DBG_INIT,
  1743. "legacy hot key reporting over procfs %s\n",
  1744. (hotkey_report_mode < 2) ?
  1745. "enabled" : "disabled");
  1746. tpacpi_inputdev->open = &hotkey_inputdev_open;
  1747. tpacpi_inputdev->close = &hotkey_inputdev_close;
  1748. hotkey_poll_setup_safe(1);
  1749. tpacpi_input_send_radiosw();
  1750. tpacpi_input_send_tabletsw();
  1751. }
  1752. return (tp_features.hotkey)? 0 : 1;
  1753. }
  1754. static void hotkey_exit(void)
  1755. {
  1756. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1757. hotkey_poll_stop_sync();
  1758. #endif
  1759. if (tp_features.hotkey) {
  1760. dbg_printk(TPACPI_DBG_EXIT,
  1761. "restoring original hot key mask\n");
  1762. /* no short-circuit boolean operator below! */
  1763. if ((hotkey_mask_set(hotkey_orig_mask) |
  1764. hotkey_status_set(hotkey_orig_status)) != 0)
  1765. printk(TPACPI_ERR
  1766. "failed to restore hot key mask "
  1767. "to BIOS defaults\n");
  1768. }
  1769. if (hotkey_dev_attributes) {
  1770. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  1771. hotkey_dev_attributes = NULL;
  1772. }
  1773. }
  1774. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  1775. {
  1776. u32 hkey;
  1777. unsigned int scancode;
  1778. int send_acpi_ev;
  1779. int ignore_acpi_ev;
  1780. int unk_ev;
  1781. if (event != 0x80) {
  1782. printk(TPACPI_ERR
  1783. "unknown HKEY notification event %d\n", event);
  1784. /* forward it to userspace, maybe it knows how to handle it */
  1785. acpi_bus_generate_netlink_event(
  1786. ibm->acpi->device->pnp.device_class,
  1787. ibm->acpi->device->dev.bus_id,
  1788. event, 0);
  1789. return;
  1790. }
  1791. while (1) {
  1792. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  1793. printk(TPACPI_ERR "failed to retrieve HKEY event\n");
  1794. return;
  1795. }
  1796. if (hkey == 0) {
  1797. /* queue empty */
  1798. return;
  1799. }
  1800. send_acpi_ev = 1;
  1801. ignore_acpi_ev = 0;
  1802. unk_ev = 0;
  1803. switch (hkey >> 12) {
  1804. case 1:
  1805. /* 0x1000-0x1FFF: key presses */
  1806. scancode = hkey & 0xfff;
  1807. if (scancode > 0 && scancode < 0x21) {
  1808. scancode--;
  1809. if (!(hotkey_source_mask & (1 << scancode))) {
  1810. tpacpi_input_send_key(scancode);
  1811. send_acpi_ev = 0;
  1812. } else {
  1813. ignore_acpi_ev = 1;
  1814. }
  1815. } else {
  1816. unk_ev = 1;
  1817. }
  1818. break;
  1819. case 2:
  1820. /* Wakeup reason */
  1821. switch (hkey) {
  1822. case 0x2304: /* suspend, undock */
  1823. case 0x2404: /* hibernation, undock */
  1824. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  1825. ignore_acpi_ev = 1;
  1826. break;
  1827. case 0x2305: /* suspend, bay eject */
  1828. case 0x2405: /* hibernation, bay eject */
  1829. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  1830. ignore_acpi_ev = 1;
  1831. break;
  1832. default:
  1833. unk_ev = 1;
  1834. }
  1835. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  1836. printk(TPACPI_INFO
  1837. "woke up due to a hot-unplug "
  1838. "request...\n");
  1839. hotkey_wakeup_reason_notify_change();
  1840. }
  1841. break;
  1842. case 3:
  1843. /* bay-related wakeups */
  1844. if (hkey == 0x3003) {
  1845. hotkey_autosleep_ack = 1;
  1846. printk(TPACPI_INFO
  1847. "bay ejected\n");
  1848. hotkey_wakeup_hotunplug_complete_notify_change();
  1849. } else {
  1850. unk_ev = 1;
  1851. }
  1852. break;
  1853. case 4:
  1854. /* dock-related wakeups */
  1855. if (hkey == 0x4003) {
  1856. hotkey_autosleep_ack = 1;
  1857. printk(TPACPI_INFO
  1858. "undocked\n");
  1859. hotkey_wakeup_hotunplug_complete_notify_change();
  1860. } else {
  1861. unk_ev = 1;
  1862. }
  1863. break;
  1864. case 5:
  1865. /* 0x5000-0x5FFF: human interface helpers */
  1866. switch (hkey) {
  1867. case 0x5010: /* Lenovo new BIOS: brightness changed */
  1868. case 0x500b: /* X61t: tablet pen inserted into bay */
  1869. case 0x500c: /* X61t: tablet pen removed from bay */
  1870. break;
  1871. case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
  1872. case 0x500a: /* X41t-X61t: swivel down (normal mode) */
  1873. tpacpi_input_send_tabletsw();
  1874. hotkey_tablet_mode_notify_change();
  1875. send_acpi_ev = 0;
  1876. break;
  1877. case 0x5001:
  1878. case 0x5002:
  1879. /* LID switch events. Do not propagate */
  1880. ignore_acpi_ev = 1;
  1881. break;
  1882. default:
  1883. unk_ev = 1;
  1884. }
  1885. break;
  1886. case 7:
  1887. /* 0x7000-0x7FFF: misc */
  1888. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  1889. tpacpi_input_send_radiosw();
  1890. hotkey_radio_sw_notify_change();
  1891. send_acpi_ev = 0;
  1892. break;
  1893. }
  1894. /* fallthrough to default */
  1895. default:
  1896. unk_ev = 1;
  1897. }
  1898. if (unk_ev) {
  1899. printk(TPACPI_NOTICE
  1900. "unhandled HKEY event 0x%04x\n", hkey);
  1901. }
  1902. /* Legacy events */
  1903. if (!ignore_acpi_ev &&
  1904. (send_acpi_ev || hotkey_report_mode < 2)) {
  1905. acpi_bus_generate_proc_event(ibm->acpi->device,
  1906. event, hkey);
  1907. }
  1908. /* netlink events */
  1909. if (!ignore_acpi_ev && send_acpi_ev) {
  1910. acpi_bus_generate_netlink_event(
  1911. ibm->acpi->device->pnp.device_class,
  1912. ibm->acpi->device->dev.bus_id,
  1913. event, hkey);
  1914. }
  1915. }
  1916. }
  1917. static void hotkey_suspend(pm_message_t state)
  1918. {
  1919. /* Do these on suspend, we get the events on early resume! */
  1920. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  1921. hotkey_autosleep_ack = 0;
  1922. }
  1923. static void hotkey_resume(void)
  1924. {
  1925. if (hotkey_mask_get())
  1926. printk(TPACPI_ERR
  1927. "error while trying to read hot key mask "
  1928. "from firmware\n");
  1929. tpacpi_input_send_radiosw();
  1930. hotkey_radio_sw_notify_change();
  1931. hotkey_tablet_mode_notify_change();
  1932. hotkey_wakeup_reason_notify_change();
  1933. hotkey_wakeup_hotunplug_complete_notify_change();
  1934. hotkey_poll_setup_safe(0);
  1935. }
  1936. /* procfs -------------------------------------------------------------- */
  1937. static int hotkey_read(char *p)
  1938. {
  1939. int res, status;
  1940. int len = 0;
  1941. if (!tp_features.hotkey) {
  1942. len += sprintf(p + len, "status:\t\tnot supported\n");
  1943. return len;
  1944. }
  1945. if (mutex_lock_interruptible(&hotkey_mutex))
  1946. return -ERESTARTSYS;
  1947. res = hotkey_status_get(&status);
  1948. if (!res)
  1949. res = hotkey_mask_get();
  1950. mutex_unlock(&hotkey_mutex);
  1951. if (res)
  1952. return res;
  1953. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  1954. if (tp_features.hotkey_mask) {
  1955. len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
  1956. len += sprintf(p + len,
  1957. "commands:\tenable, disable, reset, <mask>\n");
  1958. } else {
  1959. len += sprintf(p + len, "mask:\t\tnot supported\n");
  1960. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  1961. }
  1962. return len;
  1963. }
  1964. static int hotkey_write(char *buf)
  1965. {
  1966. int res, status;
  1967. u32 mask;
  1968. char *cmd;
  1969. if (!tp_features.hotkey)
  1970. return -ENODEV;
  1971. if (mutex_lock_interruptible(&hotkey_mutex))
  1972. return -ERESTARTSYS;
  1973. status = -1;
  1974. mask = hotkey_mask;
  1975. res = 0;
  1976. while ((cmd = next_cmd(&buf))) {
  1977. if (strlencmp(cmd, "enable") == 0) {
  1978. status = 1;
  1979. } else if (strlencmp(cmd, "disable") == 0) {
  1980. status = 0;
  1981. } else if (strlencmp(cmd, "reset") == 0) {
  1982. status = hotkey_orig_status;
  1983. mask = hotkey_orig_mask;
  1984. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  1985. /* mask set */
  1986. } else if (sscanf(cmd, "%x", &mask) == 1) {
  1987. /* mask set */
  1988. } else {
  1989. res = -EINVAL;
  1990. goto errexit;
  1991. }
  1992. }
  1993. if (status != -1)
  1994. res = hotkey_status_set(status);
  1995. if (!res && mask != hotkey_mask)
  1996. res = hotkey_mask_set(mask);
  1997. errexit:
  1998. mutex_unlock(&hotkey_mutex);
  1999. return res;
  2000. }
  2001. static const struct acpi_device_id ibm_htk_device_ids[] = {
  2002. {TPACPI_ACPI_HKEY_HID, 0},
  2003. {"", 0},
  2004. };
  2005. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  2006. .hid = ibm_htk_device_ids,
  2007. .notify = hotkey_notify,
  2008. .handle = &hkey_handle,
  2009. .type = ACPI_DEVICE_NOTIFY,
  2010. };
  2011. static struct ibm_struct hotkey_driver_data = {
  2012. .name = "hotkey",
  2013. .read = hotkey_read,
  2014. .write = hotkey_write,
  2015. .exit = hotkey_exit,
  2016. .resume = hotkey_resume,
  2017. .suspend = hotkey_suspend,
  2018. .acpi = &ibm_hotkey_acpidriver,
  2019. };
  2020. /*************************************************************************
  2021. * Bluetooth subdriver
  2022. */
  2023. enum {
  2024. /* ACPI GBDC/SBDC bits */
  2025. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  2026. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  2027. TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
  2028. };
  2029. static int bluetooth_get_radiosw(void);
  2030. static int bluetooth_set_radiosw(int radio_on);
  2031. /* sysfs bluetooth enable ---------------------------------------------- */
  2032. static ssize_t bluetooth_enable_show(struct device *dev,
  2033. struct device_attribute *attr,
  2034. char *buf)
  2035. {
  2036. int status;
  2037. status = bluetooth_get_radiosw();
  2038. if (status < 0)
  2039. return status;
  2040. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  2041. }
  2042. static ssize_t bluetooth_enable_store(struct device *dev,
  2043. struct device_attribute *attr,
  2044. const char *buf, size_t count)
  2045. {
  2046. unsigned long t;
  2047. int res;
  2048. if (parse_strtoul(buf, 1, &t))
  2049. return -EINVAL;
  2050. res = bluetooth_set_radiosw(t);
  2051. return (res) ? res : count;
  2052. }
  2053. static struct device_attribute dev_attr_bluetooth_enable =
  2054. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  2055. bluetooth_enable_show, bluetooth_enable_store);
  2056. /* --------------------------------------------------------------------- */
  2057. static struct attribute *bluetooth_attributes[] = {
  2058. &dev_attr_bluetooth_enable.attr,
  2059. NULL
  2060. };
  2061. static const struct attribute_group bluetooth_attr_group = {
  2062. .attrs = bluetooth_attributes,
  2063. };
  2064. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  2065. {
  2066. int res;
  2067. int status = 0;
  2068. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  2069. TPACPI_ACPIHANDLE_INIT(hkey);
  2070. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2071. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  2072. tp_features.bluetooth = hkey_handle &&
  2073. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  2074. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  2075. str_supported(tp_features.bluetooth),
  2076. status);
  2077. if (tp_features.bluetooth) {
  2078. if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  2079. /* no bluetooth hardware present in system */
  2080. tp_features.bluetooth = 0;
  2081. dbg_printk(TPACPI_DBG_INIT,
  2082. "bluetooth hardware not installed\n");
  2083. } else {
  2084. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2085. &bluetooth_attr_group);
  2086. if (res)
  2087. return res;
  2088. }
  2089. }
  2090. return (tp_features.bluetooth)? 0 : 1;
  2091. }
  2092. static void bluetooth_exit(void)
  2093. {
  2094. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2095. &bluetooth_attr_group);
  2096. }
  2097. static int bluetooth_get_radiosw(void)
  2098. {
  2099. int status;
  2100. if (!tp_features.bluetooth)
  2101. return -ENODEV;
  2102. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  2103. return -EIO;
  2104. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  2105. }
  2106. static int bluetooth_set_radiosw(int radio_on)
  2107. {
  2108. int status;
  2109. if (!tp_features.bluetooth)
  2110. return -ENODEV;
  2111. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  2112. return -EIO;
  2113. if (radio_on)
  2114. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  2115. else
  2116. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  2117. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  2118. return -EIO;
  2119. return 0;
  2120. }
  2121. /* procfs -------------------------------------------------------------- */
  2122. static int bluetooth_read(char *p)
  2123. {
  2124. int len = 0;
  2125. int status = bluetooth_get_radiosw();
  2126. if (!tp_features.bluetooth)
  2127. len += sprintf(p + len, "status:\t\tnot supported\n");
  2128. else {
  2129. len += sprintf(p + len, "status:\t\t%s\n",
  2130. (status)? "enabled" : "disabled");
  2131. len += sprintf(p + len, "commands:\tenable, disable\n");
  2132. }
  2133. return len;
  2134. }
  2135. static int bluetooth_write(char *buf)
  2136. {
  2137. char *cmd;
  2138. if (!tp_features.bluetooth)
  2139. return -ENODEV;
  2140. while ((cmd = next_cmd(&buf))) {
  2141. if (strlencmp(cmd, "enable") == 0) {
  2142. bluetooth_set_radiosw(1);
  2143. } else if (strlencmp(cmd, "disable") == 0) {
  2144. bluetooth_set_radiosw(0);
  2145. } else
  2146. return -EINVAL;
  2147. }
  2148. return 0;
  2149. }
  2150. static struct ibm_struct bluetooth_driver_data = {
  2151. .name = "bluetooth",
  2152. .read = bluetooth_read,
  2153. .write = bluetooth_write,
  2154. .exit = bluetooth_exit,
  2155. };
  2156. /*************************************************************************
  2157. * Wan subdriver
  2158. */
  2159. enum {
  2160. /* ACPI GWAN/SWAN bits */
  2161. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  2162. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  2163. TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
  2164. };
  2165. static int wan_get_radiosw(void);
  2166. static int wan_set_radiosw(int radio_on);
  2167. /* sysfs wan enable ---------------------------------------------------- */
  2168. static ssize_t wan_enable_show(struct device *dev,
  2169. struct device_attribute *attr,
  2170. char *buf)
  2171. {
  2172. int status;
  2173. status = wan_get_radiosw();
  2174. if (status < 0)
  2175. return status;
  2176. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  2177. }
  2178. static ssize_t wan_enable_store(struct device *dev,
  2179. struct device_attribute *attr,
  2180. const char *buf, size_t count)
  2181. {
  2182. unsigned long t;
  2183. int res;
  2184. if (parse_strtoul(buf, 1, &t))
  2185. return -EINVAL;
  2186. res = wan_set_radiosw(t);
  2187. return (res) ? res : count;
  2188. }
  2189. static struct device_attribute dev_attr_wan_enable =
  2190. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  2191. wan_enable_show, wan_enable_store);
  2192. /* --------------------------------------------------------------------- */
  2193. static struct attribute *wan_attributes[] = {
  2194. &dev_attr_wan_enable.attr,
  2195. NULL
  2196. };
  2197. static const struct attribute_group wan_attr_group = {
  2198. .attrs = wan_attributes,
  2199. };
  2200. static int __init wan_init(struct ibm_init_struct *iibm)
  2201. {
  2202. int res;
  2203. int status = 0;
  2204. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  2205. TPACPI_ACPIHANDLE_INIT(hkey);
  2206. tp_features.wan = hkey_handle &&
  2207. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  2208. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  2209. str_supported(tp_features.wan),
  2210. status);
  2211. if (tp_features.wan) {
  2212. if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
  2213. /* no wan hardware present in system */
  2214. tp_features.wan = 0;
  2215. dbg_printk(TPACPI_DBG_INIT,
  2216. "wan hardware not installed\n");
  2217. } else {
  2218. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2219. &wan_attr_group);
  2220. if (res)
  2221. return res;
  2222. }
  2223. }
  2224. return (tp_features.wan)? 0 : 1;
  2225. }
  2226. static void wan_exit(void)
  2227. {
  2228. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2229. &wan_attr_group);
  2230. }
  2231. static int wan_get_radiosw(void)
  2232. {
  2233. int status;
  2234. if (!tp_features.wan)
  2235. return -ENODEV;
  2236. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2237. return -EIO;
  2238. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  2239. }
  2240. static int wan_set_radiosw(int radio_on)
  2241. {
  2242. int status;
  2243. if (!tp_features.wan)
  2244. return -ENODEV;
  2245. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2246. return -EIO;
  2247. if (radio_on)
  2248. status |= TP_ACPI_WANCARD_RADIOSSW;
  2249. else
  2250. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  2251. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  2252. return -EIO;
  2253. return 0;
  2254. }
  2255. /* procfs -------------------------------------------------------------- */
  2256. static int wan_read(char *p)
  2257. {
  2258. int len = 0;
  2259. int status = wan_get_radiosw();
  2260. if (!tp_features.wan)
  2261. len += sprintf(p + len, "status:\t\tnot supported\n");
  2262. else {
  2263. len += sprintf(p + len, "status:\t\t%s\n",
  2264. (status)? "enabled" : "disabled");
  2265. len += sprintf(p + len, "commands:\tenable, disable\n");
  2266. }
  2267. return len;
  2268. }
  2269. static int wan_write(char *buf)
  2270. {
  2271. char *cmd;
  2272. if (!tp_features.wan)
  2273. return -ENODEV;
  2274. while ((cmd = next_cmd(&buf))) {
  2275. if (strlencmp(cmd, "enable") == 0) {
  2276. wan_set_radiosw(1);
  2277. } else if (strlencmp(cmd, "disable") == 0) {
  2278. wan_set_radiosw(0);
  2279. } else
  2280. return -EINVAL;
  2281. }
  2282. return 0;
  2283. }
  2284. static struct ibm_struct wan_driver_data = {
  2285. .name = "wan",
  2286. .read = wan_read,
  2287. .write = wan_write,
  2288. .exit = wan_exit,
  2289. .flags.experimental = 1,
  2290. };
  2291. /*************************************************************************
  2292. * Video subdriver
  2293. */
  2294. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  2295. enum video_access_mode {
  2296. TPACPI_VIDEO_NONE = 0,
  2297. TPACPI_VIDEO_570, /* 570 */
  2298. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  2299. TPACPI_VIDEO_NEW, /* all others */
  2300. };
  2301. enum { /* video status flags, based on VIDEO_570 */
  2302. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  2303. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  2304. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  2305. };
  2306. enum { /* TPACPI_VIDEO_570 constants */
  2307. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  2308. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  2309. * video_status_flags */
  2310. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  2311. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  2312. };
  2313. static enum video_access_mode video_supported;
  2314. static int video_orig_autosw;
  2315. static int video_autosw_get(void);
  2316. static int video_autosw_set(int enable);
  2317. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  2318. static int __init video_init(struct ibm_init_struct *iibm)
  2319. {
  2320. int ivga;
  2321. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  2322. TPACPI_ACPIHANDLE_INIT(vid);
  2323. TPACPI_ACPIHANDLE_INIT(vid2);
  2324. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  2325. /* G41, assume IVGA doesn't change */
  2326. vid_handle = vid2_handle;
  2327. if (!vid_handle)
  2328. /* video switching not supported on R30, R31 */
  2329. video_supported = TPACPI_VIDEO_NONE;
  2330. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  2331. /* 570 */
  2332. video_supported = TPACPI_VIDEO_570;
  2333. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  2334. /* 600e/x, 770e, 770x */
  2335. video_supported = TPACPI_VIDEO_770;
  2336. else
  2337. /* all others */
  2338. video_supported = TPACPI_VIDEO_NEW;
  2339. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  2340. str_supported(video_supported != TPACPI_VIDEO_NONE),
  2341. video_supported);
  2342. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  2343. }
  2344. static void video_exit(void)
  2345. {
  2346. dbg_printk(TPACPI_DBG_EXIT,
  2347. "restoring original video autoswitch mode\n");
  2348. if (video_autosw_set(video_orig_autosw))
  2349. printk(TPACPI_ERR "error while trying to restore original "
  2350. "video autoswitch mode\n");
  2351. }
  2352. static int video_outputsw_get(void)
  2353. {
  2354. int status = 0;
  2355. int i;
  2356. switch (video_supported) {
  2357. case TPACPI_VIDEO_570:
  2358. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  2359. TP_ACPI_VIDEO_570_PHSCMD))
  2360. return -EIO;
  2361. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  2362. break;
  2363. case TPACPI_VIDEO_770:
  2364. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2365. return -EIO;
  2366. if (i)
  2367. status |= TP_ACPI_VIDEO_S_LCD;
  2368. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2369. return -EIO;
  2370. if (i)
  2371. status |= TP_ACPI_VIDEO_S_CRT;
  2372. break;
  2373. case TPACPI_VIDEO_NEW:
  2374. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  2375. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2376. return -EIO;
  2377. if (i)
  2378. status |= TP_ACPI_VIDEO_S_CRT;
  2379. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  2380. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2381. return -EIO;
  2382. if (i)
  2383. status |= TP_ACPI_VIDEO_S_LCD;
  2384. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  2385. return -EIO;
  2386. if (i)
  2387. status |= TP_ACPI_VIDEO_S_DVI;
  2388. break;
  2389. default:
  2390. return -ENOSYS;
  2391. }
  2392. return status;
  2393. }
  2394. static int video_outputsw_set(int status)
  2395. {
  2396. int autosw;
  2397. int res = 0;
  2398. switch (video_supported) {
  2399. case TPACPI_VIDEO_570:
  2400. res = acpi_evalf(NULL, NULL,
  2401. "\\_SB.PHS2", "vdd",
  2402. TP_ACPI_VIDEO_570_PHS2CMD,
  2403. status | TP_ACPI_VIDEO_570_PHS2SET);
  2404. break;
  2405. case TPACPI_VIDEO_770:
  2406. autosw = video_autosw_get();
  2407. if (autosw < 0)
  2408. return autosw;
  2409. res = video_autosw_set(1);
  2410. if (res)
  2411. return res;
  2412. res = acpi_evalf(vid_handle, NULL,
  2413. "ASWT", "vdd", status * 0x100, 0);
  2414. if (!autosw && video_autosw_set(autosw)) {
  2415. printk(TPACPI_ERR
  2416. "video auto-switch left enabled due to error\n");
  2417. return -EIO;
  2418. }
  2419. break;
  2420. case TPACPI_VIDEO_NEW:
  2421. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  2422. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  2423. break;
  2424. default:
  2425. return -ENOSYS;
  2426. }
  2427. return (res)? 0 : -EIO;
  2428. }
  2429. static int video_autosw_get(void)
  2430. {
  2431. int autosw = 0;
  2432. switch (video_supported) {
  2433. case TPACPI_VIDEO_570:
  2434. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  2435. return -EIO;
  2436. break;
  2437. case TPACPI_VIDEO_770:
  2438. case TPACPI_VIDEO_NEW:
  2439. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  2440. return -EIO;
  2441. break;
  2442. default:
  2443. return -ENOSYS;
  2444. }
  2445. return autosw & 1;
  2446. }
  2447. static int video_autosw_set(int enable)
  2448. {
  2449. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  2450. return -EIO;
  2451. return 0;
  2452. }
  2453. static int video_outputsw_cycle(void)
  2454. {
  2455. int autosw = video_autosw_get();
  2456. int res;
  2457. if (autosw < 0)
  2458. return autosw;
  2459. switch (video_supported) {
  2460. case TPACPI_VIDEO_570:
  2461. res = video_autosw_set(1);
  2462. if (res)
  2463. return res;
  2464. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  2465. break;
  2466. case TPACPI_VIDEO_770:
  2467. case TPACPI_VIDEO_NEW:
  2468. res = video_autosw_set(1);
  2469. if (res)
  2470. return res;
  2471. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  2472. break;
  2473. default:
  2474. return -ENOSYS;
  2475. }
  2476. if (!autosw && video_autosw_set(autosw)) {
  2477. printk(TPACPI_ERR
  2478. "video auto-switch left enabled due to error\n");
  2479. return -EIO;
  2480. }
  2481. return (res)? 0 : -EIO;
  2482. }
  2483. static int video_expand_toggle(void)
  2484. {
  2485. switch (video_supported) {
  2486. case TPACPI_VIDEO_570:
  2487. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  2488. 0 : -EIO;
  2489. case TPACPI_VIDEO_770:
  2490. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  2491. 0 : -EIO;
  2492. case TPACPI_VIDEO_NEW:
  2493. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  2494. 0 : -EIO;
  2495. default:
  2496. return -ENOSYS;
  2497. }
  2498. /* not reached */
  2499. }
  2500. static int video_read(char *p)
  2501. {
  2502. int status, autosw;
  2503. int len = 0;
  2504. if (video_supported == TPACPI_VIDEO_NONE) {
  2505. len += sprintf(p + len, "status:\t\tnot supported\n");
  2506. return len;
  2507. }
  2508. status = video_outputsw_get();
  2509. if (status < 0)
  2510. return status;
  2511. autosw = video_autosw_get();
  2512. if (autosw < 0)
  2513. return autosw;
  2514. len += sprintf(p + len, "status:\t\tsupported\n");
  2515. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  2516. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  2517. if (video_supported == TPACPI_VIDEO_NEW)
  2518. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  2519. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  2520. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  2521. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  2522. if (video_supported == TPACPI_VIDEO_NEW)
  2523. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  2524. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  2525. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  2526. return len;
  2527. }
  2528. static int video_write(char *buf)
  2529. {
  2530. char *cmd;
  2531. int enable, disable, status;
  2532. int res;
  2533. if (video_supported == TPACPI_VIDEO_NONE)
  2534. return -ENODEV;
  2535. enable = 0;
  2536. disable = 0;
  2537. while ((cmd = next_cmd(&buf))) {
  2538. if (strlencmp(cmd, "lcd_enable") == 0) {
  2539. enable |= TP_ACPI_VIDEO_S_LCD;
  2540. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  2541. disable |= TP_ACPI_VIDEO_S_LCD;
  2542. } else if (strlencmp(cmd, "crt_enable") == 0) {
  2543. enable |= TP_ACPI_VIDEO_S_CRT;
  2544. } else if (strlencmp(cmd, "crt_disable") == 0) {
  2545. disable |= TP_ACPI_VIDEO_S_CRT;
  2546. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2547. strlencmp(cmd, "dvi_enable") == 0) {
  2548. enable |= TP_ACPI_VIDEO_S_DVI;
  2549. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2550. strlencmp(cmd, "dvi_disable") == 0) {
  2551. disable |= TP_ACPI_VIDEO_S_DVI;
  2552. } else if (strlencmp(cmd, "auto_enable") == 0) {
  2553. res = video_autosw_set(1);
  2554. if (res)
  2555. return res;
  2556. } else if (strlencmp(cmd, "auto_disable") == 0) {
  2557. res = video_autosw_set(0);
  2558. if (res)
  2559. return res;
  2560. } else if (strlencmp(cmd, "video_switch") == 0) {
  2561. res = video_outputsw_cycle();
  2562. if (res)
  2563. return res;
  2564. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  2565. res = video_expand_toggle();
  2566. if (res)
  2567. return res;
  2568. } else
  2569. return -EINVAL;
  2570. }
  2571. if (enable || disable) {
  2572. status = video_outputsw_get();
  2573. if (status < 0)
  2574. return status;
  2575. res = video_outputsw_set((status & ~disable) | enable);
  2576. if (res)
  2577. return res;
  2578. }
  2579. return 0;
  2580. }
  2581. static struct ibm_struct video_driver_data = {
  2582. .name = "video",
  2583. .read = video_read,
  2584. .write = video_write,
  2585. .exit = video_exit,
  2586. };
  2587. #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
  2588. /*************************************************************************
  2589. * Light (thinklight) subdriver
  2590. */
  2591. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  2592. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  2593. static int __init light_init(struct ibm_init_struct *iibm)
  2594. {
  2595. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  2596. TPACPI_ACPIHANDLE_INIT(ledb);
  2597. TPACPI_ACPIHANDLE_INIT(lght);
  2598. TPACPI_ACPIHANDLE_INIT(cmos);
  2599. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  2600. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  2601. if (tp_features.light)
  2602. /* light status not supported on
  2603. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  2604. tp_features.light_status =
  2605. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  2606. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  2607. str_supported(tp_features.light));
  2608. return (tp_features.light)? 0 : 1;
  2609. }
  2610. static int light_read(char *p)
  2611. {
  2612. int len = 0;
  2613. int status = 0;
  2614. if (!tp_features.light) {
  2615. len += sprintf(p + len, "status:\t\tnot supported\n");
  2616. } else if (!tp_features.light_status) {
  2617. len += sprintf(p + len, "status:\t\tunknown\n");
  2618. len += sprintf(p + len, "commands:\ton, off\n");
  2619. } else {
  2620. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  2621. return -EIO;
  2622. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  2623. len += sprintf(p + len, "commands:\ton, off\n");
  2624. }
  2625. return len;
  2626. }
  2627. static int light_write(char *buf)
  2628. {
  2629. int cmos_cmd, lght_cmd;
  2630. char *cmd;
  2631. int success;
  2632. if (!tp_features.light)
  2633. return -ENODEV;
  2634. while ((cmd = next_cmd(&buf))) {
  2635. if (strlencmp(cmd, "on") == 0) {
  2636. cmos_cmd = 0x0c;
  2637. lght_cmd = 1;
  2638. } else if (strlencmp(cmd, "off") == 0) {
  2639. cmos_cmd = 0x0d;
  2640. lght_cmd = 0;
  2641. } else
  2642. return -EINVAL;
  2643. success = cmos_handle ?
  2644. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  2645. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  2646. if (!success)
  2647. return -EIO;
  2648. }
  2649. return 0;
  2650. }
  2651. static struct ibm_struct light_driver_data = {
  2652. .name = "light",
  2653. .read = light_read,
  2654. .write = light_write,
  2655. };
  2656. /*************************************************************************
  2657. * Dock subdriver
  2658. */
  2659. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2660. static void dock_notify(struct ibm_struct *ibm, u32 event);
  2661. static int dock_read(char *p);
  2662. static int dock_write(char *buf);
  2663. TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  2664. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  2665. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  2666. "\\_SB.PCI.ISA.SLCE", /* 570 */
  2667. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  2668. /* don't list other alternatives as we install a notify handler on the 570 */
  2669. TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  2670. static const struct acpi_device_id ibm_pci_device_ids[] = {
  2671. {PCI_ROOT_HID_STRING, 0},
  2672. {"", 0},
  2673. };
  2674. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  2675. {
  2676. .notify = dock_notify,
  2677. .handle = &dock_handle,
  2678. .type = ACPI_SYSTEM_NOTIFY,
  2679. },
  2680. {
  2681. /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
  2682. * We just use it to get notifications of dock hotplug
  2683. * in very old thinkpads */
  2684. .hid = ibm_pci_device_ids,
  2685. .notify = dock_notify,
  2686. .handle = &pci_handle,
  2687. .type = ACPI_SYSTEM_NOTIFY,
  2688. },
  2689. };
  2690. static struct ibm_struct dock_driver_data[2] = {
  2691. {
  2692. .name = "dock",
  2693. .read = dock_read,
  2694. .write = dock_write,
  2695. .acpi = &ibm_dock_acpidriver[0],
  2696. },
  2697. {
  2698. .name = "dock",
  2699. .acpi = &ibm_dock_acpidriver[1],
  2700. },
  2701. };
  2702. #define dock_docked() (_sta(dock_handle) & 1)
  2703. static int __init dock_init(struct ibm_init_struct *iibm)
  2704. {
  2705. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  2706. TPACPI_ACPIHANDLE_INIT(dock);
  2707. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  2708. str_supported(dock_handle != NULL));
  2709. return (dock_handle)? 0 : 1;
  2710. }
  2711. static int __init dock_init2(struct ibm_init_struct *iibm)
  2712. {
  2713. int dock2_needed;
  2714. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  2715. if (dock_driver_data[0].flags.acpi_driver_registered &&
  2716. dock_driver_data[0].flags.acpi_notify_installed) {
  2717. TPACPI_ACPIHANDLE_INIT(pci);
  2718. dock2_needed = (pci_handle != NULL);
  2719. vdbg_printk(TPACPI_DBG_INIT,
  2720. "dock PCI handler for the TP 570 is %s\n",
  2721. str_supported(dock2_needed));
  2722. } else {
  2723. vdbg_printk(TPACPI_DBG_INIT,
  2724. "dock subdriver part 2 not required\n");
  2725. dock2_needed = 0;
  2726. }
  2727. return (dock2_needed)? 0 : 1;
  2728. }
  2729. static void dock_notify(struct ibm_struct *ibm, u32 event)
  2730. {
  2731. int docked = dock_docked();
  2732. int pci = ibm->acpi->hid && ibm->acpi->device &&
  2733. acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
  2734. int data;
  2735. if (event == 1 && !pci) /* 570 */
  2736. data = 1; /* button */
  2737. else if (event == 1 && pci) /* 570 */
  2738. data = 3; /* dock */
  2739. else if (event == 3 && docked)
  2740. data = 1; /* button */
  2741. else if (event == 3 && !docked)
  2742. data = 2; /* undock */
  2743. else if (event == 0 && docked)
  2744. data = 3; /* dock */
  2745. else {
  2746. printk(TPACPI_ERR "unknown dock event %d, status %d\n",
  2747. event, _sta(dock_handle));
  2748. data = 0; /* unknown */
  2749. }
  2750. acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
  2751. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2752. ibm->acpi->device->dev.bus_id,
  2753. event, data);
  2754. }
  2755. static int dock_read(char *p)
  2756. {
  2757. int len = 0;
  2758. int docked = dock_docked();
  2759. if (!dock_handle)
  2760. len += sprintf(p + len, "status:\t\tnot supported\n");
  2761. else if (!docked)
  2762. len += sprintf(p + len, "status:\t\tundocked\n");
  2763. else {
  2764. len += sprintf(p + len, "status:\t\tdocked\n");
  2765. len += sprintf(p + len, "commands:\tdock, undock\n");
  2766. }
  2767. return len;
  2768. }
  2769. static int dock_write(char *buf)
  2770. {
  2771. char *cmd;
  2772. if (!dock_docked())
  2773. return -ENODEV;
  2774. while ((cmd = next_cmd(&buf))) {
  2775. if (strlencmp(cmd, "undock") == 0) {
  2776. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  2777. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  2778. return -EIO;
  2779. } else if (strlencmp(cmd, "dock") == 0) {
  2780. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  2781. return -EIO;
  2782. } else
  2783. return -EINVAL;
  2784. }
  2785. return 0;
  2786. }
  2787. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  2788. /*************************************************************************
  2789. * Bay subdriver
  2790. */
  2791. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2792. TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  2793. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  2794. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  2795. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  2796. ); /* A21e, R30, R31 */
  2797. TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  2798. "_EJ0", /* all others */
  2799. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  2800. TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  2801. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  2802. ); /* all others */
  2803. TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  2804. "_EJ0", /* 770x */
  2805. ); /* all others */
  2806. static int __init bay_init(struct ibm_init_struct *iibm)
  2807. {
  2808. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  2809. TPACPI_ACPIHANDLE_INIT(bay);
  2810. if (bay_handle)
  2811. TPACPI_ACPIHANDLE_INIT(bay_ej);
  2812. TPACPI_ACPIHANDLE_INIT(bay2);
  2813. if (bay2_handle)
  2814. TPACPI_ACPIHANDLE_INIT(bay2_ej);
  2815. tp_features.bay_status = bay_handle &&
  2816. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  2817. tp_features.bay_status2 = bay2_handle &&
  2818. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  2819. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  2820. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  2821. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  2822. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  2823. vdbg_printk(TPACPI_DBG_INIT,
  2824. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  2825. str_supported(tp_features.bay_status),
  2826. str_supported(tp_features.bay_eject),
  2827. str_supported(tp_features.bay_status2),
  2828. str_supported(tp_features.bay_eject2));
  2829. return (tp_features.bay_status || tp_features.bay_eject ||
  2830. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  2831. }
  2832. static void bay_notify(struct ibm_struct *ibm, u32 event)
  2833. {
  2834. acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
  2835. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2836. ibm->acpi->device->dev.bus_id,
  2837. event, 0);
  2838. }
  2839. #define bay_occupied(b) (_sta(b##_handle) & 1)
  2840. static int bay_read(char *p)
  2841. {
  2842. int len = 0;
  2843. int occupied = bay_occupied(bay);
  2844. int occupied2 = bay_occupied(bay2);
  2845. int eject, eject2;
  2846. len += sprintf(p + len, "status:\t\t%s\n",
  2847. tp_features.bay_status ?
  2848. (occupied ? "occupied" : "unoccupied") :
  2849. "not supported");
  2850. if (tp_features.bay_status2)
  2851. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  2852. "occupied" : "unoccupied");
  2853. eject = tp_features.bay_eject && occupied;
  2854. eject2 = tp_features.bay_eject2 && occupied2;
  2855. if (eject && eject2)
  2856. len += sprintf(p + len, "commands:\teject, eject2\n");
  2857. else if (eject)
  2858. len += sprintf(p + len, "commands:\teject\n");
  2859. else if (eject2)
  2860. len += sprintf(p + len, "commands:\teject2\n");
  2861. return len;
  2862. }
  2863. static int bay_write(char *buf)
  2864. {
  2865. char *cmd;
  2866. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  2867. return -ENODEV;
  2868. while ((cmd = next_cmd(&buf))) {
  2869. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  2870. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  2871. return -EIO;
  2872. } else if (tp_features.bay_eject2 &&
  2873. strlencmp(cmd, "eject2") == 0) {
  2874. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  2875. return -EIO;
  2876. } else
  2877. return -EINVAL;
  2878. }
  2879. return 0;
  2880. }
  2881. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  2882. .notify = bay_notify,
  2883. .handle = &bay_handle,
  2884. .type = ACPI_SYSTEM_NOTIFY,
  2885. };
  2886. static struct ibm_struct bay_driver_data = {
  2887. .name = "bay",
  2888. .read = bay_read,
  2889. .write = bay_write,
  2890. .acpi = &ibm_bay_acpidriver,
  2891. };
  2892. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  2893. /*************************************************************************
  2894. * CMOS subdriver
  2895. */
  2896. /* sysfs cmos_command -------------------------------------------------- */
  2897. static ssize_t cmos_command_store(struct device *dev,
  2898. struct device_attribute *attr,
  2899. const char *buf, size_t count)
  2900. {
  2901. unsigned long cmos_cmd;
  2902. int res;
  2903. if (parse_strtoul(buf, 21, &cmos_cmd))
  2904. return -EINVAL;
  2905. res = issue_thinkpad_cmos_command(cmos_cmd);
  2906. return (res)? res : count;
  2907. }
  2908. static struct device_attribute dev_attr_cmos_command =
  2909. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  2910. /* --------------------------------------------------------------------- */
  2911. static int __init cmos_init(struct ibm_init_struct *iibm)
  2912. {
  2913. int res;
  2914. vdbg_printk(TPACPI_DBG_INIT,
  2915. "initializing cmos commands subdriver\n");
  2916. TPACPI_ACPIHANDLE_INIT(cmos);
  2917. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  2918. str_supported(cmos_handle != NULL));
  2919. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2920. if (res)
  2921. return res;
  2922. return (cmos_handle)? 0 : 1;
  2923. }
  2924. static void cmos_exit(void)
  2925. {
  2926. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2927. }
  2928. static int cmos_read(char *p)
  2929. {
  2930. int len = 0;
  2931. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2932. R30, R31, T20-22, X20-21 */
  2933. if (!cmos_handle)
  2934. len += sprintf(p + len, "status:\t\tnot supported\n");
  2935. else {
  2936. len += sprintf(p + len, "status:\t\tsupported\n");
  2937. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  2938. }
  2939. return len;
  2940. }
  2941. static int cmos_write(char *buf)
  2942. {
  2943. char *cmd;
  2944. int cmos_cmd, res;
  2945. while ((cmd = next_cmd(&buf))) {
  2946. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  2947. cmos_cmd >= 0 && cmos_cmd <= 21) {
  2948. /* cmos_cmd set */
  2949. } else
  2950. return -EINVAL;
  2951. res = issue_thinkpad_cmos_command(cmos_cmd);
  2952. if (res)
  2953. return res;
  2954. }
  2955. return 0;
  2956. }
  2957. static struct ibm_struct cmos_driver_data = {
  2958. .name = "cmos",
  2959. .read = cmos_read,
  2960. .write = cmos_write,
  2961. .exit = cmos_exit,
  2962. };
  2963. /*************************************************************************
  2964. * LED subdriver
  2965. */
  2966. enum led_access_mode {
  2967. TPACPI_LED_NONE = 0,
  2968. TPACPI_LED_570, /* 570 */
  2969. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2970. TPACPI_LED_NEW, /* all others */
  2971. };
  2972. enum { /* For TPACPI_LED_OLD */
  2973. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  2974. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  2975. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  2976. };
  2977. static enum led_access_mode led_supported;
  2978. TPACPI_HANDLE(led, ec, "SLED", /* 570 */
  2979. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
  2980. /* T20-22, X20-21 */
  2981. "LED", /* all others */
  2982. ); /* R30, R31 */
  2983. static int __init led_init(struct ibm_init_struct *iibm)
  2984. {
  2985. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  2986. TPACPI_ACPIHANDLE_INIT(led);
  2987. if (!led_handle)
  2988. /* led not supported on R30, R31 */
  2989. led_supported = TPACPI_LED_NONE;
  2990. else if (strlencmp(led_path, "SLED") == 0)
  2991. /* 570 */
  2992. led_supported = TPACPI_LED_570;
  2993. else if (strlencmp(led_path, "SYSL") == 0)
  2994. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2995. led_supported = TPACPI_LED_OLD;
  2996. else
  2997. /* all others */
  2998. led_supported = TPACPI_LED_NEW;
  2999. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  3000. str_supported(led_supported), led_supported);
  3001. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  3002. }
  3003. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  3004. static int led_read(char *p)
  3005. {
  3006. int len = 0;
  3007. if (!led_supported) {
  3008. len += sprintf(p + len, "status:\t\tnot supported\n");
  3009. return len;
  3010. }
  3011. len += sprintf(p + len, "status:\t\tsupported\n");
  3012. if (led_supported == TPACPI_LED_570) {
  3013. /* 570 */
  3014. int i, status;
  3015. for (i = 0; i < 8; i++) {
  3016. if (!acpi_evalf(ec_handle,
  3017. &status, "GLED", "dd", 1 << i))
  3018. return -EIO;
  3019. len += sprintf(p + len, "%d:\t\t%s\n",
  3020. i, led_status(status));
  3021. }
  3022. }
  3023. len += sprintf(p + len, "commands:\t"
  3024. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  3025. return len;
  3026. }
  3027. /* off, on, blink */
  3028. static const int led_sled_arg1[] = { 0, 1, 3 };
  3029. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  3030. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  3031. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  3032. static int led_write(char *buf)
  3033. {
  3034. char *cmd;
  3035. int led, ind, ret;
  3036. if (!led_supported)
  3037. return -ENODEV;
  3038. while ((cmd = next_cmd(&buf))) {
  3039. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  3040. return -EINVAL;
  3041. if (strstr(cmd, "off")) {
  3042. ind = 0;
  3043. } else if (strstr(cmd, "on")) {
  3044. ind = 1;
  3045. } else if (strstr(cmd, "blink")) {
  3046. ind = 2;
  3047. } else
  3048. return -EINVAL;
  3049. if (led_supported == TPACPI_LED_570) {
  3050. /* 570 */
  3051. led = 1 << led;
  3052. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  3053. led, led_sled_arg1[ind]))
  3054. return -EIO;
  3055. } else if (led_supported == TPACPI_LED_OLD) {
  3056. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  3057. led = 1 << led;
  3058. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  3059. if (ret >= 0)
  3060. ret = ec_write(TPACPI_LED_EC_HLBL,
  3061. led * led_exp_hlbl[ind]);
  3062. if (ret >= 0)
  3063. ret = ec_write(TPACPI_LED_EC_HLCL,
  3064. led * led_exp_hlcl[ind]);
  3065. if (ret < 0)
  3066. return ret;
  3067. } else {
  3068. /* all others */
  3069. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  3070. led, led_led_arg1[ind]))
  3071. return -EIO;
  3072. }
  3073. }
  3074. return 0;
  3075. }
  3076. static struct ibm_struct led_driver_data = {
  3077. .name = "led",
  3078. .read = led_read,
  3079. .write = led_write,
  3080. };
  3081. /*************************************************************************
  3082. * Beep subdriver
  3083. */
  3084. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  3085. static int __init beep_init(struct ibm_init_struct *iibm)
  3086. {
  3087. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  3088. TPACPI_ACPIHANDLE_INIT(beep);
  3089. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  3090. str_supported(beep_handle != NULL));
  3091. return (beep_handle)? 0 : 1;
  3092. }
  3093. static int beep_read(char *p)
  3094. {
  3095. int len = 0;
  3096. if (!beep_handle)
  3097. len += sprintf(p + len, "status:\t\tnot supported\n");
  3098. else {
  3099. len += sprintf(p + len, "status:\t\tsupported\n");
  3100. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  3101. }
  3102. return len;
  3103. }
  3104. static int beep_write(char *buf)
  3105. {
  3106. char *cmd;
  3107. int beep_cmd;
  3108. if (!beep_handle)
  3109. return -ENODEV;
  3110. while ((cmd = next_cmd(&buf))) {
  3111. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  3112. beep_cmd >= 0 && beep_cmd <= 17) {
  3113. /* beep_cmd set */
  3114. } else
  3115. return -EINVAL;
  3116. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  3117. return -EIO;
  3118. }
  3119. return 0;
  3120. }
  3121. static struct ibm_struct beep_driver_data = {
  3122. .name = "beep",
  3123. .read = beep_read,
  3124. .write = beep_write,
  3125. };
  3126. /*************************************************************************
  3127. * Thermal subdriver
  3128. */
  3129. enum thermal_access_mode {
  3130. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  3131. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  3132. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  3133. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  3134. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  3135. };
  3136. enum { /* TPACPI_THERMAL_TPEC_* */
  3137. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  3138. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  3139. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  3140. };
  3141. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  3142. struct ibm_thermal_sensors_struct {
  3143. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  3144. };
  3145. static enum thermal_access_mode thermal_read_mode;
  3146. /* idx is zero-based */
  3147. static int thermal_get_sensor(int idx, s32 *value)
  3148. {
  3149. int t;
  3150. s8 tmp;
  3151. char tmpi[5];
  3152. t = TP_EC_THERMAL_TMP0;
  3153. switch (thermal_read_mode) {
  3154. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  3155. case TPACPI_THERMAL_TPEC_16:
  3156. if (idx >= 8 && idx <= 15) {
  3157. t = TP_EC_THERMAL_TMP8;
  3158. idx -= 8;
  3159. }
  3160. /* fallthrough */
  3161. #endif
  3162. case TPACPI_THERMAL_TPEC_8:
  3163. if (idx <= 7) {
  3164. if (!acpi_ec_read(t + idx, &tmp))
  3165. return -EIO;
  3166. *value = tmp * 1000;
  3167. return 0;
  3168. }
  3169. break;
  3170. case TPACPI_THERMAL_ACPI_UPDT:
  3171. if (idx <= 7) {
  3172. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3173. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  3174. return -EIO;
  3175. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3176. return -EIO;
  3177. *value = (t - 2732) * 100;
  3178. return 0;
  3179. }
  3180. break;
  3181. case TPACPI_THERMAL_ACPI_TMP07:
  3182. if (idx <= 7) {
  3183. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3184. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3185. return -EIO;
  3186. if (t > 127 || t < -127)
  3187. t = TP_EC_THERMAL_TMP_NA;
  3188. *value = t * 1000;
  3189. return 0;
  3190. }
  3191. break;
  3192. case TPACPI_THERMAL_NONE:
  3193. default:
  3194. return -ENOSYS;
  3195. }
  3196. return -EINVAL;
  3197. }
  3198. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  3199. {
  3200. int res, i;
  3201. int n;
  3202. n = 8;
  3203. i = 0;
  3204. if (!s)
  3205. return -EINVAL;
  3206. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  3207. n = 16;
  3208. for (i = 0 ; i < n; i++) {
  3209. res = thermal_get_sensor(i, &s->temp[i]);
  3210. if (res)
  3211. return res;
  3212. }
  3213. return n;
  3214. }
  3215. /* sysfs temp##_input -------------------------------------------------- */
  3216. static ssize_t thermal_temp_input_show(struct device *dev,
  3217. struct device_attribute *attr,
  3218. char *buf)
  3219. {
  3220. struct sensor_device_attribute *sensor_attr =
  3221. to_sensor_dev_attr(attr);
  3222. int idx = sensor_attr->index;
  3223. s32 value;
  3224. int res;
  3225. res = thermal_get_sensor(idx, &value);
  3226. if (res)
  3227. return res;
  3228. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  3229. return -ENXIO;
  3230. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  3231. }
  3232. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  3233. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  3234. thermal_temp_input_show, NULL, _idxB)
  3235. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  3236. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  3237. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  3238. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  3239. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  3240. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  3241. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  3242. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  3243. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  3244. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  3245. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  3246. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  3247. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  3248. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  3249. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  3250. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  3251. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  3252. };
  3253. #define THERMAL_ATTRS(X) \
  3254. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  3255. static struct attribute *thermal_temp_input_attr[] = {
  3256. THERMAL_ATTRS(8),
  3257. THERMAL_ATTRS(9),
  3258. THERMAL_ATTRS(10),
  3259. THERMAL_ATTRS(11),
  3260. THERMAL_ATTRS(12),
  3261. THERMAL_ATTRS(13),
  3262. THERMAL_ATTRS(14),
  3263. THERMAL_ATTRS(15),
  3264. THERMAL_ATTRS(0),
  3265. THERMAL_ATTRS(1),
  3266. THERMAL_ATTRS(2),
  3267. THERMAL_ATTRS(3),
  3268. THERMAL_ATTRS(4),
  3269. THERMAL_ATTRS(5),
  3270. THERMAL_ATTRS(6),
  3271. THERMAL_ATTRS(7),
  3272. NULL
  3273. };
  3274. static const struct attribute_group thermal_temp_input16_group = {
  3275. .attrs = thermal_temp_input_attr
  3276. };
  3277. static const struct attribute_group thermal_temp_input8_group = {
  3278. .attrs = &thermal_temp_input_attr[8]
  3279. };
  3280. #undef THERMAL_SENSOR_ATTR_TEMP
  3281. #undef THERMAL_ATTRS
  3282. /* --------------------------------------------------------------------- */
  3283. static int __init thermal_init(struct ibm_init_struct *iibm)
  3284. {
  3285. u8 t, ta1, ta2;
  3286. int i;
  3287. int acpi_tmp7;
  3288. int res;
  3289. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  3290. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  3291. if (thinkpad_id.ec_model) {
  3292. /*
  3293. * Direct EC access mode: sensors at registers
  3294. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  3295. * non-implemented, thermal sensors return 0x80 when
  3296. * not available
  3297. */
  3298. ta1 = ta2 = 0;
  3299. for (i = 0; i < 8; i++) {
  3300. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  3301. ta1 |= t;
  3302. } else {
  3303. ta1 = 0;
  3304. break;
  3305. }
  3306. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  3307. ta2 |= t;
  3308. } else {
  3309. ta1 = 0;
  3310. break;
  3311. }
  3312. }
  3313. if (ta1 == 0) {
  3314. /* This is sheer paranoia, but we handle it anyway */
  3315. if (acpi_tmp7) {
  3316. printk(TPACPI_ERR
  3317. "ThinkPad ACPI EC access misbehaving, "
  3318. "falling back to ACPI TMPx access "
  3319. "mode\n");
  3320. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3321. } else {
  3322. printk(TPACPI_ERR
  3323. "ThinkPad ACPI EC access misbehaving, "
  3324. "disabling thermal sensors access\n");
  3325. thermal_read_mode = TPACPI_THERMAL_NONE;
  3326. }
  3327. } else {
  3328. thermal_read_mode =
  3329. (ta2 != 0) ?
  3330. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  3331. }
  3332. } else if (acpi_tmp7) {
  3333. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  3334. /* 600e/x, 770e, 770x */
  3335. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  3336. } else {
  3337. /* Standard ACPI TMPx access, max 8 sensors */
  3338. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3339. }
  3340. } else {
  3341. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  3342. thermal_read_mode = TPACPI_THERMAL_NONE;
  3343. }
  3344. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  3345. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  3346. thermal_read_mode);
  3347. switch (thermal_read_mode) {
  3348. case TPACPI_THERMAL_TPEC_16:
  3349. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3350. &thermal_temp_input16_group);
  3351. if (res)
  3352. return res;
  3353. break;
  3354. case TPACPI_THERMAL_TPEC_8:
  3355. case TPACPI_THERMAL_ACPI_TMP07:
  3356. case TPACPI_THERMAL_ACPI_UPDT:
  3357. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3358. &thermal_temp_input8_group);
  3359. if (res)
  3360. return res;
  3361. break;
  3362. case TPACPI_THERMAL_NONE:
  3363. default:
  3364. return 1;
  3365. }
  3366. return 0;
  3367. }
  3368. static void thermal_exit(void)
  3369. {
  3370. switch (thermal_read_mode) {
  3371. case TPACPI_THERMAL_TPEC_16:
  3372. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3373. &thermal_temp_input16_group);
  3374. break;
  3375. case TPACPI_THERMAL_TPEC_8:
  3376. case TPACPI_THERMAL_ACPI_TMP07:
  3377. case TPACPI_THERMAL_ACPI_UPDT:
  3378. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3379. &thermal_temp_input16_group);
  3380. break;
  3381. case TPACPI_THERMAL_NONE:
  3382. default:
  3383. break;
  3384. }
  3385. }
  3386. static int thermal_read(char *p)
  3387. {
  3388. int len = 0;
  3389. int n, i;
  3390. struct ibm_thermal_sensors_struct t;
  3391. n = thermal_get_sensors(&t);
  3392. if (unlikely(n < 0))
  3393. return n;
  3394. len += sprintf(p + len, "temperatures:\t");
  3395. if (n > 0) {
  3396. for (i = 0; i < (n - 1); i++)
  3397. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  3398. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  3399. } else
  3400. len += sprintf(p + len, "not supported\n");
  3401. return len;
  3402. }
  3403. static struct ibm_struct thermal_driver_data = {
  3404. .name = "thermal",
  3405. .read = thermal_read,
  3406. .exit = thermal_exit,
  3407. };
  3408. /*************************************************************************
  3409. * EC Dump subdriver
  3410. */
  3411. static u8 ecdump_regs[256];
  3412. static int ecdump_read(char *p)
  3413. {
  3414. int len = 0;
  3415. int i, j;
  3416. u8 v;
  3417. len += sprintf(p + len, "EC "
  3418. " +00 +01 +02 +03 +04 +05 +06 +07"
  3419. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  3420. for (i = 0; i < 256; i += 16) {
  3421. len += sprintf(p + len, "EC 0x%02x:", i);
  3422. for (j = 0; j < 16; j++) {
  3423. if (!acpi_ec_read(i + j, &v))
  3424. break;
  3425. if (v != ecdump_regs[i + j])
  3426. len += sprintf(p + len, " *%02x", v);
  3427. else
  3428. len += sprintf(p + len, " %02x", v);
  3429. ecdump_regs[i + j] = v;
  3430. }
  3431. len += sprintf(p + len, "\n");
  3432. if (j != 16)
  3433. break;
  3434. }
  3435. /* These are way too dangerous to advertise openly... */
  3436. #if 0
  3437. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  3438. " (<offset> is 00-ff, <value> is 00-ff)\n");
  3439. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  3440. " (<offset> is 00-ff, <value> is 0-255)\n");
  3441. #endif
  3442. return len;
  3443. }
  3444. static int ecdump_write(char *buf)
  3445. {
  3446. char *cmd;
  3447. int i, v;
  3448. while ((cmd = next_cmd(&buf))) {
  3449. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  3450. /* i and v set */
  3451. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  3452. /* i and v set */
  3453. } else
  3454. return -EINVAL;
  3455. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  3456. if (!acpi_ec_write(i, v))
  3457. return -EIO;
  3458. } else
  3459. return -EINVAL;
  3460. }
  3461. return 0;
  3462. }
  3463. static struct ibm_struct ecdump_driver_data = {
  3464. .name = "ecdump",
  3465. .read = ecdump_read,
  3466. .write = ecdump_write,
  3467. .flags.experimental = 1,
  3468. };
  3469. /*************************************************************************
  3470. * Backlight/brightness subdriver
  3471. */
  3472. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  3473. static struct backlight_device *ibm_backlight_device;
  3474. static int brightness_offset = 0x31;
  3475. static int brightness_mode;
  3476. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  3477. static struct mutex brightness_mutex;
  3478. /*
  3479. * ThinkPads can read brightness from two places: EC 0x31, or
  3480. * CMOS NVRAM byte 0x5E, bits 0-3.
  3481. */
  3482. static int brightness_get(struct backlight_device *bd)
  3483. {
  3484. u8 lec = 0, lcmos = 0, level = 0;
  3485. if (brightness_mode & 1) {
  3486. if (!acpi_ec_read(brightness_offset, &lec))
  3487. return -EIO;
  3488. lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3489. level = lec;
  3490. };
  3491. if (brightness_mode & 2) {
  3492. lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  3493. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  3494. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  3495. lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3496. level = lcmos;
  3497. }
  3498. if (brightness_mode == 3 && lec != lcmos) {
  3499. printk(TPACPI_ERR
  3500. "CMOS NVRAM (%u) and EC (%u) do not agree "
  3501. "on display brightness level\n",
  3502. (unsigned int) lcmos,
  3503. (unsigned int) lec);
  3504. return -EIO;
  3505. }
  3506. return level;
  3507. }
  3508. /* May return EINTR which can always be mapped to ERESTARTSYS */
  3509. static int brightness_set(int value)
  3510. {
  3511. int cmos_cmd, inc, i, res;
  3512. int current_value;
  3513. if (value > ((tp_features.bright_16levels)? 15 : 7))
  3514. return -EINVAL;
  3515. res = mutex_lock_interruptible(&brightness_mutex);
  3516. if (res < 0)
  3517. return res;
  3518. current_value = brightness_get(NULL);
  3519. if (current_value < 0) {
  3520. res = current_value;
  3521. goto errout;
  3522. }
  3523. cmos_cmd = value > current_value ?
  3524. TP_CMOS_BRIGHTNESS_UP :
  3525. TP_CMOS_BRIGHTNESS_DOWN;
  3526. inc = (value > current_value)? 1 : -1;
  3527. res = 0;
  3528. for (i = current_value; i != value; i += inc) {
  3529. if ((brightness_mode & 2) &&
  3530. issue_thinkpad_cmos_command(cmos_cmd)) {
  3531. res = -EIO;
  3532. goto errout;
  3533. }
  3534. if ((brightness_mode & 1) &&
  3535. !acpi_ec_write(brightness_offset, i + inc)) {
  3536. res = -EIO;
  3537. goto errout;;
  3538. }
  3539. }
  3540. errout:
  3541. mutex_unlock(&brightness_mutex);
  3542. return res;
  3543. }
  3544. /* sysfs backlight class ----------------------------------------------- */
  3545. static int brightness_update_status(struct backlight_device *bd)
  3546. {
  3547. /* it is the backlight class's job (caller) to handle
  3548. * EINTR and other errors properly */
  3549. return brightness_set(
  3550. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  3551. bd->props.power == FB_BLANK_UNBLANK) ?
  3552. bd->props.brightness : 0);
  3553. }
  3554. static struct backlight_ops ibm_backlight_data = {
  3555. .get_brightness = brightness_get,
  3556. .update_status = brightness_update_status,
  3557. };
  3558. /* --------------------------------------------------------------------- */
  3559. static int __init tpacpi_query_bcll_levels(acpi_handle handle)
  3560. {
  3561. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  3562. union acpi_object *obj;
  3563. int rc;
  3564. if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
  3565. obj = (union acpi_object *)buffer.pointer;
  3566. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  3567. printk(TPACPI_ERR "Unknown BCLL data, "
  3568. "please report this to %s\n", TPACPI_MAIL);
  3569. rc = 0;
  3570. } else {
  3571. rc = obj->package.count;
  3572. }
  3573. } else {
  3574. return 0;
  3575. }
  3576. kfree(buffer.pointer);
  3577. return rc;
  3578. }
  3579. static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
  3580. void *context, void **rv)
  3581. {
  3582. char name[ACPI_PATH_SEGMENT_LENGTH];
  3583. struct acpi_buffer buffer = { sizeof(name), &name };
  3584. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  3585. !strncmp("BCLL", name, sizeof(name) - 1)) {
  3586. if (tpacpi_query_bcll_levels(handle) == 16) {
  3587. *rv = handle;
  3588. return AE_CTRL_TERMINATE;
  3589. } else {
  3590. return AE_OK;
  3591. }
  3592. } else {
  3593. return AE_OK;
  3594. }
  3595. }
  3596. static int __init brightness_check_levels(void)
  3597. {
  3598. int status;
  3599. void *found_node = NULL;
  3600. if (!vid_handle) {
  3601. TPACPI_ACPIHANDLE_INIT(vid);
  3602. }
  3603. if (!vid_handle)
  3604. return 0;
  3605. /* Search for a BCLL package with 16 levels */
  3606. status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
  3607. brightness_find_bcll, NULL,
  3608. &found_node);
  3609. return (ACPI_SUCCESS(status) && found_node != NULL);
  3610. }
  3611. static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
  3612. void *context, void **rv)
  3613. {
  3614. char name[ACPI_PATH_SEGMENT_LENGTH];
  3615. struct acpi_buffer buffer = { sizeof(name), &name };
  3616. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  3617. !strncmp("_BCL", name, sizeof(name) - 1)) {
  3618. *rv = handle;
  3619. return AE_CTRL_TERMINATE;
  3620. } else {
  3621. return AE_OK;
  3622. }
  3623. }
  3624. static int __init brightness_check_std_acpi_support(void)
  3625. {
  3626. int status;
  3627. void *found_node = NULL;
  3628. if (!vid_handle) {
  3629. TPACPI_ACPIHANDLE_INIT(vid);
  3630. }
  3631. if (!vid_handle)
  3632. return 0;
  3633. /* Search for a _BCL method, but don't execute it */
  3634. status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
  3635. brightness_find_bcl, NULL, &found_node);
  3636. return (ACPI_SUCCESS(status) && found_node != NULL);
  3637. }
  3638. static int __init brightness_init(struct ibm_init_struct *iibm)
  3639. {
  3640. int b;
  3641. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  3642. mutex_init(&brightness_mutex);
  3643. if (!brightness_enable) {
  3644. dbg_printk(TPACPI_DBG_INIT,
  3645. "brightness support disabled by "
  3646. "module parameter\n");
  3647. return 1;
  3648. } else if (brightness_enable > 1) {
  3649. if (brightness_check_std_acpi_support()) {
  3650. printk(TPACPI_NOTICE
  3651. "standard ACPI backlight interface "
  3652. "available, not loading native one...\n");
  3653. return 1;
  3654. }
  3655. }
  3656. if (!brightness_mode) {
  3657. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
  3658. brightness_mode = 2;
  3659. else
  3660. brightness_mode = 3;
  3661. dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
  3662. brightness_mode);
  3663. }
  3664. if (brightness_mode > 3)
  3665. return -EINVAL;
  3666. tp_features.bright_16levels =
  3667. thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
  3668. brightness_check_levels();
  3669. b = brightness_get(NULL);
  3670. if (b < 0)
  3671. return 1;
  3672. if (tp_features.bright_16levels)
  3673. printk(TPACPI_INFO
  3674. "detected a 16-level brightness capable ThinkPad\n");
  3675. ibm_backlight_device = backlight_device_register(
  3676. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  3677. &ibm_backlight_data);
  3678. if (IS_ERR(ibm_backlight_device)) {
  3679. printk(TPACPI_ERR "Could not register backlight device\n");
  3680. return PTR_ERR(ibm_backlight_device);
  3681. }
  3682. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  3683. ibm_backlight_device->props.max_brightness =
  3684. (tp_features.bright_16levels)? 15 : 7;
  3685. ibm_backlight_device->props.brightness = b;
  3686. backlight_update_status(ibm_backlight_device);
  3687. return 0;
  3688. }
  3689. static void brightness_exit(void)
  3690. {
  3691. if (ibm_backlight_device) {
  3692. vdbg_printk(TPACPI_DBG_EXIT,
  3693. "calling backlight_device_unregister()\n");
  3694. backlight_device_unregister(ibm_backlight_device);
  3695. ibm_backlight_device = NULL;
  3696. }
  3697. }
  3698. static int brightness_read(char *p)
  3699. {
  3700. int len = 0;
  3701. int level;
  3702. level = brightness_get(NULL);
  3703. if (level < 0) {
  3704. len += sprintf(p + len, "level:\t\tunreadable\n");
  3705. } else {
  3706. len += sprintf(p + len, "level:\t\t%d\n", level);
  3707. len += sprintf(p + len, "commands:\tup, down\n");
  3708. len += sprintf(p + len, "commands:\tlevel <level>"
  3709. " (<level> is 0-%d)\n",
  3710. (tp_features.bright_16levels) ? 15 : 7);
  3711. }
  3712. return len;
  3713. }
  3714. static int brightness_write(char *buf)
  3715. {
  3716. int level;
  3717. int rc;
  3718. char *cmd;
  3719. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  3720. level = brightness_get(NULL);
  3721. if (level < 0)
  3722. return level;
  3723. while ((cmd = next_cmd(&buf))) {
  3724. if (strlencmp(cmd, "up") == 0) {
  3725. if (level < max_level)
  3726. level++;
  3727. } else if (strlencmp(cmd, "down") == 0) {
  3728. if (level > 0)
  3729. level--;
  3730. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  3731. level >= 0 && level <= max_level) {
  3732. /* new level set */
  3733. } else
  3734. return -EINVAL;
  3735. }
  3736. /*
  3737. * Now we know what the final level should be, so we try to set it.
  3738. * Doing it this way makes the syscall restartable in case of EINTR
  3739. */
  3740. rc = brightness_set(level);
  3741. return (rc == -EINTR)? ERESTARTSYS : rc;
  3742. }
  3743. static struct ibm_struct brightness_driver_data = {
  3744. .name = "brightness",
  3745. .read = brightness_read,
  3746. .write = brightness_write,
  3747. .exit = brightness_exit,
  3748. };
  3749. /*************************************************************************
  3750. * Volume subdriver
  3751. */
  3752. static int volume_offset = 0x30;
  3753. static int volume_read(char *p)
  3754. {
  3755. int len = 0;
  3756. u8 level;
  3757. if (!acpi_ec_read(volume_offset, &level)) {
  3758. len += sprintf(p + len, "level:\t\tunreadable\n");
  3759. } else {
  3760. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  3761. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  3762. len += sprintf(p + len, "commands:\tup, down, mute\n");
  3763. len += sprintf(p + len, "commands:\tlevel <level>"
  3764. " (<level> is 0-15)\n");
  3765. }
  3766. return len;
  3767. }
  3768. static int volume_write(char *buf)
  3769. {
  3770. int cmos_cmd, inc, i;
  3771. u8 level, mute;
  3772. int new_level, new_mute;
  3773. char *cmd;
  3774. while ((cmd = next_cmd(&buf))) {
  3775. if (!acpi_ec_read(volume_offset, &level))
  3776. return -EIO;
  3777. new_mute = mute = level & 0x40;
  3778. new_level = level = level & 0xf;
  3779. if (strlencmp(cmd, "up") == 0) {
  3780. if (mute)
  3781. new_mute = 0;
  3782. else
  3783. new_level = level == 15 ? 15 : level + 1;
  3784. } else if (strlencmp(cmd, "down") == 0) {
  3785. if (mute)
  3786. new_mute = 0;
  3787. else
  3788. new_level = level == 0 ? 0 : level - 1;
  3789. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  3790. new_level >= 0 && new_level <= 15) {
  3791. /* new_level set */
  3792. } else if (strlencmp(cmd, "mute") == 0) {
  3793. new_mute = 0x40;
  3794. } else
  3795. return -EINVAL;
  3796. if (new_level != level) {
  3797. /* mute doesn't change */
  3798. cmos_cmd = (new_level > level) ?
  3799. TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  3800. inc = new_level > level ? 1 : -1;
  3801. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  3802. !acpi_ec_write(volume_offset, level)))
  3803. return -EIO;
  3804. for (i = level; i != new_level; i += inc)
  3805. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  3806. !acpi_ec_write(volume_offset, i + inc))
  3807. return -EIO;
  3808. if (mute &&
  3809. (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  3810. !acpi_ec_write(volume_offset, new_level + mute))) {
  3811. return -EIO;
  3812. }
  3813. }
  3814. if (new_mute != mute) {
  3815. /* level doesn't change */
  3816. cmos_cmd = (new_mute) ?
  3817. TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  3818. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  3819. !acpi_ec_write(volume_offset, level + new_mute))
  3820. return -EIO;
  3821. }
  3822. }
  3823. return 0;
  3824. }
  3825. static struct ibm_struct volume_driver_data = {
  3826. .name = "volume",
  3827. .read = volume_read,
  3828. .write = volume_write,
  3829. };
  3830. /*************************************************************************
  3831. * Fan subdriver
  3832. */
  3833. /*
  3834. * FAN ACCESS MODES
  3835. *
  3836. * TPACPI_FAN_RD_ACPI_GFAN:
  3837. * ACPI GFAN method: returns fan level
  3838. *
  3839. * see TPACPI_FAN_WR_ACPI_SFAN
  3840. * EC 0x2f (HFSP) not available if GFAN exists
  3841. *
  3842. * TPACPI_FAN_WR_ACPI_SFAN:
  3843. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  3844. *
  3845. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  3846. * it for writing.
  3847. *
  3848. * TPACPI_FAN_WR_TPEC:
  3849. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  3850. * Supported on almost all ThinkPads
  3851. *
  3852. * Fan speed changes of any sort (including those caused by the
  3853. * disengaged mode) are usually done slowly by the firmware as the
  3854. * maximum ammount of fan duty cycle change per second seems to be
  3855. * limited.
  3856. *
  3857. * Reading is not available if GFAN exists.
  3858. * Writing is not available if SFAN exists.
  3859. *
  3860. * Bits
  3861. * 7 automatic mode engaged;
  3862. * (default operation mode of the ThinkPad)
  3863. * fan level is ignored in this mode.
  3864. * 6 full speed mode (takes precedence over bit 7);
  3865. * not available on all thinkpads. May disable
  3866. * the tachometer while the fan controller ramps up
  3867. * the speed (which can take up to a few *minutes*).
  3868. * Speeds up fan to 100% duty-cycle, which is far above
  3869. * the standard RPM levels. It is not impossible that
  3870. * it could cause hardware damage.
  3871. * 5-3 unused in some models. Extra bits for fan level
  3872. * in others, but still useless as all values above
  3873. * 7 map to the same speed as level 7 in these models.
  3874. * 2-0 fan level (0..7 usually)
  3875. * 0x00 = stop
  3876. * 0x07 = max (set when temperatures critical)
  3877. * Some ThinkPads may have other levels, see
  3878. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  3879. *
  3880. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  3881. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  3882. * does so, its initial value is meaningless (0x07).
  3883. *
  3884. * For firmware bugs, refer to:
  3885. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  3886. *
  3887. * ----
  3888. *
  3889. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  3890. * Main fan tachometer reading (in RPM)
  3891. *
  3892. * This register is present on all ThinkPads with a new-style EC, and
  3893. * it is known not to be present on the A21m/e, and T22, as there is
  3894. * something else in offset 0x84 according to the ACPI DSDT. Other
  3895. * ThinkPads from this same time period (and earlier) probably lack the
  3896. * tachometer as well.
  3897. *
  3898. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  3899. * was never fixed by IBM to report the EC firmware version string
  3900. * probably support the tachometer (like the early X models), so
  3901. * detecting it is quite hard. We need more data to know for sure.
  3902. *
  3903. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  3904. * might result.
  3905. *
  3906. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  3907. * mode.
  3908. *
  3909. * For firmware bugs, refer to:
  3910. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  3911. *
  3912. * TPACPI_FAN_WR_ACPI_FANS:
  3913. * ThinkPad X31, X40, X41. Not available in the X60.
  3914. *
  3915. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  3916. * high speed. ACPI DSDT seems to map these three speeds to levels
  3917. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  3918. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  3919. *
  3920. * The speeds are stored on handles
  3921. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  3922. *
  3923. * There are three default speed sets, acessible as handles:
  3924. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  3925. *
  3926. * ACPI DSDT switches which set is in use depending on various
  3927. * factors.
  3928. *
  3929. * TPACPI_FAN_WR_TPEC is also available and should be used to
  3930. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  3931. * but the ACPI tables just mention level 7.
  3932. */
  3933. enum { /* Fan control constants */
  3934. fan_status_offset = 0x2f, /* EC register 0x2f */
  3935. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  3936. * 0x84 must be read before 0x85 */
  3937. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  3938. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  3939. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  3940. };
  3941. enum fan_status_access_mode {
  3942. TPACPI_FAN_NONE = 0, /* No fan status or control */
  3943. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  3944. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  3945. };
  3946. enum fan_control_access_mode {
  3947. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  3948. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  3949. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  3950. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  3951. };
  3952. enum fan_control_commands {
  3953. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  3954. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  3955. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  3956. * and also watchdog cmd */
  3957. };
  3958. static int fan_control_allowed;
  3959. static enum fan_status_access_mode fan_status_access_mode;
  3960. static enum fan_control_access_mode fan_control_access_mode;
  3961. static enum fan_control_commands fan_control_commands;
  3962. static u8 fan_control_initial_status;
  3963. static u8 fan_control_desired_level;
  3964. static int fan_watchdog_maxinterval;
  3965. static struct mutex fan_mutex;
  3966. static void fan_watchdog_fire(struct work_struct *ignored);
  3967. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  3968. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  3969. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  3970. "\\FSPD", /* 600e/x, 770e, 770x */
  3971. ); /* all others */
  3972. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  3973. "JFNS", /* 770x-JL */
  3974. ); /* all others */
  3975. /*
  3976. * Call with fan_mutex held
  3977. */
  3978. static void fan_update_desired_level(u8 status)
  3979. {
  3980. if ((status &
  3981. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  3982. if (status > 7)
  3983. fan_control_desired_level = 7;
  3984. else
  3985. fan_control_desired_level = status;
  3986. }
  3987. }
  3988. static int fan_get_status(u8 *status)
  3989. {
  3990. u8 s;
  3991. /* TODO:
  3992. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  3993. switch (fan_status_access_mode) {
  3994. case TPACPI_FAN_RD_ACPI_GFAN:
  3995. /* 570, 600e/x, 770e, 770x */
  3996. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  3997. return -EIO;
  3998. if (likely(status))
  3999. *status = s & 0x07;
  4000. break;
  4001. case TPACPI_FAN_RD_TPEC:
  4002. /* all except 570, 600e/x, 770e, 770x */
  4003. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  4004. return -EIO;
  4005. if (likely(status))
  4006. *status = s;
  4007. break;
  4008. default:
  4009. return -ENXIO;
  4010. }
  4011. return 0;
  4012. }
  4013. static int fan_get_status_safe(u8 *status)
  4014. {
  4015. int rc;
  4016. u8 s;
  4017. if (mutex_lock_interruptible(&fan_mutex))
  4018. return -ERESTARTSYS;
  4019. rc = fan_get_status(&s);
  4020. if (!rc)
  4021. fan_update_desired_level(s);
  4022. mutex_unlock(&fan_mutex);
  4023. if (status)
  4024. *status = s;
  4025. return rc;
  4026. }
  4027. static int fan_get_speed(unsigned int *speed)
  4028. {
  4029. u8 hi, lo;
  4030. switch (fan_status_access_mode) {
  4031. case TPACPI_FAN_RD_TPEC:
  4032. /* all except 570, 600e/x, 770e, 770x */
  4033. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  4034. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  4035. return -EIO;
  4036. if (likely(speed))
  4037. *speed = (hi << 8) | lo;
  4038. break;
  4039. default:
  4040. return -ENXIO;
  4041. }
  4042. return 0;
  4043. }
  4044. static int fan_set_level(int level)
  4045. {
  4046. if (!fan_control_allowed)
  4047. return -EPERM;
  4048. switch (fan_control_access_mode) {
  4049. case TPACPI_FAN_WR_ACPI_SFAN:
  4050. if (level >= 0 && level <= 7) {
  4051. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  4052. return -EIO;
  4053. } else
  4054. return -EINVAL;
  4055. break;
  4056. case TPACPI_FAN_WR_ACPI_FANS:
  4057. case TPACPI_FAN_WR_TPEC:
  4058. if ((level != TP_EC_FAN_AUTO) &&
  4059. (level != TP_EC_FAN_FULLSPEED) &&
  4060. ((level < 0) || (level > 7)))
  4061. return -EINVAL;
  4062. /* safety net should the EC not support AUTO
  4063. * or FULLSPEED mode bits and just ignore them */
  4064. if (level & TP_EC_FAN_FULLSPEED)
  4065. level |= 7; /* safety min speed 7 */
  4066. else if (level & TP_EC_FAN_AUTO)
  4067. level |= 4; /* safety min speed 4 */
  4068. if (!acpi_ec_write(fan_status_offset, level))
  4069. return -EIO;
  4070. else
  4071. tp_features.fan_ctrl_status_undef = 0;
  4072. break;
  4073. default:
  4074. return -ENXIO;
  4075. }
  4076. return 0;
  4077. }
  4078. static int fan_set_level_safe(int level)
  4079. {
  4080. int rc;
  4081. if (!fan_control_allowed)
  4082. return -EPERM;
  4083. if (mutex_lock_interruptible(&fan_mutex))
  4084. return -ERESTARTSYS;
  4085. if (level == TPACPI_FAN_LAST_LEVEL)
  4086. level = fan_control_desired_level;
  4087. rc = fan_set_level(level);
  4088. if (!rc)
  4089. fan_update_desired_level(level);
  4090. mutex_unlock(&fan_mutex);
  4091. return rc;
  4092. }
  4093. static int fan_set_enable(void)
  4094. {
  4095. u8 s;
  4096. int rc;
  4097. if (!fan_control_allowed)
  4098. return -EPERM;
  4099. if (mutex_lock_interruptible(&fan_mutex))
  4100. return -ERESTARTSYS;
  4101. switch (fan_control_access_mode) {
  4102. case TPACPI_FAN_WR_ACPI_FANS:
  4103. case TPACPI_FAN_WR_TPEC:
  4104. rc = fan_get_status(&s);
  4105. if (rc < 0)
  4106. break;
  4107. /* Don't go out of emergency fan mode */
  4108. if (s != 7) {
  4109. s &= 0x07;
  4110. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  4111. }
  4112. if (!acpi_ec_write(fan_status_offset, s))
  4113. rc = -EIO;
  4114. else {
  4115. tp_features.fan_ctrl_status_undef = 0;
  4116. rc = 0;
  4117. }
  4118. break;
  4119. case TPACPI_FAN_WR_ACPI_SFAN:
  4120. rc = fan_get_status(&s);
  4121. if (rc < 0)
  4122. break;
  4123. s &= 0x07;
  4124. /* Set fan to at least level 4 */
  4125. s |= 4;
  4126. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  4127. rc = -EIO;
  4128. else
  4129. rc = 0;
  4130. break;
  4131. default:
  4132. rc = -ENXIO;
  4133. }
  4134. mutex_unlock(&fan_mutex);
  4135. return rc;
  4136. }
  4137. static int fan_set_disable(void)
  4138. {
  4139. int rc;
  4140. if (!fan_control_allowed)
  4141. return -EPERM;
  4142. if (mutex_lock_interruptible(&fan_mutex))
  4143. return -ERESTARTSYS;
  4144. rc = 0;
  4145. switch (fan_control_access_mode) {
  4146. case TPACPI_FAN_WR_ACPI_FANS:
  4147. case TPACPI_FAN_WR_TPEC:
  4148. if (!acpi_ec_write(fan_status_offset, 0x00))
  4149. rc = -EIO;
  4150. else {
  4151. fan_control_desired_level = 0;
  4152. tp_features.fan_ctrl_status_undef = 0;
  4153. }
  4154. break;
  4155. case TPACPI_FAN_WR_ACPI_SFAN:
  4156. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  4157. rc = -EIO;
  4158. else
  4159. fan_control_desired_level = 0;
  4160. break;
  4161. default:
  4162. rc = -ENXIO;
  4163. }
  4164. mutex_unlock(&fan_mutex);
  4165. return rc;
  4166. }
  4167. static int fan_set_speed(int speed)
  4168. {
  4169. int rc;
  4170. if (!fan_control_allowed)
  4171. return -EPERM;
  4172. if (mutex_lock_interruptible(&fan_mutex))
  4173. return -ERESTARTSYS;
  4174. rc = 0;
  4175. switch (fan_control_access_mode) {
  4176. case TPACPI_FAN_WR_ACPI_FANS:
  4177. if (speed >= 0 && speed <= 65535) {
  4178. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  4179. speed, speed, speed))
  4180. rc = -EIO;
  4181. } else
  4182. rc = -EINVAL;
  4183. break;
  4184. default:
  4185. rc = -ENXIO;
  4186. }
  4187. mutex_unlock(&fan_mutex);
  4188. return rc;
  4189. }
  4190. static void fan_watchdog_reset(void)
  4191. {
  4192. static int fan_watchdog_active;
  4193. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  4194. return;
  4195. if (fan_watchdog_active)
  4196. cancel_delayed_work(&fan_watchdog_task);
  4197. if (fan_watchdog_maxinterval > 0 &&
  4198. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  4199. fan_watchdog_active = 1;
  4200. if (!schedule_delayed_work(&fan_watchdog_task,
  4201. msecs_to_jiffies(fan_watchdog_maxinterval
  4202. * 1000))) {
  4203. printk(TPACPI_ERR
  4204. "failed to schedule the fan watchdog, "
  4205. "watchdog will not trigger\n");
  4206. }
  4207. } else
  4208. fan_watchdog_active = 0;
  4209. }
  4210. static void fan_watchdog_fire(struct work_struct *ignored)
  4211. {
  4212. int rc;
  4213. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  4214. return;
  4215. printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
  4216. rc = fan_set_enable();
  4217. if (rc < 0) {
  4218. printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
  4219. "will try again later...\n", -rc);
  4220. /* reschedule for later */
  4221. fan_watchdog_reset();
  4222. }
  4223. }
  4224. /*
  4225. * SYSFS fan layout: hwmon compatible (device)
  4226. *
  4227. * pwm*_enable:
  4228. * 0: "disengaged" mode
  4229. * 1: manual mode
  4230. * 2: native EC "auto" mode (recommended, hardware default)
  4231. *
  4232. * pwm*: set speed in manual mode, ignored otherwise.
  4233. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  4234. * interpolation.
  4235. *
  4236. * fan*_input: tachometer reading, RPM
  4237. *
  4238. *
  4239. * SYSFS fan layout: extensions
  4240. *
  4241. * fan_watchdog (driver):
  4242. * fan watchdog interval in seconds, 0 disables (default), max 120
  4243. */
  4244. /* sysfs fan pwm1_enable ----------------------------------------------- */
  4245. static ssize_t fan_pwm1_enable_show(struct device *dev,
  4246. struct device_attribute *attr,
  4247. char *buf)
  4248. {
  4249. int res, mode;
  4250. u8 status;
  4251. res = fan_get_status_safe(&status);
  4252. if (res)
  4253. return res;
  4254. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4255. if (status != fan_control_initial_status) {
  4256. tp_features.fan_ctrl_status_undef = 0;
  4257. } else {
  4258. /* Return most likely status. In fact, it
  4259. * might be the only possible status */
  4260. status = TP_EC_FAN_AUTO;
  4261. }
  4262. }
  4263. if (status & TP_EC_FAN_FULLSPEED) {
  4264. mode = 0;
  4265. } else if (status & TP_EC_FAN_AUTO) {
  4266. mode = 2;
  4267. } else
  4268. mode = 1;
  4269. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  4270. }
  4271. static ssize_t fan_pwm1_enable_store(struct device *dev,
  4272. struct device_attribute *attr,
  4273. const char *buf, size_t count)
  4274. {
  4275. unsigned long t;
  4276. int res, level;
  4277. if (parse_strtoul(buf, 2, &t))
  4278. return -EINVAL;
  4279. switch (t) {
  4280. case 0:
  4281. level = TP_EC_FAN_FULLSPEED;
  4282. break;
  4283. case 1:
  4284. level = TPACPI_FAN_LAST_LEVEL;
  4285. break;
  4286. case 2:
  4287. level = TP_EC_FAN_AUTO;
  4288. break;
  4289. case 3:
  4290. /* reserved for software-controlled auto mode */
  4291. return -ENOSYS;
  4292. default:
  4293. return -EINVAL;
  4294. }
  4295. res = fan_set_level_safe(level);
  4296. if (res == -ENXIO)
  4297. return -EINVAL;
  4298. else if (res < 0)
  4299. return res;
  4300. fan_watchdog_reset();
  4301. return count;
  4302. }
  4303. static struct device_attribute dev_attr_fan_pwm1_enable =
  4304. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  4305. fan_pwm1_enable_show, fan_pwm1_enable_store);
  4306. /* sysfs fan pwm1 ------------------------------------------------------ */
  4307. static ssize_t fan_pwm1_show(struct device *dev,
  4308. struct device_attribute *attr,
  4309. char *buf)
  4310. {
  4311. int res;
  4312. u8 status;
  4313. res = fan_get_status_safe(&status);
  4314. if (res)
  4315. return res;
  4316. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4317. if (status != fan_control_initial_status) {
  4318. tp_features.fan_ctrl_status_undef = 0;
  4319. } else {
  4320. status = TP_EC_FAN_AUTO;
  4321. }
  4322. }
  4323. if ((status &
  4324. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  4325. status = fan_control_desired_level;
  4326. if (status > 7)
  4327. status = 7;
  4328. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  4329. }
  4330. static ssize_t fan_pwm1_store(struct device *dev,
  4331. struct device_attribute *attr,
  4332. const char *buf, size_t count)
  4333. {
  4334. unsigned long s;
  4335. int rc;
  4336. u8 status, newlevel;
  4337. if (parse_strtoul(buf, 255, &s))
  4338. return -EINVAL;
  4339. /* scale down from 0-255 to 0-7 */
  4340. newlevel = (s >> 5) & 0x07;
  4341. if (mutex_lock_interruptible(&fan_mutex))
  4342. return -ERESTARTSYS;
  4343. rc = fan_get_status(&status);
  4344. if (!rc && (status &
  4345. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4346. rc = fan_set_level(newlevel);
  4347. if (rc == -ENXIO)
  4348. rc = -EINVAL;
  4349. else if (!rc) {
  4350. fan_update_desired_level(newlevel);
  4351. fan_watchdog_reset();
  4352. }
  4353. }
  4354. mutex_unlock(&fan_mutex);
  4355. return (rc)? rc : count;
  4356. }
  4357. static struct device_attribute dev_attr_fan_pwm1 =
  4358. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  4359. fan_pwm1_show, fan_pwm1_store);
  4360. /* sysfs fan fan1_input ------------------------------------------------ */
  4361. static ssize_t fan_fan1_input_show(struct device *dev,
  4362. struct device_attribute *attr,
  4363. char *buf)
  4364. {
  4365. int res;
  4366. unsigned int speed;
  4367. res = fan_get_speed(&speed);
  4368. if (res < 0)
  4369. return res;
  4370. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  4371. }
  4372. static struct device_attribute dev_attr_fan_fan1_input =
  4373. __ATTR(fan1_input, S_IRUGO,
  4374. fan_fan1_input_show, NULL);
  4375. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  4376. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  4377. char *buf)
  4378. {
  4379. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  4380. }
  4381. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  4382. const char *buf, size_t count)
  4383. {
  4384. unsigned long t;
  4385. if (parse_strtoul(buf, 120, &t))
  4386. return -EINVAL;
  4387. if (!fan_control_allowed)
  4388. return -EPERM;
  4389. fan_watchdog_maxinterval = t;
  4390. fan_watchdog_reset();
  4391. return count;
  4392. }
  4393. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  4394. fan_fan_watchdog_show, fan_fan_watchdog_store);
  4395. /* --------------------------------------------------------------------- */
  4396. static struct attribute *fan_attributes[] = {
  4397. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  4398. &dev_attr_fan_fan1_input.attr,
  4399. NULL
  4400. };
  4401. static const struct attribute_group fan_attr_group = {
  4402. .attrs = fan_attributes,
  4403. };
  4404. static int __init fan_init(struct ibm_init_struct *iibm)
  4405. {
  4406. int rc;
  4407. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  4408. mutex_init(&fan_mutex);
  4409. fan_status_access_mode = TPACPI_FAN_NONE;
  4410. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4411. fan_control_commands = 0;
  4412. fan_watchdog_maxinterval = 0;
  4413. tp_features.fan_ctrl_status_undef = 0;
  4414. fan_control_desired_level = 7;
  4415. TPACPI_ACPIHANDLE_INIT(fans);
  4416. TPACPI_ACPIHANDLE_INIT(gfan);
  4417. TPACPI_ACPIHANDLE_INIT(sfan);
  4418. if (gfan_handle) {
  4419. /* 570, 600e/x, 770e, 770x */
  4420. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  4421. } else {
  4422. /* all other ThinkPads: note that even old-style
  4423. * ThinkPad ECs supports the fan control register */
  4424. if (likely(acpi_ec_read(fan_status_offset,
  4425. &fan_control_initial_status))) {
  4426. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  4427. /* In some ThinkPads, neither the EC nor the ACPI
  4428. * DSDT initialize the fan status, and it ends up
  4429. * being set to 0x07 when it *could* be either
  4430. * 0x07 or 0x80.
  4431. *
  4432. * Enable for TP-1Y (T43), TP-78 (R51e),
  4433. * TP-76 (R52), TP-70 (T43, R52), which are known
  4434. * to be buggy. */
  4435. if (fan_control_initial_status == 0x07) {
  4436. switch (thinkpad_id.ec_model) {
  4437. case 0x5931: /* TP-1Y */
  4438. case 0x3837: /* TP-78 */
  4439. case 0x3637: /* TP-76 */
  4440. case 0x3037: /* TP-70 */
  4441. printk(TPACPI_NOTICE
  4442. "fan_init: initial fan status "
  4443. "is unknown, assuming it is "
  4444. "in auto mode\n");
  4445. tp_features.fan_ctrl_status_undef = 1;
  4446. ;;
  4447. }
  4448. }
  4449. } else {
  4450. printk(TPACPI_ERR
  4451. "ThinkPad ACPI EC access misbehaving, "
  4452. "fan status and control unavailable\n");
  4453. return 1;
  4454. }
  4455. }
  4456. if (sfan_handle) {
  4457. /* 570, 770x-JL */
  4458. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  4459. fan_control_commands |=
  4460. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  4461. } else {
  4462. if (!gfan_handle) {
  4463. /* gfan without sfan means no fan control */
  4464. /* all other models implement TP EC 0x2f control */
  4465. if (fans_handle) {
  4466. /* X31, X40, X41 */
  4467. fan_control_access_mode =
  4468. TPACPI_FAN_WR_ACPI_FANS;
  4469. fan_control_commands |=
  4470. TPACPI_FAN_CMD_SPEED |
  4471. TPACPI_FAN_CMD_LEVEL |
  4472. TPACPI_FAN_CMD_ENABLE;
  4473. } else {
  4474. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  4475. fan_control_commands |=
  4476. TPACPI_FAN_CMD_LEVEL |
  4477. TPACPI_FAN_CMD_ENABLE;
  4478. }
  4479. }
  4480. }
  4481. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  4482. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  4483. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  4484. fan_status_access_mode, fan_control_access_mode);
  4485. /* fan control master switch */
  4486. if (!fan_control_allowed) {
  4487. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4488. fan_control_commands = 0;
  4489. dbg_printk(TPACPI_DBG_INIT,
  4490. "fan control features disabled by parameter\n");
  4491. }
  4492. /* update fan_control_desired_level */
  4493. if (fan_status_access_mode != TPACPI_FAN_NONE)
  4494. fan_get_status_safe(NULL);
  4495. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  4496. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  4497. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4498. &fan_attr_group);
  4499. if (!(rc < 0))
  4500. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  4501. &driver_attr_fan_watchdog);
  4502. if (rc < 0)
  4503. return rc;
  4504. return 0;
  4505. } else
  4506. return 1;
  4507. }
  4508. static void fan_exit(void)
  4509. {
  4510. vdbg_printk(TPACPI_DBG_EXIT,
  4511. "cancelling any pending fan watchdog tasks\n");
  4512. /* FIXME: can we really do this unconditionally? */
  4513. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  4514. driver_remove_file(&tpacpi_hwmon_pdriver.driver,
  4515. &driver_attr_fan_watchdog);
  4516. cancel_delayed_work(&fan_watchdog_task);
  4517. flush_scheduled_work();
  4518. }
  4519. static int fan_read(char *p)
  4520. {
  4521. int len = 0;
  4522. int rc;
  4523. u8 status;
  4524. unsigned int speed = 0;
  4525. switch (fan_status_access_mode) {
  4526. case TPACPI_FAN_RD_ACPI_GFAN:
  4527. /* 570, 600e/x, 770e, 770x */
  4528. rc = fan_get_status_safe(&status);
  4529. if (rc < 0)
  4530. return rc;
  4531. len += sprintf(p + len, "status:\t\t%s\n"
  4532. "level:\t\t%d\n",
  4533. (status != 0) ? "enabled" : "disabled", status);
  4534. break;
  4535. case TPACPI_FAN_RD_TPEC:
  4536. /* all except 570, 600e/x, 770e, 770x */
  4537. rc = fan_get_status_safe(&status);
  4538. if (rc < 0)
  4539. return rc;
  4540. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4541. if (status != fan_control_initial_status)
  4542. tp_features.fan_ctrl_status_undef = 0;
  4543. else
  4544. /* Return most likely status. In fact, it
  4545. * might be the only possible status */
  4546. status = TP_EC_FAN_AUTO;
  4547. }
  4548. len += sprintf(p + len, "status:\t\t%s\n",
  4549. (status != 0) ? "enabled" : "disabled");
  4550. rc = fan_get_speed(&speed);
  4551. if (rc < 0)
  4552. return rc;
  4553. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  4554. if (status & TP_EC_FAN_FULLSPEED)
  4555. /* Disengaged mode takes precedence */
  4556. len += sprintf(p + len, "level:\t\tdisengaged\n");
  4557. else if (status & TP_EC_FAN_AUTO)
  4558. len += sprintf(p + len, "level:\t\tauto\n");
  4559. else
  4560. len += sprintf(p + len, "level:\t\t%d\n", status);
  4561. break;
  4562. case TPACPI_FAN_NONE:
  4563. default:
  4564. len += sprintf(p + len, "status:\t\tnot supported\n");
  4565. }
  4566. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  4567. len += sprintf(p + len, "commands:\tlevel <level>");
  4568. switch (fan_control_access_mode) {
  4569. case TPACPI_FAN_WR_ACPI_SFAN:
  4570. len += sprintf(p + len, " (<level> is 0-7)\n");
  4571. break;
  4572. default:
  4573. len += sprintf(p + len, " (<level> is 0-7, "
  4574. "auto, disengaged, full-speed)\n");
  4575. break;
  4576. }
  4577. }
  4578. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  4579. len += sprintf(p + len, "commands:\tenable, disable\n"
  4580. "commands:\twatchdog <timeout> (<timeout> "
  4581. "is 0 (off), 1-120 (seconds))\n");
  4582. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  4583. len += sprintf(p + len, "commands:\tspeed <speed>"
  4584. " (<speed> is 0-65535)\n");
  4585. return len;
  4586. }
  4587. static int fan_write_cmd_level(const char *cmd, int *rc)
  4588. {
  4589. int level;
  4590. if (strlencmp(cmd, "level auto") == 0)
  4591. level = TP_EC_FAN_AUTO;
  4592. else if ((strlencmp(cmd, "level disengaged") == 0) |
  4593. (strlencmp(cmd, "level full-speed") == 0))
  4594. level = TP_EC_FAN_FULLSPEED;
  4595. else if (sscanf(cmd, "level %d", &level) != 1)
  4596. return 0;
  4597. *rc = fan_set_level_safe(level);
  4598. if (*rc == -ENXIO)
  4599. printk(TPACPI_ERR "level command accepted for unsupported "
  4600. "access mode %d", fan_control_access_mode);
  4601. return 1;
  4602. }
  4603. static int fan_write_cmd_enable(const char *cmd, int *rc)
  4604. {
  4605. if (strlencmp(cmd, "enable") != 0)
  4606. return 0;
  4607. *rc = fan_set_enable();
  4608. if (*rc == -ENXIO)
  4609. printk(TPACPI_ERR "enable command accepted for unsupported "
  4610. "access mode %d", fan_control_access_mode);
  4611. return 1;
  4612. }
  4613. static int fan_write_cmd_disable(const char *cmd, int *rc)
  4614. {
  4615. if (strlencmp(cmd, "disable") != 0)
  4616. return 0;
  4617. *rc = fan_set_disable();
  4618. if (*rc == -ENXIO)
  4619. printk(TPACPI_ERR "disable command accepted for unsupported "
  4620. "access mode %d", fan_control_access_mode);
  4621. return 1;
  4622. }
  4623. static int fan_write_cmd_speed(const char *cmd, int *rc)
  4624. {
  4625. int speed;
  4626. /* TODO:
  4627. * Support speed <low> <medium> <high> ? */
  4628. if (sscanf(cmd, "speed %d", &speed) != 1)
  4629. return 0;
  4630. *rc = fan_set_speed(speed);
  4631. if (*rc == -ENXIO)
  4632. printk(TPACPI_ERR "speed command accepted for unsupported "
  4633. "access mode %d", fan_control_access_mode);
  4634. return 1;
  4635. }
  4636. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  4637. {
  4638. int interval;
  4639. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  4640. return 0;
  4641. if (interval < 0 || interval > 120)
  4642. *rc = -EINVAL;
  4643. else
  4644. fan_watchdog_maxinterval = interval;
  4645. return 1;
  4646. }
  4647. static int fan_write(char *buf)
  4648. {
  4649. char *cmd;
  4650. int rc = 0;
  4651. while (!rc && (cmd = next_cmd(&buf))) {
  4652. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  4653. fan_write_cmd_level(cmd, &rc)) &&
  4654. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  4655. (fan_write_cmd_enable(cmd, &rc) ||
  4656. fan_write_cmd_disable(cmd, &rc) ||
  4657. fan_write_cmd_watchdog(cmd, &rc))) &&
  4658. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  4659. fan_write_cmd_speed(cmd, &rc))
  4660. )
  4661. rc = -EINVAL;
  4662. else if (!rc)
  4663. fan_watchdog_reset();
  4664. }
  4665. return rc;
  4666. }
  4667. static struct ibm_struct fan_driver_data = {
  4668. .name = "fan",
  4669. .read = fan_read,
  4670. .write = fan_write,
  4671. .exit = fan_exit,
  4672. };
  4673. /****************************************************************************
  4674. ****************************************************************************
  4675. *
  4676. * Infrastructure
  4677. *
  4678. ****************************************************************************
  4679. ****************************************************************************/
  4680. /* sysfs name ---------------------------------------------------------- */
  4681. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  4682. struct device_attribute *attr,
  4683. char *buf)
  4684. {
  4685. return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
  4686. }
  4687. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  4688. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  4689. /* --------------------------------------------------------------------- */
  4690. /* /proc support */
  4691. static struct proc_dir_entry *proc_dir;
  4692. /*
  4693. * Module and infrastructure proble, init and exit handling
  4694. */
  4695. static int force_load;
  4696. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  4697. static const char * __init str_supported(int is_supported)
  4698. {
  4699. static char text_unsupported[] __initdata = "not supported";
  4700. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  4701. }
  4702. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  4703. static void ibm_exit(struct ibm_struct *ibm)
  4704. {
  4705. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  4706. list_del_init(&ibm->all_drivers);
  4707. if (ibm->flags.acpi_notify_installed) {
  4708. dbg_printk(TPACPI_DBG_EXIT,
  4709. "%s: acpi_remove_notify_handler\n", ibm->name);
  4710. BUG_ON(!ibm->acpi);
  4711. acpi_remove_notify_handler(*ibm->acpi->handle,
  4712. ibm->acpi->type,
  4713. dispatch_acpi_notify);
  4714. ibm->flags.acpi_notify_installed = 0;
  4715. ibm->flags.acpi_notify_installed = 0;
  4716. }
  4717. if (ibm->flags.proc_created) {
  4718. dbg_printk(TPACPI_DBG_EXIT,
  4719. "%s: remove_proc_entry\n", ibm->name);
  4720. remove_proc_entry(ibm->name, proc_dir);
  4721. ibm->flags.proc_created = 0;
  4722. }
  4723. if (ibm->flags.acpi_driver_registered) {
  4724. dbg_printk(TPACPI_DBG_EXIT,
  4725. "%s: acpi_bus_unregister_driver\n", ibm->name);
  4726. BUG_ON(!ibm->acpi);
  4727. acpi_bus_unregister_driver(ibm->acpi->driver);
  4728. kfree(ibm->acpi->driver);
  4729. ibm->acpi->driver = NULL;
  4730. ibm->flags.acpi_driver_registered = 0;
  4731. }
  4732. if (ibm->flags.init_called && ibm->exit) {
  4733. ibm->exit();
  4734. ibm->flags.init_called = 0;
  4735. }
  4736. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  4737. }
  4738. static int __init ibm_init(struct ibm_init_struct *iibm)
  4739. {
  4740. int ret;
  4741. struct ibm_struct *ibm = iibm->data;
  4742. struct proc_dir_entry *entry;
  4743. BUG_ON(ibm == NULL);
  4744. INIT_LIST_HEAD(&ibm->all_drivers);
  4745. if (ibm->flags.experimental && !experimental)
  4746. return 0;
  4747. dbg_printk(TPACPI_DBG_INIT,
  4748. "probing for %s\n", ibm->name);
  4749. if (iibm->init) {
  4750. ret = iibm->init(iibm);
  4751. if (ret > 0)
  4752. return 0; /* probe failed */
  4753. if (ret)
  4754. return ret;
  4755. ibm->flags.init_called = 1;
  4756. }
  4757. if (ibm->acpi) {
  4758. if (ibm->acpi->hid) {
  4759. ret = register_tpacpi_subdriver(ibm);
  4760. if (ret)
  4761. goto err_out;
  4762. }
  4763. if (ibm->acpi->notify) {
  4764. ret = setup_acpi_notify(ibm);
  4765. if (ret == -ENODEV) {
  4766. printk(TPACPI_NOTICE "disabling subdriver %s\n",
  4767. ibm->name);
  4768. ret = 0;
  4769. goto err_out;
  4770. }
  4771. if (ret < 0)
  4772. goto err_out;
  4773. }
  4774. }
  4775. dbg_printk(TPACPI_DBG_INIT,
  4776. "%s installed\n", ibm->name);
  4777. if (ibm->read) {
  4778. entry = create_proc_entry(ibm->name,
  4779. S_IFREG | S_IRUGO | S_IWUSR,
  4780. proc_dir);
  4781. if (!entry) {
  4782. printk(TPACPI_ERR "unable to create proc entry %s\n",
  4783. ibm->name);
  4784. ret = -ENODEV;
  4785. goto err_out;
  4786. }
  4787. entry->owner = THIS_MODULE;
  4788. entry->data = ibm;
  4789. entry->read_proc = &dispatch_procfs_read;
  4790. if (ibm->write)
  4791. entry->write_proc = &dispatch_procfs_write;
  4792. ibm->flags.proc_created = 1;
  4793. }
  4794. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  4795. return 0;
  4796. err_out:
  4797. dbg_printk(TPACPI_DBG_INIT,
  4798. "%s: at error exit path with result %d\n",
  4799. ibm->name, ret);
  4800. ibm_exit(ibm);
  4801. return (ret < 0)? ret : 0;
  4802. }
  4803. /* Probing */
  4804. static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
  4805. {
  4806. const struct dmi_device *dev = NULL;
  4807. char ec_fw_string[18];
  4808. if (!tp)
  4809. return;
  4810. memset(tp, 0, sizeof(*tp));
  4811. if (dmi_name_in_vendors("IBM"))
  4812. tp->vendor = PCI_VENDOR_ID_IBM;
  4813. else if (dmi_name_in_vendors("LENOVO"))
  4814. tp->vendor = PCI_VENDOR_ID_LENOVO;
  4815. else
  4816. return;
  4817. tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
  4818. GFP_KERNEL);
  4819. if (!tp->bios_version_str)
  4820. return;
  4821. tp->bios_model = tp->bios_version_str[0]
  4822. | (tp->bios_version_str[1] << 8);
  4823. /*
  4824. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  4825. * X32 or newer, all Z series; Some models must have an
  4826. * up-to-date BIOS or they will not be detected.
  4827. *
  4828. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  4829. */
  4830. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  4831. if (sscanf(dev->name,
  4832. "IBM ThinkPad Embedded Controller -[%17c",
  4833. ec_fw_string) == 1) {
  4834. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  4835. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  4836. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  4837. tp->ec_model = ec_fw_string[0]
  4838. | (ec_fw_string[1] << 8);
  4839. break;
  4840. }
  4841. }
  4842. tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
  4843. GFP_KERNEL);
  4844. if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
  4845. kfree(tp->model_str);
  4846. tp->model_str = NULL;
  4847. }
  4848. }
  4849. static int __init probe_for_thinkpad(void)
  4850. {
  4851. int is_thinkpad;
  4852. if (acpi_disabled)
  4853. return -ENODEV;
  4854. /*
  4855. * Non-ancient models have better DMI tagging, but very old models
  4856. * don't.
  4857. */
  4858. is_thinkpad = (thinkpad_id.model_str != NULL);
  4859. /* ec is required because many other handles are relative to it */
  4860. TPACPI_ACPIHANDLE_INIT(ec);
  4861. if (!ec_handle) {
  4862. if (is_thinkpad)
  4863. printk(TPACPI_ERR
  4864. "Not yet supported ThinkPad detected!\n");
  4865. return -ENODEV;
  4866. }
  4867. /*
  4868. * Risks a regression on very old machines, but reduces potential
  4869. * false positives a damn great deal
  4870. */
  4871. if (!is_thinkpad)
  4872. is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
  4873. if (!is_thinkpad && !force_load)
  4874. return -ENODEV;
  4875. return 0;
  4876. }
  4877. /* Module init, exit, parameters */
  4878. static struct ibm_init_struct ibms_init[] __initdata = {
  4879. {
  4880. .init = thinkpad_acpi_driver_init,
  4881. .data = &thinkpad_acpi_driver_data,
  4882. },
  4883. {
  4884. .init = hotkey_init,
  4885. .data = &hotkey_driver_data,
  4886. },
  4887. {
  4888. .init = bluetooth_init,
  4889. .data = &bluetooth_driver_data,
  4890. },
  4891. {
  4892. .init = wan_init,
  4893. .data = &wan_driver_data,
  4894. },
  4895. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  4896. {
  4897. .init = video_init,
  4898. .data = &video_driver_data,
  4899. },
  4900. #endif
  4901. {
  4902. .init = light_init,
  4903. .data = &light_driver_data,
  4904. },
  4905. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  4906. {
  4907. .init = dock_init,
  4908. .data = &dock_driver_data[0],
  4909. },
  4910. {
  4911. .init = dock_init2,
  4912. .data = &dock_driver_data[1],
  4913. },
  4914. #endif
  4915. #ifdef CONFIG_THINKPAD_ACPI_BAY
  4916. {
  4917. .init = bay_init,
  4918. .data = &bay_driver_data,
  4919. },
  4920. #endif
  4921. {
  4922. .init = cmos_init,
  4923. .data = &cmos_driver_data,
  4924. },
  4925. {
  4926. .init = led_init,
  4927. .data = &led_driver_data,
  4928. },
  4929. {
  4930. .init = beep_init,
  4931. .data = &beep_driver_data,
  4932. },
  4933. {
  4934. .init = thermal_init,
  4935. .data = &thermal_driver_data,
  4936. },
  4937. {
  4938. .data = &ecdump_driver_data,
  4939. },
  4940. {
  4941. .init = brightness_init,
  4942. .data = &brightness_driver_data,
  4943. },
  4944. {
  4945. .data = &volume_driver_data,
  4946. },
  4947. {
  4948. .init = fan_init,
  4949. .data = &fan_driver_data,
  4950. },
  4951. };
  4952. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  4953. {
  4954. unsigned int i;
  4955. struct ibm_struct *ibm;
  4956. if (!kp || !kp->name || !val)
  4957. return -EINVAL;
  4958. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  4959. ibm = ibms_init[i].data;
  4960. WARN_ON(ibm == NULL);
  4961. if (!ibm || !ibm->name)
  4962. continue;
  4963. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  4964. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  4965. return -ENOSPC;
  4966. strcpy(ibms_init[i].param, val);
  4967. strcat(ibms_init[i].param, ",");
  4968. return 0;
  4969. }
  4970. }
  4971. return -EINVAL;
  4972. }
  4973. module_param(experimental, int, 0);
  4974. MODULE_PARM_DESC(experimental,
  4975. "Enables experimental features when non-zero");
  4976. module_param_named(debug, dbg_level, uint, 0);
  4977. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  4978. module_param(force_load, bool, 0);
  4979. MODULE_PARM_DESC(force_load,
  4980. "Attempts to load the driver even on a "
  4981. "mis-identified ThinkPad when true");
  4982. module_param_named(fan_control, fan_control_allowed, bool, 0);
  4983. MODULE_PARM_DESC(fan_control,
  4984. "Enables setting fan parameters features when true");
  4985. module_param_named(brightness_mode, brightness_mode, int, 0);
  4986. MODULE_PARM_DESC(brightness_mode,
  4987. "Selects brightness control strategy: "
  4988. "0=auto, 1=EC, 2=CMOS, 3=both");
  4989. module_param(brightness_enable, uint, 0);
  4990. MODULE_PARM_DESC(brightness_enable,
  4991. "Enables backlight control when 1, disables when 0");
  4992. module_param(hotkey_report_mode, uint, 0);
  4993. MODULE_PARM_DESC(hotkey_report_mode,
  4994. "used for backwards compatibility with userspace, "
  4995. "see documentation");
  4996. #define TPACPI_PARAM(feature) \
  4997. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  4998. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
  4999. "at module load, see documentation")
  5000. TPACPI_PARAM(hotkey);
  5001. TPACPI_PARAM(bluetooth);
  5002. TPACPI_PARAM(video);
  5003. TPACPI_PARAM(light);
  5004. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  5005. TPACPI_PARAM(dock);
  5006. #endif
  5007. #ifdef CONFIG_THINKPAD_ACPI_BAY
  5008. TPACPI_PARAM(bay);
  5009. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  5010. TPACPI_PARAM(cmos);
  5011. TPACPI_PARAM(led);
  5012. TPACPI_PARAM(beep);
  5013. TPACPI_PARAM(ecdump);
  5014. TPACPI_PARAM(brightness);
  5015. TPACPI_PARAM(volume);
  5016. TPACPI_PARAM(fan);
  5017. static void thinkpad_acpi_module_exit(void)
  5018. {
  5019. struct ibm_struct *ibm, *itmp;
  5020. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  5021. list_for_each_entry_safe_reverse(ibm, itmp,
  5022. &tpacpi_all_drivers,
  5023. all_drivers) {
  5024. ibm_exit(ibm);
  5025. }
  5026. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  5027. if (tpacpi_inputdev) {
  5028. if (tp_features.input_device_registered)
  5029. input_unregister_device(tpacpi_inputdev);
  5030. else
  5031. input_free_device(tpacpi_inputdev);
  5032. }
  5033. if (tpacpi_hwmon)
  5034. hwmon_device_unregister(tpacpi_hwmon);
  5035. if (tp_features.sensors_pdev_attrs_registered)
  5036. device_remove_file(&tpacpi_sensors_pdev->dev,
  5037. &dev_attr_thinkpad_acpi_pdev_name);
  5038. if (tpacpi_sensors_pdev)
  5039. platform_device_unregister(tpacpi_sensors_pdev);
  5040. if (tpacpi_pdev)
  5041. platform_device_unregister(tpacpi_pdev);
  5042. if (tp_features.sensors_pdrv_attrs_registered)
  5043. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  5044. if (tp_features.platform_drv_attrs_registered)
  5045. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  5046. if (tp_features.sensors_pdrv_registered)
  5047. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  5048. if (tp_features.platform_drv_registered)
  5049. platform_driver_unregister(&tpacpi_pdriver);
  5050. if (proc_dir)
  5051. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  5052. kfree(thinkpad_id.bios_version_str);
  5053. kfree(thinkpad_id.ec_version_str);
  5054. kfree(thinkpad_id.model_str);
  5055. }
  5056. static int __init thinkpad_acpi_module_init(void)
  5057. {
  5058. int ret, i;
  5059. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  5060. /* Parameter checking */
  5061. if (hotkey_report_mode > 2)
  5062. return -EINVAL;
  5063. /* Driver-level probe */
  5064. get_thinkpad_model_data(&thinkpad_id);
  5065. ret = probe_for_thinkpad();
  5066. if (ret) {
  5067. thinkpad_acpi_module_exit();
  5068. return ret;
  5069. }
  5070. /* Driver initialization */
  5071. TPACPI_ACPIHANDLE_INIT(ecrd);
  5072. TPACPI_ACPIHANDLE_INIT(ecwr);
  5073. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  5074. if (!proc_dir) {
  5075. printk(TPACPI_ERR
  5076. "unable to create proc dir " TPACPI_PROC_DIR);
  5077. thinkpad_acpi_module_exit();
  5078. return -ENODEV;
  5079. }
  5080. proc_dir->owner = THIS_MODULE;
  5081. ret = platform_driver_register(&tpacpi_pdriver);
  5082. if (ret) {
  5083. printk(TPACPI_ERR
  5084. "unable to register main platform driver\n");
  5085. thinkpad_acpi_module_exit();
  5086. return ret;
  5087. }
  5088. tp_features.platform_drv_registered = 1;
  5089. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  5090. if (ret) {
  5091. printk(TPACPI_ERR
  5092. "unable to register hwmon platform driver\n");
  5093. thinkpad_acpi_module_exit();
  5094. return ret;
  5095. }
  5096. tp_features.sensors_pdrv_registered = 1;
  5097. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  5098. if (!ret) {
  5099. tp_features.platform_drv_attrs_registered = 1;
  5100. ret = tpacpi_create_driver_attributes(
  5101. &tpacpi_hwmon_pdriver.driver);
  5102. }
  5103. if (ret) {
  5104. printk(TPACPI_ERR
  5105. "unable to create sysfs driver attributes\n");
  5106. thinkpad_acpi_module_exit();
  5107. return ret;
  5108. }
  5109. tp_features.sensors_pdrv_attrs_registered = 1;
  5110. /* Device initialization */
  5111. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
  5112. NULL, 0);
  5113. if (IS_ERR(tpacpi_pdev)) {
  5114. ret = PTR_ERR(tpacpi_pdev);
  5115. tpacpi_pdev = NULL;
  5116. printk(TPACPI_ERR "unable to register platform device\n");
  5117. thinkpad_acpi_module_exit();
  5118. return ret;
  5119. }
  5120. tpacpi_sensors_pdev = platform_device_register_simple(
  5121. TPACPI_HWMON_DRVR_NAME,
  5122. -1, NULL, 0);
  5123. if (IS_ERR(tpacpi_sensors_pdev)) {
  5124. ret = PTR_ERR(tpacpi_sensors_pdev);
  5125. tpacpi_sensors_pdev = NULL;
  5126. printk(TPACPI_ERR
  5127. "unable to register hwmon platform device\n");
  5128. thinkpad_acpi_module_exit();
  5129. return ret;
  5130. }
  5131. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  5132. &dev_attr_thinkpad_acpi_pdev_name);
  5133. if (ret) {
  5134. printk(TPACPI_ERR
  5135. "unable to create sysfs hwmon device attributes\n");
  5136. thinkpad_acpi_module_exit();
  5137. return ret;
  5138. }
  5139. tp_features.sensors_pdev_attrs_registered = 1;
  5140. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  5141. if (IS_ERR(tpacpi_hwmon)) {
  5142. ret = PTR_ERR(tpacpi_hwmon);
  5143. tpacpi_hwmon = NULL;
  5144. printk(TPACPI_ERR "unable to register hwmon device\n");
  5145. thinkpad_acpi_module_exit();
  5146. return ret;
  5147. }
  5148. mutex_init(&tpacpi_inputdev_send_mutex);
  5149. tpacpi_inputdev = input_allocate_device();
  5150. if (!tpacpi_inputdev) {
  5151. printk(TPACPI_ERR "unable to allocate input device\n");
  5152. thinkpad_acpi_module_exit();
  5153. return -ENOMEM;
  5154. } else {
  5155. /* Prepare input device, but don't register */
  5156. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  5157. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  5158. tpacpi_inputdev->id.bustype = BUS_HOST;
  5159. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  5160. thinkpad_id.vendor :
  5161. PCI_VENDOR_ID_IBM;
  5162. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  5163. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  5164. }
  5165. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  5166. ret = ibm_init(&ibms_init[i]);
  5167. if (ret >= 0 && *ibms_init[i].param)
  5168. ret = ibms_init[i].data->write(ibms_init[i].param);
  5169. if (ret < 0) {
  5170. thinkpad_acpi_module_exit();
  5171. return ret;
  5172. }
  5173. }
  5174. ret = input_register_device(tpacpi_inputdev);
  5175. if (ret < 0) {
  5176. printk(TPACPI_ERR "unable to register input device\n");
  5177. thinkpad_acpi_module_exit();
  5178. return ret;
  5179. } else {
  5180. tp_features.input_device_registered = 1;
  5181. }
  5182. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  5183. return 0;
  5184. }
  5185. /* Please remove this in year 2009 */
  5186. MODULE_ALIAS("ibm_acpi");
  5187. /*
  5188. * DMI matching for module autoloading
  5189. *
  5190. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  5191. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  5192. *
  5193. * Only models listed in thinkwiki will be supported, so add yours
  5194. * if it is not there yet.
  5195. */
  5196. #define IBM_BIOS_MODULE_ALIAS(__type) \
  5197. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
  5198. /* Non-ancient thinkpads */
  5199. MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
  5200. MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
  5201. /* Ancient thinkpad BIOSes have to be identified by
  5202. * BIOS type or model number, and there are far less
  5203. * BIOS types than model numbers... */
  5204. IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
  5205. IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
  5206. IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
  5207. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  5208. MODULE_DESCRIPTION(TPACPI_DESC);
  5209. MODULE_VERSION(TPACPI_VERSION);
  5210. MODULE_LICENSE("GPL");
  5211. module_init(thinkpad_acpi_module_init);
  5212. module_exit(thinkpad_acpi_module_exit);