thinkpad_acpi.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136
  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: On screen display helpers */
  1776. switch (hkey) {
  1777. case 0x5010:
  1778. /* Lenovo Vista BIOS: brightness changed */
  1779. break;
  1780. case 0x5001:
  1781. case 0x5002:
  1782. /* LID switch events. Do not propagate */
  1783. ignore_acpi_ev = 1;
  1784. break;
  1785. default:
  1786. unk_ev = 1;
  1787. }
  1788. break;
  1789. case 7:
  1790. /* 0x7000-0x7FFF: misc */
  1791. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  1792. tpacpi_input_send_radiosw();
  1793. send_acpi_ev = 0;
  1794. break;
  1795. }
  1796. /* fallthrough to default */
  1797. default:
  1798. unk_ev = 1;
  1799. }
  1800. if (unk_ev) {
  1801. printk(TPACPI_NOTICE
  1802. "unhandled HKEY event 0x%04x\n", hkey);
  1803. }
  1804. /* Legacy events */
  1805. if (!ignore_acpi_ev &&
  1806. (send_acpi_ev || hotkey_report_mode < 2)) {
  1807. acpi_bus_generate_proc_event(ibm->acpi->device,
  1808. event, hkey);
  1809. }
  1810. /* netlink events */
  1811. if (!ignore_acpi_ev && send_acpi_ev) {
  1812. acpi_bus_generate_netlink_event(
  1813. ibm->acpi->device->pnp.device_class,
  1814. ibm->acpi->device->dev.bus_id,
  1815. event, hkey);
  1816. }
  1817. }
  1818. }
  1819. static void hotkey_suspend(pm_message_t state)
  1820. {
  1821. /* Do these on suspend, we get the events on early resume! */
  1822. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  1823. hotkey_autosleep_ack = 0;
  1824. }
  1825. static void hotkey_resume(void)
  1826. {
  1827. if (hotkey_mask_get())
  1828. printk(TPACPI_ERR
  1829. "error while trying to read hot key mask "
  1830. "from firmware\n");
  1831. tpacpi_input_send_radiosw();
  1832. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1833. hotkey_poll_setup_safe(0);
  1834. #endif
  1835. }
  1836. /* procfs -------------------------------------------------------------- */
  1837. static int hotkey_read(char *p)
  1838. {
  1839. int res, status;
  1840. int len = 0;
  1841. if (!tp_features.hotkey) {
  1842. len += sprintf(p + len, "status:\t\tnot supported\n");
  1843. return len;
  1844. }
  1845. if (mutex_lock_interruptible(&hotkey_mutex))
  1846. return -ERESTARTSYS;
  1847. res = hotkey_status_get(&status);
  1848. if (!res)
  1849. res = hotkey_mask_get();
  1850. mutex_unlock(&hotkey_mutex);
  1851. if (res)
  1852. return res;
  1853. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  1854. if (tp_features.hotkey_mask) {
  1855. len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
  1856. len += sprintf(p + len,
  1857. "commands:\tenable, disable, reset, <mask>\n");
  1858. } else {
  1859. len += sprintf(p + len, "mask:\t\tnot supported\n");
  1860. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  1861. }
  1862. return len;
  1863. }
  1864. static int hotkey_write(char *buf)
  1865. {
  1866. int res, status;
  1867. u32 mask;
  1868. char *cmd;
  1869. if (!tp_features.hotkey)
  1870. return -ENODEV;
  1871. if (mutex_lock_interruptible(&hotkey_mutex))
  1872. return -ERESTARTSYS;
  1873. status = -1;
  1874. mask = hotkey_mask;
  1875. res = 0;
  1876. while ((cmd = next_cmd(&buf))) {
  1877. if (strlencmp(cmd, "enable") == 0) {
  1878. status = 1;
  1879. } else if (strlencmp(cmd, "disable") == 0) {
  1880. status = 0;
  1881. } else if (strlencmp(cmd, "reset") == 0) {
  1882. status = hotkey_orig_status;
  1883. mask = hotkey_orig_mask;
  1884. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  1885. /* mask set */
  1886. } else if (sscanf(cmd, "%x", &mask) == 1) {
  1887. /* mask set */
  1888. } else {
  1889. res = -EINVAL;
  1890. goto errexit;
  1891. }
  1892. }
  1893. if (status != -1)
  1894. res = hotkey_status_set(status);
  1895. if (!res && mask != hotkey_mask)
  1896. res = hotkey_mask_set(mask);
  1897. errexit:
  1898. mutex_unlock(&hotkey_mutex);
  1899. return res;
  1900. }
  1901. static const struct acpi_device_id ibm_htk_device_ids[] = {
  1902. {TPACPI_ACPI_HKEY_HID, 0},
  1903. {"", 0},
  1904. };
  1905. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  1906. .hid = ibm_htk_device_ids,
  1907. .notify = hotkey_notify,
  1908. .handle = &hkey_handle,
  1909. .type = ACPI_DEVICE_NOTIFY,
  1910. };
  1911. static struct ibm_struct hotkey_driver_data = {
  1912. .name = "hotkey",
  1913. .read = hotkey_read,
  1914. .write = hotkey_write,
  1915. .exit = hotkey_exit,
  1916. .resume = hotkey_resume,
  1917. .suspend = hotkey_suspend,
  1918. .acpi = &ibm_hotkey_acpidriver,
  1919. };
  1920. /*************************************************************************
  1921. * Bluetooth subdriver
  1922. */
  1923. enum {
  1924. /* ACPI GBDC/SBDC bits */
  1925. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  1926. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  1927. TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
  1928. };
  1929. static int bluetooth_get_radiosw(void);
  1930. static int bluetooth_set_radiosw(int radio_on);
  1931. /* sysfs bluetooth enable ---------------------------------------------- */
  1932. static ssize_t bluetooth_enable_show(struct device *dev,
  1933. struct device_attribute *attr,
  1934. char *buf)
  1935. {
  1936. int status;
  1937. status = bluetooth_get_radiosw();
  1938. if (status < 0)
  1939. return status;
  1940. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1941. }
  1942. static ssize_t bluetooth_enable_store(struct device *dev,
  1943. struct device_attribute *attr,
  1944. const char *buf, size_t count)
  1945. {
  1946. unsigned long t;
  1947. int res;
  1948. if (parse_strtoul(buf, 1, &t))
  1949. return -EINVAL;
  1950. res = bluetooth_set_radiosw(t);
  1951. return (res) ? res : count;
  1952. }
  1953. static struct device_attribute dev_attr_bluetooth_enable =
  1954. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  1955. bluetooth_enable_show, bluetooth_enable_store);
  1956. /* --------------------------------------------------------------------- */
  1957. static struct attribute *bluetooth_attributes[] = {
  1958. &dev_attr_bluetooth_enable.attr,
  1959. NULL
  1960. };
  1961. static const struct attribute_group bluetooth_attr_group = {
  1962. .attrs = bluetooth_attributes,
  1963. };
  1964. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  1965. {
  1966. int res;
  1967. int status = 0;
  1968. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  1969. TPACPI_ACPIHANDLE_INIT(hkey);
  1970. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1971. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  1972. tp_features.bluetooth = hkey_handle &&
  1973. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  1974. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  1975. str_supported(tp_features.bluetooth),
  1976. status);
  1977. if (tp_features.bluetooth) {
  1978. if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  1979. /* no bluetooth hardware present in system */
  1980. tp_features.bluetooth = 0;
  1981. dbg_printk(TPACPI_DBG_INIT,
  1982. "bluetooth hardware not installed\n");
  1983. } else {
  1984. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1985. &bluetooth_attr_group);
  1986. if (res)
  1987. return res;
  1988. }
  1989. }
  1990. return (tp_features.bluetooth)? 0 : 1;
  1991. }
  1992. static void bluetooth_exit(void)
  1993. {
  1994. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1995. &bluetooth_attr_group);
  1996. }
  1997. static int bluetooth_get_radiosw(void)
  1998. {
  1999. int status;
  2000. if (!tp_features.bluetooth)
  2001. return -ENODEV;
  2002. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  2003. return -EIO;
  2004. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  2005. }
  2006. static int bluetooth_set_radiosw(int radio_on)
  2007. {
  2008. int status;
  2009. if (!tp_features.bluetooth)
  2010. return -ENODEV;
  2011. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  2012. return -EIO;
  2013. if (radio_on)
  2014. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  2015. else
  2016. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  2017. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  2018. return -EIO;
  2019. return 0;
  2020. }
  2021. /* procfs -------------------------------------------------------------- */
  2022. static int bluetooth_read(char *p)
  2023. {
  2024. int len = 0;
  2025. int status = bluetooth_get_radiosw();
  2026. if (!tp_features.bluetooth)
  2027. len += sprintf(p + len, "status:\t\tnot supported\n");
  2028. else {
  2029. len += sprintf(p + len, "status:\t\t%s\n",
  2030. (status)? "enabled" : "disabled");
  2031. len += sprintf(p + len, "commands:\tenable, disable\n");
  2032. }
  2033. return len;
  2034. }
  2035. static int bluetooth_write(char *buf)
  2036. {
  2037. char *cmd;
  2038. if (!tp_features.bluetooth)
  2039. return -ENODEV;
  2040. while ((cmd = next_cmd(&buf))) {
  2041. if (strlencmp(cmd, "enable") == 0) {
  2042. bluetooth_set_radiosw(1);
  2043. } else if (strlencmp(cmd, "disable") == 0) {
  2044. bluetooth_set_radiosw(0);
  2045. } else
  2046. return -EINVAL;
  2047. }
  2048. return 0;
  2049. }
  2050. static struct ibm_struct bluetooth_driver_data = {
  2051. .name = "bluetooth",
  2052. .read = bluetooth_read,
  2053. .write = bluetooth_write,
  2054. .exit = bluetooth_exit,
  2055. };
  2056. /*************************************************************************
  2057. * Wan subdriver
  2058. */
  2059. enum {
  2060. /* ACPI GWAN/SWAN bits */
  2061. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  2062. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  2063. TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
  2064. };
  2065. static int wan_get_radiosw(void);
  2066. static int wan_set_radiosw(int radio_on);
  2067. /* sysfs wan enable ---------------------------------------------------- */
  2068. static ssize_t wan_enable_show(struct device *dev,
  2069. struct device_attribute *attr,
  2070. char *buf)
  2071. {
  2072. int status;
  2073. status = wan_get_radiosw();
  2074. if (status < 0)
  2075. return status;
  2076. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  2077. }
  2078. static ssize_t wan_enable_store(struct device *dev,
  2079. struct device_attribute *attr,
  2080. const char *buf, size_t count)
  2081. {
  2082. unsigned long t;
  2083. int res;
  2084. if (parse_strtoul(buf, 1, &t))
  2085. return -EINVAL;
  2086. res = wan_set_radiosw(t);
  2087. return (res) ? res : count;
  2088. }
  2089. static struct device_attribute dev_attr_wan_enable =
  2090. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  2091. wan_enable_show, wan_enable_store);
  2092. /* --------------------------------------------------------------------- */
  2093. static struct attribute *wan_attributes[] = {
  2094. &dev_attr_wan_enable.attr,
  2095. NULL
  2096. };
  2097. static const struct attribute_group wan_attr_group = {
  2098. .attrs = wan_attributes,
  2099. };
  2100. static int __init wan_init(struct ibm_init_struct *iibm)
  2101. {
  2102. int res;
  2103. int status = 0;
  2104. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  2105. TPACPI_ACPIHANDLE_INIT(hkey);
  2106. tp_features.wan = hkey_handle &&
  2107. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  2108. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  2109. str_supported(tp_features.wan),
  2110. status);
  2111. if (tp_features.wan) {
  2112. if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
  2113. /* no wan hardware present in system */
  2114. tp_features.wan = 0;
  2115. dbg_printk(TPACPI_DBG_INIT,
  2116. "wan hardware not installed\n");
  2117. } else {
  2118. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2119. &wan_attr_group);
  2120. if (res)
  2121. return res;
  2122. }
  2123. }
  2124. return (tp_features.wan)? 0 : 1;
  2125. }
  2126. static void wan_exit(void)
  2127. {
  2128. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2129. &wan_attr_group);
  2130. }
  2131. static int wan_get_radiosw(void)
  2132. {
  2133. int status;
  2134. if (!tp_features.wan)
  2135. return -ENODEV;
  2136. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2137. return -EIO;
  2138. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  2139. }
  2140. static int wan_set_radiosw(int radio_on)
  2141. {
  2142. int status;
  2143. if (!tp_features.wan)
  2144. return -ENODEV;
  2145. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2146. return -EIO;
  2147. if (radio_on)
  2148. status |= TP_ACPI_WANCARD_RADIOSSW;
  2149. else
  2150. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  2151. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  2152. return -EIO;
  2153. return 0;
  2154. }
  2155. /* procfs -------------------------------------------------------------- */
  2156. static int wan_read(char *p)
  2157. {
  2158. int len = 0;
  2159. int status = wan_get_radiosw();
  2160. if (!tp_features.wan)
  2161. len += sprintf(p + len, "status:\t\tnot supported\n");
  2162. else {
  2163. len += sprintf(p + len, "status:\t\t%s\n",
  2164. (status)? "enabled" : "disabled");
  2165. len += sprintf(p + len, "commands:\tenable, disable\n");
  2166. }
  2167. return len;
  2168. }
  2169. static int wan_write(char *buf)
  2170. {
  2171. char *cmd;
  2172. if (!tp_features.wan)
  2173. return -ENODEV;
  2174. while ((cmd = next_cmd(&buf))) {
  2175. if (strlencmp(cmd, "enable") == 0) {
  2176. wan_set_radiosw(1);
  2177. } else if (strlencmp(cmd, "disable") == 0) {
  2178. wan_set_radiosw(0);
  2179. } else
  2180. return -EINVAL;
  2181. }
  2182. return 0;
  2183. }
  2184. static struct ibm_struct wan_driver_data = {
  2185. .name = "wan",
  2186. .read = wan_read,
  2187. .write = wan_write,
  2188. .exit = wan_exit,
  2189. .flags.experimental = 1,
  2190. };
  2191. /*************************************************************************
  2192. * Video subdriver
  2193. */
  2194. enum video_access_mode {
  2195. TPACPI_VIDEO_NONE = 0,
  2196. TPACPI_VIDEO_570, /* 570 */
  2197. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  2198. TPACPI_VIDEO_NEW, /* all others */
  2199. };
  2200. enum { /* video status flags, based on VIDEO_570 */
  2201. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  2202. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  2203. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  2204. };
  2205. enum { /* TPACPI_VIDEO_570 constants */
  2206. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  2207. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  2208. * video_status_flags */
  2209. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  2210. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  2211. };
  2212. static enum video_access_mode video_supported;
  2213. static int video_orig_autosw;
  2214. static int video_autosw_get(void);
  2215. static int video_autosw_set(int enable);
  2216. TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  2217. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  2218. "\\_SB.PCI0.VID0", /* 770e */
  2219. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  2220. "\\_SB.PCI0.AGP.VID", /* all others */
  2221. ); /* R30, R31 */
  2222. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  2223. static int __init video_init(struct ibm_init_struct *iibm)
  2224. {
  2225. int ivga;
  2226. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  2227. TPACPI_ACPIHANDLE_INIT(vid);
  2228. TPACPI_ACPIHANDLE_INIT(vid2);
  2229. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  2230. /* G41, assume IVGA doesn't change */
  2231. vid_handle = vid2_handle;
  2232. if (!vid_handle)
  2233. /* video switching not supported on R30, R31 */
  2234. video_supported = TPACPI_VIDEO_NONE;
  2235. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  2236. /* 570 */
  2237. video_supported = TPACPI_VIDEO_570;
  2238. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  2239. /* 600e/x, 770e, 770x */
  2240. video_supported = TPACPI_VIDEO_770;
  2241. else
  2242. /* all others */
  2243. video_supported = TPACPI_VIDEO_NEW;
  2244. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  2245. str_supported(video_supported != TPACPI_VIDEO_NONE),
  2246. video_supported);
  2247. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  2248. }
  2249. static void video_exit(void)
  2250. {
  2251. dbg_printk(TPACPI_DBG_EXIT,
  2252. "restoring original video autoswitch mode\n");
  2253. if (video_autosw_set(video_orig_autosw))
  2254. printk(TPACPI_ERR "error while trying to restore original "
  2255. "video autoswitch mode\n");
  2256. }
  2257. static int video_outputsw_get(void)
  2258. {
  2259. int status = 0;
  2260. int i;
  2261. switch (video_supported) {
  2262. case TPACPI_VIDEO_570:
  2263. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  2264. TP_ACPI_VIDEO_570_PHSCMD))
  2265. return -EIO;
  2266. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  2267. break;
  2268. case TPACPI_VIDEO_770:
  2269. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2270. return -EIO;
  2271. if (i)
  2272. status |= TP_ACPI_VIDEO_S_LCD;
  2273. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2274. return -EIO;
  2275. if (i)
  2276. status |= TP_ACPI_VIDEO_S_CRT;
  2277. break;
  2278. case TPACPI_VIDEO_NEW:
  2279. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  2280. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2281. return -EIO;
  2282. if (i)
  2283. status |= TP_ACPI_VIDEO_S_CRT;
  2284. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  2285. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2286. return -EIO;
  2287. if (i)
  2288. status |= TP_ACPI_VIDEO_S_LCD;
  2289. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  2290. return -EIO;
  2291. if (i)
  2292. status |= TP_ACPI_VIDEO_S_DVI;
  2293. break;
  2294. default:
  2295. return -ENOSYS;
  2296. }
  2297. return status;
  2298. }
  2299. static int video_outputsw_set(int status)
  2300. {
  2301. int autosw;
  2302. int res = 0;
  2303. switch (video_supported) {
  2304. case TPACPI_VIDEO_570:
  2305. res = acpi_evalf(NULL, NULL,
  2306. "\\_SB.PHS2", "vdd",
  2307. TP_ACPI_VIDEO_570_PHS2CMD,
  2308. status | TP_ACPI_VIDEO_570_PHS2SET);
  2309. break;
  2310. case TPACPI_VIDEO_770:
  2311. autosw = video_autosw_get();
  2312. if (autosw < 0)
  2313. return autosw;
  2314. res = video_autosw_set(1);
  2315. if (res)
  2316. return res;
  2317. res = acpi_evalf(vid_handle, NULL,
  2318. "ASWT", "vdd", status * 0x100, 0);
  2319. if (!autosw && video_autosw_set(autosw)) {
  2320. printk(TPACPI_ERR
  2321. "video auto-switch left enabled due to error\n");
  2322. return -EIO;
  2323. }
  2324. break;
  2325. case TPACPI_VIDEO_NEW:
  2326. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  2327. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  2328. break;
  2329. default:
  2330. return -ENOSYS;
  2331. }
  2332. return (res)? 0 : -EIO;
  2333. }
  2334. static int video_autosw_get(void)
  2335. {
  2336. int autosw = 0;
  2337. switch (video_supported) {
  2338. case TPACPI_VIDEO_570:
  2339. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  2340. return -EIO;
  2341. break;
  2342. case TPACPI_VIDEO_770:
  2343. case TPACPI_VIDEO_NEW:
  2344. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  2345. return -EIO;
  2346. break;
  2347. default:
  2348. return -ENOSYS;
  2349. }
  2350. return autosw & 1;
  2351. }
  2352. static int video_autosw_set(int enable)
  2353. {
  2354. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  2355. return -EIO;
  2356. return 0;
  2357. }
  2358. static int video_outputsw_cycle(void)
  2359. {
  2360. int autosw = video_autosw_get();
  2361. int res;
  2362. if (autosw < 0)
  2363. return autosw;
  2364. switch (video_supported) {
  2365. case TPACPI_VIDEO_570:
  2366. res = video_autosw_set(1);
  2367. if (res)
  2368. return res;
  2369. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  2370. break;
  2371. case TPACPI_VIDEO_770:
  2372. case TPACPI_VIDEO_NEW:
  2373. res = video_autosw_set(1);
  2374. if (res)
  2375. return res;
  2376. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  2377. break;
  2378. default:
  2379. return -ENOSYS;
  2380. }
  2381. if (!autosw && video_autosw_set(autosw)) {
  2382. printk(TPACPI_ERR
  2383. "video auto-switch left enabled due to error\n");
  2384. return -EIO;
  2385. }
  2386. return (res)? 0 : -EIO;
  2387. }
  2388. static int video_expand_toggle(void)
  2389. {
  2390. switch (video_supported) {
  2391. case TPACPI_VIDEO_570:
  2392. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  2393. 0 : -EIO;
  2394. case TPACPI_VIDEO_770:
  2395. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  2396. 0 : -EIO;
  2397. case TPACPI_VIDEO_NEW:
  2398. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  2399. 0 : -EIO;
  2400. default:
  2401. return -ENOSYS;
  2402. }
  2403. /* not reached */
  2404. }
  2405. static int video_read(char *p)
  2406. {
  2407. int status, autosw;
  2408. int len = 0;
  2409. if (video_supported == TPACPI_VIDEO_NONE) {
  2410. len += sprintf(p + len, "status:\t\tnot supported\n");
  2411. return len;
  2412. }
  2413. status = video_outputsw_get();
  2414. if (status < 0)
  2415. return status;
  2416. autosw = video_autosw_get();
  2417. if (autosw < 0)
  2418. return autosw;
  2419. len += sprintf(p + len, "status:\t\tsupported\n");
  2420. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  2421. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  2422. if (video_supported == TPACPI_VIDEO_NEW)
  2423. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  2424. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  2425. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  2426. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  2427. if (video_supported == TPACPI_VIDEO_NEW)
  2428. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  2429. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  2430. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  2431. return len;
  2432. }
  2433. static int video_write(char *buf)
  2434. {
  2435. char *cmd;
  2436. int enable, disable, status;
  2437. int res;
  2438. if (video_supported == TPACPI_VIDEO_NONE)
  2439. return -ENODEV;
  2440. enable = 0;
  2441. disable = 0;
  2442. while ((cmd = next_cmd(&buf))) {
  2443. if (strlencmp(cmd, "lcd_enable") == 0) {
  2444. enable |= TP_ACPI_VIDEO_S_LCD;
  2445. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  2446. disable |= TP_ACPI_VIDEO_S_LCD;
  2447. } else if (strlencmp(cmd, "crt_enable") == 0) {
  2448. enable |= TP_ACPI_VIDEO_S_CRT;
  2449. } else if (strlencmp(cmd, "crt_disable") == 0) {
  2450. disable |= TP_ACPI_VIDEO_S_CRT;
  2451. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2452. strlencmp(cmd, "dvi_enable") == 0) {
  2453. enable |= TP_ACPI_VIDEO_S_DVI;
  2454. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2455. strlencmp(cmd, "dvi_disable") == 0) {
  2456. disable |= TP_ACPI_VIDEO_S_DVI;
  2457. } else if (strlencmp(cmd, "auto_enable") == 0) {
  2458. res = video_autosw_set(1);
  2459. if (res)
  2460. return res;
  2461. } else if (strlencmp(cmd, "auto_disable") == 0) {
  2462. res = video_autosw_set(0);
  2463. if (res)
  2464. return res;
  2465. } else if (strlencmp(cmd, "video_switch") == 0) {
  2466. res = video_outputsw_cycle();
  2467. if (res)
  2468. return res;
  2469. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  2470. res = video_expand_toggle();
  2471. if (res)
  2472. return res;
  2473. } else
  2474. return -EINVAL;
  2475. }
  2476. if (enable || disable) {
  2477. status = video_outputsw_get();
  2478. if (status < 0)
  2479. return status;
  2480. res = video_outputsw_set((status & ~disable) | enable);
  2481. if (res)
  2482. return res;
  2483. }
  2484. return 0;
  2485. }
  2486. static struct ibm_struct video_driver_data = {
  2487. .name = "video",
  2488. .read = video_read,
  2489. .write = video_write,
  2490. .exit = video_exit,
  2491. };
  2492. /*************************************************************************
  2493. * Light (thinklight) subdriver
  2494. */
  2495. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  2496. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  2497. static int __init light_init(struct ibm_init_struct *iibm)
  2498. {
  2499. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  2500. TPACPI_ACPIHANDLE_INIT(ledb);
  2501. TPACPI_ACPIHANDLE_INIT(lght);
  2502. TPACPI_ACPIHANDLE_INIT(cmos);
  2503. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  2504. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  2505. if (tp_features.light)
  2506. /* light status not supported on
  2507. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  2508. tp_features.light_status =
  2509. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  2510. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  2511. str_supported(tp_features.light));
  2512. return (tp_features.light)? 0 : 1;
  2513. }
  2514. static int light_read(char *p)
  2515. {
  2516. int len = 0;
  2517. int status = 0;
  2518. if (!tp_features.light) {
  2519. len += sprintf(p + len, "status:\t\tnot supported\n");
  2520. } else if (!tp_features.light_status) {
  2521. len += sprintf(p + len, "status:\t\tunknown\n");
  2522. len += sprintf(p + len, "commands:\ton, off\n");
  2523. } else {
  2524. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  2525. return -EIO;
  2526. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  2527. len += sprintf(p + len, "commands:\ton, off\n");
  2528. }
  2529. return len;
  2530. }
  2531. static int light_write(char *buf)
  2532. {
  2533. int cmos_cmd, lght_cmd;
  2534. char *cmd;
  2535. int success;
  2536. if (!tp_features.light)
  2537. return -ENODEV;
  2538. while ((cmd = next_cmd(&buf))) {
  2539. if (strlencmp(cmd, "on") == 0) {
  2540. cmos_cmd = 0x0c;
  2541. lght_cmd = 1;
  2542. } else if (strlencmp(cmd, "off") == 0) {
  2543. cmos_cmd = 0x0d;
  2544. lght_cmd = 0;
  2545. } else
  2546. return -EINVAL;
  2547. success = cmos_handle ?
  2548. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  2549. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  2550. if (!success)
  2551. return -EIO;
  2552. }
  2553. return 0;
  2554. }
  2555. static struct ibm_struct light_driver_data = {
  2556. .name = "light",
  2557. .read = light_read,
  2558. .write = light_write,
  2559. };
  2560. /*************************************************************************
  2561. * Dock subdriver
  2562. */
  2563. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2564. static void dock_notify(struct ibm_struct *ibm, u32 event);
  2565. static int dock_read(char *p);
  2566. static int dock_write(char *buf);
  2567. TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  2568. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  2569. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  2570. "\\_SB.PCI.ISA.SLCE", /* 570 */
  2571. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  2572. /* don't list other alternatives as we install a notify handler on the 570 */
  2573. TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  2574. static const struct acpi_device_id ibm_pci_device_ids[] = {
  2575. {PCI_ROOT_HID_STRING, 0},
  2576. {"", 0},
  2577. };
  2578. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  2579. {
  2580. .notify = dock_notify,
  2581. .handle = &dock_handle,
  2582. .type = ACPI_SYSTEM_NOTIFY,
  2583. },
  2584. {
  2585. /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
  2586. * We just use it to get notifications of dock hotplug
  2587. * in very old thinkpads */
  2588. .hid = ibm_pci_device_ids,
  2589. .notify = dock_notify,
  2590. .handle = &pci_handle,
  2591. .type = ACPI_SYSTEM_NOTIFY,
  2592. },
  2593. };
  2594. static struct ibm_struct dock_driver_data[2] = {
  2595. {
  2596. .name = "dock",
  2597. .read = dock_read,
  2598. .write = dock_write,
  2599. .acpi = &ibm_dock_acpidriver[0],
  2600. },
  2601. {
  2602. .name = "dock",
  2603. .acpi = &ibm_dock_acpidriver[1],
  2604. },
  2605. };
  2606. #define dock_docked() (_sta(dock_handle) & 1)
  2607. static int __init dock_init(struct ibm_init_struct *iibm)
  2608. {
  2609. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  2610. TPACPI_ACPIHANDLE_INIT(dock);
  2611. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  2612. str_supported(dock_handle != NULL));
  2613. return (dock_handle)? 0 : 1;
  2614. }
  2615. static int __init dock_init2(struct ibm_init_struct *iibm)
  2616. {
  2617. int dock2_needed;
  2618. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  2619. if (dock_driver_data[0].flags.acpi_driver_registered &&
  2620. dock_driver_data[0].flags.acpi_notify_installed) {
  2621. TPACPI_ACPIHANDLE_INIT(pci);
  2622. dock2_needed = (pci_handle != NULL);
  2623. vdbg_printk(TPACPI_DBG_INIT,
  2624. "dock PCI handler for the TP 570 is %s\n",
  2625. str_supported(dock2_needed));
  2626. } else {
  2627. vdbg_printk(TPACPI_DBG_INIT,
  2628. "dock subdriver part 2 not required\n");
  2629. dock2_needed = 0;
  2630. }
  2631. return (dock2_needed)? 0 : 1;
  2632. }
  2633. static void dock_notify(struct ibm_struct *ibm, u32 event)
  2634. {
  2635. int docked = dock_docked();
  2636. int pci = ibm->acpi->hid && ibm->acpi->device &&
  2637. acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
  2638. int data;
  2639. if (event == 1 && !pci) /* 570 */
  2640. data = 1; /* button */
  2641. else if (event == 1 && pci) /* 570 */
  2642. data = 3; /* dock */
  2643. else if (event == 3 && docked)
  2644. data = 1; /* button */
  2645. else if (event == 3 && !docked)
  2646. data = 2; /* undock */
  2647. else if (event == 0 && docked)
  2648. data = 3; /* dock */
  2649. else {
  2650. printk(TPACPI_ERR "unknown dock event %d, status %d\n",
  2651. event, _sta(dock_handle));
  2652. data = 0; /* unknown */
  2653. }
  2654. acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
  2655. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2656. ibm->acpi->device->dev.bus_id,
  2657. event, data);
  2658. }
  2659. static int dock_read(char *p)
  2660. {
  2661. int len = 0;
  2662. int docked = dock_docked();
  2663. if (!dock_handle)
  2664. len += sprintf(p + len, "status:\t\tnot supported\n");
  2665. else if (!docked)
  2666. len += sprintf(p + len, "status:\t\tundocked\n");
  2667. else {
  2668. len += sprintf(p + len, "status:\t\tdocked\n");
  2669. len += sprintf(p + len, "commands:\tdock, undock\n");
  2670. }
  2671. return len;
  2672. }
  2673. static int dock_write(char *buf)
  2674. {
  2675. char *cmd;
  2676. if (!dock_docked())
  2677. return -ENODEV;
  2678. while ((cmd = next_cmd(&buf))) {
  2679. if (strlencmp(cmd, "undock") == 0) {
  2680. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  2681. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  2682. return -EIO;
  2683. } else if (strlencmp(cmd, "dock") == 0) {
  2684. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  2685. return -EIO;
  2686. } else
  2687. return -EINVAL;
  2688. }
  2689. return 0;
  2690. }
  2691. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  2692. /*************************************************************************
  2693. * Bay subdriver
  2694. */
  2695. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2696. TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  2697. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  2698. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  2699. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  2700. ); /* A21e, R30, R31 */
  2701. TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  2702. "_EJ0", /* all others */
  2703. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  2704. TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  2705. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  2706. ); /* all others */
  2707. TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  2708. "_EJ0", /* 770x */
  2709. ); /* all others */
  2710. static int __init bay_init(struct ibm_init_struct *iibm)
  2711. {
  2712. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  2713. TPACPI_ACPIHANDLE_INIT(bay);
  2714. if (bay_handle)
  2715. TPACPI_ACPIHANDLE_INIT(bay_ej);
  2716. TPACPI_ACPIHANDLE_INIT(bay2);
  2717. if (bay2_handle)
  2718. TPACPI_ACPIHANDLE_INIT(bay2_ej);
  2719. tp_features.bay_status = bay_handle &&
  2720. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  2721. tp_features.bay_status2 = bay2_handle &&
  2722. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  2723. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  2724. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  2725. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  2726. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  2727. vdbg_printk(TPACPI_DBG_INIT,
  2728. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  2729. str_supported(tp_features.bay_status),
  2730. str_supported(tp_features.bay_eject),
  2731. str_supported(tp_features.bay_status2),
  2732. str_supported(tp_features.bay_eject2));
  2733. return (tp_features.bay_status || tp_features.bay_eject ||
  2734. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  2735. }
  2736. static void bay_notify(struct ibm_struct *ibm, u32 event)
  2737. {
  2738. acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
  2739. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2740. ibm->acpi->device->dev.bus_id,
  2741. event, 0);
  2742. }
  2743. #define bay_occupied(b) (_sta(b##_handle) & 1)
  2744. static int bay_read(char *p)
  2745. {
  2746. int len = 0;
  2747. int occupied = bay_occupied(bay);
  2748. int occupied2 = bay_occupied(bay2);
  2749. int eject, eject2;
  2750. len += sprintf(p + len, "status:\t\t%s\n",
  2751. tp_features.bay_status ?
  2752. (occupied ? "occupied" : "unoccupied") :
  2753. "not supported");
  2754. if (tp_features.bay_status2)
  2755. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  2756. "occupied" : "unoccupied");
  2757. eject = tp_features.bay_eject && occupied;
  2758. eject2 = tp_features.bay_eject2 && occupied2;
  2759. if (eject && eject2)
  2760. len += sprintf(p + len, "commands:\teject, eject2\n");
  2761. else if (eject)
  2762. len += sprintf(p + len, "commands:\teject\n");
  2763. else if (eject2)
  2764. len += sprintf(p + len, "commands:\teject2\n");
  2765. return len;
  2766. }
  2767. static int bay_write(char *buf)
  2768. {
  2769. char *cmd;
  2770. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  2771. return -ENODEV;
  2772. while ((cmd = next_cmd(&buf))) {
  2773. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  2774. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  2775. return -EIO;
  2776. } else if (tp_features.bay_eject2 &&
  2777. strlencmp(cmd, "eject2") == 0) {
  2778. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  2779. return -EIO;
  2780. } else
  2781. return -EINVAL;
  2782. }
  2783. return 0;
  2784. }
  2785. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  2786. .notify = bay_notify,
  2787. .handle = &bay_handle,
  2788. .type = ACPI_SYSTEM_NOTIFY,
  2789. };
  2790. static struct ibm_struct bay_driver_data = {
  2791. .name = "bay",
  2792. .read = bay_read,
  2793. .write = bay_write,
  2794. .acpi = &ibm_bay_acpidriver,
  2795. };
  2796. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  2797. /*************************************************************************
  2798. * CMOS subdriver
  2799. */
  2800. /* sysfs cmos_command -------------------------------------------------- */
  2801. static ssize_t cmos_command_store(struct device *dev,
  2802. struct device_attribute *attr,
  2803. const char *buf, size_t count)
  2804. {
  2805. unsigned long cmos_cmd;
  2806. int res;
  2807. if (parse_strtoul(buf, 21, &cmos_cmd))
  2808. return -EINVAL;
  2809. res = issue_thinkpad_cmos_command(cmos_cmd);
  2810. return (res)? res : count;
  2811. }
  2812. static struct device_attribute dev_attr_cmos_command =
  2813. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  2814. /* --------------------------------------------------------------------- */
  2815. static int __init cmos_init(struct ibm_init_struct *iibm)
  2816. {
  2817. int res;
  2818. vdbg_printk(TPACPI_DBG_INIT,
  2819. "initializing cmos commands subdriver\n");
  2820. TPACPI_ACPIHANDLE_INIT(cmos);
  2821. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  2822. str_supported(cmos_handle != NULL));
  2823. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2824. if (res)
  2825. return res;
  2826. return (cmos_handle)? 0 : 1;
  2827. }
  2828. static void cmos_exit(void)
  2829. {
  2830. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  2831. }
  2832. static int cmos_read(char *p)
  2833. {
  2834. int len = 0;
  2835. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2836. R30, R31, T20-22, X20-21 */
  2837. if (!cmos_handle)
  2838. len += sprintf(p + len, "status:\t\tnot supported\n");
  2839. else {
  2840. len += sprintf(p + len, "status:\t\tsupported\n");
  2841. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  2842. }
  2843. return len;
  2844. }
  2845. static int cmos_write(char *buf)
  2846. {
  2847. char *cmd;
  2848. int cmos_cmd, res;
  2849. while ((cmd = next_cmd(&buf))) {
  2850. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  2851. cmos_cmd >= 0 && cmos_cmd <= 21) {
  2852. /* cmos_cmd set */
  2853. } else
  2854. return -EINVAL;
  2855. res = issue_thinkpad_cmos_command(cmos_cmd);
  2856. if (res)
  2857. return res;
  2858. }
  2859. return 0;
  2860. }
  2861. static struct ibm_struct cmos_driver_data = {
  2862. .name = "cmos",
  2863. .read = cmos_read,
  2864. .write = cmos_write,
  2865. .exit = cmos_exit,
  2866. };
  2867. /*************************************************************************
  2868. * LED subdriver
  2869. */
  2870. enum led_access_mode {
  2871. TPACPI_LED_NONE = 0,
  2872. TPACPI_LED_570, /* 570 */
  2873. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2874. TPACPI_LED_NEW, /* all others */
  2875. };
  2876. enum { /* For TPACPI_LED_OLD */
  2877. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  2878. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  2879. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  2880. };
  2881. static enum led_access_mode led_supported;
  2882. TPACPI_HANDLE(led, ec, "SLED", /* 570 */
  2883. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
  2884. /* T20-22, X20-21 */
  2885. "LED", /* all others */
  2886. ); /* R30, R31 */
  2887. static int __init led_init(struct ibm_init_struct *iibm)
  2888. {
  2889. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  2890. TPACPI_ACPIHANDLE_INIT(led);
  2891. if (!led_handle)
  2892. /* led not supported on R30, R31 */
  2893. led_supported = TPACPI_LED_NONE;
  2894. else if (strlencmp(led_path, "SLED") == 0)
  2895. /* 570 */
  2896. led_supported = TPACPI_LED_570;
  2897. else if (strlencmp(led_path, "SYSL") == 0)
  2898. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  2899. led_supported = TPACPI_LED_OLD;
  2900. else
  2901. /* all others */
  2902. led_supported = TPACPI_LED_NEW;
  2903. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  2904. str_supported(led_supported), led_supported);
  2905. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  2906. }
  2907. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  2908. static int led_read(char *p)
  2909. {
  2910. int len = 0;
  2911. if (!led_supported) {
  2912. len += sprintf(p + len, "status:\t\tnot supported\n");
  2913. return len;
  2914. }
  2915. len += sprintf(p + len, "status:\t\tsupported\n");
  2916. if (led_supported == TPACPI_LED_570) {
  2917. /* 570 */
  2918. int i, status;
  2919. for (i = 0; i < 8; i++) {
  2920. if (!acpi_evalf(ec_handle,
  2921. &status, "GLED", "dd", 1 << i))
  2922. return -EIO;
  2923. len += sprintf(p + len, "%d:\t\t%s\n",
  2924. i, led_status(status));
  2925. }
  2926. }
  2927. len += sprintf(p + len, "commands:\t"
  2928. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  2929. return len;
  2930. }
  2931. /* off, on, blink */
  2932. static const int led_sled_arg1[] = { 0, 1, 3 };
  2933. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  2934. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  2935. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  2936. static int led_write(char *buf)
  2937. {
  2938. char *cmd;
  2939. int led, ind, ret;
  2940. if (!led_supported)
  2941. return -ENODEV;
  2942. while ((cmd = next_cmd(&buf))) {
  2943. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  2944. return -EINVAL;
  2945. if (strstr(cmd, "off")) {
  2946. ind = 0;
  2947. } else if (strstr(cmd, "on")) {
  2948. ind = 1;
  2949. } else if (strstr(cmd, "blink")) {
  2950. ind = 2;
  2951. } else
  2952. return -EINVAL;
  2953. if (led_supported == TPACPI_LED_570) {
  2954. /* 570 */
  2955. led = 1 << led;
  2956. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2957. led, led_sled_arg1[ind]))
  2958. return -EIO;
  2959. } else if (led_supported == TPACPI_LED_OLD) {
  2960. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  2961. led = 1 << led;
  2962. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  2963. if (ret >= 0)
  2964. ret = ec_write(TPACPI_LED_EC_HLBL,
  2965. led * led_exp_hlbl[ind]);
  2966. if (ret >= 0)
  2967. ret = ec_write(TPACPI_LED_EC_HLCL,
  2968. led * led_exp_hlcl[ind]);
  2969. if (ret < 0)
  2970. return ret;
  2971. } else {
  2972. /* all others */
  2973. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2974. led, led_led_arg1[ind]))
  2975. return -EIO;
  2976. }
  2977. }
  2978. return 0;
  2979. }
  2980. static struct ibm_struct led_driver_data = {
  2981. .name = "led",
  2982. .read = led_read,
  2983. .write = led_write,
  2984. };
  2985. /*************************************************************************
  2986. * Beep subdriver
  2987. */
  2988. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  2989. static int __init beep_init(struct ibm_init_struct *iibm)
  2990. {
  2991. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  2992. TPACPI_ACPIHANDLE_INIT(beep);
  2993. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  2994. str_supported(beep_handle != NULL));
  2995. return (beep_handle)? 0 : 1;
  2996. }
  2997. static int beep_read(char *p)
  2998. {
  2999. int len = 0;
  3000. if (!beep_handle)
  3001. len += sprintf(p + len, "status:\t\tnot supported\n");
  3002. else {
  3003. len += sprintf(p + len, "status:\t\tsupported\n");
  3004. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  3005. }
  3006. return len;
  3007. }
  3008. static int beep_write(char *buf)
  3009. {
  3010. char *cmd;
  3011. int beep_cmd;
  3012. if (!beep_handle)
  3013. return -ENODEV;
  3014. while ((cmd = next_cmd(&buf))) {
  3015. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  3016. beep_cmd >= 0 && beep_cmd <= 17) {
  3017. /* beep_cmd set */
  3018. } else
  3019. return -EINVAL;
  3020. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  3021. return -EIO;
  3022. }
  3023. return 0;
  3024. }
  3025. static struct ibm_struct beep_driver_data = {
  3026. .name = "beep",
  3027. .read = beep_read,
  3028. .write = beep_write,
  3029. };
  3030. /*************************************************************************
  3031. * Thermal subdriver
  3032. */
  3033. enum thermal_access_mode {
  3034. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  3035. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  3036. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  3037. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  3038. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  3039. };
  3040. enum { /* TPACPI_THERMAL_TPEC_* */
  3041. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  3042. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  3043. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  3044. };
  3045. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  3046. struct ibm_thermal_sensors_struct {
  3047. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  3048. };
  3049. static enum thermal_access_mode thermal_read_mode;
  3050. /* idx is zero-based */
  3051. static int thermal_get_sensor(int idx, s32 *value)
  3052. {
  3053. int t;
  3054. s8 tmp;
  3055. char tmpi[5];
  3056. t = TP_EC_THERMAL_TMP0;
  3057. switch (thermal_read_mode) {
  3058. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  3059. case TPACPI_THERMAL_TPEC_16:
  3060. if (idx >= 8 && idx <= 15) {
  3061. t = TP_EC_THERMAL_TMP8;
  3062. idx -= 8;
  3063. }
  3064. /* fallthrough */
  3065. #endif
  3066. case TPACPI_THERMAL_TPEC_8:
  3067. if (idx <= 7) {
  3068. if (!acpi_ec_read(t + idx, &tmp))
  3069. return -EIO;
  3070. *value = tmp * 1000;
  3071. return 0;
  3072. }
  3073. break;
  3074. case TPACPI_THERMAL_ACPI_UPDT:
  3075. if (idx <= 7) {
  3076. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3077. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  3078. return -EIO;
  3079. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3080. return -EIO;
  3081. *value = (t - 2732) * 100;
  3082. return 0;
  3083. }
  3084. break;
  3085. case TPACPI_THERMAL_ACPI_TMP07:
  3086. if (idx <= 7) {
  3087. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3088. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3089. return -EIO;
  3090. if (t > 127 || t < -127)
  3091. t = TP_EC_THERMAL_TMP_NA;
  3092. *value = t * 1000;
  3093. return 0;
  3094. }
  3095. break;
  3096. case TPACPI_THERMAL_NONE:
  3097. default:
  3098. return -ENOSYS;
  3099. }
  3100. return -EINVAL;
  3101. }
  3102. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  3103. {
  3104. int res, i;
  3105. int n;
  3106. n = 8;
  3107. i = 0;
  3108. if (!s)
  3109. return -EINVAL;
  3110. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  3111. n = 16;
  3112. for (i = 0 ; i < n; i++) {
  3113. res = thermal_get_sensor(i, &s->temp[i]);
  3114. if (res)
  3115. return res;
  3116. }
  3117. return n;
  3118. }
  3119. /* sysfs temp##_input -------------------------------------------------- */
  3120. static ssize_t thermal_temp_input_show(struct device *dev,
  3121. struct device_attribute *attr,
  3122. char *buf)
  3123. {
  3124. struct sensor_device_attribute *sensor_attr =
  3125. to_sensor_dev_attr(attr);
  3126. int idx = sensor_attr->index;
  3127. s32 value;
  3128. int res;
  3129. res = thermal_get_sensor(idx, &value);
  3130. if (res)
  3131. return res;
  3132. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  3133. return -ENXIO;
  3134. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  3135. }
  3136. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  3137. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  3138. thermal_temp_input_show, NULL, _idxB)
  3139. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  3140. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  3141. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  3142. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  3143. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  3144. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  3145. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  3146. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  3147. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  3148. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  3149. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  3150. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  3151. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  3152. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  3153. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  3154. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  3155. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  3156. };
  3157. #define THERMAL_ATTRS(X) \
  3158. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  3159. static struct attribute *thermal_temp_input_attr[] = {
  3160. THERMAL_ATTRS(8),
  3161. THERMAL_ATTRS(9),
  3162. THERMAL_ATTRS(10),
  3163. THERMAL_ATTRS(11),
  3164. THERMAL_ATTRS(12),
  3165. THERMAL_ATTRS(13),
  3166. THERMAL_ATTRS(14),
  3167. THERMAL_ATTRS(15),
  3168. THERMAL_ATTRS(0),
  3169. THERMAL_ATTRS(1),
  3170. THERMAL_ATTRS(2),
  3171. THERMAL_ATTRS(3),
  3172. THERMAL_ATTRS(4),
  3173. THERMAL_ATTRS(5),
  3174. THERMAL_ATTRS(6),
  3175. THERMAL_ATTRS(7),
  3176. NULL
  3177. };
  3178. static const struct attribute_group thermal_temp_input16_group = {
  3179. .attrs = thermal_temp_input_attr
  3180. };
  3181. static const struct attribute_group thermal_temp_input8_group = {
  3182. .attrs = &thermal_temp_input_attr[8]
  3183. };
  3184. #undef THERMAL_SENSOR_ATTR_TEMP
  3185. #undef THERMAL_ATTRS
  3186. /* --------------------------------------------------------------------- */
  3187. static int __init thermal_init(struct ibm_init_struct *iibm)
  3188. {
  3189. u8 t, ta1, ta2;
  3190. int i;
  3191. int acpi_tmp7;
  3192. int res;
  3193. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  3194. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  3195. if (thinkpad_id.ec_model) {
  3196. /*
  3197. * Direct EC access mode: sensors at registers
  3198. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  3199. * non-implemented, thermal sensors return 0x80 when
  3200. * not available
  3201. */
  3202. ta1 = ta2 = 0;
  3203. for (i = 0; i < 8; i++) {
  3204. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  3205. ta1 |= t;
  3206. } else {
  3207. ta1 = 0;
  3208. break;
  3209. }
  3210. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  3211. ta2 |= t;
  3212. } else {
  3213. ta1 = 0;
  3214. break;
  3215. }
  3216. }
  3217. if (ta1 == 0) {
  3218. /* This is sheer paranoia, but we handle it anyway */
  3219. if (acpi_tmp7) {
  3220. printk(TPACPI_ERR
  3221. "ThinkPad ACPI EC access misbehaving, "
  3222. "falling back to ACPI TMPx access "
  3223. "mode\n");
  3224. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3225. } else {
  3226. printk(TPACPI_ERR
  3227. "ThinkPad ACPI EC access misbehaving, "
  3228. "disabling thermal sensors access\n");
  3229. thermal_read_mode = TPACPI_THERMAL_NONE;
  3230. }
  3231. } else {
  3232. thermal_read_mode =
  3233. (ta2 != 0) ?
  3234. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  3235. }
  3236. } else if (acpi_tmp7) {
  3237. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  3238. /* 600e/x, 770e, 770x */
  3239. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  3240. } else {
  3241. /* Standard ACPI TMPx access, max 8 sensors */
  3242. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3243. }
  3244. } else {
  3245. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  3246. thermal_read_mode = TPACPI_THERMAL_NONE;
  3247. }
  3248. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  3249. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  3250. thermal_read_mode);
  3251. switch (thermal_read_mode) {
  3252. case TPACPI_THERMAL_TPEC_16:
  3253. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3254. &thermal_temp_input16_group);
  3255. if (res)
  3256. return res;
  3257. break;
  3258. case TPACPI_THERMAL_TPEC_8:
  3259. case TPACPI_THERMAL_ACPI_TMP07:
  3260. case TPACPI_THERMAL_ACPI_UPDT:
  3261. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3262. &thermal_temp_input8_group);
  3263. if (res)
  3264. return res;
  3265. break;
  3266. case TPACPI_THERMAL_NONE:
  3267. default:
  3268. return 1;
  3269. }
  3270. return 0;
  3271. }
  3272. static void thermal_exit(void)
  3273. {
  3274. switch (thermal_read_mode) {
  3275. case TPACPI_THERMAL_TPEC_16:
  3276. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3277. &thermal_temp_input16_group);
  3278. break;
  3279. case TPACPI_THERMAL_TPEC_8:
  3280. case TPACPI_THERMAL_ACPI_TMP07:
  3281. case TPACPI_THERMAL_ACPI_UPDT:
  3282. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3283. &thermal_temp_input16_group);
  3284. break;
  3285. case TPACPI_THERMAL_NONE:
  3286. default:
  3287. break;
  3288. }
  3289. }
  3290. static int thermal_read(char *p)
  3291. {
  3292. int len = 0;
  3293. int n, i;
  3294. struct ibm_thermal_sensors_struct t;
  3295. n = thermal_get_sensors(&t);
  3296. if (unlikely(n < 0))
  3297. return n;
  3298. len += sprintf(p + len, "temperatures:\t");
  3299. if (n > 0) {
  3300. for (i = 0; i < (n - 1); i++)
  3301. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  3302. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  3303. } else
  3304. len += sprintf(p + len, "not supported\n");
  3305. return len;
  3306. }
  3307. static struct ibm_struct thermal_driver_data = {
  3308. .name = "thermal",
  3309. .read = thermal_read,
  3310. .exit = thermal_exit,
  3311. };
  3312. /*************************************************************************
  3313. * EC Dump subdriver
  3314. */
  3315. static u8 ecdump_regs[256];
  3316. static int ecdump_read(char *p)
  3317. {
  3318. int len = 0;
  3319. int i, j;
  3320. u8 v;
  3321. len += sprintf(p + len, "EC "
  3322. " +00 +01 +02 +03 +04 +05 +06 +07"
  3323. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  3324. for (i = 0; i < 256; i += 16) {
  3325. len += sprintf(p + len, "EC 0x%02x:", i);
  3326. for (j = 0; j < 16; j++) {
  3327. if (!acpi_ec_read(i + j, &v))
  3328. break;
  3329. if (v != ecdump_regs[i + j])
  3330. len += sprintf(p + len, " *%02x", v);
  3331. else
  3332. len += sprintf(p + len, " %02x", v);
  3333. ecdump_regs[i + j] = v;
  3334. }
  3335. len += sprintf(p + len, "\n");
  3336. if (j != 16)
  3337. break;
  3338. }
  3339. /* These are way too dangerous to advertise openly... */
  3340. #if 0
  3341. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  3342. " (<offset> is 00-ff, <value> is 00-ff)\n");
  3343. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  3344. " (<offset> is 00-ff, <value> is 0-255)\n");
  3345. #endif
  3346. return len;
  3347. }
  3348. static int ecdump_write(char *buf)
  3349. {
  3350. char *cmd;
  3351. int i, v;
  3352. while ((cmd = next_cmd(&buf))) {
  3353. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  3354. /* i and v set */
  3355. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  3356. /* i and v set */
  3357. } else
  3358. return -EINVAL;
  3359. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  3360. if (!acpi_ec_write(i, v))
  3361. return -EIO;
  3362. } else
  3363. return -EINVAL;
  3364. }
  3365. return 0;
  3366. }
  3367. static struct ibm_struct ecdump_driver_data = {
  3368. .name = "ecdump",
  3369. .read = ecdump_read,
  3370. .write = ecdump_write,
  3371. .flags.experimental = 1,
  3372. };
  3373. /*************************************************************************
  3374. * Backlight/brightness subdriver
  3375. */
  3376. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  3377. static struct backlight_device *ibm_backlight_device;
  3378. static int brightness_offset = 0x31;
  3379. static int brightness_mode;
  3380. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  3381. static struct mutex brightness_mutex;
  3382. /*
  3383. * ThinkPads can read brightness from two places: EC 0x31, or
  3384. * CMOS NVRAM byte 0x5E, bits 0-3.
  3385. */
  3386. static int brightness_get(struct backlight_device *bd)
  3387. {
  3388. u8 lec = 0, lcmos = 0, level = 0;
  3389. if (brightness_mode & 1) {
  3390. if (!acpi_ec_read(brightness_offset, &lec))
  3391. return -EIO;
  3392. lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3393. level = lec;
  3394. };
  3395. if (brightness_mode & 2) {
  3396. lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  3397. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  3398. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  3399. lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3400. level = lcmos;
  3401. }
  3402. if (brightness_mode == 3 && lec != lcmos) {
  3403. printk(TPACPI_ERR
  3404. "CMOS NVRAM (%u) and EC (%u) do not agree "
  3405. "on display brightness level\n",
  3406. (unsigned int) lcmos,
  3407. (unsigned int) lec);
  3408. return -EIO;
  3409. }
  3410. return level;
  3411. }
  3412. /* May return EINTR which can always be mapped to ERESTARTSYS */
  3413. static int brightness_set(int value)
  3414. {
  3415. int cmos_cmd, inc, i, res;
  3416. int current_value;
  3417. if (value > ((tp_features.bright_16levels)? 15 : 7))
  3418. return -EINVAL;
  3419. res = mutex_lock_interruptible(&brightness_mutex);
  3420. if (res < 0)
  3421. return res;
  3422. current_value = brightness_get(NULL);
  3423. if (current_value < 0) {
  3424. res = current_value;
  3425. goto errout;
  3426. }
  3427. cmos_cmd = value > current_value ?
  3428. TP_CMOS_BRIGHTNESS_UP :
  3429. TP_CMOS_BRIGHTNESS_DOWN;
  3430. inc = (value > current_value)? 1 : -1;
  3431. res = 0;
  3432. for (i = current_value; i != value; i += inc) {
  3433. if ((brightness_mode & 2) &&
  3434. issue_thinkpad_cmos_command(cmos_cmd)) {
  3435. res = -EIO;
  3436. goto errout;
  3437. }
  3438. if ((brightness_mode & 1) &&
  3439. !acpi_ec_write(brightness_offset, i + inc)) {
  3440. res = -EIO;
  3441. goto errout;;
  3442. }
  3443. }
  3444. errout:
  3445. mutex_unlock(&brightness_mutex);
  3446. return res;
  3447. }
  3448. /* sysfs backlight class ----------------------------------------------- */
  3449. static int brightness_update_status(struct backlight_device *bd)
  3450. {
  3451. /* it is the backlight class's job (caller) to handle
  3452. * EINTR and other errors properly */
  3453. return brightness_set(
  3454. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  3455. bd->props.power == FB_BLANK_UNBLANK) ?
  3456. bd->props.brightness : 0);
  3457. }
  3458. static struct backlight_ops ibm_backlight_data = {
  3459. .get_brightness = brightness_get,
  3460. .update_status = brightness_update_status,
  3461. };
  3462. /* --------------------------------------------------------------------- */
  3463. static int __init tpacpi_query_bcll_levels(acpi_handle handle)
  3464. {
  3465. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  3466. union acpi_object *obj;
  3467. int rc;
  3468. if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
  3469. obj = (union acpi_object *)buffer.pointer;
  3470. if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
  3471. printk(TPACPI_ERR "Unknown BCLL data, "
  3472. "please report this to %s\n", TPACPI_MAIL);
  3473. rc = 0;
  3474. } else {
  3475. rc = obj->package.count;
  3476. }
  3477. } else {
  3478. return 0;
  3479. }
  3480. kfree(buffer.pointer);
  3481. return rc;
  3482. }
  3483. static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
  3484. void *context, void **rv)
  3485. {
  3486. char name[ACPI_PATH_SEGMENT_LENGTH];
  3487. struct acpi_buffer buffer = { sizeof(name), &name };
  3488. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  3489. !strncmp("BCLL", name, sizeof(name) - 1)) {
  3490. if (tpacpi_query_bcll_levels(handle) == 16) {
  3491. *rv = handle;
  3492. return AE_CTRL_TERMINATE;
  3493. } else {
  3494. return AE_OK;
  3495. }
  3496. } else {
  3497. return AE_OK;
  3498. }
  3499. }
  3500. static int __init brightness_check_levels(void)
  3501. {
  3502. int status;
  3503. void *found_node = NULL;
  3504. if (!vid_handle) {
  3505. TPACPI_ACPIHANDLE_INIT(vid);
  3506. }
  3507. if (!vid_handle)
  3508. return 0;
  3509. /* Search for a BCLL package with 16 levels */
  3510. status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
  3511. brightness_find_bcll, NULL,
  3512. &found_node);
  3513. return (ACPI_SUCCESS(status) && found_node != NULL);
  3514. }
  3515. static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
  3516. void *context, void **rv)
  3517. {
  3518. char name[ACPI_PATH_SEGMENT_LENGTH];
  3519. struct acpi_buffer buffer = { sizeof(name), &name };
  3520. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
  3521. !strncmp("_BCL", name, sizeof(name) - 1)) {
  3522. *rv = handle;
  3523. return AE_CTRL_TERMINATE;
  3524. } else {
  3525. return AE_OK;
  3526. }
  3527. }
  3528. static int __init brightness_check_std_acpi_support(void)
  3529. {
  3530. int status;
  3531. void *found_node = NULL;
  3532. if (!vid_handle) {
  3533. TPACPI_ACPIHANDLE_INIT(vid);
  3534. }
  3535. if (!vid_handle)
  3536. return 0;
  3537. /* Search for a _BCL method, but don't execute it */
  3538. status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
  3539. brightness_find_bcl, NULL, &found_node);
  3540. return (ACPI_SUCCESS(status) && found_node != NULL);
  3541. }
  3542. static int __init brightness_init(struct ibm_init_struct *iibm)
  3543. {
  3544. int b;
  3545. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  3546. mutex_init(&brightness_mutex);
  3547. if (!brightness_enable) {
  3548. dbg_printk(TPACPI_DBG_INIT,
  3549. "brightness support disabled by "
  3550. "module parameter\n");
  3551. return 1;
  3552. } else if (brightness_enable > 1) {
  3553. if (brightness_check_std_acpi_support()) {
  3554. printk(TPACPI_NOTICE
  3555. "standard ACPI backlight interface "
  3556. "available, not loading native one...\n");
  3557. return 1;
  3558. }
  3559. }
  3560. if (!brightness_mode) {
  3561. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
  3562. brightness_mode = 2;
  3563. else
  3564. brightness_mode = 3;
  3565. dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
  3566. brightness_mode);
  3567. }
  3568. if (brightness_mode > 3)
  3569. return -EINVAL;
  3570. tp_features.bright_16levels =
  3571. thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
  3572. brightness_check_levels();
  3573. b = brightness_get(NULL);
  3574. if (b < 0)
  3575. return 1;
  3576. if (tp_features.bright_16levels)
  3577. printk(TPACPI_INFO
  3578. "detected a 16-level brightness capable ThinkPad\n");
  3579. ibm_backlight_device = backlight_device_register(
  3580. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  3581. &ibm_backlight_data);
  3582. if (IS_ERR(ibm_backlight_device)) {
  3583. printk(TPACPI_ERR "Could not register backlight device\n");
  3584. return PTR_ERR(ibm_backlight_device);
  3585. }
  3586. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  3587. ibm_backlight_device->props.max_brightness =
  3588. (tp_features.bright_16levels)? 15 : 7;
  3589. ibm_backlight_device->props.brightness = b;
  3590. backlight_update_status(ibm_backlight_device);
  3591. return 0;
  3592. }
  3593. static void brightness_exit(void)
  3594. {
  3595. if (ibm_backlight_device) {
  3596. vdbg_printk(TPACPI_DBG_EXIT,
  3597. "calling backlight_device_unregister()\n");
  3598. backlight_device_unregister(ibm_backlight_device);
  3599. ibm_backlight_device = NULL;
  3600. }
  3601. }
  3602. static int brightness_read(char *p)
  3603. {
  3604. int len = 0;
  3605. int level;
  3606. level = brightness_get(NULL);
  3607. if (level < 0) {
  3608. len += sprintf(p + len, "level:\t\tunreadable\n");
  3609. } else {
  3610. len += sprintf(p + len, "level:\t\t%d\n", level);
  3611. len += sprintf(p + len, "commands:\tup, down\n");
  3612. len += sprintf(p + len, "commands:\tlevel <level>"
  3613. " (<level> is 0-%d)\n",
  3614. (tp_features.bright_16levels) ? 15 : 7);
  3615. }
  3616. return len;
  3617. }
  3618. static int brightness_write(char *buf)
  3619. {
  3620. int level;
  3621. int rc;
  3622. char *cmd;
  3623. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  3624. level = brightness_get(NULL);
  3625. if (level < 0)
  3626. return level;
  3627. while ((cmd = next_cmd(&buf))) {
  3628. if (strlencmp(cmd, "up") == 0) {
  3629. if (level < max_level)
  3630. level++;
  3631. } else if (strlencmp(cmd, "down") == 0) {
  3632. if (level > 0)
  3633. level--;
  3634. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  3635. level >= 0 && level <= max_level) {
  3636. /* new level set */
  3637. } else
  3638. return -EINVAL;
  3639. }
  3640. /*
  3641. * Now we know what the final level should be, so we try to set it.
  3642. * Doing it this way makes the syscall restartable in case of EINTR
  3643. */
  3644. rc = brightness_set(level);
  3645. return (rc == -EINTR)? ERESTARTSYS : rc;
  3646. }
  3647. static struct ibm_struct brightness_driver_data = {
  3648. .name = "brightness",
  3649. .read = brightness_read,
  3650. .write = brightness_write,
  3651. .exit = brightness_exit,
  3652. };
  3653. /*************************************************************************
  3654. * Volume subdriver
  3655. */
  3656. static int volume_offset = 0x30;
  3657. static int volume_read(char *p)
  3658. {
  3659. int len = 0;
  3660. u8 level;
  3661. if (!acpi_ec_read(volume_offset, &level)) {
  3662. len += sprintf(p + len, "level:\t\tunreadable\n");
  3663. } else {
  3664. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  3665. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  3666. len += sprintf(p + len, "commands:\tup, down, mute\n");
  3667. len += sprintf(p + len, "commands:\tlevel <level>"
  3668. " (<level> is 0-15)\n");
  3669. }
  3670. return len;
  3671. }
  3672. static int volume_write(char *buf)
  3673. {
  3674. int cmos_cmd, inc, i;
  3675. u8 level, mute;
  3676. int new_level, new_mute;
  3677. char *cmd;
  3678. while ((cmd = next_cmd(&buf))) {
  3679. if (!acpi_ec_read(volume_offset, &level))
  3680. return -EIO;
  3681. new_mute = mute = level & 0x40;
  3682. new_level = level = level & 0xf;
  3683. if (strlencmp(cmd, "up") == 0) {
  3684. if (mute)
  3685. new_mute = 0;
  3686. else
  3687. new_level = level == 15 ? 15 : level + 1;
  3688. } else if (strlencmp(cmd, "down") == 0) {
  3689. if (mute)
  3690. new_mute = 0;
  3691. else
  3692. new_level = level == 0 ? 0 : level - 1;
  3693. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  3694. new_level >= 0 && new_level <= 15) {
  3695. /* new_level set */
  3696. } else if (strlencmp(cmd, "mute") == 0) {
  3697. new_mute = 0x40;
  3698. } else
  3699. return -EINVAL;
  3700. if (new_level != level) {
  3701. /* mute doesn't change */
  3702. cmos_cmd = (new_level > level) ?
  3703. TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  3704. inc = new_level > level ? 1 : -1;
  3705. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  3706. !acpi_ec_write(volume_offset, level)))
  3707. return -EIO;
  3708. for (i = level; i != new_level; i += inc)
  3709. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  3710. !acpi_ec_write(volume_offset, i + inc))
  3711. return -EIO;
  3712. if (mute &&
  3713. (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  3714. !acpi_ec_write(volume_offset, new_level + mute))) {
  3715. return -EIO;
  3716. }
  3717. }
  3718. if (new_mute != mute) {
  3719. /* level doesn't change */
  3720. cmos_cmd = (new_mute) ?
  3721. TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  3722. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  3723. !acpi_ec_write(volume_offset, level + new_mute))
  3724. return -EIO;
  3725. }
  3726. }
  3727. return 0;
  3728. }
  3729. static struct ibm_struct volume_driver_data = {
  3730. .name = "volume",
  3731. .read = volume_read,
  3732. .write = volume_write,
  3733. };
  3734. /*************************************************************************
  3735. * Fan subdriver
  3736. */
  3737. /*
  3738. * FAN ACCESS MODES
  3739. *
  3740. * TPACPI_FAN_RD_ACPI_GFAN:
  3741. * ACPI GFAN method: returns fan level
  3742. *
  3743. * see TPACPI_FAN_WR_ACPI_SFAN
  3744. * EC 0x2f (HFSP) not available if GFAN exists
  3745. *
  3746. * TPACPI_FAN_WR_ACPI_SFAN:
  3747. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  3748. *
  3749. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  3750. * it for writing.
  3751. *
  3752. * TPACPI_FAN_WR_TPEC:
  3753. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  3754. * Supported on almost all ThinkPads
  3755. *
  3756. * Fan speed changes of any sort (including those caused by the
  3757. * disengaged mode) are usually done slowly by the firmware as the
  3758. * maximum ammount of fan duty cycle change per second seems to be
  3759. * limited.
  3760. *
  3761. * Reading is not available if GFAN exists.
  3762. * Writing is not available if SFAN exists.
  3763. *
  3764. * Bits
  3765. * 7 automatic mode engaged;
  3766. * (default operation mode of the ThinkPad)
  3767. * fan level is ignored in this mode.
  3768. * 6 full speed mode (takes precedence over bit 7);
  3769. * not available on all thinkpads. May disable
  3770. * the tachometer while the fan controller ramps up
  3771. * the speed (which can take up to a few *minutes*).
  3772. * Speeds up fan to 100% duty-cycle, which is far above
  3773. * the standard RPM levels. It is not impossible that
  3774. * it could cause hardware damage.
  3775. * 5-3 unused in some models. Extra bits for fan level
  3776. * in others, but still useless as all values above
  3777. * 7 map to the same speed as level 7 in these models.
  3778. * 2-0 fan level (0..7 usually)
  3779. * 0x00 = stop
  3780. * 0x07 = max (set when temperatures critical)
  3781. * Some ThinkPads may have other levels, see
  3782. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  3783. *
  3784. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  3785. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  3786. * does so, its initial value is meaningless (0x07).
  3787. *
  3788. * For firmware bugs, refer to:
  3789. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  3790. *
  3791. * ----
  3792. *
  3793. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  3794. * Main fan tachometer reading (in RPM)
  3795. *
  3796. * This register is present on all ThinkPads with a new-style EC, and
  3797. * it is known not to be present on the A21m/e, and T22, as there is
  3798. * something else in offset 0x84 according to the ACPI DSDT. Other
  3799. * ThinkPads from this same time period (and earlier) probably lack the
  3800. * tachometer as well.
  3801. *
  3802. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  3803. * was never fixed by IBM to report the EC firmware version string
  3804. * probably support the tachometer (like the early X models), so
  3805. * detecting it is quite hard. We need more data to know for sure.
  3806. *
  3807. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  3808. * might result.
  3809. *
  3810. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  3811. * mode.
  3812. *
  3813. * For firmware bugs, refer to:
  3814. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  3815. *
  3816. * TPACPI_FAN_WR_ACPI_FANS:
  3817. * ThinkPad X31, X40, X41. Not available in the X60.
  3818. *
  3819. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  3820. * high speed. ACPI DSDT seems to map these three speeds to levels
  3821. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  3822. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  3823. *
  3824. * The speeds are stored on handles
  3825. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  3826. *
  3827. * There are three default speed sets, acessible as handles:
  3828. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  3829. *
  3830. * ACPI DSDT switches which set is in use depending on various
  3831. * factors.
  3832. *
  3833. * TPACPI_FAN_WR_TPEC is also available and should be used to
  3834. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  3835. * but the ACPI tables just mention level 7.
  3836. */
  3837. enum { /* Fan control constants */
  3838. fan_status_offset = 0x2f, /* EC register 0x2f */
  3839. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  3840. * 0x84 must be read before 0x85 */
  3841. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  3842. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  3843. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  3844. };
  3845. enum fan_status_access_mode {
  3846. TPACPI_FAN_NONE = 0, /* No fan status or control */
  3847. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  3848. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  3849. };
  3850. enum fan_control_access_mode {
  3851. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  3852. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  3853. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  3854. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  3855. };
  3856. enum fan_control_commands {
  3857. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  3858. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  3859. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  3860. * and also watchdog cmd */
  3861. };
  3862. static int fan_control_allowed;
  3863. static enum fan_status_access_mode fan_status_access_mode;
  3864. static enum fan_control_access_mode fan_control_access_mode;
  3865. static enum fan_control_commands fan_control_commands;
  3866. static u8 fan_control_initial_status;
  3867. static u8 fan_control_desired_level;
  3868. static int fan_watchdog_maxinterval;
  3869. static struct mutex fan_mutex;
  3870. static void fan_watchdog_fire(struct work_struct *ignored);
  3871. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  3872. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  3873. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  3874. "\\FSPD", /* 600e/x, 770e, 770x */
  3875. ); /* all others */
  3876. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  3877. "JFNS", /* 770x-JL */
  3878. ); /* all others */
  3879. /*
  3880. * Call with fan_mutex held
  3881. */
  3882. static void fan_update_desired_level(u8 status)
  3883. {
  3884. if ((status &
  3885. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  3886. if (status > 7)
  3887. fan_control_desired_level = 7;
  3888. else
  3889. fan_control_desired_level = status;
  3890. }
  3891. }
  3892. static int fan_get_status(u8 *status)
  3893. {
  3894. u8 s;
  3895. /* TODO:
  3896. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  3897. switch (fan_status_access_mode) {
  3898. case TPACPI_FAN_RD_ACPI_GFAN:
  3899. /* 570, 600e/x, 770e, 770x */
  3900. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  3901. return -EIO;
  3902. if (likely(status))
  3903. *status = s & 0x07;
  3904. break;
  3905. case TPACPI_FAN_RD_TPEC:
  3906. /* all except 570, 600e/x, 770e, 770x */
  3907. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  3908. return -EIO;
  3909. if (likely(status))
  3910. *status = s;
  3911. break;
  3912. default:
  3913. return -ENXIO;
  3914. }
  3915. return 0;
  3916. }
  3917. static int fan_get_status_safe(u8 *status)
  3918. {
  3919. int rc;
  3920. u8 s;
  3921. if (mutex_lock_interruptible(&fan_mutex))
  3922. return -ERESTARTSYS;
  3923. rc = fan_get_status(&s);
  3924. if (!rc)
  3925. fan_update_desired_level(s);
  3926. mutex_unlock(&fan_mutex);
  3927. if (status)
  3928. *status = s;
  3929. return rc;
  3930. }
  3931. static int fan_get_speed(unsigned int *speed)
  3932. {
  3933. u8 hi, lo;
  3934. switch (fan_status_access_mode) {
  3935. case TPACPI_FAN_RD_TPEC:
  3936. /* all except 570, 600e/x, 770e, 770x */
  3937. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  3938. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  3939. return -EIO;
  3940. if (likely(speed))
  3941. *speed = (hi << 8) | lo;
  3942. break;
  3943. default:
  3944. return -ENXIO;
  3945. }
  3946. return 0;
  3947. }
  3948. static int fan_set_level(int level)
  3949. {
  3950. if (!fan_control_allowed)
  3951. return -EPERM;
  3952. switch (fan_control_access_mode) {
  3953. case TPACPI_FAN_WR_ACPI_SFAN:
  3954. if (level >= 0 && level <= 7) {
  3955. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  3956. return -EIO;
  3957. } else
  3958. return -EINVAL;
  3959. break;
  3960. case TPACPI_FAN_WR_ACPI_FANS:
  3961. case TPACPI_FAN_WR_TPEC:
  3962. if ((level != TP_EC_FAN_AUTO) &&
  3963. (level != TP_EC_FAN_FULLSPEED) &&
  3964. ((level < 0) || (level > 7)))
  3965. return -EINVAL;
  3966. /* safety net should the EC not support AUTO
  3967. * or FULLSPEED mode bits and just ignore them */
  3968. if (level & TP_EC_FAN_FULLSPEED)
  3969. level |= 7; /* safety min speed 7 */
  3970. else if (level & TP_EC_FAN_FULLSPEED)
  3971. level |= 4; /* safety min speed 4 */
  3972. if (!acpi_ec_write(fan_status_offset, level))
  3973. return -EIO;
  3974. else
  3975. tp_features.fan_ctrl_status_undef = 0;
  3976. break;
  3977. default:
  3978. return -ENXIO;
  3979. }
  3980. return 0;
  3981. }
  3982. static int fan_set_level_safe(int level)
  3983. {
  3984. int rc;
  3985. if (!fan_control_allowed)
  3986. return -EPERM;
  3987. if (mutex_lock_interruptible(&fan_mutex))
  3988. return -ERESTARTSYS;
  3989. if (level == TPACPI_FAN_LAST_LEVEL)
  3990. level = fan_control_desired_level;
  3991. rc = fan_set_level(level);
  3992. if (!rc)
  3993. fan_update_desired_level(level);
  3994. mutex_unlock(&fan_mutex);
  3995. return rc;
  3996. }
  3997. static int fan_set_enable(void)
  3998. {
  3999. u8 s;
  4000. int rc;
  4001. if (!fan_control_allowed)
  4002. return -EPERM;
  4003. if (mutex_lock_interruptible(&fan_mutex))
  4004. return -ERESTARTSYS;
  4005. switch (fan_control_access_mode) {
  4006. case TPACPI_FAN_WR_ACPI_FANS:
  4007. case TPACPI_FAN_WR_TPEC:
  4008. rc = fan_get_status(&s);
  4009. if (rc < 0)
  4010. break;
  4011. /* Don't go out of emergency fan mode */
  4012. if (s != 7) {
  4013. s &= 0x07;
  4014. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  4015. }
  4016. if (!acpi_ec_write(fan_status_offset, s))
  4017. rc = -EIO;
  4018. else {
  4019. tp_features.fan_ctrl_status_undef = 0;
  4020. rc = 0;
  4021. }
  4022. break;
  4023. case TPACPI_FAN_WR_ACPI_SFAN:
  4024. rc = fan_get_status(&s);
  4025. if (rc < 0)
  4026. break;
  4027. s &= 0x07;
  4028. /* Set fan to at least level 4 */
  4029. s |= 4;
  4030. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  4031. rc = -EIO;
  4032. else
  4033. rc = 0;
  4034. break;
  4035. default:
  4036. rc = -ENXIO;
  4037. }
  4038. mutex_unlock(&fan_mutex);
  4039. return rc;
  4040. }
  4041. static int fan_set_disable(void)
  4042. {
  4043. int rc;
  4044. if (!fan_control_allowed)
  4045. return -EPERM;
  4046. if (mutex_lock_interruptible(&fan_mutex))
  4047. return -ERESTARTSYS;
  4048. rc = 0;
  4049. switch (fan_control_access_mode) {
  4050. case TPACPI_FAN_WR_ACPI_FANS:
  4051. case TPACPI_FAN_WR_TPEC:
  4052. if (!acpi_ec_write(fan_status_offset, 0x00))
  4053. rc = -EIO;
  4054. else {
  4055. fan_control_desired_level = 0;
  4056. tp_features.fan_ctrl_status_undef = 0;
  4057. }
  4058. break;
  4059. case TPACPI_FAN_WR_ACPI_SFAN:
  4060. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  4061. rc = -EIO;
  4062. else
  4063. fan_control_desired_level = 0;
  4064. break;
  4065. default:
  4066. rc = -ENXIO;
  4067. }
  4068. mutex_unlock(&fan_mutex);
  4069. return rc;
  4070. }
  4071. static int fan_set_speed(int speed)
  4072. {
  4073. int rc;
  4074. if (!fan_control_allowed)
  4075. return -EPERM;
  4076. if (mutex_lock_interruptible(&fan_mutex))
  4077. return -ERESTARTSYS;
  4078. rc = 0;
  4079. switch (fan_control_access_mode) {
  4080. case TPACPI_FAN_WR_ACPI_FANS:
  4081. if (speed >= 0 && speed <= 65535) {
  4082. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  4083. speed, speed, speed))
  4084. rc = -EIO;
  4085. } else
  4086. rc = -EINVAL;
  4087. break;
  4088. default:
  4089. rc = -ENXIO;
  4090. }
  4091. mutex_unlock(&fan_mutex);
  4092. return rc;
  4093. }
  4094. static void fan_watchdog_reset(void)
  4095. {
  4096. static int fan_watchdog_active;
  4097. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  4098. return;
  4099. if (fan_watchdog_active)
  4100. cancel_delayed_work(&fan_watchdog_task);
  4101. if (fan_watchdog_maxinterval > 0 &&
  4102. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  4103. fan_watchdog_active = 1;
  4104. if (!schedule_delayed_work(&fan_watchdog_task,
  4105. msecs_to_jiffies(fan_watchdog_maxinterval
  4106. * 1000))) {
  4107. printk(TPACPI_ERR
  4108. "failed to schedule the fan watchdog, "
  4109. "watchdog will not trigger\n");
  4110. }
  4111. } else
  4112. fan_watchdog_active = 0;
  4113. }
  4114. static void fan_watchdog_fire(struct work_struct *ignored)
  4115. {
  4116. int rc;
  4117. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  4118. return;
  4119. printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
  4120. rc = fan_set_enable();
  4121. if (rc < 0) {
  4122. printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
  4123. "will try again later...\n", -rc);
  4124. /* reschedule for later */
  4125. fan_watchdog_reset();
  4126. }
  4127. }
  4128. /*
  4129. * SYSFS fan layout: hwmon compatible (device)
  4130. *
  4131. * pwm*_enable:
  4132. * 0: "disengaged" mode
  4133. * 1: manual mode
  4134. * 2: native EC "auto" mode (recommended, hardware default)
  4135. *
  4136. * pwm*: set speed in manual mode, ignored otherwise.
  4137. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  4138. * interpolation.
  4139. *
  4140. * fan*_input: tachometer reading, RPM
  4141. *
  4142. *
  4143. * SYSFS fan layout: extensions
  4144. *
  4145. * fan_watchdog (driver):
  4146. * fan watchdog interval in seconds, 0 disables (default), max 120
  4147. */
  4148. /* sysfs fan pwm1_enable ----------------------------------------------- */
  4149. static ssize_t fan_pwm1_enable_show(struct device *dev,
  4150. struct device_attribute *attr,
  4151. char *buf)
  4152. {
  4153. int res, mode;
  4154. u8 status;
  4155. res = fan_get_status_safe(&status);
  4156. if (res)
  4157. return res;
  4158. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4159. if (status != fan_control_initial_status) {
  4160. tp_features.fan_ctrl_status_undef = 0;
  4161. } else {
  4162. /* Return most likely status. In fact, it
  4163. * might be the only possible status */
  4164. status = TP_EC_FAN_AUTO;
  4165. }
  4166. }
  4167. if (status & TP_EC_FAN_FULLSPEED) {
  4168. mode = 0;
  4169. } else if (status & TP_EC_FAN_AUTO) {
  4170. mode = 2;
  4171. } else
  4172. mode = 1;
  4173. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  4174. }
  4175. static ssize_t fan_pwm1_enable_store(struct device *dev,
  4176. struct device_attribute *attr,
  4177. const char *buf, size_t count)
  4178. {
  4179. unsigned long t;
  4180. int res, level;
  4181. if (parse_strtoul(buf, 2, &t))
  4182. return -EINVAL;
  4183. switch (t) {
  4184. case 0:
  4185. level = TP_EC_FAN_FULLSPEED;
  4186. break;
  4187. case 1:
  4188. level = TPACPI_FAN_LAST_LEVEL;
  4189. break;
  4190. case 2:
  4191. level = TP_EC_FAN_AUTO;
  4192. break;
  4193. case 3:
  4194. /* reserved for software-controlled auto mode */
  4195. return -ENOSYS;
  4196. default:
  4197. return -EINVAL;
  4198. }
  4199. res = fan_set_level_safe(level);
  4200. if (res == -ENXIO)
  4201. return -EINVAL;
  4202. else if (res < 0)
  4203. return res;
  4204. fan_watchdog_reset();
  4205. return count;
  4206. }
  4207. static struct device_attribute dev_attr_fan_pwm1_enable =
  4208. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  4209. fan_pwm1_enable_show, fan_pwm1_enable_store);
  4210. /* sysfs fan pwm1 ------------------------------------------------------ */
  4211. static ssize_t fan_pwm1_show(struct device *dev,
  4212. struct device_attribute *attr,
  4213. char *buf)
  4214. {
  4215. int res;
  4216. u8 status;
  4217. res = fan_get_status_safe(&status);
  4218. if (res)
  4219. return res;
  4220. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4221. if (status != fan_control_initial_status) {
  4222. tp_features.fan_ctrl_status_undef = 0;
  4223. } else {
  4224. status = TP_EC_FAN_AUTO;
  4225. }
  4226. }
  4227. if ((status &
  4228. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  4229. status = fan_control_desired_level;
  4230. if (status > 7)
  4231. status = 7;
  4232. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  4233. }
  4234. static ssize_t fan_pwm1_store(struct device *dev,
  4235. struct device_attribute *attr,
  4236. const char *buf, size_t count)
  4237. {
  4238. unsigned long s;
  4239. int rc;
  4240. u8 status, newlevel;
  4241. if (parse_strtoul(buf, 255, &s))
  4242. return -EINVAL;
  4243. /* scale down from 0-255 to 0-7 */
  4244. newlevel = (s >> 5) & 0x07;
  4245. if (mutex_lock_interruptible(&fan_mutex))
  4246. return -ERESTARTSYS;
  4247. rc = fan_get_status(&status);
  4248. if (!rc && (status &
  4249. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4250. rc = fan_set_level(newlevel);
  4251. if (rc == -ENXIO)
  4252. rc = -EINVAL;
  4253. else if (!rc) {
  4254. fan_update_desired_level(newlevel);
  4255. fan_watchdog_reset();
  4256. }
  4257. }
  4258. mutex_unlock(&fan_mutex);
  4259. return (rc)? rc : count;
  4260. }
  4261. static struct device_attribute dev_attr_fan_pwm1 =
  4262. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  4263. fan_pwm1_show, fan_pwm1_store);
  4264. /* sysfs fan fan1_input ------------------------------------------------ */
  4265. static ssize_t fan_fan1_input_show(struct device *dev,
  4266. struct device_attribute *attr,
  4267. char *buf)
  4268. {
  4269. int res;
  4270. unsigned int speed;
  4271. res = fan_get_speed(&speed);
  4272. if (res < 0)
  4273. return res;
  4274. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  4275. }
  4276. static struct device_attribute dev_attr_fan_fan1_input =
  4277. __ATTR(fan1_input, S_IRUGO,
  4278. fan_fan1_input_show, NULL);
  4279. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  4280. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  4281. char *buf)
  4282. {
  4283. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  4284. }
  4285. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  4286. const char *buf, size_t count)
  4287. {
  4288. unsigned long t;
  4289. if (parse_strtoul(buf, 120, &t))
  4290. return -EINVAL;
  4291. if (!fan_control_allowed)
  4292. return -EPERM;
  4293. fan_watchdog_maxinterval = t;
  4294. fan_watchdog_reset();
  4295. return count;
  4296. }
  4297. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  4298. fan_fan_watchdog_show, fan_fan_watchdog_store);
  4299. /* --------------------------------------------------------------------- */
  4300. static struct attribute *fan_attributes[] = {
  4301. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  4302. &dev_attr_fan_fan1_input.attr,
  4303. NULL
  4304. };
  4305. static const struct attribute_group fan_attr_group = {
  4306. .attrs = fan_attributes,
  4307. };
  4308. static int __init fan_init(struct ibm_init_struct *iibm)
  4309. {
  4310. int rc;
  4311. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  4312. mutex_init(&fan_mutex);
  4313. fan_status_access_mode = TPACPI_FAN_NONE;
  4314. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4315. fan_control_commands = 0;
  4316. fan_watchdog_maxinterval = 0;
  4317. tp_features.fan_ctrl_status_undef = 0;
  4318. fan_control_desired_level = 7;
  4319. TPACPI_ACPIHANDLE_INIT(fans);
  4320. TPACPI_ACPIHANDLE_INIT(gfan);
  4321. TPACPI_ACPIHANDLE_INIT(sfan);
  4322. if (gfan_handle) {
  4323. /* 570, 600e/x, 770e, 770x */
  4324. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  4325. } else {
  4326. /* all other ThinkPads: note that even old-style
  4327. * ThinkPad ECs supports the fan control register */
  4328. if (likely(acpi_ec_read(fan_status_offset,
  4329. &fan_control_initial_status))) {
  4330. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  4331. /* In some ThinkPads, neither the EC nor the ACPI
  4332. * DSDT initialize the fan status, and it ends up
  4333. * being set to 0x07 when it *could* be either
  4334. * 0x07 or 0x80.
  4335. *
  4336. * Enable for TP-1Y (T43), TP-78 (R51e),
  4337. * TP-76 (R52), TP-70 (T43, R52), which are known
  4338. * to be buggy. */
  4339. if (fan_control_initial_status == 0x07) {
  4340. switch (thinkpad_id.ec_model) {
  4341. case 0x5931: /* TP-1Y */
  4342. case 0x3837: /* TP-78 */
  4343. case 0x3637: /* TP-76 */
  4344. case 0x3037: /* TP-70 */
  4345. printk(TPACPI_NOTICE
  4346. "fan_init: initial fan status "
  4347. "is unknown, assuming it is "
  4348. "in auto mode\n");
  4349. tp_features.fan_ctrl_status_undef = 1;
  4350. ;;
  4351. }
  4352. }
  4353. } else {
  4354. printk(TPACPI_ERR
  4355. "ThinkPad ACPI EC access misbehaving, "
  4356. "fan status and control unavailable\n");
  4357. return 1;
  4358. }
  4359. }
  4360. if (sfan_handle) {
  4361. /* 570, 770x-JL */
  4362. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  4363. fan_control_commands |=
  4364. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  4365. } else {
  4366. if (!gfan_handle) {
  4367. /* gfan without sfan means no fan control */
  4368. /* all other models implement TP EC 0x2f control */
  4369. if (fans_handle) {
  4370. /* X31, X40, X41 */
  4371. fan_control_access_mode =
  4372. TPACPI_FAN_WR_ACPI_FANS;
  4373. fan_control_commands |=
  4374. TPACPI_FAN_CMD_SPEED |
  4375. TPACPI_FAN_CMD_LEVEL |
  4376. TPACPI_FAN_CMD_ENABLE;
  4377. } else {
  4378. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  4379. fan_control_commands |=
  4380. TPACPI_FAN_CMD_LEVEL |
  4381. TPACPI_FAN_CMD_ENABLE;
  4382. }
  4383. }
  4384. }
  4385. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  4386. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  4387. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  4388. fan_status_access_mode, fan_control_access_mode);
  4389. /* fan control master switch */
  4390. if (!fan_control_allowed) {
  4391. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4392. fan_control_commands = 0;
  4393. dbg_printk(TPACPI_DBG_INIT,
  4394. "fan control features disabled by parameter\n");
  4395. }
  4396. /* update fan_control_desired_level */
  4397. if (fan_status_access_mode != TPACPI_FAN_NONE)
  4398. fan_get_status_safe(NULL);
  4399. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  4400. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  4401. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4402. &fan_attr_group);
  4403. if (!(rc < 0))
  4404. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  4405. &driver_attr_fan_watchdog);
  4406. if (rc < 0)
  4407. return rc;
  4408. return 0;
  4409. } else
  4410. return 1;
  4411. }
  4412. static void fan_exit(void)
  4413. {
  4414. vdbg_printk(TPACPI_DBG_EXIT,
  4415. "cancelling any pending fan watchdog tasks\n");
  4416. /* FIXME: can we really do this unconditionally? */
  4417. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  4418. driver_remove_file(&tpacpi_hwmon_pdriver.driver,
  4419. &driver_attr_fan_watchdog);
  4420. cancel_delayed_work(&fan_watchdog_task);
  4421. flush_scheduled_work();
  4422. }
  4423. static int fan_read(char *p)
  4424. {
  4425. int len = 0;
  4426. int rc;
  4427. u8 status;
  4428. unsigned int speed = 0;
  4429. switch (fan_status_access_mode) {
  4430. case TPACPI_FAN_RD_ACPI_GFAN:
  4431. /* 570, 600e/x, 770e, 770x */
  4432. rc = fan_get_status_safe(&status);
  4433. if (rc < 0)
  4434. return rc;
  4435. len += sprintf(p + len, "status:\t\t%s\n"
  4436. "level:\t\t%d\n",
  4437. (status != 0) ? "enabled" : "disabled", status);
  4438. break;
  4439. case TPACPI_FAN_RD_TPEC:
  4440. /* all except 570, 600e/x, 770e, 770x */
  4441. rc = fan_get_status_safe(&status);
  4442. if (rc < 0)
  4443. return rc;
  4444. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4445. if (status != fan_control_initial_status)
  4446. tp_features.fan_ctrl_status_undef = 0;
  4447. else
  4448. /* Return most likely status. In fact, it
  4449. * might be the only possible status */
  4450. status = TP_EC_FAN_AUTO;
  4451. }
  4452. len += sprintf(p + len, "status:\t\t%s\n",
  4453. (status != 0) ? "enabled" : "disabled");
  4454. rc = fan_get_speed(&speed);
  4455. if (rc < 0)
  4456. return rc;
  4457. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  4458. if (status & TP_EC_FAN_FULLSPEED)
  4459. /* Disengaged mode takes precedence */
  4460. len += sprintf(p + len, "level:\t\tdisengaged\n");
  4461. else if (status & TP_EC_FAN_AUTO)
  4462. len += sprintf(p + len, "level:\t\tauto\n");
  4463. else
  4464. len += sprintf(p + len, "level:\t\t%d\n", status);
  4465. break;
  4466. case TPACPI_FAN_NONE:
  4467. default:
  4468. len += sprintf(p + len, "status:\t\tnot supported\n");
  4469. }
  4470. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  4471. len += sprintf(p + len, "commands:\tlevel <level>");
  4472. switch (fan_control_access_mode) {
  4473. case TPACPI_FAN_WR_ACPI_SFAN:
  4474. len += sprintf(p + len, " (<level> is 0-7)\n");
  4475. break;
  4476. default:
  4477. len += sprintf(p + len, " (<level> is 0-7, "
  4478. "auto, disengaged, full-speed)\n");
  4479. break;
  4480. }
  4481. }
  4482. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  4483. len += sprintf(p + len, "commands:\tenable, disable\n"
  4484. "commands:\twatchdog <timeout> (<timeout> "
  4485. "is 0 (off), 1-120 (seconds))\n");
  4486. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  4487. len += sprintf(p + len, "commands:\tspeed <speed>"
  4488. " (<speed> is 0-65535)\n");
  4489. return len;
  4490. }
  4491. static int fan_write_cmd_level(const char *cmd, int *rc)
  4492. {
  4493. int level;
  4494. if (strlencmp(cmd, "level auto") == 0)
  4495. level = TP_EC_FAN_AUTO;
  4496. else if ((strlencmp(cmd, "level disengaged") == 0) |
  4497. (strlencmp(cmd, "level full-speed") == 0))
  4498. level = TP_EC_FAN_FULLSPEED;
  4499. else if (sscanf(cmd, "level %d", &level) != 1)
  4500. return 0;
  4501. *rc = fan_set_level_safe(level);
  4502. if (*rc == -ENXIO)
  4503. printk(TPACPI_ERR "level command accepted for unsupported "
  4504. "access mode %d", fan_control_access_mode);
  4505. return 1;
  4506. }
  4507. static int fan_write_cmd_enable(const char *cmd, int *rc)
  4508. {
  4509. if (strlencmp(cmd, "enable") != 0)
  4510. return 0;
  4511. *rc = fan_set_enable();
  4512. if (*rc == -ENXIO)
  4513. printk(TPACPI_ERR "enable command accepted for unsupported "
  4514. "access mode %d", fan_control_access_mode);
  4515. return 1;
  4516. }
  4517. static int fan_write_cmd_disable(const char *cmd, int *rc)
  4518. {
  4519. if (strlencmp(cmd, "disable") != 0)
  4520. return 0;
  4521. *rc = fan_set_disable();
  4522. if (*rc == -ENXIO)
  4523. printk(TPACPI_ERR "disable command accepted for unsupported "
  4524. "access mode %d", fan_control_access_mode);
  4525. return 1;
  4526. }
  4527. static int fan_write_cmd_speed(const char *cmd, int *rc)
  4528. {
  4529. int speed;
  4530. /* TODO:
  4531. * Support speed <low> <medium> <high> ? */
  4532. if (sscanf(cmd, "speed %d", &speed) != 1)
  4533. return 0;
  4534. *rc = fan_set_speed(speed);
  4535. if (*rc == -ENXIO)
  4536. printk(TPACPI_ERR "speed command accepted for unsupported "
  4537. "access mode %d", fan_control_access_mode);
  4538. return 1;
  4539. }
  4540. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  4541. {
  4542. int interval;
  4543. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  4544. return 0;
  4545. if (interval < 0 || interval > 120)
  4546. *rc = -EINVAL;
  4547. else
  4548. fan_watchdog_maxinterval = interval;
  4549. return 1;
  4550. }
  4551. static int fan_write(char *buf)
  4552. {
  4553. char *cmd;
  4554. int rc = 0;
  4555. while (!rc && (cmd = next_cmd(&buf))) {
  4556. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  4557. fan_write_cmd_level(cmd, &rc)) &&
  4558. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  4559. (fan_write_cmd_enable(cmd, &rc) ||
  4560. fan_write_cmd_disable(cmd, &rc) ||
  4561. fan_write_cmd_watchdog(cmd, &rc))) &&
  4562. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  4563. fan_write_cmd_speed(cmd, &rc))
  4564. )
  4565. rc = -EINVAL;
  4566. else if (!rc)
  4567. fan_watchdog_reset();
  4568. }
  4569. return rc;
  4570. }
  4571. static struct ibm_struct fan_driver_data = {
  4572. .name = "fan",
  4573. .read = fan_read,
  4574. .write = fan_write,
  4575. .exit = fan_exit,
  4576. };
  4577. /****************************************************************************
  4578. ****************************************************************************
  4579. *
  4580. * Infrastructure
  4581. *
  4582. ****************************************************************************
  4583. ****************************************************************************/
  4584. /* sysfs name ---------------------------------------------------------- */
  4585. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  4586. struct device_attribute *attr,
  4587. char *buf)
  4588. {
  4589. return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
  4590. }
  4591. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  4592. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  4593. /* --------------------------------------------------------------------- */
  4594. /* /proc support */
  4595. static struct proc_dir_entry *proc_dir;
  4596. /*
  4597. * Module and infrastructure proble, init and exit handling
  4598. */
  4599. static int force_load;
  4600. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  4601. static const char * __init str_supported(int is_supported)
  4602. {
  4603. static char text_unsupported[] __initdata = "not supported";
  4604. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  4605. }
  4606. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  4607. static void ibm_exit(struct ibm_struct *ibm)
  4608. {
  4609. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  4610. list_del_init(&ibm->all_drivers);
  4611. if (ibm->flags.acpi_notify_installed) {
  4612. dbg_printk(TPACPI_DBG_EXIT,
  4613. "%s: acpi_remove_notify_handler\n", ibm->name);
  4614. BUG_ON(!ibm->acpi);
  4615. acpi_remove_notify_handler(*ibm->acpi->handle,
  4616. ibm->acpi->type,
  4617. dispatch_acpi_notify);
  4618. ibm->flags.acpi_notify_installed = 0;
  4619. ibm->flags.acpi_notify_installed = 0;
  4620. }
  4621. if (ibm->flags.proc_created) {
  4622. dbg_printk(TPACPI_DBG_EXIT,
  4623. "%s: remove_proc_entry\n", ibm->name);
  4624. remove_proc_entry(ibm->name, proc_dir);
  4625. ibm->flags.proc_created = 0;
  4626. }
  4627. if (ibm->flags.acpi_driver_registered) {
  4628. dbg_printk(TPACPI_DBG_EXIT,
  4629. "%s: acpi_bus_unregister_driver\n", ibm->name);
  4630. BUG_ON(!ibm->acpi);
  4631. acpi_bus_unregister_driver(ibm->acpi->driver);
  4632. kfree(ibm->acpi->driver);
  4633. ibm->acpi->driver = NULL;
  4634. ibm->flags.acpi_driver_registered = 0;
  4635. }
  4636. if (ibm->flags.init_called && ibm->exit) {
  4637. ibm->exit();
  4638. ibm->flags.init_called = 0;
  4639. }
  4640. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  4641. }
  4642. static int __init ibm_init(struct ibm_init_struct *iibm)
  4643. {
  4644. int ret;
  4645. struct ibm_struct *ibm = iibm->data;
  4646. struct proc_dir_entry *entry;
  4647. BUG_ON(ibm == NULL);
  4648. INIT_LIST_HEAD(&ibm->all_drivers);
  4649. if (ibm->flags.experimental && !experimental)
  4650. return 0;
  4651. dbg_printk(TPACPI_DBG_INIT,
  4652. "probing for %s\n", ibm->name);
  4653. if (iibm->init) {
  4654. ret = iibm->init(iibm);
  4655. if (ret > 0)
  4656. return 0; /* probe failed */
  4657. if (ret)
  4658. return ret;
  4659. ibm->flags.init_called = 1;
  4660. }
  4661. if (ibm->acpi) {
  4662. if (ibm->acpi->hid) {
  4663. ret = register_tpacpi_subdriver(ibm);
  4664. if (ret)
  4665. goto err_out;
  4666. }
  4667. if (ibm->acpi->notify) {
  4668. ret = setup_acpi_notify(ibm);
  4669. if (ret == -ENODEV) {
  4670. printk(TPACPI_NOTICE "disabling subdriver %s\n",
  4671. ibm->name);
  4672. ret = 0;
  4673. goto err_out;
  4674. }
  4675. if (ret < 0)
  4676. goto err_out;
  4677. }
  4678. }
  4679. dbg_printk(TPACPI_DBG_INIT,
  4680. "%s installed\n", ibm->name);
  4681. if (ibm->read) {
  4682. entry = create_proc_entry(ibm->name,
  4683. S_IFREG | S_IRUGO | S_IWUSR,
  4684. proc_dir);
  4685. if (!entry) {
  4686. printk(TPACPI_ERR "unable to create proc entry %s\n",
  4687. ibm->name);
  4688. ret = -ENODEV;
  4689. goto err_out;
  4690. }
  4691. entry->owner = THIS_MODULE;
  4692. entry->data = ibm;
  4693. entry->read_proc = &dispatch_procfs_read;
  4694. if (ibm->write)
  4695. entry->write_proc = &dispatch_procfs_write;
  4696. ibm->flags.proc_created = 1;
  4697. }
  4698. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  4699. return 0;
  4700. err_out:
  4701. dbg_printk(TPACPI_DBG_INIT,
  4702. "%s: at error exit path with result %d\n",
  4703. ibm->name, ret);
  4704. ibm_exit(ibm);
  4705. return (ret < 0)? ret : 0;
  4706. }
  4707. /* Probing */
  4708. static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
  4709. {
  4710. const struct dmi_device *dev = NULL;
  4711. char ec_fw_string[18];
  4712. if (!tp)
  4713. return;
  4714. memset(tp, 0, sizeof(*tp));
  4715. if (dmi_name_in_vendors("IBM"))
  4716. tp->vendor = PCI_VENDOR_ID_IBM;
  4717. else if (dmi_name_in_vendors("LENOVO"))
  4718. tp->vendor = PCI_VENDOR_ID_LENOVO;
  4719. else
  4720. return;
  4721. tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
  4722. GFP_KERNEL);
  4723. if (!tp->bios_version_str)
  4724. return;
  4725. tp->bios_model = tp->bios_version_str[0]
  4726. | (tp->bios_version_str[1] << 8);
  4727. /*
  4728. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  4729. * X32 or newer, all Z series; Some models must have an
  4730. * up-to-date BIOS or they will not be detected.
  4731. *
  4732. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  4733. */
  4734. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  4735. if (sscanf(dev->name,
  4736. "IBM ThinkPad Embedded Controller -[%17c",
  4737. ec_fw_string) == 1) {
  4738. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  4739. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  4740. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  4741. tp->ec_model = ec_fw_string[0]
  4742. | (ec_fw_string[1] << 8);
  4743. break;
  4744. }
  4745. }
  4746. tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
  4747. GFP_KERNEL);
  4748. if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
  4749. kfree(tp->model_str);
  4750. tp->model_str = NULL;
  4751. }
  4752. }
  4753. static int __init probe_for_thinkpad(void)
  4754. {
  4755. int is_thinkpad;
  4756. if (acpi_disabled)
  4757. return -ENODEV;
  4758. /*
  4759. * Non-ancient models have better DMI tagging, but very old models
  4760. * don't.
  4761. */
  4762. is_thinkpad = (thinkpad_id.model_str != NULL);
  4763. /* ec is required because many other handles are relative to it */
  4764. TPACPI_ACPIHANDLE_INIT(ec);
  4765. if (!ec_handle) {
  4766. if (is_thinkpad)
  4767. printk(TPACPI_ERR
  4768. "Not yet supported ThinkPad detected!\n");
  4769. return -ENODEV;
  4770. }
  4771. /*
  4772. * Risks a regression on very old machines, but reduces potential
  4773. * false positives a damn great deal
  4774. */
  4775. if (!is_thinkpad)
  4776. is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
  4777. if (!is_thinkpad && !force_load)
  4778. return -ENODEV;
  4779. return 0;
  4780. }
  4781. /* Module init, exit, parameters */
  4782. static struct ibm_init_struct ibms_init[] __initdata = {
  4783. {
  4784. .init = thinkpad_acpi_driver_init,
  4785. .data = &thinkpad_acpi_driver_data,
  4786. },
  4787. {
  4788. .init = hotkey_init,
  4789. .data = &hotkey_driver_data,
  4790. },
  4791. {
  4792. .init = bluetooth_init,
  4793. .data = &bluetooth_driver_data,
  4794. },
  4795. {
  4796. .init = wan_init,
  4797. .data = &wan_driver_data,
  4798. },
  4799. {
  4800. .init = video_init,
  4801. .data = &video_driver_data,
  4802. },
  4803. {
  4804. .init = light_init,
  4805. .data = &light_driver_data,
  4806. },
  4807. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  4808. {
  4809. .init = dock_init,
  4810. .data = &dock_driver_data[0],
  4811. },
  4812. {
  4813. .init = dock_init2,
  4814. .data = &dock_driver_data[1],
  4815. },
  4816. #endif
  4817. #ifdef CONFIG_THINKPAD_ACPI_BAY
  4818. {
  4819. .init = bay_init,
  4820. .data = &bay_driver_data,
  4821. },
  4822. #endif
  4823. {
  4824. .init = cmos_init,
  4825. .data = &cmos_driver_data,
  4826. },
  4827. {
  4828. .init = led_init,
  4829. .data = &led_driver_data,
  4830. },
  4831. {
  4832. .init = beep_init,
  4833. .data = &beep_driver_data,
  4834. },
  4835. {
  4836. .init = thermal_init,
  4837. .data = &thermal_driver_data,
  4838. },
  4839. {
  4840. .data = &ecdump_driver_data,
  4841. },
  4842. {
  4843. .init = brightness_init,
  4844. .data = &brightness_driver_data,
  4845. },
  4846. {
  4847. .data = &volume_driver_data,
  4848. },
  4849. {
  4850. .init = fan_init,
  4851. .data = &fan_driver_data,
  4852. },
  4853. };
  4854. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  4855. {
  4856. unsigned int i;
  4857. struct ibm_struct *ibm;
  4858. if (!kp || !kp->name || !val)
  4859. return -EINVAL;
  4860. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  4861. ibm = ibms_init[i].data;
  4862. WARN_ON(ibm == NULL);
  4863. if (!ibm || !ibm->name)
  4864. continue;
  4865. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  4866. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  4867. return -ENOSPC;
  4868. strcpy(ibms_init[i].param, val);
  4869. strcat(ibms_init[i].param, ",");
  4870. return 0;
  4871. }
  4872. }
  4873. return -EINVAL;
  4874. }
  4875. module_param(experimental, int, 0);
  4876. MODULE_PARM_DESC(experimental,
  4877. "Enables experimental features when non-zero");
  4878. module_param_named(debug, dbg_level, uint, 0);
  4879. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  4880. module_param(force_load, bool, 0);
  4881. MODULE_PARM_DESC(force_load,
  4882. "Attempts to load the driver even on a "
  4883. "mis-identified ThinkPad when true");
  4884. module_param_named(fan_control, fan_control_allowed, bool, 0);
  4885. MODULE_PARM_DESC(fan_control,
  4886. "Enables setting fan parameters features when true");
  4887. module_param_named(brightness_mode, brightness_mode, int, 0);
  4888. MODULE_PARM_DESC(brightness_mode,
  4889. "Selects brightness control strategy: "
  4890. "0=auto, 1=EC, 2=CMOS, 3=both");
  4891. module_param(brightness_enable, uint, 0);
  4892. MODULE_PARM_DESC(brightness_enable,
  4893. "Enables backlight control when 1, disables when 0");
  4894. module_param(hotkey_report_mode, uint, 0);
  4895. MODULE_PARM_DESC(hotkey_report_mode,
  4896. "used for backwards compatibility with userspace, "
  4897. "see documentation");
  4898. #define TPACPI_PARAM(feature) \
  4899. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  4900. MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
  4901. "at module load, see documentation")
  4902. TPACPI_PARAM(hotkey);
  4903. TPACPI_PARAM(bluetooth);
  4904. TPACPI_PARAM(video);
  4905. TPACPI_PARAM(light);
  4906. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  4907. TPACPI_PARAM(dock);
  4908. #endif
  4909. #ifdef CONFIG_THINKPAD_ACPI_BAY
  4910. TPACPI_PARAM(bay);
  4911. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  4912. TPACPI_PARAM(cmos);
  4913. TPACPI_PARAM(led);
  4914. TPACPI_PARAM(beep);
  4915. TPACPI_PARAM(ecdump);
  4916. TPACPI_PARAM(brightness);
  4917. TPACPI_PARAM(volume);
  4918. TPACPI_PARAM(fan);
  4919. static void thinkpad_acpi_module_exit(void)
  4920. {
  4921. struct ibm_struct *ibm, *itmp;
  4922. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  4923. list_for_each_entry_safe_reverse(ibm, itmp,
  4924. &tpacpi_all_drivers,
  4925. all_drivers) {
  4926. ibm_exit(ibm);
  4927. }
  4928. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  4929. if (tpacpi_inputdev) {
  4930. if (tp_features.input_device_registered)
  4931. input_unregister_device(tpacpi_inputdev);
  4932. else
  4933. input_free_device(tpacpi_inputdev);
  4934. }
  4935. if (tpacpi_hwmon)
  4936. hwmon_device_unregister(tpacpi_hwmon);
  4937. if (tp_features.sensors_pdev_attrs_registered)
  4938. device_remove_file(&tpacpi_sensors_pdev->dev,
  4939. &dev_attr_thinkpad_acpi_pdev_name);
  4940. if (tpacpi_sensors_pdev)
  4941. platform_device_unregister(tpacpi_sensors_pdev);
  4942. if (tpacpi_pdev)
  4943. platform_device_unregister(tpacpi_pdev);
  4944. if (tp_features.sensors_pdrv_attrs_registered)
  4945. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  4946. if (tp_features.platform_drv_attrs_registered)
  4947. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  4948. if (tp_features.sensors_pdrv_registered)
  4949. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  4950. if (tp_features.platform_drv_registered)
  4951. platform_driver_unregister(&tpacpi_pdriver);
  4952. if (proc_dir)
  4953. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  4954. kfree(thinkpad_id.bios_version_str);
  4955. kfree(thinkpad_id.ec_version_str);
  4956. kfree(thinkpad_id.model_str);
  4957. }
  4958. static int __init thinkpad_acpi_module_init(void)
  4959. {
  4960. int ret, i;
  4961. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  4962. /* Parameter checking */
  4963. if (hotkey_report_mode > 2)
  4964. return -EINVAL;
  4965. /* Driver-level probe */
  4966. get_thinkpad_model_data(&thinkpad_id);
  4967. ret = probe_for_thinkpad();
  4968. if (ret) {
  4969. thinkpad_acpi_module_exit();
  4970. return ret;
  4971. }
  4972. /* Driver initialization */
  4973. TPACPI_ACPIHANDLE_INIT(ecrd);
  4974. TPACPI_ACPIHANDLE_INIT(ecwr);
  4975. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  4976. if (!proc_dir) {
  4977. printk(TPACPI_ERR
  4978. "unable to create proc dir " TPACPI_PROC_DIR);
  4979. thinkpad_acpi_module_exit();
  4980. return -ENODEV;
  4981. }
  4982. proc_dir->owner = THIS_MODULE;
  4983. ret = platform_driver_register(&tpacpi_pdriver);
  4984. if (ret) {
  4985. printk(TPACPI_ERR
  4986. "unable to register main platform driver\n");
  4987. thinkpad_acpi_module_exit();
  4988. return ret;
  4989. }
  4990. tp_features.platform_drv_registered = 1;
  4991. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  4992. if (ret) {
  4993. printk(TPACPI_ERR
  4994. "unable to register hwmon platform driver\n");
  4995. thinkpad_acpi_module_exit();
  4996. return ret;
  4997. }
  4998. tp_features.sensors_pdrv_registered = 1;
  4999. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  5000. if (!ret) {
  5001. tp_features.platform_drv_attrs_registered = 1;
  5002. ret = tpacpi_create_driver_attributes(
  5003. &tpacpi_hwmon_pdriver.driver);
  5004. }
  5005. if (ret) {
  5006. printk(TPACPI_ERR
  5007. "unable to create sysfs driver attributes\n");
  5008. thinkpad_acpi_module_exit();
  5009. return ret;
  5010. }
  5011. tp_features.sensors_pdrv_attrs_registered = 1;
  5012. /* Device initialization */
  5013. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
  5014. NULL, 0);
  5015. if (IS_ERR(tpacpi_pdev)) {
  5016. ret = PTR_ERR(tpacpi_pdev);
  5017. tpacpi_pdev = NULL;
  5018. printk(TPACPI_ERR "unable to register platform device\n");
  5019. thinkpad_acpi_module_exit();
  5020. return ret;
  5021. }
  5022. tpacpi_sensors_pdev = platform_device_register_simple(
  5023. TPACPI_HWMON_DRVR_NAME,
  5024. -1, NULL, 0);
  5025. if (IS_ERR(tpacpi_sensors_pdev)) {
  5026. ret = PTR_ERR(tpacpi_sensors_pdev);
  5027. tpacpi_sensors_pdev = NULL;
  5028. printk(TPACPI_ERR
  5029. "unable to register hwmon platform device\n");
  5030. thinkpad_acpi_module_exit();
  5031. return ret;
  5032. }
  5033. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  5034. &dev_attr_thinkpad_acpi_pdev_name);
  5035. if (ret) {
  5036. printk(TPACPI_ERR
  5037. "unable to create sysfs hwmon device attributes\n");
  5038. thinkpad_acpi_module_exit();
  5039. return ret;
  5040. }
  5041. tp_features.sensors_pdev_attrs_registered = 1;
  5042. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  5043. if (IS_ERR(tpacpi_hwmon)) {
  5044. ret = PTR_ERR(tpacpi_hwmon);
  5045. tpacpi_hwmon = NULL;
  5046. printk(TPACPI_ERR "unable to register hwmon device\n");
  5047. thinkpad_acpi_module_exit();
  5048. return ret;
  5049. }
  5050. mutex_init(&tpacpi_inputdev_send_mutex);
  5051. tpacpi_inputdev = input_allocate_device();
  5052. if (!tpacpi_inputdev) {
  5053. printk(TPACPI_ERR "unable to allocate input device\n");
  5054. thinkpad_acpi_module_exit();
  5055. return -ENOMEM;
  5056. } else {
  5057. /* Prepare input device, but don't register */
  5058. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  5059. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  5060. tpacpi_inputdev->id.bustype = BUS_HOST;
  5061. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  5062. thinkpad_id.vendor :
  5063. PCI_VENDOR_ID_IBM;
  5064. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  5065. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  5066. }
  5067. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  5068. ret = ibm_init(&ibms_init[i]);
  5069. if (ret >= 0 && *ibms_init[i].param)
  5070. ret = ibms_init[i].data->write(ibms_init[i].param);
  5071. if (ret < 0) {
  5072. thinkpad_acpi_module_exit();
  5073. return ret;
  5074. }
  5075. }
  5076. ret = input_register_device(tpacpi_inputdev);
  5077. if (ret < 0) {
  5078. printk(TPACPI_ERR "unable to register input device\n");
  5079. thinkpad_acpi_module_exit();
  5080. return ret;
  5081. } else {
  5082. tp_features.input_device_registered = 1;
  5083. }
  5084. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  5085. return 0;
  5086. }
  5087. /* Please remove this in year 2009 */
  5088. MODULE_ALIAS("ibm_acpi");
  5089. /*
  5090. * DMI matching for module autoloading
  5091. *
  5092. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  5093. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  5094. *
  5095. * Only models listed in thinkwiki will be supported, so add yours
  5096. * if it is not there yet.
  5097. */
  5098. #define IBM_BIOS_MODULE_ALIAS(__type) \
  5099. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
  5100. /* Non-ancient thinkpads */
  5101. MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
  5102. MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
  5103. /* Ancient thinkpad BIOSes have to be identified by
  5104. * BIOS type or model number, and there are far less
  5105. * BIOS types than model numbers... */
  5106. IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
  5107. IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
  5108. IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
  5109. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  5110. MODULE_DESCRIPTION(TPACPI_DESC);
  5111. MODULE_VERSION(TPACPI_VERSION);
  5112. MODULE_LICENSE("GPL");
  5113. module_init(thinkpad_acpi_module_init);
  5114. module_exit(thinkpad_acpi_module_exit);