thinkpad_acpi.c 166 KB

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