thinkpad_acpi.c 165 KB

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