thinkpad_acpi.c 212 KB

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