thinkpad_acpi.c 229 KB

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