thinkpad_acpi.c 248 KB

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