ibm_acpi.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  1. /*
  2. * ibm_acpi.c - IBM ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define IBM_VERSION "0.13"
  23. /*
  24. * Changelog:
  25. *
  26. * 2006-11-22 0.13 new maintainer
  27. * changelog now lives in git commit history, and will
  28. * not be updated further in-file.
  29. *
  30. * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
  31. * 2005-03-17 0.11 support for 600e, 770x
  32. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  33. * support for 770e, G41
  34. * G40 and G41 don't have a thinklight
  35. * temperatures no longer experimental
  36. * experimental brightness control
  37. * experimental volume control
  38. * experimental fan enable/disable
  39. * 2005-01-16 0.10 fix module loading on R30, R31
  40. * 2005-01-16 0.9 support for 570, R30, R31
  41. * ultrabay support on A22p, A3x
  42. * limit arg for cmos, led, beep, drop experimental status
  43. * more capable led control on A21e, A22p, T20-22, X20
  44. * experimental temperatures and fan speed
  45. * experimental embedded controller register dump
  46. * mark more functions as __init, drop incorrect __exit
  47. * use MODULE_VERSION
  48. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  49. * fix parameter passing on module loading
  50. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  51. * thanks to Jim Radford <radford@blackbean.org>
  52. * 2004-11-08 0.8 fix init error case, don't return from a macro
  53. * thanks to Chris Wright <chrisw@osdl.org>
  54. * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
  55. * fix led control on A21e
  56. * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
  57. * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
  58. * proc file format changed
  59. * video_switch command
  60. * experimental cmos control
  61. * experimental led control
  62. * experimental acpi sounds
  63. * 2004-09-16 0.4 support for module parameters
  64. * hotkey mask can be prefixed by 0x
  65. * video output switching
  66. * video expansion control
  67. * ultrabay eject support
  68. * removed lcd brightness/on/off control, didn't work
  69. * 2004-08-17 0.3 support for R40
  70. * lcd off, brightness control
  71. * thinklight on/off
  72. * 2004-08-14 0.2 support for T series, X20
  73. * bluetooth enable/disable
  74. * hotkey events disabled by default
  75. * removed fan control, currently useless
  76. * 2004-08-09 0.1 initial release, support for X series
  77. */
  78. #include <linux/kernel.h>
  79. #include <linux/module.h>
  80. #include <linux/init.h>
  81. #include <linux/types.h>
  82. #include <linux/string.h>
  83. #include <linux/proc_fs.h>
  84. #include <linux/backlight.h>
  85. #include <linux/fb.h>
  86. #include <asm/uaccess.h>
  87. #include <linux/dmi.h>
  88. #include <linux/jiffies.h>
  89. #include <linux/workqueue.h>
  90. #include <acpi/acpi_drivers.h>
  91. #include <acpi/acnamesp.h>
  92. #define IBM_NAME "ibm"
  93. #define IBM_DESC "IBM ThinkPad ACPI Extras"
  94. #define IBM_FILE "ibm_acpi"
  95. #define IBM_URL "http://ibm-acpi.sf.net/"
  96. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  97. MODULE_DESCRIPTION(IBM_DESC);
  98. MODULE_VERSION(IBM_VERSION);
  99. MODULE_LICENSE("GPL");
  100. #define IBM_DIR IBM_NAME
  101. #define IBM_LOG IBM_FILE ": "
  102. #define IBM_ERR KERN_ERR IBM_LOG
  103. #define IBM_NOTICE KERN_NOTICE IBM_LOG
  104. #define IBM_INFO KERN_INFO IBM_LOG
  105. #define IBM_DEBUG KERN_DEBUG IBM_LOG
  106. #define IBM_MAX_ACPI_ARGS 3
  107. #define __unused __attribute__ ((unused))
  108. static int experimental;
  109. module_param(experimental, int, 0);
  110. static acpi_handle root_handle = NULL;
  111. #define IBM_HANDLE(object, parent, paths...) \
  112. static acpi_handle object##_handle; \
  113. static acpi_handle *object##_parent = &parent##_handle; \
  114. static char *object##_path; \
  115. static char *object##_paths[] = { paths }
  116. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  117. "\\_SB.PCI.ISA.EC", /* 570 */
  118. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  119. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  120. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  121. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  122. "\\_SB.PCI0.LPC.EC", /* all others */
  123. );
  124. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  125. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  126. "\\_SB.PCI0.VID0", /* 770e */
  127. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  128. "\\_SB.PCI0.AGP.VID", /* all others */
  129. ); /* R30, R31 */
  130. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  131. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  132. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  133. "\\CMS", /* R40, R40e */
  134. ); /* all others */
  135. #ifdef CONFIG_ACPI_IBM_DOCK
  136. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  137. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  138. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  139. "\\_SB.PCI.ISA.SLCE", /* 570 */
  140. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  141. #endif
  142. #ifdef CONFIG_ACPI_IBM_BAY
  143. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  144. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  145. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  146. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  147. ); /* A21e, R30, R31 */
  148. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  149. "_EJ0", /* all others */
  150. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  151. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  152. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  153. ); /* all others */
  154. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  155. "_EJ0", /* 770x */
  156. ); /* all others */
  157. #endif /* CONFIG_ACPI_IBM_BAY */
  158. /* don't list other alternatives as we install a notify handler on the 570 */
  159. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  160. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  161. "^HKEY", /* R30, R31 */
  162. "HKEY", /* all others */
  163. ); /* 570 */
  164. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  165. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  166. IBM_HANDLE(led, ec, "SLED", /* 570 */
  167. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  168. "LED", /* all others */
  169. ); /* R30, R31 */
  170. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  171. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  172. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  173. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  174. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  175. "\\FSPD", /* 600e/x, 770e, 770x */
  176. ); /* all others */
  177. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  178. "JFNS", /* 770x-JL */
  179. ); /* all others */
  180. #define IBM_HKEY_HID "IBM0068"
  181. #define IBM_PCI_HID "PNP0A03"
  182. enum thermal_access_mode {
  183. IBMACPI_THERMAL_NONE = 0, /* No thermal support */
  184. IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  185. IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  186. IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  187. IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  188. };
  189. #define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  190. struct ibm_thermal_sensors_struct {
  191. s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
  192. };
  193. /*
  194. * FAN ACCESS MODES
  195. *
  196. * IBMACPI_FAN_RD_ACPI_GFAN:
  197. * ACPI GFAN method: returns fan level
  198. *
  199. * see IBMACPI_FAN_WR_ACPI_SFAN
  200. * EC 0x2f not available if GFAN exists
  201. *
  202. * IBMACPI_FAN_WR_ACPI_SFAN:
  203. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  204. *
  205. * EC 0x2f might be available *for reading*, but never for writing.
  206. *
  207. * IBMACPI_FAN_WR_TPEC:
  208. * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
  209. * on almost all ThinkPads
  210. *
  211. * Fan speed changes of any sort (including those caused by the
  212. * disengaged mode) are usually done slowly by the firmware as the
  213. * maximum ammount of fan duty cycle change per second seems to be
  214. * limited.
  215. *
  216. * Reading is not available if GFAN exists.
  217. * Writing is not available if SFAN exists.
  218. *
  219. * Bits
  220. * 7 automatic mode engaged;
  221. * (default operation mode of the ThinkPad)
  222. * fan level is ignored in this mode.
  223. * 6 disengage mode (takes precedence over bit 7);
  224. * not available on all thinkpads. May disable
  225. * the tachometer, and speeds up fan to 100% duty-cycle,
  226. * which speeds it up far above the standard RPM
  227. * levels. It is not impossible that it could cause
  228. * hardware damage.
  229. * 5-3 unused in some models. Extra bits for fan level
  230. * in others, but still useless as all values above
  231. * 7 map to the same speed as level 7 in these models.
  232. * 2-0 fan level (0..7 usually)
  233. * 0x00 = stop
  234. * 0x07 = max (set when temperatures critical)
  235. * Some ThinkPads may have other levels, see
  236. * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  237. *
  238. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  239. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  240. * does so, its initial value is meaningless (0x07).
  241. *
  242. * For firmware bugs, refer to:
  243. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  244. *
  245. * ----
  246. *
  247. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  248. * Main fan tachometer reading (in RPM)
  249. *
  250. * This register is present on all ThinkPads with a new-style EC, and
  251. * it is known not to be present on the A21m/e, and T22, as there is
  252. * something else in offset 0x84 according to the ACPI DSDT. Other
  253. * ThinkPads from this same time period (and earlier) probably lack the
  254. * tachometer as well.
  255. *
  256. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  257. * was never fixed by IBM to report the EC firmware version string
  258. * probably support the tachometer (like the early X models), so
  259. * detecting it is quite hard. We need more data to know for sure.
  260. *
  261. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  262. * might result.
  263. *
  264. * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
  265. * register is not invalidated in ThinkPads that disable tachometer
  266. * readings. Thus, the tachometer readings go stale.
  267. *
  268. * For firmware bugs, refer to:
  269. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  270. *
  271. * IBMACPI_FAN_WR_ACPI_FANS:
  272. * ThinkPad X31, X40, X41. Not available in the X60.
  273. *
  274. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  275. * high speed. ACPI DSDT seems to map these three speeds to levels
  276. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  277. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  278. *
  279. * The speeds are stored on handles
  280. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  281. *
  282. * There are three default speed sets, acessible as handles:
  283. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  284. *
  285. * ACPI DSDT switches which set is in use depending on various
  286. * factors.
  287. *
  288. * IBMACPI_FAN_WR_TPEC is also available and should be used to
  289. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  290. * but the ACPI tables just mention level 7.
  291. */
  292. enum fan_status_access_mode {
  293. IBMACPI_FAN_NONE = 0, /* No fan status or control */
  294. IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  295. IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  296. };
  297. enum fan_control_access_mode {
  298. IBMACPI_FAN_WR_NONE = 0, /* No fan control */
  299. IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  300. IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  301. IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  302. };
  303. enum fan_control_commands {
  304. IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  305. IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  306. IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  307. * and also watchdog cmd */
  308. };
  309. enum { /* Fan control constants */
  310. fan_status_offset = 0x2f, /* EC register 0x2f */
  311. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  312. * 0x84 must be read before 0x85 */
  313. IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
  314. * disengaged */
  315. IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
  316. * control */
  317. };
  318. static char *ibm_thinkpad_ec_found = NULL;
  319. struct ibm_struct {
  320. char *name;
  321. char param[32];
  322. char *hid;
  323. struct acpi_driver *driver;
  324. int (*init) (void);
  325. int (*read) (char *);
  326. int (*write) (char *);
  327. void (*exit) (void);
  328. void (*notify) (struct ibm_struct *, u32);
  329. acpi_handle *handle;
  330. int type;
  331. struct acpi_device *device;
  332. int driver_registered;
  333. int proc_created;
  334. int init_called;
  335. int notify_installed;
  336. int experimental;
  337. };
  338. static struct proc_dir_entry *proc_dir = NULL;
  339. static struct backlight_device *ibm_backlight_device = NULL;
  340. #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
  341. #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  342. #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
  343. static int acpi_evalf(acpi_handle handle,
  344. void *res, char *method, char *fmt, ...)
  345. {
  346. char *fmt0 = fmt;
  347. struct acpi_object_list params;
  348. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  349. struct acpi_buffer result, *resultp;
  350. union acpi_object out_obj;
  351. acpi_status status;
  352. va_list ap;
  353. char res_type;
  354. int success;
  355. int quiet;
  356. if (!*fmt) {
  357. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  358. return 0;
  359. }
  360. if (*fmt == 'q') {
  361. quiet = 1;
  362. fmt++;
  363. } else
  364. quiet = 0;
  365. res_type = *(fmt++);
  366. params.count = 0;
  367. params.pointer = &in_objs[0];
  368. va_start(ap, fmt);
  369. while (*fmt) {
  370. char c = *(fmt++);
  371. switch (c) {
  372. case 'd': /* int */
  373. in_objs[params.count].integer.value = va_arg(ap, int);
  374. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  375. break;
  376. /* add more types as needed */
  377. default:
  378. printk(IBM_ERR "acpi_evalf() called "
  379. "with invalid format character '%c'\n", c);
  380. return 0;
  381. }
  382. }
  383. va_end(ap);
  384. if (res_type != 'v') {
  385. result.length = sizeof(out_obj);
  386. result.pointer = &out_obj;
  387. resultp = &result;
  388. } else
  389. resultp = NULL;
  390. status = acpi_evaluate_object(handle, method, &params, resultp);
  391. switch (res_type) {
  392. case 'd': /* int */
  393. if (res)
  394. *(int *)res = out_obj.integer.value;
  395. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  396. break;
  397. case 'v': /* void */
  398. success = status == AE_OK;
  399. break;
  400. /* add more types as needed */
  401. default:
  402. printk(IBM_ERR "acpi_evalf() called "
  403. "with invalid format character '%c'\n", res_type);
  404. return 0;
  405. }
  406. if (!success && !quiet)
  407. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  408. method, fmt0, status);
  409. return success;
  410. }
  411. static void __unused acpi_print_int(acpi_handle handle, char *method)
  412. {
  413. int i;
  414. if (acpi_evalf(handle, &i, method, "d"))
  415. printk(IBM_INFO "%s = 0x%x\n", method, i);
  416. else
  417. printk(IBM_ERR "error calling %s\n", method);
  418. }
  419. static char *next_cmd(char **cmds)
  420. {
  421. char *start = *cmds;
  422. char *end;
  423. while ((end = strchr(start, ',')) && end == start)
  424. start = end + 1;
  425. if (!end)
  426. return NULL;
  427. *end = 0;
  428. *cmds = end + 1;
  429. return start;
  430. }
  431. static int ibm_acpi_driver_init(void)
  432. {
  433. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  434. printk(IBM_INFO "%s\n", IBM_URL);
  435. if (ibm_thinkpad_ec_found)
  436. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  437. ibm_thinkpad_ec_found);
  438. return 0;
  439. }
  440. static int driver_read(char *p)
  441. {
  442. int len = 0;
  443. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  444. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  445. return len;
  446. }
  447. static int hotkey_supported;
  448. static int hotkey_mask_supported;
  449. static int hotkey_orig_status;
  450. static int hotkey_orig_mask;
  451. static int hotkey_get(int *status, int *mask)
  452. {
  453. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  454. return 0;
  455. if (hotkey_mask_supported)
  456. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  457. return 0;
  458. return 1;
  459. }
  460. static int hotkey_set(int status, int mask)
  461. {
  462. int i;
  463. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  464. return 0;
  465. if (hotkey_mask_supported)
  466. for (i = 0; i < 32; i++) {
  467. int bit = ((1 << i) & mask) != 0;
  468. if (!acpi_evalf(hkey_handle,
  469. NULL, "MHKM", "vdd", i + 1, bit))
  470. return 0;
  471. }
  472. return 1;
  473. }
  474. static int hotkey_init(void)
  475. {
  476. /* hotkey not supported on 570 */
  477. hotkey_supported = hkey_handle != NULL;
  478. if (hotkey_supported) {
  479. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  480. A30, R30, R31, T20-22, X20-21, X22-24 */
  481. hotkey_mask_supported =
  482. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  483. if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
  484. return -ENODEV;
  485. }
  486. return 0;
  487. }
  488. static int hotkey_read(char *p)
  489. {
  490. int status, mask;
  491. int len = 0;
  492. if (!hotkey_supported) {
  493. len += sprintf(p + len, "status:\t\tnot supported\n");
  494. return len;
  495. }
  496. if (!hotkey_get(&status, &mask))
  497. return -EIO;
  498. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  499. if (hotkey_mask_supported) {
  500. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  501. len += sprintf(p + len,
  502. "commands:\tenable, disable, reset, <mask>\n");
  503. } else {
  504. len += sprintf(p + len, "mask:\t\tnot supported\n");
  505. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  506. }
  507. return len;
  508. }
  509. static int hotkey_write(char *buf)
  510. {
  511. int status, mask;
  512. char *cmd;
  513. int do_cmd = 0;
  514. if (!hotkey_supported)
  515. return -ENODEV;
  516. if (!hotkey_get(&status, &mask))
  517. return -EIO;
  518. while ((cmd = next_cmd(&buf))) {
  519. if (strlencmp(cmd, "enable") == 0) {
  520. status = 1;
  521. } else if (strlencmp(cmd, "disable") == 0) {
  522. status = 0;
  523. } else if (strlencmp(cmd, "reset") == 0) {
  524. status = hotkey_orig_status;
  525. mask = hotkey_orig_mask;
  526. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  527. /* mask set */
  528. } else if (sscanf(cmd, "%x", &mask) == 1) {
  529. /* mask set */
  530. } else
  531. return -EINVAL;
  532. do_cmd = 1;
  533. }
  534. if (do_cmd && !hotkey_set(status, mask))
  535. return -EIO;
  536. return 0;
  537. }
  538. static void hotkey_exit(void)
  539. {
  540. if (hotkey_supported)
  541. hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  542. }
  543. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  544. {
  545. int hkey;
  546. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  547. acpi_bus_generate_event(ibm->device, event, hkey);
  548. else {
  549. printk(IBM_ERR "unknown hotkey event %d\n", event);
  550. acpi_bus_generate_event(ibm->device, event, 0);
  551. }
  552. }
  553. static int bluetooth_supported;
  554. static int bluetooth_init(void)
  555. {
  556. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  557. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  558. bluetooth_supported = hkey_handle &&
  559. acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
  560. return 0;
  561. }
  562. static int bluetooth_status(void)
  563. {
  564. int status;
  565. if (!bluetooth_supported ||
  566. !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  567. status = 0;
  568. return status;
  569. }
  570. static int bluetooth_read(char *p)
  571. {
  572. int len = 0;
  573. int status = bluetooth_status();
  574. if (!bluetooth_supported)
  575. len += sprintf(p + len, "status:\t\tnot supported\n");
  576. else if (!(status & 1))
  577. len += sprintf(p + len, "status:\t\tnot installed\n");
  578. else {
  579. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  580. len += sprintf(p + len, "commands:\tenable, disable\n");
  581. }
  582. return len;
  583. }
  584. static int bluetooth_write(char *buf)
  585. {
  586. int status = bluetooth_status();
  587. char *cmd;
  588. int do_cmd = 0;
  589. if (!bluetooth_supported)
  590. return -ENODEV;
  591. while ((cmd = next_cmd(&buf))) {
  592. if (strlencmp(cmd, "enable") == 0) {
  593. status |= 2;
  594. } else if (strlencmp(cmd, "disable") == 0) {
  595. status &= ~2;
  596. } else
  597. return -EINVAL;
  598. do_cmd = 1;
  599. }
  600. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  601. return -EIO;
  602. return 0;
  603. }
  604. static int wan_supported;
  605. static int wan_init(void)
  606. {
  607. wan_supported = hkey_handle &&
  608. acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
  609. return 0;
  610. }
  611. static int wan_status(void)
  612. {
  613. int status;
  614. if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  615. status = 0;
  616. return status;
  617. }
  618. static int wan_read(char *p)
  619. {
  620. int len = 0;
  621. int status = wan_status();
  622. if (!wan_supported)
  623. len += sprintf(p + len, "status:\t\tnot supported\n");
  624. else if (!(status & 1))
  625. len += sprintf(p + len, "status:\t\tnot installed\n");
  626. else {
  627. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  628. len += sprintf(p + len, "commands:\tenable, disable\n");
  629. }
  630. return len;
  631. }
  632. static int wan_write(char *buf)
  633. {
  634. int status = wan_status();
  635. char *cmd;
  636. int do_cmd = 0;
  637. if (!wan_supported)
  638. return -ENODEV;
  639. while ((cmd = next_cmd(&buf))) {
  640. if (strlencmp(cmd, "enable") == 0) {
  641. status |= 2;
  642. } else if (strlencmp(cmd, "disable") == 0) {
  643. status &= ~2;
  644. } else
  645. return -EINVAL;
  646. do_cmd = 1;
  647. }
  648. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  649. return -EIO;
  650. return 0;
  651. }
  652. enum video_access_mode {
  653. IBMACPI_VIDEO_NONE = 0,
  654. IBMACPI_VIDEO_570, /* 570 */
  655. IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  656. IBMACPI_VIDEO_NEW, /* all others */
  657. };
  658. static enum video_access_mode video_supported;
  659. static int video_orig_autosw;
  660. static int video_init(void)
  661. {
  662. int ivga;
  663. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  664. /* G41, assume IVGA doesn't change */
  665. vid_handle = vid2_handle;
  666. if (!vid_handle)
  667. /* video switching not supported on R30, R31 */
  668. video_supported = IBMACPI_VIDEO_NONE;
  669. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  670. /* 570 */
  671. video_supported = IBMACPI_VIDEO_570;
  672. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  673. /* 600e/x, 770e, 770x */
  674. video_supported = IBMACPI_VIDEO_770;
  675. else
  676. /* all others */
  677. video_supported = IBMACPI_VIDEO_NEW;
  678. return 0;
  679. }
  680. static int video_status(void)
  681. {
  682. int status = 0;
  683. int i;
  684. if (video_supported == IBMACPI_VIDEO_570) {
  685. if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
  686. status = i & 3;
  687. } else if (video_supported == IBMACPI_VIDEO_770) {
  688. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  689. status |= 0x01 * i;
  690. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  691. status |= 0x02 * i;
  692. } else if (video_supported == IBMACPI_VIDEO_NEW) {
  693. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
  694. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  695. status |= 0x02 * i;
  696. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
  697. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  698. status |= 0x01 * i;
  699. if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
  700. status |= 0x08 * i;
  701. }
  702. return status;
  703. }
  704. static int video_autosw(void)
  705. {
  706. int autosw = 0;
  707. if (video_supported == IBMACPI_VIDEO_570)
  708. acpi_evalf(vid_handle, &autosw, "SWIT", "d");
  709. else if (video_supported == IBMACPI_VIDEO_770 ||
  710. video_supported == IBMACPI_VIDEO_NEW)
  711. acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
  712. return autosw & 1;
  713. }
  714. static int video_read(char *p)
  715. {
  716. int status = video_status();
  717. int autosw = video_autosw();
  718. int len = 0;
  719. if (!video_supported) {
  720. len += sprintf(p + len, "status:\t\tnot supported\n");
  721. return len;
  722. }
  723. len += sprintf(p + len, "status:\t\tsupported\n");
  724. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  725. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  726. if (video_supported == IBMACPI_VIDEO_NEW)
  727. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  728. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  729. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  730. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  731. if (video_supported == IBMACPI_VIDEO_NEW)
  732. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  733. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  734. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  735. return len;
  736. }
  737. static int video_switch(void)
  738. {
  739. int autosw = video_autosw();
  740. int ret;
  741. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  742. return -EIO;
  743. ret = video_supported == IBMACPI_VIDEO_570 ?
  744. acpi_evalf(ec_handle, NULL, "_Q16", "v") :
  745. acpi_evalf(vid_handle, NULL, "VSWT", "v");
  746. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  747. return ret;
  748. }
  749. static int video_expand(void)
  750. {
  751. if (video_supported == IBMACPI_VIDEO_570)
  752. return acpi_evalf(ec_handle, NULL, "_Q17", "v");
  753. else if (video_supported == IBMACPI_VIDEO_770)
  754. return acpi_evalf(vid_handle, NULL, "VEXP", "v");
  755. else
  756. return acpi_evalf(NULL, NULL, "\\VEXP", "v");
  757. }
  758. static int video_switch2(int status)
  759. {
  760. int ret;
  761. if (video_supported == IBMACPI_VIDEO_570) {
  762. ret = acpi_evalf(NULL, NULL,
  763. "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
  764. } else if (video_supported == IBMACPI_VIDEO_770) {
  765. int autosw = video_autosw();
  766. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  767. return -EIO;
  768. ret = acpi_evalf(vid_handle, NULL,
  769. "ASWT", "vdd", status * 0x100, 0);
  770. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  771. } else {
  772. ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  773. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  774. }
  775. return ret;
  776. }
  777. static int video_write(char *buf)
  778. {
  779. char *cmd;
  780. int enable, disable, status;
  781. if (!video_supported)
  782. return -ENODEV;
  783. enable = disable = 0;
  784. while ((cmd = next_cmd(&buf))) {
  785. if (strlencmp(cmd, "lcd_enable") == 0) {
  786. enable |= 0x01;
  787. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  788. disable |= 0x01;
  789. } else if (strlencmp(cmd, "crt_enable") == 0) {
  790. enable |= 0x02;
  791. } else if (strlencmp(cmd, "crt_disable") == 0) {
  792. disable |= 0x02;
  793. } else if (video_supported == IBMACPI_VIDEO_NEW &&
  794. strlencmp(cmd, "dvi_enable") == 0) {
  795. enable |= 0x08;
  796. } else if (video_supported == IBMACPI_VIDEO_NEW &&
  797. strlencmp(cmd, "dvi_disable") == 0) {
  798. disable |= 0x08;
  799. } else if (strlencmp(cmd, "auto_enable") == 0) {
  800. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  801. return -EIO;
  802. } else if (strlencmp(cmd, "auto_disable") == 0) {
  803. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
  804. return -EIO;
  805. } else if (strlencmp(cmd, "video_switch") == 0) {
  806. if (!video_switch())
  807. return -EIO;
  808. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  809. if (!video_expand())
  810. return -EIO;
  811. } else
  812. return -EINVAL;
  813. }
  814. if (enable || disable) {
  815. status = (video_status() & 0x0f & ~disable) | enable;
  816. if (!video_switch2(status))
  817. return -EIO;
  818. }
  819. return 0;
  820. }
  821. static void video_exit(void)
  822. {
  823. acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
  824. }
  825. static int light_supported;
  826. static int light_status_supported;
  827. static int light_init(void)
  828. {
  829. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  830. light_supported = (cmos_handle || lght_handle) && !ledb_handle;
  831. if (light_supported)
  832. /* light status not supported on
  833. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  834. light_status_supported = acpi_evalf(ec_handle, NULL,
  835. "KBLT", "qv");
  836. return 0;
  837. }
  838. static int light_read(char *p)
  839. {
  840. int len = 0;
  841. int status = 0;
  842. if (!light_supported) {
  843. len += sprintf(p + len, "status:\t\tnot supported\n");
  844. } else if (!light_status_supported) {
  845. len += sprintf(p + len, "status:\t\tunknown\n");
  846. len += sprintf(p + len, "commands:\ton, off\n");
  847. } else {
  848. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  849. return -EIO;
  850. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  851. len += sprintf(p + len, "commands:\ton, off\n");
  852. }
  853. return len;
  854. }
  855. static int light_write(char *buf)
  856. {
  857. int cmos_cmd, lght_cmd;
  858. char *cmd;
  859. int success;
  860. if (!light_supported)
  861. return -ENODEV;
  862. while ((cmd = next_cmd(&buf))) {
  863. if (strlencmp(cmd, "on") == 0) {
  864. cmos_cmd = 0x0c;
  865. lght_cmd = 1;
  866. } else if (strlencmp(cmd, "off") == 0) {
  867. cmos_cmd = 0x0d;
  868. lght_cmd = 0;
  869. } else
  870. return -EINVAL;
  871. success = cmos_handle ?
  872. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  873. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  874. if (!success)
  875. return -EIO;
  876. }
  877. return 0;
  878. }
  879. #if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
  880. static int _sta(acpi_handle handle)
  881. {
  882. int status;
  883. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  884. status = 0;
  885. return status;
  886. }
  887. #endif
  888. #ifdef CONFIG_ACPI_IBM_DOCK
  889. #define dock_docked() (_sta(dock_handle) & 1)
  890. static int dock_read(char *p)
  891. {
  892. int len = 0;
  893. int docked = dock_docked();
  894. if (!dock_handle)
  895. len += sprintf(p + len, "status:\t\tnot supported\n");
  896. else if (!docked)
  897. len += sprintf(p + len, "status:\t\tundocked\n");
  898. else {
  899. len += sprintf(p + len, "status:\t\tdocked\n");
  900. len += sprintf(p + len, "commands:\tdock, undock\n");
  901. }
  902. return len;
  903. }
  904. static int dock_write(char *buf)
  905. {
  906. char *cmd;
  907. if (!dock_docked())
  908. return -ENODEV;
  909. while ((cmd = next_cmd(&buf))) {
  910. if (strlencmp(cmd, "undock") == 0) {
  911. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  912. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  913. return -EIO;
  914. } else if (strlencmp(cmd, "dock") == 0) {
  915. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  916. return -EIO;
  917. } else
  918. return -EINVAL;
  919. }
  920. return 0;
  921. }
  922. static void dock_notify(struct ibm_struct *ibm, u32 event)
  923. {
  924. int docked = dock_docked();
  925. int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
  926. if (event == 1 && !pci) /* 570 */
  927. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  928. else if (event == 1 && pci) /* 570 */
  929. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  930. else if (event == 3 && docked)
  931. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  932. else if (event == 3 && !docked)
  933. acpi_bus_generate_event(ibm->device, event, 2); /* undock */
  934. else if (event == 0 && docked)
  935. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  936. else {
  937. printk(IBM_ERR "unknown dock event %d, status %d\n",
  938. event, _sta(dock_handle));
  939. acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
  940. }
  941. }
  942. #endif
  943. #ifdef CONFIG_ACPI_IBM_BAY
  944. static int bay_status_supported;
  945. static int bay_status2_supported;
  946. static int bay_eject_supported;
  947. static int bay_eject2_supported;
  948. static int bay_init(void)
  949. {
  950. bay_status_supported = bay_handle &&
  951. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  952. bay_status2_supported = bay2_handle &&
  953. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  954. bay_eject_supported = bay_handle && bay_ej_handle &&
  955. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  956. bay_eject2_supported = bay2_handle && bay2_ej_handle &&
  957. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  958. return 0;
  959. }
  960. #define bay_occupied(b) (_sta(b##_handle) & 1)
  961. static int bay_read(char *p)
  962. {
  963. int len = 0;
  964. int occupied = bay_occupied(bay);
  965. int occupied2 = bay_occupied(bay2);
  966. int eject, eject2;
  967. len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
  968. (occupied ? "occupied" : "unoccupied") :
  969. "not supported");
  970. if (bay_status2_supported)
  971. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  972. "occupied" : "unoccupied");
  973. eject = bay_eject_supported && occupied;
  974. eject2 = bay_eject2_supported && occupied2;
  975. if (eject && eject2)
  976. len += sprintf(p + len, "commands:\teject, eject2\n");
  977. else if (eject)
  978. len += sprintf(p + len, "commands:\teject\n");
  979. else if (eject2)
  980. len += sprintf(p + len, "commands:\teject2\n");
  981. return len;
  982. }
  983. static int bay_write(char *buf)
  984. {
  985. char *cmd;
  986. if (!bay_eject_supported && !bay_eject2_supported)
  987. return -ENODEV;
  988. while ((cmd = next_cmd(&buf))) {
  989. if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
  990. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  991. return -EIO;
  992. } else if (bay_eject2_supported &&
  993. strlencmp(cmd, "eject2") == 0) {
  994. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  995. return -EIO;
  996. } else
  997. return -EINVAL;
  998. }
  999. return 0;
  1000. }
  1001. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1002. {
  1003. acpi_bus_generate_event(ibm->device, event, 0);
  1004. }
  1005. #endif /* CONFIG_ACPI_IBM_BAY */
  1006. static int cmos_read(char *p)
  1007. {
  1008. int len = 0;
  1009. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1010. R30, R31, T20-22, X20-21 */
  1011. if (!cmos_handle)
  1012. len += sprintf(p + len, "status:\t\tnot supported\n");
  1013. else {
  1014. len += sprintf(p + len, "status:\t\tsupported\n");
  1015. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1016. }
  1017. return len;
  1018. }
  1019. static int cmos_eval(int cmos_cmd)
  1020. {
  1021. if (cmos_handle)
  1022. return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
  1023. else
  1024. return 1;
  1025. }
  1026. static int cmos_write(char *buf)
  1027. {
  1028. char *cmd;
  1029. int cmos_cmd;
  1030. if (!cmos_handle)
  1031. return -EINVAL;
  1032. while ((cmd = next_cmd(&buf))) {
  1033. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1034. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1035. /* cmos_cmd set */
  1036. } else
  1037. return -EINVAL;
  1038. if (!cmos_eval(cmos_cmd))
  1039. return -EIO;
  1040. }
  1041. return 0;
  1042. }
  1043. enum led_access_mode {
  1044. IBMACPI_LED_NONE = 0,
  1045. IBMACPI_LED_570, /* 570 */
  1046. IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1047. IBMACPI_LED_NEW, /* all others */
  1048. };
  1049. static enum led_access_mode led_supported;
  1050. static int led_init(void)
  1051. {
  1052. if (!led_handle)
  1053. /* led not supported on R30, R31 */
  1054. led_supported = IBMACPI_LED_NONE;
  1055. else if (strlencmp(led_path, "SLED") == 0)
  1056. /* 570 */
  1057. led_supported = IBMACPI_LED_570;
  1058. else if (strlencmp(led_path, "SYSL") == 0)
  1059. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1060. led_supported = IBMACPI_LED_OLD;
  1061. else
  1062. /* all others */
  1063. led_supported = IBMACPI_LED_NEW;
  1064. return 0;
  1065. }
  1066. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1067. static int led_read(char *p)
  1068. {
  1069. int len = 0;
  1070. if (!led_supported) {
  1071. len += sprintf(p + len, "status:\t\tnot supported\n");
  1072. return len;
  1073. }
  1074. len += sprintf(p + len, "status:\t\tsupported\n");
  1075. if (led_supported == IBMACPI_LED_570) {
  1076. /* 570 */
  1077. int i, status;
  1078. for (i = 0; i < 8; i++) {
  1079. if (!acpi_evalf(ec_handle,
  1080. &status, "GLED", "dd", 1 << i))
  1081. return -EIO;
  1082. len += sprintf(p + len, "%d:\t\t%s\n",
  1083. i, led_status(status));
  1084. }
  1085. }
  1086. len += sprintf(p + len, "commands:\t"
  1087. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1088. return len;
  1089. }
  1090. /* off, on, blink */
  1091. static const int led_sled_arg1[] = { 0, 1, 3 };
  1092. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1093. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1094. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1095. #define EC_HLCL 0x0c
  1096. #define EC_HLBL 0x0d
  1097. #define EC_HLMS 0x0e
  1098. static int led_write(char *buf)
  1099. {
  1100. char *cmd;
  1101. int led, ind, ret;
  1102. if (!led_supported)
  1103. return -ENODEV;
  1104. while ((cmd = next_cmd(&buf))) {
  1105. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1106. return -EINVAL;
  1107. if (strstr(cmd, "off")) {
  1108. ind = 0;
  1109. } else if (strstr(cmd, "on")) {
  1110. ind = 1;
  1111. } else if (strstr(cmd, "blink")) {
  1112. ind = 2;
  1113. } else
  1114. return -EINVAL;
  1115. if (led_supported == IBMACPI_LED_570) {
  1116. /* 570 */
  1117. led = 1 << led;
  1118. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1119. led, led_sled_arg1[ind]))
  1120. return -EIO;
  1121. } else if (led_supported == IBMACPI_LED_OLD) {
  1122. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1123. led = 1 << led;
  1124. ret = ec_write(EC_HLMS, led);
  1125. if (ret >= 0)
  1126. ret =
  1127. ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
  1128. if (ret >= 0)
  1129. ret =
  1130. ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
  1131. if (ret < 0)
  1132. return ret;
  1133. } else {
  1134. /* all others */
  1135. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1136. led, led_led_arg1[ind]))
  1137. return -EIO;
  1138. }
  1139. }
  1140. return 0;
  1141. }
  1142. static int beep_read(char *p)
  1143. {
  1144. int len = 0;
  1145. if (!beep_handle)
  1146. len += sprintf(p + len, "status:\t\tnot supported\n");
  1147. else {
  1148. len += sprintf(p + len, "status:\t\tsupported\n");
  1149. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1150. }
  1151. return len;
  1152. }
  1153. static int beep_write(char *buf)
  1154. {
  1155. char *cmd;
  1156. int beep_cmd;
  1157. if (!beep_handle)
  1158. return -ENODEV;
  1159. while ((cmd = next_cmd(&buf))) {
  1160. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1161. beep_cmd >= 0 && beep_cmd <= 17) {
  1162. /* beep_cmd set */
  1163. } else
  1164. return -EINVAL;
  1165. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1166. return -EIO;
  1167. }
  1168. return 0;
  1169. }
  1170. static int acpi_ec_read(int i, u8 * p)
  1171. {
  1172. int v;
  1173. if (ecrd_handle) {
  1174. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  1175. return 0;
  1176. *p = v;
  1177. } else {
  1178. if (ec_read(i, p) < 0)
  1179. return 0;
  1180. }
  1181. return 1;
  1182. }
  1183. static int acpi_ec_write(int i, u8 v)
  1184. {
  1185. if (ecwr_handle) {
  1186. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  1187. return 0;
  1188. } else {
  1189. if (ec_write(i, v) < 0)
  1190. return 0;
  1191. }
  1192. return 1;
  1193. }
  1194. static enum thermal_access_mode thermal_read_mode;
  1195. static int thermal_init(void)
  1196. {
  1197. u8 t, ta1, ta2;
  1198. int i;
  1199. int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1200. if (ibm_thinkpad_ec_found && experimental) {
  1201. /*
  1202. * Direct EC access mode: sensors at registers
  1203. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1204. * non-implemented, thermal sensors return 0x80 when
  1205. * not available
  1206. */
  1207. ta1 = ta2 = 0;
  1208. for (i = 0; i < 8; i++) {
  1209. if (likely(acpi_ec_read(0x78 + i, &t))) {
  1210. ta1 |= t;
  1211. } else {
  1212. ta1 = 0;
  1213. break;
  1214. }
  1215. if (likely(acpi_ec_read(0xC0 + i, &t))) {
  1216. ta2 |= t;
  1217. } else {
  1218. ta1 = 0;
  1219. break;
  1220. }
  1221. }
  1222. if (ta1 == 0) {
  1223. /* This is sheer paranoia, but we handle it anyway */
  1224. if (acpi_tmp7) {
  1225. printk(IBM_ERR
  1226. "ThinkPad ACPI EC access misbehaving, "
  1227. "falling back to ACPI TMPx access mode\n");
  1228. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1229. } else {
  1230. printk(IBM_ERR
  1231. "ThinkPad ACPI EC access misbehaving, "
  1232. "disabling thermal sensors access\n");
  1233. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1234. }
  1235. } else {
  1236. thermal_read_mode =
  1237. (ta2 != 0) ?
  1238. IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
  1239. }
  1240. } else if (acpi_tmp7) {
  1241. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1242. /* 600e/x, 770e, 770x */
  1243. thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
  1244. } else {
  1245. /* Standard ACPI TMPx access, max 8 sensors */
  1246. thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
  1247. }
  1248. } else {
  1249. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1250. thermal_read_mode = IBMACPI_THERMAL_NONE;
  1251. }
  1252. return 0;
  1253. }
  1254. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1255. {
  1256. int i, t;
  1257. s8 tmp;
  1258. char tmpi[] = "TMPi";
  1259. if (!s)
  1260. return -EINVAL;
  1261. switch (thermal_read_mode) {
  1262. #if IBMACPI_MAX_THERMAL_SENSORS >= 16
  1263. case IBMACPI_THERMAL_TPEC_16:
  1264. for (i = 0; i < 8; i++) {
  1265. if (!acpi_ec_read(0xC0 + i, &tmp))
  1266. return -EIO;
  1267. s->temp[i + 8] = tmp * 1000;
  1268. }
  1269. /* fallthrough */
  1270. #endif
  1271. case IBMACPI_THERMAL_TPEC_8:
  1272. for (i = 0; i < 8; i++) {
  1273. if (!acpi_ec_read(0x78 + i, &tmp))
  1274. return -EIO;
  1275. s->temp[i] = tmp * 1000;
  1276. }
  1277. return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
  1278. case IBMACPI_THERMAL_ACPI_UPDT:
  1279. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1280. return -EIO;
  1281. for (i = 0; i < 8; i++) {
  1282. tmpi[3] = '0' + i;
  1283. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1284. return -EIO;
  1285. s->temp[i] = (t - 2732) * 100;
  1286. }
  1287. return 8;
  1288. case IBMACPI_THERMAL_ACPI_TMP07:
  1289. for (i = 0; i < 8; i++) {
  1290. tmpi[3] = '0' + i;
  1291. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1292. return -EIO;
  1293. s->temp[i] = t * 1000;
  1294. }
  1295. return 8;
  1296. case IBMACPI_THERMAL_NONE:
  1297. default:
  1298. return 0;
  1299. }
  1300. }
  1301. static int thermal_read(char *p)
  1302. {
  1303. int len = 0;
  1304. int n, i;
  1305. struct ibm_thermal_sensors_struct t;
  1306. n = thermal_get_sensors(&t);
  1307. if (unlikely(n < 0))
  1308. return n;
  1309. len += sprintf(p + len, "temperatures:\t");
  1310. if (n > 0) {
  1311. for (i = 0; i < (n - 1); i++)
  1312. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1313. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1314. } else
  1315. len += sprintf(p + len, "not supported\n");
  1316. return len;
  1317. }
  1318. static u8 ecdump_regs[256];
  1319. static int ecdump_read(char *p)
  1320. {
  1321. int len = 0;
  1322. int i, j;
  1323. u8 v;
  1324. len += sprintf(p + len, "EC "
  1325. " +00 +01 +02 +03 +04 +05 +06 +07"
  1326. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1327. for (i = 0; i < 256; i += 16) {
  1328. len += sprintf(p + len, "EC 0x%02x:", i);
  1329. for (j = 0; j < 16; j++) {
  1330. if (!acpi_ec_read(i + j, &v))
  1331. break;
  1332. if (v != ecdump_regs[i + j])
  1333. len += sprintf(p + len, " *%02x", v);
  1334. else
  1335. len += sprintf(p + len, " %02x", v);
  1336. ecdump_regs[i + j] = v;
  1337. }
  1338. len += sprintf(p + len, "\n");
  1339. if (j != 16)
  1340. break;
  1341. }
  1342. /* These are way too dangerous to advertise openly... */
  1343. #if 0
  1344. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1345. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1346. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1347. " (<offset> is 00-ff, <value> is 0-255)\n");
  1348. #endif
  1349. return len;
  1350. }
  1351. static int ecdump_write(char *buf)
  1352. {
  1353. char *cmd;
  1354. int i, v;
  1355. while ((cmd = next_cmd(&buf))) {
  1356. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1357. /* i and v set */
  1358. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1359. /* i and v set */
  1360. } else
  1361. return -EINVAL;
  1362. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1363. if (!acpi_ec_write(i, v))
  1364. return -EIO;
  1365. } else
  1366. return -EINVAL;
  1367. }
  1368. return 0;
  1369. }
  1370. static int brightness_offset = 0x31;
  1371. static int brightness_get(struct backlight_device *bd)
  1372. {
  1373. u8 level;
  1374. if (!acpi_ec_read(brightness_offset, &level))
  1375. return -EIO;
  1376. level &= 0x7;
  1377. return level;
  1378. }
  1379. static int brightness_read(char *p)
  1380. {
  1381. int len = 0;
  1382. int level;
  1383. if ((level = brightness_get(NULL)) < 0) {
  1384. len += sprintf(p + len, "level:\t\tunreadable\n");
  1385. } else {
  1386. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1387. len += sprintf(p + len, "commands:\tup, down\n");
  1388. len += sprintf(p + len, "commands:\tlevel <level>"
  1389. " (<level> is 0-7)\n");
  1390. }
  1391. return len;
  1392. }
  1393. #define BRIGHTNESS_UP 4
  1394. #define BRIGHTNESS_DOWN 5
  1395. static int brightness_set(int value)
  1396. {
  1397. int cmos_cmd, inc, i;
  1398. int current_value = brightness_get(NULL);
  1399. value &= 7;
  1400. cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
  1401. inc = value > current_value ? 1 : -1;
  1402. for (i = current_value; i != value; i += inc) {
  1403. if (!cmos_eval(cmos_cmd))
  1404. return -EIO;
  1405. if (!acpi_ec_write(brightness_offset, i + inc))
  1406. return -EIO;
  1407. }
  1408. return 0;
  1409. }
  1410. static int brightness_write(char *buf)
  1411. {
  1412. int level;
  1413. int new_level;
  1414. char *cmd;
  1415. while ((cmd = next_cmd(&buf))) {
  1416. if ((level = brightness_get(NULL)) < 0)
  1417. return level;
  1418. level &= 7;
  1419. if (strlencmp(cmd, "up") == 0) {
  1420. new_level = level == 7 ? 7 : level + 1;
  1421. } else if (strlencmp(cmd, "down") == 0) {
  1422. new_level = level == 0 ? 0 : level - 1;
  1423. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1424. new_level >= 0 && new_level <= 7) {
  1425. /* new_level set */
  1426. } else
  1427. return -EINVAL;
  1428. brightness_set(new_level);
  1429. }
  1430. return 0;
  1431. }
  1432. static int brightness_update_status(struct backlight_device *bd)
  1433. {
  1434. return brightness_set(
  1435. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  1436. bd->props.power == FB_BLANK_UNBLANK) ?
  1437. bd->props.brightness : 0);
  1438. }
  1439. static struct backlight_ops ibm_backlight_data = {
  1440. .get_brightness = brightness_get,
  1441. .update_status = brightness_update_status,
  1442. };
  1443. static int brightness_init(void)
  1444. {
  1445. int b;
  1446. b = brightness_get(NULL);
  1447. if (b < 0)
  1448. return b;
  1449. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1450. &ibm_backlight_data);
  1451. if (IS_ERR(ibm_backlight_device)) {
  1452. printk(IBM_ERR "Could not register backlight device\n");
  1453. return PTR_ERR(ibm_backlight_device);
  1454. }
  1455. ibm_backlight_device->props.max_brightness = 7;
  1456. ibm_backlight_device->props.brightness = b;
  1457. backlight_update_status(ibm_backlight_device);
  1458. return 0;
  1459. }
  1460. static void brightness_exit(void)
  1461. {
  1462. if (ibm_backlight_device) {
  1463. backlight_device_unregister(ibm_backlight_device);
  1464. ibm_backlight_device = NULL;
  1465. }
  1466. }
  1467. static int volume_offset = 0x30;
  1468. static int volume_read(char *p)
  1469. {
  1470. int len = 0;
  1471. u8 level;
  1472. if (!acpi_ec_read(volume_offset, &level)) {
  1473. len += sprintf(p + len, "level:\t\tunreadable\n");
  1474. } else {
  1475. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1476. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1477. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1478. len += sprintf(p + len, "commands:\tlevel <level>"
  1479. " (<level> is 0-15)\n");
  1480. }
  1481. return len;
  1482. }
  1483. #define VOLUME_DOWN 0
  1484. #define VOLUME_UP 1
  1485. #define VOLUME_MUTE 2
  1486. static int volume_write(char *buf)
  1487. {
  1488. int cmos_cmd, inc, i;
  1489. u8 level, mute;
  1490. int new_level, new_mute;
  1491. char *cmd;
  1492. while ((cmd = next_cmd(&buf))) {
  1493. if (!acpi_ec_read(volume_offset, &level))
  1494. return -EIO;
  1495. new_mute = mute = level & 0x40;
  1496. new_level = level = level & 0xf;
  1497. if (strlencmp(cmd, "up") == 0) {
  1498. if (mute)
  1499. new_mute = 0;
  1500. else
  1501. new_level = level == 15 ? 15 : level + 1;
  1502. } else if (strlencmp(cmd, "down") == 0) {
  1503. if (mute)
  1504. new_mute = 0;
  1505. else
  1506. new_level = level == 0 ? 0 : level - 1;
  1507. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1508. new_level >= 0 && new_level <= 15) {
  1509. /* new_level set */
  1510. } else if (strlencmp(cmd, "mute") == 0) {
  1511. new_mute = 0x40;
  1512. } else
  1513. return -EINVAL;
  1514. if (new_level != level) { /* mute doesn't change */
  1515. cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
  1516. inc = new_level > level ? 1 : -1;
  1517. if (mute && (!cmos_eval(cmos_cmd) ||
  1518. !acpi_ec_write(volume_offset, level)))
  1519. return -EIO;
  1520. for (i = level; i != new_level; i += inc)
  1521. if (!cmos_eval(cmos_cmd) ||
  1522. !acpi_ec_write(volume_offset, i + inc))
  1523. return -EIO;
  1524. if (mute && (!cmos_eval(VOLUME_MUTE) ||
  1525. !acpi_ec_write(volume_offset,
  1526. new_level + mute)))
  1527. return -EIO;
  1528. }
  1529. if (new_mute != mute) { /* level doesn't change */
  1530. cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
  1531. if (!cmos_eval(cmos_cmd) ||
  1532. !acpi_ec_write(volume_offset, level + new_mute))
  1533. return -EIO;
  1534. }
  1535. }
  1536. return 0;
  1537. }
  1538. static enum fan_status_access_mode fan_status_access_mode;
  1539. static enum fan_control_access_mode fan_control_access_mode;
  1540. static enum fan_control_commands fan_control_commands;
  1541. static int fan_control_status_known;
  1542. static u8 fan_control_initial_status;
  1543. static void fan_watchdog_fire(struct work_struct *ignored);
  1544. static int fan_watchdog_maxinterval;
  1545. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  1546. static int fan_init(void)
  1547. {
  1548. fan_status_access_mode = IBMACPI_FAN_NONE;
  1549. fan_control_access_mode = IBMACPI_FAN_WR_NONE;
  1550. fan_control_commands = 0;
  1551. fan_control_status_known = 1;
  1552. fan_watchdog_maxinterval = 0;
  1553. if (gfan_handle) {
  1554. /* 570, 600e/x, 770e, 770x */
  1555. fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
  1556. } else {
  1557. /* all other ThinkPads: note that even old-style
  1558. * ThinkPad ECs supports the fan control register */
  1559. if (likely(acpi_ec_read(fan_status_offset,
  1560. &fan_control_initial_status))) {
  1561. fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
  1562. /* In some ThinkPads, neither the EC nor the ACPI
  1563. * DSDT initialize the fan status, and it ends up
  1564. * being set to 0x07 when it *could* be either
  1565. * 0x07 or 0x80.
  1566. *
  1567. * Enable for TP-1Y (T43), TP-78 (R51e),
  1568. * TP-76 (R52), TP-70 (T43, R52), which are known
  1569. * to be buggy. */
  1570. if (fan_control_initial_status == 0x07 &&
  1571. ibm_thinkpad_ec_found &&
  1572. ((ibm_thinkpad_ec_found[0] == '1' &&
  1573. ibm_thinkpad_ec_found[1] == 'Y') ||
  1574. (ibm_thinkpad_ec_found[0] == '7' &&
  1575. (ibm_thinkpad_ec_found[1] == '6' ||
  1576. ibm_thinkpad_ec_found[1] == '8' ||
  1577. ibm_thinkpad_ec_found[1] == '0'))
  1578. )) {
  1579. printk(IBM_NOTICE
  1580. "fan_init: initial fan status is "
  1581. "unknown, assuming it is in auto "
  1582. "mode\n");
  1583. fan_control_status_known = 0;
  1584. }
  1585. } else {
  1586. printk(IBM_ERR
  1587. "ThinkPad ACPI EC access misbehaving, "
  1588. "fan status and control unavailable\n");
  1589. return 0;
  1590. }
  1591. }
  1592. if (sfan_handle) {
  1593. /* 570, 770x-JL */
  1594. fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
  1595. fan_control_commands |=
  1596. IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
  1597. } else {
  1598. if (!gfan_handle) {
  1599. /* gfan without sfan means no fan control */
  1600. /* all other models implement TP EC 0x2f control */
  1601. if (fans_handle) {
  1602. /* X31, X40, X41 */
  1603. fan_control_access_mode =
  1604. IBMACPI_FAN_WR_ACPI_FANS;
  1605. fan_control_commands |=
  1606. IBMACPI_FAN_CMD_SPEED |
  1607. IBMACPI_FAN_CMD_LEVEL |
  1608. IBMACPI_FAN_CMD_ENABLE;
  1609. } else {
  1610. fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
  1611. fan_control_commands |=
  1612. IBMACPI_FAN_CMD_LEVEL |
  1613. IBMACPI_FAN_CMD_ENABLE;
  1614. }
  1615. }
  1616. }
  1617. return 0;
  1618. }
  1619. static int fan_get_status(u8 *status)
  1620. {
  1621. u8 s;
  1622. /* TODO:
  1623. * Add IBMACPI_FAN_RD_ACPI_FANS ? */
  1624. switch (fan_status_access_mode) {
  1625. case IBMACPI_FAN_RD_ACPI_GFAN:
  1626. /* 570, 600e/x, 770e, 770x */
  1627. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  1628. return -EIO;
  1629. if (likely(status))
  1630. *status = s & 0x07;
  1631. break;
  1632. case IBMACPI_FAN_RD_TPEC:
  1633. /* all except 570, 600e/x, 770e, 770x */
  1634. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  1635. return -EIO;
  1636. if (likely(status))
  1637. *status = s;
  1638. break;
  1639. default:
  1640. return -ENXIO;
  1641. }
  1642. return 0;
  1643. }
  1644. static int fan_get_speed(unsigned int *speed)
  1645. {
  1646. u8 hi, lo;
  1647. switch (fan_status_access_mode) {
  1648. case IBMACPI_FAN_RD_TPEC:
  1649. /* all except 570, 600e/x, 770e, 770x */
  1650. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  1651. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  1652. return -EIO;
  1653. if (likely(speed))
  1654. *speed = (hi << 8) | lo;
  1655. break;
  1656. default:
  1657. return -ENXIO;
  1658. }
  1659. return 0;
  1660. }
  1661. static void fan_exit(void)
  1662. {
  1663. cancel_delayed_work(&fan_watchdog_task);
  1664. flush_scheduled_work();
  1665. }
  1666. static void fan_watchdog_reset(void)
  1667. {
  1668. static int fan_watchdog_active = 0;
  1669. if (fan_watchdog_active)
  1670. cancel_delayed_work(&fan_watchdog_task);
  1671. if (fan_watchdog_maxinterval > 0) {
  1672. fan_watchdog_active = 1;
  1673. if (!schedule_delayed_work(&fan_watchdog_task,
  1674. msecs_to_jiffies(fan_watchdog_maxinterval
  1675. * 1000))) {
  1676. printk(IBM_ERR "failed to schedule the fan watchdog, "
  1677. "watchdog will not trigger\n");
  1678. }
  1679. } else
  1680. fan_watchdog_active = 0;
  1681. }
  1682. static int fan_read(char *p)
  1683. {
  1684. int len = 0;
  1685. int rc;
  1686. u8 status;
  1687. unsigned int speed = 0;
  1688. switch (fan_status_access_mode) {
  1689. case IBMACPI_FAN_RD_ACPI_GFAN:
  1690. /* 570, 600e/x, 770e, 770x */
  1691. if ((rc = fan_get_status(&status)) < 0)
  1692. return rc;
  1693. len += sprintf(p + len, "status:\t\t%s\n"
  1694. "level:\t\t%d\n",
  1695. (status != 0) ? "enabled" : "disabled", status);
  1696. break;
  1697. case IBMACPI_FAN_RD_TPEC:
  1698. /* all except 570, 600e/x, 770e, 770x */
  1699. if ((rc = fan_get_status(&status)) < 0)
  1700. return rc;
  1701. if (unlikely(!fan_control_status_known)) {
  1702. if (status != fan_control_initial_status)
  1703. fan_control_status_known = 1;
  1704. else
  1705. /* Return most likely status. In fact, it
  1706. * might be the only possible status */
  1707. status = IBMACPI_FAN_EC_AUTO;
  1708. }
  1709. len += sprintf(p + len, "status:\t\t%s\n",
  1710. (status != 0) ? "enabled" : "disabled");
  1711. /* No ThinkPad boots on disengaged mode, we can safely
  1712. * assume the tachometer is online if fan control status
  1713. * was unknown */
  1714. if ((rc = fan_get_speed(&speed)) < 0)
  1715. return rc;
  1716. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  1717. if (status & IBMACPI_FAN_EC_DISENGAGED)
  1718. /* Disengaged mode takes precedence */
  1719. len += sprintf(p + len, "level:\t\tdisengaged\n");
  1720. else if (status & IBMACPI_FAN_EC_AUTO)
  1721. len += sprintf(p + len, "level:\t\tauto\n");
  1722. else
  1723. len += sprintf(p + len, "level:\t\t%d\n", status);
  1724. break;
  1725. case IBMACPI_FAN_NONE:
  1726. default:
  1727. len += sprintf(p + len, "status:\t\tnot supported\n");
  1728. }
  1729. if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
  1730. len += sprintf(p + len, "commands:\tlevel <level>");
  1731. switch (fan_control_access_mode) {
  1732. case IBMACPI_FAN_WR_ACPI_SFAN:
  1733. len += sprintf(p + len, " (<level> is 0-7)\n");
  1734. break;
  1735. default:
  1736. len += sprintf(p + len, " (<level> is 0-7, "
  1737. "auto, disengaged)\n");
  1738. break;
  1739. }
  1740. }
  1741. if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
  1742. len += sprintf(p + len, "commands:\tenable, disable\n"
  1743. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  1744. "1-120 (seconds))\n");
  1745. if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
  1746. len += sprintf(p + len, "commands:\tspeed <speed>"
  1747. " (<speed> is 0-65535)\n");
  1748. return len;
  1749. }
  1750. static int fan_set_level(int level)
  1751. {
  1752. switch (fan_control_access_mode) {
  1753. case IBMACPI_FAN_WR_ACPI_SFAN:
  1754. if (level >= 0 && level <= 7) {
  1755. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  1756. return -EIO;
  1757. } else
  1758. return -EINVAL;
  1759. break;
  1760. case IBMACPI_FAN_WR_ACPI_FANS:
  1761. case IBMACPI_FAN_WR_TPEC:
  1762. if ((level != IBMACPI_FAN_EC_AUTO) &&
  1763. (level != IBMACPI_FAN_EC_DISENGAGED) &&
  1764. ((level < 0) || (level > 7)))
  1765. return -EINVAL;
  1766. if (!acpi_ec_write(fan_status_offset, level))
  1767. return -EIO;
  1768. else
  1769. fan_control_status_known = 1;
  1770. break;
  1771. default:
  1772. return -ENXIO;
  1773. }
  1774. return 0;
  1775. }
  1776. static int fan_set_enable(void)
  1777. {
  1778. u8 s;
  1779. int rc;
  1780. switch (fan_control_access_mode) {
  1781. case IBMACPI_FAN_WR_ACPI_FANS:
  1782. case IBMACPI_FAN_WR_TPEC:
  1783. if ((rc = fan_get_status(&s)) < 0)
  1784. return rc;
  1785. /* Don't go out of emergency fan mode */
  1786. if (s != 7)
  1787. s = IBMACPI_FAN_EC_AUTO;
  1788. if (!acpi_ec_write(fan_status_offset, s))
  1789. return -EIO;
  1790. else
  1791. fan_control_status_known = 1;
  1792. break;
  1793. case IBMACPI_FAN_WR_ACPI_SFAN:
  1794. if ((rc = fan_get_status(&s)) < 0)
  1795. return rc;
  1796. s &= 0x07;
  1797. /* Set fan to at least level 4 */
  1798. if (s < 4)
  1799. s = 4;
  1800. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  1801. return -EIO;
  1802. break;
  1803. default:
  1804. return -ENXIO;
  1805. }
  1806. return 0;
  1807. }
  1808. static int fan_set_disable(void)
  1809. {
  1810. switch (fan_control_access_mode) {
  1811. case IBMACPI_FAN_WR_ACPI_FANS:
  1812. case IBMACPI_FAN_WR_TPEC:
  1813. if (!acpi_ec_write(fan_status_offset, 0x00))
  1814. return -EIO;
  1815. else
  1816. fan_control_status_known = 1;
  1817. break;
  1818. case IBMACPI_FAN_WR_ACPI_SFAN:
  1819. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  1820. return -EIO;
  1821. break;
  1822. default:
  1823. return -ENXIO;
  1824. }
  1825. return 0;
  1826. }
  1827. static int fan_set_speed(int speed)
  1828. {
  1829. switch (fan_control_access_mode) {
  1830. case IBMACPI_FAN_WR_ACPI_FANS:
  1831. if (speed >= 0 && speed <= 65535) {
  1832. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  1833. speed, speed, speed))
  1834. return -EIO;
  1835. } else
  1836. return -EINVAL;
  1837. break;
  1838. default:
  1839. return -ENXIO;
  1840. }
  1841. return 0;
  1842. }
  1843. static int fan_write_cmd_level(const char *cmd, int *rc)
  1844. {
  1845. int level;
  1846. if (strlencmp(cmd, "level auto") == 0)
  1847. level = IBMACPI_FAN_EC_AUTO;
  1848. else if (strlencmp(cmd, "level disengaged") == 0)
  1849. level = IBMACPI_FAN_EC_DISENGAGED;
  1850. else if (sscanf(cmd, "level %d", &level) != 1)
  1851. return 0;
  1852. if ((*rc = fan_set_level(level)) == -ENXIO)
  1853. printk(IBM_ERR "level command accepted for unsupported "
  1854. "access mode %d", fan_control_access_mode);
  1855. return 1;
  1856. }
  1857. static int fan_write_cmd_enable(const char *cmd, int *rc)
  1858. {
  1859. if (strlencmp(cmd, "enable") != 0)
  1860. return 0;
  1861. if ((*rc = fan_set_enable()) == -ENXIO)
  1862. printk(IBM_ERR "enable command accepted for unsupported "
  1863. "access mode %d", fan_control_access_mode);
  1864. return 1;
  1865. }
  1866. static int fan_write_cmd_disable(const char *cmd, int *rc)
  1867. {
  1868. if (strlencmp(cmd, "disable") != 0)
  1869. return 0;
  1870. if ((*rc = fan_set_disable()) == -ENXIO)
  1871. printk(IBM_ERR "disable command accepted for unsupported "
  1872. "access mode %d", fan_control_access_mode);
  1873. return 1;
  1874. }
  1875. static int fan_write_cmd_speed(const char *cmd, int *rc)
  1876. {
  1877. int speed;
  1878. /* TODO:
  1879. * Support speed <low> <medium> <high> ? */
  1880. if (sscanf(cmd, "speed %d", &speed) != 1)
  1881. return 0;
  1882. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  1883. printk(IBM_ERR "speed command accepted for unsupported "
  1884. "access mode %d", fan_control_access_mode);
  1885. return 1;
  1886. }
  1887. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  1888. {
  1889. int interval;
  1890. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  1891. return 0;
  1892. if (interval < 0 || interval > 120)
  1893. *rc = -EINVAL;
  1894. else
  1895. fan_watchdog_maxinterval = interval;
  1896. return 1;
  1897. }
  1898. static int fan_write(char *buf)
  1899. {
  1900. char *cmd;
  1901. int rc = 0;
  1902. while (!rc && (cmd = next_cmd(&buf))) {
  1903. if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
  1904. fan_write_cmd_level(cmd, &rc)) &&
  1905. !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
  1906. (fan_write_cmd_enable(cmd, &rc) ||
  1907. fan_write_cmd_disable(cmd, &rc) ||
  1908. fan_write_cmd_watchdog(cmd, &rc))) &&
  1909. !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
  1910. fan_write_cmd_speed(cmd, &rc))
  1911. )
  1912. rc = -EINVAL;
  1913. else if (!rc)
  1914. fan_watchdog_reset();
  1915. }
  1916. return rc;
  1917. }
  1918. static void fan_watchdog_fire(struct work_struct *ignored)
  1919. {
  1920. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  1921. if (fan_set_enable()) {
  1922. printk(IBM_ERR "fan watchdog: error while enabling fan\n");
  1923. /* reschedule for later */
  1924. fan_watchdog_reset();
  1925. }
  1926. }
  1927. static struct ibm_struct ibms[] = {
  1928. {
  1929. .name = "driver",
  1930. .init = ibm_acpi_driver_init,
  1931. .read = driver_read,
  1932. },
  1933. {
  1934. .name = "hotkey",
  1935. .hid = IBM_HKEY_HID,
  1936. .init = hotkey_init,
  1937. .read = hotkey_read,
  1938. .write = hotkey_write,
  1939. .exit = hotkey_exit,
  1940. .notify = hotkey_notify,
  1941. .handle = &hkey_handle,
  1942. .type = ACPI_DEVICE_NOTIFY,
  1943. },
  1944. {
  1945. .name = "bluetooth",
  1946. .init = bluetooth_init,
  1947. .read = bluetooth_read,
  1948. .write = bluetooth_write,
  1949. },
  1950. {
  1951. .name = "wan",
  1952. .init = wan_init,
  1953. .read = wan_read,
  1954. .write = wan_write,
  1955. .experimental = 1,
  1956. },
  1957. {
  1958. .name = "video",
  1959. .init = video_init,
  1960. .read = video_read,
  1961. .write = video_write,
  1962. .exit = video_exit,
  1963. },
  1964. {
  1965. .name = "light",
  1966. .init = light_init,
  1967. .read = light_read,
  1968. .write = light_write,
  1969. },
  1970. #ifdef CONFIG_ACPI_IBM_DOCK
  1971. {
  1972. .name = "dock",
  1973. .read = dock_read,
  1974. .write = dock_write,
  1975. .notify = dock_notify,
  1976. .handle = &dock_handle,
  1977. .type = ACPI_SYSTEM_NOTIFY,
  1978. },
  1979. {
  1980. .name = "dock",
  1981. .hid = IBM_PCI_HID,
  1982. .notify = dock_notify,
  1983. .handle = &pci_handle,
  1984. .type = ACPI_SYSTEM_NOTIFY,
  1985. },
  1986. #endif
  1987. #ifdef CONFIG_ACPI_IBM_BAY
  1988. {
  1989. .name = "bay",
  1990. .init = bay_init,
  1991. .read = bay_read,
  1992. .write = bay_write,
  1993. .notify = bay_notify,
  1994. .handle = &bay_handle,
  1995. .type = ACPI_SYSTEM_NOTIFY,
  1996. },
  1997. #endif /* CONFIG_ACPI_IBM_BAY */
  1998. {
  1999. .name = "cmos",
  2000. .read = cmos_read,
  2001. .write = cmos_write,
  2002. },
  2003. {
  2004. .name = "led",
  2005. .init = led_init,
  2006. .read = led_read,
  2007. .write = led_write,
  2008. },
  2009. {
  2010. .name = "beep",
  2011. .read = beep_read,
  2012. .write = beep_write,
  2013. },
  2014. {
  2015. .name = "thermal",
  2016. .init = thermal_init,
  2017. .read = thermal_read,
  2018. },
  2019. {
  2020. .name = "ecdump",
  2021. .read = ecdump_read,
  2022. .write = ecdump_write,
  2023. .experimental = 1,
  2024. },
  2025. {
  2026. .name = "brightness",
  2027. .read = brightness_read,
  2028. .write = brightness_write,
  2029. .init = brightness_init,
  2030. .exit = brightness_exit,
  2031. },
  2032. {
  2033. .name = "volume",
  2034. .read = volume_read,
  2035. .write = volume_write,
  2036. },
  2037. {
  2038. .name = "fan",
  2039. .read = fan_read,
  2040. .write = fan_write,
  2041. .init = fan_init,
  2042. .exit = fan_exit,
  2043. .experimental = 1,
  2044. },
  2045. };
  2046. static int dispatch_read(char *page, char **start, off_t off, int count,
  2047. int *eof, void *data)
  2048. {
  2049. struct ibm_struct *ibm = data;
  2050. int len;
  2051. if (!ibm || !ibm->read)
  2052. return -EINVAL;
  2053. len = ibm->read(page);
  2054. if (len < 0)
  2055. return len;
  2056. if (len <= off + count)
  2057. *eof = 1;
  2058. *start = page + off;
  2059. len -= off;
  2060. if (len > count)
  2061. len = count;
  2062. if (len < 0)
  2063. len = 0;
  2064. return len;
  2065. }
  2066. static int dispatch_write(struct file *file, const char __user * userbuf,
  2067. unsigned long count, void *data)
  2068. {
  2069. struct ibm_struct *ibm = data;
  2070. char *kernbuf;
  2071. int ret;
  2072. if (!ibm || !ibm->write)
  2073. return -EINVAL;
  2074. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  2075. if (!kernbuf)
  2076. return -ENOMEM;
  2077. if (copy_from_user(kernbuf, userbuf, count)) {
  2078. kfree(kernbuf);
  2079. return -EFAULT;
  2080. }
  2081. kernbuf[count] = 0;
  2082. strcat(kernbuf, ",");
  2083. ret = ibm->write(kernbuf);
  2084. if (ret == 0)
  2085. ret = count;
  2086. kfree(kernbuf);
  2087. return ret;
  2088. }
  2089. static void dispatch_notify(acpi_handle handle, u32 event, void *data)
  2090. {
  2091. struct ibm_struct *ibm = data;
  2092. if (!ibm || !ibm->notify)
  2093. return;
  2094. ibm->notify(ibm, event);
  2095. }
  2096. static int __init setup_notify(struct ibm_struct *ibm)
  2097. {
  2098. acpi_status status;
  2099. int ret;
  2100. if (!*ibm->handle)
  2101. return 0;
  2102. ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
  2103. if (ret < 0) {
  2104. printk(IBM_ERR "%s device not present\n", ibm->name);
  2105. return 0;
  2106. }
  2107. acpi_driver_data(ibm->device) = ibm;
  2108. sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
  2109. status = acpi_install_notify_handler(*ibm->handle, ibm->type,
  2110. dispatch_notify, ibm);
  2111. if (ACPI_FAILURE(status)) {
  2112. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  2113. ibm->name, status);
  2114. return -ENODEV;
  2115. }
  2116. ibm->notify_installed = 1;
  2117. return 0;
  2118. }
  2119. static int __init ibm_device_add(struct acpi_device *device)
  2120. {
  2121. return 0;
  2122. }
  2123. static int __init register_driver(struct ibm_struct *ibm)
  2124. {
  2125. int ret;
  2126. ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  2127. if (!ibm->driver) {
  2128. printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
  2129. return -1;
  2130. }
  2131. sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
  2132. ibm->driver->ids = ibm->hid;
  2133. ibm->driver->ops.add = &ibm_device_add;
  2134. ret = acpi_bus_register_driver(ibm->driver);
  2135. if (ret < 0) {
  2136. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  2137. ibm->hid, ret);
  2138. kfree(ibm->driver);
  2139. }
  2140. return ret;
  2141. }
  2142. static int __init ibm_init(struct ibm_struct *ibm)
  2143. {
  2144. int ret;
  2145. struct proc_dir_entry *entry;
  2146. if (ibm->experimental && !experimental)
  2147. return 0;
  2148. if (ibm->hid) {
  2149. ret = register_driver(ibm);
  2150. if (ret < 0)
  2151. return ret;
  2152. ibm->driver_registered = 1;
  2153. }
  2154. if (ibm->init) {
  2155. ret = ibm->init();
  2156. if (ret != 0)
  2157. return ret;
  2158. ibm->init_called = 1;
  2159. }
  2160. if (ibm->read) {
  2161. entry = create_proc_entry(ibm->name,
  2162. S_IFREG | S_IRUGO | S_IWUSR,
  2163. proc_dir);
  2164. if (!entry) {
  2165. printk(IBM_ERR "unable to create proc entry %s\n",
  2166. ibm->name);
  2167. return -ENODEV;
  2168. }
  2169. entry->owner = THIS_MODULE;
  2170. entry->data = ibm;
  2171. entry->read_proc = &dispatch_read;
  2172. if (ibm->write)
  2173. entry->write_proc = &dispatch_write;
  2174. ibm->proc_created = 1;
  2175. }
  2176. if (ibm->notify) {
  2177. ret = setup_notify(ibm);
  2178. if (ret < 0)
  2179. return ret;
  2180. }
  2181. return 0;
  2182. }
  2183. static void ibm_exit(struct ibm_struct *ibm)
  2184. {
  2185. if (ibm->notify_installed)
  2186. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  2187. dispatch_notify);
  2188. if (ibm->proc_created)
  2189. remove_proc_entry(ibm->name, proc_dir);
  2190. if (ibm->init_called && ibm->exit)
  2191. ibm->exit();
  2192. if (ibm->driver_registered) {
  2193. acpi_bus_unregister_driver(ibm->driver);
  2194. kfree(ibm->driver);
  2195. }
  2196. }
  2197. static void __init ibm_handle_init(char *name,
  2198. acpi_handle * handle, acpi_handle parent,
  2199. char **paths, int num_paths, char **path)
  2200. {
  2201. int i;
  2202. acpi_status status;
  2203. for (i = 0; i < num_paths; i++) {
  2204. status = acpi_get_handle(parent, paths[i], handle);
  2205. if (ACPI_SUCCESS(status)) {
  2206. *path = paths[i];
  2207. return;
  2208. }
  2209. }
  2210. *handle = NULL;
  2211. }
  2212. #define IBM_HANDLE_INIT(object) \
  2213. ibm_handle_init(#object, &object##_handle, *object##_parent, \
  2214. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  2215. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  2216. {
  2217. unsigned int i;
  2218. for (i = 0; i < ARRAY_SIZE(ibms); i++)
  2219. if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
  2220. if (strlen(val) > sizeof(ibms[i].param) - 2)
  2221. return -ENOSPC;
  2222. strcpy(ibms[i].param, val);
  2223. strcat(ibms[i].param, ",");
  2224. return 0;
  2225. }
  2226. return -EINVAL;
  2227. }
  2228. #define IBM_PARAM(feature) \
  2229. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  2230. IBM_PARAM(hotkey);
  2231. IBM_PARAM(bluetooth);
  2232. IBM_PARAM(video);
  2233. IBM_PARAM(light);
  2234. #ifdef CONFIG_ACPI_IBM_DOCK
  2235. IBM_PARAM(dock);
  2236. #endif
  2237. #ifdef CONFIG_ACPI_IBM_BAY
  2238. IBM_PARAM(bay);
  2239. #endif /* CONFIG_ACPI_IBM_BAY */
  2240. IBM_PARAM(cmos);
  2241. IBM_PARAM(led);
  2242. IBM_PARAM(beep);
  2243. IBM_PARAM(ecdump);
  2244. IBM_PARAM(brightness);
  2245. IBM_PARAM(volume);
  2246. IBM_PARAM(fan);
  2247. static void acpi_ibm_exit(void)
  2248. {
  2249. int i;
  2250. for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
  2251. ibm_exit(&ibms[i]);
  2252. if (proc_dir)
  2253. remove_proc_entry(IBM_DIR, acpi_root_dir);
  2254. if (ibm_thinkpad_ec_found)
  2255. kfree(ibm_thinkpad_ec_found);
  2256. }
  2257. static char* __init check_dmi_for_ec(void)
  2258. {
  2259. struct dmi_device *dev = NULL;
  2260. char ec_fw_string[18];
  2261. /*
  2262. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  2263. * X32 or newer, all Z series; Some models must have an
  2264. * up-to-date BIOS or they will not be detected.
  2265. *
  2266. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  2267. */
  2268. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  2269. if (sscanf(dev->name,
  2270. "IBM ThinkPad Embedded Controller -[%17c",
  2271. ec_fw_string) == 1) {
  2272. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  2273. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  2274. return kstrdup(ec_fw_string, GFP_KERNEL);
  2275. }
  2276. }
  2277. return NULL;
  2278. }
  2279. static int __init acpi_ibm_init(void)
  2280. {
  2281. int ret, i;
  2282. if (acpi_disabled)
  2283. return -ENODEV;
  2284. /* ec is required because many other handles are relative to it */
  2285. IBM_HANDLE_INIT(ec);
  2286. if (!ec_handle) {
  2287. printk(IBM_ERR "ec object not found\n");
  2288. return -ENODEV;
  2289. }
  2290. /* Models with newer firmware report the EC in DMI */
  2291. ibm_thinkpad_ec_found = check_dmi_for_ec();
  2292. /* these handles are not required */
  2293. IBM_HANDLE_INIT(vid);
  2294. IBM_HANDLE_INIT(vid2);
  2295. IBM_HANDLE_INIT(ledb);
  2296. IBM_HANDLE_INIT(led);
  2297. IBM_HANDLE_INIT(hkey);
  2298. IBM_HANDLE_INIT(lght);
  2299. IBM_HANDLE_INIT(cmos);
  2300. #ifdef CONFIG_ACPI_IBM_DOCK
  2301. IBM_HANDLE_INIT(dock);
  2302. #endif
  2303. IBM_HANDLE_INIT(pci);
  2304. #ifdef CONFIG_ACPI_IBM_BAY
  2305. IBM_HANDLE_INIT(bay);
  2306. if (bay_handle)
  2307. IBM_HANDLE_INIT(bay_ej);
  2308. IBM_HANDLE_INIT(bay2);
  2309. if (bay2_handle)
  2310. IBM_HANDLE_INIT(bay2_ej);
  2311. #endif /* CONFIG_ACPI_IBM_BAY */
  2312. IBM_HANDLE_INIT(beep);
  2313. IBM_HANDLE_INIT(ecrd);
  2314. IBM_HANDLE_INIT(ecwr);
  2315. IBM_HANDLE_INIT(fans);
  2316. IBM_HANDLE_INIT(gfan);
  2317. IBM_HANDLE_INIT(sfan);
  2318. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  2319. if (!proc_dir) {
  2320. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  2321. acpi_ibm_exit();
  2322. return -ENODEV;
  2323. }
  2324. proc_dir->owner = THIS_MODULE;
  2325. for (i = 0; i < ARRAY_SIZE(ibms); i++) {
  2326. ret = ibm_init(&ibms[i]);
  2327. if (ret >= 0 && *ibms[i].param)
  2328. ret = ibms[i].write(ibms[i].param);
  2329. if (ret < 0) {
  2330. acpi_ibm_exit();
  2331. return ret;
  2332. }
  2333. }
  2334. return 0;
  2335. }
  2336. module_init(acpi_ibm_init);
  2337. module_exit(acpi_ibm_exit);