thinkpad_acpi.c 213 KB

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