thinkpad_acpi.c 262 KB

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