thinkpad_acpi.c 202 KB

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