thinkpad_acpi.c 231 KB

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