ibm_acpi.c 64 KB

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