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