ibm_acpi.c 58 KB

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