thinkpad_acpi.c 151 KB

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