thinkpad_acpi.c 183 KB

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