thinkpad_acpi.c 231 KB

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