thinkpad_acpi.c 161 KB

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