thinkpad_acpi.c 248 KB

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