thinkpad_acpi.c 197 KB

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