thinkpad_acpi.c 231 KB

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