ibm_acpi.c 64 KB

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