thinkpad_acpi.c 241 KB

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