thinkpad_acpi.c 227 KB

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