thinkpad_acpi.c 250 KB

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