thinkpad_acpi.c 229 KB

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