thinkpad_acpi.c 231 KB

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