thinkpad_acpi.c 148 KB

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