thinkpad_acpi.c 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660
  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_tabletsw(void)
  1061. {
  1062. int state;
  1063. if (tp_features.hotkey_tablet &&
  1064. !hotkey_get_tablet_mode(&state)) {
  1065. mutex_lock(&tpacpi_inputdev_send_mutex);
  1066. input_report_switch(tpacpi_inputdev,
  1067. SW_TABLET_MODE, !!state);
  1068. input_sync(tpacpi_inputdev);
  1069. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1070. }
  1071. }
  1072. static void tpacpi_input_send_key(unsigned int scancode)
  1073. {
  1074. unsigned int keycode;
  1075. keycode = hotkey_keycode_map[scancode];
  1076. if (keycode != KEY_RESERVED) {
  1077. mutex_lock(&tpacpi_inputdev_send_mutex);
  1078. input_report_key(tpacpi_inputdev, keycode, 1);
  1079. if (keycode == KEY_UNKNOWN)
  1080. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1081. scancode);
  1082. input_sync(tpacpi_inputdev);
  1083. input_report_key(tpacpi_inputdev, keycode, 0);
  1084. if (keycode == KEY_UNKNOWN)
  1085. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  1086. scancode);
  1087. input_sync(tpacpi_inputdev);
  1088. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1089. }
  1090. }
  1091. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1092. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1093. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1094. {
  1095. tpacpi_input_send_key(scancode);
  1096. if (hotkey_report_mode < 2) {
  1097. acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
  1098. 0x80, 0x1001 + scancode);
  1099. }
  1100. }
  1101. static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
  1102. {
  1103. u8 d;
  1104. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1105. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1106. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1107. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1108. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1109. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1110. }
  1111. if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
  1112. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1113. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1114. }
  1115. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1116. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1117. n->displayexp_toggle =
  1118. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1119. }
  1120. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1121. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1122. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1123. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1124. n->brightness_toggle =
  1125. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1126. }
  1127. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1128. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1129. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1130. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1131. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1132. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1133. }
  1134. }
  1135. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1136. do { \
  1137. if ((mask & (1 << __scancode)) && \
  1138. oldn->__member != newn->__member) \
  1139. tpacpi_hotkey_send_key(__scancode); \
  1140. } while (0)
  1141. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1142. do { if (mask & (1 << __scancode)) \
  1143. tpacpi_hotkey_send_key(__scancode); } while (0)
  1144. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  1145. struct tp_nvram_state *newn,
  1146. u32 mask)
  1147. {
  1148. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  1149. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  1150. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  1151. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  1152. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  1153. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  1154. /* handle volume */
  1155. if (oldn->volume_toggle != newn->volume_toggle) {
  1156. if (oldn->mute != newn->mute) {
  1157. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1158. }
  1159. if (oldn->volume_level > newn->volume_level) {
  1160. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1161. } else if (oldn->volume_level < newn->volume_level) {
  1162. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1163. } else if (oldn->mute == newn->mute) {
  1164. /* repeated key presses that didn't change state */
  1165. if (newn->mute) {
  1166. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  1167. } else if (newn->volume_level != 0) {
  1168. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1169. } else {
  1170. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1171. }
  1172. }
  1173. }
  1174. /* handle brightness */
  1175. if (oldn->brightness_toggle != newn->brightness_toggle) {
  1176. if (oldn->brightness_level < newn->brightness_level) {
  1177. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1178. } else if (oldn->brightness_level > newn->brightness_level) {
  1179. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1180. } else {
  1181. /* repeated key presses that didn't change state */
  1182. if (newn->brightness_level != 0) {
  1183. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1184. } else {
  1185. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1186. }
  1187. }
  1188. }
  1189. }
  1190. #undef TPACPI_COMPARE_KEY
  1191. #undef TPACPI_MAY_SEND_KEY
  1192. static int hotkey_kthread(void *data)
  1193. {
  1194. struct tp_nvram_state s[2];
  1195. u32 mask;
  1196. unsigned int si, so;
  1197. unsigned long t;
  1198. unsigned int change_detector, must_reset;
  1199. mutex_lock(&hotkey_thread_mutex);
  1200. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  1201. goto exit;
  1202. set_freezable();
  1203. so = 0;
  1204. si = 1;
  1205. t = 0;
  1206. /* Initial state for compares */
  1207. mutex_lock(&hotkey_thread_data_mutex);
  1208. change_detector = hotkey_config_change;
  1209. mask = hotkey_source_mask & hotkey_mask;
  1210. mutex_unlock(&hotkey_thread_data_mutex);
  1211. hotkey_read_nvram(&s[so], mask);
  1212. while (!kthread_should_stop() && hotkey_poll_freq) {
  1213. if (t == 0)
  1214. t = 1000/hotkey_poll_freq;
  1215. t = msleep_interruptible(t);
  1216. if (unlikely(kthread_should_stop()))
  1217. break;
  1218. must_reset = try_to_freeze();
  1219. if (t > 0 && !must_reset)
  1220. continue;
  1221. mutex_lock(&hotkey_thread_data_mutex);
  1222. if (must_reset || hotkey_config_change != change_detector) {
  1223. /* forget old state on thaw or config change */
  1224. si = so;
  1225. t = 0;
  1226. change_detector = hotkey_config_change;
  1227. }
  1228. mask = hotkey_source_mask & hotkey_mask;
  1229. mutex_unlock(&hotkey_thread_data_mutex);
  1230. if (likely(mask)) {
  1231. hotkey_read_nvram(&s[si], mask);
  1232. if (likely(si != so)) {
  1233. hotkey_compare_and_issue_event(&s[so], &s[si],
  1234. mask);
  1235. }
  1236. }
  1237. so = si;
  1238. si ^= 1;
  1239. }
  1240. exit:
  1241. mutex_unlock(&hotkey_thread_mutex);
  1242. return 0;
  1243. }
  1244. static void hotkey_poll_stop_sync(void)
  1245. {
  1246. if (tpacpi_hotkey_task) {
  1247. if (frozen(tpacpi_hotkey_task) ||
  1248. freezing(tpacpi_hotkey_task))
  1249. thaw_process(tpacpi_hotkey_task);
  1250. kthread_stop(tpacpi_hotkey_task);
  1251. tpacpi_hotkey_task = NULL;
  1252. mutex_lock(&hotkey_thread_mutex);
  1253. /* at this point, the thread did exit */
  1254. mutex_unlock(&hotkey_thread_mutex);
  1255. }
  1256. }
  1257. /* call with hotkey_mutex held */
  1258. static void hotkey_poll_setup(int may_warn)
  1259. {
  1260. if ((hotkey_source_mask & hotkey_mask) != 0 &&
  1261. hotkey_poll_freq > 0 &&
  1262. (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
  1263. if (!tpacpi_hotkey_task) {
  1264. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  1265. NULL, TPACPI_NVRAM_KTHREAD_NAME);
  1266. if (IS_ERR(tpacpi_hotkey_task)) {
  1267. tpacpi_hotkey_task = NULL;
  1268. printk(TPACPI_ERR
  1269. "could not create kernel thread "
  1270. "for hotkey polling\n");
  1271. }
  1272. }
  1273. } else {
  1274. hotkey_poll_stop_sync();
  1275. if (may_warn &&
  1276. hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
  1277. printk(TPACPI_NOTICE
  1278. "hot keys 0x%08x require polling, "
  1279. "which is currently disabled\n",
  1280. hotkey_source_mask);
  1281. }
  1282. }
  1283. }
  1284. static void hotkey_poll_setup_safe(int may_warn)
  1285. {
  1286. mutex_lock(&hotkey_mutex);
  1287. hotkey_poll_setup(may_warn);
  1288. mutex_unlock(&hotkey_mutex);
  1289. }
  1290. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1291. static void hotkey_poll_setup_safe(int __unused)
  1292. {
  1293. }
  1294. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1295. static int hotkey_inputdev_open(struct input_dev *dev)
  1296. {
  1297. switch (tpacpi_lifecycle) {
  1298. case TPACPI_LIFE_INIT:
  1299. /*
  1300. * hotkey_init will call hotkey_poll_setup_safe
  1301. * at the appropriate moment
  1302. */
  1303. return 0;
  1304. case TPACPI_LIFE_EXITING:
  1305. return -EBUSY;
  1306. case TPACPI_LIFE_RUNNING:
  1307. hotkey_poll_setup_safe(0);
  1308. return 0;
  1309. }
  1310. /* Should only happen if tpacpi_lifecycle is corrupt */
  1311. BUG();
  1312. return -EBUSY;
  1313. }
  1314. static void hotkey_inputdev_close(struct input_dev *dev)
  1315. {
  1316. /* disable hotkey polling when possible */
  1317. if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
  1318. hotkey_poll_setup_safe(0);
  1319. }
  1320. /* sysfs hotkey enable ------------------------------------------------- */
  1321. static ssize_t hotkey_enable_show(struct device *dev,
  1322. struct device_attribute *attr,
  1323. char *buf)
  1324. {
  1325. int res, status;
  1326. res = hotkey_status_get(&status);
  1327. if (res)
  1328. return res;
  1329. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  1330. }
  1331. static ssize_t hotkey_enable_store(struct device *dev,
  1332. struct device_attribute *attr,
  1333. const char *buf, size_t count)
  1334. {
  1335. unsigned long t;
  1336. int res;
  1337. if (parse_strtoul(buf, 1, &t))
  1338. return -EINVAL;
  1339. res = hotkey_status_set(t);
  1340. return (res) ? res : count;
  1341. }
  1342. static struct device_attribute dev_attr_hotkey_enable =
  1343. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  1344. hotkey_enable_show, hotkey_enable_store);
  1345. /* sysfs hotkey mask --------------------------------------------------- */
  1346. static ssize_t hotkey_mask_show(struct device *dev,
  1347. struct device_attribute *attr,
  1348. char *buf)
  1349. {
  1350. int res;
  1351. if (mutex_lock_interruptible(&hotkey_mutex))
  1352. return -ERESTARTSYS;
  1353. res = hotkey_mask_get();
  1354. mutex_unlock(&hotkey_mutex);
  1355. return (res)?
  1356. res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
  1357. }
  1358. static ssize_t hotkey_mask_store(struct device *dev,
  1359. struct device_attribute *attr,
  1360. const char *buf, size_t count)
  1361. {
  1362. unsigned long t;
  1363. int res;
  1364. if (parse_strtoul(buf, 0xffffffffUL, &t))
  1365. return -EINVAL;
  1366. if (mutex_lock_interruptible(&hotkey_mutex))
  1367. return -ERESTARTSYS;
  1368. res = hotkey_mask_set(t);
  1369. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1370. hotkey_poll_setup(1);
  1371. #endif
  1372. mutex_unlock(&hotkey_mutex);
  1373. return (res) ? res : count;
  1374. }
  1375. static struct device_attribute dev_attr_hotkey_mask =
  1376. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  1377. hotkey_mask_show, hotkey_mask_store);
  1378. /* sysfs hotkey bios_enabled ------------------------------------------- */
  1379. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  1380. struct device_attribute *attr,
  1381. char *buf)
  1382. {
  1383. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
  1384. }
  1385. static struct device_attribute dev_attr_hotkey_bios_enabled =
  1386. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  1387. /* sysfs hotkey bios_mask ---------------------------------------------- */
  1388. static ssize_t hotkey_bios_mask_show(struct device *dev,
  1389. struct device_attribute *attr,
  1390. char *buf)
  1391. {
  1392. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  1393. }
  1394. static struct device_attribute dev_attr_hotkey_bios_mask =
  1395. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  1396. /* sysfs hotkey all_mask ----------------------------------------------- */
  1397. static ssize_t hotkey_all_mask_show(struct device *dev,
  1398. struct device_attribute *attr,
  1399. char *buf)
  1400. {
  1401. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  1402. hotkey_all_mask | hotkey_source_mask);
  1403. }
  1404. static struct device_attribute dev_attr_hotkey_all_mask =
  1405. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  1406. /* sysfs hotkey recommended_mask --------------------------------------- */
  1407. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  1408. struct device_attribute *attr,
  1409. char *buf)
  1410. {
  1411. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  1412. (hotkey_all_mask | hotkey_source_mask)
  1413. & ~hotkey_reserved_mask);
  1414. }
  1415. static struct device_attribute dev_attr_hotkey_recommended_mask =
  1416. __ATTR(hotkey_recommended_mask, S_IRUGO,
  1417. hotkey_recommended_mask_show, NULL);
  1418. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1419. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  1420. static ssize_t hotkey_source_mask_show(struct device *dev,
  1421. struct device_attribute *attr,
  1422. char *buf)
  1423. {
  1424. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
  1425. }
  1426. static ssize_t hotkey_source_mask_store(struct device *dev,
  1427. struct device_attribute *attr,
  1428. const char *buf, size_t count)
  1429. {
  1430. unsigned long t;
  1431. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  1432. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  1433. return -EINVAL;
  1434. if (mutex_lock_interruptible(&hotkey_mutex))
  1435. return -ERESTARTSYS;
  1436. HOTKEY_CONFIG_CRITICAL_START
  1437. hotkey_source_mask = t;
  1438. HOTKEY_CONFIG_CRITICAL_END
  1439. hotkey_poll_setup(1);
  1440. mutex_unlock(&hotkey_mutex);
  1441. return count;
  1442. }
  1443. static struct device_attribute dev_attr_hotkey_source_mask =
  1444. __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
  1445. hotkey_source_mask_show, hotkey_source_mask_store);
  1446. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  1447. static ssize_t hotkey_poll_freq_show(struct device *dev,
  1448. struct device_attribute *attr,
  1449. char *buf)
  1450. {
  1451. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
  1452. }
  1453. static ssize_t hotkey_poll_freq_store(struct device *dev,
  1454. struct device_attribute *attr,
  1455. const char *buf, size_t count)
  1456. {
  1457. unsigned long t;
  1458. if (parse_strtoul(buf, 25, &t))
  1459. return -EINVAL;
  1460. if (mutex_lock_interruptible(&hotkey_mutex))
  1461. return -ERESTARTSYS;
  1462. hotkey_poll_freq = t;
  1463. hotkey_poll_setup(1);
  1464. mutex_unlock(&hotkey_mutex);
  1465. return count;
  1466. }
  1467. static struct device_attribute dev_attr_hotkey_poll_freq =
  1468. __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
  1469. hotkey_poll_freq_show, hotkey_poll_freq_store);
  1470. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1471. /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
  1472. static ssize_t hotkey_radio_sw_show(struct device *dev,
  1473. struct device_attribute *attr,
  1474. char *buf)
  1475. {
  1476. int res, s;
  1477. res = hotkey_get_wlsw(&s);
  1478. if (res < 0)
  1479. return res;
  1480. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  1481. }
  1482. static struct device_attribute dev_attr_hotkey_radio_sw =
  1483. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  1484. static void hotkey_radio_sw_notify_change(void)
  1485. {
  1486. if (tp_features.hotkey_wlsw)
  1487. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1488. "hotkey_radio_sw");
  1489. }
  1490. /* sysfs hotkey tablet mode (pollable) --------------------------------- */
  1491. static ssize_t hotkey_tablet_mode_show(struct device *dev,
  1492. struct device_attribute *attr,
  1493. char *buf)
  1494. {
  1495. int res, s;
  1496. res = hotkey_get_tablet_mode(&s);
  1497. if (res < 0)
  1498. return res;
  1499. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  1500. }
  1501. static struct device_attribute dev_attr_hotkey_tablet_mode =
  1502. __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
  1503. static void hotkey_tablet_mode_notify_change(void)
  1504. {
  1505. if (tp_features.hotkey_tablet)
  1506. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1507. "hotkey_tablet_mode");
  1508. }
  1509. /* sysfs hotkey report_mode -------------------------------------------- */
  1510. static ssize_t hotkey_report_mode_show(struct device *dev,
  1511. struct device_attribute *attr,
  1512. char *buf)
  1513. {
  1514. return snprintf(buf, PAGE_SIZE, "%d\n",
  1515. (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
  1516. }
  1517. static struct device_attribute dev_attr_hotkey_report_mode =
  1518. __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
  1519. /* sysfs wakeup reason (pollable) -------------------------------------- */
  1520. static ssize_t hotkey_wakeup_reason_show(struct device *dev,
  1521. struct device_attribute *attr,
  1522. char *buf)
  1523. {
  1524. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
  1525. }
  1526. static struct device_attribute dev_attr_hotkey_wakeup_reason =
  1527. __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
  1528. static void hotkey_wakeup_reason_notify_change(void)
  1529. {
  1530. if (tp_features.hotkey_mask)
  1531. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1532. "wakeup_reason");
  1533. }
  1534. /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
  1535. static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
  1536. struct device_attribute *attr,
  1537. char *buf)
  1538. {
  1539. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
  1540. }
  1541. static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
  1542. __ATTR(wakeup_hotunplug_complete, S_IRUGO,
  1543. hotkey_wakeup_hotunplug_complete_show, NULL);
  1544. static void hotkey_wakeup_hotunplug_complete_notify_change(void)
  1545. {
  1546. if (tp_features.hotkey_mask)
  1547. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  1548. "wakeup_hotunplug_complete");
  1549. }
  1550. /* --------------------------------------------------------------------- */
  1551. static struct attribute *hotkey_attributes[] __initdata = {
  1552. &dev_attr_hotkey_enable.attr,
  1553. &dev_attr_hotkey_bios_enabled.attr,
  1554. &dev_attr_hotkey_report_mode.attr,
  1555. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1556. &dev_attr_hotkey_mask.attr,
  1557. &dev_attr_hotkey_all_mask.attr,
  1558. &dev_attr_hotkey_recommended_mask.attr,
  1559. &dev_attr_hotkey_source_mask.attr,
  1560. &dev_attr_hotkey_poll_freq.attr,
  1561. #endif
  1562. };
  1563. static struct attribute *hotkey_mask_attributes[] __initdata = {
  1564. &dev_attr_hotkey_bios_mask.attr,
  1565. #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1566. &dev_attr_hotkey_mask.attr,
  1567. &dev_attr_hotkey_all_mask.attr,
  1568. &dev_attr_hotkey_recommended_mask.attr,
  1569. #endif
  1570. &dev_attr_hotkey_wakeup_reason.attr,
  1571. &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
  1572. };
  1573. static void tpacpi_send_radiosw_update(void)
  1574. {
  1575. int wlsw;
  1576. if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
  1577. mutex_lock(&tpacpi_inputdev_send_mutex);
  1578. input_report_switch(tpacpi_inputdev,
  1579. SW_RFKILL_ALL, !!wlsw);
  1580. input_sync(tpacpi_inputdev);
  1581. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1582. }
  1583. hotkey_radio_sw_notify_change();
  1584. }
  1585. static void hotkey_exit(void)
  1586. {
  1587. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1588. hotkey_poll_stop_sync();
  1589. #endif
  1590. if (hotkey_dev_attributes)
  1591. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  1592. kfree(hotkey_keycode_map);
  1593. if (tp_features.hotkey) {
  1594. dbg_printk(TPACPI_DBG_EXIT,
  1595. "restoring original hot key mask\n");
  1596. /* no short-circuit boolean operator below! */
  1597. if ((hotkey_mask_set(hotkey_orig_mask) |
  1598. hotkey_status_set(hotkey_orig_status)) != 0)
  1599. printk(TPACPI_ERR
  1600. "failed to restore hot key mask "
  1601. "to BIOS defaults\n");
  1602. }
  1603. }
  1604. static int __init hotkey_init(struct ibm_init_struct *iibm)
  1605. {
  1606. /* Requirements for changing the default keymaps:
  1607. *
  1608. * 1. Many of the keys are mapped to KEY_RESERVED for very
  1609. * good reasons. Do not change them unless you have deep
  1610. * knowledge on the IBM and Lenovo ThinkPad firmware for
  1611. * the various ThinkPad models. The driver behaves
  1612. * differently for KEY_RESERVED: such keys have their
  1613. * hot key mask *unset* in mask_recommended, and also
  1614. * in the initial hot key mask programmed into the
  1615. * firmware at driver load time, which means the firm-
  1616. * ware may react very differently if you change them to
  1617. * something else;
  1618. *
  1619. * 2. You must be subscribed to the linux-thinkpad and
  1620. * ibm-acpi-devel mailing lists, and you should read the
  1621. * list archives since 2007 if you want to change the
  1622. * keymaps. This requirement exists so that you will
  1623. * know the past history of problems with the thinkpad-
  1624. * acpi driver keymaps, and also that you will be
  1625. * listening to any bug reports;
  1626. *
  1627. * 3. Do not send thinkpad-acpi specific patches directly to
  1628. * for merging, *ever*. Send them to the linux-acpi
  1629. * mailinglist for comments. Merging is to be done only
  1630. * through acpi-test and the ACPI maintainer.
  1631. *
  1632. * If the above is too much to ask, don't change the keymap.
  1633. * Ask the thinkpad-acpi maintainer to do it, instead.
  1634. */
  1635. static u16 ibm_keycode_map[] __initdata = {
  1636. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  1637. KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
  1638. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  1639. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  1640. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  1641. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  1642. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  1643. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  1644. /* brightness: firmware always reacts to them, unless
  1645. * X.org did some tricks in the radeon BIOS scratch
  1646. * registers of *some* models */
  1647. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  1648. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  1649. /* Thinklight: firmware always react to it */
  1650. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  1651. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  1652. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  1653. /* Volume: firmware always react to it and reprograms
  1654. * the built-in *extra* mixer. Never map it to control
  1655. * another mixer by default. */
  1656. KEY_RESERVED, /* 0x14: VOLUME UP */
  1657. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  1658. KEY_RESERVED, /* 0x16: MUTE */
  1659. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  1660. /* (assignments unknown, please report if found) */
  1661. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1662. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1663. };
  1664. static u16 lenovo_keycode_map[] __initdata = {
  1665. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  1666. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  1667. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  1668. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  1669. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  1670. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  1671. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  1672. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  1673. /* These either have to go through ACPI video, or
  1674. * act like in the IBM ThinkPads, so don't ever
  1675. * enable them by default */
  1676. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  1677. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  1678. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  1679. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  1680. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  1681. /* Volume: z60/z61, T60 (BIOS version?): firmware always
  1682. * react to it and reprograms the built-in *extra* mixer.
  1683. * Never map it to control another mixer by default.
  1684. *
  1685. * T60?, T61, R60?, R61: firmware and EC tries to send
  1686. * these over the regular keyboard, so these are no-ops,
  1687. * but there are still weird bugs re. MUTE, so do not
  1688. * change unless you get test reports from all Lenovo
  1689. * models. May cause the BIOS to interfere with the
  1690. * HDA mixer.
  1691. */
  1692. KEY_RESERVED, /* 0x14: VOLUME UP */
  1693. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  1694. KEY_RESERVED, /* 0x16: MUTE */
  1695. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  1696. /* (assignments unknown, please report if found) */
  1697. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1698. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  1699. };
  1700. #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
  1701. #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
  1702. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
  1703. int res, i;
  1704. int status;
  1705. int hkeyv;
  1706. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  1707. BUG_ON(!tpacpi_inputdev);
  1708. BUG_ON(tpacpi_inputdev->open != NULL ||
  1709. tpacpi_inputdev->close != NULL);
  1710. TPACPI_ACPIHANDLE_INIT(hkey);
  1711. mutex_init(&hotkey_mutex);
  1712. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1713. mutex_init(&hotkey_thread_mutex);
  1714. mutex_init(&hotkey_thread_data_mutex);
  1715. #endif
  1716. /* hotkey not supported on 570 */
  1717. tp_features.hotkey = hkey_handle != NULL;
  1718. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  1719. str_supported(tp_features.hotkey));
  1720. if (!tp_features.hotkey)
  1721. return 1;
  1722. hotkey_dev_attributes = create_attr_set(13, NULL);
  1723. if (!hotkey_dev_attributes)
  1724. return -ENOMEM;
  1725. res = add_many_to_attr_set(hotkey_dev_attributes,
  1726. hotkey_attributes,
  1727. ARRAY_SIZE(hotkey_attributes));
  1728. if (res)
  1729. goto err_exit;
  1730. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1731. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  1732. for HKEY interface version 0x100 */
  1733. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  1734. if ((hkeyv >> 8) != 1) {
  1735. printk(TPACPI_ERR "unknown version of the "
  1736. "HKEY interface: 0x%x\n", hkeyv);
  1737. printk(TPACPI_ERR "please report this to %s\n",
  1738. TPACPI_MAIL);
  1739. } else {
  1740. /*
  1741. * MHKV 0x100 in A31, R40, R40e,
  1742. * T4x, X31, and later
  1743. */
  1744. tp_features.hotkey_mask = 1;
  1745. }
  1746. }
  1747. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  1748. str_supported(tp_features.hotkey_mask));
  1749. if (tp_features.hotkey_mask) {
  1750. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  1751. "MHKA", "qd")) {
  1752. printk(TPACPI_ERR
  1753. "missing MHKA handler, "
  1754. "please report this to %s\n",
  1755. TPACPI_MAIL);
  1756. /* FN+F12, FN+F4, FN+F3 */
  1757. hotkey_all_mask = 0x080cU;
  1758. }
  1759. }
  1760. /* hotkey_source_mask *must* be zero for
  1761. * the first hotkey_mask_get */
  1762. res = hotkey_status_get(&hotkey_orig_status);
  1763. if (res)
  1764. goto err_exit;
  1765. if (tp_features.hotkey_mask) {
  1766. res = hotkey_mask_get();
  1767. if (res)
  1768. goto err_exit;
  1769. hotkey_orig_mask = hotkey_mask;
  1770. res = add_many_to_attr_set(
  1771. hotkey_dev_attributes,
  1772. hotkey_mask_attributes,
  1773. ARRAY_SIZE(hotkey_mask_attributes));
  1774. if (res)
  1775. goto err_exit;
  1776. }
  1777. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1778. if (tp_features.hotkey_mask) {
  1779. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  1780. & ~hotkey_all_mask;
  1781. } else {
  1782. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
  1783. }
  1784. vdbg_printk(TPACPI_DBG_INIT,
  1785. "hotkey source mask 0x%08x, polling freq %d\n",
  1786. hotkey_source_mask, hotkey_poll_freq);
  1787. #endif
  1788. /* Not all thinkpads have a hardware radio switch */
  1789. if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  1790. tp_features.hotkey_wlsw = 1;
  1791. printk(TPACPI_INFO
  1792. "radio switch found; radios are %s\n",
  1793. enabled(status, 0));
  1794. }
  1795. if (tp_features.hotkey_wlsw)
  1796. res = add_to_attr_set(hotkey_dev_attributes,
  1797. &dev_attr_hotkey_radio_sw.attr);
  1798. /* For X41t, X60t, X61t Tablets... */
  1799. if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
  1800. tp_features.hotkey_tablet = 1;
  1801. printk(TPACPI_INFO
  1802. "possible tablet mode switch found; "
  1803. "ThinkPad in %s mode\n",
  1804. (status & TP_HOTKEY_TABLET_MASK)?
  1805. "tablet" : "laptop");
  1806. res = add_to_attr_set(hotkey_dev_attributes,
  1807. &dev_attr_hotkey_tablet_mode.attr);
  1808. }
  1809. if (!res)
  1810. res = register_attr_set_with_sysfs(
  1811. hotkey_dev_attributes,
  1812. &tpacpi_pdev->dev.kobj);
  1813. if (res)
  1814. goto err_exit;
  1815. /* Set up key map */
  1816. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  1817. GFP_KERNEL);
  1818. if (!hotkey_keycode_map) {
  1819. printk(TPACPI_ERR
  1820. "failed to allocate memory for key map\n");
  1821. res = -ENOMEM;
  1822. goto err_exit;
  1823. }
  1824. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  1825. dbg_printk(TPACPI_DBG_INIT,
  1826. "using Lenovo default hot key map\n");
  1827. memcpy(hotkey_keycode_map, &lenovo_keycode_map,
  1828. TPACPI_HOTKEY_MAP_SIZE);
  1829. } else {
  1830. dbg_printk(TPACPI_DBG_INIT,
  1831. "using IBM default hot key map\n");
  1832. memcpy(hotkey_keycode_map, &ibm_keycode_map,
  1833. TPACPI_HOTKEY_MAP_SIZE);
  1834. }
  1835. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  1836. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  1837. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  1838. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  1839. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  1840. tpacpi_inputdev->keycode = hotkey_keycode_map;
  1841. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  1842. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  1843. set_bit(hotkey_keycode_map[i],
  1844. tpacpi_inputdev->keybit);
  1845. } else {
  1846. if (i < sizeof(hotkey_reserved_mask)*8)
  1847. hotkey_reserved_mask |= 1 << i;
  1848. }
  1849. }
  1850. if (tp_features.hotkey_wlsw) {
  1851. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1852. set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
  1853. }
  1854. if (tp_features.hotkey_tablet) {
  1855. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1856. set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
  1857. }
  1858. /* Do not issue duplicate brightness change events to
  1859. * userspace */
  1860. if (!tp_features.bright_acpimode)
  1861. /* update bright_acpimode... */
  1862. tpacpi_check_std_acpi_brightness_support();
  1863. if (tp_features.bright_acpimode) {
  1864. printk(TPACPI_INFO
  1865. "This ThinkPad has standard ACPI backlight "
  1866. "brightness control, supported by the ACPI "
  1867. "video driver\n");
  1868. printk(TPACPI_NOTICE
  1869. "Disabling thinkpad-acpi brightness events "
  1870. "by default...\n");
  1871. /* The hotkey_reserved_mask change below is not
  1872. * necessary while the keys are at KEY_RESERVED in the
  1873. * default map, but better safe than sorry, leave it
  1874. * here as a marker of what we have to do, especially
  1875. * when we finally become able to set this at runtime
  1876. * on response to X.org requests */
  1877. hotkey_reserved_mask |=
  1878. (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
  1879. | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
  1880. }
  1881. dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
  1882. res = hotkey_status_set(1);
  1883. if (res) {
  1884. hotkey_exit();
  1885. return res;
  1886. }
  1887. res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
  1888. & ~hotkey_reserved_mask)
  1889. | hotkey_orig_mask);
  1890. if (res < 0 && res != -ENXIO) {
  1891. hotkey_exit();
  1892. return res;
  1893. }
  1894. dbg_printk(TPACPI_DBG_INIT,
  1895. "legacy hot key reporting over procfs %s\n",
  1896. (hotkey_report_mode < 2) ?
  1897. "enabled" : "disabled");
  1898. tpacpi_inputdev->open = &hotkey_inputdev_open;
  1899. tpacpi_inputdev->close = &hotkey_inputdev_close;
  1900. hotkey_poll_setup_safe(1);
  1901. tpacpi_send_radiosw_update();
  1902. tpacpi_input_send_tabletsw();
  1903. return 0;
  1904. err_exit:
  1905. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  1906. hotkey_dev_attributes = NULL;
  1907. return (res < 0)? res : 1;
  1908. }
  1909. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  1910. {
  1911. u32 hkey;
  1912. unsigned int scancode;
  1913. int send_acpi_ev;
  1914. int ignore_acpi_ev;
  1915. int unk_ev;
  1916. if (event != 0x80) {
  1917. printk(TPACPI_ERR
  1918. "unknown HKEY notification event %d\n", event);
  1919. /* forward it to userspace, maybe it knows how to handle it */
  1920. acpi_bus_generate_netlink_event(
  1921. ibm->acpi->device->pnp.device_class,
  1922. ibm->acpi->device->dev.bus_id,
  1923. event, 0);
  1924. return;
  1925. }
  1926. while (1) {
  1927. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  1928. printk(TPACPI_ERR "failed to retrieve HKEY event\n");
  1929. return;
  1930. }
  1931. if (hkey == 0) {
  1932. /* queue empty */
  1933. return;
  1934. }
  1935. send_acpi_ev = 1;
  1936. ignore_acpi_ev = 0;
  1937. unk_ev = 0;
  1938. switch (hkey >> 12) {
  1939. case 1:
  1940. /* 0x1000-0x1FFF: key presses */
  1941. scancode = hkey & 0xfff;
  1942. if (scancode > 0 && scancode < 0x21) {
  1943. scancode--;
  1944. if (!(hotkey_source_mask & (1 << scancode))) {
  1945. tpacpi_input_send_key(scancode);
  1946. send_acpi_ev = 0;
  1947. } else {
  1948. ignore_acpi_ev = 1;
  1949. }
  1950. } else {
  1951. unk_ev = 1;
  1952. }
  1953. break;
  1954. case 2:
  1955. /* Wakeup reason */
  1956. switch (hkey) {
  1957. case 0x2304: /* suspend, undock */
  1958. case 0x2404: /* hibernation, undock */
  1959. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  1960. ignore_acpi_ev = 1;
  1961. break;
  1962. case 0x2305: /* suspend, bay eject */
  1963. case 0x2405: /* hibernation, bay eject */
  1964. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  1965. ignore_acpi_ev = 1;
  1966. break;
  1967. default:
  1968. unk_ev = 1;
  1969. }
  1970. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  1971. printk(TPACPI_INFO
  1972. "woke up due to a hot-unplug "
  1973. "request...\n");
  1974. hotkey_wakeup_reason_notify_change();
  1975. }
  1976. break;
  1977. case 3:
  1978. /* bay-related wakeups */
  1979. if (hkey == 0x3003) {
  1980. hotkey_autosleep_ack = 1;
  1981. printk(TPACPI_INFO
  1982. "bay ejected\n");
  1983. hotkey_wakeup_hotunplug_complete_notify_change();
  1984. } else {
  1985. unk_ev = 1;
  1986. }
  1987. break;
  1988. case 4:
  1989. /* dock-related wakeups */
  1990. if (hkey == 0x4003) {
  1991. hotkey_autosleep_ack = 1;
  1992. printk(TPACPI_INFO
  1993. "undocked\n");
  1994. hotkey_wakeup_hotunplug_complete_notify_change();
  1995. } else {
  1996. unk_ev = 1;
  1997. }
  1998. break;
  1999. case 5:
  2000. /* 0x5000-0x5FFF: human interface helpers */
  2001. switch (hkey) {
  2002. case 0x5010: /* Lenovo new BIOS: brightness changed */
  2003. case 0x500b: /* X61t: tablet pen inserted into bay */
  2004. case 0x500c: /* X61t: tablet pen removed from bay */
  2005. break;
  2006. case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
  2007. case 0x500a: /* X41t-X61t: swivel down (normal mode) */
  2008. tpacpi_input_send_tabletsw();
  2009. hotkey_tablet_mode_notify_change();
  2010. send_acpi_ev = 0;
  2011. break;
  2012. case 0x5001:
  2013. case 0x5002:
  2014. /* LID switch events. Do not propagate */
  2015. ignore_acpi_ev = 1;
  2016. break;
  2017. default:
  2018. unk_ev = 1;
  2019. }
  2020. break;
  2021. case 7:
  2022. /* 0x7000-0x7FFF: misc */
  2023. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  2024. tpacpi_send_radiosw_update();
  2025. send_acpi_ev = 0;
  2026. break;
  2027. }
  2028. /* fallthrough to default */
  2029. default:
  2030. unk_ev = 1;
  2031. }
  2032. if (unk_ev) {
  2033. printk(TPACPI_NOTICE
  2034. "unhandled HKEY event 0x%04x\n", hkey);
  2035. }
  2036. /* Legacy events */
  2037. if (!ignore_acpi_ev &&
  2038. (send_acpi_ev || hotkey_report_mode < 2)) {
  2039. acpi_bus_generate_proc_event(ibm->acpi->device,
  2040. event, hkey);
  2041. }
  2042. /* netlink events */
  2043. if (!ignore_acpi_ev && send_acpi_ev) {
  2044. acpi_bus_generate_netlink_event(
  2045. ibm->acpi->device->pnp.device_class,
  2046. ibm->acpi->device->dev.bus_id,
  2047. event, hkey);
  2048. }
  2049. }
  2050. }
  2051. static void hotkey_suspend(pm_message_t state)
  2052. {
  2053. /* Do these on suspend, we get the events on early resume! */
  2054. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  2055. hotkey_autosleep_ack = 0;
  2056. }
  2057. static void hotkey_resume(void)
  2058. {
  2059. if (hotkey_mask_get())
  2060. printk(TPACPI_ERR
  2061. "error while trying to read hot key mask "
  2062. "from firmware\n");
  2063. tpacpi_send_radiosw_update();
  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. !(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. }
  2217. if (tp_features.bluetooth) {
  2218. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2219. &bluetooth_attr_group);
  2220. if (res)
  2221. return res;
  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. !(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. }
  2351. if (tp_features.wan) {
  2352. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2353. &wan_attr_group);
  2354. if (res)
  2355. return res;
  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(const 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(const unsigned int led,
  3225. const enum led_status_t ledstatus)
  3226. {
  3227. /* off, on, blink. Index is led_status_t */
  3228. static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
  3229. static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
  3230. int rc = 0;
  3231. switch (led_supported) {
  3232. case TPACPI_LED_570:
  3233. /* 570 */
  3234. if (led > 7)
  3235. return -EINVAL;
  3236. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  3237. (1 << 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. if (led > 7)
  3243. return -EINVAL;
  3244. rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
  3245. if (rc >= 0)
  3246. rc = ec_write(TPACPI_LED_EC_HLBL,
  3247. (ledstatus == TPACPI_LED_BLINK) << led);
  3248. if (rc >= 0)
  3249. rc = ec_write(TPACPI_LED_EC_HLCL,
  3250. (ledstatus != TPACPI_LED_OFF) << led);
  3251. break;
  3252. case TPACPI_LED_NEW:
  3253. /* all others */
  3254. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  3255. led, led_led_arg1[ledstatus]))
  3256. rc = -EIO;
  3257. break;
  3258. default:
  3259. rc = -ENXIO;
  3260. }
  3261. if (!rc)
  3262. tpacpi_led_state_cache[led] = ledstatus;
  3263. return rc;
  3264. }
  3265. static void led_sysfs_set_status(unsigned int led,
  3266. enum led_brightness brightness)
  3267. {
  3268. led_set_status(led,
  3269. (brightness == LED_OFF) ?
  3270. TPACPI_LED_OFF :
  3271. (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
  3272. TPACPI_LED_BLINK : TPACPI_LED_ON);
  3273. }
  3274. static void led_set_status_worker(struct work_struct *work)
  3275. {
  3276. struct tpacpi_led_classdev *data =
  3277. container_of(work, struct tpacpi_led_classdev, work);
  3278. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  3279. led_sysfs_set_status(data->led, data->new_brightness);
  3280. }
  3281. static void led_sysfs_set(struct led_classdev *led_cdev,
  3282. enum led_brightness brightness)
  3283. {
  3284. struct tpacpi_led_classdev *data = container_of(led_cdev,
  3285. struct tpacpi_led_classdev, led_classdev);
  3286. data->new_brightness = brightness;
  3287. queue_work(tpacpi_wq, &data->work);
  3288. }
  3289. static int led_sysfs_blink_set(struct led_classdev *led_cdev,
  3290. unsigned long *delay_on, unsigned long *delay_off)
  3291. {
  3292. struct tpacpi_led_classdev *data = container_of(led_cdev,
  3293. struct tpacpi_led_classdev, led_classdev);
  3294. /* Can we choose the flash rate? */
  3295. if (*delay_on == 0 && *delay_off == 0) {
  3296. /* yes. set them to the hardware blink rate (1 Hz) */
  3297. *delay_on = 500; /* ms */
  3298. *delay_off = 500; /* ms */
  3299. } else if ((*delay_on != 500) || (*delay_off != 500))
  3300. return -EINVAL;
  3301. data->new_brightness = TPACPI_LED_BLINK;
  3302. queue_work(tpacpi_wq, &data->work);
  3303. return 0;
  3304. }
  3305. static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
  3306. {
  3307. int rc;
  3308. struct tpacpi_led_classdev *data = container_of(led_cdev,
  3309. struct tpacpi_led_classdev, led_classdev);
  3310. rc = led_get_status(data->led);
  3311. if (rc == TPACPI_LED_OFF || rc < 0)
  3312. rc = LED_OFF; /* no error handling in led class :( */
  3313. else
  3314. rc = LED_FULL;
  3315. return rc;
  3316. }
  3317. static void led_exit(void)
  3318. {
  3319. unsigned int i;
  3320. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  3321. if (tpacpi_leds[i].led_classdev.name)
  3322. led_classdev_unregister(&tpacpi_leds[i].led_classdev);
  3323. }
  3324. kfree(tpacpi_leds);
  3325. }
  3326. static int __init led_init(struct ibm_init_struct *iibm)
  3327. {
  3328. unsigned int i;
  3329. int rc;
  3330. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  3331. TPACPI_ACPIHANDLE_INIT(led);
  3332. if (!led_handle)
  3333. /* led not supported on R30, R31 */
  3334. led_supported = TPACPI_LED_NONE;
  3335. else if (strlencmp(led_path, "SLED") == 0)
  3336. /* 570 */
  3337. led_supported = TPACPI_LED_570;
  3338. else if (strlencmp(led_path, "SYSL") == 0)
  3339. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  3340. led_supported = TPACPI_LED_OLD;
  3341. else
  3342. /* all others */
  3343. led_supported = TPACPI_LED_NEW;
  3344. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  3345. str_supported(led_supported), led_supported);
  3346. tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
  3347. GFP_KERNEL);
  3348. if (!tpacpi_leds) {
  3349. printk(TPACPI_ERR "Out of memory for LED data\n");
  3350. return -ENOMEM;
  3351. }
  3352. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  3353. tpacpi_leds[i].led = i;
  3354. tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
  3355. tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
  3356. if (led_supported == TPACPI_LED_570)
  3357. tpacpi_leds[i].led_classdev.brightness_get =
  3358. &led_sysfs_get;
  3359. tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
  3360. INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
  3361. rc = led_classdev_register(&tpacpi_pdev->dev,
  3362. &tpacpi_leds[i].led_classdev);
  3363. if (rc < 0) {
  3364. tpacpi_leds[i].led_classdev.name = NULL;
  3365. led_exit();
  3366. return rc;
  3367. }
  3368. }
  3369. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  3370. }
  3371. #define str_led_status(s) \
  3372. ((s) == TPACPI_LED_OFF ? "off" : \
  3373. ((s) == TPACPI_LED_ON ? "on" : "blinking"))
  3374. static int led_read(char *p)
  3375. {
  3376. int len = 0;
  3377. if (!led_supported) {
  3378. len += sprintf(p + len, "status:\t\tnot supported\n");
  3379. return len;
  3380. }
  3381. len += sprintf(p + len, "status:\t\tsupported\n");
  3382. if (led_supported == TPACPI_LED_570) {
  3383. /* 570 */
  3384. int i, status;
  3385. for (i = 0; i < 8; i++) {
  3386. status = led_get_status(i);
  3387. if (status < 0)
  3388. return -EIO;
  3389. len += sprintf(p + len, "%d:\t\t%s\n",
  3390. i, str_led_status(status));
  3391. }
  3392. }
  3393. len += sprintf(p + len, "commands:\t"
  3394. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  3395. return len;
  3396. }
  3397. static int led_write(char *buf)
  3398. {
  3399. char *cmd;
  3400. int led, rc;
  3401. enum led_status_t s;
  3402. if (!led_supported)
  3403. return -ENODEV;
  3404. while ((cmd = next_cmd(&buf))) {
  3405. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  3406. return -EINVAL;
  3407. if (strstr(cmd, "off")) {
  3408. s = TPACPI_LED_OFF;
  3409. } else if (strstr(cmd, "on")) {
  3410. s = TPACPI_LED_ON;
  3411. } else if (strstr(cmd, "blink")) {
  3412. s = TPACPI_LED_BLINK;
  3413. } else {
  3414. return -EINVAL;
  3415. }
  3416. rc = led_set_status(led, s);
  3417. if (rc < 0)
  3418. return rc;
  3419. }
  3420. return 0;
  3421. }
  3422. static struct ibm_struct led_driver_data = {
  3423. .name = "led",
  3424. .read = led_read,
  3425. .write = led_write,
  3426. .exit = led_exit,
  3427. };
  3428. /*************************************************************************
  3429. * Beep subdriver
  3430. */
  3431. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  3432. static int __init beep_init(struct ibm_init_struct *iibm)
  3433. {
  3434. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  3435. TPACPI_ACPIHANDLE_INIT(beep);
  3436. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  3437. str_supported(beep_handle != NULL));
  3438. return (beep_handle)? 0 : 1;
  3439. }
  3440. static int beep_read(char *p)
  3441. {
  3442. int len = 0;
  3443. if (!beep_handle)
  3444. len += sprintf(p + len, "status:\t\tnot supported\n");
  3445. else {
  3446. len += sprintf(p + len, "status:\t\tsupported\n");
  3447. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  3448. }
  3449. return len;
  3450. }
  3451. static int beep_write(char *buf)
  3452. {
  3453. char *cmd;
  3454. int beep_cmd;
  3455. if (!beep_handle)
  3456. return -ENODEV;
  3457. while ((cmd = next_cmd(&buf))) {
  3458. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  3459. beep_cmd >= 0 && beep_cmd <= 17) {
  3460. /* beep_cmd set */
  3461. } else
  3462. return -EINVAL;
  3463. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  3464. return -EIO;
  3465. }
  3466. return 0;
  3467. }
  3468. static struct ibm_struct beep_driver_data = {
  3469. .name = "beep",
  3470. .read = beep_read,
  3471. .write = beep_write,
  3472. };
  3473. /*************************************************************************
  3474. * Thermal subdriver
  3475. */
  3476. enum thermal_access_mode {
  3477. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  3478. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  3479. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  3480. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  3481. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  3482. };
  3483. enum { /* TPACPI_THERMAL_TPEC_* */
  3484. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  3485. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  3486. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  3487. };
  3488. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  3489. struct ibm_thermal_sensors_struct {
  3490. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  3491. };
  3492. static enum thermal_access_mode thermal_read_mode;
  3493. /* idx is zero-based */
  3494. static int thermal_get_sensor(int idx, s32 *value)
  3495. {
  3496. int t;
  3497. s8 tmp;
  3498. char tmpi[5];
  3499. t = TP_EC_THERMAL_TMP0;
  3500. switch (thermal_read_mode) {
  3501. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  3502. case TPACPI_THERMAL_TPEC_16:
  3503. if (idx >= 8 && idx <= 15) {
  3504. t = TP_EC_THERMAL_TMP8;
  3505. idx -= 8;
  3506. }
  3507. /* fallthrough */
  3508. #endif
  3509. case TPACPI_THERMAL_TPEC_8:
  3510. if (idx <= 7) {
  3511. if (!acpi_ec_read(t + idx, &tmp))
  3512. return -EIO;
  3513. *value = tmp * 1000;
  3514. return 0;
  3515. }
  3516. break;
  3517. case TPACPI_THERMAL_ACPI_UPDT:
  3518. if (idx <= 7) {
  3519. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3520. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  3521. return -EIO;
  3522. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3523. return -EIO;
  3524. *value = (t - 2732) * 100;
  3525. return 0;
  3526. }
  3527. break;
  3528. case TPACPI_THERMAL_ACPI_TMP07:
  3529. if (idx <= 7) {
  3530. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3531. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3532. return -EIO;
  3533. if (t > 127 || t < -127)
  3534. t = TP_EC_THERMAL_TMP_NA;
  3535. *value = t * 1000;
  3536. return 0;
  3537. }
  3538. break;
  3539. case TPACPI_THERMAL_NONE:
  3540. default:
  3541. return -ENOSYS;
  3542. }
  3543. return -EINVAL;
  3544. }
  3545. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  3546. {
  3547. int res, i;
  3548. int n;
  3549. n = 8;
  3550. i = 0;
  3551. if (!s)
  3552. return -EINVAL;
  3553. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  3554. n = 16;
  3555. for (i = 0 ; i < n; i++) {
  3556. res = thermal_get_sensor(i, &s->temp[i]);
  3557. if (res)
  3558. return res;
  3559. }
  3560. return n;
  3561. }
  3562. /* sysfs temp##_input -------------------------------------------------- */
  3563. static ssize_t thermal_temp_input_show(struct device *dev,
  3564. struct device_attribute *attr,
  3565. char *buf)
  3566. {
  3567. struct sensor_device_attribute *sensor_attr =
  3568. to_sensor_dev_attr(attr);
  3569. int idx = sensor_attr->index;
  3570. s32 value;
  3571. int res;
  3572. res = thermal_get_sensor(idx, &value);
  3573. if (res)
  3574. return res;
  3575. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  3576. return -ENXIO;
  3577. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  3578. }
  3579. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  3580. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  3581. thermal_temp_input_show, NULL, _idxB)
  3582. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  3583. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  3584. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  3585. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  3586. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  3587. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  3588. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  3589. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  3590. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  3591. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  3592. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  3593. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  3594. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  3595. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  3596. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  3597. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  3598. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  3599. };
  3600. #define THERMAL_ATTRS(X) \
  3601. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  3602. static struct attribute *thermal_temp_input_attr[] = {
  3603. THERMAL_ATTRS(8),
  3604. THERMAL_ATTRS(9),
  3605. THERMAL_ATTRS(10),
  3606. THERMAL_ATTRS(11),
  3607. THERMAL_ATTRS(12),
  3608. THERMAL_ATTRS(13),
  3609. THERMAL_ATTRS(14),
  3610. THERMAL_ATTRS(15),
  3611. THERMAL_ATTRS(0),
  3612. THERMAL_ATTRS(1),
  3613. THERMAL_ATTRS(2),
  3614. THERMAL_ATTRS(3),
  3615. THERMAL_ATTRS(4),
  3616. THERMAL_ATTRS(5),
  3617. THERMAL_ATTRS(6),
  3618. THERMAL_ATTRS(7),
  3619. NULL
  3620. };
  3621. static const struct attribute_group thermal_temp_input16_group = {
  3622. .attrs = thermal_temp_input_attr
  3623. };
  3624. static const struct attribute_group thermal_temp_input8_group = {
  3625. .attrs = &thermal_temp_input_attr[8]
  3626. };
  3627. #undef THERMAL_SENSOR_ATTR_TEMP
  3628. #undef THERMAL_ATTRS
  3629. /* --------------------------------------------------------------------- */
  3630. static int __init thermal_init(struct ibm_init_struct *iibm)
  3631. {
  3632. u8 t, ta1, ta2;
  3633. int i;
  3634. int acpi_tmp7;
  3635. int res;
  3636. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  3637. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  3638. if (thinkpad_id.ec_model) {
  3639. /*
  3640. * Direct EC access mode: sensors at registers
  3641. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  3642. * non-implemented, thermal sensors return 0x80 when
  3643. * not available
  3644. */
  3645. ta1 = ta2 = 0;
  3646. for (i = 0; i < 8; i++) {
  3647. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  3648. ta1 |= t;
  3649. } else {
  3650. ta1 = 0;
  3651. break;
  3652. }
  3653. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  3654. ta2 |= t;
  3655. } else {
  3656. ta1 = 0;
  3657. break;
  3658. }
  3659. }
  3660. if (ta1 == 0) {
  3661. /* This is sheer paranoia, but we handle it anyway */
  3662. if (acpi_tmp7) {
  3663. printk(TPACPI_ERR
  3664. "ThinkPad ACPI EC access misbehaving, "
  3665. "falling back to ACPI TMPx access "
  3666. "mode\n");
  3667. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3668. } else {
  3669. printk(TPACPI_ERR
  3670. "ThinkPad ACPI EC access misbehaving, "
  3671. "disabling thermal sensors access\n");
  3672. thermal_read_mode = TPACPI_THERMAL_NONE;
  3673. }
  3674. } else {
  3675. thermal_read_mode =
  3676. (ta2 != 0) ?
  3677. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  3678. }
  3679. } else if (acpi_tmp7) {
  3680. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  3681. /* 600e/x, 770e, 770x */
  3682. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  3683. } else {
  3684. /* Standard ACPI TMPx access, max 8 sensors */
  3685. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3686. }
  3687. } else {
  3688. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  3689. thermal_read_mode = TPACPI_THERMAL_NONE;
  3690. }
  3691. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  3692. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  3693. thermal_read_mode);
  3694. switch (thermal_read_mode) {
  3695. case TPACPI_THERMAL_TPEC_16:
  3696. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3697. &thermal_temp_input16_group);
  3698. if (res)
  3699. return res;
  3700. break;
  3701. case TPACPI_THERMAL_TPEC_8:
  3702. case TPACPI_THERMAL_ACPI_TMP07:
  3703. case TPACPI_THERMAL_ACPI_UPDT:
  3704. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3705. &thermal_temp_input8_group);
  3706. if (res)
  3707. return res;
  3708. break;
  3709. case TPACPI_THERMAL_NONE:
  3710. default:
  3711. return 1;
  3712. }
  3713. return 0;
  3714. }
  3715. static void thermal_exit(void)
  3716. {
  3717. switch (thermal_read_mode) {
  3718. case TPACPI_THERMAL_TPEC_16:
  3719. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3720. &thermal_temp_input16_group);
  3721. break;
  3722. case TPACPI_THERMAL_TPEC_8:
  3723. case TPACPI_THERMAL_ACPI_TMP07:
  3724. case TPACPI_THERMAL_ACPI_UPDT:
  3725. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3726. &thermal_temp_input16_group);
  3727. break;
  3728. case TPACPI_THERMAL_NONE:
  3729. default:
  3730. break;
  3731. }
  3732. }
  3733. static int thermal_read(char *p)
  3734. {
  3735. int len = 0;
  3736. int n, i;
  3737. struct ibm_thermal_sensors_struct t;
  3738. n = thermal_get_sensors(&t);
  3739. if (unlikely(n < 0))
  3740. return n;
  3741. len += sprintf(p + len, "temperatures:\t");
  3742. if (n > 0) {
  3743. for (i = 0; i < (n - 1); i++)
  3744. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  3745. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  3746. } else
  3747. len += sprintf(p + len, "not supported\n");
  3748. return len;
  3749. }
  3750. static struct ibm_struct thermal_driver_data = {
  3751. .name = "thermal",
  3752. .read = thermal_read,
  3753. .exit = thermal_exit,
  3754. };
  3755. /*************************************************************************
  3756. * EC Dump subdriver
  3757. */
  3758. static u8 ecdump_regs[256];
  3759. static int ecdump_read(char *p)
  3760. {
  3761. int len = 0;
  3762. int i, j;
  3763. u8 v;
  3764. len += sprintf(p + len, "EC "
  3765. " +00 +01 +02 +03 +04 +05 +06 +07"
  3766. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  3767. for (i = 0; i < 256; i += 16) {
  3768. len += sprintf(p + len, "EC 0x%02x:", i);
  3769. for (j = 0; j < 16; j++) {
  3770. if (!acpi_ec_read(i + j, &v))
  3771. break;
  3772. if (v != ecdump_regs[i + j])
  3773. len += sprintf(p + len, " *%02x", v);
  3774. else
  3775. len += sprintf(p + len, " %02x", v);
  3776. ecdump_regs[i + j] = v;
  3777. }
  3778. len += sprintf(p + len, "\n");
  3779. if (j != 16)
  3780. break;
  3781. }
  3782. /* These are way too dangerous to advertise openly... */
  3783. #if 0
  3784. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  3785. " (<offset> is 00-ff, <value> is 00-ff)\n");
  3786. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  3787. " (<offset> is 00-ff, <value> is 0-255)\n");
  3788. #endif
  3789. return len;
  3790. }
  3791. static int ecdump_write(char *buf)
  3792. {
  3793. char *cmd;
  3794. int i, v;
  3795. while ((cmd = next_cmd(&buf))) {
  3796. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  3797. /* i and v set */
  3798. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  3799. /* i and v set */
  3800. } else
  3801. return -EINVAL;
  3802. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  3803. if (!acpi_ec_write(i, v))
  3804. return -EIO;
  3805. } else
  3806. return -EINVAL;
  3807. }
  3808. return 0;
  3809. }
  3810. static struct ibm_struct ecdump_driver_data = {
  3811. .name = "ecdump",
  3812. .read = ecdump_read,
  3813. .write = ecdump_write,
  3814. .flags.experimental = 1,
  3815. };
  3816. /*************************************************************************
  3817. * Backlight/brightness subdriver
  3818. */
  3819. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  3820. enum {
  3821. TP_EC_BACKLIGHT = 0x31,
  3822. /* TP_EC_BACKLIGHT bitmasks */
  3823. TP_EC_BACKLIGHT_LVLMSK = 0x1F,
  3824. TP_EC_BACKLIGHT_CMDMSK = 0xE0,
  3825. TP_EC_BACKLIGHT_MAPSW = 0x20,
  3826. };
  3827. static struct backlight_device *ibm_backlight_device;
  3828. static int brightness_mode;
  3829. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  3830. static struct mutex brightness_mutex;
  3831. /*
  3832. * ThinkPads can read brightness from two places: EC 0x31, or
  3833. * CMOS NVRAM byte 0x5E, bits 0-3.
  3834. *
  3835. * EC 0x31 has the following layout
  3836. * Bit 7: unknown function
  3837. * Bit 6: unknown function
  3838. * Bit 5: Z: honour scale changes, NZ: ignore scale changes
  3839. * Bit 4: must be set to zero to avoid problems
  3840. * Bit 3-0: backlight brightness level
  3841. *
  3842. * brightness_get_raw returns status data in the EC 0x31 layout
  3843. */
  3844. static int brightness_get_raw(int *status)
  3845. {
  3846. u8 lec = 0, lcmos = 0, level = 0;
  3847. if (brightness_mode & 1) {
  3848. if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
  3849. return -EIO;
  3850. level = lec & TP_EC_BACKLIGHT_LVLMSK;
  3851. };
  3852. if (brightness_mode & 2) {
  3853. lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  3854. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  3855. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  3856. lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3857. level = lcmos;
  3858. }
  3859. if (brightness_mode == 3) {
  3860. *status = lec; /* Prefer EC, CMOS is just a backing store */
  3861. lec &= TP_EC_BACKLIGHT_LVLMSK;
  3862. if (lec == lcmos)
  3863. tp_warned.bright_cmos_ec_unsync = 0;
  3864. else {
  3865. if (!tp_warned.bright_cmos_ec_unsync) {
  3866. printk(TPACPI_ERR
  3867. "CMOS NVRAM (%u) and EC (%u) do not "
  3868. "agree on display brightness level\n",
  3869. (unsigned int) lcmos,
  3870. (unsigned int) lec);
  3871. tp_warned.bright_cmos_ec_unsync = 1;
  3872. }
  3873. return -EIO;
  3874. }
  3875. } else {
  3876. *status = level;
  3877. }
  3878. return 0;
  3879. }
  3880. /* May return EINTR which can always be mapped to ERESTARTSYS */
  3881. static int brightness_set(int value)
  3882. {
  3883. int cmos_cmd, inc, i, res;
  3884. int current_value;
  3885. int command_bits;
  3886. if (value > ((tp_features.bright_16levels)? 15 : 7) ||
  3887. value < 0)
  3888. return -EINVAL;
  3889. res = mutex_lock_interruptible(&brightness_mutex);
  3890. if (res < 0)
  3891. return res;
  3892. res = brightness_get_raw(&current_value);
  3893. if (res < 0)
  3894. goto errout;
  3895. command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
  3896. current_value &= TP_EC_BACKLIGHT_LVLMSK;
  3897. cmos_cmd = value > current_value ?
  3898. TP_CMOS_BRIGHTNESS_UP :
  3899. TP_CMOS_BRIGHTNESS_DOWN;
  3900. inc = (value > current_value)? 1 : -1;
  3901. res = 0;
  3902. for (i = current_value; i != value; i += inc) {
  3903. if ((brightness_mode & 2) &&
  3904. issue_thinkpad_cmos_command(cmos_cmd)) {
  3905. res = -EIO;
  3906. goto errout;
  3907. }
  3908. if ((brightness_mode & 1) &&
  3909. !acpi_ec_write(TP_EC_BACKLIGHT,
  3910. (i + inc) | command_bits)) {
  3911. res = -EIO;
  3912. goto errout;;
  3913. }
  3914. }
  3915. errout:
  3916. mutex_unlock(&brightness_mutex);
  3917. return res;
  3918. }
  3919. /* sysfs backlight class ----------------------------------------------- */
  3920. static int brightness_update_status(struct backlight_device *bd)
  3921. {
  3922. /* it is the backlight class's job (caller) to handle
  3923. * EINTR and other errors properly */
  3924. return brightness_set(
  3925. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  3926. bd->props.power == FB_BLANK_UNBLANK) ?
  3927. bd->props.brightness : 0);
  3928. }
  3929. static int brightness_get(struct backlight_device *bd)
  3930. {
  3931. int status, res;
  3932. res = brightness_get_raw(&status);
  3933. if (res < 0)
  3934. return 0; /* FIXME: teach backlight about error handling */
  3935. return status & TP_EC_BACKLIGHT_LVLMSK;
  3936. }
  3937. static struct backlight_ops ibm_backlight_data = {
  3938. .get_brightness = brightness_get,
  3939. .update_status = brightness_update_status,
  3940. };
  3941. /* --------------------------------------------------------------------- */
  3942. static int __init brightness_init(struct ibm_init_struct *iibm)
  3943. {
  3944. int b;
  3945. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  3946. mutex_init(&brightness_mutex);
  3947. /*
  3948. * We always attempt to detect acpi support, so as to switch
  3949. * Lenovo Vista BIOS to ACPI brightness mode even if we are not
  3950. * going to publish a backlight interface
  3951. */
  3952. b = tpacpi_check_std_acpi_brightness_support();
  3953. if (b > 0) {
  3954. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  3955. printk(TPACPI_NOTICE
  3956. "Lenovo BIOS switched to ACPI backlight "
  3957. "control mode\n");
  3958. }
  3959. if (brightness_enable > 1) {
  3960. printk(TPACPI_NOTICE
  3961. "standard ACPI backlight interface "
  3962. "available, not loading native one...\n");
  3963. return 1;
  3964. }
  3965. }
  3966. if (!brightness_enable) {
  3967. dbg_printk(TPACPI_DBG_INIT,
  3968. "brightness support disabled by "
  3969. "module parameter\n");
  3970. return 1;
  3971. }
  3972. if (b > 16) {
  3973. printk(TPACPI_ERR
  3974. "Unsupported brightness interface, "
  3975. "please contact %s\n", TPACPI_MAIL);
  3976. return 1;
  3977. }
  3978. if (b == 16)
  3979. tp_features.bright_16levels = 1;
  3980. if (!brightness_mode) {
  3981. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
  3982. brightness_mode = 2;
  3983. else
  3984. brightness_mode = 3;
  3985. dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
  3986. brightness_mode);
  3987. }
  3988. if (brightness_mode > 3)
  3989. return -EINVAL;
  3990. if (brightness_get_raw(&b) < 0)
  3991. return 1;
  3992. if (tp_features.bright_16levels)
  3993. printk(TPACPI_INFO
  3994. "detected a 16-level brightness capable ThinkPad\n");
  3995. ibm_backlight_device = backlight_device_register(
  3996. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  3997. &ibm_backlight_data);
  3998. if (IS_ERR(ibm_backlight_device)) {
  3999. printk(TPACPI_ERR "Could not register backlight device\n");
  4000. return PTR_ERR(ibm_backlight_device);
  4001. }
  4002. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  4003. ibm_backlight_device->props.max_brightness =
  4004. (tp_features.bright_16levels)? 15 : 7;
  4005. ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
  4006. backlight_update_status(ibm_backlight_device);
  4007. return 0;
  4008. }
  4009. static void brightness_exit(void)
  4010. {
  4011. if (ibm_backlight_device) {
  4012. vdbg_printk(TPACPI_DBG_EXIT,
  4013. "calling backlight_device_unregister()\n");
  4014. backlight_device_unregister(ibm_backlight_device);
  4015. }
  4016. }
  4017. static int brightness_read(char *p)
  4018. {
  4019. int len = 0;
  4020. int level;
  4021. level = brightness_get(NULL);
  4022. if (level < 0) {
  4023. len += sprintf(p + len, "level:\t\tunreadable\n");
  4024. } else {
  4025. len += sprintf(p + len, "level:\t\t%d\n", level);
  4026. len += sprintf(p + len, "commands:\tup, down\n");
  4027. len += sprintf(p + len, "commands:\tlevel <level>"
  4028. " (<level> is 0-%d)\n",
  4029. (tp_features.bright_16levels) ? 15 : 7);
  4030. }
  4031. return len;
  4032. }
  4033. static int brightness_write(char *buf)
  4034. {
  4035. int level;
  4036. int rc;
  4037. char *cmd;
  4038. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  4039. level = brightness_get(NULL);
  4040. if (level < 0)
  4041. return level;
  4042. while ((cmd = next_cmd(&buf))) {
  4043. if (strlencmp(cmd, "up") == 0) {
  4044. if (level < max_level)
  4045. level++;
  4046. } else if (strlencmp(cmd, "down") == 0) {
  4047. if (level > 0)
  4048. level--;
  4049. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  4050. level >= 0 && level <= max_level) {
  4051. /* new level set */
  4052. } else
  4053. return -EINVAL;
  4054. }
  4055. /*
  4056. * Now we know what the final level should be, so we try to set it.
  4057. * Doing it this way makes the syscall restartable in case of EINTR
  4058. */
  4059. rc = brightness_set(level);
  4060. return (rc == -EINTR)? ERESTARTSYS : rc;
  4061. }
  4062. static struct ibm_struct brightness_driver_data = {
  4063. .name = "brightness",
  4064. .read = brightness_read,
  4065. .write = brightness_write,
  4066. .exit = brightness_exit,
  4067. };
  4068. /*************************************************************************
  4069. * Volume subdriver
  4070. */
  4071. static int volume_offset = 0x30;
  4072. static int volume_read(char *p)
  4073. {
  4074. int len = 0;
  4075. u8 level;
  4076. if (!acpi_ec_read(volume_offset, &level)) {
  4077. len += sprintf(p + len, "level:\t\tunreadable\n");
  4078. } else {
  4079. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  4080. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  4081. len += sprintf(p + len, "commands:\tup, down, mute\n");
  4082. len += sprintf(p + len, "commands:\tlevel <level>"
  4083. " (<level> is 0-15)\n");
  4084. }
  4085. return len;
  4086. }
  4087. static int volume_write(char *buf)
  4088. {
  4089. int cmos_cmd, inc, i;
  4090. u8 level, mute;
  4091. int new_level, new_mute;
  4092. char *cmd;
  4093. while ((cmd = next_cmd(&buf))) {
  4094. if (!acpi_ec_read(volume_offset, &level))
  4095. return -EIO;
  4096. new_mute = mute = level & 0x40;
  4097. new_level = level = level & 0xf;
  4098. if (strlencmp(cmd, "up") == 0) {
  4099. if (mute)
  4100. new_mute = 0;
  4101. else
  4102. new_level = level == 15 ? 15 : level + 1;
  4103. } else if (strlencmp(cmd, "down") == 0) {
  4104. if (mute)
  4105. new_mute = 0;
  4106. else
  4107. new_level = level == 0 ? 0 : level - 1;
  4108. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  4109. new_level >= 0 && new_level <= 15) {
  4110. /* new_level set */
  4111. } else if (strlencmp(cmd, "mute") == 0) {
  4112. new_mute = 0x40;
  4113. } else
  4114. return -EINVAL;
  4115. if (new_level != level) {
  4116. /* mute doesn't change */
  4117. cmos_cmd = (new_level > level) ?
  4118. TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  4119. inc = new_level > level ? 1 : -1;
  4120. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  4121. !acpi_ec_write(volume_offset, level)))
  4122. return -EIO;
  4123. for (i = level; i != new_level; i += inc)
  4124. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  4125. !acpi_ec_write(volume_offset, i + inc))
  4126. return -EIO;
  4127. if (mute &&
  4128. (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  4129. !acpi_ec_write(volume_offset, new_level + mute))) {
  4130. return -EIO;
  4131. }
  4132. }
  4133. if (new_mute != mute) {
  4134. /* level doesn't change */
  4135. cmos_cmd = (new_mute) ?
  4136. TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  4137. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  4138. !acpi_ec_write(volume_offset, level + new_mute))
  4139. return -EIO;
  4140. }
  4141. }
  4142. return 0;
  4143. }
  4144. static struct ibm_struct volume_driver_data = {
  4145. .name = "volume",
  4146. .read = volume_read,
  4147. .write = volume_write,
  4148. };
  4149. /*************************************************************************
  4150. * Fan subdriver
  4151. */
  4152. /*
  4153. * FAN ACCESS MODES
  4154. *
  4155. * TPACPI_FAN_RD_ACPI_GFAN:
  4156. * ACPI GFAN method: returns fan level
  4157. *
  4158. * see TPACPI_FAN_WR_ACPI_SFAN
  4159. * EC 0x2f (HFSP) not available if GFAN exists
  4160. *
  4161. * TPACPI_FAN_WR_ACPI_SFAN:
  4162. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  4163. *
  4164. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  4165. * it for writing.
  4166. *
  4167. * TPACPI_FAN_WR_TPEC:
  4168. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  4169. * Supported on almost all ThinkPads
  4170. *
  4171. * Fan speed changes of any sort (including those caused by the
  4172. * disengaged mode) are usually done slowly by the firmware as the
  4173. * maximum ammount of fan duty cycle change per second seems to be
  4174. * limited.
  4175. *
  4176. * Reading is not available if GFAN exists.
  4177. * Writing is not available if SFAN exists.
  4178. *
  4179. * Bits
  4180. * 7 automatic mode engaged;
  4181. * (default operation mode of the ThinkPad)
  4182. * fan level is ignored in this mode.
  4183. * 6 full speed mode (takes precedence over bit 7);
  4184. * not available on all thinkpads. May disable
  4185. * the tachometer while the fan controller ramps up
  4186. * the speed (which can take up to a few *minutes*).
  4187. * Speeds up fan to 100% duty-cycle, which is far above
  4188. * the standard RPM levels. It is not impossible that
  4189. * it could cause hardware damage.
  4190. * 5-3 unused in some models. Extra bits for fan level
  4191. * in others, but still useless as all values above
  4192. * 7 map to the same speed as level 7 in these models.
  4193. * 2-0 fan level (0..7 usually)
  4194. * 0x00 = stop
  4195. * 0x07 = max (set when temperatures critical)
  4196. * Some ThinkPads may have other levels, see
  4197. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  4198. *
  4199. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  4200. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  4201. * does so, its initial value is meaningless (0x07).
  4202. *
  4203. * For firmware bugs, refer to:
  4204. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  4205. *
  4206. * ----
  4207. *
  4208. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  4209. * Main fan tachometer reading (in RPM)
  4210. *
  4211. * This register is present on all ThinkPads with a new-style EC, and
  4212. * it is known not to be present on the A21m/e, and T22, as there is
  4213. * something else in offset 0x84 according to the ACPI DSDT. Other
  4214. * ThinkPads from this same time period (and earlier) probably lack the
  4215. * tachometer as well.
  4216. *
  4217. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  4218. * was never fixed by IBM to report the EC firmware version string
  4219. * probably support the tachometer (like the early X models), so
  4220. * detecting it is quite hard. We need more data to know for sure.
  4221. *
  4222. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  4223. * might result.
  4224. *
  4225. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  4226. * mode.
  4227. *
  4228. * For firmware bugs, refer to:
  4229. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  4230. *
  4231. * TPACPI_FAN_WR_ACPI_FANS:
  4232. * ThinkPad X31, X40, X41. Not available in the X60.
  4233. *
  4234. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  4235. * high speed. ACPI DSDT seems to map these three speeds to levels
  4236. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  4237. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  4238. *
  4239. * The speeds are stored on handles
  4240. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  4241. *
  4242. * There are three default speed sets, acessible as handles:
  4243. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  4244. *
  4245. * ACPI DSDT switches which set is in use depending on various
  4246. * factors.
  4247. *
  4248. * TPACPI_FAN_WR_TPEC is also available and should be used to
  4249. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  4250. * but the ACPI tables just mention level 7.
  4251. */
  4252. enum { /* Fan control constants */
  4253. fan_status_offset = 0x2f, /* EC register 0x2f */
  4254. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  4255. * 0x84 must be read before 0x85 */
  4256. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  4257. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  4258. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  4259. };
  4260. enum fan_status_access_mode {
  4261. TPACPI_FAN_NONE = 0, /* No fan status or control */
  4262. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  4263. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  4264. };
  4265. enum fan_control_access_mode {
  4266. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  4267. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  4268. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  4269. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  4270. };
  4271. enum fan_control_commands {
  4272. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  4273. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  4274. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  4275. * and also watchdog cmd */
  4276. };
  4277. static int fan_control_allowed;
  4278. static enum fan_status_access_mode fan_status_access_mode;
  4279. static enum fan_control_access_mode fan_control_access_mode;
  4280. static enum fan_control_commands fan_control_commands;
  4281. static u8 fan_control_initial_status;
  4282. static u8 fan_control_desired_level;
  4283. static int fan_watchdog_maxinterval;
  4284. static struct mutex fan_mutex;
  4285. static void fan_watchdog_fire(struct work_struct *ignored);
  4286. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  4287. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  4288. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  4289. "\\FSPD", /* 600e/x, 770e, 770x */
  4290. ); /* all others */
  4291. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  4292. "JFNS", /* 770x-JL */
  4293. ); /* all others */
  4294. /*
  4295. * Call with fan_mutex held
  4296. */
  4297. static void fan_update_desired_level(u8 status)
  4298. {
  4299. if ((status &
  4300. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4301. if (status > 7)
  4302. fan_control_desired_level = 7;
  4303. else
  4304. fan_control_desired_level = status;
  4305. }
  4306. }
  4307. static int fan_get_status(u8 *status)
  4308. {
  4309. u8 s;
  4310. /* TODO:
  4311. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  4312. switch (fan_status_access_mode) {
  4313. case TPACPI_FAN_RD_ACPI_GFAN:
  4314. /* 570, 600e/x, 770e, 770x */
  4315. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  4316. return -EIO;
  4317. if (likely(status))
  4318. *status = s & 0x07;
  4319. break;
  4320. case TPACPI_FAN_RD_TPEC:
  4321. /* all except 570, 600e/x, 770e, 770x */
  4322. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  4323. return -EIO;
  4324. if (likely(status))
  4325. *status = s;
  4326. break;
  4327. default:
  4328. return -ENXIO;
  4329. }
  4330. return 0;
  4331. }
  4332. static int fan_get_status_safe(u8 *status)
  4333. {
  4334. int rc;
  4335. u8 s;
  4336. if (mutex_lock_interruptible(&fan_mutex))
  4337. return -ERESTARTSYS;
  4338. rc = fan_get_status(&s);
  4339. if (!rc)
  4340. fan_update_desired_level(s);
  4341. mutex_unlock(&fan_mutex);
  4342. if (status)
  4343. *status = s;
  4344. return rc;
  4345. }
  4346. static int fan_get_speed(unsigned int *speed)
  4347. {
  4348. u8 hi, lo;
  4349. switch (fan_status_access_mode) {
  4350. case TPACPI_FAN_RD_TPEC:
  4351. /* all except 570, 600e/x, 770e, 770x */
  4352. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  4353. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  4354. return -EIO;
  4355. if (likely(speed))
  4356. *speed = (hi << 8) | lo;
  4357. break;
  4358. default:
  4359. return -ENXIO;
  4360. }
  4361. return 0;
  4362. }
  4363. static int fan_set_level(int level)
  4364. {
  4365. if (!fan_control_allowed)
  4366. return -EPERM;
  4367. switch (fan_control_access_mode) {
  4368. case TPACPI_FAN_WR_ACPI_SFAN:
  4369. if (level >= 0 && level <= 7) {
  4370. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  4371. return -EIO;
  4372. } else
  4373. return -EINVAL;
  4374. break;
  4375. case TPACPI_FAN_WR_ACPI_FANS:
  4376. case TPACPI_FAN_WR_TPEC:
  4377. if ((level != TP_EC_FAN_AUTO) &&
  4378. (level != TP_EC_FAN_FULLSPEED) &&
  4379. ((level < 0) || (level > 7)))
  4380. return -EINVAL;
  4381. /* safety net should the EC not support AUTO
  4382. * or FULLSPEED mode bits and just ignore them */
  4383. if (level & TP_EC_FAN_FULLSPEED)
  4384. level |= 7; /* safety min speed 7 */
  4385. else if (level & TP_EC_FAN_AUTO)
  4386. level |= 4; /* safety min speed 4 */
  4387. if (!acpi_ec_write(fan_status_offset, level))
  4388. return -EIO;
  4389. else
  4390. tp_features.fan_ctrl_status_undef = 0;
  4391. break;
  4392. default:
  4393. return -ENXIO;
  4394. }
  4395. return 0;
  4396. }
  4397. static int fan_set_level_safe(int level)
  4398. {
  4399. int rc;
  4400. if (!fan_control_allowed)
  4401. return -EPERM;
  4402. if (mutex_lock_interruptible(&fan_mutex))
  4403. return -ERESTARTSYS;
  4404. if (level == TPACPI_FAN_LAST_LEVEL)
  4405. level = fan_control_desired_level;
  4406. rc = fan_set_level(level);
  4407. if (!rc)
  4408. fan_update_desired_level(level);
  4409. mutex_unlock(&fan_mutex);
  4410. return rc;
  4411. }
  4412. static int fan_set_enable(void)
  4413. {
  4414. u8 s;
  4415. int rc;
  4416. if (!fan_control_allowed)
  4417. return -EPERM;
  4418. if (mutex_lock_interruptible(&fan_mutex))
  4419. return -ERESTARTSYS;
  4420. switch (fan_control_access_mode) {
  4421. case TPACPI_FAN_WR_ACPI_FANS:
  4422. case TPACPI_FAN_WR_TPEC:
  4423. rc = fan_get_status(&s);
  4424. if (rc < 0)
  4425. break;
  4426. /* Don't go out of emergency fan mode */
  4427. if (s != 7) {
  4428. s &= 0x07;
  4429. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  4430. }
  4431. if (!acpi_ec_write(fan_status_offset, s))
  4432. rc = -EIO;
  4433. else {
  4434. tp_features.fan_ctrl_status_undef = 0;
  4435. rc = 0;
  4436. }
  4437. break;
  4438. case TPACPI_FAN_WR_ACPI_SFAN:
  4439. rc = fan_get_status(&s);
  4440. if (rc < 0)
  4441. break;
  4442. s &= 0x07;
  4443. /* Set fan to at least level 4 */
  4444. s |= 4;
  4445. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  4446. rc = -EIO;
  4447. else
  4448. rc = 0;
  4449. break;
  4450. default:
  4451. rc = -ENXIO;
  4452. }
  4453. mutex_unlock(&fan_mutex);
  4454. return rc;
  4455. }
  4456. static int fan_set_disable(void)
  4457. {
  4458. int rc;
  4459. if (!fan_control_allowed)
  4460. return -EPERM;
  4461. if (mutex_lock_interruptible(&fan_mutex))
  4462. return -ERESTARTSYS;
  4463. rc = 0;
  4464. switch (fan_control_access_mode) {
  4465. case TPACPI_FAN_WR_ACPI_FANS:
  4466. case TPACPI_FAN_WR_TPEC:
  4467. if (!acpi_ec_write(fan_status_offset, 0x00))
  4468. rc = -EIO;
  4469. else {
  4470. fan_control_desired_level = 0;
  4471. tp_features.fan_ctrl_status_undef = 0;
  4472. }
  4473. break;
  4474. case TPACPI_FAN_WR_ACPI_SFAN:
  4475. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  4476. rc = -EIO;
  4477. else
  4478. fan_control_desired_level = 0;
  4479. break;
  4480. default:
  4481. rc = -ENXIO;
  4482. }
  4483. mutex_unlock(&fan_mutex);
  4484. return rc;
  4485. }
  4486. static int fan_set_speed(int speed)
  4487. {
  4488. int rc;
  4489. if (!fan_control_allowed)
  4490. return -EPERM;
  4491. if (mutex_lock_interruptible(&fan_mutex))
  4492. return -ERESTARTSYS;
  4493. rc = 0;
  4494. switch (fan_control_access_mode) {
  4495. case TPACPI_FAN_WR_ACPI_FANS:
  4496. if (speed >= 0 && speed <= 65535) {
  4497. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  4498. speed, speed, speed))
  4499. rc = -EIO;
  4500. } else
  4501. rc = -EINVAL;
  4502. break;
  4503. default:
  4504. rc = -ENXIO;
  4505. }
  4506. mutex_unlock(&fan_mutex);
  4507. return rc;
  4508. }
  4509. static void fan_watchdog_reset(void)
  4510. {
  4511. static int fan_watchdog_active;
  4512. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  4513. return;
  4514. if (fan_watchdog_active)
  4515. cancel_delayed_work(&fan_watchdog_task);
  4516. if (fan_watchdog_maxinterval > 0 &&
  4517. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  4518. fan_watchdog_active = 1;
  4519. if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
  4520. msecs_to_jiffies(fan_watchdog_maxinterval
  4521. * 1000))) {
  4522. printk(TPACPI_ERR
  4523. "failed to queue the fan watchdog, "
  4524. "watchdog will not trigger\n");
  4525. }
  4526. } else
  4527. fan_watchdog_active = 0;
  4528. }
  4529. static void fan_watchdog_fire(struct work_struct *ignored)
  4530. {
  4531. int rc;
  4532. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  4533. return;
  4534. printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
  4535. rc = fan_set_enable();
  4536. if (rc < 0) {
  4537. printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
  4538. "will try again later...\n", -rc);
  4539. /* reschedule for later */
  4540. fan_watchdog_reset();
  4541. }
  4542. }
  4543. /*
  4544. * SYSFS fan layout: hwmon compatible (device)
  4545. *
  4546. * pwm*_enable:
  4547. * 0: "disengaged" mode
  4548. * 1: manual mode
  4549. * 2: native EC "auto" mode (recommended, hardware default)
  4550. *
  4551. * pwm*: set speed in manual mode, ignored otherwise.
  4552. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  4553. * interpolation.
  4554. *
  4555. * fan*_input: tachometer reading, RPM
  4556. *
  4557. *
  4558. * SYSFS fan layout: extensions
  4559. *
  4560. * fan_watchdog (driver):
  4561. * fan watchdog interval in seconds, 0 disables (default), max 120
  4562. */
  4563. /* sysfs fan pwm1_enable ----------------------------------------------- */
  4564. static ssize_t fan_pwm1_enable_show(struct device *dev,
  4565. struct device_attribute *attr,
  4566. char *buf)
  4567. {
  4568. int res, mode;
  4569. u8 status;
  4570. res = fan_get_status_safe(&status);
  4571. if (res)
  4572. return res;
  4573. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4574. if (status != fan_control_initial_status) {
  4575. tp_features.fan_ctrl_status_undef = 0;
  4576. } else {
  4577. /* Return most likely status. In fact, it
  4578. * might be the only possible status */
  4579. status = TP_EC_FAN_AUTO;
  4580. }
  4581. }
  4582. if (status & TP_EC_FAN_FULLSPEED) {
  4583. mode = 0;
  4584. } else if (status & TP_EC_FAN_AUTO) {
  4585. mode = 2;
  4586. } else
  4587. mode = 1;
  4588. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  4589. }
  4590. static ssize_t fan_pwm1_enable_store(struct device *dev,
  4591. struct device_attribute *attr,
  4592. const char *buf, size_t count)
  4593. {
  4594. unsigned long t;
  4595. int res, level;
  4596. if (parse_strtoul(buf, 2, &t))
  4597. return -EINVAL;
  4598. switch (t) {
  4599. case 0:
  4600. level = TP_EC_FAN_FULLSPEED;
  4601. break;
  4602. case 1:
  4603. level = TPACPI_FAN_LAST_LEVEL;
  4604. break;
  4605. case 2:
  4606. level = TP_EC_FAN_AUTO;
  4607. break;
  4608. case 3:
  4609. /* reserved for software-controlled auto mode */
  4610. return -ENOSYS;
  4611. default:
  4612. return -EINVAL;
  4613. }
  4614. res = fan_set_level_safe(level);
  4615. if (res == -ENXIO)
  4616. return -EINVAL;
  4617. else if (res < 0)
  4618. return res;
  4619. fan_watchdog_reset();
  4620. return count;
  4621. }
  4622. static struct device_attribute dev_attr_fan_pwm1_enable =
  4623. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  4624. fan_pwm1_enable_show, fan_pwm1_enable_store);
  4625. /* sysfs fan pwm1 ------------------------------------------------------ */
  4626. static ssize_t fan_pwm1_show(struct device *dev,
  4627. struct device_attribute *attr,
  4628. char *buf)
  4629. {
  4630. int res;
  4631. u8 status;
  4632. res = fan_get_status_safe(&status);
  4633. if (res)
  4634. return res;
  4635. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4636. if (status != fan_control_initial_status) {
  4637. tp_features.fan_ctrl_status_undef = 0;
  4638. } else {
  4639. status = TP_EC_FAN_AUTO;
  4640. }
  4641. }
  4642. if ((status &
  4643. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  4644. status = fan_control_desired_level;
  4645. if (status > 7)
  4646. status = 7;
  4647. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  4648. }
  4649. static ssize_t fan_pwm1_store(struct device *dev,
  4650. struct device_attribute *attr,
  4651. const char *buf, size_t count)
  4652. {
  4653. unsigned long s;
  4654. int rc;
  4655. u8 status, newlevel;
  4656. if (parse_strtoul(buf, 255, &s))
  4657. return -EINVAL;
  4658. /* scale down from 0-255 to 0-7 */
  4659. newlevel = (s >> 5) & 0x07;
  4660. if (mutex_lock_interruptible(&fan_mutex))
  4661. return -ERESTARTSYS;
  4662. rc = fan_get_status(&status);
  4663. if (!rc && (status &
  4664. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4665. rc = fan_set_level(newlevel);
  4666. if (rc == -ENXIO)
  4667. rc = -EINVAL;
  4668. else if (!rc) {
  4669. fan_update_desired_level(newlevel);
  4670. fan_watchdog_reset();
  4671. }
  4672. }
  4673. mutex_unlock(&fan_mutex);
  4674. return (rc)? rc : count;
  4675. }
  4676. static struct device_attribute dev_attr_fan_pwm1 =
  4677. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  4678. fan_pwm1_show, fan_pwm1_store);
  4679. /* sysfs fan fan1_input ------------------------------------------------ */
  4680. static ssize_t fan_fan1_input_show(struct device *dev,
  4681. struct device_attribute *attr,
  4682. char *buf)
  4683. {
  4684. int res;
  4685. unsigned int speed;
  4686. res = fan_get_speed(&speed);
  4687. if (res < 0)
  4688. return res;
  4689. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  4690. }
  4691. static struct device_attribute dev_attr_fan_fan1_input =
  4692. __ATTR(fan1_input, S_IRUGO,
  4693. fan_fan1_input_show, NULL);
  4694. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  4695. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  4696. char *buf)
  4697. {
  4698. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  4699. }
  4700. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  4701. const char *buf, size_t count)
  4702. {
  4703. unsigned long t;
  4704. if (parse_strtoul(buf, 120, &t))
  4705. return -EINVAL;
  4706. if (!fan_control_allowed)
  4707. return -EPERM;
  4708. fan_watchdog_maxinterval = t;
  4709. fan_watchdog_reset();
  4710. return count;
  4711. }
  4712. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  4713. fan_fan_watchdog_show, fan_fan_watchdog_store);
  4714. /* --------------------------------------------------------------------- */
  4715. static struct attribute *fan_attributes[] = {
  4716. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  4717. &dev_attr_fan_fan1_input.attr,
  4718. NULL
  4719. };
  4720. static const struct attribute_group fan_attr_group = {
  4721. .attrs = fan_attributes,
  4722. };
  4723. static int __init fan_init(struct ibm_init_struct *iibm)
  4724. {
  4725. int rc;
  4726. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  4727. mutex_init(&fan_mutex);
  4728. fan_status_access_mode = TPACPI_FAN_NONE;
  4729. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4730. fan_control_commands = 0;
  4731. fan_watchdog_maxinterval = 0;
  4732. tp_features.fan_ctrl_status_undef = 0;
  4733. fan_control_desired_level = 7;
  4734. TPACPI_ACPIHANDLE_INIT(fans);
  4735. TPACPI_ACPIHANDLE_INIT(gfan);
  4736. TPACPI_ACPIHANDLE_INIT(sfan);
  4737. if (gfan_handle) {
  4738. /* 570, 600e/x, 770e, 770x */
  4739. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  4740. } else {
  4741. /* all other ThinkPads: note that even old-style
  4742. * ThinkPad ECs supports the fan control register */
  4743. if (likely(acpi_ec_read(fan_status_offset,
  4744. &fan_control_initial_status))) {
  4745. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  4746. /* In some ThinkPads, neither the EC nor the ACPI
  4747. * DSDT initialize the fan status, and it ends up
  4748. * being set to 0x07 when it *could* be either
  4749. * 0x07 or 0x80.
  4750. *
  4751. * Enable for TP-1Y (T43), TP-78 (R51e),
  4752. * TP-76 (R52), TP-70 (T43, R52), which are known
  4753. * to be buggy. */
  4754. if (fan_control_initial_status == 0x07) {
  4755. switch (thinkpad_id.ec_model) {
  4756. case 0x5931: /* TP-1Y */
  4757. case 0x3837: /* TP-78 */
  4758. case 0x3637: /* TP-76 */
  4759. case 0x3037: /* TP-70 */
  4760. printk(TPACPI_NOTICE
  4761. "fan_init: initial fan status "
  4762. "is unknown, assuming it is "
  4763. "in auto mode\n");
  4764. tp_features.fan_ctrl_status_undef = 1;
  4765. ;;
  4766. }
  4767. }
  4768. } else {
  4769. printk(TPACPI_ERR
  4770. "ThinkPad ACPI EC access misbehaving, "
  4771. "fan status and control unavailable\n");
  4772. return 1;
  4773. }
  4774. }
  4775. if (sfan_handle) {
  4776. /* 570, 770x-JL */
  4777. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  4778. fan_control_commands |=
  4779. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  4780. } else {
  4781. if (!gfan_handle) {
  4782. /* gfan without sfan means no fan control */
  4783. /* all other models implement TP EC 0x2f control */
  4784. if (fans_handle) {
  4785. /* X31, X40, X41 */
  4786. fan_control_access_mode =
  4787. TPACPI_FAN_WR_ACPI_FANS;
  4788. fan_control_commands |=
  4789. TPACPI_FAN_CMD_SPEED |
  4790. TPACPI_FAN_CMD_LEVEL |
  4791. TPACPI_FAN_CMD_ENABLE;
  4792. } else {
  4793. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  4794. fan_control_commands |=
  4795. TPACPI_FAN_CMD_LEVEL |
  4796. TPACPI_FAN_CMD_ENABLE;
  4797. }
  4798. }
  4799. }
  4800. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  4801. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  4802. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  4803. fan_status_access_mode, fan_control_access_mode);
  4804. /* fan control master switch */
  4805. if (!fan_control_allowed) {
  4806. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4807. fan_control_commands = 0;
  4808. dbg_printk(TPACPI_DBG_INIT,
  4809. "fan control features disabled by parameter\n");
  4810. }
  4811. /* update fan_control_desired_level */
  4812. if (fan_status_access_mode != TPACPI_FAN_NONE)
  4813. fan_get_status_safe(NULL);
  4814. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  4815. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  4816. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4817. &fan_attr_group);
  4818. if (rc < 0)
  4819. return rc;
  4820. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  4821. &driver_attr_fan_watchdog);
  4822. if (rc < 0) {
  4823. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  4824. &fan_attr_group);
  4825. return rc;
  4826. }
  4827. return 0;
  4828. } else
  4829. return 1;
  4830. }
  4831. static void fan_exit(void)
  4832. {
  4833. vdbg_printk(TPACPI_DBG_EXIT,
  4834. "cancelling any pending fan watchdog tasks\n");
  4835. /* FIXME: can we really do this unconditionally? */
  4836. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  4837. driver_remove_file(&tpacpi_hwmon_pdriver.driver,
  4838. &driver_attr_fan_watchdog);
  4839. cancel_delayed_work(&fan_watchdog_task);
  4840. flush_workqueue(tpacpi_wq);
  4841. }
  4842. static int fan_read(char *p)
  4843. {
  4844. int len = 0;
  4845. int rc;
  4846. u8 status;
  4847. unsigned int speed = 0;
  4848. switch (fan_status_access_mode) {
  4849. case TPACPI_FAN_RD_ACPI_GFAN:
  4850. /* 570, 600e/x, 770e, 770x */
  4851. rc = fan_get_status_safe(&status);
  4852. if (rc < 0)
  4853. return rc;
  4854. len += sprintf(p + len, "status:\t\t%s\n"
  4855. "level:\t\t%d\n",
  4856. (status != 0) ? "enabled" : "disabled", status);
  4857. break;
  4858. case TPACPI_FAN_RD_TPEC:
  4859. /* all except 570, 600e/x, 770e, 770x */
  4860. rc = fan_get_status_safe(&status);
  4861. if (rc < 0)
  4862. return rc;
  4863. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4864. if (status != fan_control_initial_status)
  4865. tp_features.fan_ctrl_status_undef = 0;
  4866. else
  4867. /* Return most likely status. In fact, it
  4868. * might be the only possible status */
  4869. status = TP_EC_FAN_AUTO;
  4870. }
  4871. len += sprintf(p + len, "status:\t\t%s\n",
  4872. (status != 0) ? "enabled" : "disabled");
  4873. rc = fan_get_speed(&speed);
  4874. if (rc < 0)
  4875. return rc;
  4876. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  4877. if (status & TP_EC_FAN_FULLSPEED)
  4878. /* Disengaged mode takes precedence */
  4879. len += sprintf(p + len, "level:\t\tdisengaged\n");
  4880. else if (status & TP_EC_FAN_AUTO)
  4881. len += sprintf(p + len, "level:\t\tauto\n");
  4882. else
  4883. len += sprintf(p + len, "level:\t\t%d\n", status);
  4884. break;
  4885. case TPACPI_FAN_NONE:
  4886. default:
  4887. len += sprintf(p + len, "status:\t\tnot supported\n");
  4888. }
  4889. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  4890. len += sprintf(p + len, "commands:\tlevel <level>");
  4891. switch (fan_control_access_mode) {
  4892. case TPACPI_FAN_WR_ACPI_SFAN:
  4893. len += sprintf(p + len, " (<level> is 0-7)\n");
  4894. break;
  4895. default:
  4896. len += sprintf(p + len, " (<level> is 0-7, "
  4897. "auto, disengaged, full-speed)\n");
  4898. break;
  4899. }
  4900. }
  4901. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  4902. len += sprintf(p + len, "commands:\tenable, disable\n"
  4903. "commands:\twatchdog <timeout> (<timeout> "
  4904. "is 0 (off), 1-120 (seconds))\n");
  4905. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  4906. len += sprintf(p + len, "commands:\tspeed <speed>"
  4907. " (<speed> is 0-65535)\n");
  4908. return len;
  4909. }
  4910. static int fan_write_cmd_level(const char *cmd, int *rc)
  4911. {
  4912. int level;
  4913. if (strlencmp(cmd, "level auto") == 0)
  4914. level = TP_EC_FAN_AUTO;
  4915. else if ((strlencmp(cmd, "level disengaged") == 0) |
  4916. (strlencmp(cmd, "level full-speed") == 0))
  4917. level = TP_EC_FAN_FULLSPEED;
  4918. else if (sscanf(cmd, "level %d", &level) != 1)
  4919. return 0;
  4920. *rc = fan_set_level_safe(level);
  4921. if (*rc == -ENXIO)
  4922. printk(TPACPI_ERR "level command accepted for unsupported "
  4923. "access mode %d", fan_control_access_mode);
  4924. return 1;
  4925. }
  4926. static int fan_write_cmd_enable(const char *cmd, int *rc)
  4927. {
  4928. if (strlencmp(cmd, "enable") != 0)
  4929. return 0;
  4930. *rc = fan_set_enable();
  4931. if (*rc == -ENXIO)
  4932. printk(TPACPI_ERR "enable command accepted for unsupported "
  4933. "access mode %d", fan_control_access_mode);
  4934. return 1;
  4935. }
  4936. static int fan_write_cmd_disable(const char *cmd, int *rc)
  4937. {
  4938. if (strlencmp(cmd, "disable") != 0)
  4939. return 0;
  4940. *rc = fan_set_disable();
  4941. if (*rc == -ENXIO)
  4942. printk(TPACPI_ERR "disable command accepted for unsupported "
  4943. "access mode %d", fan_control_access_mode);
  4944. return 1;
  4945. }
  4946. static int fan_write_cmd_speed(const char *cmd, int *rc)
  4947. {
  4948. int speed;
  4949. /* TODO:
  4950. * Support speed <low> <medium> <high> ? */
  4951. if (sscanf(cmd, "speed %d", &speed) != 1)
  4952. return 0;
  4953. *rc = fan_set_speed(speed);
  4954. if (*rc == -ENXIO)
  4955. printk(TPACPI_ERR "speed command accepted for unsupported "
  4956. "access mode %d", fan_control_access_mode);
  4957. return 1;
  4958. }
  4959. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  4960. {
  4961. int interval;
  4962. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  4963. return 0;
  4964. if (interval < 0 || interval > 120)
  4965. *rc = -EINVAL;
  4966. else
  4967. fan_watchdog_maxinterval = interval;
  4968. return 1;
  4969. }
  4970. static int fan_write(char *buf)
  4971. {
  4972. char *cmd;
  4973. int rc = 0;
  4974. while (!rc && (cmd = next_cmd(&buf))) {
  4975. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  4976. fan_write_cmd_level(cmd, &rc)) &&
  4977. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  4978. (fan_write_cmd_enable(cmd, &rc) ||
  4979. fan_write_cmd_disable(cmd, &rc) ||
  4980. fan_write_cmd_watchdog(cmd, &rc))) &&
  4981. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  4982. fan_write_cmd_speed(cmd, &rc))
  4983. )
  4984. rc = -EINVAL;
  4985. else if (!rc)
  4986. fan_watchdog_reset();
  4987. }
  4988. return rc;
  4989. }
  4990. static struct ibm_struct fan_driver_data = {
  4991. .name = "fan",
  4992. .read = fan_read,
  4993. .write = fan_write,
  4994. .exit = fan_exit,
  4995. };
  4996. /****************************************************************************
  4997. ****************************************************************************
  4998. *
  4999. * Infrastructure
  5000. *
  5001. ****************************************************************************
  5002. ****************************************************************************/
  5003. /* sysfs name ---------------------------------------------------------- */
  5004. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  5005. struct device_attribute *attr,
  5006. char *buf)
  5007. {
  5008. return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
  5009. }
  5010. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  5011. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  5012. /* --------------------------------------------------------------------- */
  5013. /* /proc support */
  5014. static struct proc_dir_entry *proc_dir;
  5015. /*
  5016. * Module and infrastructure proble, init and exit handling
  5017. */
  5018. static int force_load;
  5019. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  5020. static const char * __init str_supported(int is_supported)
  5021. {
  5022. static char text_unsupported[] __initdata = "not supported";
  5023. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  5024. }
  5025. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  5026. static void ibm_exit(struct ibm_struct *ibm)
  5027. {
  5028. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  5029. list_del_init(&ibm->all_drivers);
  5030. if (ibm->flags.acpi_notify_installed) {
  5031. dbg_printk(TPACPI_DBG_EXIT,
  5032. "%s: acpi_remove_notify_handler\n", ibm->name);
  5033. BUG_ON(!ibm->acpi);
  5034. acpi_remove_notify_handler(*ibm->acpi->handle,
  5035. ibm->acpi->type,
  5036. dispatch_acpi_notify);
  5037. ibm->flags.acpi_notify_installed = 0;
  5038. ibm->flags.acpi_notify_installed = 0;
  5039. }
  5040. if (ibm->flags.proc_created) {
  5041. dbg_printk(TPACPI_DBG_EXIT,
  5042. "%s: remove_proc_entry\n", ibm->name);
  5043. remove_proc_entry(ibm->name, proc_dir);
  5044. ibm->flags.proc_created = 0;
  5045. }
  5046. if (ibm->flags.acpi_driver_registered) {
  5047. dbg_printk(TPACPI_DBG_EXIT,
  5048. "%s: acpi_bus_unregister_driver\n", ibm->name);
  5049. BUG_ON(!ibm->acpi);
  5050. acpi_bus_unregister_driver(ibm->acpi->driver);
  5051. kfree(ibm->acpi->driver);
  5052. ibm->acpi->driver = NULL;
  5053. ibm->flags.acpi_driver_registered = 0;
  5054. }
  5055. if (ibm->flags.init_called && ibm->exit) {
  5056. ibm->exit();
  5057. ibm->flags.init_called = 0;
  5058. }
  5059. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  5060. }
  5061. static int __init ibm_init(struct ibm_init_struct *iibm)
  5062. {
  5063. int ret;
  5064. struct ibm_struct *ibm = iibm->data;
  5065. struct proc_dir_entry *entry;
  5066. BUG_ON(ibm == NULL);
  5067. INIT_LIST_HEAD(&ibm->all_drivers);
  5068. if (ibm->flags.experimental && !experimental)
  5069. return 0;
  5070. dbg_printk(TPACPI_DBG_INIT,
  5071. "probing for %s\n", ibm->name);
  5072. if (iibm->init) {
  5073. ret = iibm->init(iibm);
  5074. if (ret > 0)
  5075. return 0; /* probe failed */
  5076. if (ret)
  5077. return ret;
  5078. ibm->flags.init_called = 1;
  5079. }
  5080. if (ibm->acpi) {
  5081. if (ibm->acpi->hid) {
  5082. ret = register_tpacpi_subdriver(ibm);
  5083. if (ret)
  5084. goto err_out;
  5085. }
  5086. if (ibm->acpi->notify) {
  5087. ret = setup_acpi_notify(ibm);
  5088. if (ret == -ENODEV) {
  5089. printk(TPACPI_NOTICE "disabling subdriver %s\n",
  5090. ibm->name);
  5091. ret = 0;
  5092. goto err_out;
  5093. }
  5094. if (ret < 0)
  5095. goto err_out;
  5096. }
  5097. }
  5098. dbg_printk(TPACPI_DBG_INIT,
  5099. "%s installed\n", ibm->name);
  5100. if (ibm->read) {
  5101. entry = create_proc_entry(ibm->name,
  5102. S_IFREG | S_IRUGO | S_IWUSR,
  5103. proc_dir);
  5104. if (!entry) {
  5105. printk(TPACPI_ERR "unable to create proc entry %s\n",
  5106. ibm->name);
  5107. ret = -ENODEV;
  5108. goto err_out;
  5109. }
  5110. entry->owner = THIS_MODULE;
  5111. entry->data = ibm;
  5112. entry->read_proc = &dispatch_procfs_read;
  5113. if (ibm->write)
  5114. entry->write_proc = &dispatch_procfs_write;
  5115. ibm->flags.proc_created = 1;
  5116. }
  5117. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  5118. return 0;
  5119. err_out:
  5120. dbg_printk(TPACPI_DBG_INIT,
  5121. "%s: at error exit path with result %d\n",
  5122. ibm->name, ret);
  5123. ibm_exit(ibm);
  5124. return (ret < 0)? ret : 0;
  5125. }
  5126. /* Probing */
  5127. static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
  5128. {
  5129. const struct dmi_device *dev = NULL;
  5130. char ec_fw_string[18];
  5131. if (!tp)
  5132. return;
  5133. memset(tp, 0, sizeof(*tp));
  5134. if (dmi_name_in_vendors("IBM"))
  5135. tp->vendor = PCI_VENDOR_ID_IBM;
  5136. else if (dmi_name_in_vendors("LENOVO"))
  5137. tp->vendor = PCI_VENDOR_ID_LENOVO;
  5138. else
  5139. return;
  5140. tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
  5141. GFP_KERNEL);
  5142. if (!tp->bios_version_str)
  5143. return;
  5144. tp->bios_model = tp->bios_version_str[0]
  5145. | (tp->bios_version_str[1] << 8);
  5146. /*
  5147. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  5148. * X32 or newer, all Z series; Some models must have an
  5149. * up-to-date BIOS or they will not be detected.
  5150. *
  5151. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  5152. */
  5153. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  5154. if (sscanf(dev->name,
  5155. "IBM ThinkPad Embedded Controller -[%17c",
  5156. ec_fw_string) == 1) {
  5157. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  5158. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  5159. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  5160. tp->ec_model = ec_fw_string[0]
  5161. | (ec_fw_string[1] << 8);
  5162. break;
  5163. }
  5164. }
  5165. tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
  5166. GFP_KERNEL);
  5167. if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
  5168. kfree(tp->model_str);
  5169. tp->model_str = NULL;
  5170. }
  5171. tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
  5172. GFP_KERNEL);
  5173. }
  5174. static int __init probe_for_thinkpad(void)
  5175. {
  5176. int is_thinkpad;
  5177. if (acpi_disabled)
  5178. return -ENODEV;
  5179. /*
  5180. * Non-ancient models have better DMI tagging, but very old models
  5181. * don't.
  5182. */
  5183. is_thinkpad = (thinkpad_id.model_str != NULL);
  5184. /* ec is required because many other handles are relative to it */
  5185. TPACPI_ACPIHANDLE_INIT(ec);
  5186. if (!ec_handle) {
  5187. if (is_thinkpad)
  5188. printk(TPACPI_ERR
  5189. "Not yet supported ThinkPad detected!\n");
  5190. return -ENODEV;
  5191. }
  5192. /*
  5193. * Risks a regression on very old machines, but reduces potential
  5194. * false positives a damn great deal
  5195. */
  5196. if (!is_thinkpad)
  5197. is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
  5198. if (!is_thinkpad && !force_load)
  5199. return -ENODEV;
  5200. return 0;
  5201. }
  5202. /* Module init, exit, parameters */
  5203. static struct ibm_init_struct ibms_init[] __initdata = {
  5204. {
  5205. .init = thinkpad_acpi_driver_init,
  5206. .data = &thinkpad_acpi_driver_data,
  5207. },
  5208. {
  5209. .init = hotkey_init,
  5210. .data = &hotkey_driver_data,
  5211. },
  5212. {
  5213. .init = bluetooth_init,
  5214. .data = &bluetooth_driver_data,
  5215. },
  5216. {
  5217. .init = wan_init,
  5218. .data = &wan_driver_data,
  5219. },
  5220. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  5221. {
  5222. .init = video_init,
  5223. .data = &video_driver_data,
  5224. },
  5225. #endif
  5226. {
  5227. .init = light_init,
  5228. .data = &light_driver_data,
  5229. },
  5230. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  5231. {
  5232. .init = dock_init,
  5233. .data = &dock_driver_data[0],
  5234. },
  5235. {
  5236. .init = dock_init2,
  5237. .data = &dock_driver_data[1],
  5238. },
  5239. #endif
  5240. #ifdef CONFIG_THINKPAD_ACPI_BAY
  5241. {
  5242. .init = bay_init,
  5243. .data = &bay_driver_data,
  5244. },
  5245. #endif
  5246. {
  5247. .init = cmos_init,
  5248. .data = &cmos_driver_data,
  5249. },
  5250. {
  5251. .init = led_init,
  5252. .data = &led_driver_data,
  5253. },
  5254. {
  5255. .init = beep_init,
  5256. .data = &beep_driver_data,
  5257. },
  5258. {
  5259. .init = thermal_init,
  5260. .data = &thermal_driver_data,
  5261. },
  5262. {
  5263. .data = &ecdump_driver_data,
  5264. },
  5265. {
  5266. .init = brightness_init,
  5267. .data = &brightness_driver_data,
  5268. },
  5269. {
  5270. .data = &volume_driver_data,
  5271. },
  5272. {
  5273. .init = fan_init,
  5274. .data = &fan_driver_data,
  5275. },
  5276. };
  5277. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  5278. {
  5279. unsigned int i;
  5280. struct ibm_struct *ibm;
  5281. if (!kp || !kp->name || !val)
  5282. return -EINVAL;
  5283. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  5284. ibm = ibms_init[i].data;
  5285. WARN_ON(ibm == NULL);
  5286. if (!ibm || !ibm->name)
  5287. continue;
  5288. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  5289. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  5290. return -ENOSPC;
  5291. strcpy(ibms_init[i].param, val);
  5292. strcat(ibms_init[i].param, ",");
  5293. return 0;
  5294. }
  5295. }
  5296. return -EINVAL;
  5297. }
  5298. module_param(experimental, int, 0);
  5299. MODULE_PARM_DESC(experimental,
  5300. "Enables experimental features when non-zero");
  5301. module_param_named(debug, dbg_level, uint, 0);
  5302. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  5303. module_param(force_load, bool, 0);
  5304. MODULE_PARM_DESC(force_load,
  5305. "Attempts to load the driver even on a "
  5306. "mis-identified ThinkPad when true");
  5307. module_param_named(fan_control, fan_control_allowed, bool, 0);
  5308. MODULE_PARM_DESC(fan_control,
  5309. "Enables setting fan parameters features when true");
  5310. module_param_named(brightness_mode, brightness_mode, int, 0);
  5311. MODULE_PARM_DESC(brightness_mode,
  5312. "Selects brightness control strategy: "
  5313. "0=auto, 1=EC, 2=CMOS, 3=both");
  5314. module_param(brightness_enable, uint, 0);
  5315. MODULE_PARM_DESC(brightness_enable,
  5316. "Enables backlight control when 1, disables when 0");
  5317. module_param(hotkey_report_mode, uint, 0);
  5318. MODULE_PARM_DESC(hotkey_report_mode,
  5319. "used for backwards compatibility with userspace, "
  5320. "see documentation");
  5321. #define TPACPI_PARAM(feature) \
  5322. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  5323. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
  5324. "at module load, see documentation")
  5325. TPACPI_PARAM(hotkey);
  5326. TPACPI_PARAM(bluetooth);
  5327. TPACPI_PARAM(video);
  5328. TPACPI_PARAM(light);
  5329. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  5330. TPACPI_PARAM(dock);
  5331. #endif
  5332. #ifdef CONFIG_THINKPAD_ACPI_BAY
  5333. TPACPI_PARAM(bay);
  5334. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  5335. TPACPI_PARAM(cmos);
  5336. TPACPI_PARAM(led);
  5337. TPACPI_PARAM(beep);
  5338. TPACPI_PARAM(ecdump);
  5339. TPACPI_PARAM(brightness);
  5340. TPACPI_PARAM(volume);
  5341. TPACPI_PARAM(fan);
  5342. static void thinkpad_acpi_module_exit(void)
  5343. {
  5344. struct ibm_struct *ibm, *itmp;
  5345. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  5346. list_for_each_entry_safe_reverse(ibm, itmp,
  5347. &tpacpi_all_drivers,
  5348. all_drivers) {
  5349. ibm_exit(ibm);
  5350. }
  5351. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  5352. if (tpacpi_inputdev) {
  5353. if (tp_features.input_device_registered)
  5354. input_unregister_device(tpacpi_inputdev);
  5355. else
  5356. input_free_device(tpacpi_inputdev);
  5357. }
  5358. if (tpacpi_hwmon)
  5359. hwmon_device_unregister(tpacpi_hwmon);
  5360. if (tp_features.sensors_pdev_attrs_registered)
  5361. device_remove_file(&tpacpi_sensors_pdev->dev,
  5362. &dev_attr_thinkpad_acpi_pdev_name);
  5363. if (tpacpi_sensors_pdev)
  5364. platform_device_unregister(tpacpi_sensors_pdev);
  5365. if (tpacpi_pdev)
  5366. platform_device_unregister(tpacpi_pdev);
  5367. if (tp_features.sensors_pdrv_attrs_registered)
  5368. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  5369. if (tp_features.platform_drv_attrs_registered)
  5370. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  5371. if (tp_features.sensors_pdrv_registered)
  5372. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  5373. if (tp_features.platform_drv_registered)
  5374. platform_driver_unregister(&tpacpi_pdriver);
  5375. if (proc_dir)
  5376. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  5377. if (tpacpi_wq)
  5378. destroy_workqueue(tpacpi_wq);
  5379. kfree(thinkpad_id.bios_version_str);
  5380. kfree(thinkpad_id.ec_version_str);
  5381. kfree(thinkpad_id.model_str);
  5382. }
  5383. static int __init thinkpad_acpi_module_init(void)
  5384. {
  5385. int ret, i;
  5386. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  5387. /* Parameter checking */
  5388. if (hotkey_report_mode > 2)
  5389. return -EINVAL;
  5390. /* Driver-level probe */
  5391. get_thinkpad_model_data(&thinkpad_id);
  5392. ret = probe_for_thinkpad();
  5393. if (ret) {
  5394. thinkpad_acpi_module_exit();
  5395. return ret;
  5396. }
  5397. /* Driver initialization */
  5398. TPACPI_ACPIHANDLE_INIT(ecrd);
  5399. TPACPI_ACPIHANDLE_INIT(ecwr);
  5400. tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
  5401. if (!tpacpi_wq) {
  5402. thinkpad_acpi_module_exit();
  5403. return -ENOMEM;
  5404. }
  5405. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  5406. if (!proc_dir) {
  5407. printk(TPACPI_ERR
  5408. "unable to create proc dir " TPACPI_PROC_DIR);
  5409. thinkpad_acpi_module_exit();
  5410. return -ENODEV;
  5411. }
  5412. proc_dir->owner = THIS_MODULE;
  5413. ret = platform_driver_register(&tpacpi_pdriver);
  5414. if (ret) {
  5415. printk(TPACPI_ERR
  5416. "unable to register main platform driver\n");
  5417. thinkpad_acpi_module_exit();
  5418. return ret;
  5419. }
  5420. tp_features.platform_drv_registered = 1;
  5421. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  5422. if (ret) {
  5423. printk(TPACPI_ERR
  5424. "unable to register hwmon platform driver\n");
  5425. thinkpad_acpi_module_exit();
  5426. return ret;
  5427. }
  5428. tp_features.sensors_pdrv_registered = 1;
  5429. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  5430. if (!ret) {
  5431. tp_features.platform_drv_attrs_registered = 1;
  5432. ret = tpacpi_create_driver_attributes(
  5433. &tpacpi_hwmon_pdriver.driver);
  5434. }
  5435. if (ret) {
  5436. printk(TPACPI_ERR
  5437. "unable to create sysfs driver attributes\n");
  5438. thinkpad_acpi_module_exit();
  5439. return ret;
  5440. }
  5441. tp_features.sensors_pdrv_attrs_registered = 1;
  5442. /* Device initialization */
  5443. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
  5444. NULL, 0);
  5445. if (IS_ERR(tpacpi_pdev)) {
  5446. ret = PTR_ERR(tpacpi_pdev);
  5447. tpacpi_pdev = NULL;
  5448. printk(TPACPI_ERR "unable to register platform device\n");
  5449. thinkpad_acpi_module_exit();
  5450. return ret;
  5451. }
  5452. tpacpi_sensors_pdev = platform_device_register_simple(
  5453. TPACPI_HWMON_DRVR_NAME,
  5454. -1, NULL, 0);
  5455. if (IS_ERR(tpacpi_sensors_pdev)) {
  5456. ret = PTR_ERR(tpacpi_sensors_pdev);
  5457. tpacpi_sensors_pdev = NULL;
  5458. printk(TPACPI_ERR
  5459. "unable to register hwmon platform device\n");
  5460. thinkpad_acpi_module_exit();
  5461. return ret;
  5462. }
  5463. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  5464. &dev_attr_thinkpad_acpi_pdev_name);
  5465. if (ret) {
  5466. printk(TPACPI_ERR
  5467. "unable to create sysfs hwmon device attributes\n");
  5468. thinkpad_acpi_module_exit();
  5469. return ret;
  5470. }
  5471. tp_features.sensors_pdev_attrs_registered = 1;
  5472. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  5473. if (IS_ERR(tpacpi_hwmon)) {
  5474. ret = PTR_ERR(tpacpi_hwmon);
  5475. tpacpi_hwmon = NULL;
  5476. printk(TPACPI_ERR "unable to register hwmon device\n");
  5477. thinkpad_acpi_module_exit();
  5478. return ret;
  5479. }
  5480. mutex_init(&tpacpi_inputdev_send_mutex);
  5481. tpacpi_inputdev = input_allocate_device();
  5482. if (!tpacpi_inputdev) {
  5483. printk(TPACPI_ERR "unable to allocate input device\n");
  5484. thinkpad_acpi_module_exit();
  5485. return -ENOMEM;
  5486. } else {
  5487. /* Prepare input device, but don't register */
  5488. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  5489. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  5490. tpacpi_inputdev->id.bustype = BUS_HOST;
  5491. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  5492. thinkpad_id.vendor :
  5493. PCI_VENDOR_ID_IBM;
  5494. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  5495. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  5496. }
  5497. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  5498. ret = ibm_init(&ibms_init[i]);
  5499. if (ret >= 0 && *ibms_init[i].param)
  5500. ret = ibms_init[i].data->write(ibms_init[i].param);
  5501. if (ret < 0) {
  5502. thinkpad_acpi_module_exit();
  5503. return ret;
  5504. }
  5505. }
  5506. ret = input_register_device(tpacpi_inputdev);
  5507. if (ret < 0) {
  5508. printk(TPACPI_ERR "unable to register input device\n");
  5509. thinkpad_acpi_module_exit();
  5510. return ret;
  5511. } else {
  5512. tp_features.input_device_registered = 1;
  5513. }
  5514. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  5515. return 0;
  5516. }
  5517. /* Please remove this in year 2009 */
  5518. MODULE_ALIAS("ibm_acpi");
  5519. MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
  5520. /*
  5521. * DMI matching for module autoloading
  5522. *
  5523. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  5524. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  5525. *
  5526. * Only models listed in thinkwiki will be supported, so add yours
  5527. * if it is not there yet.
  5528. */
  5529. #define IBM_BIOS_MODULE_ALIAS(__type) \
  5530. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
  5531. /* Non-ancient thinkpads */
  5532. MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
  5533. MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
  5534. /* Ancient thinkpad BIOSes have to be identified by
  5535. * BIOS type or model number, and there are far less
  5536. * BIOS types than model numbers... */
  5537. IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
  5538. IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
  5539. IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
  5540. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  5541. MODULE_DESCRIPTION(TPACPI_DESC);
  5542. MODULE_VERSION(TPACPI_VERSION);
  5543. MODULE_LICENSE("GPL");
  5544. module_init(thinkpad_acpi_module_init);
  5545. module_exit(thinkpad_acpi_module_exit);