thinkpad_acpi.c 149 KB

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