thinkpad_acpi.c 261 KB

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