thinkpad_acpi.c 248 KB

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