thinkpad_acpi.c 250 KB

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