ibm_acpi.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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 <asm/uaccess.h>
  79. #include <acpi/acpi_drivers.h>
  80. #include <acpi/acnamesp.h>
  81. #define IBM_NAME "ibm"
  82. #define IBM_DESC "IBM ThinkPad ACPI Extras"
  83. #define IBM_FILE "ibm_acpi"
  84. #define IBM_URL "http://ibm-acpi.sf.net/"
  85. MODULE_AUTHOR("Borislav Deianov");
  86. MODULE_DESCRIPTION(IBM_DESC);
  87. MODULE_VERSION(IBM_VERSION);
  88. MODULE_LICENSE("GPL");
  89. #define IBM_DIR IBM_NAME
  90. #define IBM_LOG IBM_FILE ": "
  91. #define IBM_ERR KERN_ERR IBM_LOG
  92. #define IBM_NOTICE KERN_NOTICE IBM_LOG
  93. #define IBM_INFO KERN_INFO IBM_LOG
  94. #define IBM_DEBUG KERN_DEBUG IBM_LOG
  95. #define IBM_MAX_ACPI_ARGS 3
  96. #define __unused __attribute__ ((unused))
  97. static int experimental;
  98. module_param(experimental, int, 0);
  99. static acpi_handle root_handle = NULL;
  100. #define IBM_HANDLE(object, parent, paths...) \
  101. static acpi_handle object##_handle; \
  102. static acpi_handle *object##_parent = &parent##_handle; \
  103. static char *object##_path; \
  104. static char *object##_paths[] = { paths }
  105. /*
  106. * The following models are supported to various degrees:
  107. *
  108. * 570, 600e, 600x, 770e, 770x
  109. * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
  110. * G40, G41
  111. * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
  112. * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
  113. * X20, X21, X22, X23, X24, X30, X31, X40
  114. *
  115. * The following models have no supported features:
  116. *
  117. * 240, 240x, i1400
  118. *
  119. * Still missing DSDTs for the following models:
  120. *
  121. * A20p, A22e, A22m
  122. * R52
  123. * S31
  124. * T43p
  125. */
  126. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  127. "\\_SB.PCI.ISA.EC", /* 570 */
  128. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  129. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  130. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  131. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  132. "\\_SB.PCI0.LPC.EC", /* all others */
  133. );
  134. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  135. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  136. "\\_SB.PCI0.VID0", /* 770e */
  137. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  138. "\\_SB.PCI0.AGP.VID", /* all others */
  139. ); /* R30, R31 */
  140. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  141. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  142. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  143. "\\CMS", /* R40, R40e */
  144. ); /* all others */
  145. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  146. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  147. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  148. "\\_SB.PCI.ISA.SLCE", /* 570 */
  149. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  150. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  151. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  152. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  153. ); /* A21e, R30, R31 */
  154. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  155. "_EJ0", /* all others */
  156. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  157. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  158. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  159. ); /* all others */
  160. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  161. "_EJ0", /* 770x */
  162. ); /* all others */
  163. /* don't list other alternatives as we install a notify handler on the 570 */
  164. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  165. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  166. "^HKEY", /* R30, R31 */
  167. "HKEY", /* all others */
  168. ); /* 570 */
  169. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  170. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  171. IBM_HANDLE(led, ec, "SLED", /* 570 */
  172. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  173. "LED", /* all others */
  174. ); /* R30, R31 */
  175. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  176. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  177. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  178. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40 */
  179. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  180. "\\FSPD", /* 600e/x, 770e, 770x */
  181. ); /* all others */
  182. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  183. "JFNS", /* 770x-JL */
  184. ); /* all others */
  185. #define IBM_HKEY_HID "IBM0068"
  186. #define IBM_PCI_HID "PNP0A03"
  187. struct ibm_struct {
  188. char *name;
  189. char param[32];
  190. char *hid;
  191. struct acpi_driver *driver;
  192. int (*init) (void);
  193. int (*read) (char *);
  194. int (*write) (char *);
  195. void (*exit) (void);
  196. void (*notify) (struct ibm_struct *, u32);
  197. acpi_handle *handle;
  198. int type;
  199. struct acpi_device *device;
  200. int driver_registered;
  201. int proc_created;
  202. int init_called;
  203. int notify_installed;
  204. int experimental;
  205. };
  206. static struct proc_dir_entry *proc_dir = NULL;
  207. #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
  208. #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
  209. #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
  210. static int acpi_evalf(acpi_handle handle,
  211. void *res, char *method, char *fmt, ...)
  212. {
  213. char *fmt0 = fmt;
  214. struct acpi_object_list params;
  215. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  216. struct acpi_buffer result, *resultp;
  217. union acpi_object out_obj;
  218. acpi_status status;
  219. va_list ap;
  220. char res_type;
  221. int success;
  222. int quiet;
  223. if (!*fmt) {
  224. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  225. return 0;
  226. }
  227. if (*fmt == 'q') {
  228. quiet = 1;
  229. fmt++;
  230. } else
  231. quiet = 0;
  232. res_type = *(fmt++);
  233. params.count = 0;
  234. params.pointer = &in_objs[0];
  235. va_start(ap, fmt);
  236. while (*fmt) {
  237. char c = *(fmt++);
  238. switch (c) {
  239. case 'd': /* int */
  240. in_objs[params.count].integer.value = va_arg(ap, int);
  241. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  242. break;
  243. /* add more types as needed */
  244. default:
  245. printk(IBM_ERR "acpi_evalf() called "
  246. "with invalid format character '%c'\n", c);
  247. return 0;
  248. }
  249. }
  250. va_end(ap);
  251. if (res_type != 'v') {
  252. result.length = sizeof(out_obj);
  253. result.pointer = &out_obj;
  254. resultp = &result;
  255. } else
  256. resultp = NULL;
  257. status = acpi_evaluate_object(handle, method, &params, resultp);
  258. switch (res_type) {
  259. case 'd': /* int */
  260. if (res)
  261. *(int *)res = out_obj.integer.value;
  262. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  263. break;
  264. case 'v': /* void */
  265. success = status == AE_OK;
  266. break;
  267. /* add more types as needed */
  268. default:
  269. printk(IBM_ERR "acpi_evalf() called "
  270. "with invalid format character '%c'\n", res_type);
  271. return 0;
  272. }
  273. if (!success && !quiet)
  274. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  275. method, fmt0, status);
  276. return success;
  277. }
  278. static void __unused acpi_print_int(acpi_handle handle, char *method)
  279. {
  280. int i;
  281. if (acpi_evalf(handle, &i, method, "d"))
  282. printk(IBM_INFO "%s = 0x%x\n", method, i);
  283. else
  284. printk(IBM_ERR "error calling %s\n", method);
  285. }
  286. static char *next_cmd(char **cmds)
  287. {
  288. char *start = *cmds;
  289. char *end;
  290. while ((end = strchr(start, ',')) && end == start)
  291. start = end + 1;
  292. if (!end)
  293. return NULL;
  294. *end = 0;
  295. *cmds = end + 1;
  296. return start;
  297. }
  298. static int driver_init(void)
  299. {
  300. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  301. printk(IBM_INFO "%s\n", IBM_URL);
  302. return 0;
  303. }
  304. static int driver_read(char *p)
  305. {
  306. int len = 0;
  307. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  308. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  309. return len;
  310. }
  311. static int hotkey_supported;
  312. static int hotkey_mask_supported;
  313. static int hotkey_orig_status;
  314. static int hotkey_orig_mask;
  315. static int hotkey_get(int *status, int *mask)
  316. {
  317. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  318. return 0;
  319. if (hotkey_mask_supported)
  320. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  321. return 0;
  322. return 1;
  323. }
  324. static int hotkey_set(int status, int mask)
  325. {
  326. int i;
  327. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  328. return 0;
  329. if (hotkey_mask_supported)
  330. for (i = 0; i < 32; i++) {
  331. int bit = ((1 << i) & mask) != 0;
  332. if (!acpi_evalf(hkey_handle,
  333. NULL, "MHKM", "vdd", i + 1, bit))
  334. return 0;
  335. }
  336. return 1;
  337. }
  338. static int hotkey_init(void)
  339. {
  340. /* hotkey not supported on 570 */
  341. hotkey_supported = hkey_handle != NULL;
  342. if (hotkey_supported) {
  343. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  344. A30, R30, R31, T20-22, X20-21, X22-24 */
  345. hotkey_mask_supported =
  346. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  347. if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
  348. return -ENODEV;
  349. }
  350. return 0;
  351. }
  352. static int hotkey_read(char *p)
  353. {
  354. int status, mask;
  355. int len = 0;
  356. if (!hotkey_supported) {
  357. len += sprintf(p + len, "status:\t\tnot supported\n");
  358. return len;
  359. }
  360. if (!hotkey_get(&status, &mask))
  361. return -EIO;
  362. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  363. if (hotkey_mask_supported) {
  364. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  365. len += sprintf(p + len,
  366. "commands:\tenable, disable, reset, <mask>\n");
  367. } else {
  368. len += sprintf(p + len, "mask:\t\tnot supported\n");
  369. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  370. }
  371. return len;
  372. }
  373. static int hotkey_write(char *buf)
  374. {
  375. int status, mask;
  376. char *cmd;
  377. int do_cmd = 0;
  378. if (!hotkey_supported)
  379. return -ENODEV;
  380. if (!hotkey_get(&status, &mask))
  381. return -EIO;
  382. while ((cmd = next_cmd(&buf))) {
  383. if (strlencmp(cmd, "enable") == 0) {
  384. status = 1;
  385. } else if (strlencmp(cmd, "disable") == 0) {
  386. status = 0;
  387. } else if (strlencmp(cmd, "reset") == 0) {
  388. status = hotkey_orig_status;
  389. mask = hotkey_orig_mask;
  390. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  391. /* mask set */
  392. } else if (sscanf(cmd, "%x", &mask) == 1) {
  393. /* mask set */
  394. } else
  395. return -EINVAL;
  396. do_cmd = 1;
  397. }
  398. if (do_cmd && !hotkey_set(status, mask))
  399. return -EIO;
  400. return 0;
  401. }
  402. static void hotkey_exit(void)
  403. {
  404. if (hotkey_supported)
  405. hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  406. }
  407. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  408. {
  409. int hkey;
  410. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  411. acpi_bus_generate_event(ibm->device, event, hkey);
  412. else {
  413. printk(IBM_ERR "unknown hotkey event %d\n", event);
  414. acpi_bus_generate_event(ibm->device, event, 0);
  415. }
  416. }
  417. static int bluetooth_supported;
  418. static int bluetooth_init(void)
  419. {
  420. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  421. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  422. bluetooth_supported = hkey_handle &&
  423. acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
  424. return 0;
  425. }
  426. static int bluetooth_status(void)
  427. {
  428. int status;
  429. if (!bluetooth_supported ||
  430. !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  431. status = 0;
  432. return status;
  433. }
  434. static int bluetooth_read(char *p)
  435. {
  436. int len = 0;
  437. int status = bluetooth_status();
  438. if (!bluetooth_supported)
  439. len += sprintf(p + len, "status:\t\tnot supported\n");
  440. else if (!(status & 1))
  441. len += sprintf(p + len, "status:\t\tnot installed\n");
  442. else {
  443. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  444. len += sprintf(p + len, "commands:\tenable, disable\n");
  445. }
  446. return len;
  447. }
  448. static int bluetooth_write(char *buf)
  449. {
  450. int status = bluetooth_status();
  451. char *cmd;
  452. int do_cmd = 0;
  453. if (!bluetooth_supported)
  454. return -ENODEV;
  455. while ((cmd = next_cmd(&buf))) {
  456. if (strlencmp(cmd, "enable") == 0) {
  457. status |= 2;
  458. } else if (strlencmp(cmd, "disable") == 0) {
  459. status &= ~2;
  460. } else
  461. return -EINVAL;
  462. do_cmd = 1;
  463. }
  464. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  465. return -EIO;
  466. return 0;
  467. }
  468. static int video_supported;
  469. static int video_orig_autosw;
  470. #define VIDEO_570 1
  471. #define VIDEO_770 2
  472. #define VIDEO_NEW 3
  473. static int video_init(void)
  474. {
  475. int ivga;
  476. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  477. /* G41, assume IVGA doesn't change */
  478. vid_handle = vid2_handle;
  479. if (!vid_handle)
  480. /* video switching not supported on R30, R31 */
  481. video_supported = 0;
  482. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  483. /* 570 */
  484. video_supported = VIDEO_570;
  485. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  486. /* 600e/x, 770e, 770x */
  487. video_supported = VIDEO_770;
  488. else
  489. /* all others */
  490. video_supported = VIDEO_NEW;
  491. return 0;
  492. }
  493. static int video_status(void)
  494. {
  495. int status = 0;
  496. int i;
  497. if (video_supported == VIDEO_570) {
  498. if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
  499. status = i & 3;
  500. } else if (video_supported == VIDEO_770) {
  501. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  502. status |= 0x01 * i;
  503. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  504. status |= 0x02 * i;
  505. } else if (video_supported == VIDEO_NEW) {
  506. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
  507. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  508. status |= 0x02 * i;
  509. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
  510. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  511. status |= 0x01 * i;
  512. if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
  513. status |= 0x08 * i;
  514. }
  515. return status;
  516. }
  517. static int video_autosw(void)
  518. {
  519. int autosw = 0;
  520. if (video_supported == VIDEO_570)
  521. acpi_evalf(vid_handle, &autosw, "SWIT", "d");
  522. else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
  523. acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
  524. return autosw & 1;
  525. }
  526. static int video_read(char *p)
  527. {
  528. int status = video_status();
  529. int autosw = video_autosw();
  530. int len = 0;
  531. if (!video_supported) {
  532. len += sprintf(p + len, "status:\t\tnot supported\n");
  533. return len;
  534. }
  535. len += sprintf(p + len, "status:\t\tsupported\n");
  536. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  537. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  538. if (video_supported == VIDEO_NEW)
  539. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  540. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  541. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  542. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  543. if (video_supported == VIDEO_NEW)
  544. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  545. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  546. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  547. return len;
  548. }
  549. static int video_switch(void)
  550. {
  551. int autosw = video_autosw();
  552. int ret;
  553. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  554. return -EIO;
  555. ret = video_supported == VIDEO_570 ?
  556. acpi_evalf(ec_handle, NULL, "_Q16", "v") :
  557. acpi_evalf(vid_handle, NULL, "VSWT", "v");
  558. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  559. return ret;
  560. }
  561. static int video_expand(void)
  562. {
  563. if (video_supported == VIDEO_570)
  564. return acpi_evalf(ec_handle, NULL, "_Q17", "v");
  565. else if (video_supported == VIDEO_770)
  566. return acpi_evalf(vid_handle, NULL, "VEXP", "v");
  567. else
  568. return acpi_evalf(NULL, NULL, "\\VEXP", "v");
  569. }
  570. static int video_switch2(int status)
  571. {
  572. int ret;
  573. if (video_supported == VIDEO_570) {
  574. ret = acpi_evalf(NULL, NULL,
  575. "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
  576. } else if (video_supported == VIDEO_770) {
  577. int autosw = video_autosw();
  578. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  579. return -EIO;
  580. ret = acpi_evalf(vid_handle, NULL,
  581. "ASWT", "vdd", status * 0x100, 0);
  582. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  583. } else {
  584. ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  585. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  586. }
  587. return ret;
  588. }
  589. static int video_write(char *buf)
  590. {
  591. char *cmd;
  592. int enable, disable, status;
  593. if (!video_supported)
  594. return -ENODEV;
  595. enable = disable = 0;
  596. while ((cmd = next_cmd(&buf))) {
  597. if (strlencmp(cmd, "lcd_enable") == 0) {
  598. enable |= 0x01;
  599. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  600. disable |= 0x01;
  601. } else if (strlencmp(cmd, "crt_enable") == 0) {
  602. enable |= 0x02;
  603. } else if (strlencmp(cmd, "crt_disable") == 0) {
  604. disable |= 0x02;
  605. } else if (video_supported == VIDEO_NEW &&
  606. strlencmp(cmd, "dvi_enable") == 0) {
  607. enable |= 0x08;
  608. } else if (video_supported == VIDEO_NEW &&
  609. strlencmp(cmd, "dvi_disable") == 0) {
  610. disable |= 0x08;
  611. } else if (strlencmp(cmd, "auto_enable") == 0) {
  612. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  613. return -EIO;
  614. } else if (strlencmp(cmd, "auto_disable") == 0) {
  615. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
  616. return -EIO;
  617. } else if (strlencmp(cmd, "video_switch") == 0) {
  618. if (!video_switch())
  619. return -EIO;
  620. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  621. if (!video_expand())
  622. return -EIO;
  623. } else
  624. return -EINVAL;
  625. }
  626. if (enable || disable) {
  627. status = (video_status() & 0x0f & ~disable) | enable;
  628. if (!video_switch2(status))
  629. return -EIO;
  630. }
  631. return 0;
  632. }
  633. static void video_exit(void)
  634. {
  635. acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
  636. }
  637. static int light_supported;
  638. static int light_status_supported;
  639. static int light_init(void)
  640. {
  641. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  642. light_supported = (cmos_handle || lght_handle) && !ledb_handle;
  643. if (light_supported)
  644. /* light status not supported on
  645. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  646. light_status_supported = acpi_evalf(ec_handle, NULL,
  647. "KBLT", "qv");
  648. return 0;
  649. }
  650. static int light_read(char *p)
  651. {
  652. int len = 0;
  653. int status = 0;
  654. if (!light_supported) {
  655. len += sprintf(p + len, "status:\t\tnot supported\n");
  656. } else if (!light_status_supported) {
  657. len += sprintf(p + len, "status:\t\tunknown\n");
  658. len += sprintf(p + len, "commands:\ton, off\n");
  659. } else {
  660. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  661. return -EIO;
  662. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  663. len += sprintf(p + len, "commands:\ton, off\n");
  664. }
  665. return len;
  666. }
  667. static int light_write(char *buf)
  668. {
  669. int cmos_cmd, lght_cmd;
  670. char *cmd;
  671. int success;
  672. if (!light_supported)
  673. return -ENODEV;
  674. while ((cmd = next_cmd(&buf))) {
  675. if (strlencmp(cmd, "on") == 0) {
  676. cmos_cmd = 0x0c;
  677. lght_cmd = 1;
  678. } else if (strlencmp(cmd, "off") == 0) {
  679. cmos_cmd = 0x0d;
  680. lght_cmd = 0;
  681. } else
  682. return -EINVAL;
  683. success = cmos_handle ?
  684. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  685. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  686. if (!success)
  687. return -EIO;
  688. }
  689. return 0;
  690. }
  691. static int _sta(acpi_handle handle)
  692. {
  693. int status;
  694. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  695. status = 0;
  696. return status;
  697. }
  698. #define dock_docked() (_sta(dock_handle) & 1)
  699. static int dock_read(char *p)
  700. {
  701. int len = 0;
  702. int docked = dock_docked();
  703. if (!dock_handle)
  704. len += sprintf(p + len, "status:\t\tnot supported\n");
  705. else if (!docked)
  706. len += sprintf(p + len, "status:\t\tundocked\n");
  707. else {
  708. len += sprintf(p + len, "status:\t\tdocked\n");
  709. len += sprintf(p + len, "commands:\tdock, undock\n");
  710. }
  711. return len;
  712. }
  713. static int dock_write(char *buf)
  714. {
  715. char *cmd;
  716. if (!dock_docked())
  717. return -ENODEV;
  718. while ((cmd = next_cmd(&buf))) {
  719. if (strlencmp(cmd, "undock") == 0) {
  720. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  721. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  722. return -EIO;
  723. } else if (strlencmp(cmd, "dock") == 0) {
  724. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  725. return -EIO;
  726. } else
  727. return -EINVAL;
  728. }
  729. return 0;
  730. }
  731. static void dock_notify(struct ibm_struct *ibm, u32 event)
  732. {
  733. int docked = dock_docked();
  734. int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
  735. if (event == 1 && !pci) /* 570 */
  736. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  737. else if (event == 1 && pci) /* 570 */
  738. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  739. else if (event == 3 && docked)
  740. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  741. else if (event == 3 && !docked)
  742. acpi_bus_generate_event(ibm->device, event, 2); /* undock */
  743. else if (event == 0 && docked)
  744. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  745. else {
  746. printk(IBM_ERR "unknown dock event %d, status %d\n",
  747. event, _sta(dock_handle));
  748. acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
  749. }
  750. }
  751. static int bay_status_supported;
  752. static int bay_status2_supported;
  753. static int bay_eject_supported;
  754. static int bay_eject2_supported;
  755. static int bay_init(void)
  756. {
  757. bay_status_supported = bay_handle &&
  758. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  759. bay_status2_supported = bay2_handle &&
  760. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  761. bay_eject_supported = bay_handle && bay_ej_handle &&
  762. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  763. bay_eject2_supported = bay2_handle && bay2_ej_handle &&
  764. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  765. return 0;
  766. }
  767. #define bay_occupied(b) (_sta(b##_handle) & 1)
  768. static int bay_read(char *p)
  769. {
  770. int len = 0;
  771. int occupied = bay_occupied(bay);
  772. int occupied2 = bay_occupied(bay2);
  773. int eject, eject2;
  774. len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
  775. (occupied ? "occupied" : "unoccupied") :
  776. "not supported");
  777. if (bay_status2_supported)
  778. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  779. "occupied" : "unoccupied");
  780. eject = bay_eject_supported && occupied;
  781. eject2 = bay_eject2_supported && occupied2;
  782. if (eject && eject2)
  783. len += sprintf(p + len, "commands:\teject, eject2\n");
  784. else if (eject)
  785. len += sprintf(p + len, "commands:\teject\n");
  786. else if (eject2)
  787. len += sprintf(p + len, "commands:\teject2\n");
  788. return len;
  789. }
  790. static int bay_write(char *buf)
  791. {
  792. char *cmd;
  793. if (!bay_eject_supported && !bay_eject2_supported)
  794. return -ENODEV;
  795. while ((cmd = next_cmd(&buf))) {
  796. if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
  797. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  798. return -EIO;
  799. } else if (bay_eject2_supported &&
  800. strlencmp(cmd, "eject2") == 0) {
  801. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  802. return -EIO;
  803. } else
  804. return -EINVAL;
  805. }
  806. return 0;
  807. }
  808. static void bay_notify(struct ibm_struct *ibm, u32 event)
  809. {
  810. acpi_bus_generate_event(ibm->device, event, 0);
  811. }
  812. static int cmos_read(char *p)
  813. {
  814. int len = 0;
  815. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  816. R30, R31, T20-22, X20-21 */
  817. if (!cmos_handle)
  818. len += sprintf(p + len, "status:\t\tnot supported\n");
  819. else {
  820. len += sprintf(p + len, "status:\t\tsupported\n");
  821. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  822. }
  823. return len;
  824. }
  825. static int cmos_eval(int cmos_cmd)
  826. {
  827. if (cmos_handle)
  828. return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
  829. else
  830. return 1;
  831. }
  832. static int cmos_write(char *buf)
  833. {
  834. char *cmd;
  835. int cmos_cmd;
  836. if (!cmos_handle)
  837. return -EINVAL;
  838. while ((cmd = next_cmd(&buf))) {
  839. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  840. cmos_cmd >= 0 && cmos_cmd <= 21) {
  841. /* cmos_cmd set */
  842. } else
  843. return -EINVAL;
  844. if (!cmos_eval(cmos_cmd))
  845. return -EIO;
  846. }
  847. return 0;
  848. }
  849. static int led_supported;
  850. #define LED_570 1
  851. #define LED_OLD 2
  852. #define LED_NEW 3
  853. static int led_init(void)
  854. {
  855. if (!led_handle)
  856. /* led not supported on R30, R31 */
  857. led_supported = 0;
  858. else if (strlencmp(led_path, "SLED") == 0)
  859. /* 570 */
  860. led_supported = LED_570;
  861. else if (strlencmp(led_path, "SYSL") == 0)
  862. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  863. led_supported = LED_OLD;
  864. else
  865. /* all others */
  866. led_supported = LED_NEW;
  867. return 0;
  868. }
  869. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  870. static int led_read(char *p)
  871. {
  872. int len = 0;
  873. if (!led_supported) {
  874. len += sprintf(p + len, "status:\t\tnot supported\n");
  875. return len;
  876. }
  877. len += sprintf(p + len, "status:\t\tsupported\n");
  878. if (led_supported == LED_570) {
  879. /* 570 */
  880. int i, status;
  881. for (i = 0; i < 8; i++) {
  882. if (!acpi_evalf(ec_handle,
  883. &status, "GLED", "dd", 1 << i))
  884. return -EIO;
  885. len += sprintf(p + len, "%d:\t\t%s\n",
  886. i, led_status(status));
  887. }
  888. }
  889. len += sprintf(p + len, "commands:\t"
  890. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  891. return len;
  892. }
  893. /* off, on, blink */
  894. static const int led_sled_arg1[] = { 0, 1, 3 };
  895. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  896. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  897. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  898. #define EC_HLCL 0x0c
  899. #define EC_HLBL 0x0d
  900. #define EC_HLMS 0x0e
  901. static int led_write(char *buf)
  902. {
  903. char *cmd;
  904. int led, ind, ret;
  905. if (!led_supported)
  906. return -ENODEV;
  907. while ((cmd = next_cmd(&buf))) {
  908. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  909. return -EINVAL;
  910. if (strstr(cmd, "off")) {
  911. ind = 0;
  912. } else if (strstr(cmd, "on")) {
  913. ind = 1;
  914. } else if (strstr(cmd, "blink")) {
  915. ind = 2;
  916. } else
  917. return -EINVAL;
  918. if (led_supported == LED_570) {
  919. /* 570 */
  920. led = 1 << led;
  921. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  922. led, led_sled_arg1[ind]))
  923. return -EIO;
  924. } else if (led_supported == LED_OLD) {
  925. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  926. led = 1 << led;
  927. ret = ec_write(EC_HLMS, led);
  928. if (ret >= 0)
  929. ret =
  930. ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
  931. if (ret >= 0)
  932. ret =
  933. ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
  934. if (ret < 0)
  935. return ret;
  936. } else {
  937. /* all others */
  938. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  939. led, led_led_arg1[ind]))
  940. return -EIO;
  941. }
  942. }
  943. return 0;
  944. }
  945. static int beep_read(char *p)
  946. {
  947. int len = 0;
  948. if (!beep_handle)
  949. len += sprintf(p + len, "status:\t\tnot supported\n");
  950. else {
  951. len += sprintf(p + len, "status:\t\tsupported\n");
  952. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  953. }
  954. return len;
  955. }
  956. static int beep_write(char *buf)
  957. {
  958. char *cmd;
  959. int beep_cmd;
  960. if (!beep_handle)
  961. return -ENODEV;
  962. while ((cmd = next_cmd(&buf))) {
  963. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  964. beep_cmd >= 0 && beep_cmd <= 17) {
  965. /* beep_cmd set */
  966. } else
  967. return -EINVAL;
  968. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  969. return -EIO;
  970. }
  971. return 0;
  972. }
  973. static int acpi_ec_read(int i, u8 * p)
  974. {
  975. int v;
  976. if (ecrd_handle) {
  977. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  978. return 0;
  979. *p = v;
  980. } else {
  981. if (ec_read(i, p) < 0)
  982. return 0;
  983. }
  984. return 1;
  985. }
  986. static int acpi_ec_write(int i, u8 v)
  987. {
  988. if (ecwr_handle) {
  989. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  990. return 0;
  991. } else {
  992. if (ec_write(i, v) < 0)
  993. return 0;
  994. }
  995. return 1;
  996. }
  997. static int thermal_tmp_supported;
  998. static int thermal_updt_supported;
  999. static int thermal_init(void)
  1000. {
  1001. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1002. thermal_tmp_supported = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1003. /* 600e/x, 770e, 770x */
  1004. thermal_updt_supported = acpi_evalf(ec_handle, NULL, "UPDT", "qv");
  1005. return 0;
  1006. }
  1007. static int thermal_read(char *p)
  1008. {
  1009. int len = 0;
  1010. if (!thermal_tmp_supported)
  1011. len += sprintf(p + len, "temperatures:\tnot supported\n");
  1012. else {
  1013. int i, t;
  1014. char tmpi[] = "TMPi";
  1015. s8 tmp[8];
  1016. if (thermal_updt_supported)
  1017. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1018. return -EIO;
  1019. for (i = 0; i < 8; i++) {
  1020. tmpi[3] = '0' + i;
  1021. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1022. return -EIO;
  1023. if (thermal_updt_supported)
  1024. tmp[i] = (t - 2732 + 5) / 10;
  1025. else
  1026. tmp[i] = t;
  1027. }
  1028. len += sprintf(p + len,
  1029. "temperatures:\t%d %d %d %d %d %d %d %d\n",
  1030. tmp[0], tmp[1], tmp[2], tmp[3],
  1031. tmp[4], tmp[5], tmp[6], tmp[7]);
  1032. }
  1033. return len;
  1034. }
  1035. static u8 ecdump_regs[256];
  1036. static int ecdump_read(char *p)
  1037. {
  1038. int len = 0;
  1039. int i, j;
  1040. u8 v;
  1041. len += sprintf(p + len, "EC "
  1042. " +00 +01 +02 +03 +04 +05 +06 +07"
  1043. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1044. for (i = 0; i < 256; i += 16) {
  1045. len += sprintf(p + len, "EC 0x%02x:", i);
  1046. for (j = 0; j < 16; j++) {
  1047. if (!acpi_ec_read(i + j, &v))
  1048. break;
  1049. if (v != ecdump_regs[i + j])
  1050. len += sprintf(p + len, " *%02x", v);
  1051. else
  1052. len += sprintf(p + len, " %02x", v);
  1053. ecdump_regs[i + j] = v;
  1054. }
  1055. len += sprintf(p + len, "\n");
  1056. if (j != 16)
  1057. break;
  1058. }
  1059. /* These are way too dangerous to advertise openly... */
  1060. #if 0
  1061. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1062. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1063. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1064. " (<offset> is 00-ff, <value> is 0-255)\n");
  1065. #endif
  1066. return len;
  1067. }
  1068. static int ecdump_write(char *buf)
  1069. {
  1070. char *cmd;
  1071. int i, v;
  1072. while ((cmd = next_cmd(&buf))) {
  1073. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1074. /* i and v set */
  1075. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1076. /* i and v set */
  1077. } else
  1078. return -EINVAL;
  1079. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1080. if (!acpi_ec_write(i, v))
  1081. return -EIO;
  1082. } else
  1083. return -EINVAL;
  1084. }
  1085. return 0;
  1086. }
  1087. static int brightness_offset = 0x31;
  1088. static int brightness_read(char *p)
  1089. {
  1090. int len = 0;
  1091. u8 level;
  1092. if (!acpi_ec_read(brightness_offset, &level)) {
  1093. len += sprintf(p + len, "level:\t\tunreadable\n");
  1094. } else {
  1095. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1096. len += sprintf(p + len, "commands:\tup, down\n");
  1097. len += sprintf(p + len, "commands:\tlevel <level>"
  1098. " (<level> is 0-7)\n");
  1099. }
  1100. return len;
  1101. }
  1102. #define BRIGHTNESS_UP 4
  1103. #define BRIGHTNESS_DOWN 5
  1104. static int brightness_write(char *buf)
  1105. {
  1106. int cmos_cmd, inc, i;
  1107. u8 level;
  1108. int new_level;
  1109. char *cmd;
  1110. while ((cmd = next_cmd(&buf))) {
  1111. if (!acpi_ec_read(brightness_offset, &level))
  1112. return -EIO;
  1113. level &= 7;
  1114. if (strlencmp(cmd, "up") == 0) {
  1115. new_level = level == 7 ? 7 : level + 1;
  1116. } else if (strlencmp(cmd, "down") == 0) {
  1117. new_level = level == 0 ? 0 : level - 1;
  1118. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1119. new_level >= 0 && new_level <= 7) {
  1120. /* new_level set */
  1121. } else
  1122. return -EINVAL;
  1123. cmos_cmd = new_level > level ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
  1124. inc = new_level > level ? 1 : -1;
  1125. for (i = level; i != new_level; i += inc) {
  1126. if (!cmos_eval(cmos_cmd))
  1127. return -EIO;
  1128. if (!acpi_ec_write(brightness_offset, i + inc))
  1129. return -EIO;
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. static int volume_offset = 0x30;
  1135. static int volume_read(char *p)
  1136. {
  1137. int len = 0;
  1138. u8 level;
  1139. if (!acpi_ec_read(volume_offset, &level)) {
  1140. len += sprintf(p + len, "level:\t\tunreadable\n");
  1141. } else {
  1142. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1143. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1144. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1145. len += sprintf(p + len, "commands:\tlevel <level>"
  1146. " (<level> is 0-15)\n");
  1147. }
  1148. return len;
  1149. }
  1150. #define VOLUME_DOWN 0
  1151. #define VOLUME_UP 1
  1152. #define VOLUME_MUTE 2
  1153. static int volume_write(char *buf)
  1154. {
  1155. int cmos_cmd, inc, i;
  1156. u8 level, mute;
  1157. int new_level, new_mute;
  1158. char *cmd;
  1159. while ((cmd = next_cmd(&buf))) {
  1160. if (!acpi_ec_read(volume_offset, &level))
  1161. return -EIO;
  1162. new_mute = mute = level & 0x40;
  1163. new_level = level = level & 0xf;
  1164. if (strlencmp(cmd, "up") == 0) {
  1165. if (mute)
  1166. new_mute = 0;
  1167. else
  1168. new_level = level == 15 ? 15 : level + 1;
  1169. } else if (strlencmp(cmd, "down") == 0) {
  1170. if (mute)
  1171. new_mute = 0;
  1172. else
  1173. new_level = level == 0 ? 0 : level - 1;
  1174. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1175. new_level >= 0 && new_level <= 15) {
  1176. /* new_level set */
  1177. } else if (strlencmp(cmd, "mute") == 0) {
  1178. new_mute = 0x40;
  1179. } else
  1180. return -EINVAL;
  1181. if (new_level != level) { /* mute doesn't change */
  1182. cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
  1183. inc = new_level > level ? 1 : -1;
  1184. if (mute && (!cmos_eval(cmos_cmd) ||
  1185. !acpi_ec_write(volume_offset, level)))
  1186. return -EIO;
  1187. for (i = level; i != new_level; i += inc)
  1188. if (!cmos_eval(cmos_cmd) ||
  1189. !acpi_ec_write(volume_offset, i + inc))
  1190. return -EIO;
  1191. if (mute && (!cmos_eval(VOLUME_MUTE) ||
  1192. !acpi_ec_write(volume_offset,
  1193. new_level + mute)))
  1194. return -EIO;
  1195. }
  1196. if (new_mute != mute) { /* level doesn't change */
  1197. cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
  1198. if (!cmos_eval(cmos_cmd) ||
  1199. !acpi_ec_write(volume_offset, level + new_mute))
  1200. return -EIO;
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. static int fan_status_offset = 0x2f;
  1206. static int fan_rpm_offset = 0x84;
  1207. static int fan_read(char *p)
  1208. {
  1209. int len = 0;
  1210. int s;
  1211. u8 lo, hi, status;
  1212. if (gfan_handle) {
  1213. /* 570, 600e/x, 770e, 770x */
  1214. if (!acpi_evalf(gfan_handle, &s, NULL, "d"))
  1215. return -EIO;
  1216. len += sprintf(p + len, "level:\t\t%d\n", s);
  1217. } else {
  1218. /* all except 570, 600e/x, 770e, 770x */
  1219. if (!acpi_ec_read(fan_status_offset, &status))
  1220. len += sprintf(p + len, "status:\t\tunreadable\n");
  1221. else
  1222. len += sprintf(p + len, "status:\t\t%s\n",
  1223. enabled(status, 7));
  1224. if (!acpi_ec_read(fan_rpm_offset, &lo) ||
  1225. !acpi_ec_read(fan_rpm_offset + 1, &hi))
  1226. len += sprintf(p + len, "speed:\t\tunreadable\n");
  1227. else
  1228. len += sprintf(p + len, "speed:\t\t%d\n",
  1229. (hi << 8) + lo);
  1230. }
  1231. if (sfan_handle)
  1232. /* 570, 770x-JL */
  1233. len += sprintf(p + len, "commands:\tlevel <level>"
  1234. " (<level> is 0-7)\n");
  1235. if (!gfan_handle)
  1236. /* all except 570, 600e/x, 770e, 770x */
  1237. len += sprintf(p + len, "commands:\tenable, disable\n");
  1238. if (fans_handle)
  1239. /* X31, X40 */
  1240. len += sprintf(p + len, "commands:\tspeed <speed>"
  1241. " (<speed> is 0-65535)\n");
  1242. return len;
  1243. }
  1244. static int fan_write(char *buf)
  1245. {
  1246. char *cmd;
  1247. int level, speed;
  1248. while ((cmd = next_cmd(&buf))) {
  1249. if (sfan_handle &&
  1250. sscanf(cmd, "level %d", &level) == 1 &&
  1251. level >= 0 && level <= 7) {
  1252. /* 570, 770x-JL */
  1253. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  1254. return -EIO;
  1255. } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
  1256. /* all except 570, 600e/x, 770e, 770x */
  1257. if (!acpi_ec_write(fan_status_offset, 0x80))
  1258. return -EIO;
  1259. } else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
  1260. /* all except 570, 600e/x, 770e, 770x */
  1261. if (!acpi_ec_write(fan_status_offset, 0x00))
  1262. return -EIO;
  1263. } else if (fans_handle &&
  1264. sscanf(cmd, "speed %d", &speed) == 1 &&
  1265. speed >= 0 && speed <= 65535) {
  1266. /* X31, X40 */
  1267. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  1268. speed, speed, speed))
  1269. return -EIO;
  1270. } else
  1271. return -EINVAL;
  1272. }
  1273. return 0;
  1274. }
  1275. static struct ibm_struct ibms[] = {
  1276. {
  1277. .name = "driver",
  1278. .init = driver_init,
  1279. .read = driver_read,
  1280. },
  1281. {
  1282. .name = "hotkey",
  1283. .hid = IBM_HKEY_HID,
  1284. .init = hotkey_init,
  1285. .read = hotkey_read,
  1286. .write = hotkey_write,
  1287. .exit = hotkey_exit,
  1288. .notify = hotkey_notify,
  1289. .handle = &hkey_handle,
  1290. .type = ACPI_DEVICE_NOTIFY,
  1291. },
  1292. {
  1293. .name = "bluetooth",
  1294. .init = bluetooth_init,
  1295. .read = bluetooth_read,
  1296. .write = bluetooth_write,
  1297. },
  1298. {
  1299. .name = "video",
  1300. .init = video_init,
  1301. .read = video_read,
  1302. .write = video_write,
  1303. .exit = video_exit,
  1304. },
  1305. {
  1306. .name = "light",
  1307. .init = light_init,
  1308. .read = light_read,
  1309. .write = light_write,
  1310. },
  1311. {
  1312. .name = "dock",
  1313. .read = dock_read,
  1314. .write = dock_write,
  1315. .notify = dock_notify,
  1316. .handle = &dock_handle,
  1317. .type = ACPI_SYSTEM_NOTIFY,
  1318. },
  1319. {
  1320. .name = "dock",
  1321. .hid = IBM_PCI_HID,
  1322. .notify = dock_notify,
  1323. .handle = &pci_handle,
  1324. .type = ACPI_SYSTEM_NOTIFY,
  1325. },
  1326. {
  1327. .name = "bay",
  1328. .init = bay_init,
  1329. .read = bay_read,
  1330. .write = bay_write,
  1331. .notify = bay_notify,
  1332. .handle = &bay_handle,
  1333. .type = ACPI_SYSTEM_NOTIFY,
  1334. },
  1335. {
  1336. .name = "cmos",
  1337. .read = cmos_read,
  1338. .write = cmos_write,
  1339. },
  1340. {
  1341. .name = "led",
  1342. .init = led_init,
  1343. .read = led_read,
  1344. .write = led_write,
  1345. },
  1346. {
  1347. .name = "beep",
  1348. .read = beep_read,
  1349. .write = beep_write,
  1350. },
  1351. {
  1352. .name = "thermal",
  1353. .init = thermal_init,
  1354. .read = thermal_read,
  1355. },
  1356. {
  1357. .name = "ecdump",
  1358. .read = ecdump_read,
  1359. .write = ecdump_write,
  1360. .experimental = 1,
  1361. },
  1362. {
  1363. .name = "brightness",
  1364. .read = brightness_read,
  1365. .write = brightness_write,
  1366. .experimental = 1,
  1367. },
  1368. {
  1369. .name = "volume",
  1370. .read = volume_read,
  1371. .write = volume_write,
  1372. .experimental = 1,
  1373. },
  1374. {
  1375. .name = "fan",
  1376. .read = fan_read,
  1377. .write = fan_write,
  1378. .experimental = 1,
  1379. },
  1380. };
  1381. static int dispatch_read(char *page, char **start, off_t off, int count,
  1382. int *eof, void *data)
  1383. {
  1384. struct ibm_struct *ibm = (struct ibm_struct *)data;
  1385. int len;
  1386. if (!ibm || !ibm->read)
  1387. return -EINVAL;
  1388. len = ibm->read(page);
  1389. if (len < 0)
  1390. return len;
  1391. if (len <= off + count)
  1392. *eof = 1;
  1393. *start = page + off;
  1394. len -= off;
  1395. if (len > count)
  1396. len = count;
  1397. if (len < 0)
  1398. len = 0;
  1399. return len;
  1400. }
  1401. static int dispatch_write(struct file *file, const char __user * userbuf,
  1402. unsigned long count, void *data)
  1403. {
  1404. struct ibm_struct *ibm = (struct ibm_struct *)data;
  1405. char *kernbuf;
  1406. int ret;
  1407. if (!ibm || !ibm->write)
  1408. return -EINVAL;
  1409. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  1410. if (!kernbuf)
  1411. return -ENOMEM;
  1412. if (copy_from_user(kernbuf, userbuf, count)) {
  1413. kfree(kernbuf);
  1414. return -EFAULT;
  1415. }
  1416. kernbuf[count] = 0;
  1417. strcat(kernbuf, ",");
  1418. ret = ibm->write(kernbuf);
  1419. if (ret == 0)
  1420. ret = count;
  1421. kfree(kernbuf);
  1422. return ret;
  1423. }
  1424. static void dispatch_notify(acpi_handle handle, u32 event, void *data)
  1425. {
  1426. struct ibm_struct *ibm = (struct ibm_struct *)data;
  1427. if (!ibm || !ibm->notify)
  1428. return;
  1429. ibm->notify(ibm, event);
  1430. }
  1431. static int __init setup_notify(struct ibm_struct *ibm)
  1432. {
  1433. acpi_status status;
  1434. int ret;
  1435. if (!*ibm->handle)
  1436. return 0;
  1437. ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
  1438. if (ret < 0) {
  1439. printk(IBM_ERR "%s device not present\n", ibm->name);
  1440. return 0;
  1441. }
  1442. acpi_driver_data(ibm->device) = ibm;
  1443. sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
  1444. status = acpi_install_notify_handler(*ibm->handle, ibm->type,
  1445. dispatch_notify, ibm);
  1446. if (ACPI_FAILURE(status)) {
  1447. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  1448. ibm->name, status);
  1449. return -ENODEV;
  1450. }
  1451. return 0;
  1452. }
  1453. static int __init ibm_device_add(struct acpi_device *device)
  1454. {
  1455. return 0;
  1456. }
  1457. static int __init register_driver(struct ibm_struct *ibm)
  1458. {
  1459. int ret;
  1460. ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  1461. if (!ibm->driver) {
  1462. printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
  1463. return -1;
  1464. }
  1465. memset(ibm->driver, 0, sizeof(struct acpi_driver));
  1466. sprintf(ibm->driver->name, "%s/%s", IBM_NAME, ibm->name);
  1467. ibm->driver->ids = ibm->hid;
  1468. ibm->driver->ops.add = &ibm_device_add;
  1469. ret = acpi_bus_register_driver(ibm->driver);
  1470. if (ret < 0) {
  1471. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  1472. ibm->hid, ret);
  1473. kfree(ibm->driver);
  1474. }
  1475. return ret;
  1476. }
  1477. static int __init ibm_init(struct ibm_struct *ibm)
  1478. {
  1479. int ret;
  1480. struct proc_dir_entry *entry;
  1481. if (ibm->experimental && !experimental)
  1482. return 0;
  1483. if (ibm->hid) {
  1484. ret = register_driver(ibm);
  1485. if (ret < 0)
  1486. return ret;
  1487. ibm->driver_registered = 1;
  1488. }
  1489. if (ibm->init) {
  1490. ret = ibm->init();
  1491. if (ret != 0)
  1492. return ret;
  1493. ibm->init_called = 1;
  1494. }
  1495. if (ibm->read) {
  1496. entry = create_proc_entry(ibm->name,
  1497. S_IFREG | S_IRUGO | S_IWUSR,
  1498. proc_dir);
  1499. if (!entry) {
  1500. printk(IBM_ERR "unable to create proc entry %s\n",
  1501. ibm->name);
  1502. return -ENODEV;
  1503. }
  1504. entry->owner = THIS_MODULE;
  1505. entry->data = ibm;
  1506. entry->read_proc = &dispatch_read;
  1507. if (ibm->write)
  1508. entry->write_proc = &dispatch_write;
  1509. ibm->proc_created = 1;
  1510. }
  1511. if (ibm->notify) {
  1512. ret = setup_notify(ibm);
  1513. if (ret < 0)
  1514. return ret;
  1515. ibm->notify_installed = 1;
  1516. }
  1517. return 0;
  1518. }
  1519. static void ibm_exit(struct ibm_struct *ibm)
  1520. {
  1521. if (ibm->notify_installed)
  1522. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  1523. dispatch_notify);
  1524. if (ibm->proc_created)
  1525. remove_proc_entry(ibm->name, proc_dir);
  1526. if (ibm->init_called && ibm->exit)
  1527. ibm->exit();
  1528. if (ibm->driver_registered) {
  1529. acpi_bus_unregister_driver(ibm->driver);
  1530. kfree(ibm->driver);
  1531. }
  1532. }
  1533. static void __init ibm_handle_init(char *name,
  1534. acpi_handle * handle, acpi_handle parent,
  1535. char **paths, int num_paths, char **path)
  1536. {
  1537. int i;
  1538. acpi_status status;
  1539. for (i = 0; i < num_paths; i++) {
  1540. status = acpi_get_handle(parent, paths[i], handle);
  1541. if (ACPI_SUCCESS(status)) {
  1542. *path = paths[i];
  1543. return;
  1544. }
  1545. }
  1546. *handle = NULL;
  1547. }
  1548. #define IBM_HANDLE_INIT(object) \
  1549. ibm_handle_init(#object, &object##_handle, *object##_parent, \
  1550. object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
  1551. static int set_ibm_param(const char *val, struct kernel_param *kp)
  1552. {
  1553. unsigned int i;
  1554. for (i = 0; i < ARRAY_SIZE(ibms); i++)
  1555. if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
  1556. if (strlen(val) > sizeof(ibms[i].param) - 2)
  1557. return -ENOSPC;
  1558. strcpy(ibms[i].param, val);
  1559. strcat(ibms[i].param, ",");
  1560. return 0;
  1561. }
  1562. return -EINVAL;
  1563. }
  1564. #define IBM_PARAM(feature) \
  1565. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  1566. IBM_PARAM(hotkey);
  1567. IBM_PARAM(bluetooth);
  1568. IBM_PARAM(video);
  1569. IBM_PARAM(light);
  1570. IBM_PARAM(dock);
  1571. IBM_PARAM(bay);
  1572. IBM_PARAM(cmos);
  1573. IBM_PARAM(led);
  1574. IBM_PARAM(beep);
  1575. IBM_PARAM(ecdump);
  1576. IBM_PARAM(brightness);
  1577. IBM_PARAM(volume);
  1578. IBM_PARAM(fan);
  1579. static void acpi_ibm_exit(void)
  1580. {
  1581. int i;
  1582. for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
  1583. ibm_exit(&ibms[i]);
  1584. remove_proc_entry(IBM_DIR, acpi_root_dir);
  1585. }
  1586. static int __init acpi_ibm_init(void)
  1587. {
  1588. int ret, i;
  1589. if (acpi_disabled)
  1590. return -ENODEV;
  1591. if (!acpi_specific_hotkey_enabled) {
  1592. printk(IBM_ERR "using generic hotkey driver\n");
  1593. return -ENODEV;
  1594. }
  1595. /* ec is required because many other handles are relative to it */
  1596. IBM_HANDLE_INIT(ec);
  1597. if (!ec_handle) {
  1598. printk(IBM_ERR "ec object not found\n");
  1599. return -ENODEV;
  1600. }
  1601. /* these handles are not required */
  1602. IBM_HANDLE_INIT(vid);
  1603. IBM_HANDLE_INIT(vid2);
  1604. IBM_HANDLE_INIT(ledb);
  1605. IBM_HANDLE_INIT(led);
  1606. IBM_HANDLE_INIT(hkey);
  1607. IBM_HANDLE_INIT(lght);
  1608. IBM_HANDLE_INIT(cmos);
  1609. IBM_HANDLE_INIT(dock);
  1610. IBM_HANDLE_INIT(pci);
  1611. IBM_HANDLE_INIT(bay);
  1612. if (bay_handle)
  1613. IBM_HANDLE_INIT(bay_ej);
  1614. IBM_HANDLE_INIT(bay2);
  1615. if (bay2_handle)
  1616. IBM_HANDLE_INIT(bay2_ej);
  1617. IBM_HANDLE_INIT(beep);
  1618. IBM_HANDLE_INIT(ecrd);
  1619. IBM_HANDLE_INIT(ecwr);
  1620. IBM_HANDLE_INIT(fans);
  1621. IBM_HANDLE_INIT(gfan);
  1622. IBM_HANDLE_INIT(sfan);
  1623. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  1624. if (!proc_dir) {
  1625. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  1626. return -ENODEV;
  1627. }
  1628. proc_dir->owner = THIS_MODULE;
  1629. for (i = 0; i < ARRAY_SIZE(ibms); i++) {
  1630. ret = ibm_init(&ibms[i]);
  1631. if (ret >= 0 && *ibms[i].param)
  1632. ret = ibms[i].write(ibms[i].param);
  1633. if (ret < 0) {
  1634. acpi_ibm_exit();
  1635. return ret;
  1636. }
  1637. }
  1638. return 0;
  1639. }
  1640. module_init(acpi_ibm_init);
  1641. module_exit(acpi_ibm_exit);