thinkpad_acpi.c 154 KB

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