thinkpad_acpi.c 188 KB

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