thinkpad_acpi.c 149 KB

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