thinkpad_acpi.c 117 KB

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