thinkpad_acpi.c 150 KB

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