thinkpad_acpi.c 231 KB

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