thinkpad_acpi.c 192 KB

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