thinkpad_acpi.c 209 KB

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