thinkpad_acpi.c 184 KB

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