ibm_acpi.c 64 KB

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