thinkpad_acpi.c 182 KB

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