thinkpad_acpi.c 161 KB

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