thinkpad_acpi.c 192 KB

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