thinkpad_acpi.c 260 KB

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