thinkpad_acpi.c 231 KB

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