thinkpad_acpi.c 228 KB

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