thinkpad_acpi.c 146 KB

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