thinkpad_acpi.c 149 KB

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