thinkpad_acpi.c 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190
  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-2009 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.23"
  24. #define TPACPI_SYSFS_VERSION 0x020400
  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/sched.h>
  55. #include <linux/kthread.h>
  56. #include <linux/freezer.h>
  57. #include <linux/delay.h>
  58. #include <linux/nvram.h>
  59. #include <linux/proc_fs.h>
  60. #include <linux/sysfs.h>
  61. #include <linux/backlight.h>
  62. #include <linux/fb.h>
  63. #include <linux/platform_device.h>
  64. #include <linux/hwmon.h>
  65. #include <linux/hwmon-sysfs.h>
  66. #include <linux/input.h>
  67. #include <linux/leds.h>
  68. #include <linux/rfkill.h>
  69. #include <asm/uaccess.h>
  70. #include <linux/dmi.h>
  71. #include <linux/jiffies.h>
  72. #include <linux/workqueue.h>
  73. #include <acpi/acpi_drivers.h>
  74. #include <linux/pci_ids.h>
  75. /* ThinkPad CMOS commands */
  76. #define TP_CMOS_VOLUME_DOWN 0
  77. #define TP_CMOS_VOLUME_UP 1
  78. #define TP_CMOS_VOLUME_MUTE 2
  79. #define TP_CMOS_BRIGHTNESS_UP 4
  80. #define TP_CMOS_BRIGHTNESS_DOWN 5
  81. #define TP_CMOS_THINKLIGHT_ON 12
  82. #define TP_CMOS_THINKLIGHT_OFF 13
  83. /* NVRAM Addresses */
  84. enum tp_nvram_addr {
  85. TP_NVRAM_ADDR_HK2 = 0x57,
  86. TP_NVRAM_ADDR_THINKLIGHT = 0x58,
  87. TP_NVRAM_ADDR_VIDEO = 0x59,
  88. TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
  89. TP_NVRAM_ADDR_MIXER = 0x60,
  90. };
  91. /* NVRAM bit masks */
  92. enum {
  93. TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
  94. TP_NVRAM_MASK_HKT_ZOOM = 0x20,
  95. TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
  96. TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
  97. TP_NVRAM_MASK_THINKLIGHT = 0x10,
  98. TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
  99. TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
  100. TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
  101. TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
  102. TP_NVRAM_MASK_MUTE = 0x40,
  103. TP_NVRAM_MASK_HKT_VOLUME = 0x80,
  104. TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
  105. TP_NVRAM_POS_LEVEL_VOLUME = 0,
  106. };
  107. /* ACPI HIDs */
  108. #define TPACPI_ACPI_HKEY_HID "IBM0068"
  109. /* Input IDs */
  110. #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
  111. #define TPACPI_HKEY_INPUT_VERSION 0x4101
  112. /* ACPI \WGSV commands */
  113. enum {
  114. TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
  115. TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
  116. TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
  117. TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
  118. };
  119. /* TP_ACPI_WGSV_GET_STATE bits */
  120. enum {
  121. TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
  122. TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
  123. TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
  124. TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
  125. TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
  126. TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
  127. TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
  128. TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
  129. TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
  130. TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
  131. };
  132. /****************************************************************************
  133. * Main driver
  134. */
  135. #define TPACPI_NAME "thinkpad"
  136. #define TPACPI_DESC "ThinkPad ACPI Extras"
  137. #define TPACPI_FILE TPACPI_NAME "_acpi"
  138. #define TPACPI_URL "http://ibm-acpi.sf.net/"
  139. #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
  140. #define TPACPI_PROC_DIR "ibm"
  141. #define TPACPI_ACPI_EVENT_PREFIX "ibm"
  142. #define TPACPI_DRVR_NAME TPACPI_FILE
  143. #define TPACPI_DRVR_SHORTNAME "tpacpi"
  144. #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
  145. #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
  146. #define TPACPI_WORKQUEUE_NAME "ktpacpid"
  147. #define TPACPI_MAX_ACPI_ARGS 3
  148. /* printk headers */
  149. #define TPACPI_LOG TPACPI_FILE ": "
  150. #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
  151. #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
  152. #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
  153. #define TPACPI_ERR KERN_ERR TPACPI_LOG
  154. #define TPACPI_WARN KERN_WARNING TPACPI_LOG
  155. #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
  156. #define TPACPI_INFO KERN_INFO TPACPI_LOG
  157. #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
  158. /* Debugging printk groups */
  159. #define TPACPI_DBG_ALL 0xffff
  160. #define TPACPI_DBG_DISCLOSETASK 0x8000
  161. #define TPACPI_DBG_INIT 0x0001
  162. #define TPACPI_DBG_EXIT 0x0002
  163. #define TPACPI_DBG_RFKILL 0x0004
  164. #define TPACPI_DBG_HKEY 0x0008
  165. #define TPACPI_DBG_FAN 0x0010
  166. #define TPACPI_DBG_BRGHT 0x0020
  167. #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
  168. #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  169. #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
  170. /****************************************************************************
  171. * Driver-wide structs and misc. variables
  172. */
  173. struct ibm_struct;
  174. struct tp_acpi_drv_struct {
  175. const struct acpi_device_id *hid;
  176. struct acpi_driver *driver;
  177. void (*notify) (struct ibm_struct *, u32);
  178. acpi_handle *handle;
  179. u32 type;
  180. struct acpi_device *device;
  181. };
  182. struct ibm_struct {
  183. char *name;
  184. int (*read) (char *);
  185. int (*write) (char *);
  186. void (*exit) (void);
  187. void (*resume) (void);
  188. void (*suspend) (pm_message_t state);
  189. void (*shutdown) (void);
  190. struct list_head all_drivers;
  191. struct tp_acpi_drv_struct *acpi;
  192. struct {
  193. u8 acpi_driver_registered:1;
  194. u8 acpi_notify_installed:1;
  195. u8 proc_created:1;
  196. u8 init_called:1;
  197. u8 experimental:1;
  198. } flags;
  199. };
  200. struct ibm_init_struct {
  201. char param[32];
  202. int (*init) (struct ibm_init_struct *);
  203. struct ibm_struct *data;
  204. };
  205. static struct {
  206. u32 bluetooth:1;
  207. u32 hotkey:1;
  208. u32 hotkey_mask:1;
  209. u32 hotkey_wlsw:1;
  210. u32 hotkey_tablet:1;
  211. u32 light:1;
  212. u32 light_status:1;
  213. u32 bright_16levels:1;
  214. u32 bright_acpimode:1;
  215. u32 wan:1;
  216. u32 uwb:1;
  217. u32 fan_ctrl_status_undef:1;
  218. u32 second_fan:1;
  219. u32 beep_needs_two_args:1;
  220. u32 input_device_registered:1;
  221. u32 platform_drv_registered:1;
  222. u32 platform_drv_attrs_registered:1;
  223. u32 sensors_pdrv_registered:1;
  224. u32 sensors_pdrv_attrs_registered:1;
  225. u32 sensors_pdev_attrs_registered:1;
  226. u32 hotkey_poll_active:1;
  227. } tp_features;
  228. static struct {
  229. u16 hotkey_mask_ff:1;
  230. } tp_warned;
  231. struct thinkpad_id_data {
  232. unsigned int vendor; /* ThinkPad vendor:
  233. * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
  234. char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
  235. char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
  236. u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
  237. u16 ec_model;
  238. u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
  239. u16 ec_release;
  240. char *model_str; /* ThinkPad T43 */
  241. char *nummodel_str; /* 9384A9C for a 9384-A9C model */
  242. };
  243. static struct thinkpad_id_data thinkpad_id;
  244. static enum {
  245. TPACPI_LIFE_INIT = 0,
  246. TPACPI_LIFE_RUNNING,
  247. TPACPI_LIFE_EXITING,
  248. } tpacpi_lifecycle;
  249. static int experimental;
  250. static u32 dbg_level;
  251. static struct workqueue_struct *tpacpi_wq;
  252. enum led_status_t {
  253. TPACPI_LED_OFF = 0,
  254. TPACPI_LED_ON,
  255. TPACPI_LED_BLINK,
  256. };
  257. /* Special LED class that can defer work */
  258. struct tpacpi_led_classdev {
  259. struct led_classdev led_classdev;
  260. struct work_struct work;
  261. enum led_status_t new_state;
  262. unsigned int led;
  263. };
  264. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  265. static int dbg_wlswemul;
  266. static int tpacpi_wlsw_emulstate;
  267. static int dbg_bluetoothemul;
  268. static int tpacpi_bluetooth_emulstate;
  269. static int dbg_wwanemul;
  270. static int tpacpi_wwan_emulstate;
  271. static int dbg_uwbemul;
  272. static int tpacpi_uwb_emulstate;
  273. #endif
  274. /*************************************************************************
  275. * Debugging helpers
  276. */
  277. #define dbg_printk(a_dbg_level, format, arg...) \
  278. do { if (dbg_level & (a_dbg_level)) \
  279. printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
  280. } while (0)
  281. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  282. #define vdbg_printk dbg_printk
  283. static const char *str_supported(int is_supported);
  284. #else
  285. #define vdbg_printk(a_dbg_level, format, arg...) \
  286. do { } while (0)
  287. #endif
  288. static void tpacpi_log_usertask(const char * const what)
  289. {
  290. printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
  291. what, task_tgid_vnr(current));
  292. }
  293. #define tpacpi_disclose_usertask(what, format, arg...) \
  294. do { \
  295. if (unlikely( \
  296. (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
  297. (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
  298. printk(TPACPI_DEBUG "%s: PID %d: " format, \
  299. what, task_tgid_vnr(current), ## arg); \
  300. } \
  301. } while (0)
  302. /*
  303. * Quirk handling helpers
  304. *
  305. * ThinkPad IDs and versions seen in the field so far
  306. * are two-characters from the set [0-9A-Z], i.e. base 36.
  307. *
  308. * We use values well outside that range as specials.
  309. */
  310. #define TPACPI_MATCH_ANY 0xffffU
  311. #define TPACPI_MATCH_UNKNOWN 0U
  312. /* TPID('1', 'Y') == 0x5931 */
  313. #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
  314. #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
  315. { .vendor = PCI_VENDOR_ID_IBM, \
  316. .bios = TPID(__id1, __id2), \
  317. .ec = TPACPI_MATCH_ANY, \
  318. .quirks = (__quirk) }
  319. #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
  320. { .vendor = PCI_VENDOR_ID_LENOVO, \
  321. .bios = TPID(__id1, __id2), \
  322. .ec = TPACPI_MATCH_ANY, \
  323. .quirks = (__quirk) }
  324. struct tpacpi_quirk {
  325. unsigned int vendor;
  326. u16 bios;
  327. u16 ec;
  328. unsigned long quirks;
  329. };
  330. /**
  331. * tpacpi_check_quirks() - search BIOS/EC version on a list
  332. * @qlist: array of &struct tpacpi_quirk
  333. * @qlist_size: number of elements in @qlist
  334. *
  335. * Iterates over a quirks list until one is found that matches the
  336. * ThinkPad's vendor, BIOS and EC model.
  337. *
  338. * Returns 0 if nothing matches, otherwise returns the quirks field of
  339. * the matching &struct tpacpi_quirk entry.
  340. *
  341. * The match criteria is: vendor, ec and bios much match.
  342. */
  343. static unsigned long __init tpacpi_check_quirks(
  344. const struct tpacpi_quirk *qlist,
  345. unsigned int qlist_size)
  346. {
  347. while (qlist_size) {
  348. if ((qlist->vendor == thinkpad_id.vendor ||
  349. qlist->vendor == TPACPI_MATCH_ANY) &&
  350. (qlist->bios == thinkpad_id.bios_model ||
  351. qlist->bios == TPACPI_MATCH_ANY) &&
  352. (qlist->ec == thinkpad_id.ec_model ||
  353. qlist->ec == TPACPI_MATCH_ANY))
  354. return qlist->quirks;
  355. qlist_size--;
  356. qlist++;
  357. }
  358. return 0;
  359. }
  360. /****************************************************************************
  361. ****************************************************************************
  362. *
  363. * ACPI Helpers and device model
  364. *
  365. ****************************************************************************
  366. ****************************************************************************/
  367. /*************************************************************************
  368. * ACPI basic handles
  369. */
  370. static acpi_handle root_handle;
  371. #define TPACPI_HANDLE(object, parent, paths...) \
  372. static acpi_handle object##_handle; \
  373. static acpi_handle *object##_parent = &parent##_handle; \
  374. static char *object##_path; \
  375. static char *object##_paths[] = { paths }
  376. TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  377. "\\_SB.PCI.ISA.EC", /* 570 */
  378. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  379. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  380. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  381. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  382. "\\_SB.PCI0.LPC.EC", /* all others */
  383. );
  384. TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  385. TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  386. TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
  387. /* T4x, X31, X40 */
  388. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  389. "\\CMS", /* R40, R40e */
  390. ); /* all others */
  391. TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  392. "^HKEY", /* R30, R31 */
  393. "HKEY", /* all others */
  394. ); /* 570 */
  395. TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  396. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  397. "\\_SB.PCI0.VID0", /* 770e */
  398. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  399. "\\_SB.PCI0.AGP.VID", /* all others */
  400. ); /* R30, R31 */
  401. /*************************************************************************
  402. * ACPI helpers
  403. */
  404. static int acpi_evalf(acpi_handle handle,
  405. void *res, char *method, char *fmt, ...)
  406. {
  407. char *fmt0 = fmt;
  408. struct acpi_object_list params;
  409. union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
  410. struct acpi_buffer result, *resultp;
  411. union acpi_object out_obj;
  412. acpi_status status;
  413. va_list ap;
  414. char res_type;
  415. int success;
  416. int quiet;
  417. if (!*fmt) {
  418. printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
  419. return 0;
  420. }
  421. if (*fmt == 'q') {
  422. quiet = 1;
  423. fmt++;
  424. } else
  425. quiet = 0;
  426. res_type = *(fmt++);
  427. params.count = 0;
  428. params.pointer = &in_objs[0];
  429. va_start(ap, fmt);
  430. while (*fmt) {
  431. char c = *(fmt++);
  432. switch (c) {
  433. case 'd': /* int */
  434. in_objs[params.count].integer.value = va_arg(ap, int);
  435. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  436. break;
  437. /* add more types as needed */
  438. default:
  439. printk(TPACPI_ERR "acpi_evalf() called "
  440. "with invalid format character '%c'\n", c);
  441. return 0;
  442. }
  443. }
  444. va_end(ap);
  445. if (res_type != 'v') {
  446. result.length = sizeof(out_obj);
  447. result.pointer = &out_obj;
  448. resultp = &result;
  449. } else
  450. resultp = NULL;
  451. status = acpi_evaluate_object(handle, method, &params, resultp);
  452. switch (res_type) {
  453. case 'd': /* int */
  454. if (res)
  455. *(int *)res = out_obj.integer.value;
  456. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  457. break;
  458. case 'v': /* void */
  459. success = status == AE_OK;
  460. break;
  461. /* add more types as needed */
  462. default:
  463. printk(TPACPI_ERR "acpi_evalf() called "
  464. "with invalid format character '%c'\n", res_type);
  465. return 0;
  466. }
  467. if (!success && !quiet)
  468. printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  469. method, fmt0, status);
  470. return success;
  471. }
  472. static int acpi_ec_read(int i, u8 *p)
  473. {
  474. int v;
  475. if (ecrd_handle) {
  476. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  477. return 0;
  478. *p = v;
  479. } else {
  480. if (ec_read(i, p) < 0)
  481. return 0;
  482. }
  483. return 1;
  484. }
  485. static int acpi_ec_write(int i, u8 v)
  486. {
  487. if (ecwr_handle) {
  488. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  489. return 0;
  490. } else {
  491. if (ec_write(i, v) < 0)
  492. return 0;
  493. }
  494. return 1;
  495. }
  496. static int issue_thinkpad_cmos_command(int cmos_cmd)
  497. {
  498. if (!cmos_handle)
  499. return -ENXIO;
  500. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  501. return -EIO;
  502. return 0;
  503. }
  504. /*************************************************************************
  505. * ACPI device model
  506. */
  507. #define TPACPI_ACPIHANDLE_INIT(object) \
  508. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  509. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  510. static void drv_acpi_handle_init(char *name,
  511. acpi_handle *handle, acpi_handle parent,
  512. char **paths, int num_paths, char **path)
  513. {
  514. int i;
  515. acpi_status status;
  516. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  517. name);
  518. for (i = 0; i < num_paths; i++) {
  519. status = acpi_get_handle(parent, paths[i], handle);
  520. if (ACPI_SUCCESS(status)) {
  521. *path = paths[i];
  522. dbg_printk(TPACPI_DBG_INIT,
  523. "Found ACPI handle %s for %s\n",
  524. *path, name);
  525. return;
  526. }
  527. }
  528. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  529. name);
  530. *handle = NULL;
  531. }
  532. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  533. {
  534. struct ibm_struct *ibm = data;
  535. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  536. return;
  537. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  538. return;
  539. ibm->acpi->notify(ibm, event);
  540. }
  541. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  542. {
  543. acpi_status status;
  544. int rc;
  545. BUG_ON(!ibm->acpi);
  546. if (!*ibm->acpi->handle)
  547. return 0;
  548. vdbg_printk(TPACPI_DBG_INIT,
  549. "setting up ACPI notify for %s\n", ibm->name);
  550. rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  551. if (rc < 0) {
  552. printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
  553. ibm->name, rc);
  554. return -ENODEV;
  555. }
  556. ibm->acpi->device->driver_data = ibm;
  557. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  558. TPACPI_ACPI_EVENT_PREFIX,
  559. ibm->name);
  560. status = acpi_install_notify_handler(*ibm->acpi->handle,
  561. ibm->acpi->type, dispatch_acpi_notify, ibm);
  562. if (ACPI_FAILURE(status)) {
  563. if (status == AE_ALREADY_EXISTS) {
  564. printk(TPACPI_NOTICE
  565. "another device driver is already "
  566. "handling %s events\n", ibm->name);
  567. } else {
  568. printk(TPACPI_ERR
  569. "acpi_install_notify_handler(%s) failed: %d\n",
  570. ibm->name, status);
  571. }
  572. return -ENODEV;
  573. }
  574. ibm->flags.acpi_notify_installed = 1;
  575. return 0;
  576. }
  577. static int __init tpacpi_device_add(struct acpi_device *device)
  578. {
  579. return 0;
  580. }
  581. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  582. {
  583. int rc;
  584. dbg_printk(TPACPI_DBG_INIT,
  585. "registering %s as an ACPI driver\n", ibm->name);
  586. BUG_ON(!ibm->acpi);
  587. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  588. if (!ibm->acpi->driver) {
  589. printk(TPACPI_ERR
  590. "failed to allocate memory for ibm->acpi->driver\n");
  591. return -ENOMEM;
  592. }
  593. sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
  594. ibm->acpi->driver->ids = ibm->acpi->hid;
  595. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  596. rc = acpi_bus_register_driver(ibm->acpi->driver);
  597. if (rc < 0) {
  598. printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  599. ibm->name, rc);
  600. kfree(ibm->acpi->driver);
  601. ibm->acpi->driver = NULL;
  602. } else if (!rc)
  603. ibm->flags.acpi_driver_registered = 1;
  604. return rc;
  605. }
  606. /****************************************************************************
  607. ****************************************************************************
  608. *
  609. * Procfs Helpers
  610. *
  611. ****************************************************************************
  612. ****************************************************************************/
  613. static int dispatch_procfs_read(char *page, char **start, off_t off,
  614. int count, int *eof, void *data)
  615. {
  616. struct ibm_struct *ibm = data;
  617. int len;
  618. if (!ibm || !ibm->read)
  619. return -EINVAL;
  620. len = ibm->read(page);
  621. if (len < 0)
  622. return len;
  623. if (len <= off + count)
  624. *eof = 1;
  625. *start = page + off;
  626. len -= off;
  627. if (len > count)
  628. len = count;
  629. if (len < 0)
  630. len = 0;
  631. return len;
  632. }
  633. static int dispatch_procfs_write(struct file *file,
  634. const char __user *userbuf,
  635. unsigned long count, void *data)
  636. {
  637. struct ibm_struct *ibm = data;
  638. char *kernbuf;
  639. int ret;
  640. if (!ibm || !ibm->write)
  641. return -EINVAL;
  642. if (count > PAGE_SIZE - 2)
  643. return -EINVAL;
  644. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  645. if (!kernbuf)
  646. return -ENOMEM;
  647. if (copy_from_user(kernbuf, userbuf, count)) {
  648. kfree(kernbuf);
  649. return -EFAULT;
  650. }
  651. kernbuf[count] = 0;
  652. strcat(kernbuf, ",");
  653. ret = ibm->write(kernbuf);
  654. if (ret == 0)
  655. ret = count;
  656. kfree(kernbuf);
  657. return ret;
  658. }
  659. static char *next_cmd(char **cmds)
  660. {
  661. char *start = *cmds;
  662. char *end;
  663. while ((end = strchr(start, ',')) && end == start)
  664. start = end + 1;
  665. if (!end)
  666. return NULL;
  667. *end = 0;
  668. *cmds = end + 1;
  669. return start;
  670. }
  671. /****************************************************************************
  672. ****************************************************************************
  673. *
  674. * Device model: input, hwmon and platform
  675. *
  676. ****************************************************************************
  677. ****************************************************************************/
  678. static struct platform_device *tpacpi_pdev;
  679. static struct platform_device *tpacpi_sensors_pdev;
  680. static struct device *tpacpi_hwmon;
  681. static struct input_dev *tpacpi_inputdev;
  682. static struct mutex tpacpi_inputdev_send_mutex;
  683. static LIST_HEAD(tpacpi_all_drivers);
  684. static int tpacpi_suspend_handler(struct platform_device *pdev,
  685. pm_message_t state)
  686. {
  687. struct ibm_struct *ibm, *itmp;
  688. list_for_each_entry_safe(ibm, itmp,
  689. &tpacpi_all_drivers,
  690. all_drivers) {
  691. if (ibm->suspend)
  692. (ibm->suspend)(state);
  693. }
  694. return 0;
  695. }
  696. static int tpacpi_resume_handler(struct platform_device *pdev)
  697. {
  698. struct ibm_struct *ibm, *itmp;
  699. list_for_each_entry_safe(ibm, itmp,
  700. &tpacpi_all_drivers,
  701. all_drivers) {
  702. if (ibm->resume)
  703. (ibm->resume)();
  704. }
  705. return 0;
  706. }
  707. static void tpacpi_shutdown_handler(struct platform_device *pdev)
  708. {
  709. struct ibm_struct *ibm, *itmp;
  710. list_for_each_entry_safe(ibm, itmp,
  711. &tpacpi_all_drivers,
  712. all_drivers) {
  713. if (ibm->shutdown)
  714. (ibm->shutdown)();
  715. }
  716. }
  717. static struct platform_driver tpacpi_pdriver = {
  718. .driver = {
  719. .name = TPACPI_DRVR_NAME,
  720. .owner = THIS_MODULE,
  721. },
  722. .suspend = tpacpi_suspend_handler,
  723. .resume = tpacpi_resume_handler,
  724. .shutdown = tpacpi_shutdown_handler,
  725. };
  726. static struct platform_driver tpacpi_hwmon_pdriver = {
  727. .driver = {
  728. .name = TPACPI_HWMON_DRVR_NAME,
  729. .owner = THIS_MODULE,
  730. },
  731. };
  732. /*************************************************************************
  733. * sysfs support helpers
  734. */
  735. struct attribute_set {
  736. unsigned int members, max_members;
  737. struct attribute_group group;
  738. };
  739. struct attribute_set_obj {
  740. struct attribute_set s;
  741. struct attribute *a;
  742. } __attribute__((packed));
  743. static struct attribute_set *create_attr_set(unsigned int max_members,
  744. const char *name)
  745. {
  746. struct attribute_set_obj *sobj;
  747. if (max_members == 0)
  748. return NULL;
  749. /* Allocates space for implicit NULL at the end too */
  750. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  751. max_members * sizeof(struct attribute *),
  752. GFP_KERNEL);
  753. if (!sobj)
  754. return NULL;
  755. sobj->s.max_members = max_members;
  756. sobj->s.group.attrs = &sobj->a;
  757. sobj->s.group.name = name;
  758. return &sobj->s;
  759. }
  760. #define destroy_attr_set(_set) \
  761. kfree(_set);
  762. /* not multi-threaded safe, use it in a single thread per set */
  763. static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
  764. {
  765. if (!s || !attr)
  766. return -EINVAL;
  767. if (s->members >= s->max_members)
  768. return -ENOMEM;
  769. s->group.attrs[s->members] = attr;
  770. s->members++;
  771. return 0;
  772. }
  773. static int add_many_to_attr_set(struct attribute_set *s,
  774. struct attribute **attr,
  775. unsigned int count)
  776. {
  777. int i, res;
  778. for (i = 0; i < count; i++) {
  779. res = add_to_attr_set(s, attr[i]);
  780. if (res)
  781. return res;
  782. }
  783. return 0;
  784. }
  785. static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
  786. {
  787. sysfs_remove_group(kobj, &s->group);
  788. destroy_attr_set(s);
  789. }
  790. #define register_attr_set_with_sysfs(_attr_set, _kobj) \
  791. sysfs_create_group(_kobj, &_attr_set->group)
  792. static int parse_strtoul(const char *buf,
  793. unsigned long max, unsigned long *value)
  794. {
  795. char *endp;
  796. while (*buf && isspace(*buf))
  797. buf++;
  798. *value = simple_strtoul(buf, &endp, 0);
  799. while (*endp && isspace(*endp))
  800. endp++;
  801. if (*endp || *value > max)
  802. return -EINVAL;
  803. return 0;
  804. }
  805. static void tpacpi_disable_brightness_delay(void)
  806. {
  807. if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
  808. printk(TPACPI_NOTICE
  809. "ACPI backlight control delay disabled\n");
  810. }
  811. static int __init tpacpi_query_bcl_levels(acpi_handle handle)
  812. {
  813. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  814. union acpi_object *obj;
  815. int rc;
  816. if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
  817. obj = (union acpi_object *)buffer.pointer;
  818. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  819. printk(TPACPI_ERR "Unknown _BCL data, "
  820. "please report this to %s\n", TPACPI_MAIL);
  821. rc = 0;
  822. } else {
  823. rc = obj->package.count;
  824. }
  825. } else {
  826. return 0;
  827. }
  828. kfree(buffer.pointer);
  829. return rc;
  830. }
  831. static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
  832. u32 lvl, void *context, void **rv)
  833. {
  834. char name[ACPI_PATH_SEGMENT_LENGTH];
  835. struct acpi_buffer buffer = { sizeof(name), &name };
  836. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  837. !strncmp("_BCL", name, sizeof(name) - 1)) {
  838. BUG_ON(!rv || !*rv);
  839. **(int **)rv = tpacpi_query_bcl_levels(handle);
  840. return AE_CTRL_TERMINATE;
  841. } else {
  842. return AE_OK;
  843. }
  844. }
  845. /*
  846. * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
  847. */
  848. static int __init tpacpi_check_std_acpi_brightness_support(void)
  849. {
  850. int status;
  851. int bcl_levels = 0;
  852. void *bcl_ptr = &bcl_levels;
  853. if (!vid_handle) {
  854. TPACPI_ACPIHANDLE_INIT(vid);
  855. }
  856. if (!vid_handle)
  857. return 0;
  858. /*
  859. * Search for a _BCL method, and execute it. This is safe on all
  860. * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
  861. * BIOS in ACPI backlight control mode. We do NOT have to care
  862. * about calling the _BCL method in an enabled video device, any
  863. * will do for our purposes.
  864. */
  865. status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
  866. tpacpi_acpi_walk_find_bcl, NULL,
  867. &bcl_ptr);
  868. if (ACPI_SUCCESS(status) && bcl_levels > 2) {
  869. tp_features.bright_acpimode = 1;
  870. return (bcl_levels - 2);
  871. }
  872. return 0;
  873. }
  874. static void printk_deprecated_attribute(const char * const what,
  875. const char * const details)
  876. {
  877. tpacpi_log_usertask("deprecated sysfs attribute");
  878. printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
  879. "will be removed. %s\n",
  880. what, details);
  881. }
  882. /*************************************************************************
  883. * rfkill and radio control support helpers
  884. */
  885. /*
  886. * ThinkPad-ACPI firmware handling model:
  887. *
  888. * WLSW (master wireless switch) is event-driven, and is common to all
  889. * firmware-controlled radios. It cannot be controlled, just monitored,
  890. * as expected. It overrides all radio state in firmware
  891. *
  892. * The kernel, a masked-off hotkey, and WLSW can change the radio state
  893. * (TODO: verify how WLSW interacts with the returned radio state).
  894. *
  895. * The only time there are shadow radio state changes, is when
  896. * masked-off hotkeys are used.
  897. */
  898. /*
  899. * Internal driver API for radio state:
  900. *
  901. * int: < 0 = error, otherwise enum tpacpi_rfkill_state
  902. * bool: true means radio blocked (off)
  903. */
  904. enum tpacpi_rfkill_state {
  905. TPACPI_RFK_RADIO_OFF = 0,
  906. TPACPI_RFK_RADIO_ON
  907. };
  908. /* rfkill switches */
  909. enum tpacpi_rfk_id {
  910. TPACPI_RFK_BLUETOOTH_SW_ID = 0,
  911. TPACPI_RFK_WWAN_SW_ID,
  912. TPACPI_RFK_UWB_SW_ID,
  913. TPACPI_RFK_SW_MAX
  914. };
  915. static const char *tpacpi_rfkill_names[] = {
  916. [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
  917. [TPACPI_RFK_WWAN_SW_ID] = "wwan",
  918. [TPACPI_RFK_UWB_SW_ID] = "uwb",
  919. [TPACPI_RFK_SW_MAX] = NULL
  920. };
  921. /* ThinkPad-ACPI rfkill subdriver */
  922. struct tpacpi_rfk {
  923. struct rfkill *rfkill;
  924. enum tpacpi_rfk_id id;
  925. const struct tpacpi_rfk_ops *ops;
  926. };
  927. struct tpacpi_rfk_ops {
  928. /* firmware interface */
  929. int (*get_status)(void);
  930. int (*set_status)(const enum tpacpi_rfkill_state);
  931. };
  932. static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
  933. /* Query FW and update rfkill sw state for a given rfkill switch */
  934. static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
  935. {
  936. int status;
  937. if (!tp_rfk)
  938. return -ENODEV;
  939. status = (tp_rfk->ops->get_status)();
  940. if (status < 0)
  941. return status;
  942. rfkill_set_sw_state(tp_rfk->rfkill,
  943. (status == TPACPI_RFK_RADIO_OFF));
  944. return status;
  945. }
  946. /* Query FW and update rfkill sw state for all rfkill switches */
  947. static void tpacpi_rfk_update_swstate_all(void)
  948. {
  949. unsigned int i;
  950. for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
  951. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
  952. }
  953. /*
  954. * Sync the HW-blocking state of all rfkill switches,
  955. * do notice it causes the rfkill core to schedule uevents
  956. */
  957. static void tpacpi_rfk_update_hwblock_state(bool blocked)
  958. {
  959. unsigned int i;
  960. struct tpacpi_rfk *tp_rfk;
  961. for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
  962. tp_rfk = tpacpi_rfkill_switches[i];
  963. if (tp_rfk) {
  964. if (rfkill_set_hw_state(tp_rfk->rfkill,
  965. blocked)) {
  966. /* ignore -- we track sw block */
  967. }
  968. }
  969. }
  970. }
  971. /* Call to get the WLSW state from the firmware */
  972. static int hotkey_get_wlsw(void);
  973. /* Call to query WLSW state and update all rfkill switches */
  974. static bool tpacpi_rfk_check_hwblock_state(void)
  975. {
  976. int res = hotkey_get_wlsw();
  977. int hw_blocked;
  978. /* When unknown or unsupported, we have to assume it is unblocked */
  979. if (res < 0)
  980. return false;
  981. hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
  982. tpacpi_rfk_update_hwblock_state(hw_blocked);
  983. return hw_blocked;
  984. }
  985. static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
  986. {
  987. struct tpacpi_rfk *tp_rfk = data;
  988. int res;
  989. dbg_printk(TPACPI_DBG_RFKILL,
  990. "request to change radio state to %s\n",
  991. blocked ? "blocked" : "unblocked");
  992. /* try to set radio state */
  993. res = (tp_rfk->ops->set_status)(blocked ?
  994. TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
  995. /* and update the rfkill core with whatever the FW really did */
  996. tpacpi_rfk_update_swstate(tp_rfk);
  997. return (res < 0) ? res : 0;
  998. }
  999. static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
  1000. .set_block = tpacpi_rfk_hook_set_block,
  1001. };
  1002. static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
  1003. const struct tpacpi_rfk_ops *tp_rfkops,
  1004. const enum rfkill_type rfktype,
  1005. const char *name,
  1006. const bool set_default)
  1007. {
  1008. struct tpacpi_rfk *atp_rfk;
  1009. int res;
  1010. bool sw_state = false;
  1011. int sw_status;
  1012. BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
  1013. atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
  1014. if (atp_rfk)
  1015. atp_rfk->rfkill = rfkill_alloc(name,
  1016. &tpacpi_pdev->dev,
  1017. rfktype,
  1018. &tpacpi_rfk_rfkill_ops,
  1019. atp_rfk);
  1020. if (!atp_rfk || !atp_rfk->rfkill) {
  1021. printk(TPACPI_ERR
  1022. "failed to allocate memory for rfkill class\n");
  1023. kfree(atp_rfk);
  1024. return -ENOMEM;
  1025. }
  1026. atp_rfk->id = id;
  1027. atp_rfk->ops = tp_rfkops;
  1028. sw_status = (tp_rfkops->get_status)();
  1029. if (sw_status < 0) {
  1030. printk(TPACPI_ERR
  1031. "failed to read initial state for %s, error %d\n",
  1032. name, sw_status);
  1033. } else {
  1034. sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
  1035. if (set_default) {
  1036. /* try to keep the initial state, since we ask the
  1037. * firmware to preserve it across S5 in NVRAM */
  1038. rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
  1039. }
  1040. }
  1041. rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
  1042. res = rfkill_register(atp_rfk->rfkill);
  1043. if (res < 0) {
  1044. printk(TPACPI_ERR
  1045. "failed to register %s rfkill switch: %d\n",
  1046. name, res);
  1047. rfkill_destroy(atp_rfk->rfkill);
  1048. kfree(atp_rfk);
  1049. return res;
  1050. }
  1051. tpacpi_rfkill_switches[id] = atp_rfk;
  1052. return 0;
  1053. }
  1054. static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
  1055. {
  1056. struct tpacpi_rfk *tp_rfk;
  1057. BUG_ON(id >= TPACPI_RFK_SW_MAX);
  1058. tp_rfk = tpacpi_rfkill_switches[id];
  1059. if (tp_rfk) {
  1060. rfkill_unregister(tp_rfk->rfkill);
  1061. tpacpi_rfkill_switches[id] = NULL;
  1062. kfree(tp_rfk);
  1063. }
  1064. }
  1065. static void printk_deprecated_rfkill_attribute(const char * const what)
  1066. {
  1067. printk_deprecated_attribute(what,
  1068. "Please switch to generic rfkill before year 2010");
  1069. }
  1070. /* sysfs <radio> enable ------------------------------------------------ */
  1071. static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
  1072. struct device_attribute *attr,
  1073. char *buf)
  1074. {
  1075. int status;
  1076. printk_deprecated_rfkill_attribute(attr->attr.name);
  1077. /* This is in the ABI... */
  1078. if (tpacpi_rfk_check_hwblock_state()) {
  1079. status = TPACPI_RFK_RADIO_OFF;
  1080. } else {
  1081. status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1082. if (status < 0)
  1083. return status;
  1084. }
  1085. return snprintf(buf, PAGE_SIZE, "%d\n",
  1086. (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
  1087. }
  1088. static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
  1089. struct device_attribute *attr,
  1090. const char *buf, size_t count)
  1091. {
  1092. unsigned long t;
  1093. int res;
  1094. printk_deprecated_rfkill_attribute(attr->attr.name);
  1095. if (parse_strtoul(buf, 1, &t))
  1096. return -EINVAL;
  1097. tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
  1098. /* This is in the ABI... */
  1099. if (tpacpi_rfk_check_hwblock_state() && !!t)
  1100. return -EPERM;
  1101. res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
  1102. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
  1103. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1104. return (res < 0) ? res : count;
  1105. }
  1106. /* procfs -------------------------------------------------------------- */
  1107. static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
  1108. {
  1109. int len = 0;
  1110. if (id >= TPACPI_RFK_SW_MAX)
  1111. len += sprintf(p + len, "status:\t\tnot supported\n");
  1112. else {
  1113. int status;
  1114. /* This is in the ABI... */
  1115. if (tpacpi_rfk_check_hwblock_state()) {
  1116. status = TPACPI_RFK_RADIO_OFF;
  1117. } else {
  1118. status = tpacpi_rfk_update_swstate(
  1119. tpacpi_rfkill_switches[id]);
  1120. if (status < 0)
  1121. return status;
  1122. }
  1123. len += sprintf(p + len, "status:\t\t%s\n",
  1124. (status == TPACPI_RFK_RADIO_ON) ?
  1125. "enabled" : "disabled");
  1126. len += sprintf(p + len, "commands:\tenable, disable\n");
  1127. }
  1128. return len;
  1129. }
  1130. static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
  1131. {
  1132. char *cmd;
  1133. int status = -1;
  1134. int res = 0;
  1135. if (id >= TPACPI_RFK_SW_MAX)
  1136. return -ENODEV;
  1137. while ((cmd = next_cmd(&buf))) {
  1138. if (strlencmp(cmd, "enable") == 0)
  1139. status = TPACPI_RFK_RADIO_ON;
  1140. else if (strlencmp(cmd, "disable") == 0)
  1141. status = TPACPI_RFK_RADIO_OFF;
  1142. else
  1143. return -EINVAL;
  1144. }
  1145. if (status != -1) {
  1146. tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
  1147. (status == TPACPI_RFK_RADIO_ON) ?
  1148. "enable" : "disable",
  1149. tpacpi_rfkill_names[id]);
  1150. res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
  1151. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1152. }
  1153. return res;
  1154. }
  1155. /*************************************************************************
  1156. * thinkpad-acpi driver attributes
  1157. */
  1158. /* interface_version --------------------------------------------------- */
  1159. static ssize_t tpacpi_driver_interface_version_show(
  1160. struct device_driver *drv,
  1161. char *buf)
  1162. {
  1163. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  1164. }
  1165. static DRIVER_ATTR(interface_version, S_IRUGO,
  1166. tpacpi_driver_interface_version_show, NULL);
  1167. /* debug_level --------------------------------------------------------- */
  1168. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  1169. char *buf)
  1170. {
  1171. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  1172. }
  1173. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  1174. const char *buf, size_t count)
  1175. {
  1176. unsigned long t;
  1177. if (parse_strtoul(buf, 0xffff, &t))
  1178. return -EINVAL;
  1179. dbg_level = t;
  1180. return count;
  1181. }
  1182. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  1183. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  1184. /* version ------------------------------------------------------------- */
  1185. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  1186. char *buf)
  1187. {
  1188. return snprintf(buf, PAGE_SIZE, "%s v%s\n",
  1189. TPACPI_DESC, TPACPI_VERSION);
  1190. }
  1191. static DRIVER_ATTR(version, S_IRUGO,
  1192. tpacpi_driver_version_show, NULL);
  1193. /* --------------------------------------------------------------------- */
  1194. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1195. /* wlsw_emulstate ------------------------------------------------------ */
  1196. static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
  1197. char *buf)
  1198. {
  1199. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
  1200. }
  1201. static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
  1202. const char *buf, size_t count)
  1203. {
  1204. unsigned long t;
  1205. if (parse_strtoul(buf, 1, &t))
  1206. return -EINVAL;
  1207. if (tpacpi_wlsw_emulstate != !!t) {
  1208. tpacpi_wlsw_emulstate = !!t;
  1209. tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
  1210. }
  1211. return count;
  1212. }
  1213. static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
  1214. tpacpi_driver_wlsw_emulstate_show,
  1215. tpacpi_driver_wlsw_emulstate_store);
  1216. /* bluetooth_emulstate ------------------------------------------------- */
  1217. static ssize_t tpacpi_driver_bluetooth_emulstate_show(
  1218. struct device_driver *drv,
  1219. char *buf)
  1220. {
  1221. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
  1222. }
  1223. static ssize_t tpacpi_driver_bluetooth_emulstate_store(
  1224. struct device_driver *drv,
  1225. const char *buf, size_t count)
  1226. {
  1227. unsigned long t;
  1228. if (parse_strtoul(buf, 1, &t))
  1229. return -EINVAL;
  1230. tpacpi_bluetooth_emulstate = !!t;
  1231. return count;
  1232. }
  1233. static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
  1234. tpacpi_driver_bluetooth_emulstate_show,
  1235. tpacpi_driver_bluetooth_emulstate_store);
  1236. /* wwan_emulstate ------------------------------------------------- */
  1237. static ssize_t tpacpi_driver_wwan_emulstate_show(
  1238. struct device_driver *drv,
  1239. char *buf)
  1240. {
  1241. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
  1242. }
  1243. static ssize_t tpacpi_driver_wwan_emulstate_store(
  1244. struct device_driver *drv,
  1245. const char *buf, size_t count)
  1246. {
  1247. unsigned long t;
  1248. if (parse_strtoul(buf, 1, &t))
  1249. return -EINVAL;
  1250. tpacpi_wwan_emulstate = !!t;
  1251. return count;
  1252. }
  1253. static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
  1254. tpacpi_driver_wwan_emulstate_show,
  1255. tpacpi_driver_wwan_emulstate_store);
  1256. /* uwb_emulstate ------------------------------------------------- */
  1257. static ssize_t tpacpi_driver_uwb_emulstate_show(
  1258. struct device_driver *drv,
  1259. char *buf)
  1260. {
  1261. return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
  1262. }
  1263. static ssize_t tpacpi_driver_uwb_emulstate_store(
  1264. struct device_driver *drv,
  1265. const char *buf, size_t count)
  1266. {
  1267. unsigned long t;
  1268. if (parse_strtoul(buf, 1, &t))
  1269. return -EINVAL;
  1270. tpacpi_uwb_emulstate = !!t;
  1271. return count;
  1272. }
  1273. static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
  1274. tpacpi_driver_uwb_emulstate_show,
  1275. tpacpi_driver_uwb_emulstate_store);
  1276. #endif
  1277. /* --------------------------------------------------------------------- */
  1278. static struct driver_attribute *tpacpi_driver_attributes[] = {
  1279. &driver_attr_debug_level, &driver_attr_version,
  1280. &driver_attr_interface_version,
  1281. };
  1282. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  1283. {
  1284. int i, res;
  1285. i = 0;
  1286. res = 0;
  1287. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  1288. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  1289. i++;
  1290. }
  1291. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1292. if (!res && dbg_wlswemul)
  1293. res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
  1294. if (!res && dbg_bluetoothemul)
  1295. res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
  1296. if (!res && dbg_wwanemul)
  1297. res = driver_create_file(drv, &driver_attr_wwan_emulstate);
  1298. if (!res && dbg_uwbemul)
  1299. res = driver_create_file(drv, &driver_attr_uwb_emulstate);
  1300. #endif
  1301. return res;
  1302. }
  1303. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  1304. {
  1305. int i;
  1306. for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  1307. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  1308. #ifdef THINKPAD_ACPI_DEBUGFACILITIES
  1309. driver_remove_file(drv, &driver_attr_wlsw_emulstate);
  1310. driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
  1311. driver_remove_file(drv, &driver_attr_wwan_emulstate);
  1312. driver_remove_file(drv, &driver_attr_uwb_emulstate);
  1313. #endif
  1314. }
  1315. /*************************************************************************
  1316. * Firmware Data
  1317. */
  1318. /*
  1319. * Table of recommended minimum BIOS versions
  1320. *
  1321. * Reasons for listing:
  1322. * 1. Stable BIOS, listed because the unknown ammount of
  1323. * bugs and bad ACPI behaviour on older versions
  1324. *
  1325. * 2. BIOS or EC fw with known bugs that trigger on Linux
  1326. *
  1327. * 3. BIOS with known reduced functionality in older versions
  1328. *
  1329. * We recommend the latest BIOS and EC version.
  1330. * We only support the latest BIOS and EC fw version as a rule.
  1331. *
  1332. * Sources: IBM ThinkPad Public Web Documents (update changelogs),
  1333. * Information from users in ThinkWiki
  1334. *
  1335. * WARNING: we use this table also to detect that the machine is
  1336. * a ThinkPad in some cases, so don't remove entries lightly.
  1337. */
  1338. #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
  1339. { .vendor = (__v), \
  1340. .bios = TPID(__id1, __id2), \
  1341. .ec = TPACPI_MATCH_ANY, \
  1342. .quirks = TPACPI_MATCH_ANY << 16 \
  1343. | (__bv1) << 8 | (__bv2) }
  1344. #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
  1345. __eid1, __eid2, __ev1, __ev2) \
  1346. { .vendor = (__v), \
  1347. .bios = TPID(__bid1, __bid2), \
  1348. .ec = TPID(__eid1, __eid2), \
  1349. .quirks = (__ev1) << 24 | (__ev2) << 16 \
  1350. | (__bv1) << 8 | (__bv2) }
  1351. #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
  1352. TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
  1353. #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1354. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1355. __bv1, __bv2, __id1, __id2, __ev1, __ev2)
  1356. #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
  1357. __eid1, __eid2, __ev1, __ev2) \
  1358. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1359. __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
  1360. #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
  1361. TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
  1362. #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1363. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
  1364. __bv1, __bv2, __id1, __id2, __ev1, __ev2)
  1365. #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
  1366. __eid1, __eid2, __ev1, __ev2) \
  1367. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
  1368. __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
  1369. static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
  1370. /* Numeric models ------------------ */
  1371. /* FW MODEL BIOS VERS */
  1372. TPV_QI0('I', 'M', '6', '5'), /* 570 */
  1373. TPV_QI0('I', 'U', '2', '6'), /* 570E */
  1374. TPV_QI0('I', 'B', '5', '4'), /* 600 */
  1375. TPV_QI0('I', 'H', '4', '7'), /* 600E */
  1376. TPV_QI0('I', 'N', '3', '6'), /* 600E */
  1377. TPV_QI0('I', 'T', '5', '5'), /* 600X */
  1378. TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
  1379. TPV_QI0('I', 'I', '4', '2'), /* 770X */
  1380. TPV_QI0('I', 'O', '2', '3'), /* 770Z */
  1381. /* A-series ------------------------- */
  1382. /* FW MODEL BIOS VERS EC VERS */
  1383. TPV_QI0('I', 'W', '5', '9'), /* A20m */
  1384. TPV_QI0('I', 'V', '6', '9'), /* A20p */
  1385. TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
  1386. TPV_QI0('K', 'U', '3', '6'), /* A21e */
  1387. TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
  1388. TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
  1389. TPV_QI0('1', 'B', '1', '7'), /* A22e */
  1390. TPV_QI0('1', '3', '2', '0'), /* A22m */
  1391. TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
  1392. TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
  1393. TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
  1394. /* G-series ------------------------- */
  1395. /* FW MODEL BIOS VERS */
  1396. TPV_QI0('1', 'T', 'A', '6'), /* G40 */
  1397. TPV_QI0('1', 'X', '5', '7'), /* G41 */
  1398. /* R-series, T-series --------------- */
  1399. /* FW MODEL BIOS VERS EC VERS */
  1400. TPV_QI0('1', 'C', 'F', '0'), /* R30 */
  1401. TPV_QI0('1', 'F', 'F', '1'), /* R31 */
  1402. TPV_QI0('1', 'M', '9', '7'), /* R32 */
  1403. TPV_QI0('1', 'O', '6', '1'), /* R40 */
  1404. TPV_QI0('1', 'P', '6', '5'), /* R40 */
  1405. TPV_QI0('1', 'S', '7', '0'), /* R40e */
  1406. TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
  1407. T40/p, T41/p, T42/p (1) */
  1408. TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
  1409. TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
  1410. TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
  1411. TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
  1412. TPV_QI0('I', 'Y', '6', '1'), /* T20 */
  1413. TPV_QI0('K', 'Z', '3', '4'), /* T21 */
  1414. TPV_QI0('1', '6', '3', '2'), /* T22 */
  1415. TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
  1416. TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
  1417. TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
  1418. TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
  1419. TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
  1420. TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
  1421. /* BIOS FW BIOS VERS EC FW EC VERS */
  1422. TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
  1423. TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
  1424. /* X-series ------------------------- */
  1425. /* FW MODEL BIOS VERS EC VERS */
  1426. TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
  1427. TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
  1428. TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
  1429. TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
  1430. TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
  1431. TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
  1432. TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
  1433. TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
  1434. TPV_QL0('7', 'J', '3', '0'), /* X60t */
  1435. /* (0) - older versions lack DMI EC fw string and functionality */
  1436. /* (1) - older versions known to lack functionality */
  1437. };
  1438. #undef TPV_QL1
  1439. #undef TPV_QL0
  1440. #undef TPV_QI2
  1441. #undef TPV_QI1
  1442. #undef TPV_QI0
  1443. #undef TPV_Q_X
  1444. #undef TPV_Q
  1445. static void __init tpacpi_check_outdated_fw(void)
  1446. {
  1447. unsigned long fwvers;
  1448. u16 ec_version, bios_version;
  1449. fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1450. ARRAY_SIZE(tpacpi_bios_version_qtable));
  1451. if (!fwvers)
  1452. return;
  1453. bios_version = fwvers & 0xffffU;
  1454. ec_version = (fwvers >> 16) & 0xffffU;
  1455. /* note that unknown versions are set to 0x0000 and we use that */
  1456. if ((bios_version > thinkpad_id.bios_release) ||
  1457. (ec_version > thinkpad_id.ec_release &&
  1458. ec_version != TPACPI_MATCH_ANY)) {
  1459. /*
  1460. * The changelogs would let us track down the exact
  1461. * reason, but it is just too much of a pain to track
  1462. * it. We only list BIOSes that are either really
  1463. * broken, or really stable to begin with, so it is
  1464. * best if the user upgrades the firmware anyway.
  1465. */
  1466. printk(TPACPI_WARN
  1467. "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
  1468. printk(TPACPI_WARN
  1469. "WARNING: This firmware may be missing critical bug "
  1470. "fixes and/or important features\n");
  1471. }
  1472. }
  1473. static bool __init tpacpi_is_fw_known(void)
  1474. {
  1475. return tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1476. ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
  1477. }
  1478. /****************************************************************************
  1479. ****************************************************************************
  1480. *
  1481. * Subdrivers
  1482. *
  1483. ****************************************************************************
  1484. ****************************************************************************/
  1485. /*************************************************************************
  1486. * thinkpad-acpi init subdriver
  1487. */
  1488. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  1489. {
  1490. printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
  1491. printk(TPACPI_INFO "%s\n", TPACPI_URL);
  1492. printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
  1493. (thinkpad_id.bios_version_str) ?
  1494. thinkpad_id.bios_version_str : "unknown",
  1495. (thinkpad_id.ec_version_str) ?
  1496. thinkpad_id.ec_version_str : "unknown");
  1497. if (thinkpad_id.vendor && thinkpad_id.model_str)
  1498. printk(TPACPI_INFO "%s %s, model %s\n",
  1499. (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
  1500. "IBM" : ((thinkpad_id.vendor ==
  1501. PCI_VENDOR_ID_LENOVO) ?
  1502. "Lenovo" : "Unknown vendor"),
  1503. thinkpad_id.model_str,
  1504. (thinkpad_id.nummodel_str) ?
  1505. thinkpad_id.nummodel_str : "unknown");
  1506. tpacpi_check_outdated_fw();
  1507. return 0;
  1508. }
  1509. static int thinkpad_acpi_driver_read(char *p)
  1510. {
  1511. int len = 0;
  1512. len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
  1513. len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
  1514. return len;
  1515. }
  1516. static struct ibm_struct thinkpad_acpi_driver_data = {
  1517. .name = "driver",
  1518. .read = thinkpad_acpi_driver_read,
  1519. };
  1520. /*************************************************************************
  1521. * Hotkey subdriver
  1522. */
  1523. enum { /* hot key scan codes (derived from ACPI DSDT) */
  1524. TP_ACPI_HOTKEYSCAN_FNF1 = 0,
  1525. TP_ACPI_HOTKEYSCAN_FNF2,
  1526. TP_ACPI_HOTKEYSCAN_FNF3,
  1527. TP_ACPI_HOTKEYSCAN_FNF4,
  1528. TP_ACPI_HOTKEYSCAN_FNF5,
  1529. TP_ACPI_HOTKEYSCAN_FNF6,
  1530. TP_ACPI_HOTKEYSCAN_FNF7,
  1531. TP_ACPI_HOTKEYSCAN_FNF8,
  1532. TP_ACPI_HOTKEYSCAN_FNF9,
  1533. TP_ACPI_HOTKEYSCAN_FNF10,
  1534. TP_ACPI_HOTKEYSCAN_FNF11,
  1535. TP_ACPI_HOTKEYSCAN_FNF12,
  1536. TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
  1537. TP_ACPI_HOTKEYSCAN_FNINSERT,
  1538. TP_ACPI_HOTKEYSCAN_FNDELETE,
  1539. TP_ACPI_HOTKEYSCAN_FNHOME,
  1540. TP_ACPI_HOTKEYSCAN_FNEND,
  1541. TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1542. TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
  1543. TP_ACPI_HOTKEYSCAN_FNSPACE,
  1544. TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1545. TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1546. TP_ACPI_HOTKEYSCAN_MUTE,
  1547. TP_ACPI_HOTKEYSCAN_THINKPAD,
  1548. };
  1549. enum { /* Keys available through NVRAM polling */
  1550. TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
  1551. TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
  1552. };
  1553. enum { /* Positions of some of the keys in hotkey masks */
  1554. TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
  1555. TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
  1556. TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
  1557. TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
  1558. TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
  1559. TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1560. TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
  1561. TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1562. TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1563. TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
  1564. TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
  1565. };
  1566. enum { /* NVRAM to ACPI HKEY group map */
  1567. TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
  1568. TP_ACPI_HKEY_ZOOM_MASK |
  1569. TP_ACPI_HKEY_DISPSWTCH_MASK |
  1570. TP_ACPI_HKEY_HIBERNATE_MASK,
  1571. TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
  1572. TP_ACPI_HKEY_BRGHTDWN_MASK,
  1573. TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
  1574. TP_ACPI_HKEY_VOLDWN_MASK |
  1575. TP_ACPI_HKEY_MUTE_MASK,
  1576. };
  1577. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1578. struct tp_nvram_state {
  1579. u16 thinkpad_toggle:1;
  1580. u16 zoom_toggle:1;
  1581. u16 display_toggle:1;
  1582. u16 thinklight_toggle:1;
  1583. u16 hibernate_toggle:1;
  1584. u16 displayexp_toggle:1;
  1585. u16 display_state:1;
  1586. u16 brightness_toggle:1;
  1587. u16 volume_toggle:1;
  1588. u16 mute:1;
  1589. u8 brightness_level;
  1590. u8 volume_level;
  1591. };
  1592. /* kthread for the hotkey poller */
  1593. static struct task_struct *tpacpi_hotkey_task;
  1594. /* Acquired while the poller kthread is running, use to sync start/stop */
  1595. static struct mutex hotkey_thread_mutex;
  1596. /*
  1597. * Acquire mutex to write poller control variables.
  1598. * Increment hotkey_config_change when changing them.
  1599. *
  1600. * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1601. */
  1602. static struct mutex hotkey_thread_data_mutex;
  1603. static unsigned int hotkey_config_change;
  1604. /*
  1605. * hotkey poller control variables
  1606. *
  1607. * Must be atomic or readers will also need to acquire mutex
  1608. */
  1609. static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
  1610. static unsigned int hotkey_poll_freq = 10; /* Hz */
  1611. #define HOTKEY_CONFIG_CRITICAL_START \
  1612. do { \
  1613. mutex_lock(&hotkey_thread_data_mutex); \
  1614. hotkey_config_change++; \
  1615. } while (0);
  1616. #define HOTKEY_CONFIG_CRITICAL_END \
  1617. mutex_unlock(&hotkey_thread_data_mutex);
  1618. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1619. #define hotkey_source_mask 0U
  1620. #define HOTKEY_CONFIG_CRITICAL_START
  1621. #define HOTKEY_CONFIG_CRITICAL_END
  1622. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1623. static struct mutex hotkey_mutex;
  1624. static enum { /* Reasons for waking up */
  1625. TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
  1626. TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
  1627. TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
  1628. } hotkey_wakeup_reason;
  1629. static int hotkey_autosleep_ack;
  1630. static u32 hotkey_orig_mask;
  1631. static u32 hotkey_all_mask;
  1632. static u32 hotkey_reserved_mask;
  1633. static u32 hotkey_mask;
  1634. static unsigned int hotkey_report_mode;
  1635. static u16 *hotkey_keycode_map;
  1636. static struct attribute_set *hotkey_dev_attributes;
  1637. /* HKEY.MHKG() return bits */
  1638. #define TP_HOTKEY_TABLET_MASK (1 << 3)
  1639. static int hotkey_get_wlsw(void)
  1640. {
  1641. int status;
  1642. if (!tp_features.hotkey_wlsw)
  1643. return -ENODEV;
  1644. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1645. if (dbg_wlswemul)
  1646. return (tpacpi_wlsw_emulstate) ?
  1647. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1648. #endif
  1649. if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
  1650. return -EIO;
  1651. return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1652. }
  1653. static int hotkey_get_tablet_mode(int *status)
  1654. {
  1655. int s;
  1656. if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
  1657. return -EIO;
  1658. *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
  1659. return 0;
  1660. }
  1661. /*
  1662. * Call with hotkey_mutex held
  1663. */
  1664. static int hotkey_mask_get(void)
  1665. {
  1666. u32 m = 0;
  1667. if (tp_features.hotkey_mask) {
  1668. if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
  1669. return -EIO;
  1670. }
  1671. HOTKEY_CONFIG_CRITICAL_START
  1672. hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
  1673. HOTKEY_CONFIG_CRITICAL_END
  1674. return 0;
  1675. }
  1676. /*
  1677. * Call with hotkey_mutex held
  1678. */
  1679. static int hotkey_mask_set(u32 mask)
  1680. {
  1681. int i;
  1682. int rc = 0;
  1683. if (tp_features.hotkey_mask) {
  1684. if (!tp_warned.hotkey_mask_ff &&
  1685. (mask == 0xffff || mask == 0xffffff ||
  1686. mask == 0xffffffff)) {
  1687. tp_warned.hotkey_mask_ff = 1;
  1688. printk(TPACPI_NOTICE
  1689. "setting the hotkey mask to 0x%08x is likely "
  1690. "not the best way to go about it\n", mask);
  1691. printk(TPACPI_NOTICE
  1692. "please consider using the driver defaults, "
  1693. "and refer to up-to-date thinkpad-acpi "
  1694. "documentation\n");
  1695. }
  1696. HOTKEY_CONFIG_CRITICAL_START
  1697. for (i = 0; i < 32; i++) {
  1698. u32 m = 1 << i;
  1699. /* enable in firmware mask only keys not in NVRAM
  1700. * mode, but enable the key in the cached hotkey_mask
  1701. * regardless of mode, or the key will end up
  1702. * disabled by hotkey_mask_get() */
  1703. if (!acpi_evalf(hkey_handle,
  1704. NULL, "MHKM", "vdd", i + 1,
  1705. !!((mask & ~hotkey_source_mask) & m))) {
  1706. rc = -EIO;
  1707. break;
  1708. } else {
  1709. hotkey_mask = (hotkey_mask & ~m) | (mask & m);
  1710. }
  1711. }
  1712. HOTKEY_CONFIG_CRITICAL_END
  1713. /* hotkey_mask_get must be called unconditionally below */
  1714. if (!hotkey_mask_get() && !rc &&
  1715. (hotkey_mask & ~hotkey_source_mask) !=
  1716. (mask & ~hotkey_source_mask)) {
  1717. printk(TPACPI_NOTICE
  1718. "requested hot key mask 0x%08x, but "
  1719. "firmware forced it to 0x%08x\n",
  1720. mask, hotkey_mask);
  1721. }
  1722. } else {
  1723. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1724. HOTKEY_CONFIG_CRITICAL_START
  1725. hotkey_mask = mask & hotkey_source_mask;
  1726. HOTKEY_CONFIG_CRITICAL_END
  1727. hotkey_mask_get();
  1728. if (hotkey_mask != mask) {
  1729. printk(TPACPI_NOTICE
  1730. "requested hot key mask 0x%08x, "
  1731. "forced to 0x%08x (NVRAM poll mask is "
  1732. "0x%08x): no firmware mask support\n",
  1733. mask, hotkey_mask, hotkey_source_mask);
  1734. }
  1735. #else
  1736. hotkey_mask_get();
  1737. rc = -ENXIO;
  1738. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1739. }
  1740. return rc;
  1741. }
  1742. static int hotkey_status_get(int *status)
  1743. {
  1744. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  1745. return -EIO;
  1746. return 0;
  1747. }
  1748. static int hotkey_status_set(bool enable)
  1749. {
  1750. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
  1751. return -EIO;
  1752. return 0;
  1753. }
  1754. static void tpacpi_input_send_tabletsw(void)
  1755. {
  1756. int state;
  1757. if (tp_features.hotkey_tablet &&
  1758. !hotkey_get_tablet_mode(&state)) {
  1759. mutex_lock(&tpacpi_inputdev_send_mutex);
  1760. input_report_switch(tpacpi_inputdev,
  1761. SW_TABLET_MODE, !!state);
  1762. input_sync(tpacpi_inputdev);
  1763. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1764. }
  1765. }
  1766. static void tpacpi_input_send_key(unsigned int scancode)
  1767. {
  1768. unsigned int keycode;
  1769. keycode = hotkey_keycode_map[scancode];
  1770. if (keycode != KEY_RESERVED) {
  1771. mutex_lock(&tpacpi_inputdev_send_mutex);
  1772. input_report_key(tpacpi_inputdev, keycode, 1);
  1773. if (keycode == KEY_UNKNOWN)
  1774. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1775. scancode);
  1776. input_sync(tpacpi_inputdev);
  1777. input_report_key(tpacpi_inputdev, keycode, 0);
  1778. if (keycode == KEY_UNKNOWN)
  1779. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1780. scancode);
  1781. input_sync(tpacpi_inputdev);
  1782. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1783. }
  1784. }
  1785. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1786. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1787. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1788. {
  1789. tpacpi_input_send_key(scancode);
  1790. if (hotkey_report_mode < 2) {
  1791. acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
  1792. 0x80, 0x1001 + scancode);
  1793. }
  1794. }
  1795. static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
  1796. {
  1797. u8 d;
  1798. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1799. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1800. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1801. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1802. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1803. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1804. }
  1805. if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
  1806. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1807. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1808. }
  1809. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1810. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1811. n->displayexp_toggle =
  1812. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1813. }
  1814. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1815. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1816. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1817. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1818. n->brightness_toggle =
  1819. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1820. }
  1821. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1822. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1823. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1824. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1825. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1826. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1827. }
  1828. }
  1829. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1830. do { \
  1831. if ((mask & (1 << __scancode)) && \
  1832. oldn->__member != newn->__member) \
  1833. tpacpi_hotkey_send_key(__scancode); \
  1834. } while (0)
  1835. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1836. do { if (mask & (1 << __scancode)) \
  1837. tpacpi_hotkey_send_key(__scancode); } while (0)
  1838. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  1839. struct tp_nvram_state *newn,
  1840. u32 mask)
  1841. {
  1842. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  1843. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  1844. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  1845. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  1846. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  1847. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  1848. /* handle volume */
  1849. if (oldn->volume_toggle != newn->volume_toggle) {
  1850. if (oldn->mute != newn->mute) {
  1851. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1852. }
  1853. if (oldn->volume_level > newn->volume_level) {
  1854. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1855. } else if (oldn->volume_level < newn->volume_level) {
  1856. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1857. } else if (oldn->mute == newn->mute) {
  1858. /* repeated key presses that didn't change state */
  1859. if (newn->mute) {
  1860. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1861. } else if (newn->volume_level != 0) {
  1862. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1863. } else {
  1864. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1865. }
  1866. }
  1867. }
  1868. /* handle brightness */
  1869. if (oldn->brightness_toggle != newn->brightness_toggle) {
  1870. if (oldn->brightness_level < newn->brightness_level) {
  1871. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1872. } else if (oldn->brightness_level > newn->brightness_level) {
  1873. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1874. } else {
  1875. /* repeated key presses that didn't change state */
  1876. if (newn->brightness_level != 0) {
  1877. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1878. } else {
  1879. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1880. }
  1881. }
  1882. }
  1883. }
  1884. #undef TPACPI_COMPARE_KEY
  1885. #undef TPACPI_MAY_SEND_KEY
  1886. static int hotkey_kthread(void *data)
  1887. {
  1888. struct tp_nvram_state s[2];
  1889. u32 mask;
  1890. unsigned int si, so;
  1891. unsigned long t;
  1892. unsigned int change_detector, must_reset;
  1893. unsigned int poll_freq;
  1894. mutex_lock(&hotkey_thread_mutex);
  1895. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  1896. goto exit;
  1897. set_freezable();
  1898. so = 0;
  1899. si = 1;
  1900. t = 0;
  1901. /* Initial state for compares */
  1902. mutex_lock(&hotkey_thread_data_mutex);
  1903. change_detector = hotkey_config_change;
  1904. mask = hotkey_source_mask & hotkey_mask;
  1905. poll_freq = hotkey_poll_freq;
  1906. mutex_unlock(&hotkey_thread_data_mutex);
  1907. hotkey_read_nvram(&s[so], mask);
  1908. while (!kthread_should_stop()) {
  1909. if (t == 0) {
  1910. if (likely(poll_freq))
  1911. t = 1000/poll_freq;
  1912. else
  1913. t = 100; /* should never happen... */
  1914. }
  1915. t = msleep_interruptible(t);
  1916. if (unlikely(kthread_should_stop()))
  1917. break;
  1918. must_reset = try_to_freeze();
  1919. if (t > 0 && !must_reset)
  1920. continue;
  1921. mutex_lock(&hotkey_thread_data_mutex);
  1922. if (must_reset || hotkey_config_change != change_detector) {
  1923. /* forget old state on thaw or config change */
  1924. si = so;
  1925. t = 0;
  1926. change_detector = hotkey_config_change;
  1927. }
  1928. mask = hotkey_source_mask & hotkey_mask;
  1929. poll_freq = hotkey_poll_freq;
  1930. mutex_unlock(&hotkey_thread_data_mutex);
  1931. if (likely(mask)) {
  1932. hotkey_read_nvram(&s[si], mask);
  1933. if (likely(si != so)) {
  1934. hotkey_compare_and_issue_event(&s[so], &s[si],
  1935. mask);
  1936. }
  1937. }
  1938. so = si;
  1939. si ^= 1;
  1940. }
  1941. exit:
  1942. mutex_unlock(&hotkey_thread_mutex);
  1943. return 0;
  1944. }
  1945. /* call with hotkey_mutex held */
  1946. static void hotkey_poll_stop_sync(void)
  1947. {
  1948. if (tpacpi_hotkey_task) {
  1949. if (frozen(tpacpi_hotkey_task) ||
  1950. freezing(tpacpi_hotkey_task))
  1951. thaw_process(tpacpi_hotkey_task);
  1952. kthread_stop(tpacpi_hotkey_task);
  1953. tpacpi_hotkey_task = NULL;
  1954. mutex_lock(&hotkey_thread_mutex);
  1955. /* at this point, the thread did exit */
  1956. mutex_unlock(&hotkey_thread_mutex);
  1957. }
  1958. }
  1959. /* call with hotkey_mutex held */
  1960. static void hotkey_poll_setup(bool may_warn)
  1961. {
  1962. u32 hotkeys_to_poll = hotkey_source_mask & hotkey_mask;
  1963. if (hotkeys_to_poll != 0 && hotkey_poll_freq > 0 &&
  1964. (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
  1965. if (!tpacpi_hotkey_task) {
  1966. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  1967. NULL, TPACPI_NVRAM_KTHREAD_NAME);
  1968. if (IS_ERR(tpacpi_hotkey_task)) {
  1969. tpacpi_hotkey_task = NULL;
  1970. printk(TPACPI_ERR
  1971. "could not create kernel thread "
  1972. "for hotkey polling\n");
  1973. }
  1974. }
  1975. } else {
  1976. hotkey_poll_stop_sync();
  1977. if (may_warn && hotkeys_to_poll != 0 &&
  1978. hotkey_poll_freq == 0) {
  1979. printk(TPACPI_NOTICE
  1980. "hot keys 0x%08x require polling, "
  1981. "which is currently disabled\n",
  1982. hotkeys_to_poll);
  1983. }
  1984. }
  1985. }
  1986. static void hotkey_poll_setup_safe(bool may_warn)
  1987. {
  1988. mutex_lock(&hotkey_mutex);
  1989. hotkey_poll_setup(may_warn);
  1990. mutex_unlock(&hotkey_mutex);
  1991. }
  1992. /* call with hotkey_mutex held */
  1993. static void hotkey_poll_set_freq(unsigned int freq)
  1994. {
  1995. if (!freq)
  1996. hotkey_poll_stop_sync();
  1997. HOTKEY_CONFIG_CRITICAL_START
  1998. hotkey_poll_freq = freq;
  1999. HOTKEY_CONFIG_CRITICAL_END
  2000. }
  2001. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2002. static void hotkey_poll_setup_safe(bool __unused)
  2003. {
  2004. }
  2005. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2006. static int hotkey_inputdev_open(struct input_dev *dev)
  2007. {
  2008. switch (tpacpi_lifecycle) {
  2009. case TPACPI_LIFE_INIT:
  2010. /*
  2011. * hotkey_init will call hotkey_poll_setup_safe
  2012. * at the appropriate moment
  2013. */
  2014. return 0;
  2015. case TPACPI_LIFE_EXITING:
  2016. return -EBUSY;
  2017. case TPACPI_LIFE_RUNNING:
  2018. hotkey_poll_setup_safe(false);
  2019. return 0;
  2020. }
  2021. /* Should only happen if tpacpi_lifecycle is corrupt */
  2022. BUG();
  2023. return -EBUSY;
  2024. }
  2025. static void hotkey_inputdev_close(struct input_dev *dev)
  2026. {
  2027. /* disable hotkey polling when possible */
  2028. if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
  2029. hotkey_poll_setup_safe(false);
  2030. }
  2031. /* sysfs hotkey enable ------------------------------------------------- */
  2032. static ssize_t hotkey_enable_show(struct device *dev,
  2033. struct device_attribute *attr,
  2034. char *buf)
  2035. {
  2036. int res, status;
  2037. printk_deprecated_attribute("hotkey_enable",
  2038. "Hotkey reporting is always enabled");
  2039. res = hotkey_status_get(&status);
  2040. if (res)
  2041. return res;
  2042. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  2043. }
  2044. static ssize_t hotkey_enable_store(struct device *dev,
  2045. struct device_attribute *attr,
  2046. const char *buf, size_t count)
  2047. {
  2048. unsigned long t;
  2049. printk_deprecated_attribute("hotkey_enable",
  2050. "Hotkeys can be disabled through hotkey_mask");
  2051. if (parse_strtoul(buf, 1, &t))
  2052. return -EINVAL;
  2053. if (t == 0)
  2054. return -EPERM;
  2055. return count;
  2056. }
  2057. static struct device_attribute dev_attr_hotkey_enable =
  2058. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  2059. hotkey_enable_show, hotkey_enable_store);
  2060. /* sysfs hotkey mask --------------------------------------------------- */
  2061. static ssize_t hotkey_mask_show(struct device *dev,
  2062. struct device_attribute *attr,
  2063. char *buf)
  2064. {
  2065. int res;
  2066. if (mutex_lock_killable(&hotkey_mutex))
  2067. return -ERESTARTSYS;
  2068. res = hotkey_mask_get();
  2069. mutex_unlock(&hotkey_mutex);
  2070. return (res)?
  2071. res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
  2072. }
  2073. static ssize_t hotkey_mask_store(struct device *dev,
  2074. struct device_attribute *attr,
  2075. const char *buf, size_t count)
  2076. {
  2077. unsigned long t;
  2078. int res;
  2079. if (parse_strtoul(buf, 0xffffffffUL, &t))
  2080. return -EINVAL;
  2081. if (mutex_lock_killable(&hotkey_mutex))
  2082. return -ERESTARTSYS;
  2083. res = hotkey_mask_set(t);
  2084. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2085. hotkey_poll_setup(true);
  2086. #endif
  2087. mutex_unlock(&hotkey_mutex);
  2088. tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
  2089. return (res) ? res : count;
  2090. }
  2091. static struct device_attribute dev_attr_hotkey_mask =
  2092. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  2093. hotkey_mask_show, hotkey_mask_store);
  2094. /* sysfs hotkey bios_enabled ------------------------------------------- */
  2095. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  2096. struct device_attribute *attr,
  2097. char *buf)
  2098. {
  2099. return sprintf(buf, "0\n");
  2100. }
  2101. static struct device_attribute dev_attr_hotkey_bios_enabled =
  2102. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  2103. /* sysfs hotkey bios_mask ---------------------------------------------- */
  2104. static ssize_t hotkey_bios_mask_show(struct device *dev,
  2105. struct device_attribute *attr,
  2106. char *buf)
  2107. {
  2108. printk_deprecated_attribute("hotkey_bios_mask",
  2109. "This attribute is useless.");
  2110. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  2111. }
  2112. static struct device_attribute dev_attr_hotkey_bios_mask =
  2113. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  2114. /* sysfs hotkey all_mask ----------------------------------------------- */
  2115. static ssize_t hotkey_all_mask_show(struct device *dev,
  2116. struct device_attribute *attr,
  2117. char *buf)
  2118. {
  2119. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  2120. hotkey_all_mask | hotkey_source_mask);
  2121. }
  2122. static struct device_attribute dev_attr_hotkey_all_mask =
  2123. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  2124. /* sysfs hotkey recommended_mask --------------------------------------- */
  2125. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  2126. struct device_attribute *attr,
  2127. char *buf)
  2128. {
  2129. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  2130. (hotkey_all_mask | hotkey_source_mask)
  2131. & ~hotkey_reserved_mask);
  2132. }
  2133. static struct device_attribute dev_attr_hotkey_recommended_mask =
  2134. __ATTR(hotkey_recommended_mask, S_IRUGO,
  2135. hotkey_recommended_mask_show, NULL);
  2136. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2137. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  2138. static ssize_t hotkey_source_mask_show(struct device *dev,
  2139. struct device_attribute *attr,
  2140. char *buf)
  2141. {
  2142. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
  2143. }
  2144. static ssize_t hotkey_source_mask_store(struct device *dev,
  2145. struct device_attribute *attr,
  2146. const char *buf, size_t count)
  2147. {
  2148. unsigned long t;
  2149. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  2150. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  2151. return -EINVAL;
  2152. if (mutex_lock_killable(&hotkey_mutex))
  2153. return -ERESTARTSYS;
  2154. HOTKEY_CONFIG_CRITICAL_START
  2155. hotkey_source_mask = t;
  2156. HOTKEY_CONFIG_CRITICAL_END
  2157. hotkey_poll_setup(true);
  2158. hotkey_mask_set(hotkey_mask);
  2159. mutex_unlock(&hotkey_mutex);
  2160. tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
  2161. return count;
  2162. }
  2163. static struct device_attribute dev_attr_hotkey_source_mask =
  2164. __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
  2165. hotkey_source_mask_show, hotkey_source_mask_store);
  2166. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  2167. static ssize_t hotkey_poll_freq_show(struct device *dev,
  2168. struct device_attribute *attr,
  2169. char *buf)
  2170. {
  2171. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
  2172. }
  2173. static ssize_t hotkey_poll_freq_store(struct device *dev,
  2174. struct device_attribute *attr,
  2175. const char *buf, size_t count)
  2176. {
  2177. unsigned long t;
  2178. if (parse_strtoul(buf, 25, &t))
  2179. return -EINVAL;
  2180. if (mutex_lock_killable(&hotkey_mutex))
  2181. return -ERESTARTSYS;
  2182. hotkey_poll_set_freq(t);
  2183. hotkey_poll_setup(true);
  2184. mutex_unlock(&hotkey_mutex);
  2185. tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
  2186. return count;
  2187. }
  2188. static struct device_attribute dev_attr_hotkey_poll_freq =
  2189. __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
  2190. hotkey_poll_freq_show, hotkey_poll_freq_store);
  2191. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2192. /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
  2193. static ssize_t hotkey_radio_sw_show(struct device *dev,
  2194. struct device_attribute *attr,
  2195. char *buf)
  2196. {
  2197. int res;
  2198. res = hotkey_get_wlsw();
  2199. if (res < 0)
  2200. return res;
  2201. /* Opportunistic update */
  2202. tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
  2203. return snprintf(buf, PAGE_SIZE, "%d\n",
  2204. (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
  2205. }
  2206. static struct device_attribute dev_attr_hotkey_radio_sw =
  2207. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  2208. static void hotkey_radio_sw_notify_change(void)
  2209. {
  2210. if (tp_features.hotkey_wlsw)
  2211. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2212. "hotkey_radio_sw");
  2213. }
  2214. /* sysfs hotkey tablet mode (pollable) --------------------------------- */
  2215. static ssize_t hotkey_tablet_mode_show(struct device *dev,
  2216. struct device_attribute *attr,
  2217. char *buf)
  2218. {
  2219. int res, s;
  2220. res = hotkey_get_tablet_mode(&s);
  2221. if (res < 0)
  2222. return res;
  2223. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  2224. }
  2225. static struct device_attribute dev_attr_hotkey_tablet_mode =
  2226. __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
  2227. static void hotkey_tablet_mode_notify_change(void)
  2228. {
  2229. if (tp_features.hotkey_tablet)
  2230. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2231. "hotkey_tablet_mode");
  2232. }
  2233. /* sysfs hotkey report_mode -------------------------------------------- */
  2234. static ssize_t hotkey_report_mode_show(struct device *dev,
  2235. struct device_attribute *attr,
  2236. char *buf)
  2237. {
  2238. return snprintf(buf, PAGE_SIZE, "%d\n",
  2239. (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
  2240. }
  2241. static struct device_attribute dev_attr_hotkey_report_mode =
  2242. __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
  2243. /* sysfs wakeup reason (pollable) -------------------------------------- */
  2244. static ssize_t hotkey_wakeup_reason_show(struct device *dev,
  2245. struct device_attribute *attr,
  2246. char *buf)
  2247. {
  2248. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
  2249. }
  2250. static struct device_attribute dev_attr_hotkey_wakeup_reason =
  2251. __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
  2252. static void hotkey_wakeup_reason_notify_change(void)
  2253. {
  2254. if (tp_features.hotkey_mask)
  2255. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2256. "wakeup_reason");
  2257. }
  2258. /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
  2259. static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
  2260. struct device_attribute *attr,
  2261. char *buf)
  2262. {
  2263. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
  2264. }
  2265. static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
  2266. __ATTR(wakeup_hotunplug_complete, S_IRUGO,
  2267. hotkey_wakeup_hotunplug_complete_show, NULL);
  2268. static void hotkey_wakeup_hotunplug_complete_notify_change(void)
  2269. {
  2270. if (tp_features.hotkey_mask)
  2271. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2272. "wakeup_hotunplug_complete");
  2273. }
  2274. /* --------------------------------------------------------------------- */
  2275. static struct attribute *hotkey_attributes[] __initdata = {
  2276. &dev_attr_hotkey_enable.attr,
  2277. &dev_attr_hotkey_bios_enabled.attr,
  2278. &dev_attr_hotkey_report_mode.attr,
  2279. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2280. &dev_attr_hotkey_mask.attr,
  2281. &dev_attr_hotkey_all_mask.attr,
  2282. &dev_attr_hotkey_recommended_mask.attr,
  2283. &dev_attr_hotkey_source_mask.attr,
  2284. &dev_attr_hotkey_poll_freq.attr,
  2285. #endif
  2286. };
  2287. static struct attribute *hotkey_mask_attributes[] __initdata = {
  2288. &dev_attr_hotkey_bios_mask.attr,
  2289. #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2290. &dev_attr_hotkey_mask.attr,
  2291. &dev_attr_hotkey_all_mask.attr,
  2292. &dev_attr_hotkey_recommended_mask.attr,
  2293. #endif
  2294. &dev_attr_hotkey_wakeup_reason.attr,
  2295. &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
  2296. };
  2297. /*
  2298. * Sync both the hw and sw blocking state of all switches
  2299. */
  2300. static void tpacpi_send_radiosw_update(void)
  2301. {
  2302. int wlsw;
  2303. /*
  2304. * We must sync all rfkill controllers *before* issuing any
  2305. * rfkill input events, or we will race the rfkill core input
  2306. * handler.
  2307. *
  2308. * tpacpi_inputdev_send_mutex works as a syncronization point
  2309. * for the above.
  2310. *
  2311. * We optimize to avoid numerous calls to hotkey_get_wlsw.
  2312. */
  2313. wlsw = hotkey_get_wlsw();
  2314. /* Sync hw blocking state first if it is hw-blocked */
  2315. if (wlsw == TPACPI_RFK_RADIO_OFF)
  2316. tpacpi_rfk_update_hwblock_state(true);
  2317. /* Sync sw blocking state */
  2318. tpacpi_rfk_update_swstate_all();
  2319. /* Sync hw blocking state last if it is hw-unblocked */
  2320. if (wlsw == TPACPI_RFK_RADIO_ON)
  2321. tpacpi_rfk_update_hwblock_state(false);
  2322. /* Issue rfkill input event for WLSW switch */
  2323. if (!(wlsw < 0)) {
  2324. mutex_lock(&tpacpi_inputdev_send_mutex);
  2325. input_report_switch(tpacpi_inputdev,
  2326. SW_RFKILL_ALL, (wlsw > 0));
  2327. input_sync(tpacpi_inputdev);
  2328. mutex_unlock(&tpacpi_inputdev_send_mutex);
  2329. }
  2330. /*
  2331. * this can be unconditional, as we will poll state again
  2332. * if userspace uses the notify to read data
  2333. */
  2334. hotkey_radio_sw_notify_change();
  2335. }
  2336. static void hotkey_exit(void)
  2337. {
  2338. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2339. mutex_lock(&hotkey_mutex);
  2340. hotkey_poll_stop_sync();
  2341. mutex_unlock(&hotkey_mutex);
  2342. #endif
  2343. if (hotkey_dev_attributes)
  2344. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  2345. kfree(hotkey_keycode_map);
  2346. if (tp_features.hotkey) {
  2347. dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
  2348. "restoring original hot key mask\n");
  2349. /* no short-circuit boolean operator below! */
  2350. if ((hotkey_mask_set(hotkey_orig_mask) |
  2351. hotkey_status_set(false)) != 0)
  2352. printk(TPACPI_ERR
  2353. "failed to restore hot key mask "
  2354. "to BIOS defaults\n");
  2355. }
  2356. }
  2357. static int __init hotkey_init(struct ibm_init_struct *iibm)
  2358. {
  2359. /* Requirements for changing the default keymaps:
  2360. *
  2361. * 1. Many of the keys are mapped to KEY_RESERVED for very
  2362. * good reasons. Do not change them unless you have deep
  2363. * knowledge on the IBM and Lenovo ThinkPad firmware for
  2364. * the various ThinkPad models. The driver behaves
  2365. * differently for KEY_RESERVED: such keys have their
  2366. * hot key mask *unset* in mask_recommended, and also
  2367. * in the initial hot key mask programmed into the
  2368. * firmware at driver load time, which means the firm-
  2369. * ware may react very differently if you change them to
  2370. * something else;
  2371. *
  2372. * 2. You must be subscribed to the linux-thinkpad and
  2373. * ibm-acpi-devel mailing lists, and you should read the
  2374. * list archives since 2007 if you want to change the
  2375. * keymaps. This requirement exists so that you will
  2376. * know the past history of problems with the thinkpad-
  2377. * acpi driver keymaps, and also that you will be
  2378. * listening to any bug reports;
  2379. *
  2380. * 3. Do not send thinkpad-acpi specific patches directly to
  2381. * for merging, *ever*. Send them to the linux-acpi
  2382. * mailinglist for comments. Merging is to be done only
  2383. * through acpi-test and the ACPI maintainer.
  2384. *
  2385. * If the above is too much to ask, don't change the keymap.
  2386. * Ask the thinkpad-acpi maintainer to do it, instead.
  2387. */
  2388. static u16 ibm_keycode_map[] __initdata = {
  2389. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  2390. KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
  2391. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  2392. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  2393. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  2394. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  2395. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  2396. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  2397. /* brightness: firmware always reacts to them, unless
  2398. * X.org did some tricks in the radeon BIOS scratch
  2399. * registers of *some* models */
  2400. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  2401. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  2402. /* Thinklight: firmware always react to it */
  2403. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  2404. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  2405. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  2406. /* Volume: firmware always react to it and reprograms
  2407. * the built-in *extra* mixer. Never map it to control
  2408. * another mixer by default. */
  2409. KEY_RESERVED, /* 0x14: VOLUME UP */
  2410. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  2411. KEY_RESERVED, /* 0x16: MUTE */
  2412. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  2413. /* (assignments unknown, please report if found) */
  2414. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2415. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2416. };
  2417. static u16 lenovo_keycode_map[] __initdata = {
  2418. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  2419. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  2420. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  2421. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  2422. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  2423. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  2424. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  2425. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  2426. /* These either have to go through ACPI video, or
  2427. * act like in the IBM ThinkPads, so don't ever
  2428. * enable them by default */
  2429. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  2430. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  2431. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  2432. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  2433. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  2434. /* Volume: z60/z61, T60 (BIOS version?): firmware always
  2435. * react to it and reprograms the built-in *extra* mixer.
  2436. * Never map it to control another mixer by default.
  2437. *
  2438. * T60?, T61, R60?, R61: firmware and EC tries to send
  2439. * these over the regular keyboard, so these are no-ops,
  2440. * but there are still weird bugs re. MUTE, so do not
  2441. * change unless you get test reports from all Lenovo
  2442. * models. May cause the BIOS to interfere with the
  2443. * HDA mixer.
  2444. */
  2445. KEY_RESERVED, /* 0x14: VOLUME UP */
  2446. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  2447. KEY_RESERVED, /* 0x16: MUTE */
  2448. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  2449. /* (assignments unknown, please report if found) */
  2450. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2451. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2452. };
  2453. #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
  2454. #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
  2455. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
  2456. int res, i;
  2457. int status;
  2458. int hkeyv;
  2459. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2460. "initializing hotkey subdriver\n");
  2461. BUG_ON(!tpacpi_inputdev);
  2462. BUG_ON(tpacpi_inputdev->open != NULL ||
  2463. tpacpi_inputdev->close != NULL);
  2464. TPACPI_ACPIHANDLE_INIT(hkey);
  2465. mutex_init(&hotkey_mutex);
  2466. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2467. mutex_init(&hotkey_thread_mutex);
  2468. mutex_init(&hotkey_thread_data_mutex);
  2469. #endif
  2470. /* hotkey not supported on 570 */
  2471. tp_features.hotkey = hkey_handle != NULL;
  2472. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2473. "hotkeys are %s\n",
  2474. str_supported(tp_features.hotkey));
  2475. if (!tp_features.hotkey)
  2476. return 1;
  2477. tpacpi_disable_brightness_delay();
  2478. hotkey_dev_attributes = create_attr_set(13, NULL);
  2479. if (!hotkey_dev_attributes)
  2480. return -ENOMEM;
  2481. res = add_many_to_attr_set(hotkey_dev_attributes,
  2482. hotkey_attributes,
  2483. ARRAY_SIZE(hotkey_attributes));
  2484. if (res)
  2485. goto err_exit;
  2486. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2487. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  2488. for HKEY interface version 0x100 */
  2489. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  2490. if ((hkeyv >> 8) != 1) {
  2491. printk(TPACPI_ERR "unknown version of the "
  2492. "HKEY interface: 0x%x\n", hkeyv);
  2493. printk(TPACPI_ERR "please report this to %s\n",
  2494. TPACPI_MAIL);
  2495. } else {
  2496. /*
  2497. * MHKV 0x100 in A31, R40, R40e,
  2498. * T4x, X31, and later
  2499. */
  2500. tp_features.hotkey_mask = 1;
  2501. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2502. "firmware HKEY interface version: 0x%x\n",
  2503. hkeyv);
  2504. }
  2505. }
  2506. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2507. "hotkey masks are %s\n",
  2508. str_supported(tp_features.hotkey_mask));
  2509. if (tp_features.hotkey_mask) {
  2510. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  2511. "MHKA", "qd")) {
  2512. printk(TPACPI_ERR
  2513. "missing MHKA handler, "
  2514. "please report this to %s\n",
  2515. TPACPI_MAIL);
  2516. /* FN+F12, FN+F4, FN+F3 */
  2517. hotkey_all_mask = 0x080cU;
  2518. }
  2519. }
  2520. /* hotkey_source_mask *must* be zero for
  2521. * the first hotkey_mask_get */
  2522. if (tp_features.hotkey_mask) {
  2523. res = hotkey_mask_get();
  2524. if (res)
  2525. goto err_exit;
  2526. hotkey_orig_mask = hotkey_mask;
  2527. res = add_many_to_attr_set(
  2528. hotkey_dev_attributes,
  2529. hotkey_mask_attributes,
  2530. ARRAY_SIZE(hotkey_mask_attributes));
  2531. if (res)
  2532. goto err_exit;
  2533. }
  2534. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2535. if (tp_features.hotkey_mask) {
  2536. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  2537. & ~hotkey_all_mask;
  2538. } else {
  2539. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
  2540. }
  2541. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2542. "hotkey source mask 0x%08x, polling freq %u\n",
  2543. hotkey_source_mask, hotkey_poll_freq);
  2544. #endif
  2545. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  2546. if (dbg_wlswemul) {
  2547. tp_features.hotkey_wlsw = 1;
  2548. printk(TPACPI_INFO
  2549. "radio switch emulation enabled\n");
  2550. } else
  2551. #endif
  2552. /* Not all thinkpads have a hardware radio switch */
  2553. if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  2554. tp_features.hotkey_wlsw = 1;
  2555. printk(TPACPI_INFO
  2556. "radio switch found; radios are %s\n",
  2557. enabled(status, 0));
  2558. }
  2559. if (tp_features.hotkey_wlsw)
  2560. res = add_to_attr_set(hotkey_dev_attributes,
  2561. &dev_attr_hotkey_radio_sw.attr);
  2562. /* For X41t, X60t, X61t Tablets... */
  2563. if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
  2564. tp_features.hotkey_tablet = 1;
  2565. printk(TPACPI_INFO
  2566. "possible tablet mode switch found; "
  2567. "ThinkPad in %s mode\n",
  2568. (status & TP_HOTKEY_TABLET_MASK)?
  2569. "tablet" : "laptop");
  2570. res = add_to_attr_set(hotkey_dev_attributes,
  2571. &dev_attr_hotkey_tablet_mode.attr);
  2572. }
  2573. if (!res)
  2574. res = register_attr_set_with_sysfs(
  2575. hotkey_dev_attributes,
  2576. &tpacpi_pdev->dev.kobj);
  2577. if (res)
  2578. goto err_exit;
  2579. /* Set up key map */
  2580. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  2581. GFP_KERNEL);
  2582. if (!hotkey_keycode_map) {
  2583. printk(TPACPI_ERR
  2584. "failed to allocate memory for key map\n");
  2585. res = -ENOMEM;
  2586. goto err_exit;
  2587. }
  2588. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  2589. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2590. "using Lenovo default hot key map\n");
  2591. memcpy(hotkey_keycode_map, &lenovo_keycode_map,
  2592. TPACPI_HOTKEY_MAP_SIZE);
  2593. } else {
  2594. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2595. "using IBM default hot key map\n");
  2596. memcpy(hotkey_keycode_map, &ibm_keycode_map,
  2597. TPACPI_HOTKEY_MAP_SIZE);
  2598. }
  2599. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  2600. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  2601. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  2602. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  2603. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  2604. tpacpi_inputdev->keycode = hotkey_keycode_map;
  2605. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  2606. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  2607. set_bit(hotkey_keycode_map[i],
  2608. tpacpi_inputdev->keybit);
  2609. } else {
  2610. if (i < sizeof(hotkey_reserved_mask)*8)
  2611. hotkey_reserved_mask |= 1 << i;
  2612. }
  2613. }
  2614. if (tp_features.hotkey_wlsw) {
  2615. set_bit(EV_SW, tpacpi_inputdev->evbit);
  2616. set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
  2617. }
  2618. if (tp_features.hotkey_tablet) {
  2619. set_bit(EV_SW, tpacpi_inputdev->evbit);
  2620. set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
  2621. }
  2622. /* Do not issue duplicate brightness change events to
  2623. * userspace */
  2624. if (!tp_features.bright_acpimode)
  2625. /* update bright_acpimode... */
  2626. tpacpi_check_std_acpi_brightness_support();
  2627. if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
  2628. printk(TPACPI_INFO
  2629. "This ThinkPad has standard ACPI backlight "
  2630. "brightness control, supported by the ACPI "
  2631. "video driver\n");
  2632. printk(TPACPI_NOTICE
  2633. "Disabling thinkpad-acpi brightness events "
  2634. "by default...\n");
  2635. /* The hotkey_reserved_mask change below is not
  2636. * necessary while the keys are at KEY_RESERVED in the
  2637. * default map, but better safe than sorry, leave it
  2638. * here as a marker of what we have to do, especially
  2639. * when we finally become able to set this at runtime
  2640. * on response to X.org requests */
  2641. hotkey_reserved_mask |=
  2642. (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
  2643. | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
  2644. }
  2645. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2646. "enabling firmware HKEY event interface...\n");
  2647. res = hotkey_status_set(true);
  2648. if (res) {
  2649. hotkey_exit();
  2650. return res;
  2651. }
  2652. res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
  2653. & ~hotkey_reserved_mask)
  2654. | hotkey_orig_mask);
  2655. if (res < 0 && res != -ENXIO) {
  2656. hotkey_exit();
  2657. return res;
  2658. }
  2659. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2660. "legacy ibm/hotkey event reporting over procfs %s\n",
  2661. (hotkey_report_mode < 2) ?
  2662. "enabled" : "disabled");
  2663. tpacpi_inputdev->open = &hotkey_inputdev_open;
  2664. tpacpi_inputdev->close = &hotkey_inputdev_close;
  2665. hotkey_poll_setup_safe(true);
  2666. tpacpi_send_radiosw_update();
  2667. tpacpi_input_send_tabletsw();
  2668. return 0;
  2669. err_exit:
  2670. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  2671. hotkey_dev_attributes = NULL;
  2672. return (res < 0)? res : 1;
  2673. }
  2674. static bool hotkey_notify_hotkey(const u32 hkey,
  2675. bool *send_acpi_ev,
  2676. bool *ignore_acpi_ev)
  2677. {
  2678. /* 0x1000-0x1FFF: key presses */
  2679. unsigned int scancode = hkey & 0xfff;
  2680. *send_acpi_ev = true;
  2681. *ignore_acpi_ev = false;
  2682. if (scancode > 0 && scancode < 0x21) {
  2683. scancode--;
  2684. if (!(hotkey_source_mask & (1 << scancode))) {
  2685. tpacpi_input_send_key(scancode);
  2686. *send_acpi_ev = false;
  2687. } else {
  2688. *ignore_acpi_ev = true;
  2689. }
  2690. return true;
  2691. }
  2692. return false;
  2693. }
  2694. static bool hotkey_notify_wakeup(const u32 hkey,
  2695. bool *send_acpi_ev,
  2696. bool *ignore_acpi_ev)
  2697. {
  2698. /* 0x2000-0x2FFF: Wakeup reason */
  2699. *send_acpi_ev = true;
  2700. *ignore_acpi_ev = false;
  2701. switch (hkey) {
  2702. case 0x2304: /* suspend, undock */
  2703. case 0x2404: /* hibernation, undock */
  2704. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  2705. *ignore_acpi_ev = true;
  2706. break;
  2707. case 0x2305: /* suspend, bay eject */
  2708. case 0x2405: /* hibernation, bay eject */
  2709. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  2710. *ignore_acpi_ev = true;
  2711. break;
  2712. case 0x2313: /* Battery on critical low level (S3) */
  2713. case 0x2413: /* Battery on critical low level (S4) */
  2714. printk(TPACPI_ALERT
  2715. "EMERGENCY WAKEUP: battery almost empty\n");
  2716. /* how to auto-heal: */
  2717. /* 2313: woke up from S3, go to S4/S5 */
  2718. /* 2413: woke up from S4, go to S5 */
  2719. break;
  2720. default:
  2721. return false;
  2722. }
  2723. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  2724. printk(TPACPI_INFO
  2725. "woke up due to a hot-unplug "
  2726. "request...\n");
  2727. hotkey_wakeup_reason_notify_change();
  2728. }
  2729. return true;
  2730. }
  2731. static bool hotkey_notify_usrevent(const u32 hkey,
  2732. bool *send_acpi_ev,
  2733. bool *ignore_acpi_ev)
  2734. {
  2735. /* 0x5000-0x5FFF: human interface helpers */
  2736. *send_acpi_ev = true;
  2737. *ignore_acpi_ev = false;
  2738. switch (hkey) {
  2739. case 0x5010: /* Lenovo new BIOS: brightness changed */
  2740. case 0x500b: /* X61t: tablet pen inserted into bay */
  2741. case 0x500c: /* X61t: tablet pen removed from bay */
  2742. return true;
  2743. case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
  2744. case 0x500a: /* X41t-X61t: swivel down (normal mode) */
  2745. tpacpi_input_send_tabletsw();
  2746. hotkey_tablet_mode_notify_change();
  2747. *send_acpi_ev = false;
  2748. return true;
  2749. case 0x5001:
  2750. case 0x5002:
  2751. /* LID switch events. Do not propagate */
  2752. *ignore_acpi_ev = true;
  2753. return true;
  2754. default:
  2755. return false;
  2756. }
  2757. }
  2758. static bool hotkey_notify_thermal(const u32 hkey,
  2759. bool *send_acpi_ev,
  2760. bool *ignore_acpi_ev)
  2761. {
  2762. /* 0x6000-0x6FFF: thermal alarms */
  2763. *send_acpi_ev = true;
  2764. *ignore_acpi_ev = false;
  2765. switch (hkey) {
  2766. case 0x6011:
  2767. printk(TPACPI_CRIT
  2768. "THERMAL ALARM: battery is too hot!\n");
  2769. /* recommended action: warn user through gui */
  2770. return true;
  2771. case 0x6012:
  2772. printk(TPACPI_ALERT
  2773. "THERMAL EMERGENCY: battery is extremely hot!\n");
  2774. /* recommended action: immediate sleep/hibernate */
  2775. return true;
  2776. case 0x6021:
  2777. printk(TPACPI_CRIT
  2778. "THERMAL ALARM: "
  2779. "a sensor reports something is too hot!\n");
  2780. /* recommended action: warn user through gui, that */
  2781. /* some internal component is too hot */
  2782. return true;
  2783. case 0x6022:
  2784. printk(TPACPI_ALERT
  2785. "THERMAL EMERGENCY: "
  2786. "a sensor reports something is extremely hot!\n");
  2787. /* recommended action: immediate sleep/hibernate */
  2788. return true;
  2789. case 0x6030:
  2790. printk(TPACPI_INFO
  2791. "EC reports that Thermal Table has changed\n");
  2792. /* recommended action: do nothing, we don't have
  2793. * Lenovo ATM information */
  2794. return true;
  2795. default:
  2796. printk(TPACPI_ALERT
  2797. "THERMAL ALERT: unknown thermal alarm received\n");
  2798. return false;
  2799. }
  2800. }
  2801. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  2802. {
  2803. u32 hkey;
  2804. bool send_acpi_ev;
  2805. bool ignore_acpi_ev;
  2806. bool known_ev;
  2807. if (event != 0x80) {
  2808. printk(TPACPI_ERR
  2809. "unknown HKEY notification event %d\n", event);
  2810. /* forward it to userspace, maybe it knows how to handle it */
  2811. acpi_bus_generate_netlink_event(
  2812. ibm->acpi->device->pnp.device_class,
  2813. dev_name(&ibm->acpi->device->dev),
  2814. event, 0);
  2815. return;
  2816. }
  2817. while (1) {
  2818. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  2819. printk(TPACPI_ERR "failed to retrieve HKEY event\n");
  2820. return;
  2821. }
  2822. if (hkey == 0) {
  2823. /* queue empty */
  2824. return;
  2825. }
  2826. send_acpi_ev = true;
  2827. ignore_acpi_ev = false;
  2828. switch (hkey >> 12) {
  2829. case 1:
  2830. /* 0x1000-0x1FFF: key presses */
  2831. known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
  2832. &ignore_acpi_ev);
  2833. break;
  2834. case 2:
  2835. /* 0x2000-0x2FFF: Wakeup reason */
  2836. known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
  2837. &ignore_acpi_ev);
  2838. break;
  2839. case 3:
  2840. /* 0x3000-0x3FFF: bay-related wakeups */
  2841. if (hkey == 0x3003) {
  2842. hotkey_autosleep_ack = 1;
  2843. printk(TPACPI_INFO
  2844. "bay ejected\n");
  2845. hotkey_wakeup_hotunplug_complete_notify_change();
  2846. known_ev = true;
  2847. } else {
  2848. known_ev = false;
  2849. }
  2850. break;
  2851. case 4:
  2852. /* 0x4000-0x4FFF: dock-related wakeups */
  2853. if (hkey == 0x4003) {
  2854. hotkey_autosleep_ack = 1;
  2855. printk(TPACPI_INFO
  2856. "undocked\n");
  2857. hotkey_wakeup_hotunplug_complete_notify_change();
  2858. known_ev = true;
  2859. } else {
  2860. known_ev = false;
  2861. }
  2862. break;
  2863. case 5:
  2864. /* 0x5000-0x5FFF: human interface helpers */
  2865. known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
  2866. &ignore_acpi_ev);
  2867. break;
  2868. case 6:
  2869. /* 0x6000-0x6FFF: thermal alarms */
  2870. known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
  2871. &ignore_acpi_ev);
  2872. break;
  2873. case 7:
  2874. /* 0x7000-0x7FFF: misc */
  2875. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  2876. tpacpi_send_radiosw_update();
  2877. send_acpi_ev = 0;
  2878. known_ev = true;
  2879. break;
  2880. }
  2881. /* fallthrough to default */
  2882. default:
  2883. known_ev = false;
  2884. }
  2885. if (!known_ev) {
  2886. printk(TPACPI_NOTICE
  2887. "unhandled HKEY event 0x%04x\n", hkey);
  2888. printk(TPACPI_NOTICE
  2889. "please report the conditions when this "
  2890. "event happened to %s\n", TPACPI_MAIL);
  2891. }
  2892. /* Legacy events */
  2893. if (!ignore_acpi_ev &&
  2894. (send_acpi_ev || hotkey_report_mode < 2)) {
  2895. acpi_bus_generate_proc_event(ibm->acpi->device,
  2896. event, hkey);
  2897. }
  2898. /* netlink events */
  2899. if (!ignore_acpi_ev && send_acpi_ev) {
  2900. acpi_bus_generate_netlink_event(
  2901. ibm->acpi->device->pnp.device_class,
  2902. dev_name(&ibm->acpi->device->dev),
  2903. event, hkey);
  2904. }
  2905. }
  2906. }
  2907. static void hotkey_suspend(pm_message_t state)
  2908. {
  2909. /* Do these on suspend, we get the events on early resume! */
  2910. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  2911. hotkey_autosleep_ack = 0;
  2912. }
  2913. static void hotkey_resume(void)
  2914. {
  2915. tpacpi_disable_brightness_delay();
  2916. if (hotkey_mask_get())
  2917. printk(TPACPI_ERR
  2918. "error while trying to read hot key mask "
  2919. "from firmware\n");
  2920. tpacpi_send_radiosw_update();
  2921. hotkey_tablet_mode_notify_change();
  2922. hotkey_wakeup_reason_notify_change();
  2923. hotkey_wakeup_hotunplug_complete_notify_change();
  2924. hotkey_poll_setup_safe(false);
  2925. }
  2926. /* procfs -------------------------------------------------------------- */
  2927. static int hotkey_read(char *p)
  2928. {
  2929. int res, status;
  2930. int len = 0;
  2931. if (!tp_features.hotkey) {
  2932. len += sprintf(p + len, "status:\t\tnot supported\n");
  2933. return len;
  2934. }
  2935. if (mutex_lock_killable(&hotkey_mutex))
  2936. return -ERESTARTSYS;
  2937. res = hotkey_status_get(&status);
  2938. if (!res)
  2939. res = hotkey_mask_get();
  2940. mutex_unlock(&hotkey_mutex);
  2941. if (res)
  2942. return res;
  2943. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  2944. if (tp_features.hotkey_mask) {
  2945. len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
  2946. len += sprintf(p + len,
  2947. "commands:\tenable, disable, reset, <mask>\n");
  2948. } else {
  2949. len += sprintf(p + len, "mask:\t\tnot supported\n");
  2950. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  2951. }
  2952. return len;
  2953. }
  2954. static void hotkey_enabledisable_warn(bool enable)
  2955. {
  2956. tpacpi_log_usertask("procfs hotkey enable/disable");
  2957. if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
  2958. TPACPI_WARN
  2959. "hotkey enable/disable functionality has been "
  2960. "removed from the driver. Hotkeys are always "
  2961. "enabled\n"))
  2962. printk(TPACPI_ERR
  2963. "Please remove the hotkey=enable module "
  2964. "parameter, it is deprecated. Hotkeys are always "
  2965. "enabled\n");
  2966. }
  2967. static int hotkey_write(char *buf)
  2968. {
  2969. int res;
  2970. u32 mask;
  2971. char *cmd;
  2972. if (!tp_features.hotkey)
  2973. return -ENODEV;
  2974. if (mutex_lock_killable(&hotkey_mutex))
  2975. return -ERESTARTSYS;
  2976. mask = hotkey_mask;
  2977. res = 0;
  2978. while ((cmd = next_cmd(&buf))) {
  2979. if (strlencmp(cmd, "enable") == 0) {
  2980. hotkey_enabledisable_warn(1);
  2981. } else if (strlencmp(cmd, "disable") == 0) {
  2982. hotkey_enabledisable_warn(0);
  2983. res = -EPERM;
  2984. } else if (strlencmp(cmd, "reset") == 0) {
  2985. mask = (hotkey_all_mask | hotkey_source_mask)
  2986. & ~hotkey_reserved_mask;
  2987. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  2988. /* mask set */
  2989. } else if (sscanf(cmd, "%x", &mask) == 1) {
  2990. /* mask set */
  2991. } else {
  2992. res = -EINVAL;
  2993. goto errexit;
  2994. }
  2995. }
  2996. if (!res)
  2997. tpacpi_disclose_usertask("procfs hotkey",
  2998. "set mask to 0x%08x\n", mask);
  2999. if (!res && mask != hotkey_mask)
  3000. res = hotkey_mask_set(mask);
  3001. errexit:
  3002. mutex_unlock(&hotkey_mutex);
  3003. return res;
  3004. }
  3005. static const struct acpi_device_id ibm_htk_device_ids[] = {
  3006. {TPACPI_ACPI_HKEY_HID, 0},
  3007. {"", 0},
  3008. };
  3009. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  3010. .hid = ibm_htk_device_ids,
  3011. .notify = hotkey_notify,
  3012. .handle = &hkey_handle,
  3013. .type = ACPI_DEVICE_NOTIFY,
  3014. };
  3015. static struct ibm_struct hotkey_driver_data = {
  3016. .name = "hotkey",
  3017. .read = hotkey_read,
  3018. .write = hotkey_write,
  3019. .exit = hotkey_exit,
  3020. .resume = hotkey_resume,
  3021. .suspend = hotkey_suspend,
  3022. .acpi = &ibm_hotkey_acpidriver,
  3023. };
  3024. /*************************************************************************
  3025. * Bluetooth subdriver
  3026. */
  3027. enum {
  3028. /* ACPI GBDC/SBDC bits */
  3029. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  3030. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  3031. TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
  3032. off / last state */
  3033. };
  3034. enum {
  3035. /* ACPI \BLTH commands */
  3036. TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
  3037. TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
  3038. TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
  3039. TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
  3040. TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
  3041. };
  3042. #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
  3043. static void bluetooth_suspend(pm_message_t state)
  3044. {
  3045. /* Try to make sure radio will resume powered off */
  3046. if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
  3047. TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
  3048. vdbg_printk(TPACPI_DBG_RFKILL,
  3049. "bluetooth power down on resume request failed\n");
  3050. }
  3051. static int bluetooth_get_status(void)
  3052. {
  3053. int status;
  3054. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3055. if (dbg_bluetoothemul)
  3056. return (tpacpi_bluetooth_emulstate) ?
  3057. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3058. #endif
  3059. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  3060. return -EIO;
  3061. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
  3062. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3063. }
  3064. static int bluetooth_set_status(enum tpacpi_rfkill_state state)
  3065. {
  3066. int status;
  3067. vdbg_printk(TPACPI_DBG_RFKILL,
  3068. "will attempt to %s bluetooth\n",
  3069. (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
  3070. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3071. if (dbg_bluetoothemul) {
  3072. tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3073. return 0;
  3074. }
  3075. #endif
  3076. /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
  3077. if (state == TPACPI_RFK_RADIO_ON)
  3078. status = TP_ACPI_BLUETOOTH_RADIOSSW;
  3079. else
  3080. status = 0;
  3081. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  3082. return -EIO;
  3083. return 0;
  3084. }
  3085. /* sysfs bluetooth enable ---------------------------------------------- */
  3086. static ssize_t bluetooth_enable_show(struct device *dev,
  3087. struct device_attribute *attr,
  3088. char *buf)
  3089. {
  3090. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
  3091. attr, buf);
  3092. }
  3093. static ssize_t bluetooth_enable_store(struct device *dev,
  3094. struct device_attribute *attr,
  3095. const char *buf, size_t count)
  3096. {
  3097. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
  3098. attr, buf, count);
  3099. }
  3100. static struct device_attribute dev_attr_bluetooth_enable =
  3101. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  3102. bluetooth_enable_show, bluetooth_enable_store);
  3103. /* --------------------------------------------------------------------- */
  3104. static struct attribute *bluetooth_attributes[] = {
  3105. &dev_attr_bluetooth_enable.attr,
  3106. NULL
  3107. };
  3108. static const struct attribute_group bluetooth_attr_group = {
  3109. .attrs = bluetooth_attributes,
  3110. };
  3111. static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
  3112. .get_status = bluetooth_get_status,
  3113. .set_status = bluetooth_set_status,
  3114. };
  3115. static void bluetooth_shutdown(void)
  3116. {
  3117. /* Order firmware to save current state to NVRAM */
  3118. if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
  3119. TP_ACPI_BLTH_SAVE_STATE))
  3120. printk(TPACPI_NOTICE
  3121. "failed to save bluetooth state to NVRAM\n");
  3122. else
  3123. vdbg_printk(TPACPI_DBG_RFKILL,
  3124. "bluestooth state saved to NVRAM\n");
  3125. }
  3126. static void bluetooth_exit(void)
  3127. {
  3128. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  3129. &bluetooth_attr_group);
  3130. tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
  3131. bluetooth_shutdown();
  3132. }
  3133. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  3134. {
  3135. int res;
  3136. int status = 0;
  3137. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3138. "initializing bluetooth subdriver\n");
  3139. TPACPI_ACPIHANDLE_INIT(hkey);
  3140. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  3141. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  3142. tp_features.bluetooth = hkey_handle &&
  3143. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  3144. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3145. "bluetooth is %s, status 0x%02x\n",
  3146. str_supported(tp_features.bluetooth),
  3147. status);
  3148. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3149. if (dbg_bluetoothemul) {
  3150. tp_features.bluetooth = 1;
  3151. printk(TPACPI_INFO
  3152. "bluetooth switch emulation enabled\n");
  3153. } else
  3154. #endif
  3155. if (tp_features.bluetooth &&
  3156. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  3157. /* no bluetooth hardware present in system */
  3158. tp_features.bluetooth = 0;
  3159. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3160. "bluetooth hardware not installed\n");
  3161. }
  3162. if (!tp_features.bluetooth)
  3163. return 1;
  3164. res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
  3165. &bluetooth_tprfk_ops,
  3166. RFKILL_TYPE_BLUETOOTH,
  3167. TPACPI_RFK_BLUETOOTH_SW_NAME,
  3168. true);
  3169. if (res)
  3170. return res;
  3171. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  3172. &bluetooth_attr_group);
  3173. if (res) {
  3174. tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
  3175. return res;
  3176. }
  3177. return 0;
  3178. }
  3179. /* procfs -------------------------------------------------------------- */
  3180. static int bluetooth_read(char *p)
  3181. {
  3182. return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
  3183. }
  3184. static int bluetooth_write(char *buf)
  3185. {
  3186. return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
  3187. }
  3188. static struct ibm_struct bluetooth_driver_data = {
  3189. .name = "bluetooth",
  3190. .read = bluetooth_read,
  3191. .write = bluetooth_write,
  3192. .exit = bluetooth_exit,
  3193. .suspend = bluetooth_suspend,
  3194. .shutdown = bluetooth_shutdown,
  3195. };
  3196. /*************************************************************************
  3197. * Wan subdriver
  3198. */
  3199. enum {
  3200. /* ACPI GWAN/SWAN bits */
  3201. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  3202. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  3203. TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
  3204. off / last state */
  3205. };
  3206. #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
  3207. static void wan_suspend(pm_message_t state)
  3208. {
  3209. /* Try to make sure radio will resume powered off */
  3210. if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
  3211. TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
  3212. vdbg_printk(TPACPI_DBG_RFKILL,
  3213. "WWAN power down on resume request failed\n");
  3214. }
  3215. static int wan_get_status(void)
  3216. {
  3217. int status;
  3218. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3219. if (dbg_wwanemul)
  3220. return (tpacpi_wwan_emulstate) ?
  3221. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3222. #endif
  3223. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  3224. return -EIO;
  3225. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
  3226. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3227. }
  3228. static int wan_set_status(enum tpacpi_rfkill_state state)
  3229. {
  3230. int status;
  3231. vdbg_printk(TPACPI_DBG_RFKILL,
  3232. "will attempt to %s wwan\n",
  3233. (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
  3234. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3235. if (dbg_wwanemul) {
  3236. tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3237. return 0;
  3238. }
  3239. #endif
  3240. /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
  3241. if (state == TPACPI_RFK_RADIO_ON)
  3242. status = TP_ACPI_WANCARD_RADIOSSW;
  3243. else
  3244. status = 0;
  3245. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  3246. return -EIO;
  3247. return 0;
  3248. }
  3249. /* sysfs wan enable ---------------------------------------------------- */
  3250. static ssize_t wan_enable_show(struct device *dev,
  3251. struct device_attribute *attr,
  3252. char *buf)
  3253. {
  3254. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
  3255. attr, buf);
  3256. }
  3257. static ssize_t wan_enable_store(struct device *dev,
  3258. struct device_attribute *attr,
  3259. const char *buf, size_t count)
  3260. {
  3261. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
  3262. attr, buf, count);
  3263. }
  3264. static struct device_attribute dev_attr_wan_enable =
  3265. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  3266. wan_enable_show, wan_enable_store);
  3267. /* --------------------------------------------------------------------- */
  3268. static struct attribute *wan_attributes[] = {
  3269. &dev_attr_wan_enable.attr,
  3270. NULL
  3271. };
  3272. static const struct attribute_group wan_attr_group = {
  3273. .attrs = wan_attributes,
  3274. };
  3275. static const struct tpacpi_rfk_ops wan_tprfk_ops = {
  3276. .get_status = wan_get_status,
  3277. .set_status = wan_set_status,
  3278. };
  3279. static void wan_shutdown(void)
  3280. {
  3281. /* Order firmware to save current state to NVRAM */
  3282. if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
  3283. TP_ACPI_WGSV_SAVE_STATE))
  3284. printk(TPACPI_NOTICE
  3285. "failed to save WWAN state to NVRAM\n");
  3286. else
  3287. vdbg_printk(TPACPI_DBG_RFKILL,
  3288. "WWAN state saved to NVRAM\n");
  3289. }
  3290. static void wan_exit(void)
  3291. {
  3292. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  3293. &wan_attr_group);
  3294. tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
  3295. wan_shutdown();
  3296. }
  3297. static int __init wan_init(struct ibm_init_struct *iibm)
  3298. {
  3299. int res;
  3300. int status = 0;
  3301. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3302. "initializing wan subdriver\n");
  3303. TPACPI_ACPIHANDLE_INIT(hkey);
  3304. tp_features.wan = hkey_handle &&
  3305. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  3306. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3307. "wan is %s, status 0x%02x\n",
  3308. str_supported(tp_features.wan),
  3309. status);
  3310. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3311. if (dbg_wwanemul) {
  3312. tp_features.wan = 1;
  3313. printk(TPACPI_INFO
  3314. "wwan switch emulation enabled\n");
  3315. } else
  3316. #endif
  3317. if (tp_features.wan &&
  3318. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  3319. /* no wan hardware present in system */
  3320. tp_features.wan = 0;
  3321. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3322. "wan hardware not installed\n");
  3323. }
  3324. if (!tp_features.wan)
  3325. return 1;
  3326. res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
  3327. &wan_tprfk_ops,
  3328. RFKILL_TYPE_WWAN,
  3329. TPACPI_RFK_WWAN_SW_NAME,
  3330. true);
  3331. if (res)
  3332. return res;
  3333. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  3334. &wan_attr_group);
  3335. if (res) {
  3336. tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
  3337. return res;
  3338. }
  3339. return 0;
  3340. }
  3341. /* procfs -------------------------------------------------------------- */
  3342. static int wan_read(char *p)
  3343. {
  3344. return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
  3345. }
  3346. static int wan_write(char *buf)
  3347. {
  3348. return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
  3349. }
  3350. static struct ibm_struct wan_driver_data = {
  3351. .name = "wan",
  3352. .read = wan_read,
  3353. .write = wan_write,
  3354. .exit = wan_exit,
  3355. .suspend = wan_suspend,
  3356. .shutdown = wan_shutdown,
  3357. };
  3358. /*************************************************************************
  3359. * UWB subdriver
  3360. */
  3361. enum {
  3362. /* ACPI GUWB/SUWB bits */
  3363. TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
  3364. TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
  3365. };
  3366. #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
  3367. static int uwb_get_status(void)
  3368. {
  3369. int status;
  3370. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3371. if (dbg_uwbemul)
  3372. return (tpacpi_uwb_emulstate) ?
  3373. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3374. #endif
  3375. if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
  3376. return -EIO;
  3377. return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
  3378. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3379. }
  3380. static int uwb_set_status(enum tpacpi_rfkill_state state)
  3381. {
  3382. int status;
  3383. vdbg_printk(TPACPI_DBG_RFKILL,
  3384. "will attempt to %s UWB\n",
  3385. (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
  3386. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3387. if (dbg_uwbemul) {
  3388. tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3389. return 0;
  3390. }
  3391. #endif
  3392. if (state == TPACPI_RFK_RADIO_ON)
  3393. status = TP_ACPI_UWB_RADIOSSW;
  3394. else
  3395. status = 0;
  3396. if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
  3397. return -EIO;
  3398. return 0;
  3399. }
  3400. /* --------------------------------------------------------------------- */
  3401. static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
  3402. .get_status = uwb_get_status,
  3403. .set_status = uwb_set_status,
  3404. };
  3405. static void uwb_exit(void)
  3406. {
  3407. tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
  3408. }
  3409. static int __init uwb_init(struct ibm_init_struct *iibm)
  3410. {
  3411. int res;
  3412. int status = 0;
  3413. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3414. "initializing uwb subdriver\n");
  3415. TPACPI_ACPIHANDLE_INIT(hkey);
  3416. tp_features.uwb = hkey_handle &&
  3417. acpi_evalf(hkey_handle, &status, "GUWB", "qd");
  3418. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3419. "uwb is %s, status 0x%02x\n",
  3420. str_supported(tp_features.uwb),
  3421. status);
  3422. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3423. if (dbg_uwbemul) {
  3424. tp_features.uwb = 1;
  3425. printk(TPACPI_INFO
  3426. "uwb switch emulation enabled\n");
  3427. } else
  3428. #endif
  3429. if (tp_features.uwb &&
  3430. !(status & TP_ACPI_UWB_HWPRESENT)) {
  3431. /* no uwb hardware present in system */
  3432. tp_features.uwb = 0;
  3433. dbg_printk(TPACPI_DBG_INIT,
  3434. "uwb hardware not installed\n");
  3435. }
  3436. if (!tp_features.uwb)
  3437. return 1;
  3438. res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
  3439. &uwb_tprfk_ops,
  3440. RFKILL_TYPE_UWB,
  3441. TPACPI_RFK_UWB_SW_NAME,
  3442. false);
  3443. return res;
  3444. }
  3445. static struct ibm_struct uwb_driver_data = {
  3446. .name = "uwb",
  3447. .exit = uwb_exit,
  3448. .flags.experimental = 1,
  3449. };
  3450. /*************************************************************************
  3451. * Video subdriver
  3452. */
  3453. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  3454. enum video_access_mode {
  3455. TPACPI_VIDEO_NONE = 0,
  3456. TPACPI_VIDEO_570, /* 570 */
  3457. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  3458. TPACPI_VIDEO_NEW, /* all others */
  3459. };
  3460. enum { /* video status flags, based on VIDEO_570 */
  3461. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  3462. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  3463. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  3464. };
  3465. enum { /* TPACPI_VIDEO_570 constants */
  3466. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  3467. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  3468. * video_status_flags */
  3469. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  3470. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  3471. };
  3472. static enum video_access_mode video_supported;
  3473. static int video_orig_autosw;
  3474. static int video_autosw_get(void);
  3475. static int video_autosw_set(int enable);
  3476. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  3477. static int __init video_init(struct ibm_init_struct *iibm)
  3478. {
  3479. int ivga;
  3480. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  3481. TPACPI_ACPIHANDLE_INIT(vid);
  3482. TPACPI_ACPIHANDLE_INIT(vid2);
  3483. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  3484. /* G41, assume IVGA doesn't change */
  3485. vid_handle = vid2_handle;
  3486. if (!vid_handle)
  3487. /* video switching not supported on R30, R31 */
  3488. video_supported = TPACPI_VIDEO_NONE;
  3489. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  3490. /* 570 */
  3491. video_supported = TPACPI_VIDEO_570;
  3492. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  3493. /* 600e/x, 770e, 770x */
  3494. video_supported = TPACPI_VIDEO_770;
  3495. else
  3496. /* all others */
  3497. video_supported = TPACPI_VIDEO_NEW;
  3498. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  3499. str_supported(video_supported != TPACPI_VIDEO_NONE),
  3500. video_supported);
  3501. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  3502. }
  3503. static void video_exit(void)
  3504. {
  3505. dbg_printk(TPACPI_DBG_EXIT,
  3506. "restoring original video autoswitch mode\n");
  3507. if (video_autosw_set(video_orig_autosw))
  3508. printk(TPACPI_ERR "error while trying to restore original "
  3509. "video autoswitch mode\n");
  3510. }
  3511. static int video_outputsw_get(void)
  3512. {
  3513. int status = 0;
  3514. int i;
  3515. switch (video_supported) {
  3516. case TPACPI_VIDEO_570:
  3517. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  3518. TP_ACPI_VIDEO_570_PHSCMD))
  3519. return -EIO;
  3520. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  3521. break;
  3522. case TPACPI_VIDEO_770:
  3523. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  3524. return -EIO;
  3525. if (i)
  3526. status |= TP_ACPI_VIDEO_S_LCD;
  3527. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  3528. return -EIO;
  3529. if (i)
  3530. status |= TP_ACPI_VIDEO_S_CRT;
  3531. break;
  3532. case TPACPI_VIDEO_NEW:
  3533. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  3534. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  3535. return -EIO;
  3536. if (i)
  3537. status |= TP_ACPI_VIDEO_S_CRT;
  3538. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  3539. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  3540. return -EIO;
  3541. if (i)
  3542. status |= TP_ACPI_VIDEO_S_LCD;
  3543. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  3544. return -EIO;
  3545. if (i)
  3546. status |= TP_ACPI_VIDEO_S_DVI;
  3547. break;
  3548. default:
  3549. return -ENOSYS;
  3550. }
  3551. return status;
  3552. }
  3553. static int video_outputsw_set(int status)
  3554. {
  3555. int autosw;
  3556. int res = 0;
  3557. switch (video_supported) {
  3558. case TPACPI_VIDEO_570:
  3559. res = acpi_evalf(NULL, NULL,
  3560. "\\_SB.PHS2", "vdd",
  3561. TP_ACPI_VIDEO_570_PHS2CMD,
  3562. status | TP_ACPI_VIDEO_570_PHS2SET);
  3563. break;
  3564. case TPACPI_VIDEO_770:
  3565. autosw = video_autosw_get();
  3566. if (autosw < 0)
  3567. return autosw;
  3568. res = video_autosw_set(1);
  3569. if (res)
  3570. return res;
  3571. res = acpi_evalf(vid_handle, NULL,
  3572. "ASWT", "vdd", status * 0x100, 0);
  3573. if (!autosw && video_autosw_set(autosw)) {
  3574. printk(TPACPI_ERR
  3575. "video auto-switch left enabled due to error\n");
  3576. return -EIO;
  3577. }
  3578. break;
  3579. case TPACPI_VIDEO_NEW:
  3580. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  3581. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  3582. break;
  3583. default:
  3584. return -ENOSYS;
  3585. }
  3586. return (res)? 0 : -EIO;
  3587. }
  3588. static int video_autosw_get(void)
  3589. {
  3590. int autosw = 0;
  3591. switch (video_supported) {
  3592. case TPACPI_VIDEO_570:
  3593. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  3594. return -EIO;
  3595. break;
  3596. case TPACPI_VIDEO_770:
  3597. case TPACPI_VIDEO_NEW:
  3598. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  3599. return -EIO;
  3600. break;
  3601. default:
  3602. return -ENOSYS;
  3603. }
  3604. return autosw & 1;
  3605. }
  3606. static int video_autosw_set(int enable)
  3607. {
  3608. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  3609. return -EIO;
  3610. return 0;
  3611. }
  3612. static int video_outputsw_cycle(void)
  3613. {
  3614. int autosw = video_autosw_get();
  3615. int res;
  3616. if (autosw < 0)
  3617. return autosw;
  3618. switch (video_supported) {
  3619. case TPACPI_VIDEO_570:
  3620. res = video_autosw_set(1);
  3621. if (res)
  3622. return res;
  3623. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  3624. break;
  3625. case TPACPI_VIDEO_770:
  3626. case TPACPI_VIDEO_NEW:
  3627. res = video_autosw_set(1);
  3628. if (res)
  3629. return res;
  3630. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  3631. break;
  3632. default:
  3633. return -ENOSYS;
  3634. }
  3635. if (!autosw && video_autosw_set(autosw)) {
  3636. printk(TPACPI_ERR
  3637. "video auto-switch left enabled due to error\n");
  3638. return -EIO;
  3639. }
  3640. return (res)? 0 : -EIO;
  3641. }
  3642. static int video_expand_toggle(void)
  3643. {
  3644. switch (video_supported) {
  3645. case TPACPI_VIDEO_570:
  3646. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  3647. 0 : -EIO;
  3648. case TPACPI_VIDEO_770:
  3649. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  3650. 0 : -EIO;
  3651. case TPACPI_VIDEO_NEW:
  3652. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  3653. 0 : -EIO;
  3654. default:
  3655. return -ENOSYS;
  3656. }
  3657. /* not reached */
  3658. }
  3659. static int video_read(char *p)
  3660. {
  3661. int status, autosw;
  3662. int len = 0;
  3663. if (video_supported == TPACPI_VIDEO_NONE) {
  3664. len += sprintf(p + len, "status:\t\tnot supported\n");
  3665. return len;
  3666. }
  3667. status = video_outputsw_get();
  3668. if (status < 0)
  3669. return status;
  3670. autosw = video_autosw_get();
  3671. if (autosw < 0)
  3672. return autosw;
  3673. len += sprintf(p + len, "status:\t\tsupported\n");
  3674. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  3675. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  3676. if (video_supported == TPACPI_VIDEO_NEW)
  3677. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  3678. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  3679. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  3680. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  3681. if (video_supported == TPACPI_VIDEO_NEW)
  3682. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  3683. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  3684. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  3685. return len;
  3686. }
  3687. static int video_write(char *buf)
  3688. {
  3689. char *cmd;
  3690. int enable, disable, status;
  3691. int res;
  3692. if (video_supported == TPACPI_VIDEO_NONE)
  3693. return -ENODEV;
  3694. enable = 0;
  3695. disable = 0;
  3696. while ((cmd = next_cmd(&buf))) {
  3697. if (strlencmp(cmd, "lcd_enable") == 0) {
  3698. enable |= TP_ACPI_VIDEO_S_LCD;
  3699. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  3700. disable |= TP_ACPI_VIDEO_S_LCD;
  3701. } else if (strlencmp(cmd, "crt_enable") == 0) {
  3702. enable |= TP_ACPI_VIDEO_S_CRT;
  3703. } else if (strlencmp(cmd, "crt_disable") == 0) {
  3704. disable |= TP_ACPI_VIDEO_S_CRT;
  3705. } else if (video_supported == TPACPI_VIDEO_NEW &&
  3706. strlencmp(cmd, "dvi_enable") == 0) {
  3707. enable |= TP_ACPI_VIDEO_S_DVI;
  3708. } else if (video_supported == TPACPI_VIDEO_NEW &&
  3709. strlencmp(cmd, "dvi_disable") == 0) {
  3710. disable |= TP_ACPI_VIDEO_S_DVI;
  3711. } else if (strlencmp(cmd, "auto_enable") == 0) {
  3712. res = video_autosw_set(1);
  3713. if (res)
  3714. return res;
  3715. } else if (strlencmp(cmd, "auto_disable") == 0) {
  3716. res = video_autosw_set(0);
  3717. if (res)
  3718. return res;
  3719. } else if (strlencmp(cmd, "video_switch") == 0) {
  3720. res = video_outputsw_cycle();
  3721. if (res)
  3722. return res;
  3723. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  3724. res = video_expand_toggle();
  3725. if (res)
  3726. return res;
  3727. } else
  3728. return -EINVAL;
  3729. }
  3730. if (enable || disable) {
  3731. status = video_outputsw_get();
  3732. if (status < 0)
  3733. return status;
  3734. res = video_outputsw_set((status & ~disable) | enable);
  3735. if (res)
  3736. return res;
  3737. }
  3738. return 0;
  3739. }
  3740. static struct ibm_struct video_driver_data = {
  3741. .name = "video",
  3742. .read = video_read,
  3743. .write = video_write,
  3744. .exit = video_exit,
  3745. };
  3746. #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
  3747. /*************************************************************************
  3748. * Light (thinklight) subdriver
  3749. */
  3750. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  3751. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  3752. static int light_get_status(void)
  3753. {
  3754. int status = 0;
  3755. if (tp_features.light_status) {
  3756. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  3757. return -EIO;
  3758. return (!!status);
  3759. }
  3760. return -ENXIO;
  3761. }
  3762. static int light_set_status(int status)
  3763. {
  3764. int rc;
  3765. if (tp_features.light) {
  3766. if (cmos_handle) {
  3767. rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
  3768. (status)?
  3769. TP_CMOS_THINKLIGHT_ON :
  3770. TP_CMOS_THINKLIGHT_OFF);
  3771. } else {
  3772. rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
  3773. (status)? 1 : 0);
  3774. }
  3775. return (rc)? 0 : -EIO;
  3776. }
  3777. return -ENXIO;
  3778. }
  3779. static void light_set_status_worker(struct work_struct *work)
  3780. {
  3781. struct tpacpi_led_classdev *data =
  3782. container_of(work, struct tpacpi_led_classdev, work);
  3783. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  3784. light_set_status((data->new_state != TPACPI_LED_OFF));
  3785. }
  3786. static void light_sysfs_set(struct led_classdev *led_cdev,
  3787. enum led_brightness brightness)
  3788. {
  3789. struct tpacpi_led_classdev *data =
  3790. container_of(led_cdev,
  3791. struct tpacpi_led_classdev,
  3792. led_classdev);
  3793. data->new_state = (brightness != LED_OFF) ?
  3794. TPACPI_LED_ON : TPACPI_LED_OFF;
  3795. queue_work(tpacpi_wq, &data->work);
  3796. }
  3797. static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
  3798. {
  3799. return (light_get_status() == 1)? LED_FULL : LED_OFF;
  3800. }
  3801. static struct tpacpi_led_classdev tpacpi_led_thinklight = {
  3802. .led_classdev = {
  3803. .name = "tpacpi::thinklight",
  3804. .brightness_set = &light_sysfs_set,
  3805. .brightness_get = &light_sysfs_get,
  3806. }
  3807. };
  3808. static int __init light_init(struct ibm_init_struct *iibm)
  3809. {
  3810. int rc;
  3811. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  3812. TPACPI_ACPIHANDLE_INIT(ledb);
  3813. TPACPI_ACPIHANDLE_INIT(lght);
  3814. TPACPI_ACPIHANDLE_INIT(cmos);
  3815. INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
  3816. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  3817. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  3818. if (tp_features.light)
  3819. /* light status not supported on
  3820. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  3821. tp_features.light_status =
  3822. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  3823. vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
  3824. str_supported(tp_features.light),
  3825. str_supported(tp_features.light_status));
  3826. if (!tp_features.light)
  3827. return 1;
  3828. rc = led_classdev_register(&tpacpi_pdev->dev,
  3829. &tpacpi_led_thinklight.led_classdev);
  3830. if (rc < 0) {
  3831. tp_features.light = 0;
  3832. tp_features.light_status = 0;
  3833. } else {
  3834. rc = 0;
  3835. }
  3836. return rc;
  3837. }
  3838. static void light_exit(void)
  3839. {
  3840. led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
  3841. if (work_pending(&tpacpi_led_thinklight.work))
  3842. flush_workqueue(tpacpi_wq);
  3843. }
  3844. static int light_read(char *p)
  3845. {
  3846. int len = 0;
  3847. int status;
  3848. if (!tp_features.light) {
  3849. len += sprintf(p + len, "status:\t\tnot supported\n");
  3850. } else if (!tp_features.light_status) {
  3851. len += sprintf(p + len, "status:\t\tunknown\n");
  3852. len += sprintf(p + len, "commands:\ton, off\n");
  3853. } else {
  3854. status = light_get_status();
  3855. if (status < 0)
  3856. return status;
  3857. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  3858. len += sprintf(p + len, "commands:\ton, off\n");
  3859. }
  3860. return len;
  3861. }
  3862. static int light_write(char *buf)
  3863. {
  3864. char *cmd;
  3865. int newstatus = 0;
  3866. if (!tp_features.light)
  3867. return -ENODEV;
  3868. while ((cmd = next_cmd(&buf))) {
  3869. if (strlencmp(cmd, "on") == 0) {
  3870. newstatus = 1;
  3871. } else if (strlencmp(cmd, "off") == 0) {
  3872. newstatus = 0;
  3873. } else
  3874. return -EINVAL;
  3875. }
  3876. return light_set_status(newstatus);
  3877. }
  3878. static struct ibm_struct light_driver_data = {
  3879. .name = "light",
  3880. .read = light_read,
  3881. .write = light_write,
  3882. .exit = light_exit,
  3883. };
  3884. /*************************************************************************
  3885. * CMOS subdriver
  3886. */
  3887. /* sysfs cmos_command -------------------------------------------------- */
  3888. static ssize_t cmos_command_store(struct device *dev,
  3889. struct device_attribute *attr,
  3890. const char *buf, size_t count)
  3891. {
  3892. unsigned long cmos_cmd;
  3893. int res;
  3894. if (parse_strtoul(buf, 21, &cmos_cmd))
  3895. return -EINVAL;
  3896. res = issue_thinkpad_cmos_command(cmos_cmd);
  3897. return (res)? res : count;
  3898. }
  3899. static struct device_attribute dev_attr_cmos_command =
  3900. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  3901. /* --------------------------------------------------------------------- */
  3902. static int __init cmos_init(struct ibm_init_struct *iibm)
  3903. {
  3904. int res;
  3905. vdbg_printk(TPACPI_DBG_INIT,
  3906. "initializing cmos commands subdriver\n");
  3907. TPACPI_ACPIHANDLE_INIT(cmos);
  3908. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  3909. str_supported(cmos_handle != NULL));
  3910. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  3911. if (res)
  3912. return res;
  3913. return (cmos_handle)? 0 : 1;
  3914. }
  3915. static void cmos_exit(void)
  3916. {
  3917. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  3918. }
  3919. static int cmos_read(char *p)
  3920. {
  3921. int len = 0;
  3922. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  3923. R30, R31, T20-22, X20-21 */
  3924. if (!cmos_handle)
  3925. len += sprintf(p + len, "status:\t\tnot supported\n");
  3926. else {
  3927. len += sprintf(p + len, "status:\t\tsupported\n");
  3928. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  3929. }
  3930. return len;
  3931. }
  3932. static int cmos_write(char *buf)
  3933. {
  3934. char *cmd;
  3935. int cmos_cmd, res;
  3936. while ((cmd = next_cmd(&buf))) {
  3937. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  3938. cmos_cmd >= 0 && cmos_cmd <= 21) {
  3939. /* cmos_cmd set */
  3940. } else
  3941. return -EINVAL;
  3942. res = issue_thinkpad_cmos_command(cmos_cmd);
  3943. if (res)
  3944. return res;
  3945. }
  3946. return 0;
  3947. }
  3948. static struct ibm_struct cmos_driver_data = {
  3949. .name = "cmos",
  3950. .read = cmos_read,
  3951. .write = cmos_write,
  3952. .exit = cmos_exit,
  3953. };
  3954. /*************************************************************************
  3955. * LED subdriver
  3956. */
  3957. enum led_access_mode {
  3958. TPACPI_LED_NONE = 0,
  3959. TPACPI_LED_570, /* 570 */
  3960. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  3961. TPACPI_LED_NEW, /* all others */
  3962. };
  3963. enum { /* For TPACPI_LED_OLD */
  3964. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  3965. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  3966. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  3967. };
  3968. static enum led_access_mode led_supported;
  3969. TPACPI_HANDLE(led, ec, "SLED", /* 570 */
  3970. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
  3971. /* T20-22, X20-21 */
  3972. "LED", /* all others */
  3973. ); /* R30, R31 */
  3974. #define TPACPI_LED_NUMLEDS 16
  3975. static struct tpacpi_led_classdev *tpacpi_leds;
  3976. static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
  3977. static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
  3978. /* there's a limit of 19 chars + NULL before 2.6.26 */
  3979. "tpacpi::power",
  3980. "tpacpi:orange:batt",
  3981. "tpacpi:green:batt",
  3982. "tpacpi::dock_active",
  3983. "tpacpi::bay_active",
  3984. "tpacpi::dock_batt",
  3985. "tpacpi::unknown_led",
  3986. "tpacpi::standby",
  3987. "tpacpi::dock_status1",
  3988. "tpacpi::dock_status2",
  3989. "tpacpi::unknown_led2",
  3990. "tpacpi::unknown_led3",
  3991. "tpacpi::thinkvantage",
  3992. };
  3993. #define TPACPI_SAFE_LEDS 0x1081U
  3994. static inline bool tpacpi_is_led_restricted(const unsigned int led)
  3995. {
  3996. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  3997. return false;
  3998. #else
  3999. return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
  4000. #endif
  4001. }
  4002. static int led_get_status(const unsigned int led)
  4003. {
  4004. int status;
  4005. enum led_status_t led_s;
  4006. switch (led_supported) {
  4007. case TPACPI_LED_570:
  4008. if (!acpi_evalf(ec_handle,
  4009. &status, "GLED", "dd", 1 << led))
  4010. return -EIO;
  4011. led_s = (status == 0)?
  4012. TPACPI_LED_OFF :
  4013. ((status == 1)?
  4014. TPACPI_LED_ON :
  4015. TPACPI_LED_BLINK);
  4016. tpacpi_led_state_cache[led] = led_s;
  4017. return led_s;
  4018. default:
  4019. return -ENXIO;
  4020. }
  4021. /* not reached */
  4022. }
  4023. static int led_set_status(const unsigned int led,
  4024. const enum led_status_t ledstatus)
  4025. {
  4026. /* off, on, blink. Index is led_status_t */
  4027. static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
  4028. static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
  4029. int rc = 0;
  4030. switch (led_supported) {
  4031. case TPACPI_LED_570:
  4032. /* 570 */
  4033. if (unlikely(led > 7))
  4034. return -EINVAL;
  4035. if (unlikely(tpacpi_is_led_restricted(led)))
  4036. return -EPERM;
  4037. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4038. (1 << led), led_sled_arg1[ledstatus]))
  4039. rc = -EIO;
  4040. break;
  4041. case TPACPI_LED_OLD:
  4042. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  4043. if (unlikely(led > 7))
  4044. return -EINVAL;
  4045. if (unlikely(tpacpi_is_led_restricted(led)))
  4046. return -EPERM;
  4047. rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
  4048. if (rc >= 0)
  4049. rc = ec_write(TPACPI_LED_EC_HLBL,
  4050. (ledstatus == TPACPI_LED_BLINK) << led);
  4051. if (rc >= 0)
  4052. rc = ec_write(TPACPI_LED_EC_HLCL,
  4053. (ledstatus != TPACPI_LED_OFF) << led);
  4054. break;
  4055. case TPACPI_LED_NEW:
  4056. /* all others */
  4057. if (unlikely(led >= TPACPI_LED_NUMLEDS))
  4058. return -EINVAL;
  4059. if (unlikely(tpacpi_is_led_restricted(led)))
  4060. return -EPERM;
  4061. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4062. led, led_led_arg1[ledstatus]))
  4063. rc = -EIO;
  4064. break;
  4065. default:
  4066. rc = -ENXIO;
  4067. }
  4068. if (!rc)
  4069. tpacpi_led_state_cache[led] = ledstatus;
  4070. return rc;
  4071. }
  4072. static void led_set_status_worker(struct work_struct *work)
  4073. {
  4074. struct tpacpi_led_classdev *data =
  4075. container_of(work, struct tpacpi_led_classdev, work);
  4076. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  4077. led_set_status(data->led, data->new_state);
  4078. }
  4079. static void led_sysfs_set(struct led_classdev *led_cdev,
  4080. enum led_brightness brightness)
  4081. {
  4082. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4083. struct tpacpi_led_classdev, led_classdev);
  4084. if (brightness == LED_OFF)
  4085. data->new_state = TPACPI_LED_OFF;
  4086. else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
  4087. data->new_state = TPACPI_LED_ON;
  4088. else
  4089. data->new_state = TPACPI_LED_BLINK;
  4090. queue_work(tpacpi_wq, &data->work);
  4091. }
  4092. static int led_sysfs_blink_set(struct led_classdev *led_cdev,
  4093. unsigned long *delay_on, unsigned long *delay_off)
  4094. {
  4095. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4096. struct tpacpi_led_classdev, led_classdev);
  4097. /* Can we choose the flash rate? */
  4098. if (*delay_on == 0 && *delay_off == 0) {
  4099. /* yes. set them to the hardware blink rate (1 Hz) */
  4100. *delay_on = 500; /* ms */
  4101. *delay_off = 500; /* ms */
  4102. } else if ((*delay_on != 500) || (*delay_off != 500))
  4103. return -EINVAL;
  4104. data->new_state = TPACPI_LED_BLINK;
  4105. queue_work(tpacpi_wq, &data->work);
  4106. return 0;
  4107. }
  4108. static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
  4109. {
  4110. int rc;
  4111. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4112. struct tpacpi_led_classdev, led_classdev);
  4113. rc = led_get_status(data->led);
  4114. if (rc == TPACPI_LED_OFF || rc < 0)
  4115. rc = LED_OFF; /* no error handling in led class :( */
  4116. else
  4117. rc = LED_FULL;
  4118. return rc;
  4119. }
  4120. static void led_exit(void)
  4121. {
  4122. unsigned int i;
  4123. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  4124. if (tpacpi_leds[i].led_classdev.name)
  4125. led_classdev_unregister(&tpacpi_leds[i].led_classdev);
  4126. }
  4127. kfree(tpacpi_leds);
  4128. }
  4129. static int __init tpacpi_init_led(unsigned int led)
  4130. {
  4131. int rc;
  4132. tpacpi_leds[led].led = led;
  4133. /* LEDs with no name don't get registered */
  4134. if (!tpacpi_led_names[led])
  4135. return 0;
  4136. tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
  4137. tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
  4138. if (led_supported == TPACPI_LED_570)
  4139. tpacpi_leds[led].led_classdev.brightness_get =
  4140. &led_sysfs_get;
  4141. tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
  4142. INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
  4143. rc = led_classdev_register(&tpacpi_pdev->dev,
  4144. &tpacpi_leds[led].led_classdev);
  4145. if (rc < 0)
  4146. tpacpi_leds[led].led_classdev.name = NULL;
  4147. return rc;
  4148. }
  4149. static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
  4150. TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
  4151. TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
  4152. TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
  4153. TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
  4154. TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
  4155. TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
  4156. TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
  4157. TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
  4158. TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
  4159. TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
  4160. TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
  4161. TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
  4162. TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
  4163. TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
  4164. TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
  4165. TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
  4166. TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
  4167. TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
  4168. TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
  4169. TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
  4170. /* (1) - may have excess leds enabled on MSB */
  4171. /* Defaults (order matters, keep last, don't reorder!) */
  4172. { /* Lenovo */
  4173. .vendor = PCI_VENDOR_ID_LENOVO,
  4174. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  4175. .quirks = 0x1fffU,
  4176. },
  4177. { /* IBM ThinkPads with no EC version string */
  4178. .vendor = PCI_VENDOR_ID_IBM,
  4179. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
  4180. .quirks = 0x00ffU,
  4181. },
  4182. { /* IBM ThinkPads with EC version string */
  4183. .vendor = PCI_VENDOR_ID_IBM,
  4184. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  4185. .quirks = 0x00bfU,
  4186. },
  4187. };
  4188. #undef TPACPI_LEDQ_IBM
  4189. #undef TPACPI_LEDQ_LNV
  4190. static int __init led_init(struct ibm_init_struct *iibm)
  4191. {
  4192. unsigned int i;
  4193. int rc;
  4194. unsigned long useful_leds;
  4195. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  4196. TPACPI_ACPIHANDLE_INIT(led);
  4197. if (!led_handle)
  4198. /* led not supported on R30, R31 */
  4199. led_supported = TPACPI_LED_NONE;
  4200. else if (strlencmp(led_path, "SLED") == 0)
  4201. /* 570 */
  4202. led_supported = TPACPI_LED_570;
  4203. else if (strlencmp(led_path, "SYSL") == 0)
  4204. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  4205. led_supported = TPACPI_LED_OLD;
  4206. else
  4207. /* all others */
  4208. led_supported = TPACPI_LED_NEW;
  4209. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  4210. str_supported(led_supported), led_supported);
  4211. if (led_supported == TPACPI_LED_NONE)
  4212. return 1;
  4213. tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
  4214. GFP_KERNEL);
  4215. if (!tpacpi_leds) {
  4216. printk(TPACPI_ERR "Out of memory for LED data\n");
  4217. return -ENOMEM;
  4218. }
  4219. useful_leds = tpacpi_check_quirks(led_useful_qtable,
  4220. ARRAY_SIZE(led_useful_qtable));
  4221. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  4222. if (!tpacpi_is_led_restricted(i) &&
  4223. test_bit(i, &useful_leds)) {
  4224. rc = tpacpi_init_led(i);
  4225. if (rc < 0) {
  4226. led_exit();
  4227. return rc;
  4228. }
  4229. }
  4230. }
  4231. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  4232. printk(TPACPI_NOTICE
  4233. "warning: userspace override of important "
  4234. "firmware LEDs is enabled\n");
  4235. #endif
  4236. return 0;
  4237. }
  4238. #define str_led_status(s) \
  4239. ((s) == TPACPI_LED_OFF ? "off" : \
  4240. ((s) == TPACPI_LED_ON ? "on" : "blinking"))
  4241. static int led_read(char *p)
  4242. {
  4243. int len = 0;
  4244. if (!led_supported) {
  4245. len += sprintf(p + len, "status:\t\tnot supported\n");
  4246. return len;
  4247. }
  4248. len += sprintf(p + len, "status:\t\tsupported\n");
  4249. if (led_supported == TPACPI_LED_570) {
  4250. /* 570 */
  4251. int i, status;
  4252. for (i = 0; i < 8; i++) {
  4253. status = led_get_status(i);
  4254. if (status < 0)
  4255. return -EIO;
  4256. len += sprintf(p + len, "%d:\t\t%s\n",
  4257. i, str_led_status(status));
  4258. }
  4259. }
  4260. len += sprintf(p + len, "commands:\t"
  4261. "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
  4262. return len;
  4263. }
  4264. static int led_write(char *buf)
  4265. {
  4266. char *cmd;
  4267. int led, rc;
  4268. enum led_status_t s;
  4269. if (!led_supported)
  4270. return -ENODEV;
  4271. while ((cmd = next_cmd(&buf))) {
  4272. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
  4273. return -EINVAL;
  4274. if (strstr(cmd, "off")) {
  4275. s = TPACPI_LED_OFF;
  4276. } else if (strstr(cmd, "on")) {
  4277. s = TPACPI_LED_ON;
  4278. } else if (strstr(cmd, "blink")) {
  4279. s = TPACPI_LED_BLINK;
  4280. } else {
  4281. return -EINVAL;
  4282. }
  4283. rc = led_set_status(led, s);
  4284. if (rc < 0)
  4285. return rc;
  4286. }
  4287. return 0;
  4288. }
  4289. static struct ibm_struct led_driver_data = {
  4290. .name = "led",
  4291. .read = led_read,
  4292. .write = led_write,
  4293. .exit = led_exit,
  4294. };
  4295. /*************************************************************************
  4296. * Beep subdriver
  4297. */
  4298. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  4299. #define TPACPI_BEEP_Q1 0x0001
  4300. static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
  4301. TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
  4302. TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
  4303. };
  4304. static int __init beep_init(struct ibm_init_struct *iibm)
  4305. {
  4306. unsigned long quirks;
  4307. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  4308. TPACPI_ACPIHANDLE_INIT(beep);
  4309. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  4310. str_supported(beep_handle != NULL));
  4311. quirks = tpacpi_check_quirks(beep_quirk_table,
  4312. ARRAY_SIZE(beep_quirk_table));
  4313. tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
  4314. return (beep_handle)? 0 : 1;
  4315. }
  4316. static int beep_read(char *p)
  4317. {
  4318. int len = 0;
  4319. if (!beep_handle)
  4320. len += sprintf(p + len, "status:\t\tnot supported\n");
  4321. else {
  4322. len += sprintf(p + len, "status:\t\tsupported\n");
  4323. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  4324. }
  4325. return len;
  4326. }
  4327. static int beep_write(char *buf)
  4328. {
  4329. char *cmd;
  4330. int beep_cmd;
  4331. if (!beep_handle)
  4332. return -ENODEV;
  4333. while ((cmd = next_cmd(&buf))) {
  4334. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  4335. beep_cmd >= 0 && beep_cmd <= 17) {
  4336. /* beep_cmd set */
  4337. } else
  4338. return -EINVAL;
  4339. if (tp_features.beep_needs_two_args) {
  4340. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
  4341. beep_cmd, 0))
  4342. return -EIO;
  4343. } else {
  4344. if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
  4345. beep_cmd))
  4346. return -EIO;
  4347. }
  4348. }
  4349. return 0;
  4350. }
  4351. static struct ibm_struct beep_driver_data = {
  4352. .name = "beep",
  4353. .read = beep_read,
  4354. .write = beep_write,
  4355. };
  4356. /*************************************************************************
  4357. * Thermal subdriver
  4358. */
  4359. enum thermal_access_mode {
  4360. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  4361. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  4362. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  4363. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  4364. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  4365. };
  4366. enum { /* TPACPI_THERMAL_TPEC_* */
  4367. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  4368. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  4369. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  4370. };
  4371. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  4372. struct ibm_thermal_sensors_struct {
  4373. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  4374. };
  4375. static enum thermal_access_mode thermal_read_mode;
  4376. /* idx is zero-based */
  4377. static int thermal_get_sensor(int idx, s32 *value)
  4378. {
  4379. int t;
  4380. s8 tmp;
  4381. char tmpi[5];
  4382. t = TP_EC_THERMAL_TMP0;
  4383. switch (thermal_read_mode) {
  4384. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  4385. case TPACPI_THERMAL_TPEC_16:
  4386. if (idx >= 8 && idx <= 15) {
  4387. t = TP_EC_THERMAL_TMP8;
  4388. idx -= 8;
  4389. }
  4390. /* fallthrough */
  4391. #endif
  4392. case TPACPI_THERMAL_TPEC_8:
  4393. if (idx <= 7) {
  4394. if (!acpi_ec_read(t + idx, &tmp))
  4395. return -EIO;
  4396. *value = tmp * 1000;
  4397. return 0;
  4398. }
  4399. break;
  4400. case TPACPI_THERMAL_ACPI_UPDT:
  4401. if (idx <= 7) {
  4402. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  4403. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  4404. return -EIO;
  4405. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  4406. return -EIO;
  4407. *value = (t - 2732) * 100;
  4408. return 0;
  4409. }
  4410. break;
  4411. case TPACPI_THERMAL_ACPI_TMP07:
  4412. if (idx <= 7) {
  4413. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  4414. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  4415. return -EIO;
  4416. if (t > 127 || t < -127)
  4417. t = TP_EC_THERMAL_TMP_NA;
  4418. *value = t * 1000;
  4419. return 0;
  4420. }
  4421. break;
  4422. case TPACPI_THERMAL_NONE:
  4423. default:
  4424. return -ENOSYS;
  4425. }
  4426. return -EINVAL;
  4427. }
  4428. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  4429. {
  4430. int res, i;
  4431. int n;
  4432. n = 8;
  4433. i = 0;
  4434. if (!s)
  4435. return -EINVAL;
  4436. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  4437. n = 16;
  4438. for (i = 0 ; i < n; i++) {
  4439. res = thermal_get_sensor(i, &s->temp[i]);
  4440. if (res)
  4441. return res;
  4442. }
  4443. return n;
  4444. }
  4445. /* sysfs temp##_input -------------------------------------------------- */
  4446. static ssize_t thermal_temp_input_show(struct device *dev,
  4447. struct device_attribute *attr,
  4448. char *buf)
  4449. {
  4450. struct sensor_device_attribute *sensor_attr =
  4451. to_sensor_dev_attr(attr);
  4452. int idx = sensor_attr->index;
  4453. s32 value;
  4454. int res;
  4455. res = thermal_get_sensor(idx, &value);
  4456. if (res)
  4457. return res;
  4458. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  4459. return -ENXIO;
  4460. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  4461. }
  4462. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  4463. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  4464. thermal_temp_input_show, NULL, _idxB)
  4465. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  4466. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  4467. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  4468. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  4469. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  4470. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  4471. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  4472. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  4473. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  4474. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  4475. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  4476. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  4477. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  4478. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  4479. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  4480. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  4481. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  4482. };
  4483. #define THERMAL_ATTRS(X) \
  4484. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  4485. static struct attribute *thermal_temp_input_attr[] = {
  4486. THERMAL_ATTRS(8),
  4487. THERMAL_ATTRS(9),
  4488. THERMAL_ATTRS(10),
  4489. THERMAL_ATTRS(11),
  4490. THERMAL_ATTRS(12),
  4491. THERMAL_ATTRS(13),
  4492. THERMAL_ATTRS(14),
  4493. THERMAL_ATTRS(15),
  4494. THERMAL_ATTRS(0),
  4495. THERMAL_ATTRS(1),
  4496. THERMAL_ATTRS(2),
  4497. THERMAL_ATTRS(3),
  4498. THERMAL_ATTRS(4),
  4499. THERMAL_ATTRS(5),
  4500. THERMAL_ATTRS(6),
  4501. THERMAL_ATTRS(7),
  4502. NULL
  4503. };
  4504. static const struct attribute_group thermal_temp_input16_group = {
  4505. .attrs = thermal_temp_input_attr
  4506. };
  4507. static const struct attribute_group thermal_temp_input8_group = {
  4508. .attrs = &thermal_temp_input_attr[8]
  4509. };
  4510. #undef THERMAL_SENSOR_ATTR_TEMP
  4511. #undef THERMAL_ATTRS
  4512. /* --------------------------------------------------------------------- */
  4513. static int __init thermal_init(struct ibm_init_struct *iibm)
  4514. {
  4515. u8 t, ta1, ta2;
  4516. int i;
  4517. int acpi_tmp7;
  4518. int res;
  4519. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  4520. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  4521. if (thinkpad_id.ec_model) {
  4522. /*
  4523. * Direct EC access mode: sensors at registers
  4524. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  4525. * non-implemented, thermal sensors return 0x80 when
  4526. * not available
  4527. */
  4528. ta1 = ta2 = 0;
  4529. for (i = 0; i < 8; i++) {
  4530. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  4531. ta1 |= t;
  4532. } else {
  4533. ta1 = 0;
  4534. break;
  4535. }
  4536. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  4537. ta2 |= t;
  4538. } else {
  4539. ta1 = 0;
  4540. break;
  4541. }
  4542. }
  4543. if (ta1 == 0) {
  4544. /* This is sheer paranoia, but we handle it anyway */
  4545. if (acpi_tmp7) {
  4546. printk(TPACPI_ERR
  4547. "ThinkPad ACPI EC access misbehaving, "
  4548. "falling back to ACPI TMPx access "
  4549. "mode\n");
  4550. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  4551. } else {
  4552. printk(TPACPI_ERR
  4553. "ThinkPad ACPI EC access misbehaving, "
  4554. "disabling thermal sensors access\n");
  4555. thermal_read_mode = TPACPI_THERMAL_NONE;
  4556. }
  4557. } else {
  4558. thermal_read_mode =
  4559. (ta2 != 0) ?
  4560. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  4561. }
  4562. } else if (acpi_tmp7) {
  4563. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  4564. /* 600e/x, 770e, 770x */
  4565. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  4566. } else {
  4567. /* Standard ACPI TMPx access, max 8 sensors */
  4568. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  4569. }
  4570. } else {
  4571. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  4572. thermal_read_mode = TPACPI_THERMAL_NONE;
  4573. }
  4574. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  4575. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  4576. thermal_read_mode);
  4577. switch (thermal_read_mode) {
  4578. case TPACPI_THERMAL_TPEC_16:
  4579. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4580. &thermal_temp_input16_group);
  4581. if (res)
  4582. return res;
  4583. break;
  4584. case TPACPI_THERMAL_TPEC_8:
  4585. case TPACPI_THERMAL_ACPI_TMP07:
  4586. case TPACPI_THERMAL_ACPI_UPDT:
  4587. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4588. &thermal_temp_input8_group);
  4589. if (res)
  4590. return res;
  4591. break;
  4592. case TPACPI_THERMAL_NONE:
  4593. default:
  4594. return 1;
  4595. }
  4596. return 0;
  4597. }
  4598. static void thermal_exit(void)
  4599. {
  4600. switch (thermal_read_mode) {
  4601. case TPACPI_THERMAL_TPEC_16:
  4602. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  4603. &thermal_temp_input16_group);
  4604. break;
  4605. case TPACPI_THERMAL_TPEC_8:
  4606. case TPACPI_THERMAL_ACPI_TMP07:
  4607. case TPACPI_THERMAL_ACPI_UPDT:
  4608. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  4609. &thermal_temp_input16_group);
  4610. break;
  4611. case TPACPI_THERMAL_NONE:
  4612. default:
  4613. break;
  4614. }
  4615. }
  4616. static int thermal_read(char *p)
  4617. {
  4618. int len = 0;
  4619. int n, i;
  4620. struct ibm_thermal_sensors_struct t;
  4621. n = thermal_get_sensors(&t);
  4622. if (unlikely(n < 0))
  4623. return n;
  4624. len += sprintf(p + len, "temperatures:\t");
  4625. if (n > 0) {
  4626. for (i = 0; i < (n - 1); i++)
  4627. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  4628. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  4629. } else
  4630. len += sprintf(p + len, "not supported\n");
  4631. return len;
  4632. }
  4633. static struct ibm_struct thermal_driver_data = {
  4634. .name = "thermal",
  4635. .read = thermal_read,
  4636. .exit = thermal_exit,
  4637. };
  4638. /*************************************************************************
  4639. * EC Dump subdriver
  4640. */
  4641. static u8 ecdump_regs[256];
  4642. static int ecdump_read(char *p)
  4643. {
  4644. int len = 0;
  4645. int i, j;
  4646. u8 v;
  4647. len += sprintf(p + len, "EC "
  4648. " +00 +01 +02 +03 +04 +05 +06 +07"
  4649. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  4650. for (i = 0; i < 256; i += 16) {
  4651. len += sprintf(p + len, "EC 0x%02x:", i);
  4652. for (j = 0; j < 16; j++) {
  4653. if (!acpi_ec_read(i + j, &v))
  4654. break;
  4655. if (v != ecdump_regs[i + j])
  4656. len += sprintf(p + len, " *%02x", v);
  4657. else
  4658. len += sprintf(p + len, " %02x", v);
  4659. ecdump_regs[i + j] = v;
  4660. }
  4661. len += sprintf(p + len, "\n");
  4662. if (j != 16)
  4663. break;
  4664. }
  4665. /* These are way too dangerous to advertise openly... */
  4666. #if 0
  4667. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  4668. " (<offset> is 00-ff, <value> is 00-ff)\n");
  4669. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  4670. " (<offset> is 00-ff, <value> is 0-255)\n");
  4671. #endif
  4672. return len;
  4673. }
  4674. static int ecdump_write(char *buf)
  4675. {
  4676. char *cmd;
  4677. int i, v;
  4678. while ((cmd = next_cmd(&buf))) {
  4679. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  4680. /* i and v set */
  4681. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  4682. /* i and v set */
  4683. } else
  4684. return -EINVAL;
  4685. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  4686. if (!acpi_ec_write(i, v))
  4687. return -EIO;
  4688. } else
  4689. return -EINVAL;
  4690. }
  4691. return 0;
  4692. }
  4693. static struct ibm_struct ecdump_driver_data = {
  4694. .name = "ecdump",
  4695. .read = ecdump_read,
  4696. .write = ecdump_write,
  4697. .flags.experimental = 1,
  4698. };
  4699. /*************************************************************************
  4700. * Backlight/brightness subdriver
  4701. */
  4702. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  4703. /*
  4704. * ThinkPads can read brightness from two places: EC HBRV (0x31), or
  4705. * CMOS NVRAM byte 0x5E, bits 0-3.
  4706. *
  4707. * EC HBRV (0x31) has the following layout
  4708. * Bit 7: unknown function
  4709. * Bit 6: unknown function
  4710. * Bit 5: Z: honour scale changes, NZ: ignore scale changes
  4711. * Bit 4: must be set to zero to avoid problems
  4712. * Bit 3-0: backlight brightness level
  4713. *
  4714. * brightness_get_raw returns status data in the HBRV layout
  4715. *
  4716. * WARNING: The X61 has been verified to use HBRV for something else, so
  4717. * this should be used _only_ on IBM ThinkPads, and maybe with some careful
  4718. * testing on the very early *60 Lenovo models...
  4719. */
  4720. enum {
  4721. TP_EC_BACKLIGHT = 0x31,
  4722. /* TP_EC_BACKLIGHT bitmasks */
  4723. TP_EC_BACKLIGHT_LVLMSK = 0x1F,
  4724. TP_EC_BACKLIGHT_CMDMSK = 0xE0,
  4725. TP_EC_BACKLIGHT_MAPSW = 0x20,
  4726. };
  4727. enum tpacpi_brightness_access_mode {
  4728. TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
  4729. TPACPI_BRGHT_MODE_EC, /* EC control */
  4730. TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
  4731. TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  4732. TPACPI_BRGHT_MODE_MAX
  4733. };
  4734. static struct backlight_device *ibm_backlight_device;
  4735. static enum tpacpi_brightness_access_mode brightness_mode =
  4736. TPACPI_BRGHT_MODE_MAX;
  4737. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  4738. static struct mutex brightness_mutex;
  4739. /* NVRAM brightness access,
  4740. * call with brightness_mutex held! */
  4741. static unsigned int tpacpi_brightness_nvram_get(void)
  4742. {
  4743. u8 lnvram;
  4744. lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  4745. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  4746. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  4747. lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
  4748. return lnvram;
  4749. }
  4750. static void tpacpi_brightness_checkpoint_nvram(void)
  4751. {
  4752. u8 lec = 0;
  4753. u8 b_nvram;
  4754. if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
  4755. return;
  4756. vdbg_printk(TPACPI_DBG_BRGHT,
  4757. "trying to checkpoint backlight level to NVRAM...\n");
  4758. if (mutex_lock_killable(&brightness_mutex) < 0)
  4759. return;
  4760. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  4761. goto unlock;
  4762. lec &= TP_EC_BACKLIGHT_LVLMSK;
  4763. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  4764. if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  4765. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
  4766. /* NVRAM needs update */
  4767. b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
  4768. TP_NVRAM_POS_LEVEL_BRIGHTNESS);
  4769. b_nvram |= lec;
  4770. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
  4771. dbg_printk(TPACPI_DBG_BRGHT,
  4772. "updated NVRAM backlight level to %u (0x%02x)\n",
  4773. (unsigned int) lec, (unsigned int) b_nvram);
  4774. } else
  4775. vdbg_printk(TPACPI_DBG_BRGHT,
  4776. "NVRAM backlight level already is %u (0x%02x)\n",
  4777. (unsigned int) lec, (unsigned int) b_nvram);
  4778. unlock:
  4779. mutex_unlock(&brightness_mutex);
  4780. }
  4781. /* call with brightness_mutex held! */
  4782. static int tpacpi_brightness_get_raw(int *status)
  4783. {
  4784. u8 lec = 0;
  4785. switch (brightness_mode) {
  4786. case TPACPI_BRGHT_MODE_UCMS_STEP:
  4787. *status = tpacpi_brightness_nvram_get();
  4788. return 0;
  4789. case TPACPI_BRGHT_MODE_EC:
  4790. case TPACPI_BRGHT_MODE_ECNVRAM:
  4791. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  4792. return -EIO;
  4793. *status = lec;
  4794. return 0;
  4795. default:
  4796. return -ENXIO;
  4797. }
  4798. }
  4799. /* call with brightness_mutex held! */
  4800. /* do NOT call with illegal backlight level value */
  4801. static int tpacpi_brightness_set_ec(unsigned int value)
  4802. {
  4803. u8 lec = 0;
  4804. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  4805. return -EIO;
  4806. if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
  4807. (lec & TP_EC_BACKLIGHT_CMDMSK) |
  4808. (value & TP_EC_BACKLIGHT_LVLMSK))))
  4809. return -EIO;
  4810. return 0;
  4811. }
  4812. /* call with brightness_mutex held! */
  4813. static int tpacpi_brightness_set_ucmsstep(unsigned int value)
  4814. {
  4815. int cmos_cmd, inc;
  4816. unsigned int current_value, i;
  4817. current_value = tpacpi_brightness_nvram_get();
  4818. if (value == current_value)
  4819. return 0;
  4820. cmos_cmd = (value > current_value) ?
  4821. TP_CMOS_BRIGHTNESS_UP :
  4822. TP_CMOS_BRIGHTNESS_DOWN;
  4823. inc = (value > current_value) ? 1 : -1;
  4824. for (i = current_value; i != value; i += inc)
  4825. if (issue_thinkpad_cmos_command(cmos_cmd))
  4826. return -EIO;
  4827. return 0;
  4828. }
  4829. /* May return EINTR which can always be mapped to ERESTARTSYS */
  4830. static int brightness_set(unsigned int value)
  4831. {
  4832. int res;
  4833. if (value > ((tp_features.bright_16levels)? 15 : 7) ||
  4834. value < 0)
  4835. return -EINVAL;
  4836. vdbg_printk(TPACPI_DBG_BRGHT,
  4837. "set backlight level to %d\n", value);
  4838. res = mutex_lock_killable(&brightness_mutex);
  4839. if (res < 0)
  4840. return res;
  4841. switch (brightness_mode) {
  4842. case TPACPI_BRGHT_MODE_EC:
  4843. case TPACPI_BRGHT_MODE_ECNVRAM:
  4844. res = tpacpi_brightness_set_ec(value);
  4845. break;
  4846. case TPACPI_BRGHT_MODE_UCMS_STEP:
  4847. res = tpacpi_brightness_set_ucmsstep(value);
  4848. break;
  4849. default:
  4850. res = -ENXIO;
  4851. }
  4852. mutex_unlock(&brightness_mutex);
  4853. return res;
  4854. }
  4855. /* sysfs backlight class ----------------------------------------------- */
  4856. static int brightness_update_status(struct backlight_device *bd)
  4857. {
  4858. unsigned int level =
  4859. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  4860. bd->props.power == FB_BLANK_UNBLANK) ?
  4861. bd->props.brightness : 0;
  4862. dbg_printk(TPACPI_DBG_BRGHT,
  4863. "backlight: attempt to set level to %d\n",
  4864. level);
  4865. /* it is the backlight class's job (caller) to handle
  4866. * EINTR and other errors properly */
  4867. return brightness_set(level);
  4868. }
  4869. static int brightness_get(struct backlight_device *bd)
  4870. {
  4871. int status, res;
  4872. res = mutex_lock_killable(&brightness_mutex);
  4873. if (res < 0)
  4874. return 0;
  4875. res = tpacpi_brightness_get_raw(&status);
  4876. mutex_unlock(&brightness_mutex);
  4877. if (res < 0)
  4878. return 0;
  4879. return status & TP_EC_BACKLIGHT_LVLMSK;
  4880. }
  4881. static struct backlight_ops ibm_backlight_data = {
  4882. .get_brightness = brightness_get,
  4883. .update_status = brightness_update_status,
  4884. };
  4885. /* --------------------------------------------------------------------- */
  4886. /*
  4887. * These are only useful for models that have only one possibility
  4888. * of GPU. If the BIOS model handles both ATI and Intel, don't use
  4889. * these quirks.
  4890. */
  4891. #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
  4892. #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
  4893. #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
  4894. static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
  4895. /* Models with ATI GPUs known to require ECNVRAM mode */
  4896. TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
  4897. /* Models with ATI GPUs that can use ECNVRAM */
  4898. TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
  4899. TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  4900. TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  4901. TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  4902. /* Models with Intel Extreme Graphics 2 */
  4903. TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
  4904. TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
  4905. TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
  4906. /* Models with Intel GMA900 */
  4907. TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
  4908. TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
  4909. TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
  4910. };
  4911. static int __init brightness_init(struct ibm_init_struct *iibm)
  4912. {
  4913. int b;
  4914. unsigned long quirks;
  4915. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  4916. mutex_init(&brightness_mutex);
  4917. quirks = tpacpi_check_quirks(brightness_quirk_table,
  4918. ARRAY_SIZE(brightness_quirk_table));
  4919. /*
  4920. * We always attempt to detect acpi support, so as to switch
  4921. * Lenovo Vista BIOS to ACPI brightness mode even if we are not
  4922. * going to publish a backlight interface
  4923. */
  4924. b = tpacpi_check_std_acpi_brightness_support();
  4925. if (b > 0) {
  4926. if (acpi_video_backlight_support()) {
  4927. if (brightness_enable > 1) {
  4928. printk(TPACPI_NOTICE
  4929. "Standard ACPI backlight interface "
  4930. "available, not loading native one.\n");
  4931. return 1;
  4932. } else if (brightness_enable == 1) {
  4933. printk(TPACPI_NOTICE
  4934. "Backlight control force enabled, even if standard "
  4935. "ACPI backlight interface is available\n");
  4936. }
  4937. } else {
  4938. if (brightness_enable > 1) {
  4939. printk(TPACPI_NOTICE
  4940. "Standard ACPI backlight interface not "
  4941. "available, thinkpad_acpi native "
  4942. "brightness control enabled\n");
  4943. }
  4944. }
  4945. }
  4946. if (!brightness_enable) {
  4947. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  4948. "brightness support disabled by "
  4949. "module parameter\n");
  4950. return 1;
  4951. }
  4952. if (b > 16) {
  4953. printk(TPACPI_ERR
  4954. "Unsupported brightness interface, "
  4955. "please contact %s\n", TPACPI_MAIL);
  4956. return 1;
  4957. }
  4958. if (b == 16)
  4959. tp_features.bright_16levels = 1;
  4960. /*
  4961. * Check for module parameter bogosity, note that we
  4962. * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
  4963. * able to detect "unspecified"
  4964. */
  4965. if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
  4966. return -EINVAL;
  4967. /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
  4968. if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
  4969. brightness_mode == TPACPI_BRGHT_MODE_MAX) {
  4970. if (quirks & TPACPI_BRGHT_Q_EC)
  4971. brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
  4972. else
  4973. brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
  4974. dbg_printk(TPACPI_DBG_BRGHT,
  4975. "driver auto-selected brightness_mode=%d\n",
  4976. brightness_mode);
  4977. }
  4978. /* Safety */
  4979. if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
  4980. (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
  4981. brightness_mode == TPACPI_BRGHT_MODE_EC))
  4982. return -EINVAL;
  4983. if (tpacpi_brightness_get_raw(&b) < 0)
  4984. return 1;
  4985. if (tp_features.bright_16levels)
  4986. printk(TPACPI_INFO
  4987. "detected a 16-level brightness capable ThinkPad\n");
  4988. ibm_backlight_device = backlight_device_register(
  4989. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  4990. &ibm_backlight_data);
  4991. if (IS_ERR(ibm_backlight_device)) {
  4992. printk(TPACPI_ERR "Could not register backlight device\n");
  4993. return PTR_ERR(ibm_backlight_device);
  4994. }
  4995. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  4996. "brightness is supported\n");
  4997. if (quirks & TPACPI_BRGHT_Q_ASK) {
  4998. printk(TPACPI_NOTICE
  4999. "brightness: will use unverified default: "
  5000. "brightness_mode=%d\n", brightness_mode);
  5001. printk(TPACPI_NOTICE
  5002. "brightness: please report to %s whether it works well "
  5003. "or not on your ThinkPad\n", TPACPI_MAIL);
  5004. }
  5005. ibm_backlight_device->props.max_brightness =
  5006. (tp_features.bright_16levels)? 15 : 7;
  5007. ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
  5008. backlight_update_status(ibm_backlight_device);
  5009. return 0;
  5010. }
  5011. static void brightness_suspend(pm_message_t state)
  5012. {
  5013. tpacpi_brightness_checkpoint_nvram();
  5014. }
  5015. static void brightness_shutdown(void)
  5016. {
  5017. tpacpi_brightness_checkpoint_nvram();
  5018. }
  5019. static void brightness_exit(void)
  5020. {
  5021. if (ibm_backlight_device) {
  5022. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
  5023. "calling backlight_device_unregister()\n");
  5024. backlight_device_unregister(ibm_backlight_device);
  5025. }
  5026. tpacpi_brightness_checkpoint_nvram();
  5027. }
  5028. static int brightness_read(char *p)
  5029. {
  5030. int len = 0;
  5031. int level;
  5032. level = brightness_get(NULL);
  5033. if (level < 0) {
  5034. len += sprintf(p + len, "level:\t\tunreadable\n");
  5035. } else {
  5036. len += sprintf(p + len, "level:\t\t%d\n", level);
  5037. len += sprintf(p + len, "commands:\tup, down\n");
  5038. len += sprintf(p + len, "commands:\tlevel <level>"
  5039. " (<level> is 0-%d)\n",
  5040. (tp_features.bright_16levels) ? 15 : 7);
  5041. }
  5042. return len;
  5043. }
  5044. static int brightness_write(char *buf)
  5045. {
  5046. int level;
  5047. int rc;
  5048. char *cmd;
  5049. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  5050. level = brightness_get(NULL);
  5051. if (level < 0)
  5052. return level;
  5053. while ((cmd = next_cmd(&buf))) {
  5054. if (strlencmp(cmd, "up") == 0) {
  5055. if (level < max_level)
  5056. level++;
  5057. } else if (strlencmp(cmd, "down") == 0) {
  5058. if (level > 0)
  5059. level--;
  5060. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  5061. level >= 0 && level <= max_level) {
  5062. /* new level set */
  5063. } else
  5064. return -EINVAL;
  5065. }
  5066. tpacpi_disclose_usertask("procfs brightness",
  5067. "set level to %d\n", level);
  5068. /*
  5069. * Now we know what the final level should be, so we try to set it.
  5070. * Doing it this way makes the syscall restartable in case of EINTR
  5071. */
  5072. rc = brightness_set(level);
  5073. return (rc == -EINTR)? ERESTARTSYS : rc;
  5074. }
  5075. static struct ibm_struct brightness_driver_data = {
  5076. .name = "brightness",
  5077. .read = brightness_read,
  5078. .write = brightness_write,
  5079. .exit = brightness_exit,
  5080. .suspend = brightness_suspend,
  5081. .shutdown = brightness_shutdown,
  5082. };
  5083. /*************************************************************************
  5084. * Volume subdriver
  5085. */
  5086. static int volume_offset = 0x30;
  5087. static int volume_read(char *p)
  5088. {
  5089. int len = 0;
  5090. u8 level;
  5091. if (!acpi_ec_read(volume_offset, &level)) {
  5092. len += sprintf(p + len, "level:\t\tunreadable\n");
  5093. } else {
  5094. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  5095. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  5096. len += sprintf(p + len, "commands:\tup, down, mute\n");
  5097. len += sprintf(p + len, "commands:\tlevel <level>"
  5098. " (<level> is 0-15)\n");
  5099. }
  5100. return len;
  5101. }
  5102. static int volume_write(char *buf)
  5103. {
  5104. int cmos_cmd, inc, i;
  5105. u8 level, mute;
  5106. int new_level, new_mute;
  5107. char *cmd;
  5108. while ((cmd = next_cmd(&buf))) {
  5109. if (!acpi_ec_read(volume_offset, &level))
  5110. return -EIO;
  5111. new_mute = mute = level & 0x40;
  5112. new_level = level = level & 0xf;
  5113. if (strlencmp(cmd, "up") == 0) {
  5114. if (mute)
  5115. new_mute = 0;
  5116. else
  5117. new_level = level == 15 ? 15 : level + 1;
  5118. } else if (strlencmp(cmd, "down") == 0) {
  5119. if (mute)
  5120. new_mute = 0;
  5121. else
  5122. new_level = level == 0 ? 0 : level - 1;
  5123. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  5124. new_level >= 0 && new_level <= 15) {
  5125. /* new_level set */
  5126. } else if (strlencmp(cmd, "mute") == 0) {
  5127. new_mute = 0x40;
  5128. } else
  5129. return -EINVAL;
  5130. if (new_level != level) {
  5131. /* mute doesn't change */
  5132. cmos_cmd = (new_level > level) ?
  5133. TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  5134. inc = new_level > level ? 1 : -1;
  5135. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  5136. !acpi_ec_write(volume_offset, level)))
  5137. return -EIO;
  5138. for (i = level; i != new_level; i += inc)
  5139. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  5140. !acpi_ec_write(volume_offset, i + inc))
  5141. return -EIO;
  5142. if (mute &&
  5143. (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  5144. !acpi_ec_write(volume_offset, new_level + mute))) {
  5145. return -EIO;
  5146. }
  5147. }
  5148. if (new_mute != mute) {
  5149. /* level doesn't change */
  5150. cmos_cmd = (new_mute) ?
  5151. TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  5152. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  5153. !acpi_ec_write(volume_offset, level + new_mute))
  5154. return -EIO;
  5155. }
  5156. }
  5157. return 0;
  5158. }
  5159. static struct ibm_struct volume_driver_data = {
  5160. .name = "volume",
  5161. .read = volume_read,
  5162. .write = volume_write,
  5163. };
  5164. /*************************************************************************
  5165. * Fan subdriver
  5166. */
  5167. /*
  5168. * FAN ACCESS MODES
  5169. *
  5170. * TPACPI_FAN_RD_ACPI_GFAN:
  5171. * ACPI GFAN method: returns fan level
  5172. *
  5173. * see TPACPI_FAN_WR_ACPI_SFAN
  5174. * EC 0x2f (HFSP) not available if GFAN exists
  5175. *
  5176. * TPACPI_FAN_WR_ACPI_SFAN:
  5177. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  5178. *
  5179. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  5180. * it for writing.
  5181. *
  5182. * TPACPI_FAN_WR_TPEC:
  5183. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  5184. * Supported on almost all ThinkPads
  5185. *
  5186. * Fan speed changes of any sort (including those caused by the
  5187. * disengaged mode) are usually done slowly by the firmware as the
  5188. * maximum ammount of fan duty cycle change per second seems to be
  5189. * limited.
  5190. *
  5191. * Reading is not available if GFAN exists.
  5192. * Writing is not available if SFAN exists.
  5193. *
  5194. * Bits
  5195. * 7 automatic mode engaged;
  5196. * (default operation mode of the ThinkPad)
  5197. * fan level is ignored in this mode.
  5198. * 6 full speed mode (takes precedence over bit 7);
  5199. * not available on all thinkpads. May disable
  5200. * the tachometer while the fan controller ramps up
  5201. * the speed (which can take up to a few *minutes*).
  5202. * Speeds up fan to 100% duty-cycle, which is far above
  5203. * the standard RPM levels. It is not impossible that
  5204. * it could cause hardware damage.
  5205. * 5-3 unused in some models. Extra bits for fan level
  5206. * in others, but still useless as all values above
  5207. * 7 map to the same speed as level 7 in these models.
  5208. * 2-0 fan level (0..7 usually)
  5209. * 0x00 = stop
  5210. * 0x07 = max (set when temperatures critical)
  5211. * Some ThinkPads may have other levels, see
  5212. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  5213. *
  5214. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  5215. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  5216. * does so, its initial value is meaningless (0x07).
  5217. *
  5218. * For firmware bugs, refer to:
  5219. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  5220. *
  5221. * ----
  5222. *
  5223. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  5224. * Main fan tachometer reading (in RPM)
  5225. *
  5226. * This register is present on all ThinkPads with a new-style EC, and
  5227. * it is known not to be present on the A21m/e, and T22, as there is
  5228. * something else in offset 0x84 according to the ACPI DSDT. Other
  5229. * ThinkPads from this same time period (and earlier) probably lack the
  5230. * tachometer as well.
  5231. *
  5232. * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
  5233. * was never fixed by IBM to report the EC firmware version string
  5234. * probably support the tachometer (like the early X models), so
  5235. * detecting it is quite hard. We need more data to know for sure.
  5236. *
  5237. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  5238. * might result.
  5239. *
  5240. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  5241. * mode.
  5242. *
  5243. * For firmware bugs, refer to:
  5244. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  5245. *
  5246. * ----
  5247. *
  5248. * ThinkPad EC register 0x31 bit 0 (only on select models)
  5249. *
  5250. * When bit 0 of EC register 0x31 is zero, the tachometer registers
  5251. * show the speed of the main fan. When bit 0 of EC register 0x31
  5252. * is one, the tachometer registers show the speed of the auxiliary
  5253. * fan.
  5254. *
  5255. * Fan control seems to affect both fans, regardless of the state
  5256. * of this bit.
  5257. *
  5258. * So far, only the firmware for the X60/X61 non-tablet versions
  5259. * seem to support this (firmware TP-7M).
  5260. *
  5261. * TPACPI_FAN_WR_ACPI_FANS:
  5262. * ThinkPad X31, X40, X41. Not available in the X60.
  5263. *
  5264. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  5265. * high speed. ACPI DSDT seems to map these three speeds to levels
  5266. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  5267. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  5268. *
  5269. * The speeds are stored on handles
  5270. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  5271. *
  5272. * There are three default speed sets, acessible as handles:
  5273. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  5274. *
  5275. * ACPI DSDT switches which set is in use depending on various
  5276. * factors.
  5277. *
  5278. * TPACPI_FAN_WR_TPEC is also available and should be used to
  5279. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  5280. * but the ACPI tables just mention level 7.
  5281. */
  5282. enum { /* Fan control constants */
  5283. fan_status_offset = 0x2f, /* EC register 0x2f */
  5284. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  5285. * 0x84 must be read before 0x85 */
  5286. fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
  5287. bit 0 selects which fan is active */
  5288. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  5289. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  5290. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  5291. };
  5292. enum fan_status_access_mode {
  5293. TPACPI_FAN_NONE = 0, /* No fan status or control */
  5294. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  5295. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  5296. };
  5297. enum fan_control_access_mode {
  5298. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  5299. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  5300. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  5301. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  5302. };
  5303. enum fan_control_commands {
  5304. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  5305. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  5306. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  5307. * and also watchdog cmd */
  5308. };
  5309. static int fan_control_allowed;
  5310. static enum fan_status_access_mode fan_status_access_mode;
  5311. static enum fan_control_access_mode fan_control_access_mode;
  5312. static enum fan_control_commands fan_control_commands;
  5313. static u8 fan_control_initial_status;
  5314. static u8 fan_control_desired_level;
  5315. static u8 fan_control_resume_level;
  5316. static int fan_watchdog_maxinterval;
  5317. static struct mutex fan_mutex;
  5318. static void fan_watchdog_fire(struct work_struct *ignored);
  5319. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  5320. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  5321. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  5322. "\\FSPD", /* 600e/x, 770e, 770x */
  5323. ); /* all others */
  5324. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  5325. "JFNS", /* 770x-JL */
  5326. ); /* all others */
  5327. /*
  5328. * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
  5329. * HFSP register at boot, so it contains 0x07 but the Thinkpad could
  5330. * be in auto mode (0x80).
  5331. *
  5332. * This is corrected by any write to HFSP either by the driver, or
  5333. * by the firmware.
  5334. *
  5335. * We assume 0x07 really means auto mode while this quirk is active,
  5336. * as this is far more likely than the ThinkPad being in level 7,
  5337. * which is only used by the firmware during thermal emergencies.
  5338. *
  5339. * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
  5340. * TP-70 (T43, R52), which are known to be buggy.
  5341. */
  5342. static void fan_quirk1_setup(void)
  5343. {
  5344. if (fan_control_initial_status == 0x07) {
  5345. printk(TPACPI_NOTICE
  5346. "fan_init: initial fan status is unknown, "
  5347. "assuming it is in auto mode\n");
  5348. tp_features.fan_ctrl_status_undef = 1;
  5349. }
  5350. }
  5351. static void fan_quirk1_handle(u8 *fan_status)
  5352. {
  5353. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  5354. if (*fan_status != fan_control_initial_status) {
  5355. /* something changed the HFSP regisnter since
  5356. * driver init time, so it is not undefined
  5357. * anymore */
  5358. tp_features.fan_ctrl_status_undef = 0;
  5359. } else {
  5360. /* Return most likely status. In fact, it
  5361. * might be the only possible status */
  5362. *fan_status = TP_EC_FAN_AUTO;
  5363. }
  5364. }
  5365. }
  5366. /* Select main fan on X60/X61, NOOP on others */
  5367. static bool fan_select_fan1(void)
  5368. {
  5369. if (tp_features.second_fan) {
  5370. u8 val;
  5371. if (ec_read(fan_select_offset, &val) < 0)
  5372. return false;
  5373. val &= 0xFEU;
  5374. if (ec_write(fan_select_offset, val) < 0)
  5375. return false;
  5376. }
  5377. return true;
  5378. }
  5379. /* Select secondary fan on X60/X61 */
  5380. static bool fan_select_fan2(void)
  5381. {
  5382. u8 val;
  5383. if (!tp_features.second_fan)
  5384. return false;
  5385. if (ec_read(fan_select_offset, &val) < 0)
  5386. return false;
  5387. val |= 0x01U;
  5388. if (ec_write(fan_select_offset, val) < 0)
  5389. return false;
  5390. return true;
  5391. }
  5392. /*
  5393. * Call with fan_mutex held
  5394. */
  5395. static void fan_update_desired_level(u8 status)
  5396. {
  5397. if ((status &
  5398. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  5399. if (status > 7)
  5400. fan_control_desired_level = 7;
  5401. else
  5402. fan_control_desired_level = status;
  5403. }
  5404. }
  5405. static int fan_get_status(u8 *status)
  5406. {
  5407. u8 s;
  5408. /* TODO:
  5409. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  5410. switch (fan_status_access_mode) {
  5411. case TPACPI_FAN_RD_ACPI_GFAN:
  5412. /* 570, 600e/x, 770e, 770x */
  5413. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  5414. return -EIO;
  5415. if (likely(status))
  5416. *status = s & 0x07;
  5417. break;
  5418. case TPACPI_FAN_RD_TPEC:
  5419. /* all except 570, 600e/x, 770e, 770x */
  5420. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  5421. return -EIO;
  5422. if (likely(status)) {
  5423. *status = s;
  5424. fan_quirk1_handle(status);
  5425. }
  5426. break;
  5427. default:
  5428. return -ENXIO;
  5429. }
  5430. return 0;
  5431. }
  5432. static int fan_get_status_safe(u8 *status)
  5433. {
  5434. int rc;
  5435. u8 s;
  5436. if (mutex_lock_killable(&fan_mutex))
  5437. return -ERESTARTSYS;
  5438. rc = fan_get_status(&s);
  5439. if (!rc)
  5440. fan_update_desired_level(s);
  5441. mutex_unlock(&fan_mutex);
  5442. if (status)
  5443. *status = s;
  5444. return rc;
  5445. }
  5446. static int fan_get_speed(unsigned int *speed)
  5447. {
  5448. u8 hi, lo;
  5449. switch (fan_status_access_mode) {
  5450. case TPACPI_FAN_RD_TPEC:
  5451. /* all except 570, 600e/x, 770e, 770x */
  5452. if (unlikely(!fan_select_fan1()))
  5453. return -EIO;
  5454. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  5455. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  5456. return -EIO;
  5457. if (likely(speed))
  5458. *speed = (hi << 8) | lo;
  5459. break;
  5460. default:
  5461. return -ENXIO;
  5462. }
  5463. return 0;
  5464. }
  5465. static int fan2_get_speed(unsigned int *speed)
  5466. {
  5467. u8 hi, lo;
  5468. bool rc;
  5469. switch (fan_status_access_mode) {
  5470. case TPACPI_FAN_RD_TPEC:
  5471. /* all except 570, 600e/x, 770e, 770x */
  5472. if (unlikely(!fan_select_fan2()))
  5473. return -EIO;
  5474. rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
  5475. !acpi_ec_read(fan_rpm_offset + 1, &hi);
  5476. fan_select_fan1(); /* play it safe */
  5477. if (rc)
  5478. return -EIO;
  5479. if (likely(speed))
  5480. *speed = (hi << 8) | lo;
  5481. break;
  5482. default:
  5483. return -ENXIO;
  5484. }
  5485. return 0;
  5486. }
  5487. static int fan_set_level(int level)
  5488. {
  5489. if (!fan_control_allowed)
  5490. return -EPERM;
  5491. switch (fan_control_access_mode) {
  5492. case TPACPI_FAN_WR_ACPI_SFAN:
  5493. if (level >= 0 && level <= 7) {
  5494. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  5495. return -EIO;
  5496. } else
  5497. return -EINVAL;
  5498. break;
  5499. case TPACPI_FAN_WR_ACPI_FANS:
  5500. case TPACPI_FAN_WR_TPEC:
  5501. if (!(level & TP_EC_FAN_AUTO) &&
  5502. !(level & TP_EC_FAN_FULLSPEED) &&
  5503. ((level < 0) || (level > 7)))
  5504. return -EINVAL;
  5505. /* safety net should the EC not support AUTO
  5506. * or FULLSPEED mode bits and just ignore them */
  5507. if (level & TP_EC_FAN_FULLSPEED)
  5508. level |= 7; /* safety min speed 7 */
  5509. else if (level & TP_EC_FAN_AUTO)
  5510. level |= 4; /* safety min speed 4 */
  5511. if (!acpi_ec_write(fan_status_offset, level))
  5512. return -EIO;
  5513. else
  5514. tp_features.fan_ctrl_status_undef = 0;
  5515. break;
  5516. default:
  5517. return -ENXIO;
  5518. }
  5519. vdbg_printk(TPACPI_DBG_FAN,
  5520. "fan control: set fan control register to 0x%02x\n", level);
  5521. return 0;
  5522. }
  5523. static int fan_set_level_safe(int level)
  5524. {
  5525. int rc;
  5526. if (!fan_control_allowed)
  5527. return -EPERM;
  5528. if (mutex_lock_killable(&fan_mutex))
  5529. return -ERESTARTSYS;
  5530. if (level == TPACPI_FAN_LAST_LEVEL)
  5531. level = fan_control_desired_level;
  5532. rc = fan_set_level(level);
  5533. if (!rc)
  5534. fan_update_desired_level(level);
  5535. mutex_unlock(&fan_mutex);
  5536. return rc;
  5537. }
  5538. static int fan_set_enable(void)
  5539. {
  5540. u8 s;
  5541. int rc;
  5542. if (!fan_control_allowed)
  5543. return -EPERM;
  5544. if (mutex_lock_killable(&fan_mutex))
  5545. return -ERESTARTSYS;
  5546. switch (fan_control_access_mode) {
  5547. case TPACPI_FAN_WR_ACPI_FANS:
  5548. case TPACPI_FAN_WR_TPEC:
  5549. rc = fan_get_status(&s);
  5550. if (rc < 0)
  5551. break;
  5552. /* Don't go out of emergency fan mode */
  5553. if (s != 7) {
  5554. s &= 0x07;
  5555. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  5556. }
  5557. if (!acpi_ec_write(fan_status_offset, s))
  5558. rc = -EIO;
  5559. else {
  5560. tp_features.fan_ctrl_status_undef = 0;
  5561. rc = 0;
  5562. }
  5563. break;
  5564. case TPACPI_FAN_WR_ACPI_SFAN:
  5565. rc = fan_get_status(&s);
  5566. if (rc < 0)
  5567. break;
  5568. s &= 0x07;
  5569. /* Set fan to at least level 4 */
  5570. s |= 4;
  5571. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  5572. rc = -EIO;
  5573. else
  5574. rc = 0;
  5575. break;
  5576. default:
  5577. rc = -ENXIO;
  5578. }
  5579. mutex_unlock(&fan_mutex);
  5580. if (!rc)
  5581. vdbg_printk(TPACPI_DBG_FAN,
  5582. "fan control: set fan control register to 0x%02x\n",
  5583. s);
  5584. return rc;
  5585. }
  5586. static int fan_set_disable(void)
  5587. {
  5588. int rc;
  5589. if (!fan_control_allowed)
  5590. return -EPERM;
  5591. if (mutex_lock_killable(&fan_mutex))
  5592. return -ERESTARTSYS;
  5593. rc = 0;
  5594. switch (fan_control_access_mode) {
  5595. case TPACPI_FAN_WR_ACPI_FANS:
  5596. case TPACPI_FAN_WR_TPEC:
  5597. if (!acpi_ec_write(fan_status_offset, 0x00))
  5598. rc = -EIO;
  5599. else {
  5600. fan_control_desired_level = 0;
  5601. tp_features.fan_ctrl_status_undef = 0;
  5602. }
  5603. break;
  5604. case TPACPI_FAN_WR_ACPI_SFAN:
  5605. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  5606. rc = -EIO;
  5607. else
  5608. fan_control_desired_level = 0;
  5609. break;
  5610. default:
  5611. rc = -ENXIO;
  5612. }
  5613. if (!rc)
  5614. vdbg_printk(TPACPI_DBG_FAN,
  5615. "fan control: set fan control register to 0\n");
  5616. mutex_unlock(&fan_mutex);
  5617. return rc;
  5618. }
  5619. static int fan_set_speed(int speed)
  5620. {
  5621. int rc;
  5622. if (!fan_control_allowed)
  5623. return -EPERM;
  5624. if (mutex_lock_killable(&fan_mutex))
  5625. return -ERESTARTSYS;
  5626. rc = 0;
  5627. switch (fan_control_access_mode) {
  5628. case TPACPI_FAN_WR_ACPI_FANS:
  5629. if (speed >= 0 && speed <= 65535) {
  5630. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  5631. speed, speed, speed))
  5632. rc = -EIO;
  5633. } else
  5634. rc = -EINVAL;
  5635. break;
  5636. default:
  5637. rc = -ENXIO;
  5638. }
  5639. mutex_unlock(&fan_mutex);
  5640. return rc;
  5641. }
  5642. static void fan_watchdog_reset(void)
  5643. {
  5644. static int fan_watchdog_active;
  5645. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  5646. return;
  5647. if (fan_watchdog_active)
  5648. cancel_delayed_work(&fan_watchdog_task);
  5649. if (fan_watchdog_maxinterval > 0 &&
  5650. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  5651. fan_watchdog_active = 1;
  5652. if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
  5653. msecs_to_jiffies(fan_watchdog_maxinterval
  5654. * 1000))) {
  5655. printk(TPACPI_ERR
  5656. "failed to queue the fan watchdog, "
  5657. "watchdog will not trigger\n");
  5658. }
  5659. } else
  5660. fan_watchdog_active = 0;
  5661. }
  5662. static void fan_watchdog_fire(struct work_struct *ignored)
  5663. {
  5664. int rc;
  5665. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  5666. return;
  5667. printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
  5668. rc = fan_set_enable();
  5669. if (rc < 0) {
  5670. printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
  5671. "will try again later...\n", -rc);
  5672. /* reschedule for later */
  5673. fan_watchdog_reset();
  5674. }
  5675. }
  5676. /*
  5677. * SYSFS fan layout: hwmon compatible (device)
  5678. *
  5679. * pwm*_enable:
  5680. * 0: "disengaged" mode
  5681. * 1: manual mode
  5682. * 2: native EC "auto" mode (recommended, hardware default)
  5683. *
  5684. * pwm*: set speed in manual mode, ignored otherwise.
  5685. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  5686. * interpolation.
  5687. *
  5688. * fan*_input: tachometer reading, RPM
  5689. *
  5690. *
  5691. * SYSFS fan layout: extensions
  5692. *
  5693. * fan_watchdog (driver):
  5694. * fan watchdog interval in seconds, 0 disables (default), max 120
  5695. */
  5696. /* sysfs fan pwm1_enable ----------------------------------------------- */
  5697. static ssize_t fan_pwm1_enable_show(struct device *dev,
  5698. struct device_attribute *attr,
  5699. char *buf)
  5700. {
  5701. int res, mode;
  5702. u8 status;
  5703. res = fan_get_status_safe(&status);
  5704. if (res)
  5705. return res;
  5706. if (status & TP_EC_FAN_FULLSPEED) {
  5707. mode = 0;
  5708. } else if (status & TP_EC_FAN_AUTO) {
  5709. mode = 2;
  5710. } else
  5711. mode = 1;
  5712. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  5713. }
  5714. static ssize_t fan_pwm1_enable_store(struct device *dev,
  5715. struct device_attribute *attr,
  5716. const char *buf, size_t count)
  5717. {
  5718. unsigned long t;
  5719. int res, level;
  5720. if (parse_strtoul(buf, 2, &t))
  5721. return -EINVAL;
  5722. tpacpi_disclose_usertask("hwmon pwm1_enable",
  5723. "set fan mode to %lu\n", t);
  5724. switch (t) {
  5725. case 0:
  5726. level = TP_EC_FAN_FULLSPEED;
  5727. break;
  5728. case 1:
  5729. level = TPACPI_FAN_LAST_LEVEL;
  5730. break;
  5731. case 2:
  5732. level = TP_EC_FAN_AUTO;
  5733. break;
  5734. case 3:
  5735. /* reserved for software-controlled auto mode */
  5736. return -ENOSYS;
  5737. default:
  5738. return -EINVAL;
  5739. }
  5740. res = fan_set_level_safe(level);
  5741. if (res == -ENXIO)
  5742. return -EINVAL;
  5743. else if (res < 0)
  5744. return res;
  5745. fan_watchdog_reset();
  5746. return count;
  5747. }
  5748. static struct device_attribute dev_attr_fan_pwm1_enable =
  5749. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  5750. fan_pwm1_enable_show, fan_pwm1_enable_store);
  5751. /* sysfs fan pwm1 ------------------------------------------------------ */
  5752. static ssize_t fan_pwm1_show(struct device *dev,
  5753. struct device_attribute *attr,
  5754. char *buf)
  5755. {
  5756. int res;
  5757. u8 status;
  5758. res = fan_get_status_safe(&status);
  5759. if (res)
  5760. return res;
  5761. if ((status &
  5762. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  5763. status = fan_control_desired_level;
  5764. if (status > 7)
  5765. status = 7;
  5766. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  5767. }
  5768. static ssize_t fan_pwm1_store(struct device *dev,
  5769. struct device_attribute *attr,
  5770. const char *buf, size_t count)
  5771. {
  5772. unsigned long s;
  5773. int rc;
  5774. u8 status, newlevel;
  5775. if (parse_strtoul(buf, 255, &s))
  5776. return -EINVAL;
  5777. tpacpi_disclose_usertask("hwmon pwm1",
  5778. "set fan speed to %lu\n", s);
  5779. /* scale down from 0-255 to 0-7 */
  5780. newlevel = (s >> 5) & 0x07;
  5781. if (mutex_lock_killable(&fan_mutex))
  5782. return -ERESTARTSYS;
  5783. rc = fan_get_status(&status);
  5784. if (!rc && (status &
  5785. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  5786. rc = fan_set_level(newlevel);
  5787. if (rc == -ENXIO)
  5788. rc = -EINVAL;
  5789. else if (!rc) {
  5790. fan_update_desired_level(newlevel);
  5791. fan_watchdog_reset();
  5792. }
  5793. }
  5794. mutex_unlock(&fan_mutex);
  5795. return (rc)? rc : count;
  5796. }
  5797. static struct device_attribute dev_attr_fan_pwm1 =
  5798. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  5799. fan_pwm1_show, fan_pwm1_store);
  5800. /* sysfs fan fan1_input ------------------------------------------------ */
  5801. static ssize_t fan_fan1_input_show(struct device *dev,
  5802. struct device_attribute *attr,
  5803. char *buf)
  5804. {
  5805. int res;
  5806. unsigned int speed;
  5807. res = fan_get_speed(&speed);
  5808. if (res < 0)
  5809. return res;
  5810. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  5811. }
  5812. static struct device_attribute dev_attr_fan_fan1_input =
  5813. __ATTR(fan1_input, S_IRUGO,
  5814. fan_fan1_input_show, NULL);
  5815. /* sysfs fan fan2_input ------------------------------------------------ */
  5816. static ssize_t fan_fan2_input_show(struct device *dev,
  5817. struct device_attribute *attr,
  5818. char *buf)
  5819. {
  5820. int res;
  5821. unsigned int speed;
  5822. res = fan2_get_speed(&speed);
  5823. if (res < 0)
  5824. return res;
  5825. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  5826. }
  5827. static struct device_attribute dev_attr_fan_fan2_input =
  5828. __ATTR(fan2_input, S_IRUGO,
  5829. fan_fan2_input_show, NULL);
  5830. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  5831. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  5832. char *buf)
  5833. {
  5834. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  5835. }
  5836. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  5837. const char *buf, size_t count)
  5838. {
  5839. unsigned long t;
  5840. if (parse_strtoul(buf, 120, &t))
  5841. return -EINVAL;
  5842. if (!fan_control_allowed)
  5843. return -EPERM;
  5844. fan_watchdog_maxinterval = t;
  5845. fan_watchdog_reset();
  5846. tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
  5847. return count;
  5848. }
  5849. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  5850. fan_fan_watchdog_show, fan_fan_watchdog_store);
  5851. /* --------------------------------------------------------------------- */
  5852. static struct attribute *fan_attributes[] = {
  5853. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  5854. &dev_attr_fan_fan1_input.attr,
  5855. NULL, /* for fan2_input */
  5856. NULL
  5857. };
  5858. static const struct attribute_group fan_attr_group = {
  5859. .attrs = fan_attributes,
  5860. };
  5861. #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
  5862. #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
  5863. #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
  5864. { .vendor = PCI_VENDOR_ID_IBM, \
  5865. .bios = TPACPI_MATCH_ANY, \
  5866. .ec = TPID(__id1, __id2), \
  5867. .quirks = __quirks }
  5868. #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
  5869. { .vendor = PCI_VENDOR_ID_LENOVO, \
  5870. .bios = TPACPI_MATCH_ANY, \
  5871. .ec = TPID(__id1, __id2), \
  5872. .quirks = __quirks }
  5873. static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
  5874. TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
  5875. TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
  5876. TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
  5877. TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
  5878. TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
  5879. };
  5880. #undef TPACPI_FAN_QL
  5881. #undef TPACPI_FAN_QI
  5882. static int __init fan_init(struct ibm_init_struct *iibm)
  5883. {
  5884. int rc;
  5885. unsigned long quirks;
  5886. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  5887. "initializing fan subdriver\n");
  5888. mutex_init(&fan_mutex);
  5889. fan_status_access_mode = TPACPI_FAN_NONE;
  5890. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  5891. fan_control_commands = 0;
  5892. fan_watchdog_maxinterval = 0;
  5893. tp_features.fan_ctrl_status_undef = 0;
  5894. tp_features.second_fan = 0;
  5895. fan_control_desired_level = 7;
  5896. TPACPI_ACPIHANDLE_INIT(fans);
  5897. TPACPI_ACPIHANDLE_INIT(gfan);
  5898. TPACPI_ACPIHANDLE_INIT(sfan);
  5899. quirks = tpacpi_check_quirks(fan_quirk_table,
  5900. ARRAY_SIZE(fan_quirk_table));
  5901. if (gfan_handle) {
  5902. /* 570, 600e/x, 770e, 770x */
  5903. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  5904. } else {
  5905. /* all other ThinkPads: note that even old-style
  5906. * ThinkPad ECs supports the fan control register */
  5907. if (likely(acpi_ec_read(fan_status_offset,
  5908. &fan_control_initial_status))) {
  5909. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  5910. if (quirks & TPACPI_FAN_Q1)
  5911. fan_quirk1_setup();
  5912. if (quirks & TPACPI_FAN_2FAN) {
  5913. tp_features.second_fan = 1;
  5914. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  5915. "secondary fan support enabled\n");
  5916. }
  5917. } else {
  5918. printk(TPACPI_ERR
  5919. "ThinkPad ACPI EC access misbehaving, "
  5920. "fan status and control unavailable\n");
  5921. return 1;
  5922. }
  5923. }
  5924. if (sfan_handle) {
  5925. /* 570, 770x-JL */
  5926. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  5927. fan_control_commands |=
  5928. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  5929. } else {
  5930. if (!gfan_handle) {
  5931. /* gfan without sfan means no fan control */
  5932. /* all other models implement TP EC 0x2f control */
  5933. if (fans_handle) {
  5934. /* X31, X40, X41 */
  5935. fan_control_access_mode =
  5936. TPACPI_FAN_WR_ACPI_FANS;
  5937. fan_control_commands |=
  5938. TPACPI_FAN_CMD_SPEED |
  5939. TPACPI_FAN_CMD_LEVEL |
  5940. TPACPI_FAN_CMD_ENABLE;
  5941. } else {
  5942. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  5943. fan_control_commands |=
  5944. TPACPI_FAN_CMD_LEVEL |
  5945. TPACPI_FAN_CMD_ENABLE;
  5946. }
  5947. }
  5948. }
  5949. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  5950. "fan is %s, modes %d, %d\n",
  5951. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  5952. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  5953. fan_status_access_mode, fan_control_access_mode);
  5954. /* fan control master switch */
  5955. if (!fan_control_allowed) {
  5956. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  5957. fan_control_commands = 0;
  5958. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  5959. "fan control features disabled by parameter\n");
  5960. }
  5961. /* update fan_control_desired_level */
  5962. if (fan_status_access_mode != TPACPI_FAN_NONE)
  5963. fan_get_status_safe(NULL);
  5964. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  5965. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  5966. if (tp_features.second_fan) {
  5967. /* attach second fan tachometer */
  5968. fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
  5969. &dev_attr_fan_fan2_input.attr;
  5970. }
  5971. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  5972. &fan_attr_group);
  5973. if (rc < 0)
  5974. return rc;
  5975. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  5976. &driver_attr_fan_watchdog);
  5977. if (rc < 0) {
  5978. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  5979. &fan_attr_group);
  5980. return rc;
  5981. }
  5982. return 0;
  5983. } else
  5984. return 1;
  5985. }
  5986. static void fan_exit(void)
  5987. {
  5988. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
  5989. "cancelling any pending fan watchdog tasks\n");
  5990. /* FIXME: can we really do this unconditionally? */
  5991. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  5992. driver_remove_file(&tpacpi_hwmon_pdriver.driver,
  5993. &driver_attr_fan_watchdog);
  5994. cancel_delayed_work(&fan_watchdog_task);
  5995. flush_workqueue(tpacpi_wq);
  5996. }
  5997. static void fan_suspend(pm_message_t state)
  5998. {
  5999. int rc;
  6000. if (!fan_control_allowed)
  6001. return;
  6002. /* Store fan status in cache */
  6003. fan_control_resume_level = 0;
  6004. rc = fan_get_status_safe(&fan_control_resume_level);
  6005. if (rc < 0)
  6006. printk(TPACPI_NOTICE
  6007. "failed to read fan level for later "
  6008. "restore during resume: %d\n", rc);
  6009. /* if it is undefined, don't attempt to restore it.
  6010. * KEEP THIS LAST */
  6011. if (tp_features.fan_ctrl_status_undef)
  6012. fan_control_resume_level = 0;
  6013. }
  6014. static void fan_resume(void)
  6015. {
  6016. u8 current_level = 7;
  6017. bool do_set = false;
  6018. int rc;
  6019. /* DSDT *always* updates status on resume */
  6020. tp_features.fan_ctrl_status_undef = 0;
  6021. if (!fan_control_allowed ||
  6022. !fan_control_resume_level ||
  6023. (fan_get_status_safe(&current_level) < 0))
  6024. return;
  6025. switch (fan_control_access_mode) {
  6026. case TPACPI_FAN_WR_ACPI_SFAN:
  6027. /* never decrease fan level */
  6028. do_set = (fan_control_resume_level > current_level);
  6029. break;
  6030. case TPACPI_FAN_WR_ACPI_FANS:
  6031. case TPACPI_FAN_WR_TPEC:
  6032. /* never decrease fan level, scale is:
  6033. * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
  6034. *
  6035. * We expect the firmware to set either 7 or AUTO, but we
  6036. * handle FULLSPEED out of paranoia.
  6037. *
  6038. * So, we can safely only restore FULLSPEED or 7, anything
  6039. * else could slow the fan. Restoring AUTO is useless, at
  6040. * best that's exactly what the DSDT already set (it is the
  6041. * slower it uses).
  6042. *
  6043. * Always keep in mind that the DSDT *will* have set the
  6044. * fans to what the vendor supposes is the best level. We
  6045. * muck with it only to speed the fan up.
  6046. */
  6047. if (fan_control_resume_level != 7 &&
  6048. !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
  6049. return;
  6050. else
  6051. do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
  6052. (current_level != fan_control_resume_level);
  6053. break;
  6054. default:
  6055. return;
  6056. }
  6057. if (do_set) {
  6058. printk(TPACPI_NOTICE
  6059. "restoring fan level to 0x%02x\n",
  6060. fan_control_resume_level);
  6061. rc = fan_set_level_safe(fan_control_resume_level);
  6062. if (rc < 0)
  6063. printk(TPACPI_NOTICE
  6064. "failed to restore fan level: %d\n", rc);
  6065. }
  6066. }
  6067. static int fan_read(char *p)
  6068. {
  6069. int len = 0;
  6070. int rc;
  6071. u8 status;
  6072. unsigned int speed = 0;
  6073. switch (fan_status_access_mode) {
  6074. case TPACPI_FAN_RD_ACPI_GFAN:
  6075. /* 570, 600e/x, 770e, 770x */
  6076. rc = fan_get_status_safe(&status);
  6077. if (rc < 0)
  6078. return rc;
  6079. len += sprintf(p + len, "status:\t\t%s\n"
  6080. "level:\t\t%d\n",
  6081. (status != 0) ? "enabled" : "disabled", status);
  6082. break;
  6083. case TPACPI_FAN_RD_TPEC:
  6084. /* all except 570, 600e/x, 770e, 770x */
  6085. rc = fan_get_status_safe(&status);
  6086. if (rc < 0)
  6087. return rc;
  6088. len += sprintf(p + len, "status:\t\t%s\n",
  6089. (status != 0) ? "enabled" : "disabled");
  6090. rc = fan_get_speed(&speed);
  6091. if (rc < 0)
  6092. return rc;
  6093. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  6094. if (status & TP_EC_FAN_FULLSPEED)
  6095. /* Disengaged mode takes precedence */
  6096. len += sprintf(p + len, "level:\t\tdisengaged\n");
  6097. else if (status & TP_EC_FAN_AUTO)
  6098. len += sprintf(p + len, "level:\t\tauto\n");
  6099. else
  6100. len += sprintf(p + len, "level:\t\t%d\n", status);
  6101. break;
  6102. case TPACPI_FAN_NONE:
  6103. default:
  6104. len += sprintf(p + len, "status:\t\tnot supported\n");
  6105. }
  6106. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  6107. len += sprintf(p + len, "commands:\tlevel <level>");
  6108. switch (fan_control_access_mode) {
  6109. case TPACPI_FAN_WR_ACPI_SFAN:
  6110. len += sprintf(p + len, " (<level> is 0-7)\n");
  6111. break;
  6112. default:
  6113. len += sprintf(p + len, " (<level> is 0-7, "
  6114. "auto, disengaged, full-speed)\n");
  6115. break;
  6116. }
  6117. }
  6118. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  6119. len += sprintf(p + len, "commands:\tenable, disable\n"
  6120. "commands:\twatchdog <timeout> (<timeout> "
  6121. "is 0 (off), 1-120 (seconds))\n");
  6122. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  6123. len += sprintf(p + len, "commands:\tspeed <speed>"
  6124. " (<speed> is 0-65535)\n");
  6125. return len;
  6126. }
  6127. static int fan_write_cmd_level(const char *cmd, int *rc)
  6128. {
  6129. int level;
  6130. if (strlencmp(cmd, "level auto") == 0)
  6131. level = TP_EC_FAN_AUTO;
  6132. else if ((strlencmp(cmd, "level disengaged") == 0) |
  6133. (strlencmp(cmd, "level full-speed") == 0))
  6134. level = TP_EC_FAN_FULLSPEED;
  6135. else if (sscanf(cmd, "level %d", &level) != 1)
  6136. return 0;
  6137. *rc = fan_set_level_safe(level);
  6138. if (*rc == -ENXIO)
  6139. printk(TPACPI_ERR "level command accepted for unsupported "
  6140. "access mode %d", fan_control_access_mode);
  6141. else if (!*rc)
  6142. tpacpi_disclose_usertask("procfs fan",
  6143. "set level to %d\n", level);
  6144. return 1;
  6145. }
  6146. static int fan_write_cmd_enable(const char *cmd, int *rc)
  6147. {
  6148. if (strlencmp(cmd, "enable") != 0)
  6149. return 0;
  6150. *rc = fan_set_enable();
  6151. if (*rc == -ENXIO)
  6152. printk(TPACPI_ERR "enable command accepted for unsupported "
  6153. "access mode %d", fan_control_access_mode);
  6154. else if (!*rc)
  6155. tpacpi_disclose_usertask("procfs fan", "enable\n");
  6156. return 1;
  6157. }
  6158. static int fan_write_cmd_disable(const char *cmd, int *rc)
  6159. {
  6160. if (strlencmp(cmd, "disable") != 0)
  6161. return 0;
  6162. *rc = fan_set_disable();
  6163. if (*rc == -ENXIO)
  6164. printk(TPACPI_ERR "disable command accepted for unsupported "
  6165. "access mode %d", fan_control_access_mode);
  6166. else if (!*rc)
  6167. tpacpi_disclose_usertask("procfs fan", "disable\n");
  6168. return 1;
  6169. }
  6170. static int fan_write_cmd_speed(const char *cmd, int *rc)
  6171. {
  6172. int speed;
  6173. /* TODO:
  6174. * Support speed <low> <medium> <high> ? */
  6175. if (sscanf(cmd, "speed %d", &speed) != 1)
  6176. return 0;
  6177. *rc = fan_set_speed(speed);
  6178. if (*rc == -ENXIO)
  6179. printk(TPACPI_ERR "speed command accepted for unsupported "
  6180. "access mode %d", fan_control_access_mode);
  6181. else if (!*rc)
  6182. tpacpi_disclose_usertask("procfs fan",
  6183. "set speed to %d\n", speed);
  6184. return 1;
  6185. }
  6186. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  6187. {
  6188. int interval;
  6189. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  6190. return 0;
  6191. if (interval < 0 || interval > 120)
  6192. *rc = -EINVAL;
  6193. else {
  6194. fan_watchdog_maxinterval = interval;
  6195. tpacpi_disclose_usertask("procfs fan",
  6196. "set watchdog timer to %d\n",
  6197. interval);
  6198. }
  6199. return 1;
  6200. }
  6201. static int fan_write(char *buf)
  6202. {
  6203. char *cmd;
  6204. int rc = 0;
  6205. while (!rc && (cmd = next_cmd(&buf))) {
  6206. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  6207. fan_write_cmd_level(cmd, &rc)) &&
  6208. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  6209. (fan_write_cmd_enable(cmd, &rc) ||
  6210. fan_write_cmd_disable(cmd, &rc) ||
  6211. fan_write_cmd_watchdog(cmd, &rc))) &&
  6212. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  6213. fan_write_cmd_speed(cmd, &rc))
  6214. )
  6215. rc = -EINVAL;
  6216. else if (!rc)
  6217. fan_watchdog_reset();
  6218. }
  6219. return rc;
  6220. }
  6221. static struct ibm_struct fan_driver_data = {
  6222. .name = "fan",
  6223. .read = fan_read,
  6224. .write = fan_write,
  6225. .exit = fan_exit,
  6226. .suspend = fan_suspend,
  6227. .resume = fan_resume,
  6228. };
  6229. /****************************************************************************
  6230. ****************************************************************************
  6231. *
  6232. * Infrastructure
  6233. *
  6234. ****************************************************************************
  6235. ****************************************************************************/
  6236. /* sysfs name ---------------------------------------------------------- */
  6237. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  6238. struct device_attribute *attr,
  6239. char *buf)
  6240. {
  6241. return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
  6242. }
  6243. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  6244. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  6245. /* --------------------------------------------------------------------- */
  6246. /* /proc support */
  6247. static struct proc_dir_entry *proc_dir;
  6248. /*
  6249. * Module and infrastructure proble, init and exit handling
  6250. */
  6251. static int force_load;
  6252. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  6253. static const char * __init str_supported(int is_supported)
  6254. {
  6255. static char text_unsupported[] __initdata = "not supported";
  6256. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  6257. }
  6258. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  6259. static void ibm_exit(struct ibm_struct *ibm)
  6260. {
  6261. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  6262. list_del_init(&ibm->all_drivers);
  6263. if (ibm->flags.acpi_notify_installed) {
  6264. dbg_printk(TPACPI_DBG_EXIT,
  6265. "%s: acpi_remove_notify_handler\n", ibm->name);
  6266. BUG_ON(!ibm->acpi);
  6267. acpi_remove_notify_handler(*ibm->acpi->handle,
  6268. ibm->acpi->type,
  6269. dispatch_acpi_notify);
  6270. ibm->flags.acpi_notify_installed = 0;
  6271. ibm->flags.acpi_notify_installed = 0;
  6272. }
  6273. if (ibm->flags.proc_created) {
  6274. dbg_printk(TPACPI_DBG_EXIT,
  6275. "%s: remove_proc_entry\n", ibm->name);
  6276. remove_proc_entry(ibm->name, proc_dir);
  6277. ibm->flags.proc_created = 0;
  6278. }
  6279. if (ibm->flags.acpi_driver_registered) {
  6280. dbg_printk(TPACPI_DBG_EXIT,
  6281. "%s: acpi_bus_unregister_driver\n", ibm->name);
  6282. BUG_ON(!ibm->acpi);
  6283. acpi_bus_unregister_driver(ibm->acpi->driver);
  6284. kfree(ibm->acpi->driver);
  6285. ibm->acpi->driver = NULL;
  6286. ibm->flags.acpi_driver_registered = 0;
  6287. }
  6288. if (ibm->flags.init_called && ibm->exit) {
  6289. ibm->exit();
  6290. ibm->flags.init_called = 0;
  6291. }
  6292. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  6293. }
  6294. static int __init ibm_init(struct ibm_init_struct *iibm)
  6295. {
  6296. int ret;
  6297. struct ibm_struct *ibm = iibm->data;
  6298. struct proc_dir_entry *entry;
  6299. BUG_ON(ibm == NULL);
  6300. INIT_LIST_HEAD(&ibm->all_drivers);
  6301. if (ibm->flags.experimental && !experimental)
  6302. return 0;
  6303. dbg_printk(TPACPI_DBG_INIT,
  6304. "probing for %s\n", ibm->name);
  6305. if (iibm->init) {
  6306. ret = iibm->init(iibm);
  6307. if (ret > 0)
  6308. return 0; /* probe failed */
  6309. if (ret)
  6310. return ret;
  6311. ibm->flags.init_called = 1;
  6312. }
  6313. if (ibm->acpi) {
  6314. if (ibm->acpi->hid) {
  6315. ret = register_tpacpi_subdriver(ibm);
  6316. if (ret)
  6317. goto err_out;
  6318. }
  6319. if (ibm->acpi->notify) {
  6320. ret = setup_acpi_notify(ibm);
  6321. if (ret == -ENODEV) {
  6322. printk(TPACPI_NOTICE "disabling subdriver %s\n",
  6323. ibm->name);
  6324. ret = 0;
  6325. goto err_out;
  6326. }
  6327. if (ret < 0)
  6328. goto err_out;
  6329. }
  6330. }
  6331. dbg_printk(TPACPI_DBG_INIT,
  6332. "%s installed\n", ibm->name);
  6333. if (ibm->read) {
  6334. entry = create_proc_entry(ibm->name,
  6335. S_IFREG | S_IRUGO | S_IWUSR,
  6336. proc_dir);
  6337. if (!entry) {
  6338. printk(TPACPI_ERR "unable to create proc entry %s\n",
  6339. ibm->name);
  6340. ret = -ENODEV;
  6341. goto err_out;
  6342. }
  6343. entry->data = ibm;
  6344. entry->read_proc = &dispatch_procfs_read;
  6345. if (ibm->write)
  6346. entry->write_proc = &dispatch_procfs_write;
  6347. ibm->flags.proc_created = 1;
  6348. }
  6349. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  6350. return 0;
  6351. err_out:
  6352. dbg_printk(TPACPI_DBG_INIT,
  6353. "%s: at error exit path with result %d\n",
  6354. ibm->name, ret);
  6355. ibm_exit(ibm);
  6356. return (ret < 0)? ret : 0;
  6357. }
  6358. /* Probing */
  6359. static bool __pure __init tpacpi_is_fw_digit(const char c)
  6360. {
  6361. return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
  6362. }
  6363. /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
  6364. static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
  6365. const char t)
  6366. {
  6367. return s && strlen(s) >= 8 &&
  6368. tpacpi_is_fw_digit(s[0]) &&
  6369. tpacpi_is_fw_digit(s[1]) &&
  6370. s[2] == t && s[3] == 'T' &&
  6371. tpacpi_is_fw_digit(s[4]) &&
  6372. tpacpi_is_fw_digit(s[5]) &&
  6373. s[6] == 'W' && s[7] == 'W';
  6374. }
  6375. /* returns 0 - probe ok, or < 0 - probe error.
  6376. * Probe ok doesn't mean thinkpad found.
  6377. * On error, kfree() cleanup on tp->* is not performed, caller must do it */
  6378. static int __must_check __init get_thinkpad_model_data(
  6379. struct thinkpad_id_data *tp)
  6380. {
  6381. const struct dmi_device *dev = NULL;
  6382. char ec_fw_string[18];
  6383. char const *s;
  6384. if (!tp)
  6385. return -EINVAL;
  6386. memset(tp, 0, sizeof(*tp));
  6387. if (dmi_name_in_vendors("IBM"))
  6388. tp->vendor = PCI_VENDOR_ID_IBM;
  6389. else if (dmi_name_in_vendors("LENOVO"))
  6390. tp->vendor = PCI_VENDOR_ID_LENOVO;
  6391. else
  6392. return 0;
  6393. s = dmi_get_system_info(DMI_BIOS_VERSION);
  6394. tp->bios_version_str = kstrdup(s, GFP_KERNEL);
  6395. if (s && !tp->bios_version_str)
  6396. return -ENOMEM;
  6397. /* Really ancient ThinkPad 240X will fail this, which is fine */
  6398. if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
  6399. return 0;
  6400. tp->bios_model = tp->bios_version_str[0]
  6401. | (tp->bios_version_str[1] << 8);
  6402. tp->bios_release = (tp->bios_version_str[4] << 8)
  6403. | tp->bios_version_str[5];
  6404. /*
  6405. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  6406. * X32 or newer, all Z series; Some models must have an
  6407. * up-to-date BIOS or they will not be detected.
  6408. *
  6409. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  6410. */
  6411. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  6412. if (sscanf(dev->name,
  6413. "IBM ThinkPad Embedded Controller -[%17c",
  6414. ec_fw_string) == 1) {
  6415. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  6416. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  6417. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  6418. if (!tp->ec_version_str)
  6419. return -ENOMEM;
  6420. if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
  6421. tp->ec_model = ec_fw_string[0]
  6422. | (ec_fw_string[1] << 8);
  6423. tp->ec_release = (ec_fw_string[4] << 8)
  6424. | ec_fw_string[5];
  6425. } else {
  6426. printk(TPACPI_NOTICE
  6427. "ThinkPad firmware release %s "
  6428. "doesn't match the known patterns\n",
  6429. ec_fw_string);
  6430. printk(TPACPI_NOTICE
  6431. "please report this to %s\n",
  6432. TPACPI_MAIL);
  6433. }
  6434. break;
  6435. }
  6436. }
  6437. s = dmi_get_system_info(DMI_PRODUCT_VERSION);
  6438. if (s && !strnicmp(s, "ThinkPad", 8)) {
  6439. tp->model_str = kstrdup(s, GFP_KERNEL);
  6440. if (!tp->model_str)
  6441. return -ENOMEM;
  6442. }
  6443. s = dmi_get_system_info(DMI_PRODUCT_NAME);
  6444. tp->nummodel_str = kstrdup(s, GFP_KERNEL);
  6445. if (s && !tp->nummodel_str)
  6446. return -ENOMEM;
  6447. return 0;
  6448. }
  6449. static int __init probe_for_thinkpad(void)
  6450. {
  6451. int is_thinkpad;
  6452. if (acpi_disabled)
  6453. return -ENODEV;
  6454. /*
  6455. * Non-ancient models have better DMI tagging, but very old models
  6456. * don't. tpacpi_is_fw_known() is a cheat to help in that case.
  6457. */
  6458. is_thinkpad = (thinkpad_id.model_str != NULL) ||
  6459. (thinkpad_id.ec_model != 0) ||
  6460. tpacpi_is_fw_known();
  6461. /* ec is required because many other handles are relative to it */
  6462. TPACPI_ACPIHANDLE_INIT(ec);
  6463. if (!ec_handle) {
  6464. if (is_thinkpad)
  6465. printk(TPACPI_ERR
  6466. "Not yet supported ThinkPad detected!\n");
  6467. return -ENODEV;
  6468. }
  6469. if (!is_thinkpad && !force_load)
  6470. return -ENODEV;
  6471. return 0;
  6472. }
  6473. /* Module init, exit, parameters */
  6474. static struct ibm_init_struct ibms_init[] __initdata = {
  6475. {
  6476. .init = thinkpad_acpi_driver_init,
  6477. .data = &thinkpad_acpi_driver_data,
  6478. },
  6479. {
  6480. .init = hotkey_init,
  6481. .data = &hotkey_driver_data,
  6482. },
  6483. {
  6484. .init = bluetooth_init,
  6485. .data = &bluetooth_driver_data,
  6486. },
  6487. {
  6488. .init = wan_init,
  6489. .data = &wan_driver_data,
  6490. },
  6491. {
  6492. .init = uwb_init,
  6493. .data = &uwb_driver_data,
  6494. },
  6495. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  6496. {
  6497. .init = video_init,
  6498. .data = &video_driver_data,
  6499. },
  6500. #endif
  6501. {
  6502. .init = light_init,
  6503. .data = &light_driver_data,
  6504. },
  6505. {
  6506. .init = cmos_init,
  6507. .data = &cmos_driver_data,
  6508. },
  6509. {
  6510. .init = led_init,
  6511. .data = &led_driver_data,
  6512. },
  6513. {
  6514. .init = beep_init,
  6515. .data = &beep_driver_data,
  6516. },
  6517. {
  6518. .init = thermal_init,
  6519. .data = &thermal_driver_data,
  6520. },
  6521. {
  6522. .data = &ecdump_driver_data,
  6523. },
  6524. {
  6525. .init = brightness_init,
  6526. .data = &brightness_driver_data,
  6527. },
  6528. {
  6529. .data = &volume_driver_data,
  6530. },
  6531. {
  6532. .init = fan_init,
  6533. .data = &fan_driver_data,
  6534. },
  6535. };
  6536. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  6537. {
  6538. unsigned int i;
  6539. struct ibm_struct *ibm;
  6540. if (!kp || !kp->name || !val)
  6541. return -EINVAL;
  6542. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  6543. ibm = ibms_init[i].data;
  6544. WARN_ON(ibm == NULL);
  6545. if (!ibm || !ibm->name)
  6546. continue;
  6547. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  6548. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  6549. return -ENOSPC;
  6550. strcpy(ibms_init[i].param, val);
  6551. strcat(ibms_init[i].param, ",");
  6552. return 0;
  6553. }
  6554. }
  6555. return -EINVAL;
  6556. }
  6557. module_param(experimental, int, 0);
  6558. MODULE_PARM_DESC(experimental,
  6559. "Enables experimental features when non-zero");
  6560. module_param_named(debug, dbg_level, uint, 0);
  6561. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  6562. module_param(force_load, bool, 0);
  6563. MODULE_PARM_DESC(force_load,
  6564. "Attempts to load the driver even on a "
  6565. "mis-identified ThinkPad when true");
  6566. module_param_named(fan_control, fan_control_allowed, bool, 0);
  6567. MODULE_PARM_DESC(fan_control,
  6568. "Enables setting fan parameters features when true");
  6569. module_param_named(brightness_mode, brightness_mode, uint, 0);
  6570. MODULE_PARM_DESC(brightness_mode,
  6571. "Selects brightness control strategy: "
  6572. "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
  6573. module_param(brightness_enable, uint, 0);
  6574. MODULE_PARM_DESC(brightness_enable,
  6575. "Enables backlight control when 1, disables when 0");
  6576. module_param(hotkey_report_mode, uint, 0);
  6577. MODULE_PARM_DESC(hotkey_report_mode,
  6578. "used for backwards compatibility with userspace, "
  6579. "see documentation");
  6580. #define TPACPI_PARAM(feature) \
  6581. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  6582. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
  6583. "at module load, see documentation")
  6584. TPACPI_PARAM(hotkey);
  6585. TPACPI_PARAM(bluetooth);
  6586. TPACPI_PARAM(video);
  6587. TPACPI_PARAM(light);
  6588. TPACPI_PARAM(cmos);
  6589. TPACPI_PARAM(led);
  6590. TPACPI_PARAM(beep);
  6591. TPACPI_PARAM(ecdump);
  6592. TPACPI_PARAM(brightness);
  6593. TPACPI_PARAM(volume);
  6594. TPACPI_PARAM(fan);
  6595. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  6596. module_param(dbg_wlswemul, uint, 0);
  6597. MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
  6598. module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
  6599. MODULE_PARM_DESC(wlsw_state,
  6600. "Initial state of the emulated WLSW switch");
  6601. module_param(dbg_bluetoothemul, uint, 0);
  6602. MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
  6603. module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
  6604. MODULE_PARM_DESC(bluetooth_state,
  6605. "Initial state of the emulated bluetooth switch");
  6606. module_param(dbg_wwanemul, uint, 0);
  6607. MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
  6608. module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
  6609. MODULE_PARM_DESC(wwan_state,
  6610. "Initial state of the emulated WWAN switch");
  6611. module_param(dbg_uwbemul, uint, 0);
  6612. MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
  6613. module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
  6614. MODULE_PARM_DESC(uwb_state,
  6615. "Initial state of the emulated UWB switch");
  6616. #endif
  6617. static void thinkpad_acpi_module_exit(void)
  6618. {
  6619. struct ibm_struct *ibm, *itmp;
  6620. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  6621. list_for_each_entry_safe_reverse(ibm, itmp,
  6622. &tpacpi_all_drivers,
  6623. all_drivers) {
  6624. ibm_exit(ibm);
  6625. }
  6626. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  6627. if (tpacpi_inputdev) {
  6628. if (tp_features.input_device_registered)
  6629. input_unregister_device(tpacpi_inputdev);
  6630. else
  6631. input_free_device(tpacpi_inputdev);
  6632. }
  6633. if (tpacpi_hwmon)
  6634. hwmon_device_unregister(tpacpi_hwmon);
  6635. if (tp_features.sensors_pdev_attrs_registered)
  6636. device_remove_file(&tpacpi_sensors_pdev->dev,
  6637. &dev_attr_thinkpad_acpi_pdev_name);
  6638. if (tpacpi_sensors_pdev)
  6639. platform_device_unregister(tpacpi_sensors_pdev);
  6640. if (tpacpi_pdev)
  6641. platform_device_unregister(tpacpi_pdev);
  6642. if (tp_features.sensors_pdrv_attrs_registered)
  6643. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  6644. if (tp_features.platform_drv_attrs_registered)
  6645. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  6646. if (tp_features.sensors_pdrv_registered)
  6647. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  6648. if (tp_features.platform_drv_registered)
  6649. platform_driver_unregister(&tpacpi_pdriver);
  6650. if (proc_dir)
  6651. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  6652. if (tpacpi_wq)
  6653. destroy_workqueue(tpacpi_wq);
  6654. kfree(thinkpad_id.bios_version_str);
  6655. kfree(thinkpad_id.ec_version_str);
  6656. kfree(thinkpad_id.model_str);
  6657. }
  6658. static int __init thinkpad_acpi_module_init(void)
  6659. {
  6660. int ret, i;
  6661. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  6662. /* Parameter checking */
  6663. if (hotkey_report_mode > 2)
  6664. return -EINVAL;
  6665. /* Driver-level probe */
  6666. ret = get_thinkpad_model_data(&thinkpad_id);
  6667. if (ret) {
  6668. printk(TPACPI_ERR
  6669. "unable to get DMI data: %d\n", ret);
  6670. thinkpad_acpi_module_exit();
  6671. return ret;
  6672. }
  6673. ret = probe_for_thinkpad();
  6674. if (ret) {
  6675. thinkpad_acpi_module_exit();
  6676. return ret;
  6677. }
  6678. /* Driver initialization */
  6679. TPACPI_ACPIHANDLE_INIT(ecrd);
  6680. TPACPI_ACPIHANDLE_INIT(ecwr);
  6681. tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
  6682. if (!tpacpi_wq) {
  6683. thinkpad_acpi_module_exit();
  6684. return -ENOMEM;
  6685. }
  6686. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  6687. if (!proc_dir) {
  6688. printk(TPACPI_ERR
  6689. "unable to create proc dir " TPACPI_PROC_DIR);
  6690. thinkpad_acpi_module_exit();
  6691. return -ENODEV;
  6692. }
  6693. ret = platform_driver_register(&tpacpi_pdriver);
  6694. if (ret) {
  6695. printk(TPACPI_ERR
  6696. "unable to register main platform driver\n");
  6697. thinkpad_acpi_module_exit();
  6698. return ret;
  6699. }
  6700. tp_features.platform_drv_registered = 1;
  6701. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  6702. if (ret) {
  6703. printk(TPACPI_ERR
  6704. "unable to register hwmon platform driver\n");
  6705. thinkpad_acpi_module_exit();
  6706. return ret;
  6707. }
  6708. tp_features.sensors_pdrv_registered = 1;
  6709. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  6710. if (!ret) {
  6711. tp_features.platform_drv_attrs_registered = 1;
  6712. ret = tpacpi_create_driver_attributes(
  6713. &tpacpi_hwmon_pdriver.driver);
  6714. }
  6715. if (ret) {
  6716. printk(TPACPI_ERR
  6717. "unable to create sysfs driver attributes\n");
  6718. thinkpad_acpi_module_exit();
  6719. return ret;
  6720. }
  6721. tp_features.sensors_pdrv_attrs_registered = 1;
  6722. /* Device initialization */
  6723. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
  6724. NULL, 0);
  6725. if (IS_ERR(tpacpi_pdev)) {
  6726. ret = PTR_ERR(tpacpi_pdev);
  6727. tpacpi_pdev = NULL;
  6728. printk(TPACPI_ERR "unable to register platform device\n");
  6729. thinkpad_acpi_module_exit();
  6730. return ret;
  6731. }
  6732. tpacpi_sensors_pdev = platform_device_register_simple(
  6733. TPACPI_HWMON_DRVR_NAME,
  6734. -1, NULL, 0);
  6735. if (IS_ERR(tpacpi_sensors_pdev)) {
  6736. ret = PTR_ERR(tpacpi_sensors_pdev);
  6737. tpacpi_sensors_pdev = NULL;
  6738. printk(TPACPI_ERR
  6739. "unable to register hwmon platform device\n");
  6740. thinkpad_acpi_module_exit();
  6741. return ret;
  6742. }
  6743. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  6744. &dev_attr_thinkpad_acpi_pdev_name);
  6745. if (ret) {
  6746. printk(TPACPI_ERR
  6747. "unable to create sysfs hwmon device attributes\n");
  6748. thinkpad_acpi_module_exit();
  6749. return ret;
  6750. }
  6751. tp_features.sensors_pdev_attrs_registered = 1;
  6752. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  6753. if (IS_ERR(tpacpi_hwmon)) {
  6754. ret = PTR_ERR(tpacpi_hwmon);
  6755. tpacpi_hwmon = NULL;
  6756. printk(TPACPI_ERR "unable to register hwmon device\n");
  6757. thinkpad_acpi_module_exit();
  6758. return ret;
  6759. }
  6760. mutex_init(&tpacpi_inputdev_send_mutex);
  6761. tpacpi_inputdev = input_allocate_device();
  6762. if (!tpacpi_inputdev) {
  6763. printk(TPACPI_ERR "unable to allocate input device\n");
  6764. thinkpad_acpi_module_exit();
  6765. return -ENOMEM;
  6766. } else {
  6767. /* Prepare input device, but don't register */
  6768. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  6769. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  6770. tpacpi_inputdev->id.bustype = BUS_HOST;
  6771. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  6772. thinkpad_id.vendor :
  6773. PCI_VENDOR_ID_IBM;
  6774. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  6775. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  6776. }
  6777. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  6778. ret = ibm_init(&ibms_init[i]);
  6779. if (ret >= 0 && *ibms_init[i].param)
  6780. ret = ibms_init[i].data->write(ibms_init[i].param);
  6781. if (ret < 0) {
  6782. thinkpad_acpi_module_exit();
  6783. return ret;
  6784. }
  6785. }
  6786. ret = input_register_device(tpacpi_inputdev);
  6787. if (ret < 0) {
  6788. printk(TPACPI_ERR "unable to register input device\n");
  6789. thinkpad_acpi_module_exit();
  6790. return ret;
  6791. } else {
  6792. tp_features.input_device_registered = 1;
  6793. }
  6794. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  6795. return 0;
  6796. }
  6797. MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
  6798. /*
  6799. * This will autoload the driver in almost every ThinkPad
  6800. * in widespread use.
  6801. *
  6802. * Only _VERY_ old models, like the 240, 240x and 570 lack
  6803. * the HKEY event interface.
  6804. */
  6805. MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
  6806. /*
  6807. * DMI matching for module autoloading
  6808. *
  6809. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  6810. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  6811. *
  6812. * Only models listed in thinkwiki will be supported, so add yours
  6813. * if it is not there yet.
  6814. */
  6815. #define IBM_BIOS_MODULE_ALIAS(__type) \
  6816. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
  6817. /* Ancient thinkpad BIOSes have to be identified by
  6818. * BIOS type or model number, and there are far less
  6819. * BIOS types than model numbers... */
  6820. IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
  6821. MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
  6822. MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
  6823. MODULE_DESCRIPTION(TPACPI_DESC);
  6824. MODULE_VERSION(TPACPI_VERSION);
  6825. MODULE_LICENSE("GPL");
  6826. module_init(thinkpad_acpi_module_init);
  6827. module_exit(thinkpad_acpi_module_exit);