thinkpad_acpi.c 146 KB

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