ACPI: thinkpad-acpi: add power-management handler capability
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob561b8864b74e241a115b8e5feeb468eda1e1f5e2
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define IBM_VERSION "0.14"
25 #define TPACPI_SYSFS_VERSION 0x000200
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 /****************************************************************************
121 ****************************************************************************
123 * ACPI Helpers and device model
125 ****************************************************************************
126 ****************************************************************************/
128 /*************************************************************************
129 * ACPI basic handles
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
154 * Misc ACPI handles
157 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
160 ); /* all others */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
165 ); /* 570 */
168 /*************************************************************************
169 * ACPI helpers
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
175 char *fmt0 = fmt;
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
197 res_type = *(fmt++);
199 params.count = 0;
200 params.pointer = &in_objs[0];
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
210 /* add more types as needed */
211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
217 va_end(ap);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
226 status = acpi_evaluate_object(handle, method, &params, resultp);
228 switch (res_type) {
229 case 'd': /* int */
230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
234 case 'v': /* void */
235 success = status == AE_OK;
236 break;
237 /* add more types as needed */
238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
248 return success;
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
253 int i;
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
263 int v;
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
274 return 1;
277 static int acpi_ec_write(int i, u8 v)
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
287 return 1;
290 static int _sta(acpi_handle handle)
292 int status;
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
297 return status;
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
302 if (!cmos_handle)
303 return -ENXIO;
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
308 return 0;
311 /*************************************************************************
312 * ACPI device model
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
319 int i;
320 acpi_status status;
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
332 return;
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
338 *handle = NULL;
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
343 struct ibm_struct *ibm = data;
345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
346 return;
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
353 acpi_status status;
354 int rc;
356 BUG_ON(!ibm->acpi);
358 if (!*ibm->acpi->handle)
359 return 0;
361 vdbg_printk(TPACPI_DBG_INIT,
362 "setting up ACPI notify for %s\n", ibm->name);
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
368 return -ENODEV;
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
386 return -ENODEV;
388 ibm->flags.acpi_notify_installed = 1;
389 return 0;
392 static int __init tpacpi_device_add(struct acpi_device *device)
394 return 0;
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
399 int rc;
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
404 BUG_ON(!ibm->acpi);
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
419 ibm->acpi->hid, rc);
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
422 } else if (!rc)
423 ibm->flags.acpi_driver_registered = 1;
425 return rc;
429 /****************************************************************************
430 ****************************************************************************
432 * Procfs Helpers
434 ****************************************************************************
435 ****************************************************************************/
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
440 struct ibm_struct *ibm = data;
441 int len;
443 if (!ibm || !ibm->read)
444 return -EINVAL;
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
459 return len;
462 static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
470 if (!ibm || !ibm->write)
471 return -EINVAL;
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
488 kfree(kernbuf);
490 return ret;
493 static char *next_cmd(char **cmds)
495 char *start = *cmds;
496 char *end;
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
501 if (!end)
502 return NULL;
504 *end = 0;
505 *cmds = end + 1;
506 return start;
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: input, hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520 static struct input_dev *tpacpi_inputdev;
523 static int tpacpi_resume_handler(struct platform_device *pdev)
525 struct ibm_struct *ibm, *itmp;
527 list_for_each_entry_safe(ibm, itmp,
528 &tpacpi_all_drivers,
529 all_drivers) {
530 if (ibm->resume)
531 (ibm->resume)();
534 return 0;
537 static struct platform_driver tpacpi_pdriver = {
538 .driver = {
539 .name = IBM_DRVR_NAME,
540 .owner = THIS_MODULE,
542 .resume = tpacpi_resume_handler,
546 /*************************************************************************
547 * thinkpad-acpi driver attributes
550 /* interface_version --------------------------------------------------- */
551 static ssize_t tpacpi_driver_interface_version_show(
552 struct device_driver *drv,
553 char *buf)
555 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
558 static DRIVER_ATTR(interface_version, S_IRUGO,
559 tpacpi_driver_interface_version_show, NULL);
561 /* debug_level --------------------------------------------------------- */
562 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
563 char *buf)
565 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
568 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
569 const char *buf, size_t count)
571 unsigned long t;
573 if (parse_strtoul(buf, 0xffff, &t))
574 return -EINVAL;
576 dbg_level = t;
578 return count;
581 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
582 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
584 /* version ------------------------------------------------------------- */
585 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
586 char *buf)
588 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
591 static DRIVER_ATTR(version, S_IRUGO,
592 tpacpi_driver_version_show, NULL);
594 /* --------------------------------------------------------------------- */
596 static struct driver_attribute* tpacpi_driver_attributes[] = {
597 &driver_attr_debug_level, &driver_attr_version,
598 &driver_attr_interface_version,
601 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
603 int i, res;
605 i = 0;
606 res = 0;
607 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
608 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
609 i++;
612 return res;
615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
617 int i;
619 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
623 /*************************************************************************
624 * sysfs support helpers
627 struct attribute_set_obj {
628 struct attribute_set s;
629 struct attribute *a;
630 } __attribute__((packed));
632 static struct attribute_set *create_attr_set(unsigned int max_members,
633 const char* name)
635 struct attribute_set_obj *sobj;
637 if (max_members == 0)
638 return NULL;
640 /* Allocates space for implicit NULL at the end too */
641 sobj = kzalloc(sizeof(struct attribute_set_obj) +
642 max_members * sizeof(struct attribute *),
643 GFP_KERNEL);
644 if (!sobj)
645 return NULL;
646 sobj->s.max_members = max_members;
647 sobj->s.group.attrs = &sobj->a;
648 sobj->s.group.name = name;
650 return &sobj->s;
653 /* not multi-threaded safe, use it in a single thread per set */
654 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
656 if (!s || !attr)
657 return -EINVAL;
659 if (s->members >= s->max_members)
660 return -ENOMEM;
662 s->group.attrs[s->members] = attr;
663 s->members++;
665 return 0;
668 static int add_many_to_attr_set(struct attribute_set* s,
669 struct attribute **attr,
670 unsigned int count)
672 int i, res;
674 for (i = 0; i < count; i++) {
675 res = add_to_attr_set(s, attr[i]);
676 if (res)
677 return res;
680 return 0;
683 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
685 sysfs_remove_group(kobj, &s->group);
686 destroy_attr_set(s);
689 static int parse_strtoul(const char *buf,
690 unsigned long max, unsigned long *value)
692 char *endp;
694 *value = simple_strtoul(buf, &endp, 0);
695 while (*endp && isspace(*endp))
696 endp++;
697 if (*endp || *value > max)
698 return -EINVAL;
700 return 0;
703 /****************************************************************************
704 ****************************************************************************
706 * Subdrivers
708 ****************************************************************************
709 ****************************************************************************/
711 /*************************************************************************
712 * thinkpad-acpi init subdriver
715 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
717 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
718 printk(IBM_INFO "%s\n", IBM_URL);
720 if (ibm_thinkpad_ec_found)
721 printk(IBM_INFO "ThinkPad EC firmware %s\n",
722 ibm_thinkpad_ec_found);
724 return 0;
727 static int thinkpad_acpi_driver_read(char *p)
729 int len = 0;
731 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
732 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
734 return len;
737 static struct ibm_struct thinkpad_acpi_driver_data = {
738 .name = "driver",
739 .read = thinkpad_acpi_driver_read,
742 /*************************************************************************
743 * Hotkey subdriver
746 static int hotkey_orig_status;
747 static u32 hotkey_orig_mask;
748 static u32 hotkey_all_mask;
749 static u32 hotkey_reserved_mask;
751 static u16 hotkey_keycode_map[] = {
752 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
753 KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
754 KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
755 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
756 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
757 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
758 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
759 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
760 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
761 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
762 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
763 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
764 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
765 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
766 KEY_RESERVED, /* 0x14: VOLUME UP */
767 KEY_RESERVED, /* 0x15: VOLUME DOWN */
768 KEY_RESERVED, /* 0x16: MUTE */
769 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
770 /* (assignments unknown, please report if found) */
771 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
772 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
775 static struct attribute_set *hotkey_dev_attributes;
777 static int hotkey_get_wlsw(int *status)
779 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
780 return -EIO;
781 return 0;
784 /* sysfs hotkey enable ------------------------------------------------- */
785 static ssize_t hotkey_enable_show(struct device *dev,
786 struct device_attribute *attr,
787 char *buf)
789 int res, status;
790 u32 mask;
792 res = hotkey_get(&status, &mask);
793 if (res)
794 return res;
796 return snprintf(buf, PAGE_SIZE, "%d\n", status);
799 static ssize_t hotkey_enable_store(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf, size_t count)
803 unsigned long t;
804 int res, status;
805 u32 mask;
807 if (parse_strtoul(buf, 1, &t))
808 return -EINVAL;
810 res = hotkey_get(&status, &mask);
811 if (!res)
812 res = hotkey_set(t, mask);
814 return (res) ? res : count;
817 static struct device_attribute dev_attr_hotkey_enable =
818 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
819 hotkey_enable_show, hotkey_enable_store);
821 /* sysfs hotkey mask --------------------------------------------------- */
822 static ssize_t hotkey_mask_show(struct device *dev,
823 struct device_attribute *attr,
824 char *buf)
826 int res, status;
827 u32 mask;
829 res = hotkey_get(&status, &mask);
830 if (res)
831 return res;
833 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
836 static ssize_t hotkey_mask_store(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf, size_t count)
840 unsigned long t;
841 int res, status;
842 u32 mask;
844 if (parse_strtoul(buf, 0xffffffffUL, &t))
845 return -EINVAL;
847 res = hotkey_get(&status, &mask);
848 if (!res)
849 hotkey_set(status, t);
851 return (res) ? res : count;
854 static struct device_attribute dev_attr_hotkey_mask =
855 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
856 hotkey_mask_show, hotkey_mask_store);
858 /* sysfs hotkey bios_enabled ------------------------------------------- */
859 static ssize_t hotkey_bios_enabled_show(struct device *dev,
860 struct device_attribute *attr,
861 char *buf)
863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
866 static struct device_attribute dev_attr_hotkey_bios_enabled =
867 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
869 /* sysfs hotkey bios_mask ---------------------------------------------- */
870 static ssize_t hotkey_bios_mask_show(struct device *dev,
871 struct device_attribute *attr,
872 char *buf)
874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
877 static struct device_attribute dev_attr_hotkey_bios_mask =
878 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
880 /* sysfs hotkey all_mask ----------------------------------------------- */
881 static ssize_t hotkey_all_mask_show(struct device *dev,
882 struct device_attribute *attr,
883 char *buf)
885 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
888 static struct device_attribute dev_attr_hotkey_all_mask =
889 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
891 /* sysfs hotkey recommended_mask --------------------------------------- */
892 static ssize_t hotkey_recommended_mask_show(struct device *dev,
893 struct device_attribute *attr,
894 char *buf)
896 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
897 hotkey_all_mask & ~hotkey_reserved_mask);
900 static struct device_attribute dev_attr_hotkey_recommended_mask =
901 __ATTR(hotkey_recommended_mask, S_IRUGO,
902 hotkey_recommended_mask_show, NULL);
904 /* sysfs hotkey radio_sw ----------------------------------------------- */
905 static ssize_t hotkey_radio_sw_show(struct device *dev,
906 struct device_attribute *attr,
907 char *buf)
909 int res, s;
910 res = hotkey_get_wlsw(&s);
911 if (res < 0)
912 return res;
914 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
917 static struct device_attribute dev_attr_hotkey_radio_sw =
918 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
920 /* --------------------------------------------------------------------- */
922 static struct attribute *hotkey_mask_attributes[] = {
923 &dev_attr_hotkey_mask.attr,
924 &dev_attr_hotkey_bios_enabled.attr,
925 &dev_attr_hotkey_bios_mask.attr,
926 &dev_attr_hotkey_all_mask.attr,
927 &dev_attr_hotkey_recommended_mask.attr,
930 static int __init hotkey_init(struct ibm_init_struct *iibm)
932 int res, i;
933 int status;
935 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
937 BUG_ON(!tpacpi_inputdev);
939 IBM_ACPIHANDLE_INIT(hkey);
940 mutex_init(&hotkey_mutex);
942 /* hotkey not supported on 570 */
943 tp_features.hotkey = hkey_handle != NULL;
945 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
946 str_supported(tp_features.hotkey));
948 if (tp_features.hotkey) {
949 hotkey_dev_attributes = create_attr_set(7, NULL);
950 if (!hotkey_dev_attributes)
951 return -ENOMEM;
952 res = add_to_attr_set(hotkey_dev_attributes,
953 &dev_attr_hotkey_enable.attr);
954 if (res)
955 return res;
957 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
958 A30, R30, R31, T20-22, X20-21, X22-24 */
959 tp_features.hotkey_mask =
960 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
962 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
963 str_supported(tp_features.hotkey_mask));
965 if (tp_features.hotkey_mask) {
966 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
967 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
968 "MHKA", "qd"))
969 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
972 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
973 if (!res && tp_features.hotkey_mask) {
974 res = add_many_to_attr_set(hotkey_dev_attributes,
975 hotkey_mask_attributes,
976 ARRAY_SIZE(hotkey_mask_attributes));
979 /* Not all thinkpads have a hardware radio switch */
980 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
981 tp_features.hotkey_wlsw = 1;
982 printk(IBM_INFO
983 "radio switch found; radios are %s\n",
984 enabled(status, 0));
985 res = add_to_attr_set(hotkey_dev_attributes,
986 &dev_attr_hotkey_radio_sw.attr);
989 if (!res)
990 res = register_attr_set_with_sysfs(
991 hotkey_dev_attributes,
992 &tpacpi_pdev->dev.kobj);
993 if (res)
994 return res;
996 #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
997 for (i = 0; i < 12; i++)
998 hotkey_keycode_map[i] = KEY_UNKNOWN;
999 #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1001 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1002 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1003 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1004 tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
1005 tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
1006 tpacpi_inputdev->keycode = &hotkey_keycode_map;
1007 for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
1008 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1009 set_bit(hotkey_keycode_map[i],
1010 tpacpi_inputdev->keybit);
1011 } else {
1012 if (i < sizeof(hotkey_reserved_mask)*8)
1013 hotkey_reserved_mask |= 1 << i;
1017 #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1018 dbg_printk(TPACPI_DBG_INIT,
1019 "enabling hot key handling\n");
1020 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1021 | hotkey_orig_mask);
1022 if (res)
1023 return res;
1024 #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1027 return (tp_features.hotkey)? 0 : 1;
1030 static void hotkey_exit(void)
1032 int res;
1034 if (tp_features.hotkey) {
1035 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1036 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1037 if (res)
1038 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1041 if (hotkey_dev_attributes) {
1042 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1043 hotkey_dev_attributes = NULL;
1047 static void tpacpi_input_send_key(unsigned int scancode,
1048 unsigned int keycode)
1050 if (keycode != KEY_RESERVED) {
1051 input_report_key(tpacpi_inputdev, keycode, 1);
1052 if (keycode == KEY_UNKNOWN)
1053 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1054 scancode);
1055 input_sync(tpacpi_inputdev);
1057 input_report_key(tpacpi_inputdev, keycode, 0);
1058 if (keycode == KEY_UNKNOWN)
1059 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1060 scancode);
1061 input_sync(tpacpi_inputdev);
1065 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1067 u32 hkey;
1068 unsigned int keycode, scancode;
1069 int sendacpi = 1;
1071 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1072 if (tpacpi_inputdev->users > 0) {
1073 switch (hkey >> 12) {
1074 case 1:
1075 /* 0x1000-0x1FFF: key presses */
1076 scancode = hkey & 0xfff;
1077 if (scancode > 0 && scancode < 0x21) {
1078 scancode--;
1079 keycode = hotkey_keycode_map[scancode];
1080 tpacpi_input_send_key(scancode, keycode);
1081 sendacpi = (keycode == KEY_RESERVED
1082 || keycode == KEY_UNKNOWN);
1083 } else {
1084 printk(IBM_ERR
1085 "hotkey 0x%04x out of range for keyboard map\n",
1086 hkey);
1088 break;
1089 case 5:
1090 /* 0x5000-0x5FFF: LID */
1091 /* we don't handle it through this path, just
1092 * eat up known LID events */
1093 if (hkey != 0x5001 && hkey != 0x5002) {
1094 printk(IBM_ERR
1095 "unknown LID-related hotkey event: 0x%04x\n",
1096 hkey);
1098 break;
1099 default:
1100 /* case 2: dock-related */
1101 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1102 /* case 3: ultra-bay related. maybe bay in dock? */
1103 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1104 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1108 if (sendacpi)
1109 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1110 } else {
1111 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1112 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1117 * Call with hotkey_mutex held
1119 static int hotkey_get(int *status, u32 *mask)
1121 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1122 return -EIO;
1124 if (tp_features.hotkey_mask)
1125 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1126 return -EIO;
1128 return 0;
1132 * Call with hotkey_mutex held
1134 static int hotkey_set(int status, u32 mask)
1136 int i;
1138 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1139 return -EIO;
1141 if (tp_features.hotkey_mask)
1142 for (i = 0; i < 32; i++) {
1143 int bit = ((1 << i) & mask) != 0;
1144 if (!acpi_evalf(hkey_handle,
1145 NULL, "MHKM", "vdd", i + 1, bit))
1146 return -EIO;
1149 return 0;
1152 /* procfs -------------------------------------------------------------- */
1153 static int hotkey_read(char *p)
1155 int res, status;
1156 u32 mask;
1157 int len = 0;
1159 if (!tp_features.hotkey) {
1160 len += sprintf(p + len, "status:\t\tnot supported\n");
1161 return len;
1164 res = mutex_lock_interruptible(&hotkey_mutex);
1165 if (res < 0)
1166 return res;
1167 res = hotkey_get(&status, &mask);
1168 mutex_unlock(&hotkey_mutex);
1169 if (res)
1170 return res;
1172 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1173 if (tp_features.hotkey_mask) {
1174 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1175 len += sprintf(p + len,
1176 "commands:\tenable, disable, reset, <mask>\n");
1177 } else {
1178 len += sprintf(p + len, "mask:\t\tnot supported\n");
1179 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1182 return len;
1185 static int hotkey_write(char *buf)
1187 int res, status;
1188 u32 mask;
1189 char *cmd;
1190 int do_cmd = 0;
1192 if (!tp_features.hotkey)
1193 return -ENODEV;
1195 res = mutex_lock_interruptible(&hotkey_mutex);
1196 if (res < 0)
1197 return res;
1199 res = hotkey_get(&status, &mask);
1200 if (res)
1201 goto errexit;
1203 res = 0;
1204 while ((cmd = next_cmd(&buf))) {
1205 if (strlencmp(cmd, "enable") == 0) {
1206 status = 1;
1207 } else if (strlencmp(cmd, "disable") == 0) {
1208 status = 0;
1209 } else if (strlencmp(cmd, "reset") == 0) {
1210 status = hotkey_orig_status;
1211 mask = hotkey_orig_mask;
1212 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1213 /* mask set */
1214 } else if (sscanf(cmd, "%x", &mask) == 1) {
1215 /* mask set */
1216 } else {
1217 res = -EINVAL;
1218 goto errexit;
1220 do_cmd = 1;
1223 if (do_cmd)
1224 res = hotkey_set(status, mask);
1226 errexit:
1227 mutex_unlock(&hotkey_mutex);
1228 return res;
1231 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1232 .hid = IBM_HKEY_HID,
1233 .notify = hotkey_notify,
1234 .handle = &hkey_handle,
1235 .type = ACPI_DEVICE_NOTIFY,
1238 static struct ibm_struct hotkey_driver_data = {
1239 .name = "hotkey",
1240 .read = hotkey_read,
1241 .write = hotkey_write,
1242 .exit = hotkey_exit,
1243 .acpi = &ibm_hotkey_acpidriver,
1246 /*************************************************************************
1247 * Bluetooth subdriver
1250 /* sysfs bluetooth enable ---------------------------------------------- */
1251 static ssize_t bluetooth_enable_show(struct device *dev,
1252 struct device_attribute *attr,
1253 char *buf)
1255 int status;
1257 status = bluetooth_get_radiosw();
1258 if (status < 0)
1259 return status;
1261 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1264 static ssize_t bluetooth_enable_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1268 unsigned long t;
1269 int res;
1271 if (parse_strtoul(buf, 1, &t))
1272 return -EINVAL;
1274 res = bluetooth_set_radiosw(t);
1276 return (res) ? res : count;
1279 static struct device_attribute dev_attr_bluetooth_enable =
1280 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1281 bluetooth_enable_show, bluetooth_enable_store);
1283 /* --------------------------------------------------------------------- */
1285 static struct attribute *bluetooth_attributes[] = {
1286 &dev_attr_bluetooth_enable.attr,
1287 NULL
1290 static const struct attribute_group bluetooth_attr_group = {
1291 .attrs = bluetooth_attributes,
1294 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1296 int res;
1297 int status = 0;
1299 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1301 IBM_ACPIHANDLE_INIT(hkey);
1303 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1304 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1305 tp_features.bluetooth = hkey_handle &&
1306 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1308 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1309 str_supported(tp_features.bluetooth),
1310 status);
1312 if (tp_features.bluetooth) {
1313 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1314 /* no bluetooth hardware present in system */
1315 tp_features.bluetooth = 0;
1316 dbg_printk(TPACPI_DBG_INIT,
1317 "bluetooth hardware not installed\n");
1318 } else {
1319 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1320 &bluetooth_attr_group);
1321 if (res)
1322 return res;
1326 return (tp_features.bluetooth)? 0 : 1;
1329 static void bluetooth_exit(void)
1331 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1332 &bluetooth_attr_group);
1335 static int bluetooth_get_radiosw(void)
1337 int status;
1339 if (!tp_features.bluetooth)
1340 return -ENODEV;
1342 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1343 return -EIO;
1345 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1348 static int bluetooth_set_radiosw(int radio_on)
1350 int status;
1352 if (!tp_features.bluetooth)
1353 return -ENODEV;
1355 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1356 return -EIO;
1357 if (radio_on)
1358 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1359 else
1360 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1361 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1362 return -EIO;
1364 return 0;
1367 /* procfs -------------------------------------------------------------- */
1368 static int bluetooth_read(char *p)
1370 int len = 0;
1371 int status = bluetooth_get_radiosw();
1373 if (!tp_features.bluetooth)
1374 len += sprintf(p + len, "status:\t\tnot supported\n");
1375 else {
1376 len += sprintf(p + len, "status:\t\t%s\n",
1377 (status)? "enabled" : "disabled");
1378 len += sprintf(p + len, "commands:\tenable, disable\n");
1381 return len;
1384 static int bluetooth_write(char *buf)
1386 char *cmd;
1388 if (!tp_features.bluetooth)
1389 return -ENODEV;
1391 while ((cmd = next_cmd(&buf))) {
1392 if (strlencmp(cmd, "enable") == 0) {
1393 bluetooth_set_radiosw(1);
1394 } else if (strlencmp(cmd, "disable") == 0) {
1395 bluetooth_set_radiosw(0);
1396 } else
1397 return -EINVAL;
1400 return 0;
1403 static struct ibm_struct bluetooth_driver_data = {
1404 .name = "bluetooth",
1405 .read = bluetooth_read,
1406 .write = bluetooth_write,
1407 .exit = bluetooth_exit,
1410 /*************************************************************************
1411 * Wan subdriver
1414 /* sysfs wan enable ---------------------------------------------------- */
1415 static ssize_t wan_enable_show(struct device *dev,
1416 struct device_attribute *attr,
1417 char *buf)
1419 int status;
1421 status = wan_get_radiosw();
1422 if (status < 0)
1423 return status;
1425 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1428 static ssize_t wan_enable_store(struct device *dev,
1429 struct device_attribute *attr,
1430 const char *buf, size_t count)
1432 unsigned long t;
1433 int res;
1435 if (parse_strtoul(buf, 1, &t))
1436 return -EINVAL;
1438 res = wan_set_radiosw(t);
1440 return (res) ? res : count;
1443 static struct device_attribute dev_attr_wan_enable =
1444 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1445 wan_enable_show, wan_enable_store);
1447 /* --------------------------------------------------------------------- */
1449 static struct attribute *wan_attributes[] = {
1450 &dev_attr_wan_enable.attr,
1451 NULL
1454 static const struct attribute_group wan_attr_group = {
1455 .attrs = wan_attributes,
1458 static int __init wan_init(struct ibm_init_struct *iibm)
1460 int res;
1461 int status = 0;
1463 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1465 IBM_ACPIHANDLE_INIT(hkey);
1467 tp_features.wan = hkey_handle &&
1468 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1470 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1471 str_supported(tp_features.wan),
1472 status);
1474 if (tp_features.wan) {
1475 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1476 /* no wan hardware present in system */
1477 tp_features.wan = 0;
1478 dbg_printk(TPACPI_DBG_INIT,
1479 "wan hardware not installed\n");
1480 } else {
1481 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1482 &wan_attr_group);
1483 if (res)
1484 return res;
1488 return (tp_features.wan)? 0 : 1;
1491 static void wan_exit(void)
1493 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1494 &wan_attr_group);
1497 static int wan_get_radiosw(void)
1499 int status;
1501 if (!tp_features.wan)
1502 return -ENODEV;
1504 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1505 return -EIO;
1507 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1510 static int wan_set_radiosw(int radio_on)
1512 int status;
1514 if (!tp_features.wan)
1515 return -ENODEV;
1517 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1518 return -EIO;
1519 if (radio_on)
1520 status |= TP_ACPI_WANCARD_RADIOSSW;
1521 else
1522 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1523 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1524 return -EIO;
1526 return 0;
1529 /* procfs -------------------------------------------------------------- */
1530 static int wan_read(char *p)
1532 int len = 0;
1533 int status = wan_get_radiosw();
1535 if (!tp_features.wan)
1536 len += sprintf(p + len, "status:\t\tnot supported\n");
1537 else {
1538 len += sprintf(p + len, "status:\t\t%s\n",
1539 (status)? "enabled" : "disabled");
1540 len += sprintf(p + len, "commands:\tenable, disable\n");
1543 return len;
1546 static int wan_write(char *buf)
1548 char *cmd;
1550 if (!tp_features.wan)
1551 return -ENODEV;
1553 while ((cmd = next_cmd(&buf))) {
1554 if (strlencmp(cmd, "enable") == 0) {
1555 wan_set_radiosw(1);
1556 } else if (strlencmp(cmd, "disable") == 0) {
1557 wan_set_radiosw(0);
1558 } else
1559 return -EINVAL;
1562 return 0;
1565 static struct ibm_struct wan_driver_data = {
1566 .name = "wan",
1567 .read = wan_read,
1568 .write = wan_write,
1569 .exit = wan_exit,
1570 .flags.experimental = 1,
1573 /*************************************************************************
1574 * Video subdriver
1577 static enum video_access_mode video_supported;
1578 static int video_orig_autosw;
1580 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1581 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1582 "\\_SB.PCI0.VID0", /* 770e */
1583 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1584 "\\_SB.PCI0.AGP.VID", /* all others */
1585 ); /* R30, R31 */
1587 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1589 static int __init video_init(struct ibm_init_struct *iibm)
1591 int ivga;
1593 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1595 IBM_ACPIHANDLE_INIT(vid);
1596 IBM_ACPIHANDLE_INIT(vid2);
1598 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1599 /* G41, assume IVGA doesn't change */
1600 vid_handle = vid2_handle;
1602 if (!vid_handle)
1603 /* video switching not supported on R30, R31 */
1604 video_supported = TPACPI_VIDEO_NONE;
1605 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1606 /* 570 */
1607 video_supported = TPACPI_VIDEO_570;
1608 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1609 /* 600e/x, 770e, 770x */
1610 video_supported = TPACPI_VIDEO_770;
1611 else
1612 /* all others */
1613 video_supported = TPACPI_VIDEO_NEW;
1615 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1616 str_supported(video_supported != TPACPI_VIDEO_NONE),
1617 video_supported);
1619 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1622 static void video_exit(void)
1624 dbg_printk(TPACPI_DBG_EXIT,
1625 "restoring original video autoswitch mode\n");
1626 if (video_autosw_set(video_orig_autosw))
1627 printk(IBM_ERR "error while trying to restore original "
1628 "video autoswitch mode\n");
1631 static int video_outputsw_get(void)
1633 int status = 0;
1634 int i;
1636 switch (video_supported) {
1637 case TPACPI_VIDEO_570:
1638 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1639 TP_ACPI_VIDEO_570_PHSCMD))
1640 return -EIO;
1641 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1642 break;
1643 case TPACPI_VIDEO_770:
1644 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1645 return -EIO;
1646 if (i)
1647 status |= TP_ACPI_VIDEO_S_LCD;
1648 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1649 return -EIO;
1650 if (i)
1651 status |= TP_ACPI_VIDEO_S_CRT;
1652 break;
1653 case TPACPI_VIDEO_NEW:
1654 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1655 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1656 return -EIO;
1657 if (i)
1658 status |= TP_ACPI_VIDEO_S_CRT;
1660 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1661 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1662 return -EIO;
1663 if (i)
1664 status |= TP_ACPI_VIDEO_S_LCD;
1665 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1666 return -EIO;
1667 if (i)
1668 status |= TP_ACPI_VIDEO_S_DVI;
1669 break;
1670 default:
1671 return -ENOSYS;
1674 return status;
1677 static int video_outputsw_set(int status)
1679 int autosw;
1680 int res = 0;
1682 switch (video_supported) {
1683 case TPACPI_VIDEO_570:
1684 res = acpi_evalf(NULL, NULL,
1685 "\\_SB.PHS2", "vdd",
1686 TP_ACPI_VIDEO_570_PHS2CMD,
1687 status | TP_ACPI_VIDEO_570_PHS2SET);
1688 break;
1689 case TPACPI_VIDEO_770:
1690 autosw = video_autosw_get();
1691 if (autosw < 0)
1692 return autosw;
1694 res = video_autosw_set(1);
1695 if (res)
1696 return res;
1697 res = acpi_evalf(vid_handle, NULL,
1698 "ASWT", "vdd", status * 0x100, 0);
1699 if (!autosw && video_autosw_set(autosw)) {
1700 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1701 return -EIO;
1703 break;
1704 case TPACPI_VIDEO_NEW:
1705 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1706 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1707 break;
1708 default:
1709 return -ENOSYS;
1712 return (res)? 0 : -EIO;
1715 static int video_autosw_get(void)
1717 int autosw = 0;
1719 switch (video_supported) {
1720 case TPACPI_VIDEO_570:
1721 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1722 return -EIO;
1723 break;
1724 case TPACPI_VIDEO_770:
1725 case TPACPI_VIDEO_NEW:
1726 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1727 return -EIO;
1728 break;
1729 default:
1730 return -ENOSYS;
1733 return autosw & 1;
1736 static int video_autosw_set(int enable)
1738 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1739 return -EIO;
1740 return 0;
1743 static int video_outputsw_cycle(void)
1745 int autosw = video_autosw_get();
1746 int res;
1748 if (autosw < 0)
1749 return autosw;
1751 switch (video_supported) {
1752 case TPACPI_VIDEO_570:
1753 res = video_autosw_set(1);
1754 if (res)
1755 return res;
1756 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1757 break;
1758 case TPACPI_VIDEO_770:
1759 case TPACPI_VIDEO_NEW:
1760 res = video_autosw_set(1);
1761 if (res)
1762 return res;
1763 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1764 break;
1765 default:
1766 return -ENOSYS;
1768 if (!autosw && video_autosw_set(autosw)) {
1769 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1770 return -EIO;
1773 return (res)? 0 : -EIO;
1776 static int video_expand_toggle(void)
1778 switch (video_supported) {
1779 case TPACPI_VIDEO_570:
1780 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1781 0 : -EIO;
1782 case TPACPI_VIDEO_770:
1783 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1784 0 : -EIO;
1785 case TPACPI_VIDEO_NEW:
1786 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1787 0 : -EIO;
1788 default:
1789 return -ENOSYS;
1791 /* not reached */
1794 static int video_read(char *p)
1796 int status, autosw;
1797 int len = 0;
1799 if (video_supported == TPACPI_VIDEO_NONE) {
1800 len += sprintf(p + len, "status:\t\tnot supported\n");
1801 return len;
1804 status = video_outputsw_get();
1805 if (status < 0)
1806 return status;
1808 autosw = video_autosw_get();
1809 if (autosw < 0)
1810 return autosw;
1812 len += sprintf(p + len, "status:\t\tsupported\n");
1813 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1814 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1815 if (video_supported == TPACPI_VIDEO_NEW)
1816 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1817 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1818 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1819 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1820 if (video_supported == TPACPI_VIDEO_NEW)
1821 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1822 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1823 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1825 return len;
1828 static int video_write(char *buf)
1830 char *cmd;
1831 int enable, disable, status;
1832 int res;
1834 if (video_supported == TPACPI_VIDEO_NONE)
1835 return -ENODEV;
1837 enable = 0;
1838 disable = 0;
1840 while ((cmd = next_cmd(&buf))) {
1841 if (strlencmp(cmd, "lcd_enable") == 0) {
1842 enable |= TP_ACPI_VIDEO_S_LCD;
1843 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1844 disable |= TP_ACPI_VIDEO_S_LCD;
1845 } else if (strlencmp(cmd, "crt_enable") == 0) {
1846 enable |= TP_ACPI_VIDEO_S_CRT;
1847 } else if (strlencmp(cmd, "crt_disable") == 0) {
1848 disable |= TP_ACPI_VIDEO_S_CRT;
1849 } else if (video_supported == TPACPI_VIDEO_NEW &&
1850 strlencmp(cmd, "dvi_enable") == 0) {
1851 enable |= TP_ACPI_VIDEO_S_DVI;
1852 } else if (video_supported == TPACPI_VIDEO_NEW &&
1853 strlencmp(cmd, "dvi_disable") == 0) {
1854 disable |= TP_ACPI_VIDEO_S_DVI;
1855 } else if (strlencmp(cmd, "auto_enable") == 0) {
1856 res = video_autosw_set(1);
1857 if (res)
1858 return res;
1859 } else if (strlencmp(cmd, "auto_disable") == 0) {
1860 res = video_autosw_set(0);
1861 if (res)
1862 return res;
1863 } else if (strlencmp(cmd, "video_switch") == 0) {
1864 res = video_outputsw_cycle();
1865 if (res)
1866 return res;
1867 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1868 res = video_expand_toggle();
1869 if (res)
1870 return res;
1871 } else
1872 return -EINVAL;
1875 if (enable || disable) {
1876 status = video_outputsw_get();
1877 if (status < 0)
1878 return status;
1879 res = video_outputsw_set((status & ~disable) | enable);
1880 if (res)
1881 return res;
1884 return 0;
1887 static struct ibm_struct video_driver_data = {
1888 .name = "video",
1889 .read = video_read,
1890 .write = video_write,
1891 .exit = video_exit,
1894 /*************************************************************************
1895 * Light (thinklight) subdriver
1898 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1899 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1901 static int __init light_init(struct ibm_init_struct *iibm)
1903 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1905 IBM_ACPIHANDLE_INIT(ledb);
1906 IBM_ACPIHANDLE_INIT(lght);
1907 IBM_ACPIHANDLE_INIT(cmos);
1909 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1910 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1912 if (tp_features.light)
1913 /* light status not supported on
1914 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1915 tp_features.light_status =
1916 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1918 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1919 str_supported(tp_features.light));
1921 return (tp_features.light)? 0 : 1;
1924 static int light_read(char *p)
1926 int len = 0;
1927 int status = 0;
1929 if (!tp_features.light) {
1930 len += sprintf(p + len, "status:\t\tnot supported\n");
1931 } else if (!tp_features.light_status) {
1932 len += sprintf(p + len, "status:\t\tunknown\n");
1933 len += sprintf(p + len, "commands:\ton, off\n");
1934 } else {
1935 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1936 return -EIO;
1937 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1938 len += sprintf(p + len, "commands:\ton, off\n");
1941 return len;
1944 static int light_write(char *buf)
1946 int cmos_cmd, lght_cmd;
1947 char *cmd;
1948 int success;
1950 if (!tp_features.light)
1951 return -ENODEV;
1953 while ((cmd = next_cmd(&buf))) {
1954 if (strlencmp(cmd, "on") == 0) {
1955 cmos_cmd = 0x0c;
1956 lght_cmd = 1;
1957 } else if (strlencmp(cmd, "off") == 0) {
1958 cmos_cmd = 0x0d;
1959 lght_cmd = 0;
1960 } else
1961 return -EINVAL;
1963 success = cmos_handle ?
1964 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1965 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1966 if (!success)
1967 return -EIO;
1970 return 0;
1973 static struct ibm_struct light_driver_data = {
1974 .name = "light",
1975 .read = light_read,
1976 .write = light_write,
1979 /*************************************************************************
1980 * Dock subdriver
1983 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1985 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1986 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1987 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1988 "\\_SB.PCI.ISA.SLCE", /* 570 */
1989 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1991 /* don't list other alternatives as we install a notify handler on the 570 */
1992 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1994 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1996 .notify = dock_notify,
1997 .handle = &dock_handle,
1998 .type = ACPI_SYSTEM_NOTIFY,
2001 .hid = IBM_PCI_HID,
2002 .notify = dock_notify,
2003 .handle = &pci_handle,
2004 .type = ACPI_SYSTEM_NOTIFY,
2008 static struct ibm_struct dock_driver_data[2] = {
2010 .name = "dock",
2011 .read = dock_read,
2012 .write = dock_write,
2013 .acpi = &ibm_dock_acpidriver[0],
2016 .name = "dock",
2017 .acpi = &ibm_dock_acpidriver[1],
2021 #define dock_docked() (_sta(dock_handle) & 1)
2023 static int __init dock_init(struct ibm_init_struct *iibm)
2025 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2027 IBM_ACPIHANDLE_INIT(dock);
2029 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2030 str_supported(dock_handle != NULL));
2032 return (dock_handle)? 0 : 1;
2035 static int __init dock_init2(struct ibm_init_struct *iibm)
2037 int dock2_needed;
2039 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2041 if (dock_driver_data[0].flags.acpi_driver_registered &&
2042 dock_driver_data[0].flags.acpi_notify_installed) {
2043 IBM_ACPIHANDLE_INIT(pci);
2044 dock2_needed = (pci_handle != NULL);
2045 vdbg_printk(TPACPI_DBG_INIT,
2046 "dock PCI handler for the TP 570 is %s\n",
2047 str_supported(dock2_needed));
2048 } else {
2049 vdbg_printk(TPACPI_DBG_INIT,
2050 "dock subdriver part 2 not required\n");
2051 dock2_needed = 0;
2054 return (dock2_needed)? 0 : 1;
2057 static void dock_notify(struct ibm_struct *ibm, u32 event)
2059 int docked = dock_docked();
2060 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
2062 if (event == 1 && !pci) /* 570 */
2063 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2064 else if (event == 1 && pci) /* 570 */
2065 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2066 else if (event == 3 && docked)
2067 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2068 else if (event == 3 && !docked)
2069 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2070 else if (event == 0 && docked)
2071 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2072 else {
2073 printk(IBM_ERR "unknown dock event %d, status %d\n",
2074 event, _sta(dock_handle));
2075 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2079 static int dock_read(char *p)
2081 int len = 0;
2082 int docked = dock_docked();
2084 if (!dock_handle)
2085 len += sprintf(p + len, "status:\t\tnot supported\n");
2086 else if (!docked)
2087 len += sprintf(p + len, "status:\t\tundocked\n");
2088 else {
2089 len += sprintf(p + len, "status:\t\tdocked\n");
2090 len += sprintf(p + len, "commands:\tdock, undock\n");
2093 return len;
2096 static int dock_write(char *buf)
2098 char *cmd;
2100 if (!dock_docked())
2101 return -ENODEV;
2103 while ((cmd = next_cmd(&buf))) {
2104 if (strlencmp(cmd, "undock") == 0) {
2105 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2106 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2107 return -EIO;
2108 } else if (strlencmp(cmd, "dock") == 0) {
2109 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2110 return -EIO;
2111 } else
2112 return -EINVAL;
2115 return 0;
2118 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2120 /*************************************************************************
2121 * Bay subdriver
2124 #ifdef CONFIG_THINKPAD_ACPI_BAY
2125 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2126 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2127 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2128 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2129 ); /* A21e, R30, R31 */
2130 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2131 "_EJ0", /* all others */
2132 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2133 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2134 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2135 ); /* all others */
2136 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2137 "_EJ0", /* 770x */
2138 ); /* all others */
2140 static int __init bay_init(struct ibm_init_struct *iibm)
2142 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2144 IBM_ACPIHANDLE_INIT(bay);
2145 if (bay_handle)
2146 IBM_ACPIHANDLE_INIT(bay_ej);
2147 IBM_ACPIHANDLE_INIT(bay2);
2148 if (bay2_handle)
2149 IBM_ACPIHANDLE_INIT(bay2_ej);
2151 tp_features.bay_status = bay_handle &&
2152 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2153 tp_features.bay_status2 = bay2_handle &&
2154 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2156 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2157 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2158 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2159 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2161 vdbg_printk(TPACPI_DBG_INIT,
2162 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2163 str_supported(tp_features.bay_status),
2164 str_supported(tp_features.bay_eject),
2165 str_supported(tp_features.bay_status2),
2166 str_supported(tp_features.bay_eject2));
2168 return (tp_features.bay_status || tp_features.bay_eject ||
2169 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2172 static void bay_notify(struct ibm_struct *ibm, u32 event)
2174 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2177 #define bay_occupied(b) (_sta(b##_handle) & 1)
2179 static int bay_read(char *p)
2181 int len = 0;
2182 int occupied = bay_occupied(bay);
2183 int occupied2 = bay_occupied(bay2);
2184 int eject, eject2;
2186 len += sprintf(p + len, "status:\t\t%s\n",
2187 tp_features.bay_status ?
2188 (occupied ? "occupied" : "unoccupied") :
2189 "not supported");
2190 if (tp_features.bay_status2)
2191 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2192 "occupied" : "unoccupied");
2194 eject = tp_features.bay_eject && occupied;
2195 eject2 = tp_features.bay_eject2 && occupied2;
2197 if (eject && eject2)
2198 len += sprintf(p + len, "commands:\teject, eject2\n");
2199 else if (eject)
2200 len += sprintf(p + len, "commands:\teject\n");
2201 else if (eject2)
2202 len += sprintf(p + len, "commands:\teject2\n");
2204 return len;
2207 static int bay_write(char *buf)
2209 char *cmd;
2211 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2212 return -ENODEV;
2214 while ((cmd = next_cmd(&buf))) {
2215 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2216 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2217 return -EIO;
2218 } else if (tp_features.bay_eject2 &&
2219 strlencmp(cmd, "eject2") == 0) {
2220 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2221 return -EIO;
2222 } else
2223 return -EINVAL;
2226 return 0;
2229 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2230 .notify = bay_notify,
2231 .handle = &bay_handle,
2232 .type = ACPI_SYSTEM_NOTIFY,
2235 static struct ibm_struct bay_driver_data = {
2236 .name = "bay",
2237 .read = bay_read,
2238 .write = bay_write,
2239 .acpi = &ibm_bay_acpidriver,
2242 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2244 /*************************************************************************
2245 * CMOS subdriver
2248 /* sysfs cmos_command -------------------------------------------------- */
2249 static ssize_t cmos_command_store(struct device *dev,
2250 struct device_attribute *attr,
2251 const char *buf, size_t count)
2253 unsigned long cmos_cmd;
2254 int res;
2256 if (parse_strtoul(buf, 21, &cmos_cmd))
2257 return -EINVAL;
2259 res = issue_thinkpad_cmos_command(cmos_cmd);
2260 return (res)? res : count;
2263 static struct device_attribute dev_attr_cmos_command =
2264 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2266 /* --------------------------------------------------------------------- */
2268 static int __init cmos_init(struct ibm_init_struct *iibm)
2270 int res;
2272 vdbg_printk(TPACPI_DBG_INIT,
2273 "initializing cmos commands subdriver\n");
2275 IBM_ACPIHANDLE_INIT(cmos);
2277 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2278 str_supported(cmos_handle != NULL));
2280 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2281 if (res)
2282 return res;
2284 return (cmos_handle)? 0 : 1;
2287 static void cmos_exit(void)
2289 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2292 static int cmos_read(char *p)
2294 int len = 0;
2296 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2297 R30, R31, T20-22, X20-21 */
2298 if (!cmos_handle)
2299 len += sprintf(p + len, "status:\t\tnot supported\n");
2300 else {
2301 len += sprintf(p + len, "status:\t\tsupported\n");
2302 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2305 return len;
2308 static int cmos_write(char *buf)
2310 char *cmd;
2311 int cmos_cmd, res;
2313 while ((cmd = next_cmd(&buf))) {
2314 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2315 cmos_cmd >= 0 && cmos_cmd <= 21) {
2316 /* cmos_cmd set */
2317 } else
2318 return -EINVAL;
2320 res = issue_thinkpad_cmos_command(cmos_cmd);
2321 if (res)
2322 return res;
2325 return 0;
2328 static struct ibm_struct cmos_driver_data = {
2329 .name = "cmos",
2330 .read = cmos_read,
2331 .write = cmos_write,
2332 .exit = cmos_exit,
2335 /*************************************************************************
2336 * LED subdriver
2339 static enum led_access_mode led_supported;
2341 IBM_HANDLE(led, ec, "SLED", /* 570 */
2342 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2343 "LED", /* all others */
2344 ); /* R30, R31 */
2346 static int __init led_init(struct ibm_init_struct *iibm)
2348 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2350 IBM_ACPIHANDLE_INIT(led);
2352 if (!led_handle)
2353 /* led not supported on R30, R31 */
2354 led_supported = TPACPI_LED_NONE;
2355 else if (strlencmp(led_path, "SLED") == 0)
2356 /* 570 */
2357 led_supported = TPACPI_LED_570;
2358 else if (strlencmp(led_path, "SYSL") == 0)
2359 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2360 led_supported = TPACPI_LED_OLD;
2361 else
2362 /* all others */
2363 led_supported = TPACPI_LED_NEW;
2365 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2366 str_supported(led_supported), led_supported);
2368 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2371 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2373 static int led_read(char *p)
2375 int len = 0;
2377 if (!led_supported) {
2378 len += sprintf(p + len, "status:\t\tnot supported\n");
2379 return len;
2381 len += sprintf(p + len, "status:\t\tsupported\n");
2383 if (led_supported == TPACPI_LED_570) {
2384 /* 570 */
2385 int i, status;
2386 for (i = 0; i < 8; i++) {
2387 if (!acpi_evalf(ec_handle,
2388 &status, "GLED", "dd", 1 << i))
2389 return -EIO;
2390 len += sprintf(p + len, "%d:\t\t%s\n",
2391 i, led_status(status));
2395 len += sprintf(p + len, "commands:\t"
2396 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2398 return len;
2401 /* off, on, blink */
2402 static const int led_sled_arg1[] = { 0, 1, 3 };
2403 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2404 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2405 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2407 static int led_write(char *buf)
2409 char *cmd;
2410 int led, ind, ret;
2412 if (!led_supported)
2413 return -ENODEV;
2415 while ((cmd = next_cmd(&buf))) {
2416 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2417 return -EINVAL;
2419 if (strstr(cmd, "off")) {
2420 ind = 0;
2421 } else if (strstr(cmd, "on")) {
2422 ind = 1;
2423 } else if (strstr(cmd, "blink")) {
2424 ind = 2;
2425 } else
2426 return -EINVAL;
2428 if (led_supported == TPACPI_LED_570) {
2429 /* 570 */
2430 led = 1 << led;
2431 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2432 led, led_sled_arg1[ind]))
2433 return -EIO;
2434 } else if (led_supported == TPACPI_LED_OLD) {
2435 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2436 led = 1 << led;
2437 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2438 if (ret >= 0)
2439 ret =
2440 ec_write(TPACPI_LED_EC_HLBL,
2441 led * led_exp_hlbl[ind]);
2442 if (ret >= 0)
2443 ret =
2444 ec_write(TPACPI_LED_EC_HLCL,
2445 led * led_exp_hlcl[ind]);
2446 if (ret < 0)
2447 return ret;
2448 } else {
2449 /* all others */
2450 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2451 led, led_led_arg1[ind]))
2452 return -EIO;
2456 return 0;
2459 static struct ibm_struct led_driver_data = {
2460 .name = "led",
2461 .read = led_read,
2462 .write = led_write,
2465 /*************************************************************************
2466 * Beep subdriver
2469 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2471 static int __init beep_init(struct ibm_init_struct *iibm)
2473 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2475 IBM_ACPIHANDLE_INIT(beep);
2477 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2478 str_supported(beep_handle != NULL));
2480 return (beep_handle)? 0 : 1;
2483 static int beep_read(char *p)
2485 int len = 0;
2487 if (!beep_handle)
2488 len += sprintf(p + len, "status:\t\tnot supported\n");
2489 else {
2490 len += sprintf(p + len, "status:\t\tsupported\n");
2491 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2494 return len;
2497 static int beep_write(char *buf)
2499 char *cmd;
2500 int beep_cmd;
2502 if (!beep_handle)
2503 return -ENODEV;
2505 while ((cmd = next_cmd(&buf))) {
2506 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2507 beep_cmd >= 0 && beep_cmd <= 17) {
2508 /* beep_cmd set */
2509 } else
2510 return -EINVAL;
2511 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2512 return -EIO;
2515 return 0;
2518 static struct ibm_struct beep_driver_data = {
2519 .name = "beep",
2520 .read = beep_read,
2521 .write = beep_write,
2524 /*************************************************************************
2525 * Thermal subdriver
2528 static enum thermal_access_mode thermal_read_mode;
2530 /* sysfs temp##_input -------------------------------------------------- */
2532 static ssize_t thermal_temp_input_show(struct device *dev,
2533 struct device_attribute *attr,
2534 char *buf)
2536 struct sensor_device_attribute *sensor_attr =
2537 to_sensor_dev_attr(attr);
2538 int idx = sensor_attr->index;
2539 s32 value;
2540 int res;
2542 res = thermal_get_sensor(idx, &value);
2543 if (res)
2544 return res;
2545 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2546 return -ENXIO;
2548 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2551 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2552 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2554 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2555 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2556 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2557 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2558 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2559 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2560 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2561 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2562 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2563 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2564 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2565 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2566 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2567 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2568 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2569 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2570 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2573 #define THERMAL_ATTRS(X) \
2574 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2576 static struct attribute *thermal_temp_input_attr[] = {
2577 THERMAL_ATTRS(8),
2578 THERMAL_ATTRS(9),
2579 THERMAL_ATTRS(10),
2580 THERMAL_ATTRS(11),
2581 THERMAL_ATTRS(12),
2582 THERMAL_ATTRS(13),
2583 THERMAL_ATTRS(14),
2584 THERMAL_ATTRS(15),
2585 THERMAL_ATTRS(0),
2586 THERMAL_ATTRS(1),
2587 THERMAL_ATTRS(2),
2588 THERMAL_ATTRS(3),
2589 THERMAL_ATTRS(4),
2590 THERMAL_ATTRS(5),
2591 THERMAL_ATTRS(6),
2592 THERMAL_ATTRS(7),
2593 NULL
2596 static const struct attribute_group thermal_temp_input16_group = {
2597 .attrs = thermal_temp_input_attr
2600 static const struct attribute_group thermal_temp_input8_group = {
2601 .attrs = &thermal_temp_input_attr[8]
2604 #undef THERMAL_SENSOR_ATTR_TEMP
2605 #undef THERMAL_ATTRS
2607 /* --------------------------------------------------------------------- */
2609 static int __init thermal_init(struct ibm_init_struct *iibm)
2611 u8 t, ta1, ta2;
2612 int i;
2613 int acpi_tmp7;
2614 int res;
2616 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2618 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2620 if (ibm_thinkpad_ec_found && experimental) {
2622 * Direct EC access mode: sensors at registers
2623 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2624 * non-implemented, thermal sensors return 0x80 when
2625 * not available
2628 ta1 = ta2 = 0;
2629 for (i = 0; i < 8; i++) {
2630 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2631 ta1 |= t;
2632 } else {
2633 ta1 = 0;
2634 break;
2636 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2637 ta2 |= t;
2638 } else {
2639 ta1 = 0;
2640 break;
2643 if (ta1 == 0) {
2644 /* This is sheer paranoia, but we handle it anyway */
2645 if (acpi_tmp7) {
2646 printk(IBM_ERR
2647 "ThinkPad ACPI EC access misbehaving, "
2648 "falling back to ACPI TMPx access mode\n");
2649 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2650 } else {
2651 printk(IBM_ERR
2652 "ThinkPad ACPI EC access misbehaving, "
2653 "disabling thermal sensors access\n");
2654 thermal_read_mode = TPACPI_THERMAL_NONE;
2656 } else {
2657 thermal_read_mode =
2658 (ta2 != 0) ?
2659 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2661 } else if (acpi_tmp7) {
2662 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2663 /* 600e/x, 770e, 770x */
2664 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2665 } else {
2666 /* Standard ACPI TMPx access, max 8 sensors */
2667 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2669 } else {
2670 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2671 thermal_read_mode = TPACPI_THERMAL_NONE;
2674 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2675 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2676 thermal_read_mode);
2678 switch(thermal_read_mode) {
2679 case TPACPI_THERMAL_TPEC_16:
2680 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2681 &thermal_temp_input16_group);
2682 if (res)
2683 return res;
2684 break;
2685 case TPACPI_THERMAL_TPEC_8:
2686 case TPACPI_THERMAL_ACPI_TMP07:
2687 case TPACPI_THERMAL_ACPI_UPDT:
2688 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2689 &thermal_temp_input8_group);
2690 if (res)
2691 return res;
2692 break;
2693 case TPACPI_THERMAL_NONE:
2694 default:
2695 return 1;
2698 return 0;
2701 static void thermal_exit(void)
2703 switch(thermal_read_mode) {
2704 case TPACPI_THERMAL_TPEC_16:
2705 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2706 &thermal_temp_input16_group);
2707 break;
2708 case TPACPI_THERMAL_TPEC_8:
2709 case TPACPI_THERMAL_ACPI_TMP07:
2710 case TPACPI_THERMAL_ACPI_UPDT:
2711 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2712 &thermal_temp_input16_group);
2713 break;
2714 case TPACPI_THERMAL_NONE:
2715 default:
2716 break;
2720 /* idx is zero-based */
2721 static int thermal_get_sensor(int idx, s32 *value)
2723 int t;
2724 s8 tmp;
2725 char tmpi[5];
2727 t = TP_EC_THERMAL_TMP0;
2729 switch (thermal_read_mode) {
2730 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2731 case TPACPI_THERMAL_TPEC_16:
2732 if (idx >= 8 && idx <= 15) {
2733 t = TP_EC_THERMAL_TMP8;
2734 idx -= 8;
2736 /* fallthrough */
2737 #endif
2738 case TPACPI_THERMAL_TPEC_8:
2739 if (idx <= 7) {
2740 if (!acpi_ec_read(t + idx, &tmp))
2741 return -EIO;
2742 *value = tmp * 1000;
2743 return 0;
2745 break;
2747 case TPACPI_THERMAL_ACPI_UPDT:
2748 if (idx <= 7) {
2749 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2750 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2751 return -EIO;
2752 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2753 return -EIO;
2754 *value = (t - 2732) * 100;
2755 return 0;
2757 break;
2759 case TPACPI_THERMAL_ACPI_TMP07:
2760 if (idx <= 7) {
2761 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2762 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2763 return -EIO;
2764 *value = t * 1000;
2765 return 0;
2767 break;
2769 case TPACPI_THERMAL_NONE:
2770 default:
2771 return -ENOSYS;
2774 return -EINVAL;
2777 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2779 int res, i;
2780 int n;
2782 n = 8;
2783 i = 0;
2785 if (!s)
2786 return -EINVAL;
2788 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2789 n = 16;
2791 for(i = 0 ; i < n; i++) {
2792 res = thermal_get_sensor(i, &s->temp[i]);
2793 if (res)
2794 return res;
2797 return n;
2800 static int thermal_read(char *p)
2802 int len = 0;
2803 int n, i;
2804 struct ibm_thermal_sensors_struct t;
2806 n = thermal_get_sensors(&t);
2807 if (unlikely(n < 0))
2808 return n;
2810 len += sprintf(p + len, "temperatures:\t");
2812 if (n > 0) {
2813 for (i = 0; i < (n - 1); i++)
2814 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2815 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2816 } else
2817 len += sprintf(p + len, "not supported\n");
2819 return len;
2822 static struct ibm_struct thermal_driver_data = {
2823 .name = "thermal",
2824 .read = thermal_read,
2825 .exit = thermal_exit,
2828 /*************************************************************************
2829 * EC Dump subdriver
2832 static u8 ecdump_regs[256];
2834 static int ecdump_read(char *p)
2836 int len = 0;
2837 int i, j;
2838 u8 v;
2840 len += sprintf(p + len, "EC "
2841 " +00 +01 +02 +03 +04 +05 +06 +07"
2842 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2843 for (i = 0; i < 256; i += 16) {
2844 len += sprintf(p + len, "EC 0x%02x:", i);
2845 for (j = 0; j < 16; j++) {
2846 if (!acpi_ec_read(i + j, &v))
2847 break;
2848 if (v != ecdump_regs[i + j])
2849 len += sprintf(p + len, " *%02x", v);
2850 else
2851 len += sprintf(p + len, " %02x", v);
2852 ecdump_regs[i + j] = v;
2854 len += sprintf(p + len, "\n");
2855 if (j != 16)
2856 break;
2859 /* These are way too dangerous to advertise openly... */
2860 #if 0
2861 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2862 " (<offset> is 00-ff, <value> is 00-ff)\n");
2863 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2864 " (<offset> is 00-ff, <value> is 0-255)\n");
2865 #endif
2866 return len;
2869 static int ecdump_write(char *buf)
2871 char *cmd;
2872 int i, v;
2874 while ((cmd = next_cmd(&buf))) {
2875 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2876 /* i and v set */
2877 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2878 /* i and v set */
2879 } else
2880 return -EINVAL;
2881 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2882 if (!acpi_ec_write(i, v))
2883 return -EIO;
2884 } else
2885 return -EINVAL;
2888 return 0;
2891 static struct ibm_struct ecdump_driver_data = {
2892 .name = "ecdump",
2893 .read = ecdump_read,
2894 .write = ecdump_write,
2895 .flags.experimental = 1,
2898 /*************************************************************************
2899 * Backlight/brightness subdriver
2902 static struct backlight_device *ibm_backlight_device;
2904 static struct backlight_properties ibm_backlight_data = {
2905 .owner = THIS_MODULE,
2906 .get_brightness = brightness_get,
2907 .update_status = brightness_update_status,
2908 .max_brightness = 7,
2911 static int __init brightness_init(struct ibm_init_struct *iibm)
2913 int b;
2915 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2917 b = brightness_get(NULL);
2918 if (b < 0)
2919 return b;
2921 ibm_backlight_device = backlight_device_register(
2922 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2923 &ibm_backlight_data);
2924 if (IS_ERR(ibm_backlight_device)) {
2925 printk(IBM_ERR "Could not register backlight device\n");
2926 return PTR_ERR(ibm_backlight_device);
2928 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2930 ibm_backlight_device->props->brightness = b;
2931 brightness_update_status(ibm_backlight_device);
2933 return 0;
2936 static void brightness_exit(void)
2938 if (ibm_backlight_device) {
2939 vdbg_printk(TPACPI_DBG_EXIT,
2940 "calling backlight_device_unregister()\n");
2941 backlight_device_unregister(ibm_backlight_device);
2942 ibm_backlight_device = NULL;
2946 static int brightness_update_status(struct backlight_device *bd)
2948 return brightness_set(
2949 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
2950 bd->props->power == FB_BLANK_UNBLANK) ?
2951 bd->props->brightness : 0);
2954 static int brightness_get(struct backlight_device *bd)
2956 u8 level;
2957 if (!acpi_ec_read(brightness_offset, &level))
2958 return -EIO;
2960 level &= 0x7;
2962 return level;
2965 static int brightness_set(int value)
2967 int cmos_cmd, inc, i;
2968 int current_value = brightness_get(NULL);
2970 value &= 7;
2972 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2973 inc = value > current_value ? 1 : -1;
2974 for (i = current_value; i != value; i += inc) {
2975 if (issue_thinkpad_cmos_command(cmos_cmd))
2976 return -EIO;
2977 if (!acpi_ec_write(brightness_offset, i + inc))
2978 return -EIO;
2981 return 0;
2984 static int brightness_read(char *p)
2986 int len = 0;
2987 int level;
2989 if ((level = brightness_get(NULL)) < 0) {
2990 len += sprintf(p + len, "level:\t\tunreadable\n");
2991 } else {
2992 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2993 len += sprintf(p + len, "commands:\tup, down\n");
2994 len += sprintf(p + len, "commands:\tlevel <level>"
2995 " (<level> is 0-7)\n");
2998 return len;
3001 static int brightness_write(char *buf)
3003 int level;
3004 int new_level;
3005 char *cmd;
3007 while ((cmd = next_cmd(&buf))) {
3008 if ((level = brightness_get(NULL)) < 0)
3009 return level;
3010 level &= 7;
3012 if (strlencmp(cmd, "up") == 0) {
3013 new_level = level == 7 ? 7 : level + 1;
3014 } else if (strlencmp(cmd, "down") == 0) {
3015 new_level = level == 0 ? 0 : level - 1;
3016 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3017 new_level >= 0 && new_level <= 7) {
3018 /* new_level set */
3019 } else
3020 return -EINVAL;
3022 brightness_set(new_level);
3025 return 0;
3028 static struct ibm_struct brightness_driver_data = {
3029 .name = "brightness",
3030 .read = brightness_read,
3031 .write = brightness_write,
3032 .exit = brightness_exit,
3035 /*************************************************************************
3036 * Volume subdriver
3039 static int volume_read(char *p)
3041 int len = 0;
3042 u8 level;
3044 if (!acpi_ec_read(volume_offset, &level)) {
3045 len += sprintf(p + len, "level:\t\tunreadable\n");
3046 } else {
3047 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3048 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3049 len += sprintf(p + len, "commands:\tup, down, mute\n");
3050 len += sprintf(p + len, "commands:\tlevel <level>"
3051 " (<level> is 0-15)\n");
3054 return len;
3057 static int volume_write(char *buf)
3059 int cmos_cmd, inc, i;
3060 u8 level, mute;
3061 int new_level, new_mute;
3062 char *cmd;
3064 while ((cmd = next_cmd(&buf))) {
3065 if (!acpi_ec_read(volume_offset, &level))
3066 return -EIO;
3067 new_mute = mute = level & 0x40;
3068 new_level = level = level & 0xf;
3070 if (strlencmp(cmd, "up") == 0) {
3071 if (mute)
3072 new_mute = 0;
3073 else
3074 new_level = level == 15 ? 15 : level + 1;
3075 } else if (strlencmp(cmd, "down") == 0) {
3076 if (mute)
3077 new_mute = 0;
3078 else
3079 new_level = level == 0 ? 0 : level - 1;
3080 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3081 new_level >= 0 && new_level <= 15) {
3082 /* new_level set */
3083 } else if (strlencmp(cmd, "mute") == 0) {
3084 new_mute = 0x40;
3085 } else
3086 return -EINVAL;
3088 if (new_level != level) { /* mute doesn't change */
3089 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3090 inc = new_level > level ? 1 : -1;
3092 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3093 !acpi_ec_write(volume_offset, level)))
3094 return -EIO;
3096 for (i = level; i != new_level; i += inc)
3097 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3098 !acpi_ec_write(volume_offset, i + inc))
3099 return -EIO;
3101 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3102 !acpi_ec_write(volume_offset,
3103 new_level + mute)))
3104 return -EIO;
3107 if (new_mute != mute) { /* level doesn't change */
3108 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3110 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3111 !acpi_ec_write(volume_offset, level + new_mute))
3112 return -EIO;
3116 return 0;
3119 static struct ibm_struct volume_driver_data = {
3120 .name = "volume",
3121 .read = volume_read,
3122 .write = volume_write,
3125 /*************************************************************************
3126 * Fan subdriver
3130 * FAN ACCESS MODES
3132 * TPACPI_FAN_RD_ACPI_GFAN:
3133 * ACPI GFAN method: returns fan level
3135 * see TPACPI_FAN_WR_ACPI_SFAN
3136 * EC 0x2f (HFSP) not available if GFAN exists
3138 * TPACPI_FAN_WR_ACPI_SFAN:
3139 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3141 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3142 * it for writing.
3144 * TPACPI_FAN_WR_TPEC:
3145 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3146 * Supported on almost all ThinkPads
3148 * Fan speed changes of any sort (including those caused by the
3149 * disengaged mode) are usually done slowly by the firmware as the
3150 * maximum ammount of fan duty cycle change per second seems to be
3151 * limited.
3153 * Reading is not available if GFAN exists.
3154 * Writing is not available if SFAN exists.
3156 * Bits
3157 * 7 automatic mode engaged;
3158 * (default operation mode of the ThinkPad)
3159 * fan level is ignored in this mode.
3160 * 6 full speed mode (takes precedence over bit 7);
3161 * not available on all thinkpads. May disable
3162 * the tachometer while the fan controller ramps up
3163 * the speed (which can take up to a few *minutes*).
3164 * Speeds up fan to 100% duty-cycle, which is far above
3165 * the standard RPM levels. It is not impossible that
3166 * it could cause hardware damage.
3167 * 5-3 unused in some models. Extra bits for fan level
3168 * in others, but still useless as all values above
3169 * 7 map to the same speed as level 7 in these models.
3170 * 2-0 fan level (0..7 usually)
3171 * 0x00 = stop
3172 * 0x07 = max (set when temperatures critical)
3173 * Some ThinkPads may have other levels, see
3174 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3176 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3177 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3178 * does so, its initial value is meaningless (0x07).
3180 * For firmware bugs, refer to:
3181 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3183 * ----
3185 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3186 * Main fan tachometer reading (in RPM)
3188 * This register is present on all ThinkPads with a new-style EC, and
3189 * it is known not to be present on the A21m/e, and T22, as there is
3190 * something else in offset 0x84 according to the ACPI DSDT. Other
3191 * ThinkPads from this same time period (and earlier) probably lack the
3192 * tachometer as well.
3194 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3195 * was never fixed by IBM to report the EC firmware version string
3196 * probably support the tachometer (like the early X models), so
3197 * detecting it is quite hard. We need more data to know for sure.
3199 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3200 * might result.
3202 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3203 * mode.
3205 * For firmware bugs, refer to:
3206 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3208 * TPACPI_FAN_WR_ACPI_FANS:
3209 * ThinkPad X31, X40, X41. Not available in the X60.
3211 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3212 * high speed. ACPI DSDT seems to map these three speeds to levels
3213 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3214 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3216 * The speeds are stored on handles
3217 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3219 * There are three default speed sets, acessible as handles:
3220 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3222 * ACPI DSDT switches which set is in use depending on various
3223 * factors.
3225 * TPACPI_FAN_WR_TPEC is also available and should be used to
3226 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3227 * but the ACPI tables just mention level 7.
3230 static enum fan_status_access_mode fan_status_access_mode;
3231 static enum fan_control_access_mode fan_control_access_mode;
3232 static enum fan_control_commands fan_control_commands;
3234 static u8 fan_control_initial_status;
3235 static u8 fan_control_desired_level;
3237 static void fan_watchdog_fire(struct work_struct *ignored);
3238 static int fan_watchdog_maxinterval;
3239 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3241 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3242 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3243 "\\FSPD", /* 600e/x, 770e, 770x */
3244 ); /* all others */
3245 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3246 "JFNS", /* 770x-JL */
3247 ); /* all others */
3250 * SYSFS fan layout: hwmon compatible (device)
3252 * pwm*_enable:
3253 * 0: "disengaged" mode
3254 * 1: manual mode
3255 * 2: native EC "auto" mode (recommended, hardware default)
3257 * pwm*: set speed in manual mode, ignored otherwise.
3258 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3259 * interpolation.
3261 * fan*_input: tachometer reading, RPM
3264 * SYSFS fan layout: extensions
3266 * fan_watchdog (driver):
3267 * fan watchdog interval in seconds, 0 disables (default), max 120
3270 /* sysfs fan pwm1_enable ----------------------------------------------- */
3271 static ssize_t fan_pwm1_enable_show(struct device *dev,
3272 struct device_attribute *attr,
3273 char *buf)
3275 int res, mode;
3276 u8 status;
3278 res = fan_get_status_safe(&status);
3279 if (res)
3280 return res;
3282 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3283 if (status != fan_control_initial_status) {
3284 tp_features.fan_ctrl_status_undef = 0;
3285 } else {
3286 /* Return most likely status. In fact, it
3287 * might be the only possible status */
3288 status = TP_EC_FAN_AUTO;
3292 if (status & TP_EC_FAN_FULLSPEED) {
3293 mode = 0;
3294 } else if (status & TP_EC_FAN_AUTO) {
3295 mode = 2;
3296 } else
3297 mode = 1;
3299 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3302 static ssize_t fan_pwm1_enable_store(struct device *dev,
3303 struct device_attribute *attr,
3304 const char *buf, size_t count)
3306 unsigned long t;
3307 int res, level;
3309 if (parse_strtoul(buf, 2, &t))
3310 return -EINVAL;
3312 switch (t) {
3313 case 0:
3314 level = TP_EC_FAN_FULLSPEED;
3315 break;
3316 case 1:
3317 level = TPACPI_FAN_LAST_LEVEL;
3318 break;
3319 case 2:
3320 level = TP_EC_FAN_AUTO;
3321 break;
3322 case 3:
3323 /* reserved for software-controlled auto mode */
3324 return -ENOSYS;
3325 default:
3326 return -EINVAL;
3329 res = fan_set_level_safe(level);
3330 if (res == -ENXIO)
3331 return -EINVAL;
3332 else if (res < 0)
3333 return res;
3335 fan_watchdog_reset();
3337 return count;
3340 static struct device_attribute dev_attr_fan_pwm1_enable =
3341 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3342 fan_pwm1_enable_show, fan_pwm1_enable_store);
3344 /* sysfs fan pwm1 ------------------------------------------------------ */
3345 static ssize_t fan_pwm1_show(struct device *dev,
3346 struct device_attribute *attr,
3347 char *buf)
3349 int res;
3350 u8 status;
3352 res = fan_get_status_safe(&status);
3353 if (res)
3354 return res;
3356 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3357 if (status != fan_control_initial_status) {
3358 tp_features.fan_ctrl_status_undef = 0;
3359 } else {
3360 status = TP_EC_FAN_AUTO;
3364 if ((status &
3365 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3366 status = fan_control_desired_level;
3368 if (status > 7)
3369 status = 7;
3371 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3374 static ssize_t fan_pwm1_store(struct device *dev,
3375 struct device_attribute *attr,
3376 const char *buf, size_t count)
3378 unsigned long s;
3379 int rc;
3380 u8 status, newlevel;
3382 if (parse_strtoul(buf, 255, &s))
3383 return -EINVAL;
3385 /* scale down from 0-255 to 0-7 */
3386 newlevel = (s >> 5) & 0x07;
3388 rc = mutex_lock_interruptible(&fan_mutex);
3389 if (rc < 0)
3390 return rc;
3392 rc = fan_get_status(&status);
3393 if (!rc && (status &
3394 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3395 rc = fan_set_level(newlevel);
3396 if (rc == -ENXIO)
3397 rc = -EINVAL;
3398 else if (!rc) {
3399 fan_update_desired_level(newlevel);
3400 fan_watchdog_reset();
3404 mutex_unlock(&fan_mutex);
3405 return (rc)? rc : count;
3408 static struct device_attribute dev_attr_fan_pwm1 =
3409 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3410 fan_pwm1_show, fan_pwm1_store);
3412 /* sysfs fan fan1_input ------------------------------------------------ */
3413 static ssize_t fan_fan1_input_show(struct device *dev,
3414 struct device_attribute *attr,
3415 char *buf)
3417 int res;
3418 unsigned int speed;
3420 res = fan_get_speed(&speed);
3421 if (res < 0)
3422 return res;
3424 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3427 static struct device_attribute dev_attr_fan_fan1_input =
3428 __ATTR(fan1_input, S_IRUGO,
3429 fan_fan1_input_show, NULL);
3431 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3432 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3433 char *buf)
3435 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3438 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3439 const char *buf, size_t count)
3441 unsigned long t;
3443 if (parse_strtoul(buf, 120, &t))
3444 return -EINVAL;
3446 if (!fan_control_allowed)
3447 return -EPERM;
3449 fan_watchdog_maxinterval = t;
3450 fan_watchdog_reset();
3452 return count;
3455 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3456 fan_fan_watchdog_show, fan_fan_watchdog_store);
3458 /* --------------------------------------------------------------------- */
3459 static struct attribute *fan_attributes[] = {
3460 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3461 &dev_attr_fan_fan1_input.attr,
3462 NULL
3465 static const struct attribute_group fan_attr_group = {
3466 .attrs = fan_attributes,
3469 static int __init fan_init(struct ibm_init_struct *iibm)
3471 int rc;
3473 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3475 mutex_init(&fan_mutex);
3476 fan_status_access_mode = TPACPI_FAN_NONE;
3477 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3478 fan_control_commands = 0;
3479 fan_watchdog_maxinterval = 0;
3480 tp_features.fan_ctrl_status_undef = 0;
3481 fan_control_desired_level = 7;
3483 IBM_ACPIHANDLE_INIT(fans);
3484 IBM_ACPIHANDLE_INIT(gfan);
3485 IBM_ACPIHANDLE_INIT(sfan);
3487 if (gfan_handle) {
3488 /* 570, 600e/x, 770e, 770x */
3489 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3490 } else {
3491 /* all other ThinkPads: note that even old-style
3492 * ThinkPad ECs supports the fan control register */
3493 if (likely(acpi_ec_read(fan_status_offset,
3494 &fan_control_initial_status))) {
3495 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3497 /* In some ThinkPads, neither the EC nor the ACPI
3498 * DSDT initialize the fan status, and it ends up
3499 * being set to 0x07 when it *could* be either
3500 * 0x07 or 0x80.
3502 * Enable for TP-1Y (T43), TP-78 (R51e),
3503 * TP-76 (R52), TP-70 (T43, R52), which are known
3504 * to be buggy. */
3505 if (fan_control_initial_status == 0x07 &&
3506 ibm_thinkpad_ec_found &&
3507 ((ibm_thinkpad_ec_found[0] == '1' &&
3508 ibm_thinkpad_ec_found[1] == 'Y') ||
3509 (ibm_thinkpad_ec_found[0] == '7' &&
3510 (ibm_thinkpad_ec_found[1] == '6' ||
3511 ibm_thinkpad_ec_found[1] == '8' ||
3512 ibm_thinkpad_ec_found[1] == '0'))
3513 )) {
3514 printk(IBM_NOTICE
3515 "fan_init: initial fan status is "
3516 "unknown, assuming it is in auto "
3517 "mode\n");
3518 tp_features.fan_ctrl_status_undef = 1;
3520 } else {
3521 printk(IBM_ERR
3522 "ThinkPad ACPI EC access misbehaving, "
3523 "fan status and control unavailable\n");
3524 return 1;
3528 if (sfan_handle) {
3529 /* 570, 770x-JL */
3530 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3531 fan_control_commands |=
3532 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3533 } else {
3534 if (!gfan_handle) {
3535 /* gfan without sfan means no fan control */
3536 /* all other models implement TP EC 0x2f control */
3538 if (fans_handle) {
3539 /* X31, X40, X41 */
3540 fan_control_access_mode =
3541 TPACPI_FAN_WR_ACPI_FANS;
3542 fan_control_commands |=
3543 TPACPI_FAN_CMD_SPEED |
3544 TPACPI_FAN_CMD_LEVEL |
3545 TPACPI_FAN_CMD_ENABLE;
3546 } else {
3547 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3548 fan_control_commands |=
3549 TPACPI_FAN_CMD_LEVEL |
3550 TPACPI_FAN_CMD_ENABLE;
3555 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3556 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3557 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3558 fan_status_access_mode, fan_control_access_mode);
3560 /* fan control master switch */
3561 if (!fan_control_allowed) {
3562 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3563 fan_control_commands = 0;
3564 dbg_printk(TPACPI_DBG_INIT,
3565 "fan control features disabled by parameter\n");
3568 /* update fan_control_desired_level */
3569 if (fan_status_access_mode != TPACPI_FAN_NONE)
3570 fan_get_status_safe(NULL);
3572 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3573 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3574 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3575 &fan_attr_group);
3576 if (!(rc < 0))
3577 rc = driver_create_file(&tpacpi_pdriver.driver,
3578 &driver_attr_fan_watchdog);
3579 if (rc < 0)
3580 return rc;
3581 return 0;
3582 } else
3583 return 1;
3587 * Call with fan_mutex held
3589 static void fan_update_desired_level(u8 status)
3591 if ((status &
3592 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3593 if (status > 7)
3594 fan_control_desired_level = 7;
3595 else
3596 fan_control_desired_level = status;
3600 static int fan_get_status(u8 *status)
3602 u8 s;
3604 /* TODO:
3605 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3607 switch (fan_status_access_mode) {
3608 case TPACPI_FAN_RD_ACPI_GFAN:
3609 /* 570, 600e/x, 770e, 770x */
3611 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3612 return -EIO;
3614 if (likely(status))
3615 *status = s & 0x07;
3617 break;
3619 case TPACPI_FAN_RD_TPEC:
3620 /* all except 570, 600e/x, 770e, 770x */
3621 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3622 return -EIO;
3624 if (likely(status))
3625 *status = s;
3627 break;
3629 default:
3630 return -ENXIO;
3633 return 0;
3636 static int fan_get_status_safe(u8 *status)
3638 int rc;
3639 u8 s;
3641 rc = mutex_lock_interruptible(&fan_mutex);
3642 if (rc < 0)
3643 return rc;
3644 rc = fan_get_status(&s);
3645 if (!rc)
3646 fan_update_desired_level(s);
3647 mutex_unlock(&fan_mutex);
3649 if (status)
3650 *status = s;
3652 return rc;
3655 static void fan_exit(void)
3657 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3659 /* FIXME: can we really do this unconditionally? */
3660 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3661 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3663 cancel_delayed_work(&fan_watchdog_task);
3664 flush_scheduled_work();
3667 static int fan_get_speed(unsigned int *speed)
3669 u8 hi, lo;
3671 switch (fan_status_access_mode) {
3672 case TPACPI_FAN_RD_TPEC:
3673 /* all except 570, 600e/x, 770e, 770x */
3674 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3675 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3676 return -EIO;
3678 if (likely(speed))
3679 *speed = (hi << 8) | lo;
3681 break;
3683 default:
3684 return -ENXIO;
3687 return 0;
3690 static void fan_watchdog_fire(struct work_struct *ignored)
3692 int rc;
3694 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3695 rc = fan_set_enable();
3696 if (rc < 0) {
3697 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3698 "will try again later...\n", -rc);
3699 /* reschedule for later */
3700 fan_watchdog_reset();
3704 static void fan_watchdog_reset(void)
3706 static int fan_watchdog_active;
3708 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3709 return;
3711 if (fan_watchdog_active)
3712 cancel_delayed_work(&fan_watchdog_task);
3714 if (fan_watchdog_maxinterval > 0) {
3715 fan_watchdog_active = 1;
3716 if (!schedule_delayed_work(&fan_watchdog_task,
3717 msecs_to_jiffies(fan_watchdog_maxinterval
3718 * 1000))) {
3719 printk(IBM_ERR "failed to schedule the fan watchdog, "
3720 "watchdog will not trigger\n");
3722 } else
3723 fan_watchdog_active = 0;
3726 static int fan_set_level(int level)
3728 if (!fan_control_allowed)
3729 return -EPERM;
3731 switch (fan_control_access_mode) {
3732 case TPACPI_FAN_WR_ACPI_SFAN:
3733 if (level >= 0 && level <= 7) {
3734 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3735 return -EIO;
3736 } else
3737 return -EINVAL;
3738 break;
3740 case TPACPI_FAN_WR_ACPI_FANS:
3741 case TPACPI_FAN_WR_TPEC:
3742 if ((level != TP_EC_FAN_AUTO) &&
3743 (level != TP_EC_FAN_FULLSPEED) &&
3744 ((level < 0) || (level > 7)))
3745 return -EINVAL;
3747 /* safety net should the EC not support AUTO
3748 * or FULLSPEED mode bits and just ignore them */
3749 if (level & TP_EC_FAN_FULLSPEED)
3750 level |= 7; /* safety min speed 7 */
3751 else if (level & TP_EC_FAN_FULLSPEED)
3752 level |= 4; /* safety min speed 4 */
3754 if (!acpi_ec_write(fan_status_offset, level))
3755 return -EIO;
3756 else
3757 tp_features.fan_ctrl_status_undef = 0;
3758 break;
3760 default:
3761 return -ENXIO;
3763 return 0;
3766 static int fan_set_level_safe(int level)
3768 int rc;
3770 if (!fan_control_allowed)
3771 return -EPERM;
3773 rc = mutex_lock_interruptible(&fan_mutex);
3774 if (rc < 0)
3775 return rc;
3777 if (level == TPACPI_FAN_LAST_LEVEL)
3778 level = fan_control_desired_level;
3780 rc = fan_set_level(level);
3781 if (!rc)
3782 fan_update_desired_level(level);
3784 mutex_unlock(&fan_mutex);
3785 return rc;
3788 static int fan_set_enable(void)
3790 u8 s;
3791 int rc;
3793 if (!fan_control_allowed)
3794 return -EPERM;
3796 rc = mutex_lock_interruptible(&fan_mutex);
3797 if (rc < 0)
3798 return rc;
3800 switch (fan_control_access_mode) {
3801 case TPACPI_FAN_WR_ACPI_FANS:
3802 case TPACPI_FAN_WR_TPEC:
3803 rc = fan_get_status(&s);
3804 if (rc < 0)
3805 break;
3807 /* Don't go out of emergency fan mode */
3808 if (s != 7) {
3809 s &= 0x07;
3810 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3813 if (!acpi_ec_write(fan_status_offset, s))
3814 rc = -EIO;
3815 else {
3816 tp_features.fan_ctrl_status_undef = 0;
3817 rc = 0;
3819 break;
3821 case TPACPI_FAN_WR_ACPI_SFAN:
3822 rc = fan_get_status(&s);
3823 if (rc < 0)
3824 break;
3826 s &= 0x07;
3828 /* Set fan to at least level 4 */
3829 s |= 4;
3831 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3832 rc= -EIO;
3833 else
3834 rc = 0;
3835 break;
3837 default:
3838 rc = -ENXIO;
3841 mutex_unlock(&fan_mutex);
3842 return rc;
3845 static int fan_set_disable(void)
3847 int rc;
3849 if (!fan_control_allowed)
3850 return -EPERM;
3852 rc = mutex_lock_interruptible(&fan_mutex);
3853 if (rc < 0)
3854 return rc;
3856 rc = 0;
3857 switch (fan_control_access_mode) {
3858 case TPACPI_FAN_WR_ACPI_FANS:
3859 case TPACPI_FAN_WR_TPEC:
3860 if (!acpi_ec_write(fan_status_offset, 0x00))
3861 rc = -EIO;
3862 else {
3863 fan_control_desired_level = 0;
3864 tp_features.fan_ctrl_status_undef = 0;
3866 break;
3868 case TPACPI_FAN_WR_ACPI_SFAN:
3869 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3870 rc = -EIO;
3871 else
3872 fan_control_desired_level = 0;
3873 break;
3875 default:
3876 rc = -ENXIO;
3880 mutex_unlock(&fan_mutex);
3881 return rc;
3884 static int fan_set_speed(int speed)
3886 int rc;
3888 if (!fan_control_allowed)
3889 return -EPERM;
3891 rc = mutex_lock_interruptible(&fan_mutex);
3892 if (rc < 0)
3893 return rc;
3895 rc = 0;
3896 switch (fan_control_access_mode) {
3897 case TPACPI_FAN_WR_ACPI_FANS:
3898 if (speed >= 0 && speed <= 65535) {
3899 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3900 speed, speed, speed))
3901 rc = -EIO;
3902 } else
3903 rc = -EINVAL;
3904 break;
3906 default:
3907 rc = -ENXIO;
3910 mutex_unlock(&fan_mutex);
3911 return rc;
3914 static int fan_read(char *p)
3916 int len = 0;
3917 int rc;
3918 u8 status;
3919 unsigned int speed = 0;
3921 switch (fan_status_access_mode) {
3922 case TPACPI_FAN_RD_ACPI_GFAN:
3923 /* 570, 600e/x, 770e, 770x */
3924 if ((rc = fan_get_status_safe(&status)) < 0)
3925 return rc;
3927 len += sprintf(p + len, "status:\t\t%s\n"
3928 "level:\t\t%d\n",
3929 (status != 0) ? "enabled" : "disabled", status);
3930 break;
3932 case TPACPI_FAN_RD_TPEC:
3933 /* all except 570, 600e/x, 770e, 770x */
3934 if ((rc = fan_get_status_safe(&status)) < 0)
3935 return rc;
3937 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3938 if (status != fan_control_initial_status)
3939 tp_features.fan_ctrl_status_undef = 0;
3940 else
3941 /* Return most likely status. In fact, it
3942 * might be the only possible status */
3943 status = TP_EC_FAN_AUTO;
3946 len += sprintf(p + len, "status:\t\t%s\n",
3947 (status != 0) ? "enabled" : "disabled");
3949 if ((rc = fan_get_speed(&speed)) < 0)
3950 return rc;
3952 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3954 if (status & TP_EC_FAN_FULLSPEED)
3955 /* Disengaged mode takes precedence */
3956 len += sprintf(p + len, "level:\t\tdisengaged\n");
3957 else if (status & TP_EC_FAN_AUTO)
3958 len += sprintf(p + len, "level:\t\tauto\n");
3959 else
3960 len += sprintf(p + len, "level:\t\t%d\n", status);
3961 break;
3963 case TPACPI_FAN_NONE:
3964 default:
3965 len += sprintf(p + len, "status:\t\tnot supported\n");
3968 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3969 len += sprintf(p + len, "commands:\tlevel <level>");
3971 switch (fan_control_access_mode) {
3972 case TPACPI_FAN_WR_ACPI_SFAN:
3973 len += sprintf(p + len, " (<level> is 0-7)\n");
3974 break;
3976 default:
3977 len += sprintf(p + len, " (<level> is 0-7, "
3978 "auto, disengaged, full-speed)\n");
3979 break;
3983 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3984 len += sprintf(p + len, "commands:\tenable, disable\n"
3985 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3986 "1-120 (seconds))\n");
3988 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3989 len += sprintf(p + len, "commands:\tspeed <speed>"
3990 " (<speed> is 0-65535)\n");
3992 return len;
3995 static int fan_write_cmd_level(const char *cmd, int *rc)
3997 int level;
3999 if (strlencmp(cmd, "level auto") == 0)
4000 level = TP_EC_FAN_AUTO;
4001 else if ((strlencmp(cmd, "level disengaged") == 0) |
4002 (strlencmp(cmd, "level full-speed") == 0))
4003 level = TP_EC_FAN_FULLSPEED;
4004 else if (sscanf(cmd, "level %d", &level) != 1)
4005 return 0;
4007 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4008 printk(IBM_ERR "level command accepted for unsupported "
4009 "access mode %d", fan_control_access_mode);
4011 return 1;
4014 static int fan_write_cmd_enable(const char *cmd, int *rc)
4016 if (strlencmp(cmd, "enable") != 0)
4017 return 0;
4019 if ((*rc = fan_set_enable()) == -ENXIO)
4020 printk(IBM_ERR "enable command accepted for unsupported "
4021 "access mode %d", fan_control_access_mode);
4023 return 1;
4026 static int fan_write_cmd_disable(const char *cmd, int *rc)
4028 if (strlencmp(cmd, "disable") != 0)
4029 return 0;
4031 if ((*rc = fan_set_disable()) == -ENXIO)
4032 printk(IBM_ERR "disable command accepted for unsupported "
4033 "access mode %d", fan_control_access_mode);
4035 return 1;
4038 static int fan_write_cmd_speed(const char *cmd, int *rc)
4040 int speed;
4042 /* TODO:
4043 * Support speed <low> <medium> <high> ? */
4045 if (sscanf(cmd, "speed %d", &speed) != 1)
4046 return 0;
4048 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4049 printk(IBM_ERR "speed command accepted for unsupported "
4050 "access mode %d", fan_control_access_mode);
4052 return 1;
4055 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4057 int interval;
4059 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4060 return 0;
4062 if (interval < 0 || interval > 120)
4063 *rc = -EINVAL;
4064 else
4065 fan_watchdog_maxinterval = interval;
4067 return 1;
4070 static int fan_write(char *buf)
4072 char *cmd;
4073 int rc = 0;
4075 while (!rc && (cmd = next_cmd(&buf))) {
4076 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4077 fan_write_cmd_level(cmd, &rc)) &&
4078 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4079 (fan_write_cmd_enable(cmd, &rc) ||
4080 fan_write_cmd_disable(cmd, &rc) ||
4081 fan_write_cmd_watchdog(cmd, &rc))) &&
4082 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4083 fan_write_cmd_speed(cmd, &rc))
4085 rc = -EINVAL;
4086 else if (!rc)
4087 fan_watchdog_reset();
4090 return rc;
4093 static struct ibm_struct fan_driver_data = {
4094 .name = "fan",
4095 .read = fan_read,
4096 .write = fan_write,
4097 .exit = fan_exit,
4100 /****************************************************************************
4101 ****************************************************************************
4103 * Infrastructure
4105 ****************************************************************************
4106 ****************************************************************************/
4108 /* /proc support */
4109 static struct proc_dir_entry *proc_dir;
4111 /* Subdriver registry */
4112 static LIST_HEAD(tpacpi_all_drivers);
4116 * Module and infrastructure proble, init and exit handling
4119 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4120 static const char * __init str_supported(int is_supported)
4122 static char text_unsupported[] __initdata = "not supported";
4124 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4126 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4128 static int __init ibm_init(struct ibm_init_struct *iibm)
4130 int ret;
4131 struct ibm_struct *ibm = iibm->data;
4132 struct proc_dir_entry *entry;
4134 BUG_ON(ibm == NULL);
4136 INIT_LIST_HEAD(&ibm->all_drivers);
4138 if (ibm->flags.experimental && !experimental)
4139 return 0;
4141 dbg_printk(TPACPI_DBG_INIT,
4142 "probing for %s\n", ibm->name);
4144 if (iibm->init) {
4145 ret = iibm->init(iibm);
4146 if (ret > 0)
4147 return 0; /* probe failed */
4148 if (ret)
4149 return ret;
4151 ibm->flags.init_called = 1;
4154 if (ibm->acpi) {
4155 if (ibm->acpi->hid) {
4156 ret = register_tpacpi_subdriver(ibm);
4157 if (ret)
4158 goto err_out;
4161 if (ibm->acpi->notify) {
4162 ret = setup_acpi_notify(ibm);
4163 if (ret == -ENODEV) {
4164 printk(IBM_NOTICE "disabling subdriver %s\n",
4165 ibm->name);
4166 ret = 0;
4167 goto err_out;
4169 if (ret < 0)
4170 goto err_out;
4174 dbg_printk(TPACPI_DBG_INIT,
4175 "%s installed\n", ibm->name);
4177 if (ibm->read) {
4178 entry = create_proc_entry(ibm->name,
4179 S_IFREG | S_IRUGO | S_IWUSR,
4180 proc_dir);
4181 if (!entry) {
4182 printk(IBM_ERR "unable to create proc entry %s\n",
4183 ibm->name);
4184 ret = -ENODEV;
4185 goto err_out;
4187 entry->owner = THIS_MODULE;
4188 entry->data = ibm;
4189 entry->read_proc = &dispatch_procfs_read;
4190 if (ibm->write)
4191 entry->write_proc = &dispatch_procfs_write;
4192 ibm->flags.proc_created = 1;
4195 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4197 return 0;
4199 err_out:
4200 dbg_printk(TPACPI_DBG_INIT,
4201 "%s: at error exit path with result %d\n",
4202 ibm->name, ret);
4204 ibm_exit(ibm);
4205 return (ret < 0)? ret : 0;
4208 static void ibm_exit(struct ibm_struct *ibm)
4210 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4212 list_del_init(&ibm->all_drivers);
4214 if (ibm->flags.acpi_notify_installed) {
4215 dbg_printk(TPACPI_DBG_EXIT,
4216 "%s: acpi_remove_notify_handler\n", ibm->name);
4217 BUG_ON(!ibm->acpi);
4218 acpi_remove_notify_handler(*ibm->acpi->handle,
4219 ibm->acpi->type,
4220 dispatch_acpi_notify);
4221 ibm->flags.acpi_notify_installed = 0;
4222 ibm->flags.acpi_notify_installed = 0;
4225 if (ibm->flags.proc_created) {
4226 dbg_printk(TPACPI_DBG_EXIT,
4227 "%s: remove_proc_entry\n", ibm->name);
4228 remove_proc_entry(ibm->name, proc_dir);
4229 ibm->flags.proc_created = 0;
4232 if (ibm->flags.acpi_driver_registered) {
4233 dbg_printk(TPACPI_DBG_EXIT,
4234 "%s: acpi_bus_unregister_driver\n", ibm->name);
4235 BUG_ON(!ibm->acpi);
4236 acpi_bus_unregister_driver(ibm->acpi->driver);
4237 kfree(ibm->acpi->driver);
4238 ibm->acpi->driver = NULL;
4239 ibm->flags.acpi_driver_registered = 0;
4242 if (ibm->flags.init_called && ibm->exit) {
4243 ibm->exit();
4244 ibm->flags.init_called = 0;
4247 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4250 /* Probing */
4252 static char *ibm_thinkpad_ec_found;
4254 static char* __init check_dmi_for_ec(void)
4256 struct dmi_device *dev = NULL;
4257 char ec_fw_string[18];
4260 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4261 * X32 or newer, all Z series; Some models must have an
4262 * up-to-date BIOS or they will not be detected.
4264 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4266 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4267 if (sscanf(dev->name,
4268 "IBM ThinkPad Embedded Controller -[%17c",
4269 ec_fw_string) == 1) {
4270 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4271 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4272 return kstrdup(ec_fw_string, GFP_KERNEL);
4275 return NULL;
4278 static int __init probe_for_thinkpad(void)
4280 int is_thinkpad;
4282 if (acpi_disabled)
4283 return -ENODEV;
4286 * Non-ancient models have better DMI tagging, but very old models
4287 * don't.
4289 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4291 /* ec is required because many other handles are relative to it */
4292 IBM_ACPIHANDLE_INIT(ec);
4293 if (!ec_handle) {
4294 if (is_thinkpad)
4295 printk(IBM_ERR
4296 "Not yet supported ThinkPad detected!\n");
4297 return -ENODEV;
4301 * Risks a regression on very old machines, but reduces potential
4302 * false positives a damn great deal
4304 if (!is_thinkpad)
4305 is_thinkpad = dmi_name_in_vendors("IBM");
4307 if (!is_thinkpad && !force_load)
4308 return -ENODEV;
4310 return 0;
4314 /* Module init, exit, parameters */
4316 static struct ibm_init_struct ibms_init[] __initdata = {
4318 .init = thinkpad_acpi_driver_init,
4319 .data = &thinkpad_acpi_driver_data,
4322 .init = hotkey_init,
4323 .data = &hotkey_driver_data,
4326 .init = bluetooth_init,
4327 .data = &bluetooth_driver_data,
4330 .init = wan_init,
4331 .data = &wan_driver_data,
4334 .init = video_init,
4335 .data = &video_driver_data,
4338 .init = light_init,
4339 .data = &light_driver_data,
4341 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4343 .init = dock_init,
4344 .data = &dock_driver_data[0],
4347 .init = dock_init2,
4348 .data = &dock_driver_data[1],
4350 #endif
4351 #ifdef CONFIG_THINKPAD_ACPI_BAY
4353 .init = bay_init,
4354 .data = &bay_driver_data,
4356 #endif
4358 .init = cmos_init,
4359 .data = &cmos_driver_data,
4362 .init = led_init,
4363 .data = &led_driver_data,
4366 .init = beep_init,
4367 .data = &beep_driver_data,
4370 .init = thermal_init,
4371 .data = &thermal_driver_data,
4374 .data = &ecdump_driver_data,
4377 .init = brightness_init,
4378 .data = &brightness_driver_data,
4381 .data = &volume_driver_data,
4384 .init = fan_init,
4385 .data = &fan_driver_data,
4389 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4391 unsigned int i;
4392 struct ibm_struct *ibm;
4394 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4395 ibm = ibms_init[i].data;
4396 BUG_ON(ibm == NULL);
4398 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4399 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4400 return -ENOSPC;
4401 strcpy(ibms_init[i].param, val);
4402 strcat(ibms_init[i].param, ",");
4403 return 0;
4407 return -EINVAL;
4410 static int experimental;
4411 module_param(experimental, int, 0);
4413 static u32 dbg_level;
4414 module_param_named(debug, dbg_level, uint, 0);
4416 static int force_load;
4417 module_param(force_load, int, 0);
4419 static int fan_control_allowed;
4420 module_param_named(fan_control, fan_control_allowed, int, 0);
4422 #define IBM_PARAM(feature) \
4423 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4425 IBM_PARAM(hotkey);
4426 IBM_PARAM(bluetooth);
4427 IBM_PARAM(video);
4428 IBM_PARAM(light);
4429 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4430 IBM_PARAM(dock);
4431 #endif
4432 #ifdef CONFIG_THINKPAD_ACPI_BAY
4433 IBM_PARAM(bay);
4434 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4435 IBM_PARAM(cmos);
4436 IBM_PARAM(led);
4437 IBM_PARAM(beep);
4438 IBM_PARAM(ecdump);
4439 IBM_PARAM(brightness);
4440 IBM_PARAM(volume);
4441 IBM_PARAM(fan);
4443 static int __init thinkpad_acpi_module_init(void)
4445 int ret, i;
4447 /* Driver-level probe */
4448 ret = probe_for_thinkpad();
4449 if (ret)
4450 return ret;
4452 /* Driver initialization */
4453 ibm_thinkpad_ec_found = check_dmi_for_ec();
4454 IBM_ACPIHANDLE_INIT(ecrd);
4455 IBM_ACPIHANDLE_INIT(ecwr);
4457 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4458 if (!proc_dir) {
4459 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4460 thinkpad_acpi_module_exit();
4461 return -ENODEV;
4463 proc_dir->owner = THIS_MODULE;
4465 ret = platform_driver_register(&tpacpi_pdriver);
4466 if (ret) {
4467 printk(IBM_ERR "unable to register platform driver\n");
4468 thinkpad_acpi_module_exit();
4469 return ret;
4471 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4472 if (ret) {
4473 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4474 thinkpad_acpi_module_exit();
4475 return ret;
4479 /* Device initialization */
4480 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4481 NULL, 0);
4482 if (IS_ERR(tpacpi_pdev)) {
4483 ret = PTR_ERR(tpacpi_pdev);
4484 tpacpi_pdev = NULL;
4485 printk(IBM_ERR "unable to register platform device\n");
4486 thinkpad_acpi_module_exit();
4487 return ret;
4489 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4490 if (IS_ERR(tpacpi_hwmon)) {
4491 ret = PTR_ERR(tpacpi_hwmon);
4492 tpacpi_hwmon = NULL;
4493 printk(IBM_ERR "unable to register hwmon device\n");
4494 thinkpad_acpi_module_exit();
4495 return ret;
4497 tpacpi_inputdev = input_allocate_device();
4498 if (!tpacpi_inputdev) {
4499 printk(IBM_ERR "unable to allocate input device\n");
4500 thinkpad_acpi_module_exit();
4501 return -ENOMEM;
4502 } else {
4503 /* Prepare input device, but don't register */
4504 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4505 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4506 tpacpi_inputdev->id.bustype = BUS_HOST;
4507 tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
4508 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4509 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4511 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4512 ret = ibm_init(&ibms_init[i]);
4513 if (ret >= 0 && *ibms_init[i].param)
4514 ret = ibms_init[i].data->write(ibms_init[i].param);
4515 if (ret < 0) {
4516 thinkpad_acpi_module_exit();
4517 return ret;
4520 ret = input_register_device(tpacpi_inputdev);
4521 if (ret < 0) {
4522 printk(IBM_ERR "unable to register input device\n");
4523 thinkpad_acpi_module_exit();
4524 return ret;
4525 } else {
4526 tp_features.input_device_registered = 1;
4529 return 0;
4532 static void thinkpad_acpi_module_exit(void)
4534 struct ibm_struct *ibm, *itmp;
4536 list_for_each_entry_safe_reverse(ibm, itmp,
4537 &tpacpi_all_drivers,
4538 all_drivers) {
4539 ibm_exit(ibm);
4542 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4544 if (tpacpi_inputdev) {
4545 if (tp_features.input_device_registered)
4546 input_unregister_device(tpacpi_inputdev);
4547 else
4548 input_free_device(tpacpi_inputdev);
4551 if (tpacpi_hwmon)
4552 hwmon_device_unregister(tpacpi_hwmon);
4554 if (tpacpi_pdev)
4555 platform_device_unregister(tpacpi_pdev);
4557 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4558 platform_driver_unregister(&tpacpi_pdriver);
4560 if (proc_dir)
4561 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4563 kfree(ibm_thinkpad_ec_found);
4566 module_init(thinkpad_acpi_module_init);
4567 module_exit(thinkpad_acpi_module_exit);