thinkpad_acpi.c 180 KB

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