thinkpad_acpi.c 168 KB

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