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