thinkpad_acpi.c 213 KB

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