thinkpad_acpi.c 247 KB

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