thinkpad_acpi.c 241 KB

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