thinkpad_acpi.c 193 KB

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