thinkpad_acpi.c 303 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * thinkpad_acpi.c - ThinkPad ACPI Extras
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <[email protected]>
  6. * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #define TPACPI_VERSION "0.26"
  10. #define TPACPI_SYSFS_VERSION 0x030000
  11. /*
  12. * Changelog:
  13. * 2007-10-20 changelog trimmed down
  14. *
  15. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  16. * drivers/misc.
  17. *
  18. * 2006-11-22 0.13 new maintainer
  19. * changelog now lives in git commit history, and will
  20. * not be updated further in-file.
  21. *
  22. * 2005-03-17 0.11 support for 600e, 770x
  23. * thanks to Jamie Lentin <[email protected]>
  24. *
  25. * 2005-01-16 0.9 use MODULE_VERSION
  26. * thanks to Henrik Brix Andersen <[email protected]>
  27. * fix parameter passing on module loading
  28. * thanks to Rusty Russell <[email protected]>
  29. * thanks to Jim Radford <[email protected]>
  30. * 2004-11-08 0.8 fix init error case, don't return from a macro
  31. * thanks to Chris Wright <[email protected]>
  32. */
  33. #include <linux/acpi.h>
  34. #include <linux/backlight.h>
  35. #include <linux/bitops.h>
  36. #include <linux/delay.h>
  37. #include <linux/dmi.h>
  38. #include <linux/fb.h>
  39. #include <linux/freezer.h>
  40. #include <linux/hwmon.h>
  41. #include <linux/hwmon-sysfs.h>
  42. #include <linux/init.h>
  43. #include <linux/input.h>
  44. #include <linux/jiffies.h>
  45. #include <linux/kernel.h>
  46. #include <linux/kthread.h>
  47. #include <linux/leds.h>
  48. #include <linux/list.h>
  49. #include <linux/module.h>
  50. #include <linux/mutex.h>
  51. #include <linux/nvram.h>
  52. #include <linux/pci.h>
  53. #include <linux/platform_device.h>
  54. #include <linux/platform_profile.h>
  55. #include <linux/power_supply.h>
  56. #include <linux/proc_fs.h>
  57. #include <linux/rfkill.h>
  58. #include <linux/sched.h>
  59. #include <linux/sched/signal.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/slab.h>
  62. #include <linux/string.h>
  63. #include <linux/string_helpers.h>
  64. #include <linux/sysfs.h>
  65. #include <linux/types.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/workqueue.h>
  68. #include <acpi/battery.h>
  69. #include <acpi/video.h>
  70. #include <drm/drm_privacy_screen_driver.h>
  71. #include <sound/control.h>
  72. #include <sound/core.h>
  73. #include <sound/initval.h>
  74. #include "dual_accel_detect.h"
  75. /* ThinkPad CMOS commands */
  76. #define TP_CMOS_VOLUME_DOWN 0
  77. #define TP_CMOS_VOLUME_UP 1
  78. #define TP_CMOS_VOLUME_MUTE 2
  79. #define TP_CMOS_BRIGHTNESS_UP 4
  80. #define TP_CMOS_BRIGHTNESS_DOWN 5
  81. #define TP_CMOS_THINKLIGHT_ON 12
  82. #define TP_CMOS_THINKLIGHT_OFF 13
  83. /* NVRAM Addresses */
  84. enum tp_nvram_addr {
  85. TP_NVRAM_ADDR_HK2 = 0x57,
  86. TP_NVRAM_ADDR_THINKLIGHT = 0x58,
  87. TP_NVRAM_ADDR_VIDEO = 0x59,
  88. TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
  89. TP_NVRAM_ADDR_MIXER = 0x60,
  90. };
  91. /* NVRAM bit masks */
  92. enum {
  93. TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
  94. TP_NVRAM_MASK_HKT_ZOOM = 0x20,
  95. TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
  96. TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
  97. TP_NVRAM_MASK_THINKLIGHT = 0x10,
  98. TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
  99. TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
  100. TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
  101. TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
  102. TP_NVRAM_MASK_MUTE = 0x40,
  103. TP_NVRAM_MASK_HKT_VOLUME = 0x80,
  104. TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
  105. TP_NVRAM_POS_LEVEL_VOLUME = 0,
  106. };
  107. /* Misc NVRAM-related */
  108. enum {
  109. TP_NVRAM_LEVEL_VOLUME_MAX = 14,
  110. };
  111. /* ACPI HIDs */
  112. #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
  113. #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
  114. #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
  115. #define TPACPI_ACPI_EC_HID "PNP0C09"
  116. /* Input IDs */
  117. #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
  118. #define TPACPI_HKEY_INPUT_VERSION 0x4101
  119. /* ACPI \WGSV commands */
  120. enum {
  121. TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
  122. TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
  123. TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
  124. TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
  125. };
  126. /* TP_ACPI_WGSV_GET_STATE bits */
  127. enum {
  128. TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
  129. TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
  130. TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
  131. TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
  132. TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
  133. TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
  134. TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
  135. TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
  136. TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
  137. TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
  138. };
  139. /* HKEY events */
  140. enum tpacpi_hkey_event_t {
  141. /* Hotkey-related */
  142. TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
  143. TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
  144. TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
  145. TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
  146. TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
  147. TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
  148. TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
  149. TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
  150. TP_HKEY_EV_AMT_TOGGLE = 0x131a, /* Toggle AMT on/off */
  151. /* Reasons for waking up from S3/S4 */
  152. TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
  153. TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
  154. TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
  155. TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
  156. TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
  157. TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
  158. /* Auto-sleep after eject request */
  159. TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
  160. TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
  161. /* Misc bay events */
  162. TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
  163. TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
  164. or port replicator */
  165. TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
  166. dock or port replicator */
  167. /*
  168. * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
  169. * when keyboard cover is attached, detached or folded onto the back
  170. */
  171. TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
  172. TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
  173. /* User-interface events */
  174. TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
  175. TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
  176. TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
  177. TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
  178. TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
  179. * enter/leave tablet mode
  180. */
  181. TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
  182. TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
  183. TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
  184. /* Key-related user-interface events */
  185. TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
  186. TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
  187. TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
  188. /* Thermal events */
  189. TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
  190. TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
  191. TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
  192. TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
  193. TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
  194. TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
  195. * command completed. Related to
  196. * AML DYTC */
  197. TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
  198. * changed. Related to AML GMTS */
  199. /* AC-related events */
  200. TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
  201. /* Further user-interface events */
  202. TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
  203. TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
  204. /* Misc */
  205. TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
  206. };
  207. /****************************************************************************
  208. * Main driver
  209. */
  210. #define TPACPI_NAME "thinkpad"
  211. #define TPACPI_DESC "ThinkPad ACPI Extras"
  212. #define TPACPI_FILE TPACPI_NAME "_acpi"
  213. #define TPACPI_URL "http://ibm-acpi.sf.net/"
  214. #define TPACPI_MAIL "[email protected]"
  215. #define TPACPI_PROC_DIR "ibm"
  216. #define TPACPI_ACPI_EVENT_PREFIX "ibm"
  217. #define TPACPI_DRVR_NAME TPACPI_FILE
  218. #define TPACPI_DRVR_SHORTNAME "tpacpi"
  219. #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
  220. #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
  221. #define TPACPI_WORKQUEUE_NAME "ktpacpid"
  222. #define TPACPI_MAX_ACPI_ARGS 3
  223. /* Debugging printk groups */
  224. #define TPACPI_DBG_ALL 0xffff
  225. #define TPACPI_DBG_DISCLOSETASK 0x8000
  226. #define TPACPI_DBG_INIT 0x0001
  227. #define TPACPI_DBG_EXIT 0x0002
  228. #define TPACPI_DBG_RFKILL 0x0004
  229. #define TPACPI_DBG_HKEY 0x0008
  230. #define TPACPI_DBG_FAN 0x0010
  231. #define TPACPI_DBG_BRGHT 0x0020
  232. #define TPACPI_DBG_MIXER 0x0040
  233. #define FAN_NOT_PRESENT 65535
  234. #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
  235. /****************************************************************************
  236. * Driver-wide structs and misc. variables
  237. */
  238. struct ibm_struct;
  239. struct tp_acpi_drv_struct {
  240. const struct acpi_device_id *hid;
  241. struct acpi_driver *driver;
  242. void (*notify) (struct ibm_struct *, u32);
  243. acpi_handle *handle;
  244. u32 type;
  245. struct acpi_device *device;
  246. };
  247. struct ibm_struct {
  248. char *name;
  249. int (*read) (struct seq_file *);
  250. int (*write) (char *);
  251. void (*exit) (void);
  252. void (*resume) (void);
  253. void (*suspend) (void);
  254. void (*shutdown) (void);
  255. struct list_head all_drivers;
  256. struct tp_acpi_drv_struct *acpi;
  257. struct {
  258. u8 acpi_driver_registered:1;
  259. u8 acpi_notify_installed:1;
  260. u8 proc_created:1;
  261. u8 init_called:1;
  262. u8 experimental:1;
  263. } flags;
  264. };
  265. struct ibm_init_struct {
  266. char param[32];
  267. int (*init) (struct ibm_init_struct *);
  268. umode_t base_procfs_mode;
  269. struct ibm_struct *data;
  270. };
  271. /* DMI Quirks */
  272. struct quirk_entry {
  273. bool btusb_bug;
  274. u32 s2idle_bug_mmio;
  275. };
  276. static struct quirk_entry quirk_btusb_bug = {
  277. .btusb_bug = true,
  278. };
  279. static struct quirk_entry quirk_s2idle_bug = {
  280. .s2idle_bug_mmio = 0xfed80380,
  281. };
  282. static struct {
  283. u32 bluetooth:1;
  284. u32 hotkey:1;
  285. u32 hotkey_mask:1;
  286. u32 hotkey_wlsw:1;
  287. enum {
  288. TP_HOTKEY_TABLET_NONE = 0,
  289. TP_HOTKEY_TABLET_USES_MHKG,
  290. TP_HOTKEY_TABLET_USES_GMMS,
  291. } hotkey_tablet;
  292. u32 kbdlight:1;
  293. u32 light:1;
  294. u32 light_status:1;
  295. u32 bright_acpimode:1;
  296. u32 bright_unkfw:1;
  297. u32 wan:1;
  298. u32 uwb:1;
  299. u32 fan_ctrl_status_undef:1;
  300. u32 second_fan:1;
  301. u32 second_fan_ctl:1;
  302. u32 beep_needs_two_args:1;
  303. u32 mixer_no_level_control:1;
  304. u32 battery_force_primary:1;
  305. u32 input_device_registered:1;
  306. u32 platform_drv_registered:1;
  307. u32 sensors_pdrv_registered:1;
  308. u32 hotkey_poll_active:1;
  309. u32 has_adaptive_kbd:1;
  310. u32 kbd_lang:1;
  311. struct quirk_entry *quirks;
  312. } tp_features;
  313. static struct {
  314. u16 hotkey_mask_ff:1;
  315. u16 volume_ctrl_forbidden:1;
  316. } tp_warned;
  317. struct thinkpad_id_data {
  318. unsigned int vendor; /* ThinkPad vendor:
  319. * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
  320. char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
  321. char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
  322. u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
  323. u32 ec_model;
  324. u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
  325. u16 ec_release;
  326. char *model_str; /* ThinkPad T43 */
  327. char *nummodel_str; /* 9384A9C for a 9384-A9C model */
  328. };
  329. static struct thinkpad_id_data thinkpad_id;
  330. static enum {
  331. TPACPI_LIFE_INIT = 0,
  332. TPACPI_LIFE_RUNNING,
  333. TPACPI_LIFE_EXITING,
  334. } tpacpi_lifecycle;
  335. static int experimental;
  336. static u32 dbg_level;
  337. static struct workqueue_struct *tpacpi_wq;
  338. enum led_status_t {
  339. TPACPI_LED_OFF = 0,
  340. TPACPI_LED_ON,
  341. TPACPI_LED_BLINK,
  342. };
  343. /* tpacpi LED class */
  344. struct tpacpi_led_classdev {
  345. struct led_classdev led_classdev;
  346. int led;
  347. };
  348. /* brightness level capabilities */
  349. static unsigned int bright_maxlvl; /* 0 = unknown */
  350. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  351. static int dbg_wlswemul;
  352. static bool tpacpi_wlsw_emulstate;
  353. static int dbg_bluetoothemul;
  354. static bool tpacpi_bluetooth_emulstate;
  355. static int dbg_wwanemul;
  356. static bool tpacpi_wwan_emulstate;
  357. static int dbg_uwbemul;
  358. static bool tpacpi_uwb_emulstate;
  359. #endif
  360. /*************************************************************************
  361. * Debugging helpers
  362. */
  363. #define dbg_printk(a_dbg_level, format, arg...) \
  364. do { \
  365. if (dbg_level & (a_dbg_level)) \
  366. printk(KERN_DEBUG pr_fmt("%s: " format), \
  367. __func__, ##arg); \
  368. } while (0)
  369. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  370. #define vdbg_printk dbg_printk
  371. static const char *str_supported(int is_supported);
  372. #else
  373. static inline const char *str_supported(int is_supported) { return ""; }
  374. #define vdbg_printk(a_dbg_level, format, arg...) \
  375. do { if (0) no_printk(format, ##arg); } while (0)
  376. #endif
  377. static void tpacpi_log_usertask(const char * const what)
  378. {
  379. printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
  380. what, task_tgid_vnr(current));
  381. }
  382. #define tpacpi_disclose_usertask(what, format, arg...) \
  383. do { \
  384. if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
  385. (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
  386. printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
  387. what, task_tgid_vnr(current), ## arg); \
  388. } \
  389. } while (0)
  390. /*
  391. * Quirk handling helpers
  392. *
  393. * ThinkPad IDs and versions seen in the field so far are
  394. * two or three characters from the set [0-9A-Z], i.e. base 36.
  395. *
  396. * We use values well outside that range as specials.
  397. */
  398. #define TPACPI_MATCH_ANY 0xffffffffU
  399. #define TPACPI_MATCH_ANY_VERSION 0xffffU
  400. #define TPACPI_MATCH_UNKNOWN 0U
  401. /* TPID('1', 'Y') == 0x3159 */
  402. #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
  403. #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
  404. #define TPVER TPID
  405. #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
  406. { .vendor = PCI_VENDOR_ID_IBM, \
  407. .bios = TPID(__id1, __id2), \
  408. .ec = TPACPI_MATCH_ANY, \
  409. .quirks = (__quirk) }
  410. #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
  411. { .vendor = PCI_VENDOR_ID_LENOVO, \
  412. .bios = TPID(__id1, __id2), \
  413. .ec = TPACPI_MATCH_ANY, \
  414. .quirks = (__quirk) }
  415. #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
  416. { .vendor = PCI_VENDOR_ID_LENOVO, \
  417. .bios = TPID3(__id1, __id2, __id3), \
  418. .ec = TPACPI_MATCH_ANY, \
  419. .quirks = (__quirk) }
  420. #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
  421. { .vendor = PCI_VENDOR_ID_IBM, \
  422. .bios = TPACPI_MATCH_ANY, \
  423. .ec = TPID(__id1, __id2), \
  424. .quirks = (__quirk) }
  425. #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
  426. { .vendor = PCI_VENDOR_ID_LENOVO, \
  427. .bios = TPACPI_MATCH_ANY, \
  428. .ec = TPID(__id1, __id2), \
  429. .quirks = (__quirk) }
  430. struct tpacpi_quirk {
  431. unsigned int vendor;
  432. u32 bios;
  433. u32 ec;
  434. unsigned long quirks;
  435. };
  436. /**
  437. * tpacpi_check_quirks() - search BIOS/EC version on a list
  438. * @qlist: array of &struct tpacpi_quirk
  439. * @qlist_size: number of elements in @qlist
  440. *
  441. * Iterates over a quirks list until one is found that matches the
  442. * ThinkPad's vendor, BIOS and EC model.
  443. *
  444. * Returns 0 if nothing matches, otherwise returns the quirks field of
  445. * the matching &struct tpacpi_quirk entry.
  446. *
  447. * The match criteria is: vendor, ec and bios much match.
  448. */
  449. static unsigned long __init tpacpi_check_quirks(
  450. const struct tpacpi_quirk *qlist,
  451. unsigned int qlist_size)
  452. {
  453. while (qlist_size) {
  454. if ((qlist->vendor == thinkpad_id.vendor ||
  455. qlist->vendor == TPACPI_MATCH_ANY) &&
  456. (qlist->bios == thinkpad_id.bios_model ||
  457. qlist->bios == TPACPI_MATCH_ANY) &&
  458. (qlist->ec == thinkpad_id.ec_model ||
  459. qlist->ec == TPACPI_MATCH_ANY))
  460. return qlist->quirks;
  461. qlist_size--;
  462. qlist++;
  463. }
  464. return 0;
  465. }
  466. static inline bool __pure __init tpacpi_is_lenovo(void)
  467. {
  468. return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
  469. }
  470. static inline bool __pure __init tpacpi_is_ibm(void)
  471. {
  472. return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
  473. }
  474. /****************************************************************************
  475. ****************************************************************************
  476. *
  477. * ACPI Helpers and device model
  478. *
  479. ****************************************************************************
  480. ****************************************************************************/
  481. /*************************************************************************
  482. * ACPI basic handles
  483. */
  484. static acpi_handle root_handle;
  485. static acpi_handle ec_handle;
  486. #define TPACPI_HANDLE(object, parent, paths...) \
  487. static acpi_handle object##_handle; \
  488. static const acpi_handle * const object##_parent __initconst = \
  489. &parent##_handle; \
  490. static char *object##_paths[] __initdata = { paths }
  491. TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  492. TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  493. TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
  494. /* T4x, X31, X40 */
  495. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  496. "\\CMS", /* R40, R40e */
  497. ); /* all others */
  498. TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  499. "^HKEY", /* R30, R31 */
  500. "HKEY", /* all others */
  501. ); /* 570 */
  502. /*************************************************************************
  503. * ACPI helpers
  504. */
  505. static int acpi_evalf(acpi_handle handle,
  506. int *res, char *method, char *fmt, ...)
  507. {
  508. char *fmt0 = fmt;
  509. struct acpi_object_list params;
  510. union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
  511. struct acpi_buffer result, *resultp;
  512. union acpi_object out_obj;
  513. acpi_status status;
  514. va_list ap;
  515. char res_type;
  516. int success;
  517. int quiet;
  518. if (!*fmt) {
  519. pr_err("acpi_evalf() called with empty format\n");
  520. return 0;
  521. }
  522. if (*fmt == 'q') {
  523. quiet = 1;
  524. fmt++;
  525. } else
  526. quiet = 0;
  527. res_type = *(fmt++);
  528. params.count = 0;
  529. params.pointer = &in_objs[0];
  530. va_start(ap, fmt);
  531. while (*fmt) {
  532. char c = *(fmt++);
  533. switch (c) {
  534. case 'd': /* int */
  535. in_objs[params.count].integer.value = va_arg(ap, int);
  536. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  537. break;
  538. /* add more types as needed */
  539. default:
  540. pr_err("acpi_evalf() called with invalid format character '%c'\n",
  541. c);
  542. va_end(ap);
  543. return 0;
  544. }
  545. }
  546. va_end(ap);
  547. if (res_type != 'v') {
  548. result.length = sizeof(out_obj);
  549. result.pointer = &out_obj;
  550. resultp = &result;
  551. } else
  552. resultp = NULL;
  553. status = acpi_evaluate_object(handle, method, &params, resultp);
  554. switch (res_type) {
  555. case 'd': /* int */
  556. success = (status == AE_OK &&
  557. out_obj.type == ACPI_TYPE_INTEGER);
  558. if (success && res)
  559. *res = out_obj.integer.value;
  560. break;
  561. case 'v': /* void */
  562. success = status == AE_OK;
  563. break;
  564. /* add more types as needed */
  565. default:
  566. pr_err("acpi_evalf() called with invalid format character '%c'\n",
  567. res_type);
  568. return 0;
  569. }
  570. if (!success && !quiet)
  571. pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
  572. method, fmt0, acpi_format_exception(status));
  573. return success;
  574. }
  575. static int acpi_ec_read(int i, u8 *p)
  576. {
  577. int v;
  578. if (ecrd_handle) {
  579. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  580. return 0;
  581. *p = v;
  582. } else {
  583. if (ec_read(i, p) < 0)
  584. return 0;
  585. }
  586. return 1;
  587. }
  588. static int acpi_ec_write(int i, u8 v)
  589. {
  590. if (ecwr_handle) {
  591. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  592. return 0;
  593. } else {
  594. if (ec_write(i, v) < 0)
  595. return 0;
  596. }
  597. return 1;
  598. }
  599. static int issue_thinkpad_cmos_command(int cmos_cmd)
  600. {
  601. if (!cmos_handle)
  602. return -ENXIO;
  603. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  604. return -EIO;
  605. return 0;
  606. }
  607. /*************************************************************************
  608. * ACPI device model
  609. */
  610. #define TPACPI_ACPIHANDLE_INIT(object) \
  611. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  612. object##_paths, ARRAY_SIZE(object##_paths))
  613. static void __init drv_acpi_handle_init(const char *name,
  614. acpi_handle *handle, const acpi_handle parent,
  615. char **paths, const int num_paths)
  616. {
  617. int i;
  618. acpi_status status;
  619. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  620. name);
  621. for (i = 0; i < num_paths; i++) {
  622. status = acpi_get_handle(parent, paths[i], handle);
  623. if (ACPI_SUCCESS(status)) {
  624. dbg_printk(TPACPI_DBG_INIT,
  625. "Found ACPI handle %s for %s\n",
  626. paths[i], name);
  627. return;
  628. }
  629. }
  630. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  631. name);
  632. *handle = NULL;
  633. }
  634. static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
  635. u32 level, void *context, void **return_value)
  636. {
  637. if (!strcmp(context, "video")) {
  638. struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
  639. if (!dev || strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
  640. return AE_OK;
  641. }
  642. *(acpi_handle *)return_value = handle;
  643. return AE_CTRL_TERMINATE;
  644. }
  645. static void __init tpacpi_acpi_handle_locate(const char *name,
  646. const char *hid,
  647. acpi_handle *handle)
  648. {
  649. acpi_status status;
  650. acpi_handle device_found;
  651. BUG_ON(!name || !handle);
  652. vdbg_printk(TPACPI_DBG_INIT,
  653. "trying to locate ACPI handle for %s, using HID %s\n",
  654. name, hid ? hid : "NULL");
  655. memset(&device_found, 0, sizeof(device_found));
  656. status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
  657. (void *)name, &device_found);
  658. *handle = NULL;
  659. if (ACPI_SUCCESS(status)) {
  660. *handle = device_found;
  661. dbg_printk(TPACPI_DBG_INIT,
  662. "Found ACPI handle for %s\n", name);
  663. } else {
  664. vdbg_printk(TPACPI_DBG_INIT,
  665. "Could not locate an ACPI handle for %s: %s\n",
  666. name, acpi_format_exception(status));
  667. }
  668. }
  669. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  670. {
  671. struct ibm_struct *ibm = data;
  672. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  673. return;
  674. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  675. return;
  676. ibm->acpi->notify(ibm, event);
  677. }
  678. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  679. {
  680. acpi_status status;
  681. BUG_ON(!ibm->acpi);
  682. if (!*ibm->acpi->handle)
  683. return 0;
  684. vdbg_printk(TPACPI_DBG_INIT,
  685. "setting up ACPI notify for %s\n", ibm->name);
  686. ibm->acpi->device = acpi_fetch_acpi_dev(*ibm->acpi->handle);
  687. if (!ibm->acpi->device) {
  688. pr_err("acpi_fetch_acpi_dev(%s) failed\n", ibm->name);
  689. return -ENODEV;
  690. }
  691. ibm->acpi->device->driver_data = ibm;
  692. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  693. TPACPI_ACPI_EVENT_PREFIX,
  694. ibm->name);
  695. status = acpi_install_notify_handler(*ibm->acpi->handle,
  696. ibm->acpi->type, dispatch_acpi_notify, ibm);
  697. if (ACPI_FAILURE(status)) {
  698. if (status == AE_ALREADY_EXISTS) {
  699. pr_notice("another device driver is already handling %s events\n",
  700. ibm->name);
  701. } else {
  702. pr_err("acpi_install_notify_handler(%s) failed: %s\n",
  703. ibm->name, acpi_format_exception(status));
  704. }
  705. return -ENODEV;
  706. }
  707. ibm->flags.acpi_notify_installed = 1;
  708. return 0;
  709. }
  710. static int __init tpacpi_device_add(struct acpi_device *device)
  711. {
  712. return 0;
  713. }
  714. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  715. {
  716. int rc;
  717. dbg_printk(TPACPI_DBG_INIT,
  718. "registering %s as an ACPI driver\n", ibm->name);
  719. BUG_ON(!ibm->acpi);
  720. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  721. if (!ibm->acpi->driver) {
  722. pr_err("failed to allocate memory for ibm->acpi->driver\n");
  723. return -ENOMEM;
  724. }
  725. sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
  726. ibm->acpi->driver->ids = ibm->acpi->hid;
  727. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  728. rc = acpi_bus_register_driver(ibm->acpi->driver);
  729. if (rc < 0) {
  730. pr_err("acpi_bus_register_driver(%s) failed: %d\n",
  731. ibm->name, rc);
  732. kfree(ibm->acpi->driver);
  733. ibm->acpi->driver = NULL;
  734. } else if (!rc)
  735. ibm->flags.acpi_driver_registered = 1;
  736. return rc;
  737. }
  738. /****************************************************************************
  739. ****************************************************************************
  740. *
  741. * Procfs Helpers
  742. *
  743. ****************************************************************************
  744. ****************************************************************************/
  745. static int dispatch_proc_show(struct seq_file *m, void *v)
  746. {
  747. struct ibm_struct *ibm = m->private;
  748. if (!ibm || !ibm->read)
  749. return -EINVAL;
  750. return ibm->read(m);
  751. }
  752. static int dispatch_proc_open(struct inode *inode, struct file *file)
  753. {
  754. return single_open(file, dispatch_proc_show, pde_data(inode));
  755. }
  756. static ssize_t dispatch_proc_write(struct file *file,
  757. const char __user *userbuf,
  758. size_t count, loff_t *pos)
  759. {
  760. struct ibm_struct *ibm = pde_data(file_inode(file));
  761. char *kernbuf;
  762. int ret;
  763. if (!ibm || !ibm->write)
  764. return -EINVAL;
  765. if (count > PAGE_SIZE - 1)
  766. return -EINVAL;
  767. kernbuf = kmalloc(count + 1, GFP_KERNEL);
  768. if (!kernbuf)
  769. return -ENOMEM;
  770. if (copy_from_user(kernbuf, userbuf, count)) {
  771. kfree(kernbuf);
  772. return -EFAULT;
  773. }
  774. kernbuf[count] = 0;
  775. ret = ibm->write(kernbuf);
  776. if (ret == 0)
  777. ret = count;
  778. kfree(kernbuf);
  779. return ret;
  780. }
  781. static const struct proc_ops dispatch_proc_ops = {
  782. .proc_open = dispatch_proc_open,
  783. .proc_read = seq_read,
  784. .proc_lseek = seq_lseek,
  785. .proc_release = single_release,
  786. .proc_write = dispatch_proc_write,
  787. };
  788. /****************************************************************************
  789. ****************************************************************************
  790. *
  791. * Device model: input, hwmon and platform
  792. *
  793. ****************************************************************************
  794. ****************************************************************************/
  795. static struct platform_device *tpacpi_pdev;
  796. static struct platform_device *tpacpi_sensors_pdev;
  797. static struct device *tpacpi_hwmon;
  798. static struct input_dev *tpacpi_inputdev;
  799. static struct mutex tpacpi_inputdev_send_mutex;
  800. static LIST_HEAD(tpacpi_all_drivers);
  801. #ifdef CONFIG_PM_SLEEP
  802. static int tpacpi_suspend_handler(struct device *dev)
  803. {
  804. struct ibm_struct *ibm, *itmp;
  805. list_for_each_entry_safe(ibm, itmp,
  806. &tpacpi_all_drivers,
  807. all_drivers) {
  808. if (ibm->suspend)
  809. (ibm->suspend)();
  810. }
  811. return 0;
  812. }
  813. static int tpacpi_resume_handler(struct device *dev)
  814. {
  815. struct ibm_struct *ibm, *itmp;
  816. list_for_each_entry_safe(ibm, itmp,
  817. &tpacpi_all_drivers,
  818. all_drivers) {
  819. if (ibm->resume)
  820. (ibm->resume)();
  821. }
  822. return 0;
  823. }
  824. #endif
  825. static SIMPLE_DEV_PM_OPS(tpacpi_pm,
  826. tpacpi_suspend_handler, tpacpi_resume_handler);
  827. static void tpacpi_shutdown_handler(struct platform_device *pdev)
  828. {
  829. struct ibm_struct *ibm, *itmp;
  830. list_for_each_entry_safe(ibm, itmp,
  831. &tpacpi_all_drivers,
  832. all_drivers) {
  833. if (ibm->shutdown)
  834. (ibm->shutdown)();
  835. }
  836. }
  837. /*************************************************************************
  838. * sysfs support helpers
  839. */
  840. static int parse_strtoul(const char *buf,
  841. unsigned long max, unsigned long *value)
  842. {
  843. char *endp;
  844. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  845. endp = skip_spaces(endp);
  846. if (*endp || *value > max)
  847. return -EINVAL;
  848. return 0;
  849. }
  850. static void tpacpi_disable_brightness_delay(void)
  851. {
  852. if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
  853. pr_notice("ACPI backlight control delay disabled\n");
  854. }
  855. static void printk_deprecated_attribute(const char * const what,
  856. const char * const details)
  857. {
  858. tpacpi_log_usertask("deprecated sysfs attribute");
  859. pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
  860. what, details);
  861. }
  862. /*************************************************************************
  863. * rfkill and radio control support helpers
  864. */
  865. /*
  866. * ThinkPad-ACPI firmware handling model:
  867. *
  868. * WLSW (master wireless switch) is event-driven, and is common to all
  869. * firmware-controlled radios. It cannot be controlled, just monitored,
  870. * as expected. It overrides all radio state in firmware
  871. *
  872. * The kernel, a masked-off hotkey, and WLSW can change the radio state
  873. * (TODO: verify how WLSW interacts with the returned radio state).
  874. *
  875. * The only time there are shadow radio state changes, is when
  876. * masked-off hotkeys are used.
  877. */
  878. /*
  879. * Internal driver API for radio state:
  880. *
  881. * int: < 0 = error, otherwise enum tpacpi_rfkill_state
  882. * bool: true means radio blocked (off)
  883. */
  884. enum tpacpi_rfkill_state {
  885. TPACPI_RFK_RADIO_OFF = 0,
  886. TPACPI_RFK_RADIO_ON
  887. };
  888. /* rfkill switches */
  889. enum tpacpi_rfk_id {
  890. TPACPI_RFK_BLUETOOTH_SW_ID = 0,
  891. TPACPI_RFK_WWAN_SW_ID,
  892. TPACPI_RFK_UWB_SW_ID,
  893. TPACPI_RFK_SW_MAX
  894. };
  895. static const char *tpacpi_rfkill_names[] = {
  896. [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
  897. [TPACPI_RFK_WWAN_SW_ID] = "wwan",
  898. [TPACPI_RFK_UWB_SW_ID] = "uwb",
  899. [TPACPI_RFK_SW_MAX] = NULL
  900. };
  901. /* ThinkPad-ACPI rfkill subdriver */
  902. struct tpacpi_rfk {
  903. struct rfkill *rfkill;
  904. enum tpacpi_rfk_id id;
  905. const struct tpacpi_rfk_ops *ops;
  906. };
  907. struct tpacpi_rfk_ops {
  908. /* firmware interface */
  909. int (*get_status)(void);
  910. int (*set_status)(const enum tpacpi_rfkill_state);
  911. };
  912. static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
  913. /* Query FW and update rfkill sw state for a given rfkill switch */
  914. static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
  915. {
  916. int status;
  917. if (!tp_rfk)
  918. return -ENODEV;
  919. status = (tp_rfk->ops->get_status)();
  920. if (status < 0)
  921. return status;
  922. rfkill_set_sw_state(tp_rfk->rfkill,
  923. (status == TPACPI_RFK_RADIO_OFF));
  924. return status;
  925. }
  926. /*
  927. * Sync the HW-blocking state of all rfkill switches,
  928. * do notice it causes the rfkill core to schedule uevents
  929. */
  930. static void tpacpi_rfk_update_hwblock_state(bool blocked)
  931. {
  932. unsigned int i;
  933. struct tpacpi_rfk *tp_rfk;
  934. for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
  935. tp_rfk = tpacpi_rfkill_switches[i];
  936. if (tp_rfk) {
  937. if (rfkill_set_hw_state(tp_rfk->rfkill,
  938. blocked)) {
  939. /* ignore -- we track sw block */
  940. }
  941. }
  942. }
  943. }
  944. /* Call to get the WLSW state from the firmware */
  945. static int hotkey_get_wlsw(void);
  946. /* Call to query WLSW state and update all rfkill switches */
  947. static bool tpacpi_rfk_check_hwblock_state(void)
  948. {
  949. int res = hotkey_get_wlsw();
  950. int hw_blocked;
  951. /* When unknown or unsupported, we have to assume it is unblocked */
  952. if (res < 0)
  953. return false;
  954. hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
  955. tpacpi_rfk_update_hwblock_state(hw_blocked);
  956. return hw_blocked;
  957. }
  958. static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
  959. {
  960. struct tpacpi_rfk *tp_rfk = data;
  961. int res;
  962. dbg_printk(TPACPI_DBG_RFKILL,
  963. "request to change radio state to %s\n",
  964. blocked ? "blocked" : "unblocked");
  965. /* try to set radio state */
  966. res = (tp_rfk->ops->set_status)(blocked ?
  967. TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
  968. /* and update the rfkill core with whatever the FW really did */
  969. tpacpi_rfk_update_swstate(tp_rfk);
  970. return (res < 0) ? res : 0;
  971. }
  972. static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
  973. .set_block = tpacpi_rfk_hook_set_block,
  974. };
  975. static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
  976. const struct tpacpi_rfk_ops *tp_rfkops,
  977. const enum rfkill_type rfktype,
  978. const char *name,
  979. const bool set_default)
  980. {
  981. struct tpacpi_rfk *atp_rfk;
  982. int res;
  983. bool sw_state = false;
  984. bool hw_state;
  985. int sw_status;
  986. BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
  987. atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
  988. if (atp_rfk)
  989. atp_rfk->rfkill = rfkill_alloc(name,
  990. &tpacpi_pdev->dev,
  991. rfktype,
  992. &tpacpi_rfk_rfkill_ops,
  993. atp_rfk);
  994. if (!atp_rfk || !atp_rfk->rfkill) {
  995. pr_err("failed to allocate memory for rfkill class\n");
  996. kfree(atp_rfk);
  997. return -ENOMEM;
  998. }
  999. atp_rfk->id = id;
  1000. atp_rfk->ops = tp_rfkops;
  1001. sw_status = (tp_rfkops->get_status)();
  1002. if (sw_status < 0) {
  1003. pr_err("failed to read initial state for %s, error %d\n",
  1004. name, sw_status);
  1005. } else {
  1006. sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
  1007. if (set_default) {
  1008. /* try to keep the initial state, since we ask the
  1009. * firmware to preserve it across S5 in NVRAM */
  1010. rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
  1011. }
  1012. }
  1013. hw_state = tpacpi_rfk_check_hwblock_state();
  1014. rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
  1015. res = rfkill_register(atp_rfk->rfkill);
  1016. if (res < 0) {
  1017. pr_err("failed to register %s rfkill switch: %d\n", name, res);
  1018. rfkill_destroy(atp_rfk->rfkill);
  1019. kfree(atp_rfk);
  1020. return res;
  1021. }
  1022. tpacpi_rfkill_switches[id] = atp_rfk;
  1023. pr_info("rfkill switch %s: radio is %sblocked\n",
  1024. name, (sw_state || hw_state) ? "" : "un");
  1025. return 0;
  1026. }
  1027. static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
  1028. {
  1029. struct tpacpi_rfk *tp_rfk;
  1030. BUG_ON(id >= TPACPI_RFK_SW_MAX);
  1031. tp_rfk = tpacpi_rfkill_switches[id];
  1032. if (tp_rfk) {
  1033. rfkill_unregister(tp_rfk->rfkill);
  1034. rfkill_destroy(tp_rfk->rfkill);
  1035. tpacpi_rfkill_switches[id] = NULL;
  1036. kfree(tp_rfk);
  1037. }
  1038. }
  1039. static void printk_deprecated_rfkill_attribute(const char * const what)
  1040. {
  1041. printk_deprecated_attribute(what,
  1042. "Please switch to generic rfkill before year 2010");
  1043. }
  1044. /* sysfs <radio> enable ------------------------------------------------ */
  1045. static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
  1046. struct device_attribute *attr,
  1047. char *buf)
  1048. {
  1049. int status;
  1050. printk_deprecated_rfkill_attribute(attr->attr.name);
  1051. /* This is in the ABI... */
  1052. if (tpacpi_rfk_check_hwblock_state()) {
  1053. status = TPACPI_RFK_RADIO_OFF;
  1054. } else {
  1055. status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1056. if (status < 0)
  1057. return status;
  1058. }
  1059. return sysfs_emit(buf, "%d\n",
  1060. (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
  1061. }
  1062. static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
  1063. struct device_attribute *attr,
  1064. const char *buf, size_t count)
  1065. {
  1066. unsigned long t;
  1067. int res;
  1068. printk_deprecated_rfkill_attribute(attr->attr.name);
  1069. if (parse_strtoul(buf, 1, &t))
  1070. return -EINVAL;
  1071. tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
  1072. /* This is in the ABI... */
  1073. if (tpacpi_rfk_check_hwblock_state() && !!t)
  1074. return -EPERM;
  1075. res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
  1076. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
  1077. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1078. return (res < 0) ? res : count;
  1079. }
  1080. /* procfs -------------------------------------------------------------- */
  1081. static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
  1082. {
  1083. if (id >= TPACPI_RFK_SW_MAX)
  1084. seq_printf(m, "status:\t\tnot supported\n");
  1085. else {
  1086. int status;
  1087. /* This is in the ABI... */
  1088. if (tpacpi_rfk_check_hwblock_state()) {
  1089. status = TPACPI_RFK_RADIO_OFF;
  1090. } else {
  1091. status = tpacpi_rfk_update_swstate(
  1092. tpacpi_rfkill_switches[id]);
  1093. if (status < 0)
  1094. return status;
  1095. }
  1096. seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status == TPACPI_RFK_RADIO_ON));
  1097. seq_printf(m, "commands:\tenable, disable\n");
  1098. }
  1099. return 0;
  1100. }
  1101. static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
  1102. {
  1103. char *cmd;
  1104. int status = -1;
  1105. int res = 0;
  1106. if (id >= TPACPI_RFK_SW_MAX)
  1107. return -ENODEV;
  1108. while ((cmd = strsep(&buf, ","))) {
  1109. if (strlencmp(cmd, "enable") == 0)
  1110. status = TPACPI_RFK_RADIO_ON;
  1111. else if (strlencmp(cmd, "disable") == 0)
  1112. status = TPACPI_RFK_RADIO_OFF;
  1113. else
  1114. return -EINVAL;
  1115. }
  1116. if (status != -1) {
  1117. tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
  1118. str_enable_disable(status == TPACPI_RFK_RADIO_ON),
  1119. tpacpi_rfkill_names[id]);
  1120. res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
  1121. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1122. }
  1123. return res;
  1124. }
  1125. /*************************************************************************
  1126. * thinkpad-acpi driver attributes
  1127. */
  1128. /* interface_version --------------------------------------------------- */
  1129. static ssize_t interface_version_show(struct device_driver *drv, char *buf)
  1130. {
  1131. return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
  1132. }
  1133. static DRIVER_ATTR_RO(interface_version);
  1134. /* debug_level --------------------------------------------------------- */
  1135. static ssize_t debug_level_show(struct device_driver *drv, char *buf)
  1136. {
  1137. return sysfs_emit(buf, "0x%04x\n", dbg_level);
  1138. }
  1139. static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
  1140. size_t count)
  1141. {
  1142. unsigned long t;
  1143. if (parse_strtoul(buf, 0xffff, &t))
  1144. return -EINVAL;
  1145. dbg_level = t;
  1146. return count;
  1147. }
  1148. static DRIVER_ATTR_RW(debug_level);
  1149. /* version ------------------------------------------------------------- */
  1150. static ssize_t version_show(struct device_driver *drv, char *buf)
  1151. {
  1152. return sysfs_emit(buf, "%s v%s\n",
  1153. TPACPI_DESC, TPACPI_VERSION);
  1154. }
  1155. static DRIVER_ATTR_RO(version);
  1156. /* --------------------------------------------------------------------- */
  1157. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1158. /* wlsw_emulstate ------------------------------------------------------ */
  1159. static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
  1160. {
  1161. return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
  1162. }
  1163. static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
  1164. size_t count)
  1165. {
  1166. unsigned long t;
  1167. if (parse_strtoul(buf, 1, &t))
  1168. return -EINVAL;
  1169. if (tpacpi_wlsw_emulstate != !!t) {
  1170. tpacpi_wlsw_emulstate = !!t;
  1171. tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
  1172. }
  1173. return count;
  1174. }
  1175. static DRIVER_ATTR_RW(wlsw_emulstate);
  1176. /* bluetooth_emulstate ------------------------------------------------- */
  1177. static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
  1178. {
  1179. return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
  1180. }
  1181. static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
  1182. const char *buf, size_t count)
  1183. {
  1184. unsigned long t;
  1185. if (parse_strtoul(buf, 1, &t))
  1186. return -EINVAL;
  1187. tpacpi_bluetooth_emulstate = !!t;
  1188. return count;
  1189. }
  1190. static DRIVER_ATTR_RW(bluetooth_emulstate);
  1191. /* wwan_emulstate ------------------------------------------------- */
  1192. static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
  1193. {
  1194. return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
  1195. }
  1196. static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
  1197. size_t count)
  1198. {
  1199. unsigned long t;
  1200. if (parse_strtoul(buf, 1, &t))
  1201. return -EINVAL;
  1202. tpacpi_wwan_emulstate = !!t;
  1203. return count;
  1204. }
  1205. static DRIVER_ATTR_RW(wwan_emulstate);
  1206. /* uwb_emulstate ------------------------------------------------- */
  1207. static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
  1208. {
  1209. return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
  1210. }
  1211. static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
  1212. size_t count)
  1213. {
  1214. unsigned long t;
  1215. if (parse_strtoul(buf, 1, &t))
  1216. return -EINVAL;
  1217. tpacpi_uwb_emulstate = !!t;
  1218. return count;
  1219. }
  1220. static DRIVER_ATTR_RW(uwb_emulstate);
  1221. #endif
  1222. /*************************************************************************
  1223. * Firmware Data
  1224. */
  1225. /*
  1226. * Table of recommended minimum BIOS versions
  1227. *
  1228. * Reasons for listing:
  1229. * 1. Stable BIOS, listed because the unknown amount of
  1230. * bugs and bad ACPI behaviour on older versions
  1231. *
  1232. * 2. BIOS or EC fw with known bugs that trigger on Linux
  1233. *
  1234. * 3. BIOS with known reduced functionality in older versions
  1235. *
  1236. * We recommend the latest BIOS and EC version.
  1237. * We only support the latest BIOS and EC fw version as a rule.
  1238. *
  1239. * Sources: IBM ThinkPad Public Web Documents (update changelogs),
  1240. * Information from users in ThinkWiki
  1241. *
  1242. * WARNING: we use this table also to detect that the machine is
  1243. * a ThinkPad in some cases, so don't remove entries lightly.
  1244. */
  1245. #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
  1246. { .vendor = (__v), \
  1247. .bios = TPID(__id1, __id2), \
  1248. .ec = TPACPI_MATCH_ANY, \
  1249. .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
  1250. | TPVER(__bv1, __bv2) }
  1251. #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
  1252. __eid, __ev1, __ev2) \
  1253. { .vendor = (__v), \
  1254. .bios = TPID(__bid1, __bid2), \
  1255. .ec = __eid, \
  1256. .quirks = TPVER(__ev1, __ev2) << 16 \
  1257. | TPVER(__bv1, __bv2) }
  1258. #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
  1259. TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
  1260. /* Outdated IBM BIOSes often lack the EC id string */
  1261. #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1262. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1263. __bv1, __bv2, TPID(__id1, __id2), \
  1264. __ev1, __ev2), \
  1265. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1266. __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
  1267. __ev1, __ev2)
  1268. /* Outdated IBM BIOSes often lack the EC id string */
  1269. #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
  1270. __eid1, __eid2, __ev1, __ev2) \
  1271. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1272. __bv1, __bv2, TPID(__eid1, __eid2), \
  1273. __ev1, __ev2), \
  1274. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1275. __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
  1276. __ev1, __ev2)
  1277. #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
  1278. TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
  1279. #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1280. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
  1281. __bv1, __bv2, TPID(__id1, __id2), \
  1282. __ev1, __ev2)
  1283. #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
  1284. __eid1, __eid2, __ev1, __ev2) \
  1285. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
  1286. __bv1, __bv2, TPID(__eid1, __eid2), \
  1287. __ev1, __ev2)
  1288. static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
  1289. /* Numeric models ------------------ */
  1290. /* FW MODEL BIOS VERS */
  1291. TPV_QI0('I', 'M', '6', '5'), /* 570 */
  1292. TPV_QI0('I', 'U', '2', '6'), /* 570E */
  1293. TPV_QI0('I', 'B', '5', '4'), /* 600 */
  1294. TPV_QI0('I', 'H', '4', '7'), /* 600E */
  1295. TPV_QI0('I', 'N', '3', '6'), /* 600E */
  1296. TPV_QI0('I', 'T', '5', '5'), /* 600X */
  1297. TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
  1298. TPV_QI0('I', 'I', '4', '2'), /* 770X */
  1299. TPV_QI0('I', 'O', '2', '3'), /* 770Z */
  1300. /* A-series ------------------------- */
  1301. /* FW MODEL BIOS VERS EC VERS */
  1302. TPV_QI0('I', 'W', '5', '9'), /* A20m */
  1303. TPV_QI0('I', 'V', '6', '9'), /* A20p */
  1304. TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
  1305. TPV_QI0('K', 'U', '3', '6'), /* A21e */
  1306. TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
  1307. TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
  1308. TPV_QI0('1', 'B', '1', '7'), /* A22e */
  1309. TPV_QI0('1', '3', '2', '0'), /* A22m */
  1310. TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
  1311. TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
  1312. TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
  1313. /* G-series ------------------------- */
  1314. /* FW MODEL BIOS VERS */
  1315. TPV_QI0('1', 'T', 'A', '6'), /* G40 */
  1316. TPV_QI0('1', 'X', '5', '7'), /* G41 */
  1317. /* R-series, T-series --------------- */
  1318. /* FW MODEL BIOS VERS EC VERS */
  1319. TPV_QI0('1', 'C', 'F', '0'), /* R30 */
  1320. TPV_QI0('1', 'F', 'F', '1'), /* R31 */
  1321. TPV_QI0('1', 'M', '9', '7'), /* R32 */
  1322. TPV_QI0('1', 'O', '6', '1'), /* R40 */
  1323. TPV_QI0('1', 'P', '6', '5'), /* R40 */
  1324. TPV_QI0('1', 'S', '7', '0'), /* R40e */
  1325. TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
  1326. T40/p, T41/p, T42/p (1) */
  1327. TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
  1328. TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
  1329. TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
  1330. TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
  1331. TPV_QI0('I', 'Y', '6', '1'), /* T20 */
  1332. TPV_QI0('K', 'Z', '3', '4'), /* T21 */
  1333. TPV_QI0('1', '6', '3', '2'), /* T22 */
  1334. TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
  1335. TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
  1336. TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
  1337. TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
  1338. TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
  1339. TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
  1340. /* BIOS FW BIOS VERS EC FW EC VERS */
  1341. TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
  1342. TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
  1343. /* X-series ------------------------- */
  1344. /* FW MODEL BIOS VERS EC VERS */
  1345. TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
  1346. TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
  1347. TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
  1348. TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
  1349. TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
  1350. TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
  1351. TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
  1352. TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
  1353. TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
  1354. /* (0) - older versions lack DMI EC fw string and functionality */
  1355. /* (1) - older versions known to lack functionality */
  1356. };
  1357. #undef TPV_QL1
  1358. #undef TPV_QL0
  1359. #undef TPV_QI2
  1360. #undef TPV_QI1
  1361. #undef TPV_QI0
  1362. #undef TPV_Q_X
  1363. #undef TPV_Q
  1364. static void __init tpacpi_check_outdated_fw(void)
  1365. {
  1366. unsigned long fwvers;
  1367. u16 ec_version, bios_version;
  1368. fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1369. ARRAY_SIZE(tpacpi_bios_version_qtable));
  1370. if (!fwvers)
  1371. return;
  1372. bios_version = fwvers & 0xffffU;
  1373. ec_version = (fwvers >> 16) & 0xffffU;
  1374. /* note that unknown versions are set to 0x0000 and we use that */
  1375. if ((bios_version > thinkpad_id.bios_release) ||
  1376. (ec_version > thinkpad_id.ec_release &&
  1377. ec_version != TPACPI_MATCH_ANY_VERSION)) {
  1378. /*
  1379. * The changelogs would let us track down the exact
  1380. * reason, but it is just too much of a pain to track
  1381. * it. We only list BIOSes that are either really
  1382. * broken, or really stable to begin with, so it is
  1383. * best if the user upgrades the firmware anyway.
  1384. */
  1385. pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
  1386. pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
  1387. }
  1388. }
  1389. static bool __init tpacpi_is_fw_known(void)
  1390. {
  1391. return tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1392. ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
  1393. }
  1394. /****************************************************************************
  1395. ****************************************************************************
  1396. *
  1397. * Subdrivers
  1398. *
  1399. ****************************************************************************
  1400. ****************************************************************************/
  1401. /*************************************************************************
  1402. * thinkpad-acpi metadata subdriver
  1403. */
  1404. static int thinkpad_acpi_driver_read(struct seq_file *m)
  1405. {
  1406. seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
  1407. seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
  1408. return 0;
  1409. }
  1410. static struct ibm_struct thinkpad_acpi_driver_data = {
  1411. .name = "driver",
  1412. .read = thinkpad_acpi_driver_read,
  1413. };
  1414. /*************************************************************************
  1415. * Hotkey subdriver
  1416. */
  1417. /*
  1418. * ThinkPad firmware event model
  1419. *
  1420. * The ThinkPad firmware has two main event interfaces: normal ACPI
  1421. * notifications (which follow the ACPI standard), and a private event
  1422. * interface.
  1423. *
  1424. * The private event interface also issues events for the hotkeys. As
  1425. * the driver gained features, the event handling code ended up being
  1426. * built around the hotkey subdriver. This will need to be refactored
  1427. * to a more formal event API eventually.
  1428. *
  1429. * Some "hotkeys" are actually supposed to be used as event reports,
  1430. * such as "brightness has changed", "volume has changed", depending on
  1431. * the ThinkPad model and how the firmware is operating.
  1432. *
  1433. * Unlike other classes, hotkey-class events have mask/unmask control on
  1434. * non-ancient firmware. However, how it behaves changes a lot with the
  1435. * firmware model and version.
  1436. */
  1437. enum { /* hot key scan codes (derived from ACPI DSDT) */
  1438. TP_ACPI_HOTKEYSCAN_FNF1 = 0,
  1439. TP_ACPI_HOTKEYSCAN_FNF2,
  1440. TP_ACPI_HOTKEYSCAN_FNF3,
  1441. TP_ACPI_HOTKEYSCAN_FNF4,
  1442. TP_ACPI_HOTKEYSCAN_FNF5,
  1443. TP_ACPI_HOTKEYSCAN_FNF6,
  1444. TP_ACPI_HOTKEYSCAN_FNF7,
  1445. TP_ACPI_HOTKEYSCAN_FNF8,
  1446. TP_ACPI_HOTKEYSCAN_FNF9,
  1447. TP_ACPI_HOTKEYSCAN_FNF10,
  1448. TP_ACPI_HOTKEYSCAN_FNF11,
  1449. TP_ACPI_HOTKEYSCAN_FNF12,
  1450. TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
  1451. TP_ACPI_HOTKEYSCAN_FNINSERT,
  1452. TP_ACPI_HOTKEYSCAN_FNDELETE,
  1453. TP_ACPI_HOTKEYSCAN_FNHOME,
  1454. TP_ACPI_HOTKEYSCAN_FNEND,
  1455. TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1456. TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
  1457. TP_ACPI_HOTKEYSCAN_FNSPACE,
  1458. TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1459. TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1460. TP_ACPI_HOTKEYSCAN_MUTE,
  1461. TP_ACPI_HOTKEYSCAN_THINKPAD,
  1462. TP_ACPI_HOTKEYSCAN_UNK1,
  1463. TP_ACPI_HOTKEYSCAN_UNK2,
  1464. TP_ACPI_HOTKEYSCAN_UNK3,
  1465. TP_ACPI_HOTKEYSCAN_UNK4,
  1466. TP_ACPI_HOTKEYSCAN_UNK5,
  1467. TP_ACPI_HOTKEYSCAN_UNK6,
  1468. TP_ACPI_HOTKEYSCAN_UNK7,
  1469. TP_ACPI_HOTKEYSCAN_UNK8,
  1470. /* Adaptive keyboard keycodes */
  1471. TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
  1472. TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
  1473. TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
  1474. TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
  1475. TP_ACPI_HOTKEYSCAN_CLOUD,
  1476. TP_ACPI_HOTKEYSCAN_UNK9,
  1477. TP_ACPI_HOTKEYSCAN_VOICE,
  1478. TP_ACPI_HOTKEYSCAN_UNK10,
  1479. TP_ACPI_HOTKEYSCAN_GESTURES,
  1480. TP_ACPI_HOTKEYSCAN_UNK11,
  1481. TP_ACPI_HOTKEYSCAN_UNK12,
  1482. TP_ACPI_HOTKEYSCAN_UNK13,
  1483. TP_ACPI_HOTKEYSCAN_CONFIG,
  1484. TP_ACPI_HOTKEYSCAN_NEW_TAB,
  1485. TP_ACPI_HOTKEYSCAN_RELOAD,
  1486. TP_ACPI_HOTKEYSCAN_BACK,
  1487. TP_ACPI_HOTKEYSCAN_MIC_DOWN,
  1488. TP_ACPI_HOTKEYSCAN_MIC_UP,
  1489. TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
  1490. TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
  1491. TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
  1492. /* Lenovo extended keymap, starting at 0x1300 */
  1493. TP_ACPI_HOTKEYSCAN_EXTENDED_START,
  1494. /* first new observed key (star, favorites) is 0x1311 */
  1495. TP_ACPI_HOTKEYSCAN_STAR = 69,
  1496. TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
  1497. TP_ACPI_HOTKEYSCAN_CALCULATOR,
  1498. TP_ACPI_HOTKEYSCAN_BLUETOOTH,
  1499. TP_ACPI_HOTKEYSCAN_KEYBOARD,
  1500. TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
  1501. TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
  1502. TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
  1503. TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
  1504. /* Hotkey keymap size */
  1505. TPACPI_HOTKEY_MAP_LEN
  1506. };
  1507. enum { /* Keys/events available through NVRAM polling */
  1508. TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
  1509. TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
  1510. };
  1511. enum { /* Positions of some of the keys in hotkey masks */
  1512. TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
  1513. TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
  1514. TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
  1515. TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
  1516. TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
  1517. TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1518. TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
  1519. TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1520. TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1521. TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
  1522. TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
  1523. };
  1524. enum { /* NVRAM to ACPI HKEY group map */
  1525. TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
  1526. TP_ACPI_HKEY_ZOOM_MASK |
  1527. TP_ACPI_HKEY_DISPSWTCH_MASK |
  1528. TP_ACPI_HKEY_HIBERNATE_MASK,
  1529. TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
  1530. TP_ACPI_HKEY_BRGHTDWN_MASK,
  1531. TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
  1532. TP_ACPI_HKEY_VOLDWN_MASK |
  1533. TP_ACPI_HKEY_MUTE_MASK,
  1534. };
  1535. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1536. struct tp_nvram_state {
  1537. u16 thinkpad_toggle:1;
  1538. u16 zoom_toggle:1;
  1539. u16 display_toggle:1;
  1540. u16 thinklight_toggle:1;
  1541. u16 hibernate_toggle:1;
  1542. u16 displayexp_toggle:1;
  1543. u16 display_state:1;
  1544. u16 brightness_toggle:1;
  1545. u16 volume_toggle:1;
  1546. u16 mute:1;
  1547. u8 brightness_level;
  1548. u8 volume_level;
  1549. };
  1550. /* kthread for the hotkey poller */
  1551. static struct task_struct *tpacpi_hotkey_task;
  1552. /*
  1553. * Acquire mutex to write poller control variables as an
  1554. * atomic block.
  1555. *
  1556. * Increment hotkey_config_change when changing them if you
  1557. * want the kthread to forget old state.
  1558. *
  1559. * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1560. */
  1561. static struct mutex hotkey_thread_data_mutex;
  1562. static unsigned int hotkey_config_change;
  1563. /*
  1564. * hotkey poller control variables
  1565. *
  1566. * Must be atomic or readers will also need to acquire mutex
  1567. *
  1568. * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1569. * should be used only when the changes need to be taken as
  1570. * a block, OR when one needs to force the kthread to forget
  1571. * old state.
  1572. */
  1573. static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
  1574. static unsigned int hotkey_poll_freq = 10; /* Hz */
  1575. #define HOTKEY_CONFIG_CRITICAL_START \
  1576. do { \
  1577. mutex_lock(&hotkey_thread_data_mutex); \
  1578. hotkey_config_change++; \
  1579. } while (0);
  1580. #define HOTKEY_CONFIG_CRITICAL_END \
  1581. mutex_unlock(&hotkey_thread_data_mutex);
  1582. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1583. #define hotkey_source_mask 0U
  1584. #define HOTKEY_CONFIG_CRITICAL_START
  1585. #define HOTKEY_CONFIG_CRITICAL_END
  1586. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1587. static struct mutex hotkey_mutex;
  1588. static enum { /* Reasons for waking up */
  1589. TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
  1590. TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
  1591. TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
  1592. } hotkey_wakeup_reason;
  1593. static int hotkey_autosleep_ack;
  1594. static u32 hotkey_orig_mask; /* events the BIOS had enabled */
  1595. static u32 hotkey_all_mask; /* all events supported in fw */
  1596. static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
  1597. static u32 hotkey_reserved_mask; /* events better left disabled */
  1598. static u32 hotkey_driver_mask; /* events needed by the driver */
  1599. static u32 hotkey_user_mask; /* events visible to userspace */
  1600. static u32 hotkey_acpi_mask; /* events enabled in firmware */
  1601. static u16 *hotkey_keycode_map;
  1602. static void tpacpi_driver_event(const unsigned int hkey_event);
  1603. static void hotkey_driver_event(const unsigned int scancode);
  1604. static void hotkey_poll_setup(const bool may_warn);
  1605. /* HKEY.MHKG() return bits */
  1606. #define TP_HOTKEY_TABLET_MASK (1 << 3)
  1607. enum {
  1608. TP_ACPI_MULTI_MODE_INVALID = 0,
  1609. TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
  1610. TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
  1611. TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
  1612. TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
  1613. TP_ACPI_MULTI_MODE_STAND = 1 << 4,
  1614. TP_ACPI_MULTI_MODE_TENT = 1 << 5,
  1615. TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
  1616. };
  1617. enum {
  1618. /* The following modes are considered tablet mode for the purpose of
  1619. * reporting the status to userspace. i.e. in all these modes it makes
  1620. * sense to disable the laptop input devices such as touchpad and
  1621. * keyboard.
  1622. */
  1623. TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
  1624. TP_ACPI_MULTI_MODE_STAND |
  1625. TP_ACPI_MULTI_MODE_TENT |
  1626. TP_ACPI_MULTI_MODE_STAND_TENT,
  1627. };
  1628. static int hotkey_get_wlsw(void)
  1629. {
  1630. int status;
  1631. if (!tp_features.hotkey_wlsw)
  1632. return -ENODEV;
  1633. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1634. if (dbg_wlswemul)
  1635. return (tpacpi_wlsw_emulstate) ?
  1636. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1637. #endif
  1638. if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
  1639. return -EIO;
  1640. return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1641. }
  1642. static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
  1643. {
  1644. int type = (s >> 16) & 0xffff;
  1645. int value = s & 0xffff;
  1646. int mode = TP_ACPI_MULTI_MODE_INVALID;
  1647. int valid_modes = 0;
  1648. if (has_tablet_mode)
  1649. *has_tablet_mode = 0;
  1650. switch (type) {
  1651. case 1:
  1652. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1653. TP_ACPI_MULTI_MODE_TABLET |
  1654. TP_ACPI_MULTI_MODE_STAND_TENT;
  1655. break;
  1656. case 2:
  1657. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1658. TP_ACPI_MULTI_MODE_FLAT |
  1659. TP_ACPI_MULTI_MODE_TABLET |
  1660. TP_ACPI_MULTI_MODE_STAND |
  1661. TP_ACPI_MULTI_MODE_TENT;
  1662. break;
  1663. case 3:
  1664. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1665. TP_ACPI_MULTI_MODE_FLAT;
  1666. break;
  1667. case 4:
  1668. case 5:
  1669. /* In mode 4, FLAT is not specified as a valid mode. However,
  1670. * it can be seen at least on the X1 Yoga 2nd Generation.
  1671. */
  1672. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1673. TP_ACPI_MULTI_MODE_FLAT |
  1674. TP_ACPI_MULTI_MODE_TABLET |
  1675. TP_ACPI_MULTI_MODE_STAND |
  1676. TP_ACPI_MULTI_MODE_TENT;
  1677. break;
  1678. default:
  1679. pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
  1680. type, value, TPACPI_MAIL);
  1681. return 0;
  1682. }
  1683. if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
  1684. *has_tablet_mode = 1;
  1685. switch (value) {
  1686. case 1:
  1687. mode = TP_ACPI_MULTI_MODE_LAPTOP;
  1688. break;
  1689. case 2:
  1690. mode = TP_ACPI_MULTI_MODE_FLAT;
  1691. break;
  1692. case 3:
  1693. mode = TP_ACPI_MULTI_MODE_TABLET;
  1694. break;
  1695. case 4:
  1696. if (type == 1)
  1697. mode = TP_ACPI_MULTI_MODE_STAND_TENT;
  1698. else
  1699. mode = TP_ACPI_MULTI_MODE_STAND;
  1700. break;
  1701. case 5:
  1702. mode = TP_ACPI_MULTI_MODE_TENT;
  1703. break;
  1704. default:
  1705. if (type == 5 && value == 0xffff) {
  1706. pr_warn("Multi mode status is undetected, assuming laptop\n");
  1707. return 0;
  1708. }
  1709. }
  1710. if (!(mode & valid_modes)) {
  1711. pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
  1712. value, type, TPACPI_MAIL);
  1713. return 0;
  1714. }
  1715. return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
  1716. }
  1717. static int hotkey_get_tablet_mode(int *status)
  1718. {
  1719. int s;
  1720. switch (tp_features.hotkey_tablet) {
  1721. case TP_HOTKEY_TABLET_USES_MHKG:
  1722. if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
  1723. return -EIO;
  1724. *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
  1725. break;
  1726. case TP_HOTKEY_TABLET_USES_GMMS:
  1727. if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
  1728. return -EIO;
  1729. *status = hotkey_gmms_get_tablet_mode(s, NULL);
  1730. break;
  1731. default:
  1732. break;
  1733. }
  1734. return 0;
  1735. }
  1736. /*
  1737. * Reads current event mask from firmware, and updates
  1738. * hotkey_acpi_mask accordingly. Also resets any bits
  1739. * from hotkey_user_mask that are unavailable to be
  1740. * delivered (shadow requirement of the userspace ABI).
  1741. *
  1742. * Call with hotkey_mutex held
  1743. */
  1744. static int hotkey_mask_get(void)
  1745. {
  1746. if (tp_features.hotkey_mask) {
  1747. u32 m = 0;
  1748. if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
  1749. return -EIO;
  1750. hotkey_acpi_mask = m;
  1751. } else {
  1752. /* no mask support doesn't mean no event support... */
  1753. hotkey_acpi_mask = hotkey_all_mask;
  1754. }
  1755. /* sync userspace-visible mask */
  1756. hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
  1757. return 0;
  1758. }
  1759. static void hotkey_mask_warn_incomplete_mask(void)
  1760. {
  1761. /* log only what the user can fix... */
  1762. const u32 wantedmask = hotkey_driver_mask &
  1763. ~(hotkey_acpi_mask | hotkey_source_mask) &
  1764. (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
  1765. if (wantedmask)
  1766. pr_notice("required events 0x%08x not enabled!\n", wantedmask);
  1767. }
  1768. /*
  1769. * Set the firmware mask when supported
  1770. *
  1771. * Also calls hotkey_mask_get to update hotkey_acpi_mask.
  1772. *
  1773. * NOTE: does not set bits in hotkey_user_mask, but may reset them.
  1774. *
  1775. * Call with hotkey_mutex held
  1776. */
  1777. static int hotkey_mask_set(u32 mask)
  1778. {
  1779. int i;
  1780. int rc = 0;
  1781. const u32 fwmask = mask & ~hotkey_source_mask;
  1782. if (tp_features.hotkey_mask) {
  1783. for (i = 0; i < 32; i++) {
  1784. if (!acpi_evalf(hkey_handle,
  1785. NULL, "MHKM", "vdd", i + 1,
  1786. !!(mask & (1 << i)))) {
  1787. rc = -EIO;
  1788. break;
  1789. }
  1790. }
  1791. }
  1792. /*
  1793. * We *must* make an inconditional call to hotkey_mask_get to
  1794. * refresh hotkey_acpi_mask and update hotkey_user_mask
  1795. *
  1796. * Take the opportunity to also log when we cannot _enable_
  1797. * a given event.
  1798. */
  1799. if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
  1800. pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
  1801. fwmask, hotkey_acpi_mask);
  1802. }
  1803. if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
  1804. hotkey_mask_warn_incomplete_mask();
  1805. return rc;
  1806. }
  1807. /*
  1808. * Sets hotkey_user_mask and tries to set the firmware mask
  1809. *
  1810. * Call with hotkey_mutex held
  1811. */
  1812. static int hotkey_user_mask_set(const u32 mask)
  1813. {
  1814. int rc;
  1815. /* Give people a chance to notice they are doing something that
  1816. * is bound to go boom on their users sooner or later */
  1817. if (!tp_warned.hotkey_mask_ff &&
  1818. (mask == 0xffff || mask == 0xffffff ||
  1819. mask == 0xffffffff)) {
  1820. tp_warned.hotkey_mask_ff = 1;
  1821. pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
  1822. mask);
  1823. pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
  1824. }
  1825. /* Try to enable what the user asked for, plus whatever we need.
  1826. * this syncs everything but won't enable bits in hotkey_user_mask */
  1827. rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
  1828. /* Enable the available bits in hotkey_user_mask */
  1829. hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
  1830. return rc;
  1831. }
  1832. /*
  1833. * Sets the driver hotkey mask.
  1834. *
  1835. * Can be called even if the hotkey subdriver is inactive
  1836. */
  1837. static int tpacpi_hotkey_driver_mask_set(const u32 mask)
  1838. {
  1839. int rc;
  1840. /* Do the right thing if hotkey_init has not been called yet */
  1841. if (!tp_features.hotkey) {
  1842. hotkey_driver_mask = mask;
  1843. return 0;
  1844. }
  1845. mutex_lock(&hotkey_mutex);
  1846. HOTKEY_CONFIG_CRITICAL_START
  1847. hotkey_driver_mask = mask;
  1848. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1849. hotkey_source_mask |= (mask & ~hotkey_all_mask);
  1850. #endif
  1851. HOTKEY_CONFIG_CRITICAL_END
  1852. rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
  1853. ~hotkey_source_mask);
  1854. hotkey_poll_setup(true);
  1855. mutex_unlock(&hotkey_mutex);
  1856. return rc;
  1857. }
  1858. static int hotkey_status_get(int *status)
  1859. {
  1860. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  1861. return -EIO;
  1862. return 0;
  1863. }
  1864. static int hotkey_status_set(bool enable)
  1865. {
  1866. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
  1867. return -EIO;
  1868. return 0;
  1869. }
  1870. static void tpacpi_input_send_tabletsw(void)
  1871. {
  1872. int state;
  1873. if (tp_features.hotkey_tablet &&
  1874. !hotkey_get_tablet_mode(&state)) {
  1875. mutex_lock(&tpacpi_inputdev_send_mutex);
  1876. input_report_switch(tpacpi_inputdev,
  1877. SW_TABLET_MODE, !!state);
  1878. input_sync(tpacpi_inputdev);
  1879. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1880. }
  1881. }
  1882. /* Do NOT call without validating scancode first */
  1883. static void tpacpi_input_send_key(const unsigned int scancode)
  1884. {
  1885. const unsigned int keycode = hotkey_keycode_map[scancode];
  1886. if (keycode != KEY_RESERVED) {
  1887. mutex_lock(&tpacpi_inputdev_send_mutex);
  1888. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
  1889. input_report_key(tpacpi_inputdev, keycode, 1);
  1890. input_sync(tpacpi_inputdev);
  1891. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
  1892. input_report_key(tpacpi_inputdev, keycode, 0);
  1893. input_sync(tpacpi_inputdev);
  1894. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1895. }
  1896. }
  1897. /* Do NOT call without validating scancode first */
  1898. static void tpacpi_input_send_key_masked(const unsigned int scancode)
  1899. {
  1900. hotkey_driver_event(scancode);
  1901. if (hotkey_user_mask & (1 << scancode))
  1902. tpacpi_input_send_key(scancode);
  1903. }
  1904. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1905. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1906. /* Do NOT call without validating scancode first */
  1907. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1908. {
  1909. tpacpi_input_send_key_masked(scancode);
  1910. }
  1911. static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
  1912. {
  1913. u8 d;
  1914. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1915. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1916. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1917. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1918. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1919. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1920. }
  1921. if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
  1922. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1923. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1924. }
  1925. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1926. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1927. n->displayexp_toggle =
  1928. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1929. }
  1930. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1931. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1932. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1933. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1934. n->brightness_toggle =
  1935. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1936. }
  1937. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1938. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1939. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1940. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1941. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1942. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1943. }
  1944. }
  1945. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1946. do { \
  1947. if ((event_mask & (1 << __scancode)) && \
  1948. oldn->__member != newn->__member) \
  1949. tpacpi_hotkey_send_key(__scancode); \
  1950. } while (0)
  1951. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1952. do { \
  1953. if (event_mask & (1 << __scancode)) \
  1954. tpacpi_hotkey_send_key(__scancode); \
  1955. } while (0)
  1956. static void issue_volchange(const unsigned int oldvol,
  1957. const unsigned int newvol,
  1958. const u32 event_mask)
  1959. {
  1960. unsigned int i = oldvol;
  1961. while (i > newvol) {
  1962. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1963. i--;
  1964. }
  1965. while (i < newvol) {
  1966. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1967. i++;
  1968. }
  1969. }
  1970. static void issue_brightnesschange(const unsigned int oldbrt,
  1971. const unsigned int newbrt,
  1972. const u32 event_mask)
  1973. {
  1974. unsigned int i = oldbrt;
  1975. while (i > newbrt) {
  1976. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  1977. i--;
  1978. }
  1979. while (i < newbrt) {
  1980. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  1981. i++;
  1982. }
  1983. }
  1984. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  1985. struct tp_nvram_state *newn,
  1986. const u32 event_mask)
  1987. {
  1988. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  1989. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  1990. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  1991. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  1992. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  1993. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  1994. /*
  1995. * Handle volume
  1996. *
  1997. * This code is supposed to duplicate the IBM firmware behaviour:
  1998. * - Pressing MUTE issues mute hotkey message, even when already mute
  1999. * - Pressing Volume up/down issues volume up/down hotkey messages,
  2000. * even when already at maximum or minimum volume
  2001. * - The act of unmuting issues volume up/down notification,
  2002. * depending which key was used to unmute
  2003. *
  2004. * We are constrained to what the NVRAM can tell us, which is not much
  2005. * and certainly not enough if more than one volume hotkey was pressed
  2006. * since the last poll cycle.
  2007. *
  2008. * Just to make our life interesting, some newer Lenovo ThinkPads have
  2009. * bugs in the BIOS and may fail to update volume_toggle properly.
  2010. */
  2011. if (newn->mute) {
  2012. /* muted */
  2013. if (!oldn->mute ||
  2014. oldn->volume_toggle != newn->volume_toggle ||
  2015. oldn->volume_level != newn->volume_level) {
  2016. /* recently muted, or repeated mute keypress, or
  2017. * multiple presses ending in mute */
  2018. issue_volchange(oldn->volume_level, newn->volume_level,
  2019. event_mask);
  2020. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  2021. }
  2022. } else {
  2023. /* unmute */
  2024. if (oldn->mute) {
  2025. /* recently unmuted, issue 'unmute' keypress */
  2026. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  2027. }
  2028. if (oldn->volume_level != newn->volume_level) {
  2029. issue_volchange(oldn->volume_level, newn->volume_level,
  2030. event_mask);
  2031. } else if (oldn->volume_toggle != newn->volume_toggle) {
  2032. /* repeated vol up/down keypress at end of scale ? */
  2033. if (newn->volume_level == 0)
  2034. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  2035. else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
  2036. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  2037. }
  2038. }
  2039. /* handle brightness */
  2040. if (oldn->brightness_level != newn->brightness_level) {
  2041. issue_brightnesschange(oldn->brightness_level,
  2042. newn->brightness_level, event_mask);
  2043. } else if (oldn->brightness_toggle != newn->brightness_toggle) {
  2044. /* repeated key presses that didn't change state */
  2045. if (newn->brightness_level == 0)
  2046. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  2047. else if (newn->brightness_level >= bright_maxlvl
  2048. && !tp_features.bright_unkfw)
  2049. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  2050. }
  2051. #undef TPACPI_COMPARE_KEY
  2052. #undef TPACPI_MAY_SEND_KEY
  2053. }
  2054. /*
  2055. * Polling driver
  2056. *
  2057. * We track all events in hotkey_source_mask all the time, since
  2058. * most of them are edge-based. We only issue those requested by
  2059. * hotkey_user_mask or hotkey_driver_mask, though.
  2060. */
  2061. static int hotkey_kthread(void *data)
  2062. {
  2063. struct tp_nvram_state s[2] = { 0 };
  2064. u32 poll_mask, event_mask;
  2065. unsigned int si, so;
  2066. unsigned long t;
  2067. unsigned int change_detector;
  2068. unsigned int poll_freq;
  2069. bool was_frozen;
  2070. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  2071. goto exit;
  2072. set_freezable();
  2073. so = 0;
  2074. si = 1;
  2075. t = 0;
  2076. /* Initial state for compares */
  2077. mutex_lock(&hotkey_thread_data_mutex);
  2078. change_detector = hotkey_config_change;
  2079. poll_mask = hotkey_source_mask;
  2080. event_mask = hotkey_source_mask &
  2081. (hotkey_driver_mask | hotkey_user_mask);
  2082. poll_freq = hotkey_poll_freq;
  2083. mutex_unlock(&hotkey_thread_data_mutex);
  2084. hotkey_read_nvram(&s[so], poll_mask);
  2085. while (!kthread_should_stop()) {
  2086. if (t == 0) {
  2087. if (likely(poll_freq))
  2088. t = 1000/poll_freq;
  2089. else
  2090. t = 100; /* should never happen... */
  2091. }
  2092. t = msleep_interruptible(t);
  2093. if (unlikely(kthread_freezable_should_stop(&was_frozen)))
  2094. break;
  2095. if (t > 0 && !was_frozen)
  2096. continue;
  2097. mutex_lock(&hotkey_thread_data_mutex);
  2098. if (was_frozen || hotkey_config_change != change_detector) {
  2099. /* forget old state on thaw or config change */
  2100. si = so;
  2101. t = 0;
  2102. change_detector = hotkey_config_change;
  2103. }
  2104. poll_mask = hotkey_source_mask;
  2105. event_mask = hotkey_source_mask &
  2106. (hotkey_driver_mask | hotkey_user_mask);
  2107. poll_freq = hotkey_poll_freq;
  2108. mutex_unlock(&hotkey_thread_data_mutex);
  2109. if (likely(poll_mask)) {
  2110. hotkey_read_nvram(&s[si], poll_mask);
  2111. if (likely(si != so)) {
  2112. hotkey_compare_and_issue_event(&s[so], &s[si],
  2113. event_mask);
  2114. }
  2115. }
  2116. so = si;
  2117. si ^= 1;
  2118. }
  2119. exit:
  2120. return 0;
  2121. }
  2122. /* call with hotkey_mutex held */
  2123. static void hotkey_poll_stop_sync(void)
  2124. {
  2125. if (tpacpi_hotkey_task) {
  2126. kthread_stop(tpacpi_hotkey_task);
  2127. tpacpi_hotkey_task = NULL;
  2128. }
  2129. }
  2130. /* call with hotkey_mutex held */
  2131. static void hotkey_poll_setup(const bool may_warn)
  2132. {
  2133. const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
  2134. const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
  2135. if (hotkey_poll_freq > 0 &&
  2136. (poll_driver_mask ||
  2137. (poll_user_mask && tpacpi_inputdev->users > 0))) {
  2138. if (!tpacpi_hotkey_task) {
  2139. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  2140. NULL, TPACPI_NVRAM_KTHREAD_NAME);
  2141. if (IS_ERR(tpacpi_hotkey_task)) {
  2142. tpacpi_hotkey_task = NULL;
  2143. pr_err("could not create kernel thread for hotkey polling\n");
  2144. }
  2145. }
  2146. } else {
  2147. hotkey_poll_stop_sync();
  2148. if (may_warn && (poll_driver_mask || poll_user_mask) &&
  2149. hotkey_poll_freq == 0) {
  2150. pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
  2151. poll_user_mask, poll_driver_mask);
  2152. }
  2153. }
  2154. }
  2155. static void hotkey_poll_setup_safe(const bool may_warn)
  2156. {
  2157. mutex_lock(&hotkey_mutex);
  2158. hotkey_poll_setup(may_warn);
  2159. mutex_unlock(&hotkey_mutex);
  2160. }
  2161. /* call with hotkey_mutex held */
  2162. static void hotkey_poll_set_freq(unsigned int freq)
  2163. {
  2164. if (!freq)
  2165. hotkey_poll_stop_sync();
  2166. hotkey_poll_freq = freq;
  2167. }
  2168. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2169. static void hotkey_poll_setup(const bool __unused)
  2170. {
  2171. }
  2172. static void hotkey_poll_setup_safe(const bool __unused)
  2173. {
  2174. }
  2175. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2176. static int hotkey_inputdev_open(struct input_dev *dev)
  2177. {
  2178. switch (tpacpi_lifecycle) {
  2179. case TPACPI_LIFE_INIT:
  2180. case TPACPI_LIFE_RUNNING:
  2181. hotkey_poll_setup_safe(false);
  2182. return 0;
  2183. case TPACPI_LIFE_EXITING:
  2184. return -EBUSY;
  2185. }
  2186. /* Should only happen if tpacpi_lifecycle is corrupt */
  2187. BUG();
  2188. return -EBUSY;
  2189. }
  2190. static void hotkey_inputdev_close(struct input_dev *dev)
  2191. {
  2192. /* disable hotkey polling when possible */
  2193. if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
  2194. !(hotkey_source_mask & hotkey_driver_mask))
  2195. hotkey_poll_setup_safe(false);
  2196. }
  2197. /* sysfs hotkey enable ------------------------------------------------- */
  2198. static ssize_t hotkey_enable_show(struct device *dev,
  2199. struct device_attribute *attr,
  2200. char *buf)
  2201. {
  2202. int res, status;
  2203. printk_deprecated_attribute("hotkey_enable",
  2204. "Hotkey reporting is always enabled");
  2205. res = hotkey_status_get(&status);
  2206. if (res)
  2207. return res;
  2208. return sysfs_emit(buf, "%d\n", status);
  2209. }
  2210. static ssize_t hotkey_enable_store(struct device *dev,
  2211. struct device_attribute *attr,
  2212. const char *buf, size_t count)
  2213. {
  2214. unsigned long t;
  2215. printk_deprecated_attribute("hotkey_enable",
  2216. "Hotkeys can be disabled through hotkey_mask");
  2217. if (parse_strtoul(buf, 1, &t))
  2218. return -EINVAL;
  2219. if (t == 0)
  2220. return -EPERM;
  2221. return count;
  2222. }
  2223. static DEVICE_ATTR_RW(hotkey_enable);
  2224. /* sysfs hotkey mask --------------------------------------------------- */
  2225. static ssize_t hotkey_mask_show(struct device *dev,
  2226. struct device_attribute *attr,
  2227. char *buf)
  2228. {
  2229. return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
  2230. }
  2231. static ssize_t hotkey_mask_store(struct device *dev,
  2232. struct device_attribute *attr,
  2233. const char *buf, size_t count)
  2234. {
  2235. unsigned long t;
  2236. int res;
  2237. if (parse_strtoul(buf, 0xffffffffUL, &t))
  2238. return -EINVAL;
  2239. if (mutex_lock_killable(&hotkey_mutex))
  2240. return -ERESTARTSYS;
  2241. res = hotkey_user_mask_set(t);
  2242. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2243. hotkey_poll_setup(true);
  2244. #endif
  2245. mutex_unlock(&hotkey_mutex);
  2246. tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
  2247. return (res) ? res : count;
  2248. }
  2249. static DEVICE_ATTR_RW(hotkey_mask);
  2250. /* sysfs hotkey bios_enabled ------------------------------------------- */
  2251. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  2252. struct device_attribute *attr,
  2253. char *buf)
  2254. {
  2255. return sprintf(buf, "0\n");
  2256. }
  2257. static DEVICE_ATTR_RO(hotkey_bios_enabled);
  2258. /* sysfs hotkey bios_mask ---------------------------------------------- */
  2259. static ssize_t hotkey_bios_mask_show(struct device *dev,
  2260. struct device_attribute *attr,
  2261. char *buf)
  2262. {
  2263. printk_deprecated_attribute("hotkey_bios_mask",
  2264. "This attribute is useless.");
  2265. return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
  2266. }
  2267. static DEVICE_ATTR_RO(hotkey_bios_mask);
  2268. /* sysfs hotkey all_mask ----------------------------------------------- */
  2269. static ssize_t hotkey_all_mask_show(struct device *dev,
  2270. struct device_attribute *attr,
  2271. char *buf)
  2272. {
  2273. return sysfs_emit(buf, "0x%08x\n",
  2274. hotkey_all_mask | hotkey_source_mask);
  2275. }
  2276. static DEVICE_ATTR_RO(hotkey_all_mask);
  2277. /* sysfs hotkey all_mask ----------------------------------------------- */
  2278. static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
  2279. struct device_attribute *attr,
  2280. char *buf)
  2281. {
  2282. return sysfs_emit(buf, "0x%08x\n",
  2283. hotkey_adaptive_all_mask | hotkey_source_mask);
  2284. }
  2285. static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
  2286. /* sysfs hotkey recommended_mask --------------------------------------- */
  2287. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  2288. struct device_attribute *attr,
  2289. char *buf)
  2290. {
  2291. return sysfs_emit(buf, "0x%08x\n",
  2292. (hotkey_all_mask | hotkey_source_mask)
  2293. & ~hotkey_reserved_mask);
  2294. }
  2295. static DEVICE_ATTR_RO(hotkey_recommended_mask);
  2296. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2297. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  2298. static ssize_t hotkey_source_mask_show(struct device *dev,
  2299. struct device_attribute *attr,
  2300. char *buf)
  2301. {
  2302. return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
  2303. }
  2304. static ssize_t hotkey_source_mask_store(struct device *dev,
  2305. struct device_attribute *attr,
  2306. const char *buf, size_t count)
  2307. {
  2308. unsigned long t;
  2309. u32 r_ev;
  2310. int rc;
  2311. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  2312. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  2313. return -EINVAL;
  2314. if (mutex_lock_killable(&hotkey_mutex))
  2315. return -ERESTARTSYS;
  2316. HOTKEY_CONFIG_CRITICAL_START
  2317. hotkey_source_mask = t;
  2318. HOTKEY_CONFIG_CRITICAL_END
  2319. rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
  2320. ~hotkey_source_mask);
  2321. hotkey_poll_setup(true);
  2322. /* check if events needed by the driver got disabled */
  2323. r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
  2324. & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
  2325. mutex_unlock(&hotkey_mutex);
  2326. if (rc < 0)
  2327. pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
  2328. if (r_ev)
  2329. pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
  2330. r_ev);
  2331. tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
  2332. return (rc < 0) ? rc : count;
  2333. }
  2334. static DEVICE_ATTR_RW(hotkey_source_mask);
  2335. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  2336. static ssize_t hotkey_poll_freq_show(struct device *dev,
  2337. struct device_attribute *attr,
  2338. char *buf)
  2339. {
  2340. return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
  2341. }
  2342. static ssize_t hotkey_poll_freq_store(struct device *dev,
  2343. struct device_attribute *attr,
  2344. const char *buf, size_t count)
  2345. {
  2346. unsigned long t;
  2347. if (parse_strtoul(buf, 25, &t))
  2348. return -EINVAL;
  2349. if (mutex_lock_killable(&hotkey_mutex))
  2350. return -ERESTARTSYS;
  2351. hotkey_poll_set_freq(t);
  2352. hotkey_poll_setup(true);
  2353. mutex_unlock(&hotkey_mutex);
  2354. tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
  2355. return count;
  2356. }
  2357. static DEVICE_ATTR_RW(hotkey_poll_freq);
  2358. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2359. /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
  2360. static ssize_t hotkey_radio_sw_show(struct device *dev,
  2361. struct device_attribute *attr,
  2362. char *buf)
  2363. {
  2364. int res;
  2365. res = hotkey_get_wlsw();
  2366. if (res < 0)
  2367. return res;
  2368. /* Opportunistic update */
  2369. tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
  2370. return sysfs_emit(buf, "%d\n",
  2371. (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
  2372. }
  2373. static DEVICE_ATTR_RO(hotkey_radio_sw);
  2374. static void hotkey_radio_sw_notify_change(void)
  2375. {
  2376. if (tp_features.hotkey_wlsw)
  2377. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2378. "hotkey_radio_sw");
  2379. }
  2380. /* sysfs hotkey tablet mode (pollable) --------------------------------- */
  2381. static ssize_t hotkey_tablet_mode_show(struct device *dev,
  2382. struct device_attribute *attr,
  2383. char *buf)
  2384. {
  2385. int res, s;
  2386. res = hotkey_get_tablet_mode(&s);
  2387. if (res < 0)
  2388. return res;
  2389. return sysfs_emit(buf, "%d\n", !!s);
  2390. }
  2391. static DEVICE_ATTR_RO(hotkey_tablet_mode);
  2392. static void hotkey_tablet_mode_notify_change(void)
  2393. {
  2394. if (tp_features.hotkey_tablet)
  2395. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2396. "hotkey_tablet_mode");
  2397. }
  2398. /* sysfs wakeup reason (pollable) -------------------------------------- */
  2399. static ssize_t hotkey_wakeup_reason_show(struct device *dev,
  2400. struct device_attribute *attr,
  2401. char *buf)
  2402. {
  2403. return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
  2404. }
  2405. static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
  2406. static void hotkey_wakeup_reason_notify_change(void)
  2407. {
  2408. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2409. "wakeup_reason");
  2410. }
  2411. /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
  2412. static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
  2413. struct device_attribute *attr,
  2414. char *buf)
  2415. {
  2416. return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
  2417. }
  2418. static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
  2419. hotkey_wakeup_hotunplug_complete_show, NULL);
  2420. static void hotkey_wakeup_hotunplug_complete_notify_change(void)
  2421. {
  2422. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2423. "wakeup_hotunplug_complete");
  2424. }
  2425. /* sysfs adaptive kbd mode --------------------------------------------- */
  2426. static int adaptive_keyboard_get_mode(void);
  2427. static int adaptive_keyboard_set_mode(int new_mode);
  2428. enum ADAPTIVE_KEY_MODE {
  2429. HOME_MODE,
  2430. WEB_BROWSER_MODE,
  2431. WEB_CONFERENCE_MODE,
  2432. FUNCTION_MODE,
  2433. LAYFLAT_MODE
  2434. };
  2435. static ssize_t adaptive_kbd_mode_show(struct device *dev,
  2436. struct device_attribute *attr,
  2437. char *buf)
  2438. {
  2439. int current_mode;
  2440. current_mode = adaptive_keyboard_get_mode();
  2441. if (current_mode < 0)
  2442. return current_mode;
  2443. return sysfs_emit(buf, "%d\n", current_mode);
  2444. }
  2445. static ssize_t adaptive_kbd_mode_store(struct device *dev,
  2446. struct device_attribute *attr,
  2447. const char *buf, size_t count)
  2448. {
  2449. unsigned long t;
  2450. int res;
  2451. if (parse_strtoul(buf, LAYFLAT_MODE, &t))
  2452. return -EINVAL;
  2453. res = adaptive_keyboard_set_mode(t);
  2454. return (res < 0) ? res : count;
  2455. }
  2456. static DEVICE_ATTR_RW(adaptive_kbd_mode);
  2457. static struct attribute *adaptive_kbd_attributes[] = {
  2458. &dev_attr_adaptive_kbd_mode.attr,
  2459. NULL
  2460. };
  2461. static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
  2462. struct attribute *attr, int n)
  2463. {
  2464. return tp_features.has_adaptive_kbd ? attr->mode : 0;
  2465. }
  2466. static const struct attribute_group adaptive_kbd_attr_group = {
  2467. .is_visible = hadaptive_kbd_attr_is_visible,
  2468. .attrs = adaptive_kbd_attributes,
  2469. };
  2470. /* --------------------------------------------------------------------- */
  2471. static struct attribute *hotkey_attributes[] = {
  2472. &dev_attr_hotkey_enable.attr,
  2473. &dev_attr_hotkey_bios_enabled.attr,
  2474. &dev_attr_hotkey_bios_mask.attr,
  2475. &dev_attr_wakeup_reason.attr,
  2476. &dev_attr_wakeup_hotunplug_complete.attr,
  2477. &dev_attr_hotkey_mask.attr,
  2478. &dev_attr_hotkey_all_mask.attr,
  2479. &dev_attr_hotkey_adaptive_all_mask.attr,
  2480. &dev_attr_hotkey_recommended_mask.attr,
  2481. &dev_attr_hotkey_tablet_mode.attr,
  2482. &dev_attr_hotkey_radio_sw.attr,
  2483. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2484. &dev_attr_hotkey_source_mask.attr,
  2485. &dev_attr_hotkey_poll_freq.attr,
  2486. #endif
  2487. NULL
  2488. };
  2489. static umode_t hotkey_attr_is_visible(struct kobject *kobj,
  2490. struct attribute *attr, int n)
  2491. {
  2492. if (attr == &dev_attr_hotkey_tablet_mode.attr) {
  2493. if (!tp_features.hotkey_tablet)
  2494. return 0;
  2495. } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
  2496. if (!tp_features.hotkey_wlsw)
  2497. return 0;
  2498. }
  2499. return attr->mode;
  2500. }
  2501. static const struct attribute_group hotkey_attr_group = {
  2502. .is_visible = hotkey_attr_is_visible,
  2503. .attrs = hotkey_attributes,
  2504. };
  2505. /*
  2506. * Sync both the hw and sw blocking state of all switches
  2507. */
  2508. static void tpacpi_send_radiosw_update(void)
  2509. {
  2510. int wlsw;
  2511. /*
  2512. * We must sync all rfkill controllers *before* issuing any
  2513. * rfkill input events, or we will race the rfkill core input
  2514. * handler.
  2515. *
  2516. * tpacpi_inputdev_send_mutex works as a synchronization point
  2517. * for the above.
  2518. *
  2519. * We optimize to avoid numerous calls to hotkey_get_wlsw.
  2520. */
  2521. wlsw = hotkey_get_wlsw();
  2522. /* Sync hw blocking state first if it is hw-blocked */
  2523. if (wlsw == TPACPI_RFK_RADIO_OFF)
  2524. tpacpi_rfk_update_hwblock_state(true);
  2525. /* Sync hw blocking state last if it is hw-unblocked */
  2526. if (wlsw == TPACPI_RFK_RADIO_ON)
  2527. tpacpi_rfk_update_hwblock_state(false);
  2528. /* Issue rfkill input event for WLSW switch */
  2529. if (!(wlsw < 0)) {
  2530. mutex_lock(&tpacpi_inputdev_send_mutex);
  2531. input_report_switch(tpacpi_inputdev,
  2532. SW_RFKILL_ALL, (wlsw > 0));
  2533. input_sync(tpacpi_inputdev);
  2534. mutex_unlock(&tpacpi_inputdev_send_mutex);
  2535. }
  2536. /*
  2537. * this can be unconditional, as we will poll state again
  2538. * if userspace uses the notify to read data
  2539. */
  2540. hotkey_radio_sw_notify_change();
  2541. }
  2542. static void hotkey_exit(void)
  2543. {
  2544. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2545. mutex_lock(&hotkey_mutex);
  2546. hotkey_poll_stop_sync();
  2547. mutex_unlock(&hotkey_mutex);
  2548. #endif
  2549. dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
  2550. "restoring original HKEY status and mask\n");
  2551. /* yes, there is a bitwise or below, we want the
  2552. * functions to be called even if one of them fail */
  2553. if (((tp_features.hotkey_mask &&
  2554. hotkey_mask_set(hotkey_orig_mask)) |
  2555. hotkey_status_set(false)) != 0)
  2556. pr_err("failed to restore hot key mask to BIOS defaults\n");
  2557. }
  2558. static void __init hotkey_unmap(const unsigned int scancode)
  2559. {
  2560. if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
  2561. clear_bit(hotkey_keycode_map[scancode],
  2562. tpacpi_inputdev->keybit);
  2563. hotkey_keycode_map[scancode] = KEY_RESERVED;
  2564. }
  2565. }
  2566. /*
  2567. * HKEY quirks:
  2568. * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
  2569. */
  2570. #define TPACPI_HK_Q_INIMASK 0x0001
  2571. static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
  2572. TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
  2573. TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
  2574. TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
  2575. TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
  2576. TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
  2577. TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
  2578. TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
  2579. TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
  2580. TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
  2581. TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
  2582. TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
  2583. TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
  2584. TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
  2585. TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
  2586. TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
  2587. TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
  2588. TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
  2589. TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
  2590. TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
  2591. };
  2592. typedef u16 tpacpi_keymap_entry_t;
  2593. typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
  2594. static int hotkey_init_tablet_mode(void)
  2595. {
  2596. int in_tablet_mode = 0, res;
  2597. char *type = NULL;
  2598. if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
  2599. int has_tablet_mode;
  2600. in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
  2601. &has_tablet_mode);
  2602. /*
  2603. * The Yoga 11e series has 2 accelerometers described by a
  2604. * BOSC0200 ACPI node. This setup relies on a Windows service
  2605. * which calls special ACPI methods on this node to report
  2606. * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
  2607. * does not support this, so skip the hotkey on these models.
  2608. */
  2609. if (has_tablet_mode && !dual_accel_detect())
  2610. tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
  2611. type = "GMMS";
  2612. } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
  2613. /* For X41t, X60t, X61t Tablets... */
  2614. tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
  2615. in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
  2616. type = "MHKG";
  2617. }
  2618. if (!tp_features.hotkey_tablet)
  2619. return 0;
  2620. pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
  2621. type, in_tablet_mode ? "tablet" : "laptop");
  2622. return in_tablet_mode;
  2623. }
  2624. static int __init hotkey_init(struct ibm_init_struct *iibm)
  2625. {
  2626. /* Requirements for changing the default keymaps:
  2627. *
  2628. * 1. Many of the keys are mapped to KEY_RESERVED for very
  2629. * good reasons. Do not change them unless you have deep
  2630. * knowledge on the IBM and Lenovo ThinkPad firmware for
  2631. * the various ThinkPad models. The driver behaves
  2632. * differently for KEY_RESERVED: such keys have their
  2633. * hot key mask *unset* in mask_recommended, and also
  2634. * in the initial hot key mask programmed into the
  2635. * firmware at driver load time, which means the firm-
  2636. * ware may react very differently if you change them to
  2637. * something else;
  2638. *
  2639. * 2. You must be subscribed to the linux-thinkpad and
  2640. * ibm-acpi-devel mailing lists, and you should read the
  2641. * list archives since 2007 if you want to change the
  2642. * keymaps. This requirement exists so that you will
  2643. * know the past history of problems with the thinkpad-
  2644. * acpi driver keymaps, and also that you will be
  2645. * listening to any bug reports;
  2646. *
  2647. * 3. Do not send thinkpad-acpi specific patches directly to
  2648. * for merging, *ever*. Send them to the linux-acpi
  2649. * mailinglist for comments. Merging is to be done only
  2650. * through acpi-test and the ACPI maintainer.
  2651. *
  2652. * If the above is too much to ask, don't change the keymap.
  2653. * Ask the thinkpad-acpi maintainer to do it, instead.
  2654. */
  2655. enum keymap_index {
  2656. TPACPI_KEYMAP_IBM_GENERIC = 0,
  2657. TPACPI_KEYMAP_LENOVO_GENERIC,
  2658. };
  2659. static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
  2660. /* Generic keymap for IBM ThinkPads */
  2661. [TPACPI_KEYMAP_IBM_GENERIC] = {
  2662. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  2663. KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
  2664. KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  2665. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  2666. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  2667. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  2668. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  2669. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  2670. /* brightness: firmware always reacts to them */
  2671. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  2672. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  2673. /* Thinklight: firmware always react to it */
  2674. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  2675. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  2676. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  2677. /* Volume: firmware always react to it and reprograms
  2678. * the built-in *extra* mixer. Never map it to control
  2679. * another mixer by default. */
  2680. KEY_RESERVED, /* 0x14: VOLUME UP */
  2681. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  2682. KEY_RESERVED, /* 0x16: MUTE */
  2683. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  2684. /* (assignments unknown, please report if found) */
  2685. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2686. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2687. /* No assignments, only used for Adaptive keyboards. */
  2688. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2689. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2690. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2691. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2692. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2693. /* No assignment, used for newer Lenovo models */
  2694. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2695. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2696. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2697. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2698. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2699. KEY_UNKNOWN, KEY_UNKNOWN
  2700. },
  2701. /* Generic keymap for Lenovo ThinkPads */
  2702. [TPACPI_KEYMAP_LENOVO_GENERIC] = {
  2703. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  2704. KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
  2705. KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
  2706. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  2707. /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
  2708. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  2709. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  2710. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  2711. /* These should be enabled --only-- when ACPI video
  2712. * is disabled (i.e. in "vendor" mode), and are handled
  2713. * in a special way by the init code */
  2714. KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
  2715. KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
  2716. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  2717. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  2718. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  2719. /* Volume: z60/z61, T60 (BIOS version?): firmware always
  2720. * react to it and reprograms the built-in *extra* mixer.
  2721. * Never map it to control another mixer by default.
  2722. *
  2723. * T60?, T61, R60?, R61: firmware and EC tries to send
  2724. * these over the regular keyboard, so these are no-ops,
  2725. * but there are still weird bugs re. MUTE, so do not
  2726. * change unless you get test reports from all Lenovo
  2727. * models. May cause the BIOS to interfere with the
  2728. * HDA mixer.
  2729. */
  2730. KEY_RESERVED, /* 0x14: VOLUME UP */
  2731. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  2732. KEY_RESERVED, /* 0x16: MUTE */
  2733. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  2734. /* (assignments unknown, please report if found) */
  2735. KEY_UNKNOWN, KEY_UNKNOWN,
  2736. /*
  2737. * The mic mute button only sends 0x1a. It does not
  2738. * automatically mute the mic or change the mute light.
  2739. */
  2740. KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
  2741. /* (assignments unknown, please report if found) */
  2742. KEY_UNKNOWN,
  2743. /* Extra keys in use since the X240 / T440 / T540 */
  2744. KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
  2745. /*
  2746. * These are the adaptive keyboard keycodes for Carbon X1 2014.
  2747. * The first item in this list is the Mute button which is
  2748. * emitted with 0x103 through
  2749. * adaptive_keyboard_hotkey_notify_hotkey() when the sound
  2750. * symbol is held.
  2751. * We'll need to offset those by 0x20.
  2752. */
  2753. KEY_RESERVED, /* Mute held, 0x103 */
  2754. KEY_BRIGHTNESS_MIN, /* Backlight off */
  2755. KEY_RESERVED, /* Clipping tool */
  2756. KEY_RESERVED, /* Cloud */
  2757. KEY_RESERVED,
  2758. KEY_VOICECOMMAND, /* Voice */
  2759. KEY_RESERVED,
  2760. KEY_RESERVED, /* Gestures */
  2761. KEY_RESERVED,
  2762. KEY_RESERVED,
  2763. KEY_RESERVED,
  2764. KEY_CONFIG, /* Settings */
  2765. KEY_RESERVED, /* New tab */
  2766. KEY_REFRESH, /* Reload */
  2767. KEY_BACK, /* Back */
  2768. KEY_RESERVED, /* Microphone down */
  2769. KEY_RESERVED, /* Microphone up */
  2770. KEY_RESERVED, /* Microphone cancellation */
  2771. KEY_RESERVED, /* Camera mode */
  2772. KEY_RESERVED, /* Rotate display, 0x116 */
  2773. /*
  2774. * These are found in 2017 models (e.g. T470s, X270).
  2775. * The lowest known value is 0x311, which according to
  2776. * the manual should launch a user defined favorite
  2777. * application.
  2778. *
  2779. * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
  2780. * corresponding to 0x34.
  2781. */
  2782. /* (assignments unknown, please report if found) */
  2783. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2784. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2785. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2786. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  2787. KEY_UNKNOWN,
  2788. KEY_BOOKMARKS, /* Favorite app, 0x311 */
  2789. KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
  2790. KEY_CALC, /* Calculator (above numpad, P52) */
  2791. KEY_BLUETOOTH, /* Bluetooth */
  2792. KEY_KEYBOARD, /* Keyboard, 0x315 */
  2793. KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
  2794. KEY_NOTIFICATION_CENTER, /* Notification Center */
  2795. KEY_PICKUP_PHONE, /* Answer incoming call */
  2796. KEY_HANGUP_PHONE, /* Decline incoming call */
  2797. },
  2798. };
  2799. static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
  2800. /* Generic maps (fallback) */
  2801. {
  2802. .vendor = PCI_VENDOR_ID_IBM,
  2803. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  2804. .quirks = TPACPI_KEYMAP_IBM_GENERIC,
  2805. },
  2806. {
  2807. .vendor = PCI_VENDOR_ID_LENOVO,
  2808. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  2809. .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
  2810. },
  2811. };
  2812. #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
  2813. #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
  2814. int res, i;
  2815. int status;
  2816. int hkeyv;
  2817. bool radiosw_state = false;
  2818. bool tabletsw_state = false;
  2819. unsigned long quirks;
  2820. unsigned long keymap_id;
  2821. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2822. "initializing hotkey subdriver\n");
  2823. BUG_ON(!tpacpi_inputdev);
  2824. BUG_ON(tpacpi_inputdev->open != NULL ||
  2825. tpacpi_inputdev->close != NULL);
  2826. TPACPI_ACPIHANDLE_INIT(hkey);
  2827. mutex_init(&hotkey_mutex);
  2828. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2829. mutex_init(&hotkey_thread_data_mutex);
  2830. #endif
  2831. /* hotkey not supported on 570 */
  2832. tp_features.hotkey = hkey_handle != NULL;
  2833. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2834. "hotkeys are %s\n",
  2835. str_supported(tp_features.hotkey));
  2836. if (!tp_features.hotkey)
  2837. return -ENODEV;
  2838. quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
  2839. ARRAY_SIZE(tpacpi_hotkey_qtable));
  2840. tpacpi_disable_brightness_delay();
  2841. /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
  2842. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  2843. for HKEY interface version 0x100 */
  2844. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  2845. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2846. "firmware HKEY interface version: 0x%x\n",
  2847. hkeyv);
  2848. switch (hkeyv >> 8) {
  2849. case 1:
  2850. /*
  2851. * MHKV 0x100 in A31, R40, R40e,
  2852. * T4x, X31, and later
  2853. */
  2854. /* Paranoia check AND init hotkey_all_mask */
  2855. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  2856. "MHKA", "qd")) {
  2857. pr_err("missing MHKA handler, please report this to %s\n",
  2858. TPACPI_MAIL);
  2859. /* Fallback: pre-init for FN+F3,F4,F12 */
  2860. hotkey_all_mask = 0x080cU;
  2861. } else {
  2862. tp_features.hotkey_mask = 1;
  2863. }
  2864. break;
  2865. case 2:
  2866. /*
  2867. * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
  2868. */
  2869. /* Paranoia check AND init hotkey_all_mask */
  2870. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  2871. "MHKA", "dd", 1)) {
  2872. pr_err("missing MHKA handler, please report this to %s\n",
  2873. TPACPI_MAIL);
  2874. /* Fallback: pre-init for FN+F3,F4,F12 */
  2875. hotkey_all_mask = 0x080cU;
  2876. } else {
  2877. tp_features.hotkey_mask = 1;
  2878. }
  2879. /*
  2880. * Check if we have an adaptive keyboard, like on the
  2881. * Lenovo Carbon X1 2014 (2nd Gen).
  2882. */
  2883. if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
  2884. "MHKA", "dd", 2)) {
  2885. if (hotkey_adaptive_all_mask != 0)
  2886. tp_features.has_adaptive_kbd = true;
  2887. } else {
  2888. tp_features.has_adaptive_kbd = false;
  2889. hotkey_adaptive_all_mask = 0x0U;
  2890. }
  2891. break;
  2892. default:
  2893. pr_err("unknown version of the HKEY interface: 0x%x\n",
  2894. hkeyv);
  2895. pr_err("please report this to %s\n", TPACPI_MAIL);
  2896. break;
  2897. }
  2898. }
  2899. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2900. "hotkey masks are %s\n",
  2901. str_supported(tp_features.hotkey_mask));
  2902. /* Init hotkey_all_mask if not initialized yet */
  2903. if (!tp_features.hotkey_mask && !hotkey_all_mask &&
  2904. (quirks & TPACPI_HK_Q_INIMASK))
  2905. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  2906. /* Init hotkey_acpi_mask and hotkey_orig_mask */
  2907. if (tp_features.hotkey_mask) {
  2908. /* hotkey_source_mask *must* be zero for
  2909. * the first hotkey_mask_get to return hotkey_orig_mask */
  2910. res = hotkey_mask_get();
  2911. if (res)
  2912. return res;
  2913. hotkey_orig_mask = hotkey_acpi_mask;
  2914. } else {
  2915. hotkey_orig_mask = hotkey_all_mask;
  2916. hotkey_acpi_mask = hotkey_all_mask;
  2917. }
  2918. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  2919. if (dbg_wlswemul) {
  2920. tp_features.hotkey_wlsw = 1;
  2921. radiosw_state = !!tpacpi_wlsw_emulstate;
  2922. pr_info("radio switch emulation enabled\n");
  2923. } else
  2924. #endif
  2925. /* Not all thinkpads have a hardware radio switch */
  2926. if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  2927. tp_features.hotkey_wlsw = 1;
  2928. radiosw_state = !!status;
  2929. pr_info("radio switch found; radios are %s\n", str_enabled_disabled(status & BIT(0)));
  2930. }
  2931. tabletsw_state = hotkey_init_tablet_mode();
  2932. /* Set up key map */
  2933. keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
  2934. ARRAY_SIZE(tpacpi_keymap_qtable));
  2935. BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
  2936. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2937. "using keymap number %lu\n", keymap_id);
  2938. hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
  2939. TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
  2940. if (!hotkey_keycode_map) {
  2941. pr_err("failed to allocate memory for key map\n");
  2942. return -ENOMEM;
  2943. }
  2944. input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
  2945. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  2946. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  2947. tpacpi_inputdev->keycode = hotkey_keycode_map;
  2948. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  2949. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  2950. input_set_capability(tpacpi_inputdev, EV_KEY,
  2951. hotkey_keycode_map[i]);
  2952. } else {
  2953. if (i < sizeof(hotkey_reserved_mask)*8)
  2954. hotkey_reserved_mask |= 1 << i;
  2955. }
  2956. }
  2957. if (tp_features.hotkey_wlsw) {
  2958. input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
  2959. input_report_switch(tpacpi_inputdev,
  2960. SW_RFKILL_ALL, radiosw_state);
  2961. }
  2962. if (tp_features.hotkey_tablet) {
  2963. input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
  2964. input_report_switch(tpacpi_inputdev,
  2965. SW_TABLET_MODE, tabletsw_state);
  2966. }
  2967. /* Do not issue duplicate brightness change events to
  2968. * userspace. tpacpi_detect_brightness_capabilities() must have
  2969. * been called before this point */
  2970. if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
  2971. pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
  2972. pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
  2973. /* Disable brightness up/down on Lenovo thinkpads when
  2974. * ACPI is handling them, otherwise it is plain impossible
  2975. * for userspace to do something even remotely sane */
  2976. hotkey_reserved_mask |=
  2977. (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
  2978. | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
  2979. hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
  2980. hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
  2981. }
  2982. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2983. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  2984. & ~hotkey_all_mask
  2985. & ~hotkey_reserved_mask;
  2986. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2987. "hotkey source mask 0x%08x, polling freq %u\n",
  2988. hotkey_source_mask, hotkey_poll_freq);
  2989. #endif
  2990. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2991. "enabling firmware HKEY event interface...\n");
  2992. res = hotkey_status_set(true);
  2993. if (res) {
  2994. hotkey_exit();
  2995. return res;
  2996. }
  2997. res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
  2998. | hotkey_driver_mask)
  2999. & ~hotkey_source_mask);
  3000. if (res < 0 && res != -ENXIO) {
  3001. hotkey_exit();
  3002. return res;
  3003. }
  3004. hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
  3005. & ~hotkey_reserved_mask;
  3006. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  3007. "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
  3008. hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
  3009. tpacpi_inputdev->open = &hotkey_inputdev_open;
  3010. tpacpi_inputdev->close = &hotkey_inputdev_close;
  3011. hotkey_poll_setup_safe(true);
  3012. return 0;
  3013. }
  3014. /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
  3015. * mode, Web conference mode, Function mode and Lay-flat mode.
  3016. * We support Home mode and Function mode currently.
  3017. *
  3018. * Will consider support rest of modes in future.
  3019. *
  3020. */
  3021. static const int adaptive_keyboard_modes[] = {
  3022. HOME_MODE,
  3023. /* WEB_BROWSER_MODE = 2,
  3024. WEB_CONFERENCE_MODE = 3, */
  3025. FUNCTION_MODE
  3026. };
  3027. #define DFR_CHANGE_ROW 0x101
  3028. #define DFR_SHOW_QUICKVIEW_ROW 0x102
  3029. #define FIRST_ADAPTIVE_KEY 0x103
  3030. /* press Fn key a while second, it will switch to Function Mode. Then
  3031. * release Fn key, previous mode be restored.
  3032. */
  3033. static bool adaptive_keyboard_mode_is_saved;
  3034. static int adaptive_keyboard_prev_mode;
  3035. static int adaptive_keyboard_get_mode(void)
  3036. {
  3037. int mode = 0;
  3038. if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
  3039. pr_err("Cannot read adaptive keyboard mode\n");
  3040. return -EIO;
  3041. }
  3042. return mode;
  3043. }
  3044. static int adaptive_keyboard_set_mode(int new_mode)
  3045. {
  3046. if (new_mode < 0 ||
  3047. new_mode > LAYFLAT_MODE)
  3048. return -EINVAL;
  3049. if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
  3050. pr_err("Cannot set adaptive keyboard mode\n");
  3051. return -EIO;
  3052. }
  3053. return 0;
  3054. }
  3055. static int adaptive_keyboard_get_next_mode(int mode)
  3056. {
  3057. size_t i;
  3058. size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
  3059. for (i = 0; i <= max_mode; i++) {
  3060. if (adaptive_keyboard_modes[i] == mode)
  3061. break;
  3062. }
  3063. if (i >= max_mode)
  3064. i = 0;
  3065. else
  3066. i++;
  3067. return adaptive_keyboard_modes[i];
  3068. }
  3069. static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
  3070. {
  3071. int current_mode = 0;
  3072. int new_mode = 0;
  3073. int keycode;
  3074. switch (scancode) {
  3075. case DFR_CHANGE_ROW:
  3076. if (adaptive_keyboard_mode_is_saved) {
  3077. new_mode = adaptive_keyboard_prev_mode;
  3078. adaptive_keyboard_mode_is_saved = false;
  3079. } else {
  3080. current_mode = adaptive_keyboard_get_mode();
  3081. if (current_mode < 0)
  3082. return false;
  3083. new_mode = adaptive_keyboard_get_next_mode(
  3084. current_mode);
  3085. }
  3086. if (adaptive_keyboard_set_mode(new_mode) < 0)
  3087. return false;
  3088. return true;
  3089. case DFR_SHOW_QUICKVIEW_ROW:
  3090. current_mode = adaptive_keyboard_get_mode();
  3091. if (current_mode < 0)
  3092. return false;
  3093. adaptive_keyboard_prev_mode = current_mode;
  3094. adaptive_keyboard_mode_is_saved = true;
  3095. if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
  3096. return false;
  3097. return true;
  3098. default:
  3099. if (scancode < FIRST_ADAPTIVE_KEY ||
  3100. scancode >= FIRST_ADAPTIVE_KEY +
  3101. TP_ACPI_HOTKEYSCAN_EXTENDED_START -
  3102. TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
  3103. pr_info("Unhandled adaptive keyboard key: 0x%x\n",
  3104. scancode);
  3105. return false;
  3106. }
  3107. keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
  3108. TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
  3109. if (keycode != KEY_RESERVED) {
  3110. mutex_lock(&tpacpi_inputdev_send_mutex);
  3111. input_report_key(tpacpi_inputdev, keycode, 1);
  3112. input_sync(tpacpi_inputdev);
  3113. input_report_key(tpacpi_inputdev, keycode, 0);
  3114. input_sync(tpacpi_inputdev);
  3115. mutex_unlock(&tpacpi_inputdev_send_mutex);
  3116. }
  3117. return true;
  3118. }
  3119. }
  3120. static bool hotkey_notify_extended_hotkey(const u32 hkey)
  3121. {
  3122. unsigned int scancode;
  3123. switch (hkey) {
  3124. case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
  3125. case TP_HKEY_EV_AMT_TOGGLE:
  3126. tpacpi_driver_event(hkey);
  3127. return true;
  3128. }
  3129. /* Extended keycodes start at 0x300 and our offset into the map
  3130. * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
  3131. * will be positive, but might not be in the correct range.
  3132. */
  3133. scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
  3134. if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
  3135. scancode < TPACPI_HOTKEY_MAP_LEN) {
  3136. tpacpi_input_send_key(scancode);
  3137. return true;
  3138. }
  3139. return false;
  3140. }
  3141. static bool hotkey_notify_hotkey(const u32 hkey,
  3142. bool *send_acpi_ev,
  3143. bool *ignore_acpi_ev)
  3144. {
  3145. /* 0x1000-0x1FFF: key presses */
  3146. unsigned int scancode = hkey & 0xfff;
  3147. *send_acpi_ev = true;
  3148. *ignore_acpi_ev = false;
  3149. /*
  3150. * Original events are in the 0x10XX range, the adaptive keyboard
  3151. * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
  3152. * models, additional keys are emitted through 0x13XX.
  3153. */
  3154. switch ((hkey >> 8) & 0xf) {
  3155. case 0:
  3156. if (scancode > 0 &&
  3157. scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
  3158. /* HKEY event 0x1001 is scancode 0x00 */
  3159. scancode--;
  3160. if (!(hotkey_source_mask & (1 << scancode))) {
  3161. tpacpi_input_send_key_masked(scancode);
  3162. *send_acpi_ev = false;
  3163. } else {
  3164. *ignore_acpi_ev = true;
  3165. }
  3166. return true;
  3167. }
  3168. break;
  3169. case 1:
  3170. return adaptive_keyboard_hotkey_notify_hotkey(scancode);
  3171. case 3:
  3172. return hotkey_notify_extended_hotkey(hkey);
  3173. }
  3174. return false;
  3175. }
  3176. static bool hotkey_notify_wakeup(const u32 hkey,
  3177. bool *send_acpi_ev,
  3178. bool *ignore_acpi_ev)
  3179. {
  3180. /* 0x2000-0x2FFF: Wakeup reason */
  3181. *send_acpi_ev = true;
  3182. *ignore_acpi_ev = false;
  3183. switch (hkey) {
  3184. case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
  3185. case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
  3186. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  3187. *ignore_acpi_ev = true;
  3188. break;
  3189. case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
  3190. case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
  3191. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  3192. *ignore_acpi_ev = true;
  3193. break;
  3194. case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
  3195. case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
  3196. pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
  3197. /* how to auto-heal: */
  3198. /* 2313: woke up from S3, go to S4/S5 */
  3199. /* 2413: woke up from S4, go to S5 */
  3200. break;
  3201. default:
  3202. return false;
  3203. }
  3204. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  3205. pr_info("woke up due to a hot-unplug request...\n");
  3206. hotkey_wakeup_reason_notify_change();
  3207. }
  3208. return true;
  3209. }
  3210. static bool hotkey_notify_dockevent(const u32 hkey,
  3211. bool *send_acpi_ev,
  3212. bool *ignore_acpi_ev)
  3213. {
  3214. /* 0x4000-0x4FFF: dock-related events */
  3215. *send_acpi_ev = true;
  3216. *ignore_acpi_ev = false;
  3217. switch (hkey) {
  3218. case TP_HKEY_EV_UNDOCK_ACK:
  3219. /* ACPI undock operation completed after wakeup */
  3220. hotkey_autosleep_ack = 1;
  3221. pr_info("undocked\n");
  3222. hotkey_wakeup_hotunplug_complete_notify_change();
  3223. return true;
  3224. case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
  3225. pr_info("docked into hotplug port replicator\n");
  3226. return true;
  3227. case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
  3228. pr_info("undocked from hotplug port replicator\n");
  3229. return true;
  3230. /*
  3231. * Deliberately ignore attaching and detaching the keybord cover to avoid
  3232. * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
  3233. * to userspace.
  3234. *
  3235. * Please refer to the following thread for more information and a preliminary
  3236. * implementation using the GTOP ("Get Tablet OPtions") interface that could be
  3237. * extended to other attachment options of the ThinkPad X1 Tablet series, such as
  3238. * the Pico cartridge dock module:
  3239. * https://lore.kernel.org/platform-driver-x86/[email protected]/
  3240. */
  3241. case TP_HKEY_EV_KBD_COVER_ATTACH:
  3242. case TP_HKEY_EV_KBD_COVER_DETACH:
  3243. *send_acpi_ev = false;
  3244. *ignore_acpi_ev = true;
  3245. return true;
  3246. default:
  3247. return false;
  3248. }
  3249. }
  3250. static bool hotkey_notify_usrevent(const u32 hkey,
  3251. bool *send_acpi_ev,
  3252. bool *ignore_acpi_ev)
  3253. {
  3254. /* 0x5000-0x5FFF: human interface helpers */
  3255. *send_acpi_ev = true;
  3256. *ignore_acpi_ev = false;
  3257. switch (hkey) {
  3258. case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
  3259. case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
  3260. return true;
  3261. case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
  3262. case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
  3263. tpacpi_input_send_tabletsw();
  3264. hotkey_tablet_mode_notify_change();
  3265. *send_acpi_ev = false;
  3266. return true;
  3267. case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
  3268. case TP_HKEY_EV_LID_OPEN: /* Lid opened */
  3269. case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
  3270. /* do not propagate these events */
  3271. *ignore_acpi_ev = true;
  3272. return true;
  3273. default:
  3274. return false;
  3275. }
  3276. }
  3277. static void thermal_dump_all_sensors(void);
  3278. static void palmsensor_refresh(void);
  3279. static bool hotkey_notify_6xxx(const u32 hkey,
  3280. bool *send_acpi_ev,
  3281. bool *ignore_acpi_ev)
  3282. {
  3283. /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
  3284. *send_acpi_ev = true;
  3285. *ignore_acpi_ev = false;
  3286. switch (hkey) {
  3287. case TP_HKEY_EV_THM_TABLE_CHANGED:
  3288. pr_debug("EC reports: Thermal Table has changed\n");
  3289. /* recommended action: do nothing, we don't have
  3290. * Lenovo ATM information */
  3291. return true;
  3292. case TP_HKEY_EV_THM_CSM_COMPLETED:
  3293. pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
  3294. /* Thermal event - pass on to event handler */
  3295. tpacpi_driver_event(hkey);
  3296. return true;
  3297. case TP_HKEY_EV_THM_TRANSFM_CHANGED:
  3298. pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
  3299. /* recommended action: do nothing, we don't have
  3300. * Lenovo ATM information */
  3301. return true;
  3302. case TP_HKEY_EV_ALARM_BAT_HOT:
  3303. pr_crit("THERMAL ALARM: battery is too hot!\n");
  3304. /* recommended action: warn user through gui */
  3305. break;
  3306. case TP_HKEY_EV_ALARM_BAT_XHOT:
  3307. pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
  3308. /* recommended action: immediate sleep/hibernate */
  3309. break;
  3310. case TP_HKEY_EV_ALARM_SENSOR_HOT:
  3311. pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
  3312. /* recommended action: warn user through gui, that */
  3313. /* some internal component is too hot */
  3314. break;
  3315. case TP_HKEY_EV_ALARM_SENSOR_XHOT:
  3316. pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
  3317. /* recommended action: immediate sleep/hibernate */
  3318. break;
  3319. case TP_HKEY_EV_AC_CHANGED:
  3320. /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
  3321. * AC status changed; can be triggered by plugging or
  3322. * unplugging AC adapter, docking or undocking. */
  3323. fallthrough;
  3324. case TP_HKEY_EV_KEY_NUMLOCK:
  3325. case TP_HKEY_EV_KEY_FN:
  3326. /* key press events, we just ignore them as long as the EC
  3327. * is still reporting them in the normal keyboard stream */
  3328. *send_acpi_ev = false;
  3329. *ignore_acpi_ev = true;
  3330. return true;
  3331. case TP_HKEY_EV_KEY_FN_ESC:
  3332. /* Get the media key status to force the status LED to update */
  3333. acpi_evalf(hkey_handle, NULL, "GMKS", "v");
  3334. *send_acpi_ev = false;
  3335. *ignore_acpi_ev = true;
  3336. return true;
  3337. case TP_HKEY_EV_TABLET_CHANGED:
  3338. tpacpi_input_send_tabletsw();
  3339. hotkey_tablet_mode_notify_change();
  3340. *send_acpi_ev = false;
  3341. return true;
  3342. case TP_HKEY_EV_PALM_DETECTED:
  3343. case TP_HKEY_EV_PALM_UNDETECTED:
  3344. /* palm detected - pass on to event handler */
  3345. palmsensor_refresh();
  3346. return true;
  3347. default:
  3348. /* report simply as unknown, no sensor dump */
  3349. return false;
  3350. }
  3351. thermal_dump_all_sensors();
  3352. return true;
  3353. }
  3354. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  3355. {
  3356. u32 hkey;
  3357. bool send_acpi_ev;
  3358. bool ignore_acpi_ev;
  3359. bool known_ev;
  3360. if (event != 0x80) {
  3361. pr_err("unknown HKEY notification event %d\n", event);
  3362. /* forward it to userspace, maybe it knows how to handle it */
  3363. acpi_bus_generate_netlink_event(
  3364. ibm->acpi->device->pnp.device_class,
  3365. dev_name(&ibm->acpi->device->dev),
  3366. event, 0);
  3367. return;
  3368. }
  3369. while (1) {
  3370. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  3371. pr_err("failed to retrieve HKEY event\n");
  3372. return;
  3373. }
  3374. if (hkey == 0) {
  3375. /* queue empty */
  3376. return;
  3377. }
  3378. send_acpi_ev = true;
  3379. ignore_acpi_ev = false;
  3380. switch (hkey >> 12) {
  3381. case 1:
  3382. /* 0x1000-0x1FFF: key presses */
  3383. known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
  3384. &ignore_acpi_ev);
  3385. break;
  3386. case 2:
  3387. /* 0x2000-0x2FFF: Wakeup reason */
  3388. known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
  3389. &ignore_acpi_ev);
  3390. break;
  3391. case 3:
  3392. /* 0x3000-0x3FFF: bay-related wakeups */
  3393. switch (hkey) {
  3394. case TP_HKEY_EV_BAYEJ_ACK:
  3395. hotkey_autosleep_ack = 1;
  3396. pr_info("bay ejected\n");
  3397. hotkey_wakeup_hotunplug_complete_notify_change();
  3398. known_ev = true;
  3399. break;
  3400. case TP_HKEY_EV_OPTDRV_EJ:
  3401. /* FIXME: kick libata if SATA link offline */
  3402. known_ev = true;
  3403. break;
  3404. default:
  3405. known_ev = false;
  3406. }
  3407. break;
  3408. case 4:
  3409. /* 0x4000-0x4FFF: dock-related events */
  3410. known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
  3411. &ignore_acpi_ev);
  3412. break;
  3413. case 5:
  3414. /* 0x5000-0x5FFF: human interface helpers */
  3415. known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
  3416. &ignore_acpi_ev);
  3417. break;
  3418. case 6:
  3419. /* 0x6000-0x6FFF: thermal alarms/notices and
  3420. * keyboard events */
  3421. known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
  3422. &ignore_acpi_ev);
  3423. break;
  3424. case 7:
  3425. /* 0x7000-0x7FFF: misc */
  3426. if (tp_features.hotkey_wlsw &&
  3427. hkey == TP_HKEY_EV_RFKILL_CHANGED) {
  3428. tpacpi_send_radiosw_update();
  3429. send_acpi_ev = 0;
  3430. known_ev = true;
  3431. break;
  3432. }
  3433. fallthrough; /* to default */
  3434. default:
  3435. known_ev = false;
  3436. }
  3437. if (!known_ev) {
  3438. pr_notice("unhandled HKEY event 0x%04x\n", hkey);
  3439. pr_notice("please report the conditions when this event happened to %s\n",
  3440. TPACPI_MAIL);
  3441. }
  3442. /* netlink events */
  3443. if (!ignore_acpi_ev && send_acpi_ev) {
  3444. acpi_bus_generate_netlink_event(
  3445. ibm->acpi->device->pnp.device_class,
  3446. dev_name(&ibm->acpi->device->dev),
  3447. event, hkey);
  3448. }
  3449. }
  3450. }
  3451. static void hotkey_suspend(void)
  3452. {
  3453. /* Do these on suspend, we get the events on early resume! */
  3454. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  3455. hotkey_autosleep_ack = 0;
  3456. /* save previous mode of adaptive keyboard of X1 Carbon */
  3457. if (tp_features.has_adaptive_kbd) {
  3458. if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
  3459. "GTRW", "dd", 0)) {
  3460. pr_err("Cannot read adaptive keyboard mode.\n");
  3461. }
  3462. }
  3463. }
  3464. static void hotkey_resume(void)
  3465. {
  3466. tpacpi_disable_brightness_delay();
  3467. if (hotkey_status_set(true) < 0 ||
  3468. hotkey_mask_set(hotkey_acpi_mask) < 0)
  3469. pr_err("error while attempting to reset the event firmware interface\n");
  3470. tpacpi_send_radiosw_update();
  3471. tpacpi_input_send_tabletsw();
  3472. hotkey_tablet_mode_notify_change();
  3473. hotkey_wakeup_reason_notify_change();
  3474. hotkey_wakeup_hotunplug_complete_notify_change();
  3475. hotkey_poll_setup_safe(false);
  3476. /* restore previous mode of adapive keyboard of X1 Carbon */
  3477. if (tp_features.has_adaptive_kbd) {
  3478. if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
  3479. adaptive_keyboard_prev_mode)) {
  3480. pr_err("Cannot set adaptive keyboard mode.\n");
  3481. }
  3482. }
  3483. }
  3484. /* procfs -------------------------------------------------------------- */
  3485. static int hotkey_read(struct seq_file *m)
  3486. {
  3487. int res, status;
  3488. if (!tp_features.hotkey) {
  3489. seq_printf(m, "status:\t\tnot supported\n");
  3490. return 0;
  3491. }
  3492. if (mutex_lock_killable(&hotkey_mutex))
  3493. return -ERESTARTSYS;
  3494. res = hotkey_status_get(&status);
  3495. if (!res)
  3496. res = hotkey_mask_get();
  3497. mutex_unlock(&hotkey_mutex);
  3498. if (res)
  3499. return res;
  3500. seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
  3501. if (hotkey_all_mask) {
  3502. seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
  3503. seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
  3504. } else {
  3505. seq_printf(m, "mask:\t\tnot supported\n");
  3506. seq_printf(m, "commands:\tenable, disable, reset\n");
  3507. }
  3508. return 0;
  3509. }
  3510. static void hotkey_enabledisable_warn(bool enable)
  3511. {
  3512. tpacpi_log_usertask("procfs hotkey enable/disable");
  3513. if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
  3514. pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
  3515. pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
  3516. }
  3517. static int hotkey_write(char *buf)
  3518. {
  3519. int res;
  3520. u32 mask;
  3521. char *cmd;
  3522. if (!tp_features.hotkey)
  3523. return -ENODEV;
  3524. if (mutex_lock_killable(&hotkey_mutex))
  3525. return -ERESTARTSYS;
  3526. mask = hotkey_user_mask;
  3527. res = 0;
  3528. while ((cmd = strsep(&buf, ","))) {
  3529. if (strlencmp(cmd, "enable") == 0) {
  3530. hotkey_enabledisable_warn(1);
  3531. } else if (strlencmp(cmd, "disable") == 0) {
  3532. hotkey_enabledisable_warn(0);
  3533. res = -EPERM;
  3534. } else if (strlencmp(cmd, "reset") == 0) {
  3535. mask = (hotkey_all_mask | hotkey_source_mask)
  3536. & ~hotkey_reserved_mask;
  3537. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  3538. /* mask set */
  3539. } else if (sscanf(cmd, "%x", &mask) == 1) {
  3540. /* mask set */
  3541. } else {
  3542. res = -EINVAL;
  3543. goto errexit;
  3544. }
  3545. }
  3546. if (!res) {
  3547. tpacpi_disclose_usertask("procfs hotkey",
  3548. "set mask to 0x%08x\n", mask);
  3549. res = hotkey_user_mask_set(mask);
  3550. }
  3551. errexit:
  3552. mutex_unlock(&hotkey_mutex);
  3553. return res;
  3554. }
  3555. static const struct acpi_device_id ibm_htk_device_ids[] = {
  3556. {TPACPI_ACPI_IBM_HKEY_HID, 0},
  3557. {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
  3558. {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
  3559. {"", 0},
  3560. };
  3561. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  3562. .hid = ibm_htk_device_ids,
  3563. .notify = hotkey_notify,
  3564. .handle = &hkey_handle,
  3565. .type = ACPI_DEVICE_NOTIFY,
  3566. };
  3567. static struct ibm_struct hotkey_driver_data = {
  3568. .name = "hotkey",
  3569. .read = hotkey_read,
  3570. .write = hotkey_write,
  3571. .exit = hotkey_exit,
  3572. .resume = hotkey_resume,
  3573. .suspend = hotkey_suspend,
  3574. .acpi = &ibm_hotkey_acpidriver,
  3575. };
  3576. /*************************************************************************
  3577. * Bluetooth subdriver
  3578. */
  3579. enum {
  3580. /* ACPI GBDC/SBDC bits */
  3581. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  3582. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  3583. TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
  3584. 0 = disable, 1 = enable */
  3585. };
  3586. enum {
  3587. /* ACPI \BLTH commands */
  3588. TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
  3589. TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
  3590. TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
  3591. TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
  3592. TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
  3593. };
  3594. #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
  3595. static int bluetooth_get_status(void)
  3596. {
  3597. int status;
  3598. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3599. if (dbg_bluetoothemul)
  3600. return (tpacpi_bluetooth_emulstate) ?
  3601. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3602. #endif
  3603. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  3604. return -EIO;
  3605. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
  3606. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3607. }
  3608. static int bluetooth_set_status(enum tpacpi_rfkill_state state)
  3609. {
  3610. int status;
  3611. vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s bluetooth\n",
  3612. str_enable_disable(state == TPACPI_RFK_RADIO_ON));
  3613. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3614. if (dbg_bluetoothemul) {
  3615. tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3616. return 0;
  3617. }
  3618. #endif
  3619. if (state == TPACPI_RFK_RADIO_ON)
  3620. status = TP_ACPI_BLUETOOTH_RADIOSSW
  3621. | TP_ACPI_BLUETOOTH_RESUMECTRL;
  3622. else
  3623. status = 0;
  3624. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  3625. return -EIO;
  3626. return 0;
  3627. }
  3628. /* sysfs bluetooth enable ---------------------------------------------- */
  3629. static ssize_t bluetooth_enable_show(struct device *dev,
  3630. struct device_attribute *attr,
  3631. char *buf)
  3632. {
  3633. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
  3634. attr, buf);
  3635. }
  3636. static ssize_t bluetooth_enable_store(struct device *dev,
  3637. struct device_attribute *attr,
  3638. const char *buf, size_t count)
  3639. {
  3640. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
  3641. attr, buf, count);
  3642. }
  3643. static DEVICE_ATTR_RW(bluetooth_enable);
  3644. /* --------------------------------------------------------------------- */
  3645. static struct attribute *bluetooth_attributes[] = {
  3646. &dev_attr_bluetooth_enable.attr,
  3647. NULL
  3648. };
  3649. static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
  3650. struct attribute *attr, int n)
  3651. {
  3652. return tp_features.bluetooth ? attr->mode : 0;
  3653. }
  3654. static const struct attribute_group bluetooth_attr_group = {
  3655. .is_visible = bluetooth_attr_is_visible,
  3656. .attrs = bluetooth_attributes,
  3657. };
  3658. static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
  3659. .get_status = bluetooth_get_status,
  3660. .set_status = bluetooth_set_status,
  3661. };
  3662. static void bluetooth_shutdown(void)
  3663. {
  3664. /* Order firmware to save current state to NVRAM */
  3665. if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
  3666. TP_ACPI_BLTH_SAVE_STATE))
  3667. pr_notice("failed to save bluetooth state to NVRAM\n");
  3668. else
  3669. vdbg_printk(TPACPI_DBG_RFKILL,
  3670. "bluetooth state saved to NVRAM\n");
  3671. }
  3672. static void bluetooth_exit(void)
  3673. {
  3674. tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
  3675. bluetooth_shutdown();
  3676. }
  3677. static const struct dmi_system_id fwbug_list[] __initconst = {
  3678. {
  3679. .ident = "ThinkPad E485",
  3680. .driver_data = &quirk_btusb_bug,
  3681. .matches = {
  3682. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3683. DMI_MATCH(DMI_BOARD_NAME, "20KU"),
  3684. },
  3685. },
  3686. {
  3687. .ident = "ThinkPad E585",
  3688. .driver_data = &quirk_btusb_bug,
  3689. .matches = {
  3690. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3691. DMI_MATCH(DMI_BOARD_NAME, "20KV"),
  3692. },
  3693. },
  3694. {
  3695. .ident = "ThinkPad A285 - 20MW",
  3696. .driver_data = &quirk_btusb_bug,
  3697. .matches = {
  3698. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3699. DMI_MATCH(DMI_BOARD_NAME, "20MW"),
  3700. },
  3701. },
  3702. {
  3703. .ident = "ThinkPad A285 - 20MX",
  3704. .driver_data = &quirk_btusb_bug,
  3705. .matches = {
  3706. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3707. DMI_MATCH(DMI_BOARD_NAME, "20MX"),
  3708. },
  3709. },
  3710. {
  3711. .ident = "ThinkPad A485 - 20MU",
  3712. .driver_data = &quirk_btusb_bug,
  3713. .matches = {
  3714. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3715. DMI_MATCH(DMI_BOARD_NAME, "20MU"),
  3716. },
  3717. },
  3718. {
  3719. .ident = "ThinkPad A485 - 20MV",
  3720. .driver_data = &quirk_btusb_bug,
  3721. .matches = {
  3722. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3723. DMI_MATCH(DMI_BOARD_NAME, "20MV"),
  3724. },
  3725. },
  3726. {
  3727. .ident = "L14 Gen2 AMD",
  3728. .driver_data = &quirk_s2idle_bug,
  3729. .matches = {
  3730. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3731. DMI_MATCH(DMI_PRODUCT_NAME, "20X5"),
  3732. }
  3733. },
  3734. {
  3735. .ident = "T14s Gen2 AMD",
  3736. .driver_data = &quirk_s2idle_bug,
  3737. .matches = {
  3738. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3739. DMI_MATCH(DMI_PRODUCT_NAME, "20XF"),
  3740. }
  3741. },
  3742. {
  3743. .ident = "X13 Gen2 AMD",
  3744. .driver_data = &quirk_s2idle_bug,
  3745. .matches = {
  3746. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3747. DMI_MATCH(DMI_PRODUCT_NAME, "20XH"),
  3748. }
  3749. },
  3750. {
  3751. .ident = "T14 Gen2 AMD",
  3752. .driver_data = &quirk_s2idle_bug,
  3753. .matches = {
  3754. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3755. DMI_MATCH(DMI_PRODUCT_NAME, "20XK"),
  3756. }
  3757. },
  3758. {
  3759. .ident = "T14 Gen1 AMD",
  3760. .driver_data = &quirk_s2idle_bug,
  3761. .matches = {
  3762. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3763. DMI_MATCH(DMI_PRODUCT_NAME, "20UD"),
  3764. }
  3765. },
  3766. {
  3767. .ident = "T14 Gen1 AMD",
  3768. .driver_data = &quirk_s2idle_bug,
  3769. .matches = {
  3770. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3771. DMI_MATCH(DMI_PRODUCT_NAME, "20UE"),
  3772. }
  3773. },
  3774. {
  3775. .ident = "T14s Gen1 AMD",
  3776. .driver_data = &quirk_s2idle_bug,
  3777. .matches = {
  3778. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3779. DMI_MATCH(DMI_PRODUCT_NAME, "20UH"),
  3780. }
  3781. },
  3782. {
  3783. .ident = "T14s Gen1 AMD",
  3784. .driver_data = &quirk_s2idle_bug,
  3785. .matches = {
  3786. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3787. DMI_MATCH(DMI_PRODUCT_NAME, "20UJ"),
  3788. }
  3789. },
  3790. {
  3791. .ident = "P14s Gen1 AMD",
  3792. .driver_data = &quirk_s2idle_bug,
  3793. .matches = {
  3794. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3795. DMI_MATCH(DMI_PRODUCT_NAME, "20Y1"),
  3796. }
  3797. },
  3798. {
  3799. .ident = "P14s Gen2 AMD",
  3800. .driver_data = &quirk_s2idle_bug,
  3801. .matches = {
  3802. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3803. DMI_MATCH(DMI_PRODUCT_NAME, "21A0"),
  3804. }
  3805. },
  3806. {
  3807. .ident = "P14s Gen2 AMD",
  3808. .driver_data = &quirk_s2idle_bug,
  3809. .matches = {
  3810. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3811. DMI_MATCH(DMI_PRODUCT_NAME, "21A1"),
  3812. }
  3813. },
  3814. /* https://bugzilla.kernel.org/show_bug.cgi?id=218024 */
  3815. {
  3816. .ident = "V14 G4 AMN",
  3817. .driver_data = &quirk_s2idle_bug,
  3818. .matches = {
  3819. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3820. DMI_MATCH(DMI_PRODUCT_NAME, "82YT"),
  3821. }
  3822. },
  3823. {
  3824. .ident = "V14 G4 AMN",
  3825. .driver_data = &quirk_s2idle_bug,
  3826. .matches = {
  3827. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3828. DMI_MATCH(DMI_PRODUCT_NAME, "83GE"),
  3829. }
  3830. },
  3831. {
  3832. .ident = "V15 G4 AMN",
  3833. .driver_data = &quirk_s2idle_bug,
  3834. .matches = {
  3835. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3836. DMI_MATCH(DMI_PRODUCT_NAME, "82YU"),
  3837. }
  3838. },
  3839. {
  3840. .ident = "V15 G4 AMN",
  3841. .driver_data = &quirk_s2idle_bug,
  3842. .matches = {
  3843. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3844. DMI_MATCH(DMI_PRODUCT_NAME, "83CQ"),
  3845. }
  3846. },
  3847. {
  3848. .ident = "IdeaPad 1 14AMN7",
  3849. .driver_data = &quirk_s2idle_bug,
  3850. .matches = {
  3851. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3852. DMI_MATCH(DMI_PRODUCT_NAME, "82VF"),
  3853. }
  3854. },
  3855. {
  3856. .ident = "IdeaPad 1 15AMN7",
  3857. .driver_data = &quirk_s2idle_bug,
  3858. .matches = {
  3859. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3860. DMI_MATCH(DMI_PRODUCT_NAME, "82VG"),
  3861. }
  3862. },
  3863. {
  3864. .ident = "IdeaPad 1 15AMN7",
  3865. .driver_data = &quirk_s2idle_bug,
  3866. .matches = {
  3867. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3868. DMI_MATCH(DMI_PRODUCT_NAME, "82X5"),
  3869. }
  3870. },
  3871. {
  3872. .ident = "IdeaPad Slim 3 14AMN8",
  3873. .driver_data = &quirk_s2idle_bug,
  3874. .matches = {
  3875. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3876. DMI_MATCH(DMI_PRODUCT_NAME, "82XN"),
  3877. }
  3878. },
  3879. {
  3880. .ident = "IdeaPad Slim 3 15AMN8",
  3881. .driver_data = &quirk_s2idle_bug,
  3882. .matches = {
  3883. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  3884. DMI_MATCH(DMI_PRODUCT_NAME, "82XQ"),
  3885. }
  3886. },
  3887. {}
  3888. };
  3889. #ifdef CONFIG_SUSPEND
  3890. /*
  3891. * Lenovo laptops from a variety of generations run a SMI handler during the D3->D0
  3892. * transition that occurs specifically when exiting suspend to idle which can cause
  3893. * large delays during resume when the IOMMU translation layer is enabled (the default
  3894. * behavior) for NVME devices:
  3895. *
  3896. * To avoid this firmware problem, skip the SMI handler on these machines before the
  3897. * D0 transition occurs.
  3898. */
  3899. static void thinkpad_acpi_amd_s2idle_restore(void)
  3900. {
  3901. struct resource *res;
  3902. void __iomem *addr;
  3903. u8 val;
  3904. res = request_mem_region_muxed(tp_features.quirks->s2idle_bug_mmio, 1,
  3905. "thinkpad_acpi_pm80");
  3906. if (!res)
  3907. return;
  3908. addr = ioremap(tp_features.quirks->s2idle_bug_mmio, 1);
  3909. if (!addr)
  3910. goto cleanup_resource;
  3911. val = ioread8(addr);
  3912. iowrite8(val & ~BIT(0), addr);
  3913. iounmap(addr);
  3914. cleanup_resource:
  3915. release_resource(res);
  3916. kfree(res);
  3917. }
  3918. static struct acpi_s2idle_dev_ops thinkpad_acpi_s2idle_dev_ops = {
  3919. .restore = thinkpad_acpi_amd_s2idle_restore,
  3920. };
  3921. #endif
  3922. static const struct pci_device_id fwbug_cards_ids[] __initconst = {
  3923. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
  3924. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
  3925. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
  3926. {}
  3927. };
  3928. static int __init have_bt_fwbug(void)
  3929. {
  3930. /*
  3931. * Some AMD based ThinkPads have a firmware bug that calling
  3932. * "GBDC" will cause bluetooth on Intel wireless cards blocked
  3933. */
  3934. if (tp_features.quirks && tp_features.quirks->btusb_bug &&
  3935. pci_dev_present(fwbug_cards_ids)) {
  3936. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3937. FW_BUG "disable bluetooth subdriver for Intel cards\n");
  3938. return 1;
  3939. } else
  3940. return 0;
  3941. }
  3942. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  3943. {
  3944. int res;
  3945. int status = 0;
  3946. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3947. "initializing bluetooth subdriver\n");
  3948. TPACPI_ACPIHANDLE_INIT(hkey);
  3949. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  3950. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  3951. tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
  3952. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  3953. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3954. "bluetooth is %s, status 0x%02x\n",
  3955. str_supported(tp_features.bluetooth),
  3956. status);
  3957. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3958. if (dbg_bluetoothemul) {
  3959. tp_features.bluetooth = 1;
  3960. pr_info("bluetooth switch emulation enabled\n");
  3961. } else
  3962. #endif
  3963. if (tp_features.bluetooth &&
  3964. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  3965. /* no bluetooth hardware present in system */
  3966. tp_features.bluetooth = 0;
  3967. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3968. "bluetooth hardware not installed\n");
  3969. }
  3970. if (!tp_features.bluetooth)
  3971. return -ENODEV;
  3972. res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
  3973. &bluetooth_tprfk_ops,
  3974. RFKILL_TYPE_BLUETOOTH,
  3975. TPACPI_RFK_BLUETOOTH_SW_NAME,
  3976. true);
  3977. return res;
  3978. }
  3979. /* procfs -------------------------------------------------------------- */
  3980. static int bluetooth_read(struct seq_file *m)
  3981. {
  3982. return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
  3983. }
  3984. static int bluetooth_write(char *buf)
  3985. {
  3986. return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
  3987. }
  3988. static struct ibm_struct bluetooth_driver_data = {
  3989. .name = "bluetooth",
  3990. .read = bluetooth_read,
  3991. .write = bluetooth_write,
  3992. .exit = bluetooth_exit,
  3993. .shutdown = bluetooth_shutdown,
  3994. };
  3995. /*************************************************************************
  3996. * Wan subdriver
  3997. */
  3998. enum {
  3999. /* ACPI GWAN/SWAN bits */
  4000. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  4001. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  4002. TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
  4003. 0 = disable, 1 = enable */
  4004. };
  4005. #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
  4006. static int wan_get_status(void)
  4007. {
  4008. int status;
  4009. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  4010. if (dbg_wwanemul)
  4011. return (tpacpi_wwan_emulstate) ?
  4012. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  4013. #endif
  4014. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  4015. return -EIO;
  4016. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
  4017. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  4018. }
  4019. static int wan_set_status(enum tpacpi_rfkill_state state)
  4020. {
  4021. int status;
  4022. vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n",
  4023. str_enable_disable(state == TPACPI_RFK_RADIO_ON));
  4024. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  4025. if (dbg_wwanemul) {
  4026. tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
  4027. return 0;
  4028. }
  4029. #endif
  4030. if (state == TPACPI_RFK_RADIO_ON)
  4031. status = TP_ACPI_WANCARD_RADIOSSW
  4032. | TP_ACPI_WANCARD_RESUMECTRL;
  4033. else
  4034. status = 0;
  4035. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  4036. return -EIO;
  4037. return 0;
  4038. }
  4039. /* sysfs wan enable ---------------------------------------------------- */
  4040. static ssize_t wan_enable_show(struct device *dev,
  4041. struct device_attribute *attr,
  4042. char *buf)
  4043. {
  4044. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
  4045. attr, buf);
  4046. }
  4047. static ssize_t wan_enable_store(struct device *dev,
  4048. struct device_attribute *attr,
  4049. const char *buf, size_t count)
  4050. {
  4051. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
  4052. attr, buf, count);
  4053. }
  4054. static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  4055. wan_enable_show, wan_enable_store);
  4056. /* --------------------------------------------------------------------- */
  4057. static struct attribute *wan_attributes[] = {
  4058. &dev_attr_wwan_enable.attr,
  4059. NULL
  4060. };
  4061. static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  4062. int n)
  4063. {
  4064. return tp_features.wan ? attr->mode : 0;
  4065. }
  4066. static const struct attribute_group wan_attr_group = {
  4067. .is_visible = wan_attr_is_visible,
  4068. .attrs = wan_attributes,
  4069. };
  4070. static const struct tpacpi_rfk_ops wan_tprfk_ops = {
  4071. .get_status = wan_get_status,
  4072. .set_status = wan_set_status,
  4073. };
  4074. static void wan_shutdown(void)
  4075. {
  4076. /* Order firmware to save current state to NVRAM */
  4077. if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
  4078. TP_ACPI_WGSV_SAVE_STATE))
  4079. pr_notice("failed to save WWAN state to NVRAM\n");
  4080. else
  4081. vdbg_printk(TPACPI_DBG_RFKILL,
  4082. "WWAN state saved to NVRAM\n");
  4083. }
  4084. static void wan_exit(void)
  4085. {
  4086. tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
  4087. wan_shutdown();
  4088. }
  4089. static int __init wan_init(struct ibm_init_struct *iibm)
  4090. {
  4091. int res;
  4092. int status = 0;
  4093. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  4094. "initializing wan subdriver\n");
  4095. TPACPI_ACPIHANDLE_INIT(hkey);
  4096. tp_features.wan = hkey_handle &&
  4097. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  4098. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  4099. "wan is %s, status 0x%02x\n",
  4100. str_supported(tp_features.wan),
  4101. status);
  4102. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  4103. if (dbg_wwanemul) {
  4104. tp_features.wan = 1;
  4105. pr_info("wwan switch emulation enabled\n");
  4106. } else
  4107. #endif
  4108. if (tp_features.wan &&
  4109. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  4110. /* no wan hardware present in system */
  4111. tp_features.wan = 0;
  4112. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  4113. "wan hardware not installed\n");
  4114. }
  4115. if (!tp_features.wan)
  4116. return -ENODEV;
  4117. res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
  4118. &wan_tprfk_ops,
  4119. RFKILL_TYPE_WWAN,
  4120. TPACPI_RFK_WWAN_SW_NAME,
  4121. true);
  4122. return res;
  4123. }
  4124. /* procfs -------------------------------------------------------------- */
  4125. static int wan_read(struct seq_file *m)
  4126. {
  4127. return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
  4128. }
  4129. static int wan_write(char *buf)
  4130. {
  4131. return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
  4132. }
  4133. static struct ibm_struct wan_driver_data = {
  4134. .name = "wan",
  4135. .read = wan_read,
  4136. .write = wan_write,
  4137. .exit = wan_exit,
  4138. .shutdown = wan_shutdown,
  4139. };
  4140. /*************************************************************************
  4141. * UWB subdriver
  4142. */
  4143. enum {
  4144. /* ACPI GUWB/SUWB bits */
  4145. TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
  4146. TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
  4147. };
  4148. #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
  4149. static int uwb_get_status(void)
  4150. {
  4151. int status;
  4152. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  4153. if (dbg_uwbemul)
  4154. return (tpacpi_uwb_emulstate) ?
  4155. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  4156. #endif
  4157. if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
  4158. return -EIO;
  4159. return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
  4160. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  4161. }
  4162. static int uwb_set_status(enum tpacpi_rfkill_state state)
  4163. {
  4164. int status;
  4165. vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n",
  4166. str_enable_disable(state == TPACPI_RFK_RADIO_ON));
  4167. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  4168. if (dbg_uwbemul) {
  4169. tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
  4170. return 0;
  4171. }
  4172. #endif
  4173. if (state == TPACPI_RFK_RADIO_ON)
  4174. status = TP_ACPI_UWB_RADIOSSW;
  4175. else
  4176. status = 0;
  4177. if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
  4178. return -EIO;
  4179. return 0;
  4180. }
  4181. /* --------------------------------------------------------------------- */
  4182. static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
  4183. .get_status = uwb_get_status,
  4184. .set_status = uwb_set_status,
  4185. };
  4186. static void uwb_exit(void)
  4187. {
  4188. tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
  4189. }
  4190. static int __init uwb_init(struct ibm_init_struct *iibm)
  4191. {
  4192. int res;
  4193. int status = 0;
  4194. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  4195. "initializing uwb subdriver\n");
  4196. TPACPI_ACPIHANDLE_INIT(hkey);
  4197. tp_features.uwb = hkey_handle &&
  4198. acpi_evalf(hkey_handle, &status, "GUWB", "qd");
  4199. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  4200. "uwb is %s, status 0x%02x\n",
  4201. str_supported(tp_features.uwb),
  4202. status);
  4203. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  4204. if (dbg_uwbemul) {
  4205. tp_features.uwb = 1;
  4206. pr_info("uwb switch emulation enabled\n");
  4207. } else
  4208. #endif
  4209. if (tp_features.uwb &&
  4210. !(status & TP_ACPI_UWB_HWPRESENT)) {
  4211. /* no uwb hardware present in system */
  4212. tp_features.uwb = 0;
  4213. dbg_printk(TPACPI_DBG_INIT,
  4214. "uwb hardware not installed\n");
  4215. }
  4216. if (!tp_features.uwb)
  4217. return -ENODEV;
  4218. res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
  4219. &uwb_tprfk_ops,
  4220. RFKILL_TYPE_UWB,
  4221. TPACPI_RFK_UWB_SW_NAME,
  4222. false);
  4223. return res;
  4224. }
  4225. static struct ibm_struct uwb_driver_data = {
  4226. .name = "uwb",
  4227. .exit = uwb_exit,
  4228. .flags.experimental = 1,
  4229. };
  4230. /*************************************************************************
  4231. * Video subdriver
  4232. */
  4233. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  4234. enum video_access_mode {
  4235. TPACPI_VIDEO_NONE = 0,
  4236. TPACPI_VIDEO_570, /* 570 */
  4237. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  4238. TPACPI_VIDEO_NEW, /* all others */
  4239. };
  4240. enum { /* video status flags, based on VIDEO_570 */
  4241. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  4242. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  4243. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  4244. };
  4245. enum { /* TPACPI_VIDEO_570 constants */
  4246. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  4247. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  4248. * video_status_flags */
  4249. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  4250. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  4251. };
  4252. static enum video_access_mode video_supported;
  4253. static int video_orig_autosw;
  4254. static int video_autosw_get(void);
  4255. static int video_autosw_set(int enable);
  4256. TPACPI_HANDLE(vid, root,
  4257. "\\_SB.PCI.AGP.VGA", /* 570 */
  4258. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  4259. "\\_SB.PCI0.VID0", /* 770e */
  4260. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  4261. "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
  4262. "\\_SB.PCI0.AGP.VID", /* all others */
  4263. ); /* R30, R31 */
  4264. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  4265. static int __init video_init(struct ibm_init_struct *iibm)
  4266. {
  4267. int ivga;
  4268. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  4269. TPACPI_ACPIHANDLE_INIT(vid);
  4270. if (tpacpi_is_ibm())
  4271. TPACPI_ACPIHANDLE_INIT(vid2);
  4272. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  4273. /* G41, assume IVGA doesn't change */
  4274. vid_handle = vid2_handle;
  4275. if (!vid_handle)
  4276. /* video switching not supported on R30, R31 */
  4277. video_supported = TPACPI_VIDEO_NONE;
  4278. else if (tpacpi_is_ibm() &&
  4279. acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  4280. /* 570 */
  4281. video_supported = TPACPI_VIDEO_570;
  4282. else if (tpacpi_is_ibm() &&
  4283. acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  4284. /* 600e/x, 770e, 770x */
  4285. video_supported = TPACPI_VIDEO_770;
  4286. else
  4287. /* all others */
  4288. video_supported = TPACPI_VIDEO_NEW;
  4289. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  4290. str_supported(video_supported != TPACPI_VIDEO_NONE),
  4291. video_supported);
  4292. return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
  4293. }
  4294. static void video_exit(void)
  4295. {
  4296. dbg_printk(TPACPI_DBG_EXIT,
  4297. "restoring original video autoswitch mode\n");
  4298. if (video_autosw_set(video_orig_autosw))
  4299. pr_err("error while trying to restore original video autoswitch mode\n");
  4300. }
  4301. static int video_outputsw_get(void)
  4302. {
  4303. int status = 0;
  4304. int i;
  4305. switch (video_supported) {
  4306. case TPACPI_VIDEO_570:
  4307. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  4308. TP_ACPI_VIDEO_570_PHSCMD))
  4309. return -EIO;
  4310. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  4311. break;
  4312. case TPACPI_VIDEO_770:
  4313. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  4314. return -EIO;
  4315. if (i)
  4316. status |= TP_ACPI_VIDEO_S_LCD;
  4317. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  4318. return -EIO;
  4319. if (i)
  4320. status |= TP_ACPI_VIDEO_S_CRT;
  4321. break;
  4322. case TPACPI_VIDEO_NEW:
  4323. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  4324. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  4325. return -EIO;
  4326. if (i)
  4327. status |= TP_ACPI_VIDEO_S_CRT;
  4328. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  4329. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  4330. return -EIO;
  4331. if (i)
  4332. status |= TP_ACPI_VIDEO_S_LCD;
  4333. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  4334. return -EIO;
  4335. if (i)
  4336. status |= TP_ACPI_VIDEO_S_DVI;
  4337. break;
  4338. default:
  4339. return -ENOSYS;
  4340. }
  4341. return status;
  4342. }
  4343. static int video_outputsw_set(int status)
  4344. {
  4345. int autosw;
  4346. int res = 0;
  4347. switch (video_supported) {
  4348. case TPACPI_VIDEO_570:
  4349. res = acpi_evalf(NULL, NULL,
  4350. "\\_SB.PHS2", "vdd",
  4351. TP_ACPI_VIDEO_570_PHS2CMD,
  4352. status | TP_ACPI_VIDEO_570_PHS2SET);
  4353. break;
  4354. case TPACPI_VIDEO_770:
  4355. autosw = video_autosw_get();
  4356. if (autosw < 0)
  4357. return autosw;
  4358. res = video_autosw_set(1);
  4359. if (res)
  4360. return res;
  4361. res = acpi_evalf(vid_handle, NULL,
  4362. "ASWT", "vdd", status * 0x100, 0);
  4363. if (!autosw && video_autosw_set(autosw)) {
  4364. pr_err("video auto-switch left enabled due to error\n");
  4365. return -EIO;
  4366. }
  4367. break;
  4368. case TPACPI_VIDEO_NEW:
  4369. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  4370. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  4371. break;
  4372. default:
  4373. return -ENOSYS;
  4374. }
  4375. return (res) ? 0 : -EIO;
  4376. }
  4377. static int video_autosw_get(void)
  4378. {
  4379. int autosw = 0;
  4380. switch (video_supported) {
  4381. case TPACPI_VIDEO_570:
  4382. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  4383. return -EIO;
  4384. break;
  4385. case TPACPI_VIDEO_770:
  4386. case TPACPI_VIDEO_NEW:
  4387. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  4388. return -EIO;
  4389. break;
  4390. default:
  4391. return -ENOSYS;
  4392. }
  4393. return autosw & 1;
  4394. }
  4395. static int video_autosw_set(int enable)
  4396. {
  4397. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
  4398. return -EIO;
  4399. return 0;
  4400. }
  4401. static int video_outputsw_cycle(void)
  4402. {
  4403. int autosw = video_autosw_get();
  4404. int res;
  4405. if (autosw < 0)
  4406. return autosw;
  4407. switch (video_supported) {
  4408. case TPACPI_VIDEO_570:
  4409. res = video_autosw_set(1);
  4410. if (res)
  4411. return res;
  4412. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  4413. break;
  4414. case TPACPI_VIDEO_770:
  4415. case TPACPI_VIDEO_NEW:
  4416. res = video_autosw_set(1);
  4417. if (res)
  4418. return res;
  4419. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  4420. break;
  4421. default:
  4422. return -ENOSYS;
  4423. }
  4424. if (!autosw && video_autosw_set(autosw)) {
  4425. pr_err("video auto-switch left enabled due to error\n");
  4426. return -EIO;
  4427. }
  4428. return (res) ? 0 : -EIO;
  4429. }
  4430. static int video_expand_toggle(void)
  4431. {
  4432. switch (video_supported) {
  4433. case TPACPI_VIDEO_570:
  4434. return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
  4435. 0 : -EIO;
  4436. case TPACPI_VIDEO_770:
  4437. return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
  4438. 0 : -EIO;
  4439. case TPACPI_VIDEO_NEW:
  4440. return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
  4441. 0 : -EIO;
  4442. default:
  4443. return -ENOSYS;
  4444. }
  4445. /* not reached */
  4446. }
  4447. static int video_read(struct seq_file *m)
  4448. {
  4449. int status, autosw;
  4450. if (video_supported == TPACPI_VIDEO_NONE) {
  4451. seq_printf(m, "status:\t\tnot supported\n");
  4452. return 0;
  4453. }
  4454. /* Even reads can crash X.org, so... */
  4455. if (!capable(CAP_SYS_ADMIN))
  4456. return -EPERM;
  4457. status = video_outputsw_get();
  4458. if (status < 0)
  4459. return status;
  4460. autosw = video_autosw_get();
  4461. if (autosw < 0)
  4462. return autosw;
  4463. seq_printf(m, "status:\t\tsupported\n");
  4464. seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
  4465. seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1)));
  4466. if (video_supported == TPACPI_VIDEO_NEW)
  4467. seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3)));
  4468. seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0)));
  4469. seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
  4470. seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
  4471. if (video_supported == TPACPI_VIDEO_NEW)
  4472. seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
  4473. seq_printf(m, "commands:\tauto_enable, auto_disable\n");
  4474. seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
  4475. return 0;
  4476. }
  4477. static int video_write(char *buf)
  4478. {
  4479. char *cmd;
  4480. int enable, disable, status;
  4481. int res;
  4482. if (video_supported == TPACPI_VIDEO_NONE)
  4483. return -ENODEV;
  4484. /* Even reads can crash X.org, let alone writes... */
  4485. if (!capable(CAP_SYS_ADMIN))
  4486. return -EPERM;
  4487. enable = 0;
  4488. disable = 0;
  4489. while ((cmd = strsep(&buf, ","))) {
  4490. if (strlencmp(cmd, "lcd_enable") == 0) {
  4491. enable |= TP_ACPI_VIDEO_S_LCD;
  4492. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  4493. disable |= TP_ACPI_VIDEO_S_LCD;
  4494. } else if (strlencmp(cmd, "crt_enable") == 0) {
  4495. enable |= TP_ACPI_VIDEO_S_CRT;
  4496. } else if (strlencmp(cmd, "crt_disable") == 0) {
  4497. disable |= TP_ACPI_VIDEO_S_CRT;
  4498. } else if (video_supported == TPACPI_VIDEO_NEW &&
  4499. strlencmp(cmd, "dvi_enable") == 0) {
  4500. enable |= TP_ACPI_VIDEO_S_DVI;
  4501. } else if (video_supported == TPACPI_VIDEO_NEW &&
  4502. strlencmp(cmd, "dvi_disable") == 0) {
  4503. disable |= TP_ACPI_VIDEO_S_DVI;
  4504. } else if (strlencmp(cmd, "auto_enable") == 0) {
  4505. res = video_autosw_set(1);
  4506. if (res)
  4507. return res;
  4508. } else if (strlencmp(cmd, "auto_disable") == 0) {
  4509. res = video_autosw_set(0);
  4510. if (res)
  4511. return res;
  4512. } else if (strlencmp(cmd, "video_switch") == 0) {
  4513. res = video_outputsw_cycle();
  4514. if (res)
  4515. return res;
  4516. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  4517. res = video_expand_toggle();
  4518. if (res)
  4519. return res;
  4520. } else
  4521. return -EINVAL;
  4522. }
  4523. if (enable || disable) {
  4524. status = video_outputsw_get();
  4525. if (status < 0)
  4526. return status;
  4527. res = video_outputsw_set((status & ~disable) | enable);
  4528. if (res)
  4529. return res;
  4530. }
  4531. return 0;
  4532. }
  4533. static struct ibm_struct video_driver_data = {
  4534. .name = "video",
  4535. .read = video_read,
  4536. .write = video_write,
  4537. .exit = video_exit,
  4538. };
  4539. #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
  4540. /*************************************************************************
  4541. * Keyboard backlight subdriver
  4542. */
  4543. static enum led_brightness kbdlight_brightness;
  4544. static DEFINE_MUTEX(kbdlight_mutex);
  4545. static int kbdlight_set_level(int level)
  4546. {
  4547. int ret = 0;
  4548. if (!hkey_handle)
  4549. return -ENXIO;
  4550. mutex_lock(&kbdlight_mutex);
  4551. if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
  4552. ret = -EIO;
  4553. else
  4554. kbdlight_brightness = level;
  4555. mutex_unlock(&kbdlight_mutex);
  4556. return ret;
  4557. }
  4558. static int kbdlight_get_level(void)
  4559. {
  4560. int status = 0;
  4561. if (!hkey_handle)
  4562. return -ENXIO;
  4563. if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
  4564. return -EIO;
  4565. if (status < 0)
  4566. return status;
  4567. return status & 0x3;
  4568. }
  4569. static bool kbdlight_is_supported(void)
  4570. {
  4571. int status = 0;
  4572. if (!hkey_handle)
  4573. return false;
  4574. if (!acpi_has_method(hkey_handle, "MLCG")) {
  4575. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
  4576. return false;
  4577. }
  4578. if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
  4579. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
  4580. return false;
  4581. }
  4582. if (status < 0) {
  4583. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
  4584. return false;
  4585. }
  4586. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
  4587. /*
  4588. * Guessed test for keyboard backlight:
  4589. *
  4590. * Machines with backlight keyboard return:
  4591. * b010100000010000000XX - ThinkPad X1 Carbon 3rd
  4592. * b110100010010000000XX - ThinkPad x230
  4593. * b010100000010000000XX - ThinkPad x240
  4594. * b010100000010000000XX - ThinkPad W541
  4595. * (XX is current backlight level)
  4596. *
  4597. * Machines without backlight keyboard return:
  4598. * b10100001000000000000 - ThinkPad x230
  4599. * b10110001000000000000 - ThinkPad E430
  4600. * b00000000000000000000 - ThinkPad E450
  4601. *
  4602. * Candidate BITs for detection test (XOR):
  4603. * b01000000001000000000
  4604. * ^
  4605. */
  4606. return status & BIT(9);
  4607. }
  4608. static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
  4609. enum led_brightness brightness)
  4610. {
  4611. return kbdlight_set_level(brightness);
  4612. }
  4613. static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
  4614. {
  4615. int level;
  4616. level = kbdlight_get_level();
  4617. if (level < 0)
  4618. return 0;
  4619. return level;
  4620. }
  4621. static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
  4622. .led_classdev = {
  4623. .name = "tpacpi::kbd_backlight",
  4624. .max_brightness = 2,
  4625. .flags = LED_BRIGHT_HW_CHANGED,
  4626. .brightness_set_blocking = &kbdlight_sysfs_set,
  4627. .brightness_get = &kbdlight_sysfs_get,
  4628. }
  4629. };
  4630. static int __init kbdlight_init(struct ibm_init_struct *iibm)
  4631. {
  4632. int rc;
  4633. vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
  4634. TPACPI_ACPIHANDLE_INIT(hkey);
  4635. if (!kbdlight_is_supported()) {
  4636. tp_features.kbdlight = 0;
  4637. vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
  4638. return -ENODEV;
  4639. }
  4640. kbdlight_brightness = kbdlight_sysfs_get(NULL);
  4641. tp_features.kbdlight = 1;
  4642. rc = led_classdev_register(&tpacpi_pdev->dev,
  4643. &tpacpi_led_kbdlight.led_classdev);
  4644. if (rc < 0) {
  4645. tp_features.kbdlight = 0;
  4646. return rc;
  4647. }
  4648. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
  4649. TP_ACPI_HKEY_KBD_LIGHT_MASK);
  4650. return 0;
  4651. }
  4652. static void kbdlight_exit(void)
  4653. {
  4654. led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
  4655. }
  4656. static int kbdlight_set_level_and_update(int level)
  4657. {
  4658. int ret;
  4659. struct led_classdev *led_cdev;
  4660. ret = kbdlight_set_level(level);
  4661. led_cdev = &tpacpi_led_kbdlight.led_classdev;
  4662. if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
  4663. led_cdev->brightness = level;
  4664. return ret;
  4665. }
  4666. static int kbdlight_read(struct seq_file *m)
  4667. {
  4668. int level;
  4669. if (!tp_features.kbdlight) {
  4670. seq_printf(m, "status:\t\tnot supported\n");
  4671. } else {
  4672. level = kbdlight_get_level();
  4673. if (level < 0)
  4674. seq_printf(m, "status:\t\terror %d\n", level);
  4675. else
  4676. seq_printf(m, "status:\t\t%d\n", level);
  4677. seq_printf(m, "commands:\t0, 1, 2\n");
  4678. }
  4679. return 0;
  4680. }
  4681. static int kbdlight_write(char *buf)
  4682. {
  4683. char *cmd;
  4684. int res, level = -EINVAL;
  4685. if (!tp_features.kbdlight)
  4686. return -ENODEV;
  4687. while ((cmd = strsep(&buf, ","))) {
  4688. res = kstrtoint(cmd, 10, &level);
  4689. if (res < 0)
  4690. return res;
  4691. }
  4692. if (level >= 3 || level < 0)
  4693. return -EINVAL;
  4694. return kbdlight_set_level_and_update(level);
  4695. }
  4696. static void kbdlight_suspend(void)
  4697. {
  4698. struct led_classdev *led_cdev;
  4699. if (!tp_features.kbdlight)
  4700. return;
  4701. led_cdev = &tpacpi_led_kbdlight.led_classdev;
  4702. led_update_brightness(led_cdev);
  4703. led_classdev_suspend(led_cdev);
  4704. }
  4705. static void kbdlight_resume(void)
  4706. {
  4707. if (!tp_features.kbdlight)
  4708. return;
  4709. led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
  4710. }
  4711. static struct ibm_struct kbdlight_driver_data = {
  4712. .name = "kbdlight",
  4713. .read = kbdlight_read,
  4714. .write = kbdlight_write,
  4715. .suspend = kbdlight_suspend,
  4716. .resume = kbdlight_resume,
  4717. .exit = kbdlight_exit,
  4718. };
  4719. /*************************************************************************
  4720. * Light (thinklight) subdriver
  4721. */
  4722. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  4723. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  4724. static int light_get_status(void)
  4725. {
  4726. int status = 0;
  4727. if (tp_features.light_status) {
  4728. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  4729. return -EIO;
  4730. return (!!status);
  4731. }
  4732. return -ENXIO;
  4733. }
  4734. static int light_set_status(int status)
  4735. {
  4736. int rc;
  4737. if (tp_features.light) {
  4738. if (cmos_handle) {
  4739. rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
  4740. (status) ?
  4741. TP_CMOS_THINKLIGHT_ON :
  4742. TP_CMOS_THINKLIGHT_OFF);
  4743. } else {
  4744. rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
  4745. (status) ? 1 : 0);
  4746. }
  4747. return (rc) ? 0 : -EIO;
  4748. }
  4749. return -ENXIO;
  4750. }
  4751. static int light_sysfs_set(struct led_classdev *led_cdev,
  4752. enum led_brightness brightness)
  4753. {
  4754. return light_set_status((brightness != LED_OFF) ?
  4755. TPACPI_LED_ON : TPACPI_LED_OFF);
  4756. }
  4757. static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
  4758. {
  4759. return (light_get_status() == 1) ? LED_ON : LED_OFF;
  4760. }
  4761. static struct tpacpi_led_classdev tpacpi_led_thinklight = {
  4762. .led_classdev = {
  4763. .name = "tpacpi::thinklight",
  4764. .max_brightness = 1,
  4765. .brightness_set_blocking = &light_sysfs_set,
  4766. .brightness_get = &light_sysfs_get,
  4767. }
  4768. };
  4769. static int __init light_init(struct ibm_init_struct *iibm)
  4770. {
  4771. int rc;
  4772. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  4773. if (tpacpi_is_ibm()) {
  4774. TPACPI_ACPIHANDLE_INIT(ledb);
  4775. TPACPI_ACPIHANDLE_INIT(lght);
  4776. }
  4777. TPACPI_ACPIHANDLE_INIT(cmos);
  4778. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  4779. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  4780. if (tp_features.light)
  4781. /* light status not supported on
  4782. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  4783. tp_features.light_status =
  4784. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  4785. vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
  4786. str_supported(tp_features.light),
  4787. str_supported(tp_features.light_status));
  4788. if (!tp_features.light)
  4789. return -ENODEV;
  4790. rc = led_classdev_register(&tpacpi_pdev->dev,
  4791. &tpacpi_led_thinklight.led_classdev);
  4792. if (rc < 0) {
  4793. tp_features.light = 0;
  4794. tp_features.light_status = 0;
  4795. } else {
  4796. rc = 0;
  4797. }
  4798. return rc;
  4799. }
  4800. static void light_exit(void)
  4801. {
  4802. led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
  4803. }
  4804. static int light_read(struct seq_file *m)
  4805. {
  4806. int status;
  4807. if (!tp_features.light) {
  4808. seq_printf(m, "status:\t\tnot supported\n");
  4809. } else if (!tp_features.light_status) {
  4810. seq_printf(m, "status:\t\tunknown\n");
  4811. seq_printf(m, "commands:\ton, off\n");
  4812. } else {
  4813. status = light_get_status();
  4814. if (status < 0)
  4815. return status;
  4816. seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0)));
  4817. seq_printf(m, "commands:\ton, off\n");
  4818. }
  4819. return 0;
  4820. }
  4821. static int light_write(char *buf)
  4822. {
  4823. char *cmd;
  4824. int newstatus = 0;
  4825. if (!tp_features.light)
  4826. return -ENODEV;
  4827. while ((cmd = strsep(&buf, ","))) {
  4828. if (strlencmp(cmd, "on") == 0) {
  4829. newstatus = 1;
  4830. } else if (strlencmp(cmd, "off") == 0) {
  4831. newstatus = 0;
  4832. } else
  4833. return -EINVAL;
  4834. }
  4835. return light_set_status(newstatus);
  4836. }
  4837. static struct ibm_struct light_driver_data = {
  4838. .name = "light",
  4839. .read = light_read,
  4840. .write = light_write,
  4841. .exit = light_exit,
  4842. };
  4843. /*************************************************************************
  4844. * CMOS subdriver
  4845. */
  4846. /* sysfs cmos_command -------------------------------------------------- */
  4847. static ssize_t cmos_command_store(struct device *dev,
  4848. struct device_attribute *attr,
  4849. const char *buf, size_t count)
  4850. {
  4851. unsigned long cmos_cmd;
  4852. int res;
  4853. if (parse_strtoul(buf, 21, &cmos_cmd))
  4854. return -EINVAL;
  4855. res = issue_thinkpad_cmos_command(cmos_cmd);
  4856. return (res) ? res : count;
  4857. }
  4858. static DEVICE_ATTR_WO(cmos_command);
  4859. static struct attribute *cmos_attributes[] = {
  4860. &dev_attr_cmos_command.attr,
  4861. NULL
  4862. };
  4863. static umode_t cmos_attr_is_visible(struct kobject *kobj,
  4864. struct attribute *attr, int n)
  4865. {
  4866. return cmos_handle ? attr->mode : 0;
  4867. }
  4868. static const struct attribute_group cmos_attr_group = {
  4869. .is_visible = cmos_attr_is_visible,
  4870. .attrs = cmos_attributes,
  4871. };
  4872. /* --------------------------------------------------------------------- */
  4873. static int __init cmos_init(struct ibm_init_struct *iibm)
  4874. {
  4875. vdbg_printk(TPACPI_DBG_INIT,
  4876. "initializing cmos commands subdriver\n");
  4877. TPACPI_ACPIHANDLE_INIT(cmos);
  4878. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  4879. str_supported(cmos_handle != NULL));
  4880. return cmos_handle ? 0 : -ENODEV;
  4881. }
  4882. static int cmos_read(struct seq_file *m)
  4883. {
  4884. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  4885. R30, R31, T20-22, X20-21 */
  4886. if (!cmos_handle)
  4887. seq_printf(m, "status:\t\tnot supported\n");
  4888. else {
  4889. seq_printf(m, "status:\t\tsupported\n");
  4890. seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
  4891. }
  4892. return 0;
  4893. }
  4894. static int cmos_write(char *buf)
  4895. {
  4896. char *cmd;
  4897. int cmos_cmd, res;
  4898. while ((cmd = strsep(&buf, ","))) {
  4899. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  4900. cmos_cmd >= 0 && cmos_cmd <= 21) {
  4901. /* cmos_cmd set */
  4902. } else
  4903. return -EINVAL;
  4904. res = issue_thinkpad_cmos_command(cmos_cmd);
  4905. if (res)
  4906. return res;
  4907. }
  4908. return 0;
  4909. }
  4910. static struct ibm_struct cmos_driver_data = {
  4911. .name = "cmos",
  4912. .read = cmos_read,
  4913. .write = cmos_write,
  4914. };
  4915. /*************************************************************************
  4916. * LED subdriver
  4917. */
  4918. enum led_access_mode {
  4919. TPACPI_LED_NONE = 0,
  4920. TPACPI_LED_570, /* 570 */
  4921. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  4922. TPACPI_LED_NEW, /* all others */
  4923. };
  4924. enum { /* For TPACPI_LED_OLD */
  4925. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  4926. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  4927. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  4928. };
  4929. static enum led_access_mode led_supported;
  4930. static acpi_handle led_handle;
  4931. #define TPACPI_LED_NUMLEDS 16
  4932. static struct tpacpi_led_classdev *tpacpi_leds;
  4933. static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
  4934. static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
  4935. /* there's a limit of 19 chars + NULL before 2.6.26 */
  4936. "tpacpi::power",
  4937. "tpacpi:orange:batt",
  4938. "tpacpi:green:batt",
  4939. "tpacpi::dock_active",
  4940. "tpacpi::bay_active",
  4941. "tpacpi::dock_batt",
  4942. "tpacpi::unknown_led",
  4943. "tpacpi::standby",
  4944. "tpacpi::dock_status1",
  4945. "tpacpi::dock_status2",
  4946. "tpacpi::lid_logo_dot",
  4947. "tpacpi::unknown_led3",
  4948. "tpacpi::thinkvantage",
  4949. };
  4950. #define TPACPI_SAFE_LEDS 0x1481U
  4951. static inline bool tpacpi_is_led_restricted(const unsigned int led)
  4952. {
  4953. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  4954. return false;
  4955. #else
  4956. return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
  4957. #endif
  4958. }
  4959. static int led_get_status(const unsigned int led)
  4960. {
  4961. int status;
  4962. enum led_status_t led_s;
  4963. switch (led_supported) {
  4964. case TPACPI_LED_570:
  4965. if (!acpi_evalf(ec_handle,
  4966. &status, "GLED", "dd", 1 << led))
  4967. return -EIO;
  4968. led_s = (status == 0) ?
  4969. TPACPI_LED_OFF :
  4970. ((status == 1) ?
  4971. TPACPI_LED_ON :
  4972. TPACPI_LED_BLINK);
  4973. tpacpi_led_state_cache[led] = led_s;
  4974. return led_s;
  4975. default:
  4976. return -ENXIO;
  4977. }
  4978. /* not reached */
  4979. }
  4980. static int led_set_status(const unsigned int led,
  4981. const enum led_status_t ledstatus)
  4982. {
  4983. /* off, on, blink. Index is led_status_t */
  4984. static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
  4985. static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
  4986. int rc = 0;
  4987. switch (led_supported) {
  4988. case TPACPI_LED_570:
  4989. /* 570 */
  4990. if (unlikely(led > 7))
  4991. return -EINVAL;
  4992. if (unlikely(tpacpi_is_led_restricted(led)))
  4993. return -EPERM;
  4994. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4995. (1 << led), led_sled_arg1[ledstatus]))
  4996. return -EIO;
  4997. break;
  4998. case TPACPI_LED_OLD:
  4999. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  5000. if (unlikely(led > 7))
  5001. return -EINVAL;
  5002. if (unlikely(tpacpi_is_led_restricted(led)))
  5003. return -EPERM;
  5004. rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
  5005. if (rc >= 0)
  5006. rc = ec_write(TPACPI_LED_EC_HLBL,
  5007. (ledstatus == TPACPI_LED_BLINK) << led);
  5008. if (rc >= 0)
  5009. rc = ec_write(TPACPI_LED_EC_HLCL,
  5010. (ledstatus != TPACPI_LED_OFF) << led);
  5011. break;
  5012. case TPACPI_LED_NEW:
  5013. /* all others */
  5014. if (unlikely(led >= TPACPI_LED_NUMLEDS))
  5015. return -EINVAL;
  5016. if (unlikely(tpacpi_is_led_restricted(led)))
  5017. return -EPERM;
  5018. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  5019. led, led_led_arg1[ledstatus]))
  5020. return -EIO;
  5021. break;
  5022. default:
  5023. return -ENXIO;
  5024. }
  5025. if (!rc)
  5026. tpacpi_led_state_cache[led] = ledstatus;
  5027. return rc;
  5028. }
  5029. static int led_sysfs_set(struct led_classdev *led_cdev,
  5030. enum led_brightness brightness)
  5031. {
  5032. struct tpacpi_led_classdev *data = container_of(led_cdev,
  5033. struct tpacpi_led_classdev, led_classdev);
  5034. enum led_status_t new_state;
  5035. if (brightness == LED_OFF)
  5036. new_state = TPACPI_LED_OFF;
  5037. else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
  5038. new_state = TPACPI_LED_ON;
  5039. else
  5040. new_state = TPACPI_LED_BLINK;
  5041. return led_set_status(data->led, new_state);
  5042. }
  5043. static int led_sysfs_blink_set(struct led_classdev *led_cdev,
  5044. unsigned long *delay_on, unsigned long *delay_off)
  5045. {
  5046. struct tpacpi_led_classdev *data = container_of(led_cdev,
  5047. struct tpacpi_led_classdev, led_classdev);
  5048. /* Can we choose the flash rate? */
  5049. if (*delay_on == 0 && *delay_off == 0) {
  5050. /* yes. set them to the hardware blink rate (1 Hz) */
  5051. *delay_on = 500; /* ms */
  5052. *delay_off = 500; /* ms */
  5053. } else if ((*delay_on != 500) || (*delay_off != 500))
  5054. return -EINVAL;
  5055. return led_set_status(data->led, TPACPI_LED_BLINK);
  5056. }
  5057. static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
  5058. {
  5059. int rc;
  5060. struct tpacpi_led_classdev *data = container_of(led_cdev,
  5061. struct tpacpi_led_classdev, led_classdev);
  5062. rc = led_get_status(data->led);
  5063. if (rc == TPACPI_LED_OFF || rc < 0)
  5064. rc = LED_OFF; /* no error handling in led class :( */
  5065. else
  5066. rc = LED_FULL;
  5067. return rc;
  5068. }
  5069. static void led_exit(void)
  5070. {
  5071. unsigned int i;
  5072. for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
  5073. led_classdev_unregister(&tpacpi_leds[i].led_classdev);
  5074. kfree(tpacpi_leds);
  5075. }
  5076. static int __init tpacpi_init_led(unsigned int led)
  5077. {
  5078. /* LEDs with no name don't get registered */
  5079. if (!tpacpi_led_names[led])
  5080. return 0;
  5081. tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
  5082. tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
  5083. if (led_supported == TPACPI_LED_570)
  5084. tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
  5085. tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
  5086. tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
  5087. tpacpi_leds[led].led = led;
  5088. return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
  5089. }
  5090. static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
  5091. TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
  5092. TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
  5093. TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
  5094. TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
  5095. TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
  5096. TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
  5097. TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
  5098. TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
  5099. TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
  5100. TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
  5101. TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
  5102. TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
  5103. TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
  5104. TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
  5105. TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
  5106. TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
  5107. TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
  5108. TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
  5109. TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
  5110. TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
  5111. /* (1) - may have excess leds enabled on MSB */
  5112. /* Defaults (order matters, keep last, don't reorder!) */
  5113. { /* Lenovo */
  5114. .vendor = PCI_VENDOR_ID_LENOVO,
  5115. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  5116. .quirks = 0x1fffU,
  5117. },
  5118. { /* IBM ThinkPads with no EC version string */
  5119. .vendor = PCI_VENDOR_ID_IBM,
  5120. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
  5121. .quirks = 0x00ffU,
  5122. },
  5123. { /* IBM ThinkPads with EC version string */
  5124. .vendor = PCI_VENDOR_ID_IBM,
  5125. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  5126. .quirks = 0x00bfU,
  5127. },
  5128. };
  5129. static enum led_access_mode __init led_init_detect_mode(void)
  5130. {
  5131. acpi_status status;
  5132. if (tpacpi_is_ibm()) {
  5133. /* 570 */
  5134. status = acpi_get_handle(ec_handle, "SLED", &led_handle);
  5135. if (ACPI_SUCCESS(status))
  5136. return TPACPI_LED_570;
  5137. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  5138. status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
  5139. if (ACPI_SUCCESS(status))
  5140. return TPACPI_LED_OLD;
  5141. }
  5142. /* most others */
  5143. status = acpi_get_handle(ec_handle, "LED", &led_handle);
  5144. if (ACPI_SUCCESS(status))
  5145. return TPACPI_LED_NEW;
  5146. /* R30, R31, and unknown firmwares */
  5147. led_handle = NULL;
  5148. return TPACPI_LED_NONE;
  5149. }
  5150. static int __init led_init(struct ibm_init_struct *iibm)
  5151. {
  5152. unsigned int i;
  5153. int rc;
  5154. unsigned long useful_leds;
  5155. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  5156. led_supported = led_init_detect_mode();
  5157. if (led_supported != TPACPI_LED_NONE) {
  5158. useful_leds = tpacpi_check_quirks(led_useful_qtable,
  5159. ARRAY_SIZE(led_useful_qtable));
  5160. if (!useful_leds) {
  5161. led_handle = NULL;
  5162. led_supported = TPACPI_LED_NONE;
  5163. }
  5164. }
  5165. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  5166. str_supported(led_supported), led_supported);
  5167. if (led_supported == TPACPI_LED_NONE)
  5168. return -ENODEV;
  5169. tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
  5170. GFP_KERNEL);
  5171. if (!tpacpi_leds) {
  5172. pr_err("Out of memory for LED data\n");
  5173. return -ENOMEM;
  5174. }
  5175. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  5176. tpacpi_leds[i].led = -1;
  5177. if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
  5178. rc = tpacpi_init_led(i);
  5179. if (rc < 0) {
  5180. led_exit();
  5181. return rc;
  5182. }
  5183. }
  5184. }
  5185. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  5186. pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
  5187. #endif
  5188. return 0;
  5189. }
  5190. #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s))
  5191. static int led_read(struct seq_file *m)
  5192. {
  5193. if (!led_supported) {
  5194. seq_printf(m, "status:\t\tnot supported\n");
  5195. return 0;
  5196. }
  5197. seq_printf(m, "status:\t\tsupported\n");
  5198. if (led_supported == TPACPI_LED_570) {
  5199. /* 570 */
  5200. int i, status;
  5201. for (i = 0; i < 8; i++) {
  5202. status = led_get_status(i);
  5203. if (status < 0)
  5204. return -EIO;
  5205. seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status));
  5206. }
  5207. }
  5208. seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
  5209. return 0;
  5210. }
  5211. static int led_write(char *buf)
  5212. {
  5213. char *cmd;
  5214. int led, rc;
  5215. enum led_status_t s;
  5216. if (!led_supported)
  5217. return -ENODEV;
  5218. while ((cmd = strsep(&buf, ","))) {
  5219. if (sscanf(cmd, "%d", &led) != 1)
  5220. return -EINVAL;
  5221. if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
  5222. return -ENODEV;
  5223. if (tpacpi_leds[led].led < 0)
  5224. return -ENODEV;
  5225. if (strstr(cmd, "off")) {
  5226. s = TPACPI_LED_OFF;
  5227. } else if (strstr(cmd, "on")) {
  5228. s = TPACPI_LED_ON;
  5229. } else if (strstr(cmd, "blink")) {
  5230. s = TPACPI_LED_BLINK;
  5231. } else {
  5232. return -EINVAL;
  5233. }
  5234. rc = led_set_status(led, s);
  5235. if (rc < 0)
  5236. return rc;
  5237. }
  5238. return 0;
  5239. }
  5240. static struct ibm_struct led_driver_data = {
  5241. .name = "led",
  5242. .read = led_read,
  5243. .write = led_write,
  5244. .exit = led_exit,
  5245. };
  5246. /*************************************************************************
  5247. * Beep subdriver
  5248. */
  5249. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  5250. #define TPACPI_BEEP_Q1 0x0001
  5251. static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
  5252. TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
  5253. TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
  5254. };
  5255. static int __init beep_init(struct ibm_init_struct *iibm)
  5256. {
  5257. unsigned long quirks;
  5258. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  5259. TPACPI_ACPIHANDLE_INIT(beep);
  5260. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  5261. str_supported(beep_handle != NULL));
  5262. quirks = tpacpi_check_quirks(beep_quirk_table,
  5263. ARRAY_SIZE(beep_quirk_table));
  5264. tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
  5265. return (beep_handle) ? 0 : -ENODEV;
  5266. }
  5267. static int beep_read(struct seq_file *m)
  5268. {
  5269. if (!beep_handle)
  5270. seq_printf(m, "status:\t\tnot supported\n");
  5271. else {
  5272. seq_printf(m, "status:\t\tsupported\n");
  5273. seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
  5274. }
  5275. return 0;
  5276. }
  5277. static int beep_write(char *buf)
  5278. {
  5279. char *cmd;
  5280. int beep_cmd;
  5281. if (!beep_handle)
  5282. return -ENODEV;
  5283. while ((cmd = strsep(&buf, ","))) {
  5284. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  5285. beep_cmd >= 0 && beep_cmd <= 17) {
  5286. /* beep_cmd set */
  5287. } else
  5288. return -EINVAL;
  5289. if (tp_features.beep_needs_two_args) {
  5290. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
  5291. beep_cmd, 0))
  5292. return -EIO;
  5293. } else {
  5294. if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
  5295. beep_cmd))
  5296. return -EIO;
  5297. }
  5298. }
  5299. return 0;
  5300. }
  5301. static struct ibm_struct beep_driver_data = {
  5302. .name = "beep",
  5303. .read = beep_read,
  5304. .write = beep_write,
  5305. };
  5306. /*************************************************************************
  5307. * Thermal subdriver
  5308. */
  5309. enum thermal_access_mode {
  5310. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  5311. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  5312. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  5313. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  5314. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  5315. };
  5316. enum { /* TPACPI_THERMAL_TPEC_* */
  5317. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  5318. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  5319. TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
  5320. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  5321. TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
  5322. };
  5323. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  5324. struct ibm_thermal_sensors_struct {
  5325. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  5326. };
  5327. static enum thermal_access_mode thermal_read_mode;
  5328. static bool thermal_use_labels;
  5329. /* idx is zero-based */
  5330. static int thermal_get_sensor(int idx, s32 *value)
  5331. {
  5332. int t;
  5333. s8 tmp;
  5334. char tmpi[5];
  5335. t = TP_EC_THERMAL_TMP0;
  5336. switch (thermal_read_mode) {
  5337. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  5338. case TPACPI_THERMAL_TPEC_16:
  5339. if (idx >= 8 && idx <= 15) {
  5340. t = TP_EC_THERMAL_TMP8;
  5341. idx -= 8;
  5342. }
  5343. #endif
  5344. fallthrough;
  5345. case TPACPI_THERMAL_TPEC_8:
  5346. if (idx <= 7) {
  5347. if (!acpi_ec_read(t + idx, &tmp))
  5348. return -EIO;
  5349. *value = tmp * 1000;
  5350. return 0;
  5351. }
  5352. break;
  5353. case TPACPI_THERMAL_ACPI_UPDT:
  5354. if (idx <= 7) {
  5355. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  5356. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  5357. return -EIO;
  5358. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  5359. return -EIO;
  5360. *value = (t - 2732) * 100;
  5361. return 0;
  5362. }
  5363. break;
  5364. case TPACPI_THERMAL_ACPI_TMP07:
  5365. if (idx <= 7) {
  5366. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  5367. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  5368. return -EIO;
  5369. if (t > 127 || t < -127)
  5370. t = TP_EC_THERMAL_TMP_NA;
  5371. *value = t * 1000;
  5372. return 0;
  5373. }
  5374. break;
  5375. case TPACPI_THERMAL_NONE:
  5376. default:
  5377. return -ENOSYS;
  5378. }
  5379. return -EINVAL;
  5380. }
  5381. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  5382. {
  5383. int res, i;
  5384. int n;
  5385. n = 8;
  5386. i = 0;
  5387. if (!s)
  5388. return -EINVAL;
  5389. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  5390. n = 16;
  5391. for (i = 0 ; i < n; i++) {
  5392. res = thermal_get_sensor(i, &s->temp[i]);
  5393. if (res)
  5394. return res;
  5395. }
  5396. return n;
  5397. }
  5398. static void thermal_dump_all_sensors(void)
  5399. {
  5400. int n, i;
  5401. struct ibm_thermal_sensors_struct t;
  5402. n = thermal_get_sensors(&t);
  5403. if (n <= 0)
  5404. return;
  5405. pr_notice("temperatures (Celsius):");
  5406. for (i = 0; i < n; i++) {
  5407. if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
  5408. pr_cont(" %d", (int)(t.temp[i] / 1000));
  5409. else
  5410. pr_cont(" N/A");
  5411. }
  5412. pr_cont("\n");
  5413. }
  5414. /* sysfs temp##_input -------------------------------------------------- */
  5415. static ssize_t thermal_temp_input_show(struct device *dev,
  5416. struct device_attribute *attr,
  5417. char *buf)
  5418. {
  5419. struct sensor_device_attribute *sensor_attr =
  5420. to_sensor_dev_attr(attr);
  5421. int idx = sensor_attr->index;
  5422. s32 value;
  5423. int res;
  5424. res = thermal_get_sensor(idx, &value);
  5425. if (res)
  5426. return res;
  5427. if (value == TPACPI_THERMAL_SENSOR_NA)
  5428. return -ENXIO;
  5429. return sysfs_emit(buf, "%d\n", value);
  5430. }
  5431. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  5432. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  5433. thermal_temp_input_show, NULL, _idxB)
  5434. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  5435. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  5436. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  5437. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  5438. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  5439. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  5440. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  5441. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  5442. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  5443. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  5444. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  5445. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  5446. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  5447. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  5448. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  5449. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  5450. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  5451. };
  5452. #define THERMAL_ATTRS(X) \
  5453. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  5454. static struct attribute *thermal_temp_input_attr[] = {
  5455. THERMAL_ATTRS(0),
  5456. THERMAL_ATTRS(1),
  5457. THERMAL_ATTRS(2),
  5458. THERMAL_ATTRS(3),
  5459. THERMAL_ATTRS(4),
  5460. THERMAL_ATTRS(5),
  5461. THERMAL_ATTRS(6),
  5462. THERMAL_ATTRS(7),
  5463. THERMAL_ATTRS(8),
  5464. THERMAL_ATTRS(9),
  5465. THERMAL_ATTRS(10),
  5466. THERMAL_ATTRS(11),
  5467. THERMAL_ATTRS(12),
  5468. THERMAL_ATTRS(13),
  5469. THERMAL_ATTRS(14),
  5470. THERMAL_ATTRS(15),
  5471. NULL
  5472. };
  5473. static umode_t thermal_attr_is_visible(struct kobject *kobj,
  5474. struct attribute *attr, int n)
  5475. {
  5476. if (thermal_read_mode == TPACPI_THERMAL_NONE)
  5477. return 0;
  5478. if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) ||
  5479. attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) ||
  5480. attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) ||
  5481. attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) {
  5482. if (thermal_read_mode != TPACPI_THERMAL_TPEC_16)
  5483. return 0;
  5484. }
  5485. return attr->mode;
  5486. }
  5487. static const struct attribute_group thermal_attr_group = {
  5488. .is_visible = thermal_attr_is_visible,
  5489. .attrs = thermal_temp_input_attr,
  5490. };
  5491. #undef THERMAL_SENSOR_ATTR_TEMP
  5492. #undef THERMAL_ATTRS
  5493. static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
  5494. {
  5495. return sysfs_emit(buf, "CPU\n");
  5496. }
  5497. static DEVICE_ATTR_RO(temp1_label);
  5498. static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
  5499. {
  5500. return sysfs_emit(buf, "GPU\n");
  5501. }
  5502. static DEVICE_ATTR_RO(temp2_label);
  5503. static struct attribute *temp_label_attributes[] = {
  5504. &dev_attr_temp1_label.attr,
  5505. &dev_attr_temp2_label.attr,
  5506. NULL
  5507. };
  5508. static umode_t temp_label_attr_is_visible(struct kobject *kobj,
  5509. struct attribute *attr, int n)
  5510. {
  5511. return thermal_use_labels ? attr->mode : 0;
  5512. }
  5513. static const struct attribute_group temp_label_attr_group = {
  5514. .is_visible = temp_label_attr_is_visible,
  5515. .attrs = temp_label_attributes,
  5516. };
  5517. /* --------------------------------------------------------------------- */
  5518. static int __init thermal_init(struct ibm_init_struct *iibm)
  5519. {
  5520. u8 t, ta1, ta2, ver = 0;
  5521. int i;
  5522. int acpi_tmp7;
  5523. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  5524. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  5525. if (thinkpad_id.ec_model) {
  5526. /*
  5527. * Direct EC access mode: sensors at registers
  5528. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  5529. * non-implemented, thermal sensors return 0x80 when
  5530. * not available
  5531. * The above rule is unfortunately flawed. This has been seen with
  5532. * 0xC2 (power supply ID) causing thermal control problems.
  5533. * The EC version can be determined by offset 0xEF and at least for
  5534. * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
  5535. * are not thermal registers.
  5536. */
  5537. if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
  5538. pr_warn("Thinkpad ACPI EC unable to access EC version\n");
  5539. ta1 = ta2 = 0;
  5540. for (i = 0; i < 8; i++) {
  5541. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  5542. ta1 |= t;
  5543. } else {
  5544. ta1 = 0;
  5545. break;
  5546. }
  5547. if (ver < 3) {
  5548. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  5549. ta2 |= t;
  5550. } else {
  5551. ta1 = 0;
  5552. break;
  5553. }
  5554. }
  5555. }
  5556. if (ta1 == 0) {
  5557. /* This is sheer paranoia, but we handle it anyway */
  5558. if (acpi_tmp7) {
  5559. pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
  5560. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  5561. } else {
  5562. pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
  5563. thermal_read_mode = TPACPI_THERMAL_NONE;
  5564. }
  5565. } else {
  5566. if (ver >= 3) {
  5567. thermal_read_mode = TPACPI_THERMAL_TPEC_8;
  5568. thermal_use_labels = true;
  5569. } else {
  5570. thermal_read_mode =
  5571. (ta2 != 0) ?
  5572. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  5573. }
  5574. }
  5575. } else if (acpi_tmp7) {
  5576. if (tpacpi_is_ibm() &&
  5577. acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  5578. /* 600e/x, 770e, 770x */
  5579. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  5580. } else {
  5581. /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
  5582. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  5583. }
  5584. } else {
  5585. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  5586. thermal_read_mode = TPACPI_THERMAL_NONE;
  5587. }
  5588. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  5589. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  5590. thermal_read_mode);
  5591. return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
  5592. }
  5593. static int thermal_read(struct seq_file *m)
  5594. {
  5595. int n, i;
  5596. struct ibm_thermal_sensors_struct t;
  5597. n = thermal_get_sensors(&t);
  5598. if (unlikely(n < 0))
  5599. return n;
  5600. seq_printf(m, "temperatures:\t");
  5601. if (n > 0) {
  5602. for (i = 0; i < (n - 1); i++)
  5603. seq_printf(m, "%d ", t.temp[i] / 1000);
  5604. seq_printf(m, "%d\n", t.temp[i] / 1000);
  5605. } else
  5606. seq_printf(m, "not supported\n");
  5607. return 0;
  5608. }
  5609. static struct ibm_struct thermal_driver_data = {
  5610. .name = "thermal",
  5611. .read = thermal_read,
  5612. };
  5613. /*************************************************************************
  5614. * Backlight/brightness subdriver
  5615. */
  5616. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  5617. /*
  5618. * ThinkPads can read brightness from two places: EC HBRV (0x31), or
  5619. * CMOS NVRAM byte 0x5E, bits 0-3.
  5620. *
  5621. * EC HBRV (0x31) has the following layout
  5622. * Bit 7: unknown function
  5623. * Bit 6: unknown function
  5624. * Bit 5: Z: honour scale changes, NZ: ignore scale changes
  5625. * Bit 4: must be set to zero to avoid problems
  5626. * Bit 3-0: backlight brightness level
  5627. *
  5628. * brightness_get_raw returns status data in the HBRV layout
  5629. *
  5630. * WARNING: The X61 has been verified to use HBRV for something else, so
  5631. * this should be used _only_ on IBM ThinkPads, and maybe with some careful
  5632. * testing on the very early *60 Lenovo models...
  5633. */
  5634. enum {
  5635. TP_EC_BACKLIGHT = 0x31,
  5636. /* TP_EC_BACKLIGHT bitmasks */
  5637. TP_EC_BACKLIGHT_LVLMSK = 0x1F,
  5638. TP_EC_BACKLIGHT_CMDMSK = 0xE0,
  5639. TP_EC_BACKLIGHT_MAPSW = 0x20,
  5640. };
  5641. enum tpacpi_brightness_access_mode {
  5642. TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
  5643. TPACPI_BRGHT_MODE_EC, /* EC control */
  5644. TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
  5645. TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  5646. TPACPI_BRGHT_MODE_MAX
  5647. };
  5648. static struct backlight_device *ibm_backlight_device;
  5649. static enum tpacpi_brightness_access_mode brightness_mode =
  5650. TPACPI_BRGHT_MODE_MAX;
  5651. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  5652. static struct mutex brightness_mutex;
  5653. /* NVRAM brightness access,
  5654. * call with brightness_mutex held! */
  5655. static unsigned int tpacpi_brightness_nvram_get(void)
  5656. {
  5657. u8 lnvram;
  5658. lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  5659. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  5660. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  5661. lnvram &= bright_maxlvl;
  5662. return lnvram;
  5663. }
  5664. static void tpacpi_brightness_checkpoint_nvram(void)
  5665. {
  5666. u8 lec = 0;
  5667. u8 b_nvram;
  5668. if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
  5669. return;
  5670. vdbg_printk(TPACPI_DBG_BRGHT,
  5671. "trying to checkpoint backlight level to NVRAM...\n");
  5672. if (mutex_lock_killable(&brightness_mutex) < 0)
  5673. return;
  5674. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  5675. goto unlock;
  5676. lec &= TP_EC_BACKLIGHT_LVLMSK;
  5677. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  5678. if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  5679. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
  5680. /* NVRAM needs update */
  5681. b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
  5682. TP_NVRAM_POS_LEVEL_BRIGHTNESS);
  5683. b_nvram |= lec;
  5684. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
  5685. dbg_printk(TPACPI_DBG_BRGHT,
  5686. "updated NVRAM backlight level to %u (0x%02x)\n",
  5687. (unsigned int) lec, (unsigned int) b_nvram);
  5688. } else
  5689. vdbg_printk(TPACPI_DBG_BRGHT,
  5690. "NVRAM backlight level already is %u (0x%02x)\n",
  5691. (unsigned int) lec, (unsigned int) b_nvram);
  5692. unlock:
  5693. mutex_unlock(&brightness_mutex);
  5694. }
  5695. /* call with brightness_mutex held! */
  5696. static int tpacpi_brightness_get_raw(int *status)
  5697. {
  5698. u8 lec = 0;
  5699. switch (brightness_mode) {
  5700. case TPACPI_BRGHT_MODE_UCMS_STEP:
  5701. *status = tpacpi_brightness_nvram_get();
  5702. return 0;
  5703. case TPACPI_BRGHT_MODE_EC:
  5704. case TPACPI_BRGHT_MODE_ECNVRAM:
  5705. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  5706. return -EIO;
  5707. *status = lec;
  5708. return 0;
  5709. default:
  5710. return -ENXIO;
  5711. }
  5712. }
  5713. /* call with brightness_mutex held! */
  5714. /* do NOT call with illegal backlight level value */
  5715. static int tpacpi_brightness_set_ec(unsigned int value)
  5716. {
  5717. u8 lec = 0;
  5718. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  5719. return -EIO;
  5720. if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
  5721. (lec & TP_EC_BACKLIGHT_CMDMSK) |
  5722. (value & TP_EC_BACKLIGHT_LVLMSK))))
  5723. return -EIO;
  5724. return 0;
  5725. }
  5726. /* call with brightness_mutex held! */
  5727. static int tpacpi_brightness_set_ucmsstep(unsigned int value)
  5728. {
  5729. int cmos_cmd, inc;
  5730. unsigned int current_value, i;
  5731. current_value = tpacpi_brightness_nvram_get();
  5732. if (value == current_value)
  5733. return 0;
  5734. cmos_cmd = (value > current_value) ?
  5735. TP_CMOS_BRIGHTNESS_UP :
  5736. TP_CMOS_BRIGHTNESS_DOWN;
  5737. inc = (value > current_value) ? 1 : -1;
  5738. for (i = current_value; i != value; i += inc)
  5739. if (issue_thinkpad_cmos_command(cmos_cmd))
  5740. return -EIO;
  5741. return 0;
  5742. }
  5743. /* May return EINTR which can always be mapped to ERESTARTSYS */
  5744. static int brightness_set(unsigned int value)
  5745. {
  5746. int res;
  5747. if (value > bright_maxlvl)
  5748. return -EINVAL;
  5749. vdbg_printk(TPACPI_DBG_BRGHT,
  5750. "set backlight level to %d\n", value);
  5751. res = mutex_lock_killable(&brightness_mutex);
  5752. if (res < 0)
  5753. return res;
  5754. switch (brightness_mode) {
  5755. case TPACPI_BRGHT_MODE_EC:
  5756. case TPACPI_BRGHT_MODE_ECNVRAM:
  5757. res = tpacpi_brightness_set_ec(value);
  5758. break;
  5759. case TPACPI_BRGHT_MODE_UCMS_STEP:
  5760. res = tpacpi_brightness_set_ucmsstep(value);
  5761. break;
  5762. default:
  5763. res = -ENXIO;
  5764. }
  5765. mutex_unlock(&brightness_mutex);
  5766. return res;
  5767. }
  5768. /* sysfs backlight class ----------------------------------------------- */
  5769. static int brightness_update_status(struct backlight_device *bd)
  5770. {
  5771. int level = backlight_get_brightness(bd);
  5772. dbg_printk(TPACPI_DBG_BRGHT,
  5773. "backlight: attempt to set level to %d\n",
  5774. level);
  5775. /* it is the backlight class's job (caller) to handle
  5776. * EINTR and other errors properly */
  5777. return brightness_set(level);
  5778. }
  5779. static int brightness_get(struct backlight_device *bd)
  5780. {
  5781. int status, res;
  5782. res = mutex_lock_killable(&brightness_mutex);
  5783. if (res < 0)
  5784. return 0;
  5785. res = tpacpi_brightness_get_raw(&status);
  5786. mutex_unlock(&brightness_mutex);
  5787. if (res < 0)
  5788. return 0;
  5789. return status & TP_EC_BACKLIGHT_LVLMSK;
  5790. }
  5791. static void tpacpi_brightness_notify_change(void)
  5792. {
  5793. backlight_force_update(ibm_backlight_device,
  5794. BACKLIGHT_UPDATE_HOTKEY);
  5795. }
  5796. static const struct backlight_ops ibm_backlight_data = {
  5797. .get_brightness = brightness_get,
  5798. .update_status = brightness_update_status,
  5799. };
  5800. /* --------------------------------------------------------------------- */
  5801. static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
  5802. {
  5803. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  5804. union acpi_object *obj;
  5805. acpi_status status;
  5806. int rc;
  5807. status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
  5808. if (ACPI_FAILURE(status))
  5809. return 0;
  5810. obj = buffer.pointer;
  5811. if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
  5812. acpi_handle_info(adev->handle,
  5813. "Unknown _BCL data, please report this to %s\n",
  5814. TPACPI_MAIL);
  5815. rc = 0;
  5816. } else {
  5817. rc = obj->package.count;
  5818. }
  5819. kfree(obj);
  5820. return rc;
  5821. }
  5822. /*
  5823. * Call _BCL method of video device. On some ThinkPads this will
  5824. * switch the firmware to the ACPI brightness control mode.
  5825. */
  5826. static int __init tpacpi_query_bcl_levels(acpi_handle handle)
  5827. {
  5828. struct acpi_device *device;
  5829. device = acpi_fetch_acpi_dev(handle);
  5830. if (!device)
  5831. return 0;
  5832. return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL);
  5833. }
  5834. /*
  5835. * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
  5836. */
  5837. static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
  5838. {
  5839. acpi_handle video_device;
  5840. int bcl_levels = 0;
  5841. tpacpi_acpi_handle_locate("video", NULL, &video_device);
  5842. if (video_device)
  5843. bcl_levels = tpacpi_query_bcl_levels(video_device);
  5844. tp_features.bright_acpimode = (bcl_levels > 0);
  5845. return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
  5846. }
  5847. /*
  5848. * These are only useful for models that have only one possibility
  5849. * of GPU. If the BIOS model handles both ATI and Intel, don't use
  5850. * these quirks.
  5851. */
  5852. #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
  5853. #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
  5854. #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
  5855. static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
  5856. /* Models with ATI GPUs known to require ECNVRAM mode */
  5857. TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
  5858. /* Models with ATI GPUs that can use ECNVRAM */
  5859. TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
  5860. TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5861. TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
  5862. TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5863. /* Models with Intel Extreme Graphics 2 */
  5864. TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
  5865. TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5866. TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5867. /* Models with Intel GMA900 */
  5868. TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
  5869. TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
  5870. TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
  5871. };
  5872. /*
  5873. * Returns < 0 for error, otherwise sets tp_features.bright_*
  5874. * and bright_maxlvl.
  5875. */
  5876. static void __init tpacpi_detect_brightness_capabilities(void)
  5877. {
  5878. unsigned int b;
  5879. vdbg_printk(TPACPI_DBG_INIT,
  5880. "detecting firmware brightness interface capabilities\n");
  5881. /* we could run a quirks check here (same table used by
  5882. * brightness_init) if needed */
  5883. /*
  5884. * We always attempt to detect acpi support, so as to switch
  5885. * Lenovo Vista BIOS to ACPI brightness mode even if we are not
  5886. * going to publish a backlight interface
  5887. */
  5888. b = tpacpi_check_std_acpi_brightness_support();
  5889. switch (b) {
  5890. case 16:
  5891. bright_maxlvl = 15;
  5892. break;
  5893. case 8:
  5894. case 0:
  5895. bright_maxlvl = 7;
  5896. break;
  5897. default:
  5898. tp_features.bright_unkfw = 1;
  5899. bright_maxlvl = b - 1;
  5900. }
  5901. pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
  5902. }
  5903. static int __init brightness_init(struct ibm_init_struct *iibm)
  5904. {
  5905. struct backlight_properties props;
  5906. int b;
  5907. unsigned long quirks;
  5908. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  5909. mutex_init(&brightness_mutex);
  5910. quirks = tpacpi_check_quirks(brightness_quirk_table,
  5911. ARRAY_SIZE(brightness_quirk_table));
  5912. /* tpacpi_detect_brightness_capabilities() must have run already */
  5913. /* if it is unknown, we don't handle it: it wouldn't be safe */
  5914. if (tp_features.bright_unkfw)
  5915. return -ENODEV;
  5916. if (!brightness_enable) {
  5917. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5918. "brightness support disabled by module parameter\n");
  5919. return -ENODEV;
  5920. }
  5921. if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
  5922. if (brightness_enable > 1) {
  5923. pr_info("Standard ACPI backlight interface available, not loading native one\n");
  5924. return -ENODEV;
  5925. } else if (brightness_enable == 1) {
  5926. pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
  5927. return -ENODEV;
  5928. }
  5929. } else if (!tp_features.bright_acpimode) {
  5930. pr_notice("ACPI backlight interface not available\n");
  5931. return -ENODEV;
  5932. }
  5933. pr_notice("ACPI native brightness control enabled\n");
  5934. /*
  5935. * Check for module parameter bogosity, note that we
  5936. * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
  5937. * able to detect "unspecified"
  5938. */
  5939. if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
  5940. return -EINVAL;
  5941. /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
  5942. if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
  5943. brightness_mode == TPACPI_BRGHT_MODE_MAX) {
  5944. if (quirks & TPACPI_BRGHT_Q_EC)
  5945. brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
  5946. else
  5947. brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
  5948. dbg_printk(TPACPI_DBG_BRGHT,
  5949. "driver auto-selected brightness_mode=%d\n",
  5950. brightness_mode);
  5951. }
  5952. /* Safety */
  5953. if (!tpacpi_is_ibm() &&
  5954. (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
  5955. brightness_mode == TPACPI_BRGHT_MODE_EC))
  5956. return -EINVAL;
  5957. if (tpacpi_brightness_get_raw(&b) < 0)
  5958. return -ENODEV;
  5959. memset(&props, 0, sizeof(struct backlight_properties));
  5960. props.type = BACKLIGHT_PLATFORM;
  5961. props.max_brightness = bright_maxlvl;
  5962. props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
  5963. ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
  5964. NULL, NULL,
  5965. &ibm_backlight_data,
  5966. &props);
  5967. if (IS_ERR(ibm_backlight_device)) {
  5968. int rc = PTR_ERR(ibm_backlight_device);
  5969. ibm_backlight_device = NULL;
  5970. pr_err("Could not register backlight device\n");
  5971. return rc;
  5972. }
  5973. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5974. "brightness is supported\n");
  5975. if (quirks & TPACPI_BRGHT_Q_ASK) {
  5976. pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
  5977. brightness_mode);
  5978. pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
  5979. TPACPI_MAIL);
  5980. }
  5981. /* Added by mistake in early 2007. Probably useless, but it could
  5982. * be working around some unknown firmware problem where the value
  5983. * read at startup doesn't match the real hardware state... so leave
  5984. * it in place just in case */
  5985. backlight_update_status(ibm_backlight_device);
  5986. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5987. "brightness: registering brightness hotkeys as change notification\n");
  5988. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
  5989. | TP_ACPI_HKEY_BRGHTUP_MASK
  5990. | TP_ACPI_HKEY_BRGHTDWN_MASK);
  5991. return 0;
  5992. }
  5993. static void brightness_suspend(void)
  5994. {
  5995. tpacpi_brightness_checkpoint_nvram();
  5996. }
  5997. static void brightness_shutdown(void)
  5998. {
  5999. tpacpi_brightness_checkpoint_nvram();
  6000. }
  6001. static void brightness_exit(void)
  6002. {
  6003. if (ibm_backlight_device) {
  6004. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
  6005. "calling backlight_device_unregister()\n");
  6006. backlight_device_unregister(ibm_backlight_device);
  6007. }
  6008. tpacpi_brightness_checkpoint_nvram();
  6009. }
  6010. static int brightness_read(struct seq_file *m)
  6011. {
  6012. int level;
  6013. level = brightness_get(NULL);
  6014. if (level < 0) {
  6015. seq_printf(m, "level:\t\tunreadable\n");
  6016. } else {
  6017. seq_printf(m, "level:\t\t%d\n", level);
  6018. seq_printf(m, "commands:\tup, down\n");
  6019. seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
  6020. bright_maxlvl);
  6021. }
  6022. return 0;
  6023. }
  6024. static int brightness_write(char *buf)
  6025. {
  6026. int level;
  6027. int rc;
  6028. char *cmd;
  6029. level = brightness_get(NULL);
  6030. if (level < 0)
  6031. return level;
  6032. while ((cmd = strsep(&buf, ","))) {
  6033. if (strlencmp(cmd, "up") == 0) {
  6034. if (level < bright_maxlvl)
  6035. level++;
  6036. } else if (strlencmp(cmd, "down") == 0) {
  6037. if (level > 0)
  6038. level--;
  6039. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  6040. level >= 0 && level <= bright_maxlvl) {
  6041. /* new level set */
  6042. } else
  6043. return -EINVAL;
  6044. }
  6045. tpacpi_disclose_usertask("procfs brightness",
  6046. "set level to %d\n", level);
  6047. /*
  6048. * Now we know what the final level should be, so we try to set it.
  6049. * Doing it this way makes the syscall restartable in case of EINTR
  6050. */
  6051. rc = brightness_set(level);
  6052. if (!rc && ibm_backlight_device)
  6053. backlight_force_update(ibm_backlight_device,
  6054. BACKLIGHT_UPDATE_SYSFS);
  6055. return (rc == -EINTR) ? -ERESTARTSYS : rc;
  6056. }
  6057. static struct ibm_struct brightness_driver_data = {
  6058. .name = "brightness",
  6059. .read = brightness_read,
  6060. .write = brightness_write,
  6061. .exit = brightness_exit,
  6062. .suspend = brightness_suspend,
  6063. .shutdown = brightness_shutdown,
  6064. };
  6065. /*************************************************************************
  6066. * Volume subdriver
  6067. */
  6068. /*
  6069. * IBM ThinkPads have a simple volume controller with MUTE gating.
  6070. * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
  6071. *
  6072. * Since the *61 series (and probably also the later *60 series), Lenovo
  6073. * ThinkPads only implement the MUTE gate.
  6074. *
  6075. * EC register 0x30
  6076. * Bit 6: MUTE (1 mutes sound)
  6077. * Bit 3-0: Volume
  6078. * Other bits should be zero as far as we know.
  6079. *
  6080. * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
  6081. * bits 3-0 (volume). Other bits in NVRAM may have other functions,
  6082. * such as bit 7 which is used to detect repeated presses of MUTE,
  6083. * and we leave them unchanged.
  6084. *
  6085. * On newer Lenovo ThinkPads, the EC can automatically change the volume
  6086. * in response to user input. Unfortunately, this rarely works well.
  6087. * The laptop changes the state of its internal MUTE gate and, on some
  6088. * models, sends KEY_MUTE, causing any user code that responds to the
  6089. * mute button to get confused. The hardware MUTE gate is also
  6090. * unnecessary, since user code can handle the mute button without
  6091. * kernel or EC help.
  6092. *
  6093. * To avoid confusing userspace, we simply disable all EC-based mute
  6094. * and volume controls when possible.
  6095. */
  6096. #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
  6097. #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
  6098. #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
  6099. #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
  6100. #if SNDRV_CARDS <= 32
  6101. #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
  6102. #else
  6103. #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
  6104. #endif
  6105. static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
  6106. static char *alsa_id = "ThinkPadEC";
  6107. static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
  6108. struct tpacpi_alsa_data {
  6109. struct snd_card *card;
  6110. struct snd_ctl_elem_id *ctl_mute_id;
  6111. struct snd_ctl_elem_id *ctl_vol_id;
  6112. };
  6113. static struct snd_card *alsa_card;
  6114. enum {
  6115. TP_EC_AUDIO = 0x30,
  6116. /* TP_EC_AUDIO bits */
  6117. TP_EC_AUDIO_MUTESW = 6,
  6118. /* TP_EC_AUDIO bitmasks */
  6119. TP_EC_AUDIO_LVL_MSK = 0x0F,
  6120. TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
  6121. /* Maximum volume */
  6122. TP_EC_VOLUME_MAX = 14,
  6123. };
  6124. enum tpacpi_volume_access_mode {
  6125. TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
  6126. TPACPI_VOL_MODE_EC, /* Pure EC control */
  6127. TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
  6128. TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  6129. TPACPI_VOL_MODE_MAX
  6130. };
  6131. enum tpacpi_volume_capabilities {
  6132. TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
  6133. TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
  6134. TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
  6135. TPACPI_VOL_CAP_MAX
  6136. };
  6137. enum tpacpi_mute_btn_mode {
  6138. TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
  6139. /* We don't know what mode 1 is. */
  6140. TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
  6141. TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
  6142. };
  6143. static enum tpacpi_volume_access_mode volume_mode =
  6144. TPACPI_VOL_MODE_MAX;
  6145. static enum tpacpi_volume_capabilities volume_capabilities;
  6146. static bool volume_control_allowed;
  6147. static bool software_mute_requested = true;
  6148. static bool software_mute_active;
  6149. static int software_mute_orig_mode;
  6150. /*
  6151. * Used to syncronize writers to TP_EC_AUDIO and
  6152. * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
  6153. */
  6154. static struct mutex volume_mutex;
  6155. static void tpacpi_volume_checkpoint_nvram(void)
  6156. {
  6157. u8 lec = 0;
  6158. u8 b_nvram;
  6159. u8 ec_mask;
  6160. if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
  6161. return;
  6162. if (!volume_control_allowed)
  6163. return;
  6164. if (software_mute_active)
  6165. return;
  6166. vdbg_printk(TPACPI_DBG_MIXER,
  6167. "trying to checkpoint mixer state to NVRAM...\n");
  6168. if (tp_features.mixer_no_level_control)
  6169. ec_mask = TP_EC_AUDIO_MUTESW_MSK;
  6170. else
  6171. ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
  6172. if (mutex_lock_killable(&volume_mutex) < 0)
  6173. return;
  6174. if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
  6175. goto unlock;
  6176. lec &= ec_mask;
  6177. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  6178. if (lec != (b_nvram & ec_mask)) {
  6179. /* NVRAM needs update */
  6180. b_nvram &= ~ec_mask;
  6181. b_nvram |= lec;
  6182. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
  6183. dbg_printk(TPACPI_DBG_MIXER,
  6184. "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
  6185. (unsigned int) lec, (unsigned int) b_nvram);
  6186. } else {
  6187. vdbg_printk(TPACPI_DBG_MIXER,
  6188. "NVRAM mixer status already is 0x%02x (0x%02x)\n",
  6189. (unsigned int) lec, (unsigned int) b_nvram);
  6190. }
  6191. unlock:
  6192. mutex_unlock(&volume_mutex);
  6193. }
  6194. static int volume_get_status_ec(u8 *status)
  6195. {
  6196. u8 s;
  6197. if (!acpi_ec_read(TP_EC_AUDIO, &s))
  6198. return -EIO;
  6199. *status = s;
  6200. dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
  6201. return 0;
  6202. }
  6203. static int volume_get_status(u8 *status)
  6204. {
  6205. return volume_get_status_ec(status);
  6206. }
  6207. static int volume_set_status_ec(const u8 status)
  6208. {
  6209. if (!acpi_ec_write(TP_EC_AUDIO, status))
  6210. return -EIO;
  6211. dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
  6212. /*
  6213. * On X200s, and possibly on others, it can take a while for
  6214. * reads to become correct.
  6215. */
  6216. msleep(1);
  6217. return 0;
  6218. }
  6219. static int volume_set_status(const u8 status)
  6220. {
  6221. return volume_set_status_ec(status);
  6222. }
  6223. /* returns < 0 on error, 0 on no change, 1 on change */
  6224. static int __volume_set_mute_ec(const bool mute)
  6225. {
  6226. int rc;
  6227. u8 s, n;
  6228. if (mutex_lock_killable(&volume_mutex) < 0)
  6229. return -EINTR;
  6230. rc = volume_get_status_ec(&s);
  6231. if (rc)
  6232. goto unlock;
  6233. n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
  6234. s & ~TP_EC_AUDIO_MUTESW_MSK;
  6235. if (n != s) {
  6236. rc = volume_set_status_ec(n);
  6237. if (!rc)
  6238. rc = 1;
  6239. }
  6240. unlock:
  6241. mutex_unlock(&volume_mutex);
  6242. return rc;
  6243. }
  6244. static int volume_alsa_set_mute(const bool mute)
  6245. {
  6246. dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
  6247. (mute) ? "" : "un");
  6248. return __volume_set_mute_ec(mute);
  6249. }
  6250. static int volume_set_mute(const bool mute)
  6251. {
  6252. int rc;
  6253. dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
  6254. (mute) ? "" : "un");
  6255. rc = __volume_set_mute_ec(mute);
  6256. return (rc < 0) ? rc : 0;
  6257. }
  6258. /* returns < 0 on error, 0 on no change, 1 on change */
  6259. static int __volume_set_volume_ec(const u8 vol)
  6260. {
  6261. int rc;
  6262. u8 s, n;
  6263. if (vol > TP_EC_VOLUME_MAX)
  6264. return -EINVAL;
  6265. if (mutex_lock_killable(&volume_mutex) < 0)
  6266. return -EINTR;
  6267. rc = volume_get_status_ec(&s);
  6268. if (rc)
  6269. goto unlock;
  6270. n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
  6271. if (n != s) {
  6272. rc = volume_set_status_ec(n);
  6273. if (!rc)
  6274. rc = 1;
  6275. }
  6276. unlock:
  6277. mutex_unlock(&volume_mutex);
  6278. return rc;
  6279. }
  6280. static int volume_set_software_mute(bool startup)
  6281. {
  6282. int result;
  6283. if (!tpacpi_is_lenovo())
  6284. return -ENODEV;
  6285. if (startup) {
  6286. if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
  6287. "HAUM", "qd"))
  6288. return -EIO;
  6289. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6290. "Initial HAUM setting was %d\n",
  6291. software_mute_orig_mode);
  6292. }
  6293. if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
  6294. (int)TP_EC_MUTE_BTN_NONE))
  6295. return -EIO;
  6296. if (result != TP_EC_MUTE_BTN_NONE)
  6297. pr_warn("Unexpected SAUM result %d\n",
  6298. result);
  6299. /*
  6300. * In software mute mode, the standard codec controls take
  6301. * precendence, so we unmute the ThinkPad HW switch at
  6302. * startup. Just on case there are SAUM-capable ThinkPads
  6303. * with level controls, set max HW volume as well.
  6304. */
  6305. if (tp_features.mixer_no_level_control)
  6306. result = volume_set_mute(false);
  6307. else
  6308. result = volume_set_status(TP_EC_VOLUME_MAX);
  6309. if (result != 0)
  6310. pr_warn("Failed to unmute the HW mute switch\n");
  6311. return 0;
  6312. }
  6313. static void volume_exit_software_mute(void)
  6314. {
  6315. int r;
  6316. if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
  6317. || r != software_mute_orig_mode)
  6318. pr_warn("Failed to restore mute mode\n");
  6319. }
  6320. static int volume_alsa_set_volume(const u8 vol)
  6321. {
  6322. dbg_printk(TPACPI_DBG_MIXER,
  6323. "ALSA: trying to set volume level to %hu\n", vol);
  6324. return __volume_set_volume_ec(vol);
  6325. }
  6326. static void volume_alsa_notify_change(void)
  6327. {
  6328. struct tpacpi_alsa_data *d;
  6329. if (alsa_card && alsa_card->private_data) {
  6330. d = alsa_card->private_data;
  6331. if (d->ctl_mute_id)
  6332. snd_ctl_notify(alsa_card,
  6333. SNDRV_CTL_EVENT_MASK_VALUE,
  6334. d->ctl_mute_id);
  6335. if (d->ctl_vol_id)
  6336. snd_ctl_notify(alsa_card,
  6337. SNDRV_CTL_EVENT_MASK_VALUE,
  6338. d->ctl_vol_id);
  6339. }
  6340. }
  6341. static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
  6342. struct snd_ctl_elem_info *uinfo)
  6343. {
  6344. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  6345. uinfo->count = 1;
  6346. uinfo->value.integer.min = 0;
  6347. uinfo->value.integer.max = TP_EC_VOLUME_MAX;
  6348. return 0;
  6349. }
  6350. static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
  6351. struct snd_ctl_elem_value *ucontrol)
  6352. {
  6353. u8 s;
  6354. int rc;
  6355. rc = volume_get_status(&s);
  6356. if (rc < 0)
  6357. return rc;
  6358. ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
  6359. return 0;
  6360. }
  6361. static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
  6362. struct snd_ctl_elem_value *ucontrol)
  6363. {
  6364. tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
  6365. ucontrol->value.integer.value[0]);
  6366. return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
  6367. }
  6368. #define volume_alsa_mute_info snd_ctl_boolean_mono_info
  6369. static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
  6370. struct snd_ctl_elem_value *ucontrol)
  6371. {
  6372. u8 s;
  6373. int rc;
  6374. rc = volume_get_status(&s);
  6375. if (rc < 0)
  6376. return rc;
  6377. ucontrol->value.integer.value[0] =
  6378. (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
  6379. return 0;
  6380. }
  6381. static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
  6382. struct snd_ctl_elem_value *ucontrol)
  6383. {
  6384. tpacpi_disclose_usertask("ALSA", "%smute\n",
  6385. ucontrol->value.integer.value[0] ?
  6386. "un" : "");
  6387. return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
  6388. }
  6389. static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
  6390. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6391. .name = "Console Playback Volume",
  6392. .index = 0,
  6393. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  6394. .info = volume_alsa_vol_info,
  6395. .get = volume_alsa_vol_get,
  6396. };
  6397. static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
  6398. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6399. .name = "Console Playback Switch",
  6400. .index = 0,
  6401. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  6402. .info = volume_alsa_mute_info,
  6403. .get = volume_alsa_mute_get,
  6404. };
  6405. static void volume_suspend(void)
  6406. {
  6407. tpacpi_volume_checkpoint_nvram();
  6408. }
  6409. static void volume_resume(void)
  6410. {
  6411. if (software_mute_active) {
  6412. if (volume_set_software_mute(false) < 0)
  6413. pr_warn("Failed to restore software mute\n");
  6414. } else {
  6415. volume_alsa_notify_change();
  6416. }
  6417. }
  6418. static void volume_shutdown(void)
  6419. {
  6420. tpacpi_volume_checkpoint_nvram();
  6421. }
  6422. static void volume_exit(void)
  6423. {
  6424. if (alsa_card) {
  6425. snd_card_free(alsa_card);
  6426. alsa_card = NULL;
  6427. }
  6428. tpacpi_volume_checkpoint_nvram();
  6429. if (software_mute_active)
  6430. volume_exit_software_mute();
  6431. }
  6432. static int __init volume_create_alsa_mixer(void)
  6433. {
  6434. struct snd_card *card;
  6435. struct tpacpi_alsa_data *data;
  6436. struct snd_kcontrol *ctl_vol;
  6437. struct snd_kcontrol *ctl_mute;
  6438. int rc;
  6439. rc = snd_card_new(&tpacpi_pdev->dev,
  6440. alsa_index, alsa_id, THIS_MODULE,
  6441. sizeof(struct tpacpi_alsa_data), &card);
  6442. if (rc < 0 || !card) {
  6443. pr_err("Failed to create ALSA card structures: %d\n", rc);
  6444. return -ENODEV;
  6445. }
  6446. BUG_ON(!card->private_data);
  6447. data = card->private_data;
  6448. data->card = card;
  6449. strscpy(card->driver, TPACPI_ALSA_DRVNAME,
  6450. sizeof(card->driver));
  6451. strscpy(card->shortname, TPACPI_ALSA_SHRTNAME,
  6452. sizeof(card->shortname));
  6453. snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
  6454. (thinkpad_id.ec_version_str) ?
  6455. thinkpad_id.ec_version_str : "(unknown)");
  6456. snprintf(card->longname, sizeof(card->longname),
  6457. "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
  6458. (thinkpad_id.ec_version_str) ?
  6459. thinkpad_id.ec_version_str : "unknown");
  6460. if (volume_control_allowed) {
  6461. volume_alsa_control_vol.put = volume_alsa_vol_put;
  6462. volume_alsa_control_vol.access =
  6463. SNDRV_CTL_ELEM_ACCESS_READWRITE;
  6464. volume_alsa_control_mute.put = volume_alsa_mute_put;
  6465. volume_alsa_control_mute.access =
  6466. SNDRV_CTL_ELEM_ACCESS_READWRITE;
  6467. }
  6468. if (!tp_features.mixer_no_level_control) {
  6469. ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
  6470. rc = snd_ctl_add(card, ctl_vol);
  6471. if (rc < 0) {
  6472. pr_err("Failed to create ALSA volume control: %d\n",
  6473. rc);
  6474. goto err_exit;
  6475. }
  6476. data->ctl_vol_id = &ctl_vol->id;
  6477. }
  6478. ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
  6479. rc = snd_ctl_add(card, ctl_mute);
  6480. if (rc < 0) {
  6481. pr_err("Failed to create ALSA mute control: %d\n", rc);
  6482. goto err_exit;
  6483. }
  6484. data->ctl_mute_id = &ctl_mute->id;
  6485. rc = snd_card_register(card);
  6486. if (rc < 0) {
  6487. pr_err("Failed to register ALSA card: %d\n", rc);
  6488. goto err_exit;
  6489. }
  6490. alsa_card = card;
  6491. return 0;
  6492. err_exit:
  6493. snd_card_free(card);
  6494. return -ENODEV;
  6495. }
  6496. #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
  6497. #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
  6498. static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
  6499. /* Whitelist volume level on all IBM by default */
  6500. { .vendor = PCI_VENDOR_ID_IBM,
  6501. .bios = TPACPI_MATCH_ANY,
  6502. .ec = TPACPI_MATCH_ANY,
  6503. .quirks = TPACPI_VOL_Q_LEVEL },
  6504. /* Lenovo models with volume control (needs confirmation) */
  6505. TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
  6506. TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
  6507. TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
  6508. TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
  6509. TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
  6510. TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
  6511. TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
  6512. /* Whitelist mute-only on all Lenovo by default */
  6513. { .vendor = PCI_VENDOR_ID_LENOVO,
  6514. .bios = TPACPI_MATCH_ANY,
  6515. .ec = TPACPI_MATCH_ANY,
  6516. .quirks = TPACPI_VOL_Q_MUTEONLY }
  6517. };
  6518. static int __init volume_init(struct ibm_init_struct *iibm)
  6519. {
  6520. unsigned long quirks;
  6521. int rc;
  6522. vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
  6523. mutex_init(&volume_mutex);
  6524. /*
  6525. * Check for module parameter bogosity, note that we
  6526. * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
  6527. * able to detect "unspecified"
  6528. */
  6529. if (volume_mode > TPACPI_VOL_MODE_MAX)
  6530. return -EINVAL;
  6531. if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
  6532. pr_err("UCMS step volume mode not implemented, please contact %s\n",
  6533. TPACPI_MAIL);
  6534. return -ENODEV;
  6535. }
  6536. if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
  6537. return -EINVAL;
  6538. /*
  6539. * The ALSA mixer is our primary interface.
  6540. * When disabled, don't install the subdriver at all
  6541. */
  6542. if (!alsa_enable) {
  6543. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6544. "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
  6545. return -ENODEV;
  6546. }
  6547. quirks = tpacpi_check_quirks(volume_quirk_table,
  6548. ARRAY_SIZE(volume_quirk_table));
  6549. switch (volume_capabilities) {
  6550. case TPACPI_VOL_CAP_AUTO:
  6551. if (quirks & TPACPI_VOL_Q_MUTEONLY)
  6552. tp_features.mixer_no_level_control = 1;
  6553. else if (quirks & TPACPI_VOL_Q_LEVEL)
  6554. tp_features.mixer_no_level_control = 0;
  6555. else
  6556. return -ENODEV; /* no mixer */
  6557. break;
  6558. case TPACPI_VOL_CAP_VOLMUTE:
  6559. tp_features.mixer_no_level_control = 0;
  6560. break;
  6561. case TPACPI_VOL_CAP_MUTEONLY:
  6562. tp_features.mixer_no_level_control = 1;
  6563. break;
  6564. default:
  6565. return -ENODEV;
  6566. }
  6567. if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
  6568. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6569. "using user-supplied volume_capabilities=%d\n",
  6570. volume_capabilities);
  6571. if (volume_mode == TPACPI_VOL_MODE_AUTO ||
  6572. volume_mode == TPACPI_VOL_MODE_MAX) {
  6573. volume_mode = TPACPI_VOL_MODE_ECNVRAM;
  6574. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6575. "driver auto-selected volume_mode=%d\n",
  6576. volume_mode);
  6577. } else {
  6578. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6579. "using user-supplied volume_mode=%d\n",
  6580. volume_mode);
  6581. }
  6582. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6583. "mute is supported, volume control is %s\n",
  6584. str_supported(!tp_features.mixer_no_level_control));
  6585. if (software_mute_requested && volume_set_software_mute(true) == 0) {
  6586. software_mute_active = true;
  6587. } else {
  6588. rc = volume_create_alsa_mixer();
  6589. if (rc) {
  6590. pr_err("Could not create the ALSA mixer interface\n");
  6591. return rc;
  6592. }
  6593. pr_info("Console audio control enabled, mode: %s\n",
  6594. (volume_control_allowed) ?
  6595. "override (read/write)" :
  6596. "monitor (read only)");
  6597. }
  6598. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6599. "registering volume hotkeys as change notification\n");
  6600. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
  6601. | TP_ACPI_HKEY_VOLUP_MASK
  6602. | TP_ACPI_HKEY_VOLDWN_MASK
  6603. | TP_ACPI_HKEY_MUTE_MASK);
  6604. return 0;
  6605. }
  6606. static int volume_read(struct seq_file *m)
  6607. {
  6608. u8 status;
  6609. if (volume_get_status(&status) < 0) {
  6610. seq_printf(m, "level:\t\tunreadable\n");
  6611. } else {
  6612. if (tp_features.mixer_no_level_control)
  6613. seq_printf(m, "level:\t\tunsupported\n");
  6614. else
  6615. seq_printf(m, "level:\t\t%d\n",
  6616. status & TP_EC_AUDIO_LVL_MSK);
  6617. seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW)));
  6618. if (volume_control_allowed) {
  6619. seq_printf(m, "commands:\tunmute, mute\n");
  6620. if (!tp_features.mixer_no_level_control) {
  6621. seq_printf(m, "commands:\tup, down\n");
  6622. seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
  6623. TP_EC_VOLUME_MAX);
  6624. }
  6625. }
  6626. }
  6627. return 0;
  6628. }
  6629. static int volume_write(char *buf)
  6630. {
  6631. u8 s;
  6632. u8 new_level, new_mute;
  6633. int l;
  6634. char *cmd;
  6635. int rc;
  6636. /*
  6637. * We do allow volume control at driver startup, so that the
  6638. * user can set initial state through the volume=... parameter hack.
  6639. */
  6640. if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
  6641. if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
  6642. tp_warned.volume_ctrl_forbidden = 1;
  6643. pr_notice("Console audio control in monitor mode, changes are not allowed\n");
  6644. pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
  6645. }
  6646. return -EPERM;
  6647. }
  6648. rc = volume_get_status(&s);
  6649. if (rc < 0)
  6650. return rc;
  6651. new_level = s & TP_EC_AUDIO_LVL_MSK;
  6652. new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
  6653. while ((cmd = strsep(&buf, ","))) {
  6654. if (!tp_features.mixer_no_level_control) {
  6655. if (strlencmp(cmd, "up") == 0) {
  6656. if (new_mute)
  6657. new_mute = 0;
  6658. else if (new_level < TP_EC_VOLUME_MAX)
  6659. new_level++;
  6660. continue;
  6661. } else if (strlencmp(cmd, "down") == 0) {
  6662. if (new_mute)
  6663. new_mute = 0;
  6664. else if (new_level > 0)
  6665. new_level--;
  6666. continue;
  6667. } else if (sscanf(cmd, "level %u", &l) == 1 &&
  6668. l >= 0 && l <= TP_EC_VOLUME_MAX) {
  6669. new_level = l;
  6670. continue;
  6671. }
  6672. }
  6673. if (strlencmp(cmd, "mute") == 0)
  6674. new_mute = TP_EC_AUDIO_MUTESW_MSK;
  6675. else if (strlencmp(cmd, "unmute") == 0)
  6676. new_mute = 0;
  6677. else
  6678. return -EINVAL;
  6679. }
  6680. if (tp_features.mixer_no_level_control) {
  6681. tpacpi_disclose_usertask("procfs volume", "%smute\n",
  6682. new_mute ? "" : "un");
  6683. rc = volume_set_mute(!!new_mute);
  6684. } else {
  6685. tpacpi_disclose_usertask("procfs volume",
  6686. "%smute and set level to %d\n",
  6687. new_mute ? "" : "un", new_level);
  6688. rc = volume_set_status(new_mute | new_level);
  6689. }
  6690. volume_alsa_notify_change();
  6691. return (rc == -EINTR) ? -ERESTARTSYS : rc;
  6692. }
  6693. static struct ibm_struct volume_driver_data = {
  6694. .name = "volume",
  6695. .read = volume_read,
  6696. .write = volume_write,
  6697. .exit = volume_exit,
  6698. .suspend = volume_suspend,
  6699. .resume = volume_resume,
  6700. .shutdown = volume_shutdown,
  6701. };
  6702. #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  6703. #define alsa_card NULL
  6704. static inline void volume_alsa_notify_change(void)
  6705. {
  6706. }
  6707. static int __init volume_init(struct ibm_init_struct *iibm)
  6708. {
  6709. pr_info("volume: disabled as there is no ALSA support in this kernel\n");
  6710. return -ENODEV;
  6711. }
  6712. static struct ibm_struct volume_driver_data = {
  6713. .name = "volume",
  6714. };
  6715. #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  6716. /*************************************************************************
  6717. * Fan subdriver
  6718. */
  6719. /*
  6720. * FAN ACCESS MODES
  6721. *
  6722. * TPACPI_FAN_RD_ACPI_GFAN:
  6723. * ACPI GFAN method: returns fan level
  6724. *
  6725. * see TPACPI_FAN_WR_ACPI_SFAN
  6726. * EC 0x2f (HFSP) not available if GFAN exists
  6727. *
  6728. * TPACPI_FAN_WR_ACPI_SFAN:
  6729. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  6730. *
  6731. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  6732. * it for writing.
  6733. *
  6734. * TPACPI_FAN_WR_TPEC:
  6735. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  6736. * Supported on almost all ThinkPads
  6737. *
  6738. * Fan speed changes of any sort (including those caused by the
  6739. * disengaged mode) are usually done slowly by the firmware as the
  6740. * maximum amount of fan duty cycle change per second seems to be
  6741. * limited.
  6742. *
  6743. * Reading is not available if GFAN exists.
  6744. * Writing is not available if SFAN exists.
  6745. *
  6746. * Bits
  6747. * 7 automatic mode engaged;
  6748. * (default operation mode of the ThinkPad)
  6749. * fan level is ignored in this mode.
  6750. * 6 full speed mode (takes precedence over bit 7);
  6751. * not available on all thinkpads. May disable
  6752. * the tachometer while the fan controller ramps up
  6753. * the speed (which can take up to a few *minutes*).
  6754. * Speeds up fan to 100% duty-cycle, which is far above
  6755. * the standard RPM levels. It is not impossible that
  6756. * it could cause hardware damage.
  6757. * 5-3 unused in some models. Extra bits for fan level
  6758. * in others, but still useless as all values above
  6759. * 7 map to the same speed as level 7 in these models.
  6760. * 2-0 fan level (0..7 usually)
  6761. * 0x00 = stop
  6762. * 0x07 = max (set when temperatures critical)
  6763. * Some ThinkPads may have other levels, see
  6764. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  6765. *
  6766. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  6767. * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
  6768. * does so, its initial value is meaningless (0x07).
  6769. *
  6770. * For firmware bugs, refer to:
  6771. * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  6772. *
  6773. * ----
  6774. *
  6775. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  6776. * Main fan tachometer reading (in RPM)
  6777. *
  6778. * This register is present on all ThinkPads with a new-style EC, and
  6779. * it is known not to be present on the A21m/e, and T22, as there is
  6780. * something else in offset 0x84 according to the ACPI DSDT. Other
  6781. * ThinkPads from this same time period (and earlier) probably lack the
  6782. * tachometer as well.
  6783. *
  6784. * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
  6785. * was never fixed by IBM to report the EC firmware version string
  6786. * probably support the tachometer (like the early X models), so
  6787. * detecting it is quite hard. We need more data to know for sure.
  6788. *
  6789. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  6790. * might result.
  6791. *
  6792. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  6793. * mode.
  6794. *
  6795. * For firmware bugs, refer to:
  6796. * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  6797. *
  6798. * ----
  6799. *
  6800. * ThinkPad EC register 0x31 bit 0 (only on select models)
  6801. *
  6802. * When bit 0 of EC register 0x31 is zero, the tachometer registers
  6803. * show the speed of the main fan. When bit 0 of EC register 0x31
  6804. * is one, the tachometer registers show the speed of the auxiliary
  6805. * fan.
  6806. *
  6807. * Fan control seems to affect both fans, regardless of the state
  6808. * of this bit.
  6809. *
  6810. * So far, only the firmware for the X60/X61 non-tablet versions
  6811. * seem to support this (firmware TP-7M).
  6812. *
  6813. * TPACPI_FAN_WR_ACPI_FANS:
  6814. * ThinkPad X31, X40, X41. Not available in the X60.
  6815. *
  6816. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  6817. * high speed. ACPI DSDT seems to map these three speeds to levels
  6818. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  6819. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  6820. *
  6821. * The speeds are stored on handles
  6822. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  6823. *
  6824. * There are three default speed sets, accessible as handles:
  6825. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  6826. *
  6827. * ACPI DSDT switches which set is in use depending on various
  6828. * factors.
  6829. *
  6830. * TPACPI_FAN_WR_TPEC is also available and should be used to
  6831. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  6832. * but the ACPI tables just mention level 7.
  6833. */
  6834. enum { /* Fan control constants */
  6835. fan_status_offset = 0x2f, /* EC register 0x2f */
  6836. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  6837. * 0x84 must be read before 0x85 */
  6838. fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
  6839. bit 0 selects which fan is active */
  6840. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  6841. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  6842. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  6843. };
  6844. enum fan_status_access_mode {
  6845. TPACPI_FAN_NONE = 0, /* No fan status or control */
  6846. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  6847. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  6848. };
  6849. enum fan_control_access_mode {
  6850. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  6851. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  6852. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  6853. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  6854. };
  6855. enum fan_control_commands {
  6856. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  6857. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  6858. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  6859. * and also watchdog cmd */
  6860. };
  6861. static bool fan_control_allowed;
  6862. static enum fan_status_access_mode fan_status_access_mode;
  6863. static enum fan_control_access_mode fan_control_access_mode;
  6864. static enum fan_control_commands fan_control_commands;
  6865. static u8 fan_control_initial_status;
  6866. static u8 fan_control_desired_level;
  6867. static u8 fan_control_resume_level;
  6868. static int fan_watchdog_maxinterval;
  6869. static struct mutex fan_mutex;
  6870. static void fan_watchdog_fire(struct work_struct *ignored);
  6871. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  6872. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  6873. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  6874. "\\FSPD", /* 600e/x, 770e, 770x */
  6875. ); /* all others */
  6876. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  6877. "JFNS", /* 770x-JL */
  6878. ); /* all others */
  6879. /*
  6880. * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
  6881. * HFSP register at boot, so it contains 0x07 but the Thinkpad could
  6882. * be in auto mode (0x80).
  6883. *
  6884. * This is corrected by any write to HFSP either by the driver, or
  6885. * by the firmware.
  6886. *
  6887. * We assume 0x07 really means auto mode while this quirk is active,
  6888. * as this is far more likely than the ThinkPad being in level 7,
  6889. * which is only used by the firmware during thermal emergencies.
  6890. *
  6891. * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
  6892. * TP-70 (T43, R52), which are known to be buggy.
  6893. */
  6894. static void fan_quirk1_setup(void)
  6895. {
  6896. if (fan_control_initial_status == 0x07) {
  6897. pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
  6898. tp_features.fan_ctrl_status_undef = 1;
  6899. }
  6900. }
  6901. static void fan_quirk1_handle(u8 *fan_status)
  6902. {
  6903. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  6904. if (*fan_status != fan_control_initial_status) {
  6905. /* something changed the HFSP regisnter since
  6906. * driver init time, so it is not undefined
  6907. * anymore */
  6908. tp_features.fan_ctrl_status_undef = 0;
  6909. } else {
  6910. /* Return most likely status. In fact, it
  6911. * might be the only possible status */
  6912. *fan_status = TP_EC_FAN_AUTO;
  6913. }
  6914. }
  6915. }
  6916. /* Select main fan on X60/X61, NOOP on others */
  6917. static bool fan_select_fan1(void)
  6918. {
  6919. if (tp_features.second_fan) {
  6920. u8 val;
  6921. if (ec_read(fan_select_offset, &val) < 0)
  6922. return false;
  6923. val &= 0xFEU;
  6924. if (ec_write(fan_select_offset, val) < 0)
  6925. return false;
  6926. }
  6927. return true;
  6928. }
  6929. /* Select secondary fan on X60/X61 */
  6930. static bool fan_select_fan2(void)
  6931. {
  6932. u8 val;
  6933. if (!tp_features.second_fan)
  6934. return false;
  6935. if (ec_read(fan_select_offset, &val) < 0)
  6936. return false;
  6937. val |= 0x01U;
  6938. if (ec_write(fan_select_offset, val) < 0)
  6939. return false;
  6940. return true;
  6941. }
  6942. /*
  6943. * Call with fan_mutex held
  6944. */
  6945. static void fan_update_desired_level(u8 status)
  6946. {
  6947. if ((status &
  6948. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  6949. if (status > 7)
  6950. fan_control_desired_level = 7;
  6951. else
  6952. fan_control_desired_level = status;
  6953. }
  6954. }
  6955. static int fan_get_status(u8 *status)
  6956. {
  6957. u8 s;
  6958. /* TODO:
  6959. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  6960. switch (fan_status_access_mode) {
  6961. case TPACPI_FAN_RD_ACPI_GFAN: {
  6962. /* 570, 600e/x, 770e, 770x */
  6963. int res;
  6964. if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
  6965. return -EIO;
  6966. if (likely(status))
  6967. *status = res & 0x07;
  6968. break;
  6969. }
  6970. case TPACPI_FAN_RD_TPEC:
  6971. /* all except 570, 600e/x, 770e, 770x */
  6972. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  6973. return -EIO;
  6974. if (likely(status)) {
  6975. *status = s;
  6976. fan_quirk1_handle(status);
  6977. }
  6978. break;
  6979. default:
  6980. return -ENXIO;
  6981. }
  6982. return 0;
  6983. }
  6984. static int fan_get_status_safe(u8 *status)
  6985. {
  6986. int rc;
  6987. u8 s;
  6988. if (mutex_lock_killable(&fan_mutex))
  6989. return -ERESTARTSYS;
  6990. rc = fan_get_status(&s);
  6991. if (!rc)
  6992. fan_update_desired_level(s);
  6993. mutex_unlock(&fan_mutex);
  6994. if (rc)
  6995. return rc;
  6996. if (status)
  6997. *status = s;
  6998. return 0;
  6999. }
  7000. static int fan_get_speed(unsigned int *speed)
  7001. {
  7002. u8 hi, lo;
  7003. switch (fan_status_access_mode) {
  7004. case TPACPI_FAN_RD_TPEC:
  7005. /* all except 570, 600e/x, 770e, 770x */
  7006. if (unlikely(!fan_select_fan1()))
  7007. return -EIO;
  7008. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  7009. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  7010. return -EIO;
  7011. if (likely(speed))
  7012. *speed = (hi << 8) | lo;
  7013. break;
  7014. default:
  7015. return -ENXIO;
  7016. }
  7017. return 0;
  7018. }
  7019. static int fan2_get_speed(unsigned int *speed)
  7020. {
  7021. u8 hi, lo;
  7022. bool rc;
  7023. switch (fan_status_access_mode) {
  7024. case TPACPI_FAN_RD_TPEC:
  7025. /* all except 570, 600e/x, 770e, 770x */
  7026. if (unlikely(!fan_select_fan2()))
  7027. return -EIO;
  7028. rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
  7029. !acpi_ec_read(fan_rpm_offset + 1, &hi);
  7030. fan_select_fan1(); /* play it safe */
  7031. if (rc)
  7032. return -EIO;
  7033. if (likely(speed))
  7034. *speed = (hi << 8) | lo;
  7035. break;
  7036. default:
  7037. return -ENXIO;
  7038. }
  7039. return 0;
  7040. }
  7041. static int fan_set_level(int level)
  7042. {
  7043. if (!fan_control_allowed)
  7044. return -EPERM;
  7045. switch (fan_control_access_mode) {
  7046. case TPACPI_FAN_WR_ACPI_SFAN:
  7047. if ((level < 0) || (level > 7))
  7048. return -EINVAL;
  7049. if (tp_features.second_fan_ctl) {
  7050. if (!fan_select_fan2() ||
  7051. !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
  7052. pr_warn("Couldn't set 2nd fan level, disabling support\n");
  7053. tp_features.second_fan_ctl = 0;
  7054. }
  7055. fan_select_fan1();
  7056. }
  7057. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  7058. return -EIO;
  7059. break;
  7060. case TPACPI_FAN_WR_ACPI_FANS:
  7061. case TPACPI_FAN_WR_TPEC:
  7062. if (!(level & TP_EC_FAN_AUTO) &&
  7063. !(level & TP_EC_FAN_FULLSPEED) &&
  7064. ((level < 0) || (level > 7)))
  7065. return -EINVAL;
  7066. /* safety net should the EC not support AUTO
  7067. * or FULLSPEED mode bits and just ignore them */
  7068. if (level & TP_EC_FAN_FULLSPEED)
  7069. level |= 7; /* safety min speed 7 */
  7070. else if (level & TP_EC_FAN_AUTO)
  7071. level |= 4; /* safety min speed 4 */
  7072. if (tp_features.second_fan_ctl) {
  7073. if (!fan_select_fan2() ||
  7074. !acpi_ec_write(fan_status_offset, level)) {
  7075. pr_warn("Couldn't set 2nd fan level, disabling support\n");
  7076. tp_features.second_fan_ctl = 0;
  7077. }
  7078. fan_select_fan1();
  7079. }
  7080. if (!acpi_ec_write(fan_status_offset, level))
  7081. return -EIO;
  7082. else
  7083. tp_features.fan_ctrl_status_undef = 0;
  7084. break;
  7085. default:
  7086. return -ENXIO;
  7087. }
  7088. vdbg_printk(TPACPI_DBG_FAN,
  7089. "fan control: set fan control register to 0x%02x\n", level);
  7090. return 0;
  7091. }
  7092. static int fan_set_level_safe(int level)
  7093. {
  7094. int rc;
  7095. if (!fan_control_allowed)
  7096. return -EPERM;
  7097. if (mutex_lock_killable(&fan_mutex))
  7098. return -ERESTARTSYS;
  7099. if (level == TPACPI_FAN_LAST_LEVEL)
  7100. level = fan_control_desired_level;
  7101. rc = fan_set_level(level);
  7102. if (!rc)
  7103. fan_update_desired_level(level);
  7104. mutex_unlock(&fan_mutex);
  7105. return rc;
  7106. }
  7107. static int fan_set_enable(void)
  7108. {
  7109. u8 s;
  7110. int rc;
  7111. if (!fan_control_allowed)
  7112. return -EPERM;
  7113. if (mutex_lock_killable(&fan_mutex))
  7114. return -ERESTARTSYS;
  7115. switch (fan_control_access_mode) {
  7116. case TPACPI_FAN_WR_ACPI_FANS:
  7117. case TPACPI_FAN_WR_TPEC:
  7118. rc = fan_get_status(&s);
  7119. if (rc)
  7120. break;
  7121. /* Don't go out of emergency fan mode */
  7122. if (s != 7) {
  7123. s &= 0x07;
  7124. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  7125. }
  7126. if (!acpi_ec_write(fan_status_offset, s))
  7127. rc = -EIO;
  7128. else {
  7129. tp_features.fan_ctrl_status_undef = 0;
  7130. rc = 0;
  7131. }
  7132. break;
  7133. case TPACPI_FAN_WR_ACPI_SFAN:
  7134. rc = fan_get_status(&s);
  7135. if (rc)
  7136. break;
  7137. s &= 0x07;
  7138. /* Set fan to at least level 4 */
  7139. s |= 4;
  7140. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  7141. rc = -EIO;
  7142. else
  7143. rc = 0;
  7144. break;
  7145. default:
  7146. rc = -ENXIO;
  7147. }
  7148. mutex_unlock(&fan_mutex);
  7149. if (!rc)
  7150. vdbg_printk(TPACPI_DBG_FAN,
  7151. "fan control: set fan control register to 0x%02x\n",
  7152. s);
  7153. return rc;
  7154. }
  7155. static int fan_set_disable(void)
  7156. {
  7157. int rc;
  7158. if (!fan_control_allowed)
  7159. return -EPERM;
  7160. if (mutex_lock_killable(&fan_mutex))
  7161. return -ERESTARTSYS;
  7162. rc = 0;
  7163. switch (fan_control_access_mode) {
  7164. case TPACPI_FAN_WR_ACPI_FANS:
  7165. case TPACPI_FAN_WR_TPEC:
  7166. if (!acpi_ec_write(fan_status_offset, 0x00))
  7167. rc = -EIO;
  7168. else {
  7169. fan_control_desired_level = 0;
  7170. tp_features.fan_ctrl_status_undef = 0;
  7171. }
  7172. break;
  7173. case TPACPI_FAN_WR_ACPI_SFAN:
  7174. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  7175. rc = -EIO;
  7176. else
  7177. fan_control_desired_level = 0;
  7178. break;
  7179. default:
  7180. rc = -ENXIO;
  7181. }
  7182. if (!rc)
  7183. vdbg_printk(TPACPI_DBG_FAN,
  7184. "fan control: set fan control register to 0\n");
  7185. mutex_unlock(&fan_mutex);
  7186. return rc;
  7187. }
  7188. static int fan_set_speed(int speed)
  7189. {
  7190. int rc;
  7191. if (!fan_control_allowed)
  7192. return -EPERM;
  7193. if (mutex_lock_killable(&fan_mutex))
  7194. return -ERESTARTSYS;
  7195. rc = 0;
  7196. switch (fan_control_access_mode) {
  7197. case TPACPI_FAN_WR_ACPI_FANS:
  7198. if (speed >= 0 && speed <= 65535) {
  7199. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  7200. speed, speed, speed))
  7201. rc = -EIO;
  7202. } else
  7203. rc = -EINVAL;
  7204. break;
  7205. default:
  7206. rc = -ENXIO;
  7207. }
  7208. mutex_unlock(&fan_mutex);
  7209. return rc;
  7210. }
  7211. static void fan_watchdog_reset(void)
  7212. {
  7213. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  7214. return;
  7215. if (fan_watchdog_maxinterval > 0 &&
  7216. tpacpi_lifecycle != TPACPI_LIFE_EXITING)
  7217. mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
  7218. msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
  7219. else
  7220. cancel_delayed_work(&fan_watchdog_task);
  7221. }
  7222. static void fan_watchdog_fire(struct work_struct *ignored)
  7223. {
  7224. int rc;
  7225. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  7226. return;
  7227. pr_notice("fan watchdog: enabling fan\n");
  7228. rc = fan_set_enable();
  7229. if (rc < 0) {
  7230. pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
  7231. rc);
  7232. /* reschedule for later */
  7233. fan_watchdog_reset();
  7234. }
  7235. }
  7236. /*
  7237. * SYSFS fan layout: hwmon compatible (device)
  7238. *
  7239. * pwm*_enable:
  7240. * 0: "disengaged" mode
  7241. * 1: manual mode
  7242. * 2: native EC "auto" mode (recommended, hardware default)
  7243. *
  7244. * pwm*: set speed in manual mode, ignored otherwise.
  7245. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  7246. * interpolation.
  7247. *
  7248. * fan*_input: tachometer reading, RPM
  7249. *
  7250. *
  7251. * SYSFS fan layout: extensions
  7252. *
  7253. * fan_watchdog (driver):
  7254. * fan watchdog interval in seconds, 0 disables (default), max 120
  7255. */
  7256. /* sysfs fan pwm1_enable ----------------------------------------------- */
  7257. static ssize_t fan_pwm1_enable_show(struct device *dev,
  7258. struct device_attribute *attr,
  7259. char *buf)
  7260. {
  7261. int res, mode;
  7262. u8 status;
  7263. res = fan_get_status_safe(&status);
  7264. if (res)
  7265. return res;
  7266. if (status & TP_EC_FAN_FULLSPEED) {
  7267. mode = 0;
  7268. } else if (status & TP_EC_FAN_AUTO) {
  7269. mode = 2;
  7270. } else
  7271. mode = 1;
  7272. return sysfs_emit(buf, "%d\n", mode);
  7273. }
  7274. static ssize_t fan_pwm1_enable_store(struct device *dev,
  7275. struct device_attribute *attr,
  7276. const char *buf, size_t count)
  7277. {
  7278. unsigned long t;
  7279. int res, level;
  7280. if (parse_strtoul(buf, 2, &t))
  7281. return -EINVAL;
  7282. tpacpi_disclose_usertask("hwmon pwm1_enable",
  7283. "set fan mode to %lu\n", t);
  7284. switch (t) {
  7285. case 0:
  7286. level = TP_EC_FAN_FULLSPEED;
  7287. break;
  7288. case 1:
  7289. level = TPACPI_FAN_LAST_LEVEL;
  7290. break;
  7291. case 2:
  7292. level = TP_EC_FAN_AUTO;
  7293. break;
  7294. case 3:
  7295. /* reserved for software-controlled auto mode */
  7296. return -ENOSYS;
  7297. default:
  7298. return -EINVAL;
  7299. }
  7300. res = fan_set_level_safe(level);
  7301. if (res == -ENXIO)
  7302. return -EINVAL;
  7303. else if (res < 0)
  7304. return res;
  7305. fan_watchdog_reset();
  7306. return count;
  7307. }
  7308. static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  7309. fan_pwm1_enable_show, fan_pwm1_enable_store);
  7310. /* sysfs fan pwm1 ------------------------------------------------------ */
  7311. static ssize_t fan_pwm1_show(struct device *dev,
  7312. struct device_attribute *attr,
  7313. char *buf)
  7314. {
  7315. int res;
  7316. u8 status;
  7317. res = fan_get_status_safe(&status);
  7318. if (res)
  7319. return res;
  7320. if ((status &
  7321. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  7322. status = fan_control_desired_level;
  7323. if (status > 7)
  7324. status = 7;
  7325. return sysfs_emit(buf, "%u\n", (status * 255) / 7);
  7326. }
  7327. static ssize_t fan_pwm1_store(struct device *dev,
  7328. struct device_attribute *attr,
  7329. const char *buf, size_t count)
  7330. {
  7331. unsigned long s;
  7332. int rc;
  7333. u8 status, newlevel;
  7334. if (parse_strtoul(buf, 255, &s))
  7335. return -EINVAL;
  7336. tpacpi_disclose_usertask("hwmon pwm1",
  7337. "set fan speed to %lu\n", s);
  7338. /* scale down from 0-255 to 0-7 */
  7339. newlevel = (s >> 5) & 0x07;
  7340. if (mutex_lock_killable(&fan_mutex))
  7341. return -ERESTARTSYS;
  7342. rc = fan_get_status(&status);
  7343. if (!rc && (status &
  7344. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  7345. rc = fan_set_level(newlevel);
  7346. if (rc == -ENXIO)
  7347. rc = -EINVAL;
  7348. else if (!rc) {
  7349. fan_update_desired_level(newlevel);
  7350. fan_watchdog_reset();
  7351. }
  7352. }
  7353. mutex_unlock(&fan_mutex);
  7354. return (rc) ? rc : count;
  7355. }
  7356. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
  7357. /* sysfs fan fan1_input ------------------------------------------------ */
  7358. static ssize_t fan_fan1_input_show(struct device *dev,
  7359. struct device_attribute *attr,
  7360. char *buf)
  7361. {
  7362. int res;
  7363. unsigned int speed;
  7364. res = fan_get_speed(&speed);
  7365. if (res < 0)
  7366. return res;
  7367. return sysfs_emit(buf, "%u\n", speed);
  7368. }
  7369. static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
  7370. /* sysfs fan fan2_input ------------------------------------------------ */
  7371. static ssize_t fan_fan2_input_show(struct device *dev,
  7372. struct device_attribute *attr,
  7373. char *buf)
  7374. {
  7375. int res;
  7376. unsigned int speed;
  7377. res = fan2_get_speed(&speed);
  7378. if (res < 0)
  7379. return res;
  7380. return sysfs_emit(buf, "%u\n", speed);
  7381. }
  7382. static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
  7383. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  7384. static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
  7385. {
  7386. return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
  7387. }
  7388. static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
  7389. size_t count)
  7390. {
  7391. unsigned long t;
  7392. if (parse_strtoul(buf, 120, &t))
  7393. return -EINVAL;
  7394. if (!fan_control_allowed)
  7395. return -EPERM;
  7396. fan_watchdog_maxinterval = t;
  7397. fan_watchdog_reset();
  7398. tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
  7399. return count;
  7400. }
  7401. static DRIVER_ATTR_RW(fan_watchdog);
  7402. /* --------------------------------------------------------------------- */
  7403. static struct attribute *fan_attributes[] = {
  7404. &dev_attr_pwm1_enable.attr,
  7405. &dev_attr_pwm1.attr,
  7406. &dev_attr_fan1_input.attr,
  7407. &dev_attr_fan2_input.attr,
  7408. NULL
  7409. };
  7410. static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  7411. int n)
  7412. {
  7413. if (fan_status_access_mode == TPACPI_FAN_NONE &&
  7414. fan_control_access_mode == TPACPI_FAN_WR_NONE)
  7415. return 0;
  7416. if (attr == &dev_attr_fan2_input.attr) {
  7417. if (!tp_features.second_fan)
  7418. return 0;
  7419. }
  7420. return attr->mode;
  7421. }
  7422. static const struct attribute_group fan_attr_group = {
  7423. .is_visible = fan_attr_is_visible,
  7424. .attrs = fan_attributes,
  7425. };
  7426. static struct attribute *fan_driver_attributes[] = {
  7427. &driver_attr_fan_watchdog.attr,
  7428. NULL
  7429. };
  7430. static const struct attribute_group fan_driver_attr_group = {
  7431. .is_visible = fan_attr_is_visible,
  7432. .attrs = fan_driver_attributes,
  7433. };
  7434. #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
  7435. #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
  7436. #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
  7437. #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
  7438. static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
  7439. TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
  7440. TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
  7441. TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
  7442. TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
  7443. TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
  7444. TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
  7445. TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
  7446. TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
  7447. TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
  7448. TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
  7449. TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
  7450. TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
  7451. TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
  7452. TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
  7453. TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
  7454. TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
  7455. TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
  7456. };
  7457. static int __init fan_init(struct ibm_init_struct *iibm)
  7458. {
  7459. unsigned long quirks;
  7460. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  7461. "initializing fan subdriver\n");
  7462. mutex_init(&fan_mutex);
  7463. fan_status_access_mode = TPACPI_FAN_NONE;
  7464. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  7465. fan_control_commands = 0;
  7466. fan_watchdog_maxinterval = 0;
  7467. tp_features.fan_ctrl_status_undef = 0;
  7468. tp_features.second_fan = 0;
  7469. tp_features.second_fan_ctl = 0;
  7470. fan_control_desired_level = 7;
  7471. if (tpacpi_is_ibm()) {
  7472. TPACPI_ACPIHANDLE_INIT(fans);
  7473. TPACPI_ACPIHANDLE_INIT(gfan);
  7474. TPACPI_ACPIHANDLE_INIT(sfan);
  7475. }
  7476. quirks = tpacpi_check_quirks(fan_quirk_table,
  7477. ARRAY_SIZE(fan_quirk_table));
  7478. if (quirks & TPACPI_FAN_NOFAN) {
  7479. pr_info("No integrated ThinkPad fan available\n");
  7480. return -ENODEV;
  7481. }
  7482. if (gfan_handle) {
  7483. /* 570, 600e/x, 770e, 770x */
  7484. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  7485. } else {
  7486. /* all other ThinkPads: note that even old-style
  7487. * ThinkPad ECs supports the fan control register */
  7488. if (likely(acpi_ec_read(fan_status_offset,
  7489. &fan_control_initial_status))) {
  7490. int res;
  7491. unsigned int speed;
  7492. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  7493. if (quirks & TPACPI_FAN_Q1)
  7494. fan_quirk1_setup();
  7495. /* Try and probe the 2nd fan */
  7496. tp_features.second_fan = 1; /* needed for get_speed to work */
  7497. res = fan2_get_speed(&speed);
  7498. if (res >= 0 && speed != FAN_NOT_PRESENT) {
  7499. /* It responded - so let's assume it's there */
  7500. tp_features.second_fan = 1;
  7501. tp_features.second_fan_ctl = 1;
  7502. pr_info("secondary fan control detected & enabled\n");
  7503. } else {
  7504. /* Fan not auto-detected */
  7505. tp_features.second_fan = 0;
  7506. if (quirks & TPACPI_FAN_2FAN) {
  7507. tp_features.second_fan = 1;
  7508. pr_info("secondary fan support enabled\n");
  7509. }
  7510. if (quirks & TPACPI_FAN_2CTL) {
  7511. tp_features.second_fan = 1;
  7512. tp_features.second_fan_ctl = 1;
  7513. pr_info("secondary fan control enabled\n");
  7514. }
  7515. }
  7516. } else {
  7517. pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
  7518. return -ENODEV;
  7519. }
  7520. }
  7521. if (sfan_handle) {
  7522. /* 570, 770x-JL */
  7523. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  7524. fan_control_commands |=
  7525. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  7526. } else {
  7527. if (!gfan_handle) {
  7528. /* gfan without sfan means no fan control */
  7529. /* all other models implement TP EC 0x2f control */
  7530. if (fans_handle) {
  7531. /* X31, X40, X41 */
  7532. fan_control_access_mode =
  7533. TPACPI_FAN_WR_ACPI_FANS;
  7534. fan_control_commands |=
  7535. TPACPI_FAN_CMD_SPEED |
  7536. TPACPI_FAN_CMD_LEVEL |
  7537. TPACPI_FAN_CMD_ENABLE;
  7538. } else {
  7539. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  7540. fan_control_commands |=
  7541. TPACPI_FAN_CMD_LEVEL |
  7542. TPACPI_FAN_CMD_ENABLE;
  7543. }
  7544. }
  7545. }
  7546. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  7547. "fan is %s, modes %d, %d\n",
  7548. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  7549. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  7550. fan_status_access_mode, fan_control_access_mode);
  7551. /* fan control master switch */
  7552. if (!fan_control_allowed) {
  7553. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  7554. fan_control_commands = 0;
  7555. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  7556. "fan control features disabled by parameter\n");
  7557. }
  7558. /* update fan_control_desired_level */
  7559. if (fan_status_access_mode != TPACPI_FAN_NONE)
  7560. fan_get_status_safe(NULL);
  7561. if (fan_status_access_mode == TPACPI_FAN_NONE &&
  7562. fan_control_access_mode == TPACPI_FAN_WR_NONE)
  7563. return -ENODEV;
  7564. return 0;
  7565. }
  7566. static void fan_exit(void)
  7567. {
  7568. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
  7569. "cancelling any pending fan watchdog tasks\n");
  7570. cancel_delayed_work(&fan_watchdog_task);
  7571. flush_workqueue(tpacpi_wq);
  7572. }
  7573. static void fan_suspend(void)
  7574. {
  7575. int rc;
  7576. if (!fan_control_allowed)
  7577. return;
  7578. /* Store fan status in cache */
  7579. fan_control_resume_level = 0;
  7580. rc = fan_get_status_safe(&fan_control_resume_level);
  7581. if (rc)
  7582. pr_notice("failed to read fan level for later restore during resume: %d\n",
  7583. rc);
  7584. /* if it is undefined, don't attempt to restore it.
  7585. * KEEP THIS LAST */
  7586. if (tp_features.fan_ctrl_status_undef)
  7587. fan_control_resume_level = 0;
  7588. }
  7589. static void fan_resume(void)
  7590. {
  7591. u8 current_level = 7;
  7592. bool do_set = false;
  7593. int rc;
  7594. /* DSDT *always* updates status on resume */
  7595. tp_features.fan_ctrl_status_undef = 0;
  7596. if (!fan_control_allowed ||
  7597. !fan_control_resume_level ||
  7598. fan_get_status_safe(&current_level))
  7599. return;
  7600. switch (fan_control_access_mode) {
  7601. case TPACPI_FAN_WR_ACPI_SFAN:
  7602. /* never decrease fan level */
  7603. do_set = (fan_control_resume_level > current_level);
  7604. break;
  7605. case TPACPI_FAN_WR_ACPI_FANS:
  7606. case TPACPI_FAN_WR_TPEC:
  7607. /* never decrease fan level, scale is:
  7608. * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
  7609. *
  7610. * We expect the firmware to set either 7 or AUTO, but we
  7611. * handle FULLSPEED out of paranoia.
  7612. *
  7613. * So, we can safely only restore FULLSPEED or 7, anything
  7614. * else could slow the fan. Restoring AUTO is useless, at
  7615. * best that's exactly what the DSDT already set (it is the
  7616. * slower it uses).
  7617. *
  7618. * Always keep in mind that the DSDT *will* have set the
  7619. * fans to what the vendor supposes is the best level. We
  7620. * muck with it only to speed the fan up.
  7621. */
  7622. if (fan_control_resume_level != 7 &&
  7623. !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
  7624. return;
  7625. else
  7626. do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
  7627. (current_level != fan_control_resume_level);
  7628. break;
  7629. default:
  7630. return;
  7631. }
  7632. if (do_set) {
  7633. pr_notice("restoring fan level to 0x%02x\n",
  7634. fan_control_resume_level);
  7635. rc = fan_set_level_safe(fan_control_resume_level);
  7636. if (rc < 0)
  7637. pr_notice("failed to restore fan level: %d\n", rc);
  7638. }
  7639. }
  7640. static int fan_read(struct seq_file *m)
  7641. {
  7642. int rc;
  7643. u8 status;
  7644. unsigned int speed = 0;
  7645. switch (fan_status_access_mode) {
  7646. case TPACPI_FAN_RD_ACPI_GFAN:
  7647. /* 570, 600e/x, 770e, 770x */
  7648. rc = fan_get_status_safe(&status);
  7649. if (rc)
  7650. return rc;
  7651. seq_printf(m, "status:\t\t%s\n"
  7652. "level:\t\t%d\n",
  7653. str_enabled_disabled(status), status);
  7654. break;
  7655. case TPACPI_FAN_RD_TPEC:
  7656. /* all except 570, 600e/x, 770e, 770x */
  7657. rc = fan_get_status_safe(&status);
  7658. if (rc)
  7659. return rc;
  7660. seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status));
  7661. rc = fan_get_speed(&speed);
  7662. if (rc < 0)
  7663. return rc;
  7664. seq_printf(m, "speed:\t\t%d\n", speed);
  7665. if (status & TP_EC_FAN_FULLSPEED)
  7666. /* Disengaged mode takes precedence */
  7667. seq_printf(m, "level:\t\tdisengaged\n");
  7668. else if (status & TP_EC_FAN_AUTO)
  7669. seq_printf(m, "level:\t\tauto\n");
  7670. else
  7671. seq_printf(m, "level:\t\t%d\n", status);
  7672. break;
  7673. case TPACPI_FAN_NONE:
  7674. default:
  7675. seq_printf(m, "status:\t\tnot supported\n");
  7676. }
  7677. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  7678. seq_printf(m, "commands:\tlevel <level>");
  7679. switch (fan_control_access_mode) {
  7680. case TPACPI_FAN_WR_ACPI_SFAN:
  7681. seq_printf(m, " (<level> is 0-7)\n");
  7682. break;
  7683. default:
  7684. seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
  7685. break;
  7686. }
  7687. }
  7688. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  7689. seq_printf(m, "commands:\tenable, disable\n"
  7690. "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
  7691. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  7692. seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
  7693. return 0;
  7694. }
  7695. static int fan_write_cmd_level(const char *cmd, int *rc)
  7696. {
  7697. int level;
  7698. if (strlencmp(cmd, "level auto") == 0)
  7699. level = TP_EC_FAN_AUTO;
  7700. else if ((strlencmp(cmd, "level disengaged") == 0) ||
  7701. (strlencmp(cmd, "level full-speed") == 0))
  7702. level = TP_EC_FAN_FULLSPEED;
  7703. else if (sscanf(cmd, "level %d", &level) != 1)
  7704. return 0;
  7705. *rc = fan_set_level_safe(level);
  7706. if (*rc == -ENXIO)
  7707. pr_err("level command accepted for unsupported access mode %d\n",
  7708. fan_control_access_mode);
  7709. else if (!*rc)
  7710. tpacpi_disclose_usertask("procfs fan",
  7711. "set level to %d\n", level);
  7712. return 1;
  7713. }
  7714. static int fan_write_cmd_enable(const char *cmd, int *rc)
  7715. {
  7716. if (strlencmp(cmd, "enable") != 0)
  7717. return 0;
  7718. *rc = fan_set_enable();
  7719. if (*rc == -ENXIO)
  7720. pr_err("enable command accepted for unsupported access mode %d\n",
  7721. fan_control_access_mode);
  7722. else if (!*rc)
  7723. tpacpi_disclose_usertask("procfs fan", "enable\n");
  7724. return 1;
  7725. }
  7726. static int fan_write_cmd_disable(const char *cmd, int *rc)
  7727. {
  7728. if (strlencmp(cmd, "disable") != 0)
  7729. return 0;
  7730. *rc = fan_set_disable();
  7731. if (*rc == -ENXIO)
  7732. pr_err("disable command accepted for unsupported access mode %d\n",
  7733. fan_control_access_mode);
  7734. else if (!*rc)
  7735. tpacpi_disclose_usertask("procfs fan", "disable\n");
  7736. return 1;
  7737. }
  7738. static int fan_write_cmd_speed(const char *cmd, int *rc)
  7739. {
  7740. int speed;
  7741. /* TODO:
  7742. * Support speed <low> <medium> <high> ? */
  7743. if (sscanf(cmd, "speed %d", &speed) != 1)
  7744. return 0;
  7745. *rc = fan_set_speed(speed);
  7746. if (*rc == -ENXIO)
  7747. pr_err("speed command accepted for unsupported access mode %d\n",
  7748. fan_control_access_mode);
  7749. else if (!*rc)
  7750. tpacpi_disclose_usertask("procfs fan",
  7751. "set speed to %d\n", speed);
  7752. return 1;
  7753. }
  7754. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  7755. {
  7756. int interval;
  7757. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  7758. return 0;
  7759. if (interval < 0 || interval > 120)
  7760. *rc = -EINVAL;
  7761. else {
  7762. fan_watchdog_maxinterval = interval;
  7763. tpacpi_disclose_usertask("procfs fan",
  7764. "set watchdog timer to %d\n",
  7765. interval);
  7766. }
  7767. return 1;
  7768. }
  7769. static int fan_write(char *buf)
  7770. {
  7771. char *cmd;
  7772. int rc = 0;
  7773. while (!rc && (cmd = strsep(&buf, ","))) {
  7774. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  7775. fan_write_cmd_level(cmd, &rc)) &&
  7776. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  7777. (fan_write_cmd_enable(cmd, &rc) ||
  7778. fan_write_cmd_disable(cmd, &rc) ||
  7779. fan_write_cmd_watchdog(cmd, &rc))) &&
  7780. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  7781. fan_write_cmd_speed(cmd, &rc))
  7782. )
  7783. rc = -EINVAL;
  7784. else if (!rc)
  7785. fan_watchdog_reset();
  7786. }
  7787. return rc;
  7788. }
  7789. static struct ibm_struct fan_driver_data = {
  7790. .name = "fan",
  7791. .read = fan_read,
  7792. .write = fan_write,
  7793. .exit = fan_exit,
  7794. .suspend = fan_suspend,
  7795. .resume = fan_resume,
  7796. };
  7797. /*************************************************************************
  7798. * Mute LED subdriver
  7799. */
  7800. #define TPACPI_LED_MAX 2
  7801. struct tp_led_table {
  7802. acpi_string name;
  7803. int on_value;
  7804. int off_value;
  7805. int state;
  7806. };
  7807. static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
  7808. [LED_AUDIO_MUTE] = {
  7809. .name = "SSMS",
  7810. .on_value = 1,
  7811. .off_value = 0,
  7812. },
  7813. [LED_AUDIO_MICMUTE] = {
  7814. .name = "MMTS",
  7815. .on_value = 2,
  7816. .off_value = 0,
  7817. },
  7818. };
  7819. static int mute_led_on_off(struct tp_led_table *t, bool state)
  7820. {
  7821. acpi_handle temp;
  7822. int output;
  7823. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
  7824. pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
  7825. return -EIO;
  7826. }
  7827. if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
  7828. state ? t->on_value : t->off_value))
  7829. return -EIO;
  7830. t->state = state;
  7831. return state;
  7832. }
  7833. static int tpacpi_led_set(int whichled, bool on)
  7834. {
  7835. struct tp_led_table *t;
  7836. t = &led_tables[whichled];
  7837. if (t->state < 0 || t->state == on)
  7838. return t->state;
  7839. return mute_led_on_off(t, on);
  7840. }
  7841. static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
  7842. enum led_brightness brightness)
  7843. {
  7844. return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
  7845. }
  7846. static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
  7847. enum led_brightness brightness)
  7848. {
  7849. return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
  7850. }
  7851. static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
  7852. [LED_AUDIO_MUTE] = {
  7853. .name = "platform::mute",
  7854. .max_brightness = 1,
  7855. .brightness_set_blocking = tpacpi_led_mute_set,
  7856. .default_trigger = "audio-mute",
  7857. },
  7858. [LED_AUDIO_MICMUTE] = {
  7859. .name = "platform::micmute",
  7860. .max_brightness = 1,
  7861. .brightness_set_blocking = tpacpi_led_micmute_set,
  7862. .default_trigger = "audio-micmute",
  7863. },
  7864. };
  7865. static int mute_led_init(struct ibm_init_struct *iibm)
  7866. {
  7867. acpi_handle temp;
  7868. int i, err;
  7869. for (i = 0; i < TPACPI_LED_MAX; i++) {
  7870. struct tp_led_table *t = &led_tables[i];
  7871. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
  7872. t->state = -ENODEV;
  7873. continue;
  7874. }
  7875. mute_led_cdev[i].brightness = ledtrig_audio_get(i);
  7876. err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
  7877. if (err < 0) {
  7878. while (i--)
  7879. led_classdev_unregister(&mute_led_cdev[i]);
  7880. return err;
  7881. }
  7882. }
  7883. return 0;
  7884. }
  7885. static void mute_led_exit(void)
  7886. {
  7887. int i;
  7888. for (i = 0; i < TPACPI_LED_MAX; i++) {
  7889. led_classdev_unregister(&mute_led_cdev[i]);
  7890. tpacpi_led_set(i, false);
  7891. }
  7892. }
  7893. static void mute_led_resume(void)
  7894. {
  7895. int i;
  7896. for (i = 0; i < TPACPI_LED_MAX; i++) {
  7897. struct tp_led_table *t = &led_tables[i];
  7898. if (t->state >= 0)
  7899. mute_led_on_off(t, t->state);
  7900. }
  7901. }
  7902. static struct ibm_struct mute_led_driver_data = {
  7903. .name = "mute_led",
  7904. .exit = mute_led_exit,
  7905. .resume = mute_led_resume,
  7906. };
  7907. /*
  7908. * Battery Wear Control Driver
  7909. * Contact: Ognjen Galic <[email protected]>
  7910. */
  7911. /* Metadata */
  7912. #define GET_START "BCTG"
  7913. #define SET_START "BCCS"
  7914. #define GET_STOP "BCSG"
  7915. #define SET_STOP "BCSS"
  7916. #define GET_DISCHARGE "BDSG"
  7917. #define SET_DISCHARGE "BDSS"
  7918. #define GET_INHIBIT "BICG"
  7919. #define SET_INHIBIT "BICS"
  7920. enum {
  7921. BAT_ANY = 0,
  7922. BAT_PRIMARY = 1,
  7923. BAT_SECONDARY = 2
  7924. };
  7925. enum {
  7926. /* Error condition bit */
  7927. METHOD_ERR = BIT(31),
  7928. };
  7929. enum {
  7930. /* This is used in the get/set helpers */
  7931. THRESHOLD_START,
  7932. THRESHOLD_STOP,
  7933. FORCE_DISCHARGE,
  7934. INHIBIT_CHARGE,
  7935. };
  7936. struct tpacpi_battery_data {
  7937. int charge_start;
  7938. int start_support;
  7939. int charge_stop;
  7940. int stop_support;
  7941. unsigned int charge_behaviours;
  7942. };
  7943. struct tpacpi_battery_driver_data {
  7944. struct tpacpi_battery_data batteries[3];
  7945. int individual_addressing;
  7946. };
  7947. static struct tpacpi_battery_driver_data battery_info;
  7948. /* ACPI helpers/functions/probes */
  7949. /**
  7950. * This evaluates a ACPI method call specific to the battery
  7951. * ACPI extension. The specifics are that an error is marked
  7952. * in the 32rd bit of the response, so we just check that here.
  7953. */
  7954. static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
  7955. {
  7956. int response;
  7957. if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
  7958. acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
  7959. return AE_ERROR;
  7960. }
  7961. if (response & METHOD_ERR) {
  7962. acpi_handle_err(hkey_handle,
  7963. "%s evaluated but flagged as error", method);
  7964. return AE_ERROR;
  7965. }
  7966. *ret = response;
  7967. return AE_OK;
  7968. }
  7969. static int tpacpi_battery_get(int what, int battery, int *ret)
  7970. {
  7971. switch (what) {
  7972. case THRESHOLD_START:
  7973. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
  7974. return -ENODEV;
  7975. /* The value is in the low 8 bits of the response */
  7976. *ret = *ret & 0xFF;
  7977. return 0;
  7978. case THRESHOLD_STOP:
  7979. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
  7980. return -ENODEV;
  7981. /* Value is in lower 8 bits */
  7982. *ret = *ret & 0xFF;
  7983. /*
  7984. * On the stop value, if we return 0 that
  7985. * does not make any sense. 0 means Default, which
  7986. * means that charging stops at 100%, so we return
  7987. * that.
  7988. */
  7989. if (*ret == 0)
  7990. *ret = 100;
  7991. return 0;
  7992. case FORCE_DISCHARGE:
  7993. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
  7994. return -ENODEV;
  7995. /* The force discharge status is in bit 0 */
  7996. *ret = *ret & 0x01;
  7997. return 0;
  7998. case INHIBIT_CHARGE:
  7999. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
  8000. return -ENODEV;
  8001. /* The inhibit charge status is in bit 0 */
  8002. *ret = *ret & 0x01;
  8003. return 0;
  8004. default:
  8005. pr_crit("wrong parameter: %d", what);
  8006. return -EINVAL;
  8007. }
  8008. }
  8009. static int tpacpi_battery_set(int what, int battery, int value)
  8010. {
  8011. int param, ret;
  8012. /* The first 8 bits are the value of the threshold */
  8013. param = value;
  8014. /* The battery ID is in bits 8-9, 2 bits */
  8015. param |= battery << 8;
  8016. switch (what) {
  8017. case THRESHOLD_START:
  8018. if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
  8019. pr_err("failed to set charge threshold on battery %d",
  8020. battery);
  8021. return -ENODEV;
  8022. }
  8023. return 0;
  8024. case THRESHOLD_STOP:
  8025. if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
  8026. pr_err("failed to set stop threshold: %d", battery);
  8027. return -ENODEV;
  8028. }
  8029. return 0;
  8030. case FORCE_DISCHARGE:
  8031. /* Force discharge is in bit 0,
  8032. * break on AC attach is in bit 1 (won't work on some ThinkPads),
  8033. * battery ID is in bits 8-9, 2 bits.
  8034. */
  8035. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
  8036. pr_err("failed to set force discharge on %d", battery);
  8037. return -ENODEV;
  8038. }
  8039. return 0;
  8040. case INHIBIT_CHARGE:
  8041. /* When setting inhibit charge, we set a default value of
  8042. * always breaking on AC detach and the effective time is set to
  8043. * be permanent.
  8044. * The battery ID is in bits 4-5, 2 bits,
  8045. * the effective time is in bits 8-23, 2 bytes.
  8046. * A time of FFFF indicates forever.
  8047. */
  8048. param = value;
  8049. param |= battery << 4;
  8050. param |= 0xFFFF << 8;
  8051. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
  8052. pr_err("failed to set inhibit charge on %d", battery);
  8053. return -ENODEV;
  8054. }
  8055. return 0;
  8056. default:
  8057. pr_crit("wrong parameter: %d", what);
  8058. return -EINVAL;
  8059. }
  8060. }
  8061. static int tpacpi_battery_set_validate(int what, int battery, int value)
  8062. {
  8063. int ret, v;
  8064. ret = tpacpi_battery_set(what, battery, value);
  8065. if (ret < 0)
  8066. return ret;
  8067. ret = tpacpi_battery_get(what, battery, &v);
  8068. if (ret < 0)
  8069. return ret;
  8070. if (v == value)
  8071. return 0;
  8072. msleep(500);
  8073. ret = tpacpi_battery_get(what, battery, &v);
  8074. if (ret < 0)
  8075. return ret;
  8076. if (v == value)
  8077. return 0;
  8078. return -EIO;
  8079. }
  8080. static int tpacpi_battery_probe(int battery)
  8081. {
  8082. int ret = 0;
  8083. memset(&battery_info.batteries[battery], 0,
  8084. sizeof(battery_info.batteries[battery]));
  8085. /*
  8086. * 1) Get the current start threshold
  8087. * 2) Check for support
  8088. * 3) Get the current stop threshold
  8089. * 4) Check for support
  8090. * 5) Get the current force discharge status
  8091. * 6) Check for support
  8092. * 7) Get the current inhibit charge status
  8093. * 8) Check for support
  8094. */
  8095. if (acpi_has_method(hkey_handle, GET_START)) {
  8096. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
  8097. pr_err("Error probing battery %d\n", battery);
  8098. return -ENODEV;
  8099. }
  8100. /* Individual addressing is in bit 9 */
  8101. if (ret & BIT(9))
  8102. battery_info.individual_addressing = true;
  8103. /* Support is marked in bit 8 */
  8104. if (ret & BIT(8))
  8105. battery_info.batteries[battery].start_support = 1;
  8106. else
  8107. return -ENODEV;
  8108. if (tpacpi_battery_get(THRESHOLD_START, battery,
  8109. &battery_info.batteries[battery].charge_start)) {
  8110. pr_err("Error probing battery %d\n", battery);
  8111. return -ENODEV;
  8112. }
  8113. }
  8114. if (acpi_has_method(hkey_handle, GET_STOP)) {
  8115. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
  8116. pr_err("Error probing battery stop; %d\n", battery);
  8117. return -ENODEV;
  8118. }
  8119. /* Support is marked in bit 8 */
  8120. if (ret & BIT(8))
  8121. battery_info.batteries[battery].stop_support = 1;
  8122. else
  8123. return -ENODEV;
  8124. if (tpacpi_battery_get(THRESHOLD_STOP, battery,
  8125. &battery_info.batteries[battery].charge_stop)) {
  8126. pr_err("Error probing battery stop: %d\n", battery);
  8127. return -ENODEV;
  8128. }
  8129. }
  8130. if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
  8131. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
  8132. pr_err("Error probing battery discharge; %d\n", battery);
  8133. return -ENODEV;
  8134. }
  8135. /* Support is marked in bit 8 */
  8136. if (ret & BIT(8))
  8137. battery_info.batteries[battery].charge_behaviours |=
  8138. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
  8139. }
  8140. if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
  8141. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
  8142. pr_err("Error probing battery inhibit charge; %d\n", battery);
  8143. return -ENODEV;
  8144. }
  8145. /* Support is marked in bit 5 */
  8146. if (ret & BIT(5))
  8147. battery_info.batteries[battery].charge_behaviours |=
  8148. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
  8149. }
  8150. battery_info.batteries[battery].charge_behaviours |=
  8151. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
  8152. pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
  8153. battery,
  8154. battery_info.batteries[battery].charge_start,
  8155. battery_info.batteries[battery].charge_stop,
  8156. battery_info.batteries[battery].charge_behaviours);
  8157. return 0;
  8158. }
  8159. /* General helper functions */
  8160. static int tpacpi_battery_get_id(const char *battery_name)
  8161. {
  8162. if (strcmp(battery_name, "BAT0") == 0 ||
  8163. tp_features.battery_force_primary)
  8164. return BAT_PRIMARY;
  8165. if (strcmp(battery_name, "BAT1") == 0)
  8166. return BAT_SECONDARY;
  8167. /*
  8168. * If for some reason the battery is not BAT0 nor is it
  8169. * BAT1, we will assume it's the default, first battery,
  8170. * AKA primary.
  8171. */
  8172. pr_warn("unknown battery %s, assuming primary", battery_name);
  8173. return BAT_PRIMARY;
  8174. }
  8175. /* sysfs interface */
  8176. static ssize_t tpacpi_battery_store(int what,
  8177. struct device *dev,
  8178. const char *buf, size_t count)
  8179. {
  8180. struct power_supply *supply = to_power_supply(dev);
  8181. unsigned long value;
  8182. int battery, rval;
  8183. /*
  8184. * Some systems have support for more than
  8185. * one battery. If that is the case,
  8186. * tpacpi_battery_probe marked that addressing
  8187. * them individually is supported, so we do that
  8188. * based on the device struct.
  8189. *
  8190. * On systems that are not supported, we assume
  8191. * the primary as most of the ACPI calls fail
  8192. * with "Any Battery" as the parameter.
  8193. */
  8194. if (battery_info.individual_addressing)
  8195. /* BAT_PRIMARY or BAT_SECONDARY */
  8196. battery = tpacpi_battery_get_id(supply->desc->name);
  8197. else
  8198. battery = BAT_PRIMARY;
  8199. rval = kstrtoul(buf, 10, &value);
  8200. if (rval)
  8201. return rval;
  8202. switch (what) {
  8203. case THRESHOLD_START:
  8204. if (!battery_info.batteries[battery].start_support)
  8205. return -ENODEV;
  8206. /* valid values are [0, 99] */
  8207. if (value > 99)
  8208. return -EINVAL;
  8209. if (value > battery_info.batteries[battery].charge_stop)
  8210. return -EINVAL;
  8211. if (tpacpi_battery_set(THRESHOLD_START, battery, value))
  8212. return -ENODEV;
  8213. battery_info.batteries[battery].charge_start = value;
  8214. return count;
  8215. case THRESHOLD_STOP:
  8216. if (!battery_info.batteries[battery].stop_support)
  8217. return -ENODEV;
  8218. /* valid values are [1, 100] */
  8219. if (value < 1 || value > 100)
  8220. return -EINVAL;
  8221. if (value < battery_info.batteries[battery].charge_start)
  8222. return -EINVAL;
  8223. battery_info.batteries[battery].charge_stop = value;
  8224. /*
  8225. * When 100 is passed to stop, we need to flip
  8226. * it to 0 as that the EC understands that as
  8227. * "Default", which will charge to 100%
  8228. */
  8229. if (value == 100)
  8230. value = 0;
  8231. if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
  8232. return -EINVAL;
  8233. return count;
  8234. default:
  8235. pr_crit("Wrong parameter: %d", what);
  8236. return -EINVAL;
  8237. }
  8238. return count;
  8239. }
  8240. static ssize_t tpacpi_battery_show(int what,
  8241. struct device *dev,
  8242. char *buf)
  8243. {
  8244. struct power_supply *supply = to_power_supply(dev);
  8245. int ret, battery;
  8246. /*
  8247. * Some systems have support for more than
  8248. * one battery. If that is the case,
  8249. * tpacpi_battery_probe marked that addressing
  8250. * them individually is supported, so we;
  8251. * based on the device struct.
  8252. *
  8253. * On systems that are not supported, we assume
  8254. * the primary as most of the ACPI calls fail
  8255. * with "Any Battery" as the parameter.
  8256. */
  8257. if (battery_info.individual_addressing)
  8258. /* BAT_PRIMARY or BAT_SECONDARY */
  8259. battery = tpacpi_battery_get_id(supply->desc->name);
  8260. else
  8261. battery = BAT_PRIMARY;
  8262. if (tpacpi_battery_get(what, battery, &ret))
  8263. return -ENODEV;
  8264. return sprintf(buf, "%d\n", ret);
  8265. }
  8266. static ssize_t charge_control_start_threshold_show(struct device *device,
  8267. struct device_attribute *attr,
  8268. char *buf)
  8269. {
  8270. return tpacpi_battery_show(THRESHOLD_START, device, buf);
  8271. }
  8272. static ssize_t charge_control_end_threshold_show(struct device *device,
  8273. struct device_attribute *attr,
  8274. char *buf)
  8275. {
  8276. return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
  8277. }
  8278. static ssize_t charge_behaviour_show(struct device *dev,
  8279. struct device_attribute *attr,
  8280. char *buf)
  8281. {
  8282. enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
  8283. struct power_supply *supply = to_power_supply(dev);
  8284. unsigned int available;
  8285. int ret, battery;
  8286. battery = tpacpi_battery_get_id(supply->desc->name);
  8287. available = battery_info.batteries[battery].charge_behaviours;
  8288. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
  8289. if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
  8290. return -ENODEV;
  8291. if (ret) {
  8292. active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
  8293. goto out;
  8294. }
  8295. }
  8296. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
  8297. if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
  8298. return -ENODEV;
  8299. if (ret) {
  8300. active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
  8301. goto out;
  8302. }
  8303. }
  8304. out:
  8305. return power_supply_charge_behaviour_show(dev, available, active, buf);
  8306. }
  8307. static ssize_t charge_control_start_threshold_store(struct device *dev,
  8308. struct device_attribute *attr,
  8309. const char *buf, size_t count)
  8310. {
  8311. return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
  8312. }
  8313. static ssize_t charge_control_end_threshold_store(struct device *dev,
  8314. struct device_attribute *attr,
  8315. const char *buf, size_t count)
  8316. {
  8317. return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
  8318. }
  8319. static ssize_t charge_behaviour_store(struct device *dev,
  8320. struct device_attribute *attr,
  8321. const char *buf, size_t count)
  8322. {
  8323. struct power_supply *supply = to_power_supply(dev);
  8324. int selected, battery, ret = 0;
  8325. unsigned int available;
  8326. battery = tpacpi_battery_get_id(supply->desc->name);
  8327. available = battery_info.batteries[battery].charge_behaviours;
  8328. selected = power_supply_charge_behaviour_parse(available, buf);
  8329. if (selected < 0)
  8330. return selected;
  8331. switch (selected) {
  8332. case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
  8333. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
  8334. ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
  8335. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
  8336. ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
  8337. if (ret < 0)
  8338. return ret;
  8339. break;
  8340. case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
  8341. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
  8342. ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
  8343. ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
  8344. if (ret < 0)
  8345. return ret;
  8346. break;
  8347. case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
  8348. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
  8349. ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
  8350. ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
  8351. if (ret < 0)
  8352. return ret;
  8353. break;
  8354. default:
  8355. dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
  8356. return -EINVAL;
  8357. }
  8358. return count;
  8359. }
  8360. static DEVICE_ATTR_RW(charge_control_start_threshold);
  8361. static DEVICE_ATTR_RW(charge_control_end_threshold);
  8362. static DEVICE_ATTR_RW(charge_behaviour);
  8363. static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
  8364. charge_start_threshold,
  8365. 0644,
  8366. charge_control_start_threshold_show,
  8367. charge_control_start_threshold_store
  8368. );
  8369. static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
  8370. charge_stop_threshold,
  8371. 0644,
  8372. charge_control_end_threshold_show,
  8373. charge_control_end_threshold_store
  8374. );
  8375. static struct attribute *tpacpi_battery_attrs[] = {
  8376. &dev_attr_charge_control_start_threshold.attr,
  8377. &dev_attr_charge_control_end_threshold.attr,
  8378. &dev_attr_charge_start_threshold.attr,
  8379. &dev_attr_charge_stop_threshold.attr,
  8380. &dev_attr_charge_behaviour.attr,
  8381. NULL,
  8382. };
  8383. ATTRIBUTE_GROUPS(tpacpi_battery);
  8384. /* ACPI battery hooking */
  8385. static int tpacpi_battery_add(struct power_supply *battery)
  8386. {
  8387. int batteryid = tpacpi_battery_get_id(battery->desc->name);
  8388. if (tpacpi_battery_probe(batteryid))
  8389. return -ENODEV;
  8390. if (device_add_groups(&battery->dev, tpacpi_battery_groups))
  8391. return -ENODEV;
  8392. return 0;
  8393. }
  8394. static int tpacpi_battery_remove(struct power_supply *battery)
  8395. {
  8396. device_remove_groups(&battery->dev, tpacpi_battery_groups);
  8397. return 0;
  8398. }
  8399. static struct acpi_battery_hook battery_hook = {
  8400. .add_battery = tpacpi_battery_add,
  8401. .remove_battery = tpacpi_battery_remove,
  8402. .name = "ThinkPad Battery Extension",
  8403. };
  8404. /* Subdriver init/exit */
  8405. static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
  8406. /*
  8407. * Individual addressing is broken on models that expose the
  8408. * primary battery as BAT1.
  8409. */
  8410. TPACPI_Q_LNV('8', 'F', true), /* Thinkpad X120e */
  8411. TPACPI_Q_LNV('J', '7', true), /* B5400 */
  8412. TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
  8413. TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
  8414. TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
  8415. TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
  8416. TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
  8417. };
  8418. static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
  8419. {
  8420. memset(&battery_info, 0, sizeof(battery_info));
  8421. tp_features.battery_force_primary = tpacpi_check_quirks(
  8422. battery_quirk_table,
  8423. ARRAY_SIZE(battery_quirk_table));
  8424. battery_hook_register(&battery_hook);
  8425. return 0;
  8426. }
  8427. static void tpacpi_battery_exit(void)
  8428. {
  8429. battery_hook_unregister(&battery_hook);
  8430. }
  8431. static struct ibm_struct battery_driver_data = {
  8432. .name = "battery",
  8433. .exit = tpacpi_battery_exit,
  8434. };
  8435. /*************************************************************************
  8436. * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
  8437. */
  8438. static struct drm_privacy_screen *lcdshadow_dev;
  8439. static acpi_handle lcdshadow_get_handle;
  8440. static acpi_handle lcdshadow_set_handle;
  8441. static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
  8442. enum drm_privacy_screen_status state)
  8443. {
  8444. int output;
  8445. if (WARN_ON(!mutex_is_locked(&priv->lock)))
  8446. return -EIO;
  8447. if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
  8448. return -EIO;
  8449. priv->hw_state = priv->sw_state = state;
  8450. return 0;
  8451. }
  8452. static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
  8453. {
  8454. int output;
  8455. if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
  8456. return;
  8457. priv->hw_state = priv->sw_state = output & 0x1;
  8458. }
  8459. static const struct drm_privacy_screen_ops lcdshadow_ops = {
  8460. .set_sw_state = lcdshadow_set_sw_state,
  8461. .get_hw_state = lcdshadow_get_hw_state,
  8462. };
  8463. static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
  8464. {
  8465. acpi_status status1, status2;
  8466. int output;
  8467. status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
  8468. status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
  8469. if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
  8470. return 0;
  8471. if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
  8472. return -EIO;
  8473. if (!(output & 0x10000))
  8474. return 0;
  8475. lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
  8476. &lcdshadow_ops, NULL);
  8477. if (IS_ERR(lcdshadow_dev))
  8478. return PTR_ERR(lcdshadow_dev);
  8479. return 0;
  8480. }
  8481. static void lcdshadow_exit(void)
  8482. {
  8483. drm_privacy_screen_unregister(lcdshadow_dev);
  8484. }
  8485. static void lcdshadow_resume(void)
  8486. {
  8487. if (!lcdshadow_dev)
  8488. return;
  8489. mutex_lock(&lcdshadow_dev->lock);
  8490. lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
  8491. mutex_unlock(&lcdshadow_dev->lock);
  8492. }
  8493. static int lcdshadow_read(struct seq_file *m)
  8494. {
  8495. if (!lcdshadow_dev) {
  8496. seq_puts(m, "status:\t\tnot supported\n");
  8497. } else {
  8498. seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
  8499. seq_puts(m, "commands:\t0, 1\n");
  8500. }
  8501. return 0;
  8502. }
  8503. static int lcdshadow_write(char *buf)
  8504. {
  8505. char *cmd;
  8506. int res, state = -EINVAL;
  8507. if (!lcdshadow_dev)
  8508. return -ENODEV;
  8509. while ((cmd = strsep(&buf, ","))) {
  8510. res = kstrtoint(cmd, 10, &state);
  8511. if (res < 0)
  8512. return res;
  8513. }
  8514. if (state >= 2 || state < 0)
  8515. return -EINVAL;
  8516. mutex_lock(&lcdshadow_dev->lock);
  8517. res = lcdshadow_set_sw_state(lcdshadow_dev, state);
  8518. mutex_unlock(&lcdshadow_dev->lock);
  8519. drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
  8520. return res;
  8521. }
  8522. static struct ibm_struct lcdshadow_driver_data = {
  8523. .name = "lcdshadow",
  8524. .exit = lcdshadow_exit,
  8525. .resume = lcdshadow_resume,
  8526. .read = lcdshadow_read,
  8527. .write = lcdshadow_write,
  8528. };
  8529. /*************************************************************************
  8530. * Thinkpad sensor interfaces
  8531. */
  8532. #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
  8533. #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
  8534. #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
  8535. #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
  8536. #define DYTC_CMD_GET 2 /* To get current IC function and mode */
  8537. #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
  8538. #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
  8539. #define PALMSENSOR_ON_BIT 1 /* psensor status */
  8540. static bool has_palmsensor;
  8541. static bool has_lapsensor;
  8542. static bool palm_state;
  8543. static bool lap_state;
  8544. static int dytc_version;
  8545. static int dytc_command(int command, int *output)
  8546. {
  8547. acpi_handle dytc_handle;
  8548. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
  8549. /* Platform doesn't support DYTC */
  8550. return -ENODEV;
  8551. }
  8552. if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
  8553. return -EIO;
  8554. return 0;
  8555. }
  8556. static int lapsensor_get(bool *present, bool *state)
  8557. {
  8558. int output, err;
  8559. *present = false;
  8560. err = dytc_command(DYTC_CMD_GET, &output);
  8561. if (err)
  8562. return err;
  8563. *present = true; /*If we get his far, we have lapmode support*/
  8564. *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
  8565. return 0;
  8566. }
  8567. static int palmsensor_get(bool *present, bool *state)
  8568. {
  8569. acpi_handle psensor_handle;
  8570. int output;
  8571. *present = false;
  8572. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
  8573. return -ENODEV;
  8574. if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
  8575. return -EIO;
  8576. *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
  8577. *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
  8578. return 0;
  8579. }
  8580. static void lapsensor_refresh(void)
  8581. {
  8582. bool state;
  8583. int err;
  8584. if (has_lapsensor) {
  8585. err = lapsensor_get(&has_lapsensor, &state);
  8586. if (err)
  8587. return;
  8588. if (lap_state != state) {
  8589. lap_state = state;
  8590. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
  8591. }
  8592. }
  8593. }
  8594. static void palmsensor_refresh(void)
  8595. {
  8596. bool state;
  8597. int err;
  8598. if (has_palmsensor) {
  8599. err = palmsensor_get(&has_palmsensor, &state);
  8600. if (err)
  8601. return;
  8602. if (palm_state != state) {
  8603. palm_state = state;
  8604. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
  8605. }
  8606. }
  8607. }
  8608. static ssize_t dytc_lapmode_show(struct device *dev,
  8609. struct device_attribute *attr,
  8610. char *buf)
  8611. {
  8612. if (has_lapsensor)
  8613. return sysfs_emit(buf, "%d\n", lap_state);
  8614. return sysfs_emit(buf, "\n");
  8615. }
  8616. static DEVICE_ATTR_RO(dytc_lapmode);
  8617. static ssize_t palmsensor_show(struct device *dev,
  8618. struct device_attribute *attr,
  8619. char *buf)
  8620. {
  8621. if (has_palmsensor)
  8622. return sysfs_emit(buf, "%d\n", palm_state);
  8623. return sysfs_emit(buf, "\n");
  8624. }
  8625. static DEVICE_ATTR_RO(palmsensor);
  8626. static struct attribute *proxsensor_attributes[] = {
  8627. &dev_attr_dytc_lapmode.attr,
  8628. &dev_attr_palmsensor.attr,
  8629. NULL
  8630. };
  8631. static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
  8632. struct attribute *attr, int n)
  8633. {
  8634. if (attr == &dev_attr_dytc_lapmode.attr) {
  8635. /*
  8636. * Platforms before DYTC version 5 claim to have a lap sensor,
  8637. * but it doesn't work, so we ignore them.
  8638. */
  8639. if (!has_lapsensor || dytc_version < 5)
  8640. return 0;
  8641. } else if (attr == &dev_attr_palmsensor.attr) {
  8642. if (!has_palmsensor)
  8643. return 0;
  8644. }
  8645. return attr->mode;
  8646. }
  8647. static const struct attribute_group proxsensor_attr_group = {
  8648. .is_visible = proxsensor_attr_is_visible,
  8649. .attrs = proxsensor_attributes,
  8650. };
  8651. static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
  8652. {
  8653. int palm_err, lap_err;
  8654. palm_err = palmsensor_get(&has_palmsensor, &palm_state);
  8655. lap_err = lapsensor_get(&has_lapsensor, &lap_state);
  8656. /* If support isn't available for both devices return -ENODEV */
  8657. if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
  8658. return -ENODEV;
  8659. /* Otherwise, if there was an error return it */
  8660. if (palm_err && (palm_err != -ENODEV))
  8661. return palm_err;
  8662. if (lap_err && (lap_err != -ENODEV))
  8663. return lap_err;
  8664. return 0;
  8665. }
  8666. static struct ibm_struct proxsensor_driver_data = {
  8667. .name = "proximity-sensor",
  8668. };
  8669. /*************************************************************************
  8670. * DYTC Platform Profile interface
  8671. */
  8672. #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
  8673. #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
  8674. #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
  8675. #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
  8676. #define DYTC_FC_MMC 27 /* MMC Mode supported */
  8677. #define DYTC_FC_PSC 29 /* PSC Mode supported */
  8678. #define DYTC_FC_AMT 31 /* AMT mode supported */
  8679. #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
  8680. #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
  8681. #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
  8682. #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
  8683. #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
  8684. #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
  8685. #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
  8686. #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
  8687. #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
  8688. #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */
  8689. #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */
  8690. #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */
  8691. #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
  8692. #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
  8693. #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
  8694. #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
  8695. #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
  8696. #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
  8697. #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
  8698. #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
  8699. #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
  8700. #define DYTC_SET_COMMAND(function, mode, on) \
  8701. (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
  8702. (mode) << DYTC_SET_MODE_BIT | \
  8703. (on) << DYTC_SET_VALID_BIT)
  8704. #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
  8705. #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
  8706. static int dytc_control_amt(bool enable);
  8707. static bool dytc_amt_active;
  8708. static enum platform_profile_option dytc_current_profile;
  8709. static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
  8710. static DEFINE_MUTEX(dytc_mutex);
  8711. static int dytc_capabilities;
  8712. static bool dytc_mmc_get_available;
  8713. static int profile_force;
  8714. static int convert_dytc_to_profile(int funcmode, int dytcmode,
  8715. enum platform_profile_option *profile)
  8716. {
  8717. switch (funcmode) {
  8718. case DYTC_FUNCTION_MMC:
  8719. switch (dytcmode) {
  8720. case DYTC_MODE_MMC_LOWPOWER:
  8721. *profile = PLATFORM_PROFILE_LOW_POWER;
  8722. break;
  8723. case DYTC_MODE_MMC_DEFAULT:
  8724. case DYTC_MODE_MMC_BALANCE:
  8725. *profile = PLATFORM_PROFILE_BALANCED;
  8726. break;
  8727. case DYTC_MODE_MMC_PERFORM:
  8728. *profile = PLATFORM_PROFILE_PERFORMANCE;
  8729. break;
  8730. default: /* Unknown mode */
  8731. return -EINVAL;
  8732. }
  8733. return 0;
  8734. case DYTC_FUNCTION_PSC:
  8735. switch (dytcmode) {
  8736. case DYTC_MODE_PSC_LOWPOWER:
  8737. *profile = PLATFORM_PROFILE_LOW_POWER;
  8738. break;
  8739. case DYTC_MODE_PSC_BALANCE:
  8740. *profile = PLATFORM_PROFILE_BALANCED;
  8741. break;
  8742. case DYTC_MODE_PSC_PERFORM:
  8743. *profile = PLATFORM_PROFILE_PERFORMANCE;
  8744. break;
  8745. default: /* Unknown mode */
  8746. return -EINVAL;
  8747. }
  8748. return 0;
  8749. case DYTC_FUNCTION_AMT:
  8750. /* For now return balanced. It's the closest we have to 'auto' */
  8751. *profile = PLATFORM_PROFILE_BALANCED;
  8752. return 0;
  8753. default:
  8754. /* Unknown function */
  8755. return -EOPNOTSUPP;
  8756. }
  8757. return 0;
  8758. }
  8759. static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
  8760. {
  8761. switch (profile) {
  8762. case PLATFORM_PROFILE_LOW_POWER:
  8763. if (dytc_capabilities & BIT(DYTC_FC_MMC))
  8764. *perfmode = DYTC_MODE_MMC_LOWPOWER;
  8765. else if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8766. *perfmode = DYTC_MODE_PSC_LOWPOWER;
  8767. break;
  8768. case PLATFORM_PROFILE_BALANCED:
  8769. if (dytc_capabilities & BIT(DYTC_FC_MMC))
  8770. *perfmode = DYTC_MODE_MMC_BALANCE;
  8771. else if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8772. *perfmode = DYTC_MODE_PSC_BALANCE;
  8773. break;
  8774. case PLATFORM_PROFILE_PERFORMANCE:
  8775. if (dytc_capabilities & BIT(DYTC_FC_MMC))
  8776. *perfmode = DYTC_MODE_MMC_PERFORM;
  8777. else if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8778. *perfmode = DYTC_MODE_PSC_PERFORM;
  8779. break;
  8780. default: /* Unknown profile */
  8781. return -EOPNOTSUPP;
  8782. }
  8783. return 0;
  8784. }
  8785. /*
  8786. * dytc_profile_get: Function to register with platform_profile
  8787. * handler. Returns current platform profile.
  8788. */
  8789. static int dytc_profile_get(struct platform_profile_handler *pprof,
  8790. enum platform_profile_option *profile)
  8791. {
  8792. *profile = dytc_current_profile;
  8793. return 0;
  8794. }
  8795. static int dytc_control_amt(bool enable)
  8796. {
  8797. int dummy;
  8798. int err;
  8799. int cmd;
  8800. if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) {
  8801. pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n");
  8802. return -ENODEV;
  8803. }
  8804. if (enable)
  8805. cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable);
  8806. else
  8807. cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable);
  8808. pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd);
  8809. err = dytc_command(cmd, &dummy);
  8810. if (err)
  8811. return err;
  8812. dytc_amt_active = enable;
  8813. return 0;
  8814. }
  8815. /*
  8816. * Helper function - check if we are in CQL mode and if we are
  8817. * - disable CQL,
  8818. * - run the command
  8819. * - enable CQL
  8820. * If not in CQL mode, just run the command
  8821. */
  8822. static int dytc_cql_command(int command, int *output)
  8823. {
  8824. int err, cmd_err, dummy;
  8825. int cur_funcmode;
  8826. /* Determine if we are in CQL mode. This alters the commands we do */
  8827. err = dytc_command(DYTC_CMD_GET, output);
  8828. if (err)
  8829. return err;
  8830. cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
  8831. /* Check if we're OK to return immediately */
  8832. if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
  8833. return 0;
  8834. if (cur_funcmode == DYTC_FUNCTION_CQL) {
  8835. atomic_inc(&dytc_ignore_event);
  8836. err = dytc_command(DYTC_DISABLE_CQL, &dummy);
  8837. if (err)
  8838. return err;
  8839. }
  8840. cmd_err = dytc_command(command, output);
  8841. /* Check return condition after we've restored CQL state */
  8842. if (cur_funcmode == DYTC_FUNCTION_CQL) {
  8843. err = dytc_command(DYTC_ENABLE_CQL, &dummy);
  8844. if (err)
  8845. return err;
  8846. }
  8847. return cmd_err;
  8848. }
  8849. /*
  8850. * dytc_profile_set: Function to register with platform_profile
  8851. * handler. Sets current platform profile.
  8852. */
  8853. static int dytc_profile_set(struct platform_profile_handler *pprof,
  8854. enum platform_profile_option profile)
  8855. {
  8856. int perfmode;
  8857. int output;
  8858. int err;
  8859. err = mutex_lock_interruptible(&dytc_mutex);
  8860. if (err)
  8861. return err;
  8862. err = convert_profile_to_dytc(profile, &perfmode);
  8863. if (err)
  8864. goto unlock;
  8865. if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
  8866. if (profile == PLATFORM_PROFILE_BALANCED) {
  8867. /*
  8868. * To get back to balanced mode we need to issue a reset command.
  8869. * Note we still need to disable CQL mode before hand and re-enable
  8870. * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
  8871. * stuck at 0 for aprox. 30 minutes.
  8872. */
  8873. err = dytc_cql_command(DYTC_CMD_RESET, &output);
  8874. if (err)
  8875. goto unlock;
  8876. } else {
  8877. /* Determine if we are in CQL mode. This alters the commands we do */
  8878. err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
  8879. &output);
  8880. if (err)
  8881. goto unlock;
  8882. }
  8883. } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
  8884. err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
  8885. if (err)
  8886. goto unlock;
  8887. /* system supports AMT, activate it when on balanced */
  8888. if (dytc_capabilities & BIT(DYTC_FC_AMT))
  8889. dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED);
  8890. }
  8891. /* Success - update current profile */
  8892. dytc_current_profile = profile;
  8893. unlock:
  8894. mutex_unlock(&dytc_mutex);
  8895. return err;
  8896. }
  8897. static void dytc_profile_refresh(void)
  8898. {
  8899. enum platform_profile_option profile;
  8900. int output = 0, err = 0;
  8901. int perfmode, funcmode = 0;
  8902. mutex_lock(&dytc_mutex);
  8903. if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
  8904. if (dytc_mmc_get_available)
  8905. err = dytc_command(DYTC_CMD_MMC_GET, &output);
  8906. else
  8907. err = dytc_cql_command(DYTC_CMD_GET, &output);
  8908. funcmode = DYTC_FUNCTION_MMC;
  8909. } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
  8910. err = dytc_command(DYTC_CMD_GET, &output);
  8911. /* Check if we are PSC mode, or have AMT enabled */
  8912. funcmode = (output >> DYTC_GET_FUNCTION_BIT) & 0xF;
  8913. } else { /* Unknown profile mode */
  8914. err = -ENODEV;
  8915. }
  8916. mutex_unlock(&dytc_mutex);
  8917. if (err)
  8918. return;
  8919. perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
  8920. convert_dytc_to_profile(funcmode, perfmode, &profile);
  8921. if (profile != dytc_current_profile) {
  8922. dytc_current_profile = profile;
  8923. platform_profile_notify();
  8924. }
  8925. }
  8926. static struct platform_profile_handler dytc_profile = {
  8927. .profile_get = dytc_profile_get,
  8928. .profile_set = dytc_profile_set,
  8929. };
  8930. static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
  8931. {
  8932. int err, output;
  8933. /* Setup supported modes */
  8934. set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
  8935. set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
  8936. set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
  8937. err = dytc_command(DYTC_CMD_QUERY, &output);
  8938. if (err)
  8939. return err;
  8940. if (output & BIT(DYTC_QUERY_ENABLE_BIT))
  8941. dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
  8942. /* Check DYTC is enabled and supports mode setting */
  8943. if (dytc_version < 5)
  8944. return -ENODEV;
  8945. /* Check what capabilities are supported */
  8946. err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities);
  8947. if (err)
  8948. return err;
  8949. /* Check if user wants to override the profile selection */
  8950. if (profile_force) {
  8951. switch (profile_force) {
  8952. case -1:
  8953. dytc_capabilities = 0;
  8954. break;
  8955. case 1:
  8956. dytc_capabilities = BIT(DYTC_FC_MMC);
  8957. break;
  8958. case 2:
  8959. dytc_capabilities = BIT(DYTC_FC_PSC);
  8960. break;
  8961. }
  8962. pr_debug("Profile selection forced: 0x%x\n", dytc_capabilities);
  8963. }
  8964. if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */
  8965. pr_debug("MMC is supported\n");
  8966. /*
  8967. * Check if MMC_GET functionality available
  8968. * Version > 6 and return success from MMC_GET command
  8969. */
  8970. dytc_mmc_get_available = false;
  8971. if (dytc_version >= 6) {
  8972. err = dytc_command(DYTC_CMD_MMC_GET, &output);
  8973. if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
  8974. dytc_mmc_get_available = true;
  8975. }
  8976. } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */
  8977. pr_debug("PSC is supported\n");
  8978. } else {
  8979. dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
  8980. return -ENODEV;
  8981. }
  8982. dbg_printk(TPACPI_DBG_INIT,
  8983. "DYTC version %d: thermal mode available\n", dytc_version);
  8984. /* Create platform_profile structure and register */
  8985. err = platform_profile_register(&dytc_profile);
  8986. /*
  8987. * If for some reason platform_profiles aren't enabled
  8988. * don't quit terminally.
  8989. */
  8990. if (err)
  8991. return -ENODEV;
  8992. /* Ensure initial values are correct */
  8993. dytc_profile_refresh();
  8994. /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */
  8995. if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8996. dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
  8997. return 0;
  8998. }
  8999. static void dytc_profile_exit(void)
  9000. {
  9001. platform_profile_remove();
  9002. }
  9003. static struct ibm_struct dytc_profile_driver_data = {
  9004. .name = "dytc-profile",
  9005. .exit = dytc_profile_exit,
  9006. };
  9007. /*************************************************************************
  9008. * Keyboard language interface
  9009. */
  9010. struct keyboard_lang_data {
  9011. const char *lang_str;
  9012. int lang_code;
  9013. };
  9014. static const struct keyboard_lang_data keyboard_lang_data[] = {
  9015. {"be", 0x080c},
  9016. {"cz", 0x0405},
  9017. {"da", 0x0406},
  9018. {"de", 0x0c07},
  9019. {"en", 0x0000},
  9020. {"es", 0x2c0a},
  9021. {"et", 0x0425},
  9022. {"fr", 0x040c},
  9023. {"fr-ch", 0x100c},
  9024. {"hu", 0x040e},
  9025. {"it", 0x0410},
  9026. {"jp", 0x0411},
  9027. {"nl", 0x0413},
  9028. {"nn", 0x0414},
  9029. {"pl", 0x0415},
  9030. {"pt", 0x0816},
  9031. {"sl", 0x041b},
  9032. {"sv", 0x081d},
  9033. {"tr", 0x041f},
  9034. };
  9035. static int set_keyboard_lang_command(int command)
  9036. {
  9037. acpi_handle sskl_handle;
  9038. int output;
  9039. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
  9040. /* Platform doesn't support SSKL */
  9041. return -ENODEV;
  9042. }
  9043. if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
  9044. return -EIO;
  9045. return 0;
  9046. }
  9047. static int get_keyboard_lang(int *output)
  9048. {
  9049. acpi_handle gskl_handle;
  9050. int kbd_lang;
  9051. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
  9052. /* Platform doesn't support GSKL */
  9053. return -ENODEV;
  9054. }
  9055. if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
  9056. return -EIO;
  9057. /*
  9058. * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
  9059. * '(' and ')') keys which use layout dependent key-press emulation.
  9060. */
  9061. if (kbd_lang & METHOD_ERR)
  9062. return -ENODEV;
  9063. *output = kbd_lang;
  9064. return 0;
  9065. }
  9066. /* sysfs keyboard language entry */
  9067. static ssize_t keyboard_lang_show(struct device *dev,
  9068. struct device_attribute *attr,
  9069. char *buf)
  9070. {
  9071. int output, err, i, len = 0;
  9072. err = get_keyboard_lang(&output);
  9073. if (err)
  9074. return err;
  9075. for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
  9076. if (i)
  9077. len += sysfs_emit_at(buf, len, "%s", " ");
  9078. if (output == keyboard_lang_data[i].lang_code) {
  9079. len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
  9080. } else {
  9081. len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
  9082. }
  9083. }
  9084. len += sysfs_emit_at(buf, len, "\n");
  9085. return len;
  9086. }
  9087. static ssize_t keyboard_lang_store(struct device *dev,
  9088. struct device_attribute *attr,
  9089. const char *buf, size_t count)
  9090. {
  9091. int err, i;
  9092. bool lang_found = false;
  9093. int lang_code = 0;
  9094. for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
  9095. if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
  9096. lang_code = keyboard_lang_data[i].lang_code;
  9097. lang_found = true;
  9098. break;
  9099. }
  9100. }
  9101. if (lang_found) {
  9102. lang_code = lang_code | 1 << 24;
  9103. /* Set language code */
  9104. err = set_keyboard_lang_command(lang_code);
  9105. if (err)
  9106. return err;
  9107. } else {
  9108. dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
  9109. return -EINVAL;
  9110. }
  9111. tpacpi_disclose_usertask(attr->attr.name,
  9112. "keyboard language is set to %s\n", buf);
  9113. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
  9114. return count;
  9115. }
  9116. static DEVICE_ATTR_RW(keyboard_lang);
  9117. static struct attribute *kbdlang_attributes[] = {
  9118. &dev_attr_keyboard_lang.attr,
  9119. NULL
  9120. };
  9121. static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
  9122. struct attribute *attr, int n)
  9123. {
  9124. return tp_features.kbd_lang ? attr->mode : 0;
  9125. }
  9126. static const struct attribute_group kbdlang_attr_group = {
  9127. .is_visible = kbdlang_attr_is_visible,
  9128. .attrs = kbdlang_attributes,
  9129. };
  9130. static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
  9131. {
  9132. int err, output;
  9133. err = get_keyboard_lang(&output);
  9134. tp_features.kbd_lang = !err;
  9135. return err;
  9136. }
  9137. static struct ibm_struct kbdlang_driver_data = {
  9138. .name = "kbdlang",
  9139. };
  9140. /*************************************************************************
  9141. * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
  9142. * and WLAN feature.
  9143. */
  9144. #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
  9145. #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
  9146. #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
  9147. static bool has_antennatype;
  9148. static int wwan_antennatype;
  9149. static int dprc_command(int command, int *output)
  9150. {
  9151. acpi_handle dprc_handle;
  9152. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
  9153. /* Platform doesn't support DPRC */
  9154. return -ENODEV;
  9155. }
  9156. if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
  9157. return -EIO;
  9158. /*
  9159. * METHOD_ERR gets returned on devices where few commands are not supported
  9160. * for example command to get WWAN Antenna type command is not supported on
  9161. * some devices.
  9162. */
  9163. if (*output & METHOD_ERR)
  9164. return -ENODEV;
  9165. return 0;
  9166. }
  9167. static int get_wwan_antenna(int *wwan_antennatype)
  9168. {
  9169. int output, err;
  9170. /* Get current Antenna type */
  9171. err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
  9172. if (err)
  9173. return err;
  9174. if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
  9175. *wwan_antennatype = 1;
  9176. else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
  9177. *wwan_antennatype = 2;
  9178. else
  9179. return -ENODEV;
  9180. return 0;
  9181. }
  9182. /* sysfs wwan antenna type entry */
  9183. static ssize_t wwan_antenna_type_show(struct device *dev,
  9184. struct device_attribute *attr,
  9185. char *buf)
  9186. {
  9187. switch (wwan_antennatype) {
  9188. case 1:
  9189. return sysfs_emit(buf, "type a\n");
  9190. case 2:
  9191. return sysfs_emit(buf, "type b\n");
  9192. default:
  9193. return -ENODATA;
  9194. }
  9195. }
  9196. static DEVICE_ATTR_RO(wwan_antenna_type);
  9197. static struct attribute *dprc_attributes[] = {
  9198. &dev_attr_wwan_antenna_type.attr,
  9199. NULL
  9200. };
  9201. static umode_t dprc_attr_is_visible(struct kobject *kobj,
  9202. struct attribute *attr, int n)
  9203. {
  9204. return has_antennatype ? attr->mode : 0;
  9205. }
  9206. static const struct attribute_group dprc_attr_group = {
  9207. .is_visible = dprc_attr_is_visible,
  9208. .attrs = dprc_attributes,
  9209. };
  9210. static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
  9211. {
  9212. int err;
  9213. err = get_wwan_antenna(&wwan_antennatype);
  9214. if (err)
  9215. return err;
  9216. has_antennatype = true;
  9217. return 0;
  9218. }
  9219. static struct ibm_struct dprc_driver_data = {
  9220. .name = "dprc",
  9221. };
  9222. /* --------------------------------------------------------------------- */
  9223. static struct attribute *tpacpi_driver_attributes[] = {
  9224. &driver_attr_debug_level.attr,
  9225. &driver_attr_version.attr,
  9226. &driver_attr_interface_version.attr,
  9227. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9228. &driver_attr_wlsw_emulstate.attr,
  9229. &driver_attr_bluetooth_emulstate.attr,
  9230. &driver_attr_wwan_emulstate.attr,
  9231. &driver_attr_uwb_emulstate.attr,
  9232. #endif
  9233. NULL
  9234. };
  9235. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9236. static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
  9237. struct attribute *attr, int n)
  9238. {
  9239. if (attr == &driver_attr_wlsw_emulstate.attr) {
  9240. if (!dbg_wlswemul)
  9241. return 0;
  9242. } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
  9243. if (!dbg_bluetoothemul)
  9244. return 0;
  9245. } else if (attr == &driver_attr_wwan_emulstate.attr) {
  9246. if (!dbg_wwanemul)
  9247. return 0;
  9248. } else if (attr == &driver_attr_uwb_emulstate.attr) {
  9249. if (!dbg_uwbemul)
  9250. return 0;
  9251. }
  9252. return attr->mode;
  9253. }
  9254. #endif
  9255. static const struct attribute_group tpacpi_driver_attr_group = {
  9256. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9257. .is_visible = tpacpi_attr_is_visible,
  9258. #endif
  9259. .attrs = tpacpi_driver_attributes,
  9260. };
  9261. static const struct attribute_group *tpacpi_driver_groups[] = {
  9262. &tpacpi_driver_attr_group,
  9263. NULL,
  9264. };
  9265. static const struct attribute_group *tpacpi_groups[] = {
  9266. &adaptive_kbd_attr_group,
  9267. &hotkey_attr_group,
  9268. &bluetooth_attr_group,
  9269. &wan_attr_group,
  9270. &cmos_attr_group,
  9271. &proxsensor_attr_group,
  9272. &kbdlang_attr_group,
  9273. &dprc_attr_group,
  9274. NULL,
  9275. };
  9276. static const struct attribute_group *tpacpi_hwmon_groups[] = {
  9277. &thermal_attr_group,
  9278. &temp_label_attr_group,
  9279. &fan_attr_group,
  9280. NULL,
  9281. };
  9282. static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
  9283. &fan_driver_attr_group,
  9284. NULL,
  9285. };
  9286. /****************************************************************************
  9287. ****************************************************************************
  9288. *
  9289. * Platform drivers
  9290. *
  9291. ****************************************************************************
  9292. ****************************************************************************/
  9293. static struct platform_driver tpacpi_pdriver = {
  9294. .driver = {
  9295. .name = TPACPI_DRVR_NAME,
  9296. .pm = &tpacpi_pm,
  9297. .groups = tpacpi_driver_groups,
  9298. .dev_groups = tpacpi_groups,
  9299. },
  9300. .shutdown = tpacpi_shutdown_handler,
  9301. };
  9302. static struct platform_driver tpacpi_hwmon_pdriver = {
  9303. .driver = {
  9304. .name = TPACPI_HWMON_DRVR_NAME,
  9305. .groups = tpacpi_hwmon_driver_groups,
  9306. },
  9307. };
  9308. /****************************************************************************
  9309. ****************************************************************************
  9310. *
  9311. * Infrastructure
  9312. *
  9313. ****************************************************************************
  9314. ****************************************************************************/
  9315. /*
  9316. * HKEY event callout for other subdrivers go here
  9317. * (yes, it is ugly, but it is quick, safe, and gets the job done
  9318. */
  9319. static void tpacpi_driver_event(const unsigned int hkey_event)
  9320. {
  9321. if (ibm_backlight_device) {
  9322. switch (hkey_event) {
  9323. case TP_HKEY_EV_BRGHT_UP:
  9324. case TP_HKEY_EV_BRGHT_DOWN:
  9325. tpacpi_brightness_notify_change();
  9326. }
  9327. }
  9328. if (alsa_card) {
  9329. switch (hkey_event) {
  9330. case TP_HKEY_EV_VOL_UP:
  9331. case TP_HKEY_EV_VOL_DOWN:
  9332. case TP_HKEY_EV_VOL_MUTE:
  9333. volume_alsa_notify_change();
  9334. }
  9335. }
  9336. if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
  9337. enum led_brightness brightness;
  9338. mutex_lock(&kbdlight_mutex);
  9339. /*
  9340. * Check the brightness actually changed, setting the brightness
  9341. * through kbdlight_set_level() also triggers this event.
  9342. */
  9343. brightness = kbdlight_sysfs_get(NULL);
  9344. if (kbdlight_brightness != brightness) {
  9345. kbdlight_brightness = brightness;
  9346. led_classdev_notify_brightness_hw_changed(
  9347. &tpacpi_led_kbdlight.led_classdev, brightness);
  9348. }
  9349. mutex_unlock(&kbdlight_mutex);
  9350. }
  9351. if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
  9352. lapsensor_refresh();
  9353. /* If we are already accessing DYTC then skip dytc update */
  9354. if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
  9355. dytc_profile_refresh();
  9356. }
  9357. if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
  9358. enum drm_privacy_screen_status old_hw_state;
  9359. bool changed;
  9360. mutex_lock(&lcdshadow_dev->lock);
  9361. old_hw_state = lcdshadow_dev->hw_state;
  9362. lcdshadow_get_hw_state(lcdshadow_dev);
  9363. changed = lcdshadow_dev->hw_state != old_hw_state;
  9364. mutex_unlock(&lcdshadow_dev->lock);
  9365. if (changed)
  9366. drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
  9367. }
  9368. if (hkey_event == TP_HKEY_EV_AMT_TOGGLE) {
  9369. /* If we're enabling AMT we need to force balanced mode */
  9370. if (!dytc_amt_active)
  9371. /* This will also set AMT mode enabled */
  9372. dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
  9373. else
  9374. dytc_control_amt(!dytc_amt_active);
  9375. }
  9376. }
  9377. static void hotkey_driver_event(const unsigned int scancode)
  9378. {
  9379. tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
  9380. }
  9381. /* --------------------------------------------------------------------- */
  9382. /* /proc support */
  9383. static struct proc_dir_entry *proc_dir;
  9384. /*
  9385. * Module and infrastructure proble, init and exit handling
  9386. */
  9387. static bool force_load;
  9388. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  9389. static const char * __init str_supported(int is_supported)
  9390. {
  9391. static char text_unsupported[] __initdata = "not supported";
  9392. return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
  9393. }
  9394. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  9395. static void ibm_exit(struct ibm_struct *ibm)
  9396. {
  9397. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  9398. list_del_init(&ibm->all_drivers);
  9399. if (ibm->flags.acpi_notify_installed) {
  9400. dbg_printk(TPACPI_DBG_EXIT,
  9401. "%s: acpi_remove_notify_handler\n", ibm->name);
  9402. BUG_ON(!ibm->acpi);
  9403. acpi_remove_notify_handler(*ibm->acpi->handle,
  9404. ibm->acpi->type,
  9405. dispatch_acpi_notify);
  9406. ibm->flags.acpi_notify_installed = 0;
  9407. }
  9408. if (ibm->flags.proc_created) {
  9409. dbg_printk(TPACPI_DBG_EXIT,
  9410. "%s: remove_proc_entry\n", ibm->name);
  9411. remove_proc_entry(ibm->name, proc_dir);
  9412. ibm->flags.proc_created = 0;
  9413. }
  9414. if (ibm->flags.acpi_driver_registered) {
  9415. dbg_printk(TPACPI_DBG_EXIT,
  9416. "%s: acpi_bus_unregister_driver\n", ibm->name);
  9417. BUG_ON(!ibm->acpi);
  9418. acpi_bus_unregister_driver(ibm->acpi->driver);
  9419. kfree(ibm->acpi->driver);
  9420. ibm->acpi->driver = NULL;
  9421. ibm->flags.acpi_driver_registered = 0;
  9422. }
  9423. if (ibm->flags.init_called && ibm->exit) {
  9424. ibm->exit();
  9425. ibm->flags.init_called = 0;
  9426. }
  9427. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  9428. }
  9429. static int __init ibm_init(struct ibm_init_struct *iibm)
  9430. {
  9431. int ret;
  9432. struct ibm_struct *ibm = iibm->data;
  9433. struct proc_dir_entry *entry;
  9434. BUG_ON(ibm == NULL);
  9435. INIT_LIST_HEAD(&ibm->all_drivers);
  9436. if (ibm->flags.experimental && !experimental)
  9437. return 0;
  9438. dbg_printk(TPACPI_DBG_INIT,
  9439. "probing for %s\n", ibm->name);
  9440. if (iibm->init) {
  9441. ret = iibm->init(iibm);
  9442. if (ret > 0 || ret == -ENODEV)
  9443. return 0; /* subdriver functionality not available */
  9444. if (ret)
  9445. return ret;
  9446. ibm->flags.init_called = 1;
  9447. }
  9448. if (ibm->acpi) {
  9449. if (ibm->acpi->hid) {
  9450. ret = register_tpacpi_subdriver(ibm);
  9451. if (ret)
  9452. goto err_out;
  9453. }
  9454. if (ibm->acpi->notify) {
  9455. ret = setup_acpi_notify(ibm);
  9456. if (ret == -ENODEV) {
  9457. pr_notice("disabling subdriver %s\n",
  9458. ibm->name);
  9459. ret = 0;
  9460. goto err_out;
  9461. }
  9462. if (ret < 0)
  9463. goto err_out;
  9464. }
  9465. }
  9466. dbg_printk(TPACPI_DBG_INIT,
  9467. "%s installed\n", ibm->name);
  9468. if (ibm->read) {
  9469. umode_t mode = iibm->base_procfs_mode;
  9470. if (!mode)
  9471. mode = S_IRUGO;
  9472. if (ibm->write)
  9473. mode |= S_IWUSR;
  9474. entry = proc_create_data(ibm->name, mode, proc_dir,
  9475. &dispatch_proc_ops, ibm);
  9476. if (!entry) {
  9477. pr_err("unable to create proc entry %s\n", ibm->name);
  9478. ret = -ENODEV;
  9479. goto err_out;
  9480. }
  9481. ibm->flags.proc_created = 1;
  9482. }
  9483. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  9484. return 0;
  9485. err_out:
  9486. dbg_printk(TPACPI_DBG_INIT,
  9487. "%s: at error exit path with result %d\n",
  9488. ibm->name, ret);
  9489. ibm_exit(ibm);
  9490. return (ret < 0) ? ret : 0;
  9491. }
  9492. /* Probing */
  9493. static char __init tpacpi_parse_fw_id(const char * const s,
  9494. u32 *model, u16 *release)
  9495. {
  9496. int i;
  9497. if (!s || strlen(s) < 8)
  9498. goto invalid;
  9499. for (i = 0; i < 8; i++)
  9500. if (!((s[i] >= '0' && s[i] <= '9') ||
  9501. (s[i] >= 'A' && s[i] <= 'Z')))
  9502. goto invalid;
  9503. /*
  9504. * Most models: xxyTkkWW (#.##c)
  9505. * Ancient 570/600 and -SL lacks (#.##c)
  9506. */
  9507. if (s[3] == 'T' || s[3] == 'N') {
  9508. *model = TPID(s[0], s[1]);
  9509. *release = TPVER(s[4], s[5]);
  9510. return s[2];
  9511. /* New models: xxxyTkkW (#.##c); T550 and some others */
  9512. } else if (s[4] == 'T' || s[4] == 'N') {
  9513. *model = TPID3(s[0], s[1], s[2]);
  9514. *release = TPVER(s[5], s[6]);
  9515. return s[3];
  9516. }
  9517. invalid:
  9518. return '\0';
  9519. }
  9520. static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
  9521. {
  9522. char *ec_fw_string = (char *) private;
  9523. const char *dmi_data = (const char *)dm;
  9524. /*
  9525. * ThinkPad Embedded Controller Program Table on newer models
  9526. *
  9527. * Offset | Name | Width | Description
  9528. * ----------------------------------------------------
  9529. * 0x00 | Type | BYTE | 0x8C
  9530. * 0x01 | Length | BYTE |
  9531. * 0x02 | Handle | WORD | Varies
  9532. * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
  9533. * 0x0A | OEM struct offset | BYTE | 0x0B
  9534. * 0x0B | OEM struct number | BYTE | 0x07, for this structure
  9535. * 0x0C | OEM struct revision | BYTE | 0x01, for this format
  9536. * 0x0D | ECP version ID | STR ID |
  9537. * 0x0E | ECP release date | STR ID |
  9538. */
  9539. /* Return if data structure not match */
  9540. if (dm->type != 140 || dm->length < 0x0F ||
  9541. memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
  9542. dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
  9543. dmi_data[0x0C] != 0x01)
  9544. return;
  9545. /* fwstr is the first 8byte string */
  9546. strncpy(ec_fw_string, dmi_data + 0x0F, 8);
  9547. }
  9548. /* returns 0 - probe ok, or < 0 - probe error.
  9549. * Probe ok doesn't mean thinkpad found.
  9550. * On error, kfree() cleanup on tp->* is not performed, caller must do it */
  9551. static int __must_check __init get_thinkpad_model_data(
  9552. struct thinkpad_id_data *tp)
  9553. {
  9554. const struct dmi_device *dev = NULL;
  9555. char ec_fw_string[18] = {0};
  9556. char const *s;
  9557. char t;
  9558. if (!tp)
  9559. return -EINVAL;
  9560. memset(tp, 0, sizeof(*tp));
  9561. if (dmi_name_in_vendors("IBM"))
  9562. tp->vendor = PCI_VENDOR_ID_IBM;
  9563. else if (dmi_name_in_vendors("LENOVO"))
  9564. tp->vendor = PCI_VENDOR_ID_LENOVO;
  9565. else
  9566. return 0;
  9567. s = dmi_get_system_info(DMI_BIOS_VERSION);
  9568. tp->bios_version_str = kstrdup(s, GFP_KERNEL);
  9569. if (s && !tp->bios_version_str)
  9570. return -ENOMEM;
  9571. /* Really ancient ThinkPad 240X will fail this, which is fine */
  9572. t = tpacpi_parse_fw_id(tp->bios_version_str,
  9573. &tp->bios_model, &tp->bios_release);
  9574. if (t != 'E' && t != 'C')
  9575. return 0;
  9576. /*
  9577. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  9578. * X32 or newer, all Z series; Some models must have an
  9579. * up-to-date BIOS or they will not be detected.
  9580. *
  9581. * See https://thinkwiki.org/wiki/List_of_DMI_IDs
  9582. */
  9583. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  9584. if (sscanf(dev->name,
  9585. "IBM ThinkPad Embedded Controller -[%17c",
  9586. ec_fw_string) == 1) {
  9587. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  9588. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  9589. break;
  9590. }
  9591. }
  9592. /* Newer ThinkPads have different EC program info table */
  9593. if (!ec_fw_string[0])
  9594. dmi_walk(find_new_ec_fwstr, &ec_fw_string);
  9595. if (ec_fw_string[0]) {
  9596. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  9597. if (!tp->ec_version_str)
  9598. return -ENOMEM;
  9599. t = tpacpi_parse_fw_id(ec_fw_string,
  9600. &tp->ec_model, &tp->ec_release);
  9601. if (t != 'H') {
  9602. pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
  9603. ec_fw_string);
  9604. pr_notice("please report this to %s\n", TPACPI_MAIL);
  9605. }
  9606. }
  9607. s = dmi_get_system_info(DMI_PRODUCT_VERSION);
  9608. if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
  9609. tp->model_str = kstrdup(s, GFP_KERNEL);
  9610. if (!tp->model_str)
  9611. return -ENOMEM;
  9612. } else {
  9613. s = dmi_get_system_info(DMI_BIOS_VENDOR);
  9614. if (s && !(strncasecmp(s, "Lenovo", 6))) {
  9615. tp->model_str = kstrdup(s, GFP_KERNEL);
  9616. if (!tp->model_str)
  9617. return -ENOMEM;
  9618. }
  9619. }
  9620. s = dmi_get_system_info(DMI_PRODUCT_NAME);
  9621. tp->nummodel_str = kstrdup(s, GFP_KERNEL);
  9622. if (s && !tp->nummodel_str)
  9623. return -ENOMEM;
  9624. return 0;
  9625. }
  9626. static int __init probe_for_thinkpad(void)
  9627. {
  9628. int is_thinkpad;
  9629. if (acpi_disabled)
  9630. return -ENODEV;
  9631. /* It would be dangerous to run the driver in this case */
  9632. if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
  9633. return -ENODEV;
  9634. /*
  9635. * Non-ancient models have better DMI tagging, but very old models
  9636. * don't. tpacpi_is_fw_known() is a cheat to help in that case.
  9637. */
  9638. is_thinkpad = (thinkpad_id.model_str != NULL) ||
  9639. (thinkpad_id.ec_model != 0) ||
  9640. tpacpi_is_fw_known();
  9641. /* The EC handler is required */
  9642. tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
  9643. if (!ec_handle) {
  9644. if (is_thinkpad)
  9645. pr_err("Not yet supported ThinkPad detected!\n");
  9646. return -ENODEV;
  9647. }
  9648. if (!is_thinkpad && !force_load)
  9649. return -ENODEV;
  9650. return 0;
  9651. }
  9652. static void __init thinkpad_acpi_init_banner(void)
  9653. {
  9654. pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
  9655. pr_info("%s\n", TPACPI_URL);
  9656. pr_info("ThinkPad BIOS %s, EC %s\n",
  9657. (thinkpad_id.bios_version_str) ?
  9658. thinkpad_id.bios_version_str : "unknown",
  9659. (thinkpad_id.ec_version_str) ?
  9660. thinkpad_id.ec_version_str : "unknown");
  9661. BUG_ON(!thinkpad_id.vendor);
  9662. if (thinkpad_id.model_str)
  9663. pr_info("%s %s, model %s\n",
  9664. (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
  9665. "IBM" : ((thinkpad_id.vendor ==
  9666. PCI_VENDOR_ID_LENOVO) ?
  9667. "Lenovo" : "Unknown vendor"),
  9668. thinkpad_id.model_str,
  9669. (thinkpad_id.nummodel_str) ?
  9670. thinkpad_id.nummodel_str : "unknown");
  9671. }
  9672. /* Module init, exit, parameters */
  9673. static struct ibm_init_struct ibms_init[] __initdata = {
  9674. {
  9675. .data = &thinkpad_acpi_driver_data,
  9676. },
  9677. {
  9678. .init = hotkey_init,
  9679. .data = &hotkey_driver_data,
  9680. },
  9681. {
  9682. .init = bluetooth_init,
  9683. .data = &bluetooth_driver_data,
  9684. },
  9685. {
  9686. .init = wan_init,
  9687. .data = &wan_driver_data,
  9688. },
  9689. {
  9690. .init = uwb_init,
  9691. .data = &uwb_driver_data,
  9692. },
  9693. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  9694. {
  9695. .init = video_init,
  9696. .base_procfs_mode = S_IRUSR,
  9697. .data = &video_driver_data,
  9698. },
  9699. #endif
  9700. {
  9701. .init = kbdlight_init,
  9702. .data = &kbdlight_driver_data,
  9703. },
  9704. {
  9705. .init = light_init,
  9706. .data = &light_driver_data,
  9707. },
  9708. {
  9709. .init = cmos_init,
  9710. .data = &cmos_driver_data,
  9711. },
  9712. {
  9713. .init = led_init,
  9714. .data = &led_driver_data,
  9715. },
  9716. {
  9717. .init = beep_init,
  9718. .data = &beep_driver_data,
  9719. },
  9720. {
  9721. .init = thermal_init,
  9722. .data = &thermal_driver_data,
  9723. },
  9724. {
  9725. .init = brightness_init,
  9726. .data = &brightness_driver_data,
  9727. },
  9728. {
  9729. .init = volume_init,
  9730. .data = &volume_driver_data,
  9731. },
  9732. {
  9733. .init = fan_init,
  9734. .data = &fan_driver_data,
  9735. },
  9736. {
  9737. .init = mute_led_init,
  9738. .data = &mute_led_driver_data,
  9739. },
  9740. {
  9741. .init = tpacpi_battery_init,
  9742. .data = &battery_driver_data,
  9743. },
  9744. {
  9745. .init = tpacpi_lcdshadow_init,
  9746. .data = &lcdshadow_driver_data,
  9747. },
  9748. {
  9749. .init = tpacpi_proxsensor_init,
  9750. .data = &proxsensor_driver_data,
  9751. },
  9752. {
  9753. .init = tpacpi_dytc_profile_init,
  9754. .data = &dytc_profile_driver_data,
  9755. },
  9756. {
  9757. .init = tpacpi_kbdlang_init,
  9758. .data = &kbdlang_driver_data,
  9759. },
  9760. {
  9761. .init = tpacpi_dprc_init,
  9762. .data = &dprc_driver_data,
  9763. },
  9764. };
  9765. static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
  9766. {
  9767. unsigned int i;
  9768. struct ibm_struct *ibm;
  9769. if (!kp || !kp->name || !val)
  9770. return -EINVAL;
  9771. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  9772. ibm = ibms_init[i].data;
  9773. if (!ibm || !ibm->name)
  9774. continue;
  9775. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  9776. if (strlen(val) > sizeof(ibms_init[i].param) - 1)
  9777. return -ENOSPC;
  9778. strcpy(ibms_init[i].param, val);
  9779. return 0;
  9780. }
  9781. }
  9782. return -EINVAL;
  9783. }
  9784. module_param(experimental, int, 0444);
  9785. MODULE_PARM_DESC(experimental,
  9786. "Enables experimental features when non-zero");
  9787. module_param_named(debug, dbg_level, uint, 0);
  9788. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  9789. module_param(force_load, bool, 0444);
  9790. MODULE_PARM_DESC(force_load,
  9791. "Attempts to load the driver even on a mis-identified ThinkPad when true");
  9792. module_param_named(fan_control, fan_control_allowed, bool, 0444);
  9793. MODULE_PARM_DESC(fan_control,
  9794. "Enables setting fan parameters features when true");
  9795. module_param_named(brightness_mode, brightness_mode, uint, 0444);
  9796. MODULE_PARM_DESC(brightness_mode,
  9797. "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
  9798. module_param(brightness_enable, uint, 0444);
  9799. MODULE_PARM_DESC(brightness_enable,
  9800. "Enables backlight control when 1, disables when 0");
  9801. #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
  9802. module_param_named(volume_mode, volume_mode, uint, 0444);
  9803. MODULE_PARM_DESC(volume_mode,
  9804. "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
  9805. module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
  9806. MODULE_PARM_DESC(volume_capabilities,
  9807. "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
  9808. module_param_named(volume_control, volume_control_allowed, bool, 0444);
  9809. MODULE_PARM_DESC(volume_control,
  9810. "Enables software override for the console audio control when true");
  9811. module_param_named(software_mute, software_mute_requested, bool, 0444);
  9812. MODULE_PARM_DESC(software_mute,
  9813. "Request full software mute control");
  9814. /* ALSA module API parameters */
  9815. module_param_named(index, alsa_index, int, 0444);
  9816. MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
  9817. module_param_named(id, alsa_id, charp, 0444);
  9818. MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
  9819. module_param_named(enable, alsa_enable, bool, 0444);
  9820. MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
  9821. #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  9822. /* The module parameter can't be read back, that's why 0 is used here */
  9823. #define TPACPI_PARAM(feature) \
  9824. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  9825. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
  9826. TPACPI_PARAM(hotkey);
  9827. TPACPI_PARAM(bluetooth);
  9828. TPACPI_PARAM(video);
  9829. TPACPI_PARAM(light);
  9830. TPACPI_PARAM(cmos);
  9831. TPACPI_PARAM(led);
  9832. TPACPI_PARAM(beep);
  9833. TPACPI_PARAM(brightness);
  9834. TPACPI_PARAM(volume);
  9835. TPACPI_PARAM(fan);
  9836. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9837. module_param(dbg_wlswemul, uint, 0444);
  9838. MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
  9839. module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
  9840. MODULE_PARM_DESC(wlsw_state,
  9841. "Initial state of the emulated WLSW switch");
  9842. module_param(dbg_bluetoothemul, uint, 0444);
  9843. MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
  9844. module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
  9845. MODULE_PARM_DESC(bluetooth_state,
  9846. "Initial state of the emulated bluetooth switch");
  9847. module_param(dbg_wwanemul, uint, 0444);
  9848. MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
  9849. module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
  9850. MODULE_PARM_DESC(wwan_state,
  9851. "Initial state of the emulated WWAN switch");
  9852. module_param(dbg_uwbemul, uint, 0444);
  9853. MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
  9854. module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
  9855. MODULE_PARM_DESC(uwb_state,
  9856. "Initial state of the emulated UWB switch");
  9857. #endif
  9858. module_param(profile_force, int, 0444);
  9859. MODULE_PARM_DESC(profile_force, "Force profile mode. -1=off, 1=MMC, 2=PSC");
  9860. static void thinkpad_acpi_module_exit(void)
  9861. {
  9862. struct ibm_struct *ibm, *itmp;
  9863. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  9864. #ifdef CONFIG_SUSPEND
  9865. if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio)
  9866. acpi_unregister_lps0_dev(&thinkpad_acpi_s2idle_dev_ops);
  9867. #endif
  9868. if (tpacpi_hwmon)
  9869. hwmon_device_unregister(tpacpi_hwmon);
  9870. if (tp_features.sensors_pdrv_registered)
  9871. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  9872. if (tp_features.platform_drv_registered)
  9873. platform_driver_unregister(&tpacpi_pdriver);
  9874. list_for_each_entry_safe_reverse(ibm, itmp,
  9875. &tpacpi_all_drivers,
  9876. all_drivers) {
  9877. ibm_exit(ibm);
  9878. }
  9879. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  9880. if (tpacpi_inputdev) {
  9881. if (tp_features.input_device_registered)
  9882. input_unregister_device(tpacpi_inputdev);
  9883. else
  9884. input_free_device(tpacpi_inputdev);
  9885. kfree(hotkey_keycode_map);
  9886. }
  9887. if (tpacpi_sensors_pdev)
  9888. platform_device_unregister(tpacpi_sensors_pdev);
  9889. if (tpacpi_pdev)
  9890. platform_device_unregister(tpacpi_pdev);
  9891. if (proc_dir)
  9892. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  9893. if (tpacpi_wq)
  9894. destroy_workqueue(tpacpi_wq);
  9895. kfree(thinkpad_id.bios_version_str);
  9896. kfree(thinkpad_id.ec_version_str);
  9897. kfree(thinkpad_id.model_str);
  9898. kfree(thinkpad_id.nummodel_str);
  9899. }
  9900. static int __init thinkpad_acpi_module_init(void)
  9901. {
  9902. const struct dmi_system_id *dmi_id;
  9903. int ret, i;
  9904. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  9905. /* Driver-level probe */
  9906. ret = get_thinkpad_model_data(&thinkpad_id);
  9907. if (ret) {
  9908. pr_err("unable to get DMI data: %d\n", ret);
  9909. thinkpad_acpi_module_exit();
  9910. return ret;
  9911. }
  9912. ret = probe_for_thinkpad();
  9913. if (ret) {
  9914. thinkpad_acpi_module_exit();
  9915. return ret;
  9916. }
  9917. /* Driver initialization */
  9918. thinkpad_acpi_init_banner();
  9919. tpacpi_check_outdated_fw();
  9920. TPACPI_ACPIHANDLE_INIT(ecrd);
  9921. TPACPI_ACPIHANDLE_INIT(ecwr);
  9922. tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
  9923. if (!tpacpi_wq) {
  9924. thinkpad_acpi_module_exit();
  9925. return -ENOMEM;
  9926. }
  9927. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  9928. if (!proc_dir) {
  9929. pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
  9930. thinkpad_acpi_module_exit();
  9931. return -ENODEV;
  9932. }
  9933. dmi_id = dmi_first_match(fwbug_list);
  9934. if (dmi_id)
  9935. tp_features.quirks = dmi_id->driver_data;
  9936. /* Device initialization */
  9937. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE,
  9938. NULL, 0);
  9939. if (IS_ERR(tpacpi_pdev)) {
  9940. ret = PTR_ERR(tpacpi_pdev);
  9941. tpacpi_pdev = NULL;
  9942. pr_err("unable to register platform device\n");
  9943. thinkpad_acpi_module_exit();
  9944. return ret;
  9945. }
  9946. tpacpi_sensors_pdev = platform_device_register_simple(
  9947. TPACPI_HWMON_DRVR_NAME,
  9948. PLATFORM_DEVID_NONE, NULL, 0);
  9949. if (IS_ERR(tpacpi_sensors_pdev)) {
  9950. ret = PTR_ERR(tpacpi_sensors_pdev);
  9951. tpacpi_sensors_pdev = NULL;
  9952. pr_err("unable to register hwmon platform device\n");
  9953. thinkpad_acpi_module_exit();
  9954. return ret;
  9955. }
  9956. mutex_init(&tpacpi_inputdev_send_mutex);
  9957. tpacpi_inputdev = input_allocate_device();
  9958. if (!tpacpi_inputdev) {
  9959. thinkpad_acpi_module_exit();
  9960. return -ENOMEM;
  9961. } else {
  9962. /* Prepare input device, but don't register */
  9963. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  9964. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  9965. tpacpi_inputdev->id.bustype = BUS_HOST;
  9966. tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
  9967. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  9968. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  9969. tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
  9970. }
  9971. /* Init subdriver dependencies */
  9972. tpacpi_detect_brightness_capabilities();
  9973. /* Init subdrivers */
  9974. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  9975. ret = ibm_init(&ibms_init[i]);
  9976. if (ret >= 0 && *ibms_init[i].param)
  9977. ret = ibms_init[i].data->write(ibms_init[i].param);
  9978. if (ret < 0) {
  9979. thinkpad_acpi_module_exit();
  9980. return ret;
  9981. }
  9982. }
  9983. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  9984. ret = platform_driver_register(&tpacpi_pdriver);
  9985. if (ret) {
  9986. pr_err("unable to register main platform driver\n");
  9987. thinkpad_acpi_module_exit();
  9988. return ret;
  9989. }
  9990. tp_features.platform_drv_registered = 1;
  9991. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  9992. if (ret) {
  9993. pr_err("unable to register hwmon platform driver\n");
  9994. thinkpad_acpi_module_exit();
  9995. return ret;
  9996. }
  9997. tp_features.sensors_pdrv_registered = 1;
  9998. tpacpi_hwmon = hwmon_device_register_with_groups(
  9999. &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
  10000. if (IS_ERR(tpacpi_hwmon)) {
  10001. ret = PTR_ERR(tpacpi_hwmon);
  10002. tpacpi_hwmon = NULL;
  10003. pr_err("unable to register hwmon device\n");
  10004. thinkpad_acpi_module_exit();
  10005. return ret;
  10006. }
  10007. ret = input_register_device(tpacpi_inputdev);
  10008. if (ret < 0) {
  10009. pr_err("unable to register input device\n");
  10010. thinkpad_acpi_module_exit();
  10011. return ret;
  10012. } else {
  10013. tp_features.input_device_registered = 1;
  10014. }
  10015. #ifdef CONFIG_SUSPEND
  10016. if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio) {
  10017. if (!acpi_register_lps0_dev(&thinkpad_acpi_s2idle_dev_ops))
  10018. pr_info("Using s2idle quirk to avoid %s platform firmware bug\n",
  10019. (dmi_id && dmi_id->ident) ? dmi_id->ident : "");
  10020. }
  10021. #endif
  10022. return 0;
  10023. }
  10024. MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
  10025. /*
  10026. * This will autoload the driver in almost every ThinkPad
  10027. * in widespread use.
  10028. *
  10029. * Only _VERY_ old models, like the 240, 240x and 570 lack
  10030. * the HKEY event interface.
  10031. */
  10032. MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
  10033. /*
  10034. * DMI matching for module autoloading
  10035. *
  10036. * See https://thinkwiki.org/wiki/List_of_DMI_IDs
  10037. * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  10038. *
  10039. * Only models listed in thinkwiki will be supported, so add yours
  10040. * if it is not there yet.
  10041. */
  10042. #define IBM_BIOS_MODULE_ALIAS(__type) \
  10043. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
  10044. /* Ancient thinkpad BIOSes have to be identified by
  10045. * BIOS type or model number, and there are far less
  10046. * BIOS types than model numbers... */
  10047. IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
  10048. MODULE_AUTHOR("Borislav Deianov <[email protected]>");
  10049. MODULE_AUTHOR("Henrique de Moraes Holschuh <[email protected]>");
  10050. MODULE_DESCRIPTION(TPACPI_DESC);
  10051. MODULE_VERSION(TPACPI_VERSION);
  10052. MODULE_LICENSE("GPL");
  10053. module_init(thinkpad_acpi_module_init);
  10054. module_exit(thinkpad_acpi_module_exit);