ACPI: thinkpad-acpi: add sysfs support to fan subdriver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob7a1b7358a7eb03cca66c7e273d8bb14816ff6e40
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 0x000100
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");
95 #define __unused __attribute__ ((unused))
97 /****************************************************************************
98 ****************************************************************************
100 * ACPI Helpers and device model
102 ****************************************************************************
103 ****************************************************************************/
105 /*************************************************************************
106 * ACPI basic handles
109 static acpi_handle root_handle = NULL;
111 #define IBM_HANDLE(object, parent, paths...) \
112 static acpi_handle object##_handle; \
113 static acpi_handle *object##_parent = &parent##_handle; \
114 static char *object##_path; \
115 static char *object##_paths[] = { paths }
117 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
118 "\\_SB.PCI.ISA.EC", /* 570 */
119 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
120 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
121 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
122 "\\_SB.PCI0.ICH3.EC0", /* R31 */
123 "\\_SB.PCI0.LPC.EC", /* all others */
126 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
127 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
130 /*************************************************************************
131 * Misc ACPI handles
134 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
135 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
136 "\\CMS", /* R40, R40e */
137 ); /* all others */
139 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
140 "^HKEY", /* R30, R31 */
141 "HKEY", /* all others */
142 ); /* 570 */
145 /*************************************************************************
146 * ACPI helpers
149 static int acpi_evalf(acpi_handle handle,
150 void *res, char *method, char *fmt, ...)
152 char *fmt0 = fmt;
153 struct acpi_object_list params;
154 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
155 struct acpi_buffer result, *resultp;
156 union acpi_object out_obj;
157 acpi_status status;
158 va_list ap;
159 char res_type;
160 int success;
161 int quiet;
163 if (!*fmt) {
164 printk(IBM_ERR "acpi_evalf() called with empty format\n");
165 return 0;
168 if (*fmt == 'q') {
169 quiet = 1;
170 fmt++;
171 } else
172 quiet = 0;
174 res_type = *(fmt++);
176 params.count = 0;
177 params.pointer = &in_objs[0];
179 va_start(ap, fmt);
180 while (*fmt) {
181 char c = *(fmt++);
182 switch (c) {
183 case 'd': /* int */
184 in_objs[params.count].integer.value = va_arg(ap, int);
185 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
186 break;
187 /* add more types as needed */
188 default:
189 printk(IBM_ERR "acpi_evalf() called "
190 "with invalid format character '%c'\n", c);
191 return 0;
194 va_end(ap);
196 if (res_type != 'v') {
197 result.length = sizeof(out_obj);
198 result.pointer = &out_obj;
199 resultp = &result;
200 } else
201 resultp = NULL;
203 status = acpi_evaluate_object(handle, method, &params, resultp);
205 switch (res_type) {
206 case 'd': /* int */
207 if (res)
208 *(int *)res = out_obj.integer.value;
209 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
210 break;
211 case 'v': /* void */
212 success = status == AE_OK;
213 break;
214 /* add more types as needed */
215 default:
216 printk(IBM_ERR "acpi_evalf() called "
217 "with invalid format character '%c'\n", res_type);
218 return 0;
221 if (!success && !quiet)
222 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
223 method, fmt0, status);
225 return success;
228 static void __unused acpi_print_int(acpi_handle handle, char *method)
230 int i;
232 if (acpi_evalf(handle, &i, method, "d"))
233 printk(IBM_INFO "%s = 0x%x\n", method, i);
234 else
235 printk(IBM_ERR "error calling %s\n", method);
238 static int acpi_ec_read(int i, u8 * p)
240 int v;
242 if (ecrd_handle) {
243 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
244 return 0;
245 *p = v;
246 } else {
247 if (ec_read(i, p) < 0)
248 return 0;
251 return 1;
254 static int acpi_ec_write(int i, u8 v)
256 if (ecwr_handle) {
257 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
258 return 0;
259 } else {
260 if (ec_write(i, v) < 0)
261 return 0;
264 return 1;
267 static int _sta(acpi_handle handle)
269 int status;
271 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
272 status = 0;
274 return status;
277 static int issue_thinkpad_cmos_command(int cmos_cmd)
279 if (!cmos_handle)
280 return -ENXIO;
282 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
283 return -EIO;
285 return 0;
288 /*************************************************************************
289 * ACPI device model
292 static void drv_acpi_handle_init(char *name,
293 acpi_handle *handle, acpi_handle parent,
294 char **paths, int num_paths, char **path)
296 int i;
297 acpi_status status;
299 for (i = 0; i < num_paths; i++) {
300 status = acpi_get_handle(parent, paths[i], handle);
301 if (ACPI_SUCCESS(status)) {
302 *path = paths[i];
303 return;
307 *handle = NULL;
310 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
312 struct ibm_struct *ibm = data;
314 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
315 return;
317 ibm->acpi->notify(ibm, event);
320 static int __init setup_acpi_notify(struct ibm_struct *ibm)
322 acpi_status status;
323 int ret;
325 BUG_ON(!ibm->acpi);
327 if (!*ibm->acpi->handle)
328 return 0;
330 dbg_printk(TPACPI_DBG_INIT,
331 "setting up ACPI notify for %s\n", ibm->name);
333 ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
334 if (ret < 0) {
335 printk(IBM_ERR "%s device not present\n", ibm->name);
336 return -ENODEV;
339 acpi_driver_data(ibm->acpi->device) = ibm;
340 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
341 IBM_ACPI_EVENT_PREFIX,
342 ibm->name);
344 status = acpi_install_notify_handler(*ibm->acpi->handle,
345 ibm->acpi->type, dispatch_acpi_notify, ibm);
346 if (ACPI_FAILURE(status)) {
347 if (status == AE_ALREADY_EXISTS) {
348 printk(IBM_NOTICE "another device driver is already handling %s events\n",
349 ibm->name);
350 } else {
351 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
352 ibm->name, status);
354 return -ENODEV;
356 ibm->flags.acpi_notify_installed = 1;
357 return 0;
360 static int __init tpacpi_device_add(struct acpi_device *device)
362 return 0;
365 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
367 int ret;
369 dbg_printk(TPACPI_DBG_INIT,
370 "registering %s as an ACPI driver\n", ibm->name);
372 BUG_ON(!ibm->acpi);
374 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
375 if (!ibm->acpi->driver) {
376 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
377 return -ENOMEM;
380 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
381 ibm->acpi->driver->ids = ibm->acpi->hid;
382 ibm->acpi->driver->ops.add = &tpacpi_device_add;
384 ret = acpi_bus_register_driver(ibm->acpi->driver);
385 if (ret < 0) {
386 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
387 ibm->acpi->hid, ret);
388 kfree(ibm->acpi->driver);
389 ibm->acpi->driver = NULL;
390 } else if (!ret)
391 ibm->flags.acpi_driver_registered = 1;
393 return ret;
397 /****************************************************************************
398 ****************************************************************************
400 * Procfs Helpers
402 ****************************************************************************
403 ****************************************************************************/
405 static int dispatch_procfs_read(char *page, char **start, off_t off,
406 int count, int *eof, void *data)
408 struct ibm_struct *ibm = data;
409 int len;
411 if (!ibm || !ibm->read)
412 return -EINVAL;
414 len = ibm->read(page);
415 if (len < 0)
416 return len;
418 if (len <= off + count)
419 *eof = 1;
420 *start = page + off;
421 len -= off;
422 if (len > count)
423 len = count;
424 if (len < 0)
425 len = 0;
427 return len;
430 static int dispatch_procfs_write(struct file *file,
431 const char __user * userbuf,
432 unsigned long count, void *data)
434 struct ibm_struct *ibm = data;
435 char *kernbuf;
436 int ret;
438 if (!ibm || !ibm->write)
439 return -EINVAL;
441 kernbuf = kmalloc(count + 2, GFP_KERNEL);
442 if (!kernbuf)
443 return -ENOMEM;
445 if (copy_from_user(kernbuf, userbuf, count)) {
446 kfree(kernbuf);
447 return -EFAULT;
450 kernbuf[count] = 0;
451 strcat(kernbuf, ",");
452 ret = ibm->write(kernbuf);
453 if (ret == 0)
454 ret = count;
456 kfree(kernbuf);
458 return ret;
461 static char *next_cmd(char **cmds)
463 char *start = *cmds;
464 char *end;
466 while ((end = strchr(start, ',')) && end == start)
467 start = end + 1;
469 if (!end)
470 return NULL;
472 *end = 0;
473 *cmds = end + 1;
474 return start;
478 /****************************************************************************
479 ****************************************************************************
481 * Device model: hwmon and platform
483 ****************************************************************************
484 ****************************************************************************/
486 static struct platform_device *tpacpi_pdev = NULL;
487 static struct class_device *tpacpi_hwmon = NULL;
489 static struct platform_driver tpacpi_pdriver = {
490 .driver = {
491 .name = IBM_DRVR_NAME,
492 .owner = THIS_MODULE,
497 /*************************************************************************
498 * thinkpad-acpi driver attributes
501 /* interface_version --------------------------------------------------- */
502 static ssize_t tpacpi_driver_interface_version_show(
503 struct device_driver *drv,
504 char *buf)
506 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
509 static DRIVER_ATTR(interface_version, S_IRUGO,
510 tpacpi_driver_interface_version_show, NULL);
512 /* debug_level --------------------------------------------------------- */
513 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
514 char *buf)
516 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
519 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
520 const char *buf, size_t count)
522 unsigned long t;
524 if (parse_strtoul(buf, 0xffff, &t))
525 return -EINVAL;
527 dbg_level = t;
529 return count;
532 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
533 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
535 /* version ------------------------------------------------------------- */
536 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
537 char *buf)
539 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
542 static DRIVER_ATTR(version, S_IRUGO,
543 tpacpi_driver_version_show, NULL);
545 /* --------------------------------------------------------------------- */
547 static struct driver_attribute* tpacpi_driver_attributes[] = {
548 &driver_attr_debug_level, &driver_attr_version,
549 &driver_attr_interface_version,
552 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
554 int i, res;
556 i = 0;
557 res = 0;
558 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
559 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
560 i++;
563 return res;
566 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
568 int i;
570 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
571 driver_remove_file(drv, tpacpi_driver_attributes[i]);
574 /*************************************************************************
575 * sysfs support helpers
578 struct attribute_set_obj {
579 struct attribute_set s;
580 struct attribute *a;
581 } __attribute__((packed));
583 static struct attribute_set *create_attr_set(unsigned int max_members,
584 const char* name)
586 struct attribute_set_obj *sobj;
588 if (max_members == 0)
589 return NULL;
591 /* Allocates space for implicit NULL at the end too */
592 sobj = kzalloc(sizeof(struct attribute_set_obj) +
593 max_members * sizeof(struct attribute *),
594 GFP_KERNEL);
595 if (!sobj)
596 return NULL;
597 sobj->s.max_members = max_members;
598 sobj->s.group.attrs = &sobj->a;
599 sobj->s.group.name = name;
601 return &sobj->s;
604 /* not multi-threaded safe, use it in a single thread per set */
605 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
607 if (!s || !attr)
608 return -EINVAL;
610 if (s->members >= s->max_members)
611 return -ENOMEM;
613 s->group.attrs[s->members] = attr;
614 s->members++;
616 return 0;
619 static int add_many_to_attr_set(struct attribute_set* s,
620 struct attribute **attr,
621 unsigned int count)
623 int i, res;
625 for (i = 0; i < count; i++) {
626 res = add_to_attr_set(s, attr[i]);
627 if (res)
628 return res;
631 return 0;
634 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
636 sysfs_remove_group(kobj, &s->group);
637 destroy_attr_set(s);
640 static int parse_strtoul(const char *buf,
641 unsigned long max, unsigned long *value)
643 char *endp;
645 *value = simple_strtoul(buf, &endp, 0);
646 while (*endp && isspace(*endp))
647 endp++;
648 if (*endp || *value > max)
649 return -EINVAL;
651 return 0;
654 /****************************************************************************
655 ****************************************************************************
657 * Subdrivers
659 ****************************************************************************
660 ****************************************************************************/
662 /*************************************************************************
663 * thinkpad-acpi init subdriver
666 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
668 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
669 printk(IBM_INFO "%s\n", IBM_URL);
671 if (ibm_thinkpad_ec_found)
672 printk(IBM_INFO "ThinkPad EC firmware %s\n",
673 ibm_thinkpad_ec_found);
675 return 0;
678 static int thinkpad_acpi_driver_read(char *p)
680 int len = 0;
682 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
683 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
685 return len;
688 static struct ibm_struct thinkpad_acpi_driver_data = {
689 .name = "driver",
690 .read = thinkpad_acpi_driver_read,
693 /*************************************************************************
694 * Hotkey subdriver
697 static int hotkey_orig_status;
698 static int hotkey_orig_mask;
700 static int __init hotkey_init(struct ibm_init_struct *iibm)
702 int res;
704 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
706 IBM_ACPIHANDLE_INIT(hkey);
707 mutex_init(&hotkey_mutex);
709 /* hotkey not supported on 570 */
710 tp_features.hotkey = hkey_handle != NULL;
712 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
713 str_supported(tp_features.hotkey));
715 if (tp_features.hotkey) {
716 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
717 A30, R30, R31, T20-22, X20-21, X22-24 */
718 tp_features.hotkey_mask =
719 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
721 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
722 str_supported(tp_features.hotkey_mask));
724 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
725 if (res)
726 return res;
729 return (tp_features.hotkey)? 0 : 1;
732 static void hotkey_exit(void)
734 int res;
736 if (tp_features.hotkey) {
737 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
738 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
739 if (res)
740 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
744 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
746 int hkey;
748 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
749 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
750 else {
751 printk(IBM_ERR "unknown hotkey event %d\n", event);
752 acpi_bus_generate_event(ibm->acpi->device, event, 0);
757 * Call with hotkey_mutex held
759 static int hotkey_get(int *status, int *mask)
761 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
762 return -EIO;
764 if (tp_features.hotkey_mask)
765 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
766 return -EIO;
768 return 0;
772 * Call with hotkey_mutex held
774 static int hotkey_set(int status, int mask)
776 int i;
778 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
779 return -EIO;
781 if (tp_features.hotkey_mask)
782 for (i = 0; i < 32; i++) {
783 int bit = ((1 << i) & mask) != 0;
784 if (!acpi_evalf(hkey_handle,
785 NULL, "MHKM", "vdd", i + 1, bit))
786 return -EIO;
789 return 0;
792 static int hotkey_read(char *p)
794 int res, status, mask;
795 int len = 0;
797 if (!tp_features.hotkey) {
798 len += sprintf(p + len, "status:\t\tnot supported\n");
799 return len;
802 res = mutex_lock_interruptible(&hotkey_mutex);
803 if (res < 0)
804 return res;
805 res = hotkey_get(&status, &mask);
806 mutex_unlock(&hotkey_mutex);
807 if (res)
808 return res;
810 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
811 if (tp_features.hotkey_mask) {
812 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
813 len += sprintf(p + len,
814 "commands:\tenable, disable, reset, <mask>\n");
815 } else {
816 len += sprintf(p + len, "mask:\t\tnot supported\n");
817 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
820 return len;
823 static int hotkey_write(char *buf)
825 int res, status, mask;
826 char *cmd;
827 int do_cmd = 0;
829 if (!tp_features.hotkey)
830 return -ENODEV;
832 res = mutex_lock_interruptible(&hotkey_mutex);
833 if (res < 0)
834 return res;
836 res = hotkey_get(&status, &mask);
837 if (res)
838 goto errexit;
840 res = 0;
841 while ((cmd = next_cmd(&buf))) {
842 if (strlencmp(cmd, "enable") == 0) {
843 status = 1;
844 } else if (strlencmp(cmd, "disable") == 0) {
845 status = 0;
846 } else if (strlencmp(cmd, "reset") == 0) {
847 status = hotkey_orig_status;
848 mask = hotkey_orig_mask;
849 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
850 /* mask set */
851 } else if (sscanf(cmd, "%x", &mask) == 1) {
852 /* mask set */
853 } else {
854 res = -EINVAL;
855 goto errexit;
857 do_cmd = 1;
860 if (do_cmd)
861 res = hotkey_set(status, mask);
863 errexit:
864 mutex_unlock(&hotkey_mutex);
865 return res;
868 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
869 .hid = IBM_HKEY_HID,
870 .notify = hotkey_notify,
871 .handle = &hkey_handle,
872 .type = ACPI_DEVICE_NOTIFY,
875 static struct ibm_struct hotkey_driver_data = {
876 .name = "hotkey",
877 .read = hotkey_read,
878 .write = hotkey_write,
879 .exit = hotkey_exit,
880 .acpi = &ibm_hotkey_acpidriver,
883 /*************************************************************************
884 * Bluetooth subdriver
887 static int __init bluetooth_init(struct ibm_init_struct *iibm)
889 int status = 0;
891 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
893 IBM_ACPIHANDLE_INIT(hkey);
895 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
896 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
897 tp_features.bluetooth = hkey_handle &&
898 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
900 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
901 str_supported(tp_features.bluetooth),
902 status);
904 if (tp_features.bluetooth &&
905 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
906 /* no bluetooth hardware present in system */
907 tp_features.bluetooth = 0;
908 dbg_printk(TPACPI_DBG_INIT,
909 "bluetooth hardware not installed\n");
912 return (tp_features.bluetooth)? 0 : 1;
915 static int bluetooth_get_radiosw(void)
917 int status;
919 if (!tp_features.bluetooth)
920 return -ENODEV;
922 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
923 return -EIO;
925 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
928 static int bluetooth_set_radiosw(int radio_on)
930 int status;
932 if (!tp_features.bluetooth)
933 return -ENODEV;
935 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
936 return -EIO;
937 if (radio_on)
938 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
939 else
940 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
941 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
942 return -EIO;
944 return 0;
947 static int bluetooth_read(char *p)
949 int len = 0;
950 int status = bluetooth_get_radiosw();
952 if (!tp_features.bluetooth)
953 len += sprintf(p + len, "status:\t\tnot supported\n");
954 else {
955 len += sprintf(p + len, "status:\t\t%s\n",
956 (status)? "enabled" : "disabled");
957 len += sprintf(p + len, "commands:\tenable, disable\n");
960 return len;
963 static int bluetooth_write(char *buf)
965 char *cmd;
967 if (!tp_features.bluetooth)
968 return -ENODEV;
970 while ((cmd = next_cmd(&buf))) {
971 if (strlencmp(cmd, "enable") == 0) {
972 bluetooth_set_radiosw(1);
973 } else if (strlencmp(cmd, "disable") == 0) {
974 bluetooth_set_radiosw(0);
975 } else
976 return -EINVAL;
979 return 0;
982 static struct ibm_struct bluetooth_driver_data = {
983 .name = "bluetooth",
984 .read = bluetooth_read,
985 .write = bluetooth_write,
988 /*************************************************************************
989 * Wan subdriver
992 static int __init wan_init(struct ibm_init_struct *iibm)
994 int status = 0;
996 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
998 IBM_ACPIHANDLE_INIT(hkey);
1000 tp_features.wan = hkey_handle &&
1001 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1003 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1004 str_supported(tp_features.wan),
1005 status);
1007 if (tp_features.wan &&
1008 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
1009 /* no wan hardware present in system */
1010 tp_features.wan = 0;
1011 dbg_printk(TPACPI_DBG_INIT,
1012 "wan hardware not installed\n");
1015 return (tp_features.wan)? 0 : 1;
1018 static int wan_get_radiosw(void)
1020 int status;
1022 if (!tp_features.wan)
1023 return -ENODEV;
1025 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1026 return -EIO;
1028 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1031 static int wan_set_radiosw(int radio_on)
1033 int status;
1035 if (!tp_features.wan)
1036 return -ENODEV;
1038 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1039 return -EIO;
1040 if (radio_on)
1041 status |= TP_ACPI_WANCARD_RADIOSSW;
1042 else
1043 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1044 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1045 return -EIO;
1047 return 0;
1050 static int wan_read(char *p)
1052 int len = 0;
1053 int status = wan_get_radiosw();
1055 if (!tp_features.wan)
1056 len += sprintf(p + len, "status:\t\tnot supported\n");
1057 else {
1058 len += sprintf(p + len, "status:\t\t%s\n",
1059 (status)? "enabled" : "disabled");
1060 len += sprintf(p + len, "commands:\tenable, disable\n");
1063 return len;
1066 static int wan_write(char *buf)
1068 char *cmd;
1070 if (!tp_features.wan)
1071 return -ENODEV;
1073 while ((cmd = next_cmd(&buf))) {
1074 if (strlencmp(cmd, "enable") == 0) {
1075 wan_set_radiosw(1);
1076 } else if (strlencmp(cmd, "disable") == 0) {
1077 wan_set_radiosw(0);
1078 } else
1079 return -EINVAL;
1082 return 0;
1085 static struct ibm_struct wan_driver_data = {
1086 .name = "wan",
1087 .read = wan_read,
1088 .write = wan_write,
1089 .flags.experimental = 1,
1092 /*************************************************************************
1093 * Video subdriver
1096 static enum video_access_mode video_supported;
1097 static int video_orig_autosw;
1099 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1100 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1101 "\\_SB.PCI0.VID0", /* 770e */
1102 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1103 "\\_SB.PCI0.AGP.VID", /* all others */
1104 ); /* R30, R31 */
1106 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1108 static int __init video_init(struct ibm_init_struct *iibm)
1110 int ivga;
1112 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1114 IBM_ACPIHANDLE_INIT(vid);
1115 IBM_ACPIHANDLE_INIT(vid2);
1117 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1118 /* G41, assume IVGA doesn't change */
1119 vid_handle = vid2_handle;
1121 if (!vid_handle)
1122 /* video switching not supported on R30, R31 */
1123 video_supported = TPACPI_VIDEO_NONE;
1124 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1125 /* 570 */
1126 video_supported = TPACPI_VIDEO_570;
1127 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1128 /* 600e/x, 770e, 770x */
1129 video_supported = TPACPI_VIDEO_770;
1130 else
1131 /* all others */
1132 video_supported = TPACPI_VIDEO_NEW;
1134 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1135 str_supported(video_supported != TPACPI_VIDEO_NONE),
1136 video_supported);
1138 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1141 static void video_exit(void)
1143 dbg_printk(TPACPI_DBG_EXIT,
1144 "restoring original video autoswitch mode\n");
1145 if (video_autosw_set(video_orig_autosw))
1146 printk(IBM_ERR "error while trying to restore original "
1147 "video autoswitch mode\n");
1150 static int video_outputsw_get(void)
1152 int status = 0;
1153 int i;
1155 switch (video_supported) {
1156 case TPACPI_VIDEO_570:
1157 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1158 TP_ACPI_VIDEO_570_PHSCMD))
1159 return -EIO;
1160 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1161 break;
1162 case TPACPI_VIDEO_770:
1163 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1164 return -EIO;
1165 if (i)
1166 status |= TP_ACPI_VIDEO_S_LCD;
1167 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1168 return -EIO;
1169 if (i)
1170 status |= TP_ACPI_VIDEO_S_CRT;
1171 break;
1172 case TPACPI_VIDEO_NEW:
1173 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1174 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1175 return -EIO;
1176 if (i)
1177 status |= TP_ACPI_VIDEO_S_CRT;
1179 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1180 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1181 return -EIO;
1182 if (i)
1183 status |= TP_ACPI_VIDEO_S_LCD;
1184 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1185 return -EIO;
1186 if (i)
1187 status |= TP_ACPI_VIDEO_S_DVI;
1188 break;
1189 default:
1190 return -ENOSYS;
1193 return status;
1196 static int video_outputsw_set(int status)
1198 int autosw;
1199 int res = 0;
1201 switch (video_supported) {
1202 case TPACPI_VIDEO_570:
1203 res = acpi_evalf(NULL, NULL,
1204 "\\_SB.PHS2", "vdd",
1205 TP_ACPI_VIDEO_570_PHS2CMD,
1206 status | TP_ACPI_VIDEO_570_PHS2SET);
1207 break;
1208 case TPACPI_VIDEO_770:
1209 autosw = video_autosw_get();
1210 if (autosw < 0)
1211 return autosw;
1213 res = video_autosw_set(1);
1214 if (res)
1215 return res;
1216 res = acpi_evalf(vid_handle, NULL,
1217 "ASWT", "vdd", status * 0x100, 0);
1218 if (!autosw && video_autosw_set(autosw)) {
1219 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1220 return -EIO;
1222 break;
1223 case TPACPI_VIDEO_NEW:
1224 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1225 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1226 break;
1227 default:
1228 return -ENOSYS;
1231 return (res)? 0 : -EIO;
1234 static int video_autosw_get(void)
1236 int autosw = 0;
1238 switch (video_supported) {
1239 case TPACPI_VIDEO_570:
1240 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1241 return -EIO;
1242 break;
1243 case TPACPI_VIDEO_770:
1244 case TPACPI_VIDEO_NEW:
1245 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1246 return -EIO;
1247 break;
1248 default:
1249 return -ENOSYS;
1252 return autosw & 1;
1255 static int video_autosw_set(int enable)
1257 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1258 return -EIO;
1259 return 0;
1262 static int video_outputsw_cycle(void)
1264 int autosw = video_autosw_get();
1265 int res;
1267 if (autosw < 0)
1268 return autosw;
1270 switch (video_supported) {
1271 case TPACPI_VIDEO_570:
1272 res = video_autosw_set(1);
1273 if (res)
1274 return res;
1275 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1276 break;
1277 case TPACPI_VIDEO_770:
1278 case TPACPI_VIDEO_NEW:
1279 res = video_autosw_set(1);
1280 if (res)
1281 return res;
1282 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1283 break;
1284 default:
1285 return -ENOSYS;
1287 if (!autosw && video_autosw_set(autosw)) {
1288 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1289 return -EIO;
1292 return (res)? 0 : -EIO;
1295 static int video_expand_toggle(void)
1297 switch (video_supported) {
1298 case TPACPI_VIDEO_570:
1299 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1300 0 : -EIO;
1301 case TPACPI_VIDEO_770:
1302 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1303 0 : -EIO;
1304 case TPACPI_VIDEO_NEW:
1305 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1306 0 : -EIO;
1307 default:
1308 return -ENOSYS;
1310 /* not reached */
1313 static int video_read(char *p)
1315 int status, autosw;
1316 int len = 0;
1318 if (video_supported == TPACPI_VIDEO_NONE) {
1319 len += sprintf(p + len, "status:\t\tnot supported\n");
1320 return len;
1323 status = video_outputsw_get();
1324 if (status < 0)
1325 return status;
1327 autosw = video_autosw_get();
1328 if (autosw < 0)
1329 return autosw;
1331 len += sprintf(p + len, "status:\t\tsupported\n");
1332 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1333 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1334 if (video_supported == TPACPI_VIDEO_NEW)
1335 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1336 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1337 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1338 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1339 if (video_supported == TPACPI_VIDEO_NEW)
1340 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1341 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1342 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1344 return len;
1347 static int video_write(char *buf)
1349 char *cmd;
1350 int enable, disable, status;
1351 int res;
1353 if (video_supported == TPACPI_VIDEO_NONE)
1354 return -ENODEV;
1356 enable = 0;
1357 disable = 0;
1359 while ((cmd = next_cmd(&buf))) {
1360 if (strlencmp(cmd, "lcd_enable") == 0) {
1361 enable |= TP_ACPI_VIDEO_S_LCD;
1362 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1363 disable |= TP_ACPI_VIDEO_S_LCD;
1364 } else if (strlencmp(cmd, "crt_enable") == 0) {
1365 enable |= TP_ACPI_VIDEO_S_CRT;
1366 } else if (strlencmp(cmd, "crt_disable") == 0) {
1367 disable |= TP_ACPI_VIDEO_S_CRT;
1368 } else if (video_supported == TPACPI_VIDEO_NEW &&
1369 strlencmp(cmd, "dvi_enable") == 0) {
1370 enable |= TP_ACPI_VIDEO_S_DVI;
1371 } else if (video_supported == TPACPI_VIDEO_NEW &&
1372 strlencmp(cmd, "dvi_disable") == 0) {
1373 disable |= TP_ACPI_VIDEO_S_DVI;
1374 } else if (strlencmp(cmd, "auto_enable") == 0) {
1375 res = video_autosw_set(1);
1376 if (res)
1377 return res;
1378 } else if (strlencmp(cmd, "auto_disable") == 0) {
1379 res = video_autosw_set(0);
1380 if (res)
1381 return res;
1382 } else if (strlencmp(cmd, "video_switch") == 0) {
1383 res = video_outputsw_cycle();
1384 if (res)
1385 return res;
1386 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1387 res = video_expand_toggle();
1388 if (res)
1389 return res;
1390 } else
1391 return -EINVAL;
1394 if (enable || disable) {
1395 status = video_outputsw_get();
1396 if (status < 0)
1397 return status;
1398 res = video_outputsw_set((status & ~disable) | enable);
1399 if (res)
1400 return res;
1403 return 0;
1406 static struct ibm_struct video_driver_data = {
1407 .name = "video",
1408 .read = video_read,
1409 .write = video_write,
1410 .exit = video_exit,
1413 /*************************************************************************
1414 * Light (thinklight) subdriver
1417 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1418 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1420 static int __init light_init(struct ibm_init_struct *iibm)
1422 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1424 IBM_ACPIHANDLE_INIT(ledb);
1425 IBM_ACPIHANDLE_INIT(lght);
1426 IBM_ACPIHANDLE_INIT(cmos);
1428 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1429 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1431 if (tp_features.light)
1432 /* light status not supported on
1433 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1434 tp_features.light_status =
1435 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1437 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1438 str_supported(tp_features.light));
1440 return (tp_features.light)? 0 : 1;
1443 static int light_read(char *p)
1445 int len = 0;
1446 int status = 0;
1448 if (!tp_features.light) {
1449 len += sprintf(p + len, "status:\t\tnot supported\n");
1450 } else if (!tp_features.light_status) {
1451 len += sprintf(p + len, "status:\t\tunknown\n");
1452 len += sprintf(p + len, "commands:\ton, off\n");
1453 } else {
1454 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1455 return -EIO;
1456 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1457 len += sprintf(p + len, "commands:\ton, off\n");
1460 return len;
1463 static int light_write(char *buf)
1465 int cmos_cmd, lght_cmd;
1466 char *cmd;
1467 int success;
1469 if (!tp_features.light)
1470 return -ENODEV;
1472 while ((cmd = next_cmd(&buf))) {
1473 if (strlencmp(cmd, "on") == 0) {
1474 cmos_cmd = 0x0c;
1475 lght_cmd = 1;
1476 } else if (strlencmp(cmd, "off") == 0) {
1477 cmos_cmd = 0x0d;
1478 lght_cmd = 0;
1479 } else
1480 return -EINVAL;
1482 success = cmos_handle ?
1483 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1484 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1485 if (!success)
1486 return -EIO;
1489 return 0;
1492 static struct ibm_struct light_driver_data = {
1493 .name = "light",
1494 .read = light_read,
1495 .write = light_write,
1498 /*************************************************************************
1499 * Dock subdriver
1502 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1504 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1505 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1506 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1507 "\\_SB.PCI.ISA.SLCE", /* 570 */
1508 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1510 /* don't list other alternatives as we install a notify handler on the 570 */
1511 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1513 #define dock_docked() (_sta(dock_handle) & 1)
1515 static int __init dock_init(struct ibm_init_struct *iibm)
1517 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1519 IBM_ACPIHANDLE_INIT(dock);
1520 IBM_ACPIHANDLE_INIT(pci);
1522 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1523 str_supported(dock_handle != NULL));
1525 return (dock_handle)? 0 : 1;
1528 static void dock_notify(struct ibm_struct *ibm, u32 event)
1530 int docked = dock_docked();
1531 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1533 if (event == 1 && !pci) /* 570 */
1534 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1535 else if (event == 1 && pci) /* 570 */
1536 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1537 else if (event == 3 && docked)
1538 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1539 else if (event == 3 && !docked)
1540 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1541 else if (event == 0 && docked)
1542 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1543 else {
1544 printk(IBM_ERR "unknown dock event %d, status %d\n",
1545 event, _sta(dock_handle));
1546 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1550 static int dock_read(char *p)
1552 int len = 0;
1553 int docked = dock_docked();
1555 if (!dock_handle)
1556 len += sprintf(p + len, "status:\t\tnot supported\n");
1557 else if (!docked)
1558 len += sprintf(p + len, "status:\t\tundocked\n");
1559 else {
1560 len += sprintf(p + len, "status:\t\tdocked\n");
1561 len += sprintf(p + len, "commands:\tdock, undock\n");
1564 return len;
1567 static int dock_write(char *buf)
1569 char *cmd;
1571 if (!dock_docked())
1572 return -ENODEV;
1574 while ((cmd = next_cmd(&buf))) {
1575 if (strlencmp(cmd, "undock") == 0) {
1576 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1577 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1578 return -EIO;
1579 } else if (strlencmp(cmd, "dock") == 0) {
1580 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1581 return -EIO;
1582 } else
1583 return -EINVAL;
1586 return 0;
1589 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1591 .notify = dock_notify,
1592 .handle = &dock_handle,
1593 .type = ACPI_SYSTEM_NOTIFY,
1596 .hid = IBM_PCI_HID,
1597 .notify = dock_notify,
1598 .handle = &pci_handle,
1599 .type = ACPI_SYSTEM_NOTIFY,
1603 static struct ibm_struct dock_driver_data[2] = {
1605 .name = "dock",
1606 .read = dock_read,
1607 .write = dock_write,
1608 .acpi = &ibm_dock_acpidriver[0],
1611 .name = "dock",
1612 .acpi = &ibm_dock_acpidriver[1],
1616 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1618 /*************************************************************************
1619 * Bay subdriver
1622 #ifdef CONFIG_THINKPAD_ACPI_BAY
1623 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1624 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1625 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1626 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1627 ); /* A21e, R30, R31 */
1628 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1629 "_EJ0", /* all others */
1630 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1631 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1632 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1633 ); /* all others */
1634 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1635 "_EJ0", /* 770x */
1636 ); /* all others */
1638 static int __init bay_init(struct ibm_init_struct *iibm)
1640 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1642 IBM_ACPIHANDLE_INIT(bay);
1643 if (bay_handle)
1644 IBM_ACPIHANDLE_INIT(bay_ej);
1645 IBM_ACPIHANDLE_INIT(bay2);
1646 if (bay2_handle)
1647 IBM_ACPIHANDLE_INIT(bay2_ej);
1649 tp_features.bay_status = bay_handle &&
1650 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1651 tp_features.bay_status2 = bay2_handle &&
1652 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1654 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1655 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1656 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1657 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1659 vdbg_printk(TPACPI_DBG_INIT,
1660 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1661 str_supported(tp_features.bay_status),
1662 str_supported(tp_features.bay_eject),
1663 str_supported(tp_features.bay_status2),
1664 str_supported(tp_features.bay_eject2));
1666 return (tp_features.bay_status || tp_features.bay_eject ||
1667 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1670 static void bay_notify(struct ibm_struct *ibm, u32 event)
1672 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1675 #define bay_occupied(b) (_sta(b##_handle) & 1)
1677 static int bay_read(char *p)
1679 int len = 0;
1680 int occupied = bay_occupied(bay);
1681 int occupied2 = bay_occupied(bay2);
1682 int eject, eject2;
1684 len += sprintf(p + len, "status:\t\t%s\n",
1685 tp_features.bay_status ?
1686 (occupied ? "occupied" : "unoccupied") :
1687 "not supported");
1688 if (tp_features.bay_status2)
1689 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1690 "occupied" : "unoccupied");
1692 eject = tp_features.bay_eject && occupied;
1693 eject2 = tp_features.bay_eject2 && occupied2;
1695 if (eject && eject2)
1696 len += sprintf(p + len, "commands:\teject, eject2\n");
1697 else if (eject)
1698 len += sprintf(p + len, "commands:\teject\n");
1699 else if (eject2)
1700 len += sprintf(p + len, "commands:\teject2\n");
1702 return len;
1705 static int bay_write(char *buf)
1707 char *cmd;
1709 if (!tp_features.bay_eject && !tp_features.bay_eject2)
1710 return -ENODEV;
1712 while ((cmd = next_cmd(&buf))) {
1713 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
1714 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1715 return -EIO;
1716 } else if (tp_features.bay_eject2 &&
1717 strlencmp(cmd, "eject2") == 0) {
1718 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1719 return -EIO;
1720 } else
1721 return -EINVAL;
1724 return 0;
1727 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1728 .notify = bay_notify,
1729 .handle = &bay_handle,
1730 .type = ACPI_SYSTEM_NOTIFY,
1733 static struct ibm_struct bay_driver_data = {
1734 .name = "bay",
1735 .read = bay_read,
1736 .write = bay_write,
1737 .acpi = &ibm_bay_acpidriver,
1740 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1742 /*************************************************************************
1743 * CMOS subdriver
1746 static int __init cmos_init(struct ibm_init_struct *iibm)
1748 vdbg_printk(TPACPI_DBG_INIT,
1749 "initializing cmos commands subdriver\n");
1751 IBM_ACPIHANDLE_INIT(cmos);
1753 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1754 str_supported(cmos_handle != NULL));
1755 return (cmos_handle)? 0 : 1;
1758 static int cmos_read(char *p)
1760 int len = 0;
1762 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1763 R30, R31, T20-22, X20-21 */
1764 if (!cmos_handle)
1765 len += sprintf(p + len, "status:\t\tnot supported\n");
1766 else {
1767 len += sprintf(p + len, "status:\t\tsupported\n");
1768 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1771 return len;
1774 static int cmos_write(char *buf)
1776 char *cmd;
1777 int cmos_cmd, res;
1779 while ((cmd = next_cmd(&buf))) {
1780 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1781 cmos_cmd >= 0 && cmos_cmd <= 21) {
1782 /* cmos_cmd set */
1783 } else
1784 return -EINVAL;
1786 res = issue_thinkpad_cmos_command(cmos_cmd);
1787 if (res)
1788 return res;
1791 return 0;
1794 static struct ibm_struct cmos_driver_data = {
1795 .name = "cmos",
1796 .read = cmos_read,
1797 .write = cmos_write,
1800 /*************************************************************************
1801 * LED subdriver
1804 static enum led_access_mode led_supported;
1806 IBM_HANDLE(led, ec, "SLED", /* 570 */
1807 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1808 "LED", /* all others */
1809 ); /* R30, R31 */
1811 static int __init led_init(struct ibm_init_struct *iibm)
1813 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1815 IBM_ACPIHANDLE_INIT(led);
1817 if (!led_handle)
1818 /* led not supported on R30, R31 */
1819 led_supported = TPACPI_LED_NONE;
1820 else if (strlencmp(led_path, "SLED") == 0)
1821 /* 570 */
1822 led_supported = TPACPI_LED_570;
1823 else if (strlencmp(led_path, "SYSL") == 0)
1824 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1825 led_supported = TPACPI_LED_OLD;
1826 else
1827 /* all others */
1828 led_supported = TPACPI_LED_NEW;
1830 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1831 str_supported(led_supported), led_supported);
1833 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
1836 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1838 static int led_read(char *p)
1840 int len = 0;
1842 if (!led_supported) {
1843 len += sprintf(p + len, "status:\t\tnot supported\n");
1844 return len;
1846 len += sprintf(p + len, "status:\t\tsupported\n");
1848 if (led_supported == TPACPI_LED_570) {
1849 /* 570 */
1850 int i, status;
1851 for (i = 0; i < 8; i++) {
1852 if (!acpi_evalf(ec_handle,
1853 &status, "GLED", "dd", 1 << i))
1854 return -EIO;
1855 len += sprintf(p + len, "%d:\t\t%s\n",
1856 i, led_status(status));
1860 len += sprintf(p + len, "commands:\t"
1861 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1863 return len;
1866 /* off, on, blink */
1867 static const int led_sled_arg1[] = { 0, 1, 3 };
1868 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1869 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1870 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1872 static int led_write(char *buf)
1874 char *cmd;
1875 int led, ind, ret;
1877 if (!led_supported)
1878 return -ENODEV;
1880 while ((cmd = next_cmd(&buf))) {
1881 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1882 return -EINVAL;
1884 if (strstr(cmd, "off")) {
1885 ind = 0;
1886 } else if (strstr(cmd, "on")) {
1887 ind = 1;
1888 } else if (strstr(cmd, "blink")) {
1889 ind = 2;
1890 } else
1891 return -EINVAL;
1893 if (led_supported == TPACPI_LED_570) {
1894 /* 570 */
1895 led = 1 << led;
1896 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1897 led, led_sled_arg1[ind]))
1898 return -EIO;
1899 } else if (led_supported == TPACPI_LED_OLD) {
1900 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1901 led = 1 << led;
1902 ret = ec_write(TPACPI_LED_EC_HLMS, led);
1903 if (ret >= 0)
1904 ret =
1905 ec_write(TPACPI_LED_EC_HLBL,
1906 led * led_exp_hlbl[ind]);
1907 if (ret >= 0)
1908 ret =
1909 ec_write(TPACPI_LED_EC_HLCL,
1910 led * led_exp_hlcl[ind]);
1911 if (ret < 0)
1912 return ret;
1913 } else {
1914 /* all others */
1915 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1916 led, led_led_arg1[ind]))
1917 return -EIO;
1921 return 0;
1924 static struct ibm_struct led_driver_data = {
1925 .name = "led",
1926 .read = led_read,
1927 .write = led_write,
1930 /*************************************************************************
1931 * Beep subdriver
1934 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1936 static int __init beep_init(struct ibm_init_struct *iibm)
1938 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1940 IBM_ACPIHANDLE_INIT(beep);
1942 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1943 str_supported(beep_handle != NULL));
1945 return (beep_handle)? 0 : 1;
1948 static int beep_read(char *p)
1950 int len = 0;
1952 if (!beep_handle)
1953 len += sprintf(p + len, "status:\t\tnot supported\n");
1954 else {
1955 len += sprintf(p + len, "status:\t\tsupported\n");
1956 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1959 return len;
1962 static int beep_write(char *buf)
1964 char *cmd;
1965 int beep_cmd;
1967 if (!beep_handle)
1968 return -ENODEV;
1970 while ((cmd = next_cmd(&buf))) {
1971 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1972 beep_cmd >= 0 && beep_cmd <= 17) {
1973 /* beep_cmd set */
1974 } else
1975 return -EINVAL;
1976 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1977 return -EIO;
1980 return 0;
1983 static struct ibm_struct beep_driver_data = {
1984 .name = "beep",
1985 .read = beep_read,
1986 .write = beep_write,
1989 /*************************************************************************
1990 * Thermal subdriver
1993 static enum thermal_access_mode thermal_read_mode;
1995 /* sysfs temp##_input -------------------------------------------------- */
1997 static ssize_t thermal_temp_input_show(struct device *dev,
1998 struct device_attribute *attr,
1999 char *buf)
2001 struct sensor_device_attribute *sensor_attr =
2002 to_sensor_dev_attr(attr);
2003 int idx = sensor_attr->index;
2004 s32 value;
2005 int res;
2007 res = thermal_get_sensor(idx, &value);
2008 if (res)
2009 return res;
2010 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2011 return -ENXIO;
2013 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2016 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2017 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2019 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2020 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2021 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2022 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2023 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2024 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2025 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2026 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2027 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2028 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2029 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2030 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2031 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2032 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2033 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2034 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2035 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2038 #define THERMAL_ATTRS(X) \
2039 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2041 static struct attribute *thermal_temp_input_attr[] = {
2042 THERMAL_ATTRS(8),
2043 THERMAL_ATTRS(9),
2044 THERMAL_ATTRS(10),
2045 THERMAL_ATTRS(11),
2046 THERMAL_ATTRS(12),
2047 THERMAL_ATTRS(13),
2048 THERMAL_ATTRS(14),
2049 THERMAL_ATTRS(15),
2050 THERMAL_ATTRS(0),
2051 THERMAL_ATTRS(1),
2052 THERMAL_ATTRS(2),
2053 THERMAL_ATTRS(3),
2054 THERMAL_ATTRS(4),
2055 THERMAL_ATTRS(5),
2056 THERMAL_ATTRS(6),
2057 THERMAL_ATTRS(7),
2058 NULL
2061 static const struct attribute_group thermal_temp_input16_group = {
2062 .attrs = thermal_temp_input_attr
2065 static const struct attribute_group thermal_temp_input8_group = {
2066 .attrs = &thermal_temp_input_attr[8]
2069 #undef THERMAL_SENSOR_ATTR_TEMP
2070 #undef THERMAL_ATTRS
2072 /* --------------------------------------------------------------------- */
2074 static int __init thermal_init(struct ibm_init_struct *iibm)
2076 u8 t, ta1, ta2;
2077 int i;
2078 int acpi_tmp7;
2079 int res;
2081 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2083 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2085 if (ibm_thinkpad_ec_found && experimental) {
2087 * Direct EC access mode: sensors at registers
2088 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2089 * non-implemented, thermal sensors return 0x80 when
2090 * not available
2093 ta1 = ta2 = 0;
2094 for (i = 0; i < 8; i++) {
2095 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2096 ta1 |= t;
2097 } else {
2098 ta1 = 0;
2099 break;
2101 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2102 ta2 |= t;
2103 } else {
2104 ta1 = 0;
2105 break;
2108 if (ta1 == 0) {
2109 /* This is sheer paranoia, but we handle it anyway */
2110 if (acpi_tmp7) {
2111 printk(IBM_ERR
2112 "ThinkPad ACPI EC access misbehaving, "
2113 "falling back to ACPI TMPx access mode\n");
2114 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2115 } else {
2116 printk(IBM_ERR
2117 "ThinkPad ACPI EC access misbehaving, "
2118 "disabling thermal sensors access\n");
2119 thermal_read_mode = TPACPI_THERMAL_NONE;
2121 } else {
2122 thermal_read_mode =
2123 (ta2 != 0) ?
2124 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2126 } else if (acpi_tmp7) {
2127 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2128 /* 600e/x, 770e, 770x */
2129 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2130 } else {
2131 /* Standard ACPI TMPx access, max 8 sensors */
2132 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2134 } else {
2135 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2136 thermal_read_mode = TPACPI_THERMAL_NONE;
2139 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2140 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2141 thermal_read_mode);
2143 switch(thermal_read_mode) {
2144 case TPACPI_THERMAL_TPEC_16:
2145 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2146 &thermal_temp_input16_group);
2147 if (res)
2148 return res;
2149 break;
2150 case TPACPI_THERMAL_TPEC_8:
2151 case TPACPI_THERMAL_ACPI_TMP07:
2152 case TPACPI_THERMAL_ACPI_UPDT:
2153 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2154 &thermal_temp_input8_group);
2155 if (res)
2156 return res;
2157 break;
2158 case TPACPI_THERMAL_NONE:
2159 default:
2160 return 1;
2163 return 0;
2166 static void thermal_exit(void)
2168 switch(thermal_read_mode) {
2169 case TPACPI_THERMAL_TPEC_16:
2170 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2171 &thermal_temp_input16_group);
2172 break;
2173 case TPACPI_THERMAL_TPEC_8:
2174 case TPACPI_THERMAL_ACPI_TMP07:
2175 case TPACPI_THERMAL_ACPI_UPDT:
2176 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2177 &thermal_temp_input16_group);
2178 break;
2179 case TPACPI_THERMAL_NONE:
2180 default:
2181 break;
2185 /* idx is zero-based */
2186 static int thermal_get_sensor(int idx, s32 *value)
2188 int t;
2189 s8 tmp;
2190 char tmpi[5];
2192 t = TP_EC_THERMAL_TMP0;
2194 switch (thermal_read_mode) {
2195 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2196 case TPACPI_THERMAL_TPEC_16:
2197 if (idx >= 8 && idx <= 15) {
2198 t = TP_EC_THERMAL_TMP8;
2199 idx -= 8;
2201 /* fallthrough */
2202 #endif
2203 case TPACPI_THERMAL_TPEC_8:
2204 if (idx <= 7) {
2205 if (!acpi_ec_read(t + idx, &tmp))
2206 return -EIO;
2207 *value = tmp * 1000;
2208 return 0;
2210 break;
2212 case TPACPI_THERMAL_ACPI_UPDT:
2213 if (idx <= 7) {
2214 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2215 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2216 return -EIO;
2217 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2218 return -EIO;
2219 *value = (t - 2732) * 100;
2220 return 0;
2222 break;
2224 case TPACPI_THERMAL_ACPI_TMP07:
2225 if (idx <= 7) {
2226 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2227 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2228 return -EIO;
2229 *value = t * 1000;
2230 return 0;
2232 break;
2234 case TPACPI_THERMAL_NONE:
2235 default:
2236 return -ENOSYS;
2239 return -EINVAL;
2242 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2244 int res, i;
2245 int n;
2247 n = 8;
2248 i = 0;
2250 if (!s)
2251 return -EINVAL;
2253 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2254 n = 16;
2256 for(i = 0 ; i < n; i++) {
2257 res = thermal_get_sensor(i, &s->temp[i]);
2258 if (res)
2259 return res;
2262 return n;
2265 static int thermal_read(char *p)
2267 int len = 0;
2268 int n, i;
2269 struct ibm_thermal_sensors_struct t;
2271 n = thermal_get_sensors(&t);
2272 if (unlikely(n < 0))
2273 return n;
2275 len += sprintf(p + len, "temperatures:\t");
2277 if (n > 0) {
2278 for (i = 0; i < (n - 1); i++)
2279 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2280 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2281 } else
2282 len += sprintf(p + len, "not supported\n");
2284 return len;
2287 static struct ibm_struct thermal_driver_data = {
2288 .name = "thermal",
2289 .read = thermal_read,
2290 .exit = thermal_exit,
2293 /*************************************************************************
2294 * EC Dump subdriver
2297 static u8 ecdump_regs[256];
2299 static int ecdump_read(char *p)
2301 int len = 0;
2302 int i, j;
2303 u8 v;
2305 len += sprintf(p + len, "EC "
2306 " +00 +01 +02 +03 +04 +05 +06 +07"
2307 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2308 for (i = 0; i < 256; i += 16) {
2309 len += sprintf(p + len, "EC 0x%02x:", i);
2310 for (j = 0; j < 16; j++) {
2311 if (!acpi_ec_read(i + j, &v))
2312 break;
2313 if (v != ecdump_regs[i + j])
2314 len += sprintf(p + len, " *%02x", v);
2315 else
2316 len += sprintf(p + len, " %02x", v);
2317 ecdump_regs[i + j] = v;
2319 len += sprintf(p + len, "\n");
2320 if (j != 16)
2321 break;
2324 /* These are way too dangerous to advertise openly... */
2325 #if 0
2326 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2327 " (<offset> is 00-ff, <value> is 00-ff)\n");
2328 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2329 " (<offset> is 00-ff, <value> is 0-255)\n");
2330 #endif
2331 return len;
2334 static int ecdump_write(char *buf)
2336 char *cmd;
2337 int i, v;
2339 while ((cmd = next_cmd(&buf))) {
2340 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2341 /* i and v set */
2342 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2343 /* i and v set */
2344 } else
2345 return -EINVAL;
2346 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2347 if (!acpi_ec_write(i, v))
2348 return -EIO;
2349 } else
2350 return -EINVAL;
2353 return 0;
2356 static struct ibm_struct ecdump_driver_data = {
2357 .name = "ecdump",
2358 .read = ecdump_read,
2359 .write = ecdump_write,
2360 .flags.experimental = 1,
2363 /*************************************************************************
2364 * Backlight/brightness subdriver
2367 static struct backlight_device *ibm_backlight_device = NULL;
2369 static struct backlight_ops ibm_backlight_data = {
2370 .get_brightness = brightness_get,
2371 .update_status = brightness_update_status,
2374 static int __init brightness_init(struct ibm_init_struct *iibm)
2376 int b;
2378 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2380 b = brightness_get(NULL);
2381 if (b < 0)
2382 return b;
2384 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
2385 &ibm_backlight_data);
2386 if (IS_ERR(ibm_backlight_device)) {
2387 printk(IBM_ERR "Could not register backlight device\n");
2388 return PTR_ERR(ibm_backlight_device);
2390 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2392 ibm_backlight_device->props.max_brightness = 7;
2393 ibm_backlight_device->props.brightness = b;
2394 backlight_update_status(ibm_backlight_device);
2396 return 0;
2399 static void brightness_exit(void)
2401 if (ibm_backlight_device) {
2402 vdbg_printk(TPACPI_DBG_EXIT,
2403 "calling backlight_device_unregister()\n");
2404 backlight_device_unregister(ibm_backlight_device);
2405 ibm_backlight_device = NULL;
2409 static int brightness_update_status(struct backlight_device *bd)
2411 return brightness_set(
2412 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2413 bd->props.power == FB_BLANK_UNBLANK) ?
2414 bd->props.brightness : 0);
2417 static int brightness_get(struct backlight_device *bd)
2419 u8 level;
2420 if (!acpi_ec_read(brightness_offset, &level))
2421 return -EIO;
2423 level &= 0x7;
2425 return level;
2428 static int brightness_set(int value)
2430 int cmos_cmd, inc, i;
2431 int current_value = brightness_get(NULL);
2433 value &= 7;
2435 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2436 inc = value > current_value ? 1 : -1;
2437 for (i = current_value; i != value; i += inc) {
2438 if (issue_thinkpad_cmos_command(cmos_cmd))
2439 return -EIO;
2440 if (!acpi_ec_write(brightness_offset, i + inc))
2441 return -EIO;
2444 return 0;
2447 static int brightness_read(char *p)
2449 int len = 0;
2450 int level;
2452 if ((level = brightness_get(NULL)) < 0) {
2453 len += sprintf(p + len, "level:\t\tunreadable\n");
2454 } else {
2455 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2456 len += sprintf(p + len, "commands:\tup, down\n");
2457 len += sprintf(p + len, "commands:\tlevel <level>"
2458 " (<level> is 0-7)\n");
2461 return len;
2464 static int brightness_write(char *buf)
2466 int level;
2467 int new_level;
2468 char *cmd;
2470 while ((cmd = next_cmd(&buf))) {
2471 if ((level = brightness_get(NULL)) < 0)
2472 return level;
2473 level &= 7;
2475 if (strlencmp(cmd, "up") == 0) {
2476 new_level = level == 7 ? 7 : level + 1;
2477 } else if (strlencmp(cmd, "down") == 0) {
2478 new_level = level == 0 ? 0 : level - 1;
2479 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2480 new_level >= 0 && new_level <= 7) {
2481 /* new_level set */
2482 } else
2483 return -EINVAL;
2485 brightness_set(new_level);
2488 return 0;
2491 static struct ibm_struct brightness_driver_data = {
2492 .name = "brightness",
2493 .read = brightness_read,
2494 .write = brightness_write,
2495 .exit = brightness_exit,
2498 /*************************************************************************
2499 * Volume subdriver
2502 static int volume_read(char *p)
2504 int len = 0;
2505 u8 level;
2507 if (!acpi_ec_read(volume_offset, &level)) {
2508 len += sprintf(p + len, "level:\t\tunreadable\n");
2509 } else {
2510 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2511 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2512 len += sprintf(p + len, "commands:\tup, down, mute\n");
2513 len += sprintf(p + len, "commands:\tlevel <level>"
2514 " (<level> is 0-15)\n");
2517 return len;
2520 static int volume_write(char *buf)
2522 int cmos_cmd, inc, i;
2523 u8 level, mute;
2524 int new_level, new_mute;
2525 char *cmd;
2527 while ((cmd = next_cmd(&buf))) {
2528 if (!acpi_ec_read(volume_offset, &level))
2529 return -EIO;
2530 new_mute = mute = level & 0x40;
2531 new_level = level = level & 0xf;
2533 if (strlencmp(cmd, "up") == 0) {
2534 if (mute)
2535 new_mute = 0;
2536 else
2537 new_level = level == 15 ? 15 : level + 1;
2538 } else if (strlencmp(cmd, "down") == 0) {
2539 if (mute)
2540 new_mute = 0;
2541 else
2542 new_level = level == 0 ? 0 : level - 1;
2543 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2544 new_level >= 0 && new_level <= 15) {
2545 /* new_level set */
2546 } else if (strlencmp(cmd, "mute") == 0) {
2547 new_mute = 0x40;
2548 } else
2549 return -EINVAL;
2551 if (new_level != level) { /* mute doesn't change */
2552 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2553 inc = new_level > level ? 1 : -1;
2555 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2556 !acpi_ec_write(volume_offset, level)))
2557 return -EIO;
2559 for (i = level; i != new_level; i += inc)
2560 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2561 !acpi_ec_write(volume_offset, i + inc))
2562 return -EIO;
2564 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2565 !acpi_ec_write(volume_offset,
2566 new_level + mute)))
2567 return -EIO;
2570 if (new_mute != mute) { /* level doesn't change */
2571 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2573 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2574 !acpi_ec_write(volume_offset, level + new_mute))
2575 return -EIO;
2579 return 0;
2582 static struct ibm_struct volume_driver_data = {
2583 .name = "volume",
2584 .read = volume_read,
2585 .write = volume_write,
2588 /*************************************************************************
2589 * Fan subdriver
2593 * FAN ACCESS MODES
2595 * TPACPI_FAN_RD_ACPI_GFAN:
2596 * ACPI GFAN method: returns fan level
2598 * see TPACPI_FAN_WR_ACPI_SFAN
2599 * EC 0x2f (HFSP) not available if GFAN exists
2601 * TPACPI_FAN_WR_ACPI_SFAN:
2602 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2604 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2605 * it for writing.
2607 * TPACPI_FAN_WR_TPEC:
2608 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2609 * Supported on almost all ThinkPads
2611 * Fan speed changes of any sort (including those caused by the
2612 * disengaged mode) are usually done slowly by the firmware as the
2613 * maximum ammount of fan duty cycle change per second seems to be
2614 * limited.
2616 * Reading is not available if GFAN exists.
2617 * Writing is not available if SFAN exists.
2619 * Bits
2620 * 7 automatic mode engaged;
2621 * (default operation mode of the ThinkPad)
2622 * fan level is ignored in this mode.
2623 * 6 full speed mode (takes precedence over bit 7);
2624 * not available on all thinkpads. May disable
2625 * the tachometer while the fan controller ramps up
2626 * the speed (which can take up to a few *minutes*).
2627 * Speeds up fan to 100% duty-cycle, which is far above
2628 * the standard RPM levels. It is not impossible that
2629 * it could cause hardware damage.
2630 * 5-3 unused in some models. Extra bits for fan level
2631 * in others, but still useless as all values above
2632 * 7 map to the same speed as level 7 in these models.
2633 * 2-0 fan level (0..7 usually)
2634 * 0x00 = stop
2635 * 0x07 = max (set when temperatures critical)
2636 * Some ThinkPads may have other levels, see
2637 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2639 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2640 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2641 * does so, its initial value is meaningless (0x07).
2643 * For firmware bugs, refer to:
2644 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2646 * ----
2648 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2649 * Main fan tachometer reading (in RPM)
2651 * This register is present on all ThinkPads with a new-style EC, and
2652 * it is known not to be present on the A21m/e, and T22, as there is
2653 * something else in offset 0x84 according to the ACPI DSDT. Other
2654 * ThinkPads from this same time period (and earlier) probably lack the
2655 * tachometer as well.
2657 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2658 * was never fixed by IBM to report the EC firmware version string
2659 * probably support the tachometer (like the early X models), so
2660 * detecting it is quite hard. We need more data to know for sure.
2662 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2663 * might result.
2665 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2666 * mode.
2668 * For firmware bugs, refer to:
2669 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2671 * TPACPI_FAN_WR_ACPI_FANS:
2672 * ThinkPad X31, X40, X41. Not available in the X60.
2674 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2675 * high speed. ACPI DSDT seems to map these three speeds to levels
2676 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2677 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2679 * The speeds are stored on handles
2680 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2682 * There are three default speed sets, acessible as handles:
2683 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2685 * ACPI DSDT switches which set is in use depending on various
2686 * factors.
2688 * TPACPI_FAN_WR_TPEC is also available and should be used to
2689 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2690 * but the ACPI tables just mention level 7.
2693 static enum fan_status_access_mode fan_status_access_mode;
2694 static enum fan_control_access_mode fan_control_access_mode;
2695 static enum fan_control_commands fan_control_commands;
2697 static u8 fan_control_initial_status;
2698 static u8 fan_control_desired_level;
2700 static void fan_watchdog_fire(struct work_struct *ignored);
2701 static int fan_watchdog_maxinterval;
2702 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
2704 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2705 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2706 "\\FSPD", /* 600e/x, 770e, 770x */
2707 ); /* all others */
2708 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2709 "JFNS", /* 770x-JL */
2710 ); /* all others */
2713 * SYSFS fan layout: hwmon compatible (device)
2715 * pwm*_enable:
2716 * 0: "disengaged" mode
2717 * 1: manual mode
2718 * 2: native EC "auto" mode (recommended, hardware default)
2720 * pwm*: set speed in manual mode, ignored otherwise.
2721 * 0 is level 0; 255 is level 7. Intermediate points done with linear
2722 * interpolation.
2724 * fan*_input: tachometer reading, RPM
2727 * SYSFS fan layout: extensions
2729 * fan_watchdog (driver):
2730 * fan watchdog interval in seconds, 0 disables (default), max 120
2733 /* sysfs fan pwm1_enable ----------------------------------------------- */
2734 static ssize_t fan_pwm1_enable_show(struct device *dev,
2735 struct device_attribute *attr,
2736 char *buf)
2738 int res, mode;
2739 u8 status;
2741 res = fan_get_status_safe(&status);
2742 if (res)
2743 return res;
2745 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2746 if (status != fan_control_initial_status) {
2747 tp_features.fan_ctrl_status_undef = 0;
2748 } else {
2749 /* Return most likely status. In fact, it
2750 * might be the only possible status */
2751 status = TP_EC_FAN_AUTO;
2755 if (status & TP_EC_FAN_FULLSPEED) {
2756 mode = 0;
2757 } else if (status & TP_EC_FAN_AUTO) {
2758 mode = 2;
2759 } else
2760 mode = 1;
2762 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
2765 static ssize_t fan_pwm1_enable_store(struct device *dev,
2766 struct device_attribute *attr,
2767 const char *buf, size_t count)
2769 unsigned long t;
2770 int res, level;
2772 if (parse_strtoul(buf, 2, &t))
2773 return -EINVAL;
2775 switch (t) {
2776 case 0:
2777 level = TP_EC_FAN_FULLSPEED;
2778 break;
2779 case 1:
2780 level = TPACPI_FAN_LAST_LEVEL;
2781 break;
2782 case 2:
2783 level = TP_EC_FAN_AUTO;
2784 break;
2785 case 3:
2786 /* reserved for software-controlled auto mode */
2787 return -ENOSYS;
2788 default:
2789 return -EINVAL;
2792 res = fan_set_level_safe(level);
2793 if (res < 0)
2794 return res;
2796 fan_watchdog_reset();
2798 return count;
2801 static struct device_attribute dev_attr_fan_pwm1_enable =
2802 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
2803 fan_pwm1_enable_show, fan_pwm1_enable_store);
2805 /* sysfs fan pwm1 ------------------------------------------------------ */
2806 static ssize_t fan_pwm1_show(struct device *dev,
2807 struct device_attribute *attr,
2808 char *buf)
2810 int res;
2811 u8 status;
2813 res = fan_get_status_safe(&status);
2814 if (res)
2815 return res;
2817 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2818 if (status != fan_control_initial_status) {
2819 tp_features.fan_ctrl_status_undef = 0;
2820 } else {
2821 status = TP_EC_FAN_AUTO;
2825 if ((status &
2826 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
2827 status = fan_control_desired_level;
2829 if (status > 7)
2830 status = 7;
2832 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
2835 static ssize_t fan_pwm1_store(struct device *dev,
2836 struct device_attribute *attr,
2837 const char *buf, size_t count)
2839 unsigned long s;
2840 int rc;
2841 u8 status, newlevel;
2843 if (parse_strtoul(buf, 255, &s))
2844 return -EINVAL;
2846 /* scale down from 0-255 to 0-7 */
2847 newlevel = (s >> 5) & 0x07;
2849 rc = mutex_lock_interruptible(&fan_mutex);
2850 if (rc < 0)
2851 return rc;
2853 rc = fan_get_status(&status);
2854 if (!rc && (status &
2855 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
2856 rc = fan_set_level(newlevel);
2857 if (!rc)
2858 fan_update_desired_level(newlevel);
2859 fan_watchdog_reset();
2862 mutex_unlock(&fan_mutex);
2863 return (rc)? rc : count;
2866 static struct device_attribute dev_attr_fan_pwm1 =
2867 __ATTR(pwm1, S_IWUSR | S_IRUGO,
2868 fan_pwm1_show, fan_pwm1_store);
2870 /* sysfs fan fan1_input ------------------------------------------------ */
2871 static ssize_t fan_fan1_input_show(struct device *dev,
2872 struct device_attribute *attr,
2873 char *buf)
2875 int res;
2876 unsigned int speed;
2878 res = fan_get_speed(&speed);
2879 if (res < 0)
2880 return res;
2882 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
2885 static struct device_attribute dev_attr_fan_fan1_input =
2886 __ATTR(fan1_input, S_IRUGO,
2887 fan_fan1_input_show, NULL);
2889 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
2890 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
2891 char *buf)
2893 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
2896 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
2897 const char *buf, size_t count)
2899 unsigned long t;
2901 if (parse_strtoul(buf, 120, &t))
2902 return -EINVAL;
2904 fan_watchdog_maxinterval = t;
2905 fan_watchdog_reset();
2907 return count;
2910 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
2911 fan_fan_watchdog_show, fan_fan_watchdog_store);
2913 /* --------------------------------------------------------------------- */
2914 static struct attribute *fan_attributes[] = {
2915 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
2916 &dev_attr_fan_fan1_input.attr,
2917 NULL
2920 static const struct attribute_group fan_attr_group = {
2921 .attrs = fan_attributes,
2924 static int __init fan_init(struct ibm_init_struct *iibm)
2926 int rc;
2928 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2930 mutex_init(&fan_mutex);
2931 fan_status_access_mode = TPACPI_FAN_NONE;
2932 fan_control_access_mode = TPACPI_FAN_WR_NONE;
2933 fan_control_commands = 0;
2934 fan_watchdog_maxinterval = 0;
2935 tp_features.fan_ctrl_status_undef = 0;
2936 fan_control_desired_level = 7;
2938 IBM_ACPIHANDLE_INIT(fans);
2939 IBM_ACPIHANDLE_INIT(gfan);
2940 IBM_ACPIHANDLE_INIT(sfan);
2942 if (gfan_handle) {
2943 /* 570, 600e/x, 770e, 770x */
2944 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
2945 } else {
2946 /* all other ThinkPads: note that even old-style
2947 * ThinkPad ECs supports the fan control register */
2948 if (likely(acpi_ec_read(fan_status_offset,
2949 &fan_control_initial_status))) {
2950 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
2952 /* In some ThinkPads, neither the EC nor the ACPI
2953 * DSDT initialize the fan status, and it ends up
2954 * being set to 0x07 when it *could* be either
2955 * 0x07 or 0x80.
2957 * Enable for TP-1Y (T43), TP-78 (R51e),
2958 * TP-76 (R52), TP-70 (T43, R52), which are known
2959 * to be buggy. */
2960 if (fan_control_initial_status == 0x07 &&
2961 ibm_thinkpad_ec_found &&
2962 ((ibm_thinkpad_ec_found[0] == '1' &&
2963 ibm_thinkpad_ec_found[1] == 'Y') ||
2964 (ibm_thinkpad_ec_found[0] == '7' &&
2965 (ibm_thinkpad_ec_found[1] == '6' ||
2966 ibm_thinkpad_ec_found[1] == '8' ||
2967 ibm_thinkpad_ec_found[1] == '0'))
2968 )) {
2969 printk(IBM_NOTICE
2970 "fan_init: initial fan status is "
2971 "unknown, assuming it is in auto "
2972 "mode\n");
2973 tp_features.fan_ctrl_status_undef = 1;
2975 } else {
2976 printk(IBM_ERR
2977 "ThinkPad ACPI EC access misbehaving, "
2978 "fan status and control unavailable\n");
2979 return 1;
2983 if (sfan_handle) {
2984 /* 570, 770x-JL */
2985 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
2986 fan_control_commands |=
2987 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
2988 } else {
2989 if (!gfan_handle) {
2990 /* gfan without sfan means no fan control */
2991 /* all other models implement TP EC 0x2f control */
2993 if (fans_handle) {
2994 /* X31, X40, X41 */
2995 fan_control_access_mode =
2996 TPACPI_FAN_WR_ACPI_FANS;
2997 fan_control_commands |=
2998 TPACPI_FAN_CMD_SPEED |
2999 TPACPI_FAN_CMD_LEVEL |
3000 TPACPI_FAN_CMD_ENABLE;
3001 } else {
3002 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3003 fan_control_commands |=
3004 TPACPI_FAN_CMD_LEVEL |
3005 TPACPI_FAN_CMD_ENABLE;
3010 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3011 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3012 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3013 fan_status_access_mode, fan_control_access_mode);
3015 /* update fan_control_desired_level */
3016 if (fan_status_access_mode != TPACPI_FAN_NONE)
3017 fan_get_status_safe(NULL);
3019 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3020 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3021 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3022 &fan_attr_group);
3023 if (!(rc < 0))
3024 rc = driver_create_file(&tpacpi_pdriver.driver,
3025 &driver_attr_fan_watchdog);
3026 if (rc < 0)
3027 return rc;
3028 return 0;
3029 } else
3030 return 1;
3034 * Call with fan_mutex held
3036 static void fan_update_desired_level(u8 status)
3038 if ((status &
3039 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3040 if (status > 7)
3041 fan_control_desired_level = 7;
3042 else
3043 fan_control_desired_level = status;
3047 static int fan_get_status(u8 *status)
3049 u8 s;
3051 /* TODO:
3052 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3054 switch (fan_status_access_mode) {
3055 case TPACPI_FAN_RD_ACPI_GFAN:
3056 /* 570, 600e/x, 770e, 770x */
3058 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3059 return -EIO;
3061 if (likely(status))
3062 *status = s & 0x07;
3064 break;
3066 case TPACPI_FAN_RD_TPEC:
3067 /* all except 570, 600e/x, 770e, 770x */
3068 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3069 return -EIO;
3071 if (likely(status))
3072 *status = s;
3074 break;
3076 default:
3077 return -ENXIO;
3080 return 0;
3083 static int fan_get_status_safe(u8 *status)
3085 int rc;
3086 u8 s;
3088 rc = mutex_lock_interruptible(&fan_mutex);
3089 if (rc < 0)
3090 return rc;
3091 rc = fan_get_status(&s);
3092 if (!rc)
3093 fan_update_desired_level(s);
3094 mutex_unlock(&fan_mutex);
3096 if (status)
3097 *status = s;
3099 return rc;
3102 static void fan_exit(void)
3104 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3106 /* FIXME: can we really do this unconditionally? */
3107 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3108 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3110 cancel_delayed_work(&fan_watchdog_task);
3111 flush_scheduled_work();
3114 static int fan_get_speed(unsigned int *speed)
3116 u8 hi, lo;
3118 switch (fan_status_access_mode) {
3119 case TPACPI_FAN_RD_TPEC:
3120 /* all except 570, 600e/x, 770e, 770x */
3121 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3122 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3123 return -EIO;
3125 if (likely(speed))
3126 *speed = (hi << 8) | lo;
3128 break;
3130 default:
3131 return -ENXIO;
3134 return 0;
3137 static void fan_watchdog_fire(struct work_struct *ignored)
3139 int rc;
3141 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3142 rc = fan_set_enable();
3143 if (rc < 0) {
3144 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3145 "will try again later...\n", -rc);
3146 /* reschedule for later */
3147 fan_watchdog_reset();
3151 static void fan_watchdog_reset(void)
3153 static int fan_watchdog_active = 0;
3155 if (fan_watchdog_active)
3156 cancel_delayed_work(&fan_watchdog_task);
3158 if (fan_watchdog_maxinterval > 0) {
3159 fan_watchdog_active = 1;
3160 if (!schedule_delayed_work(&fan_watchdog_task,
3161 msecs_to_jiffies(fan_watchdog_maxinterval
3162 * 1000))) {
3163 printk(IBM_ERR "failed to schedule the fan watchdog, "
3164 "watchdog will not trigger\n");
3166 } else
3167 fan_watchdog_active = 0;
3170 static int fan_set_level(int level)
3172 switch (fan_control_access_mode) {
3173 case TPACPI_FAN_WR_ACPI_SFAN:
3174 if (level >= 0 && level <= 7) {
3175 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3176 return -EIO;
3177 } else
3178 return -EINVAL;
3179 break;
3181 case TPACPI_FAN_WR_ACPI_FANS:
3182 case TPACPI_FAN_WR_TPEC:
3183 if ((level != TP_EC_FAN_AUTO) &&
3184 (level != TP_EC_FAN_FULLSPEED) &&
3185 ((level < 0) || (level > 7)))
3186 return -EINVAL;
3188 if (!acpi_ec_write(fan_status_offset, level))
3189 return -EIO;
3190 else
3191 tp_features.fan_ctrl_status_undef = 0;
3192 break;
3194 default:
3195 return -ENXIO;
3197 return 0;
3200 static int fan_set_level_safe(int level)
3202 int rc;
3204 rc = mutex_lock_interruptible(&fan_mutex);
3205 if (rc < 0)
3206 return rc;
3208 if (level == TPACPI_FAN_LAST_LEVEL)
3209 level = fan_control_desired_level;
3211 rc = fan_set_level(level);
3212 if (!rc)
3213 fan_update_desired_level(level);
3215 mutex_unlock(&fan_mutex);
3216 return rc;
3219 static int fan_set_enable(void)
3221 u8 s;
3222 int rc;
3224 rc = mutex_lock_interruptible(&fan_mutex);
3225 if (rc < 0)
3226 return rc;
3228 switch (fan_control_access_mode) {
3229 case TPACPI_FAN_WR_ACPI_FANS:
3230 case TPACPI_FAN_WR_TPEC:
3231 rc = fan_get_status(&s);
3232 if (rc < 0)
3233 break;
3235 /* Don't go out of emergency fan mode */
3236 if (s != 7)
3237 s = TP_EC_FAN_AUTO;
3239 if (!acpi_ec_write(fan_status_offset, s))
3240 rc = -EIO;
3241 else {
3242 tp_features.fan_ctrl_status_undef = 0;
3243 rc = 0;
3245 break;
3247 case TPACPI_FAN_WR_ACPI_SFAN:
3248 rc = fan_get_status(&s);
3249 if (rc < 0)
3250 break;
3252 s &= 0x07;
3254 /* Set fan to at least level 4 */
3255 if (s < 4)
3256 s = 4;
3258 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3259 rc= -EIO;
3260 else
3261 rc = 0;
3262 break;
3264 default:
3265 rc = -ENXIO;
3268 mutex_unlock(&fan_mutex);
3269 return rc;
3272 static int fan_set_disable(void)
3274 int rc;
3276 rc = mutex_lock_interruptible(&fan_mutex);
3277 if (rc < 0)
3278 return rc;
3280 rc = 0;
3281 switch (fan_control_access_mode) {
3282 case TPACPI_FAN_WR_ACPI_FANS:
3283 case TPACPI_FAN_WR_TPEC:
3284 if (!acpi_ec_write(fan_status_offset, 0x00))
3285 rc = -EIO;
3286 else {
3287 fan_control_desired_level = 0;
3288 tp_features.fan_ctrl_status_undef = 0;
3290 break;
3292 case TPACPI_FAN_WR_ACPI_SFAN:
3293 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3294 rc = -EIO;
3295 else
3296 fan_control_desired_level = 0;
3297 break;
3299 default:
3300 rc = -ENXIO;
3304 mutex_unlock(&fan_mutex);
3305 return rc;
3308 static int fan_set_speed(int speed)
3310 int rc;
3312 rc = mutex_lock_interruptible(&fan_mutex);
3313 if (rc < 0)
3314 return rc;
3316 rc = 0;
3317 switch (fan_control_access_mode) {
3318 case TPACPI_FAN_WR_ACPI_FANS:
3319 if (speed >= 0 && speed <= 65535) {
3320 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3321 speed, speed, speed))
3322 rc = -EIO;
3323 } else
3324 rc = -EINVAL;
3325 break;
3327 default:
3328 rc = -ENXIO;
3331 mutex_unlock(&fan_mutex);
3332 return rc;
3335 static int fan_read(char *p)
3337 int len = 0;
3338 int rc;
3339 u8 status;
3340 unsigned int speed = 0;
3342 switch (fan_status_access_mode) {
3343 case TPACPI_FAN_RD_ACPI_GFAN:
3344 /* 570, 600e/x, 770e, 770x */
3345 if ((rc = fan_get_status_safe(&status)) < 0)
3346 return rc;
3348 len += sprintf(p + len, "status:\t\t%s\n"
3349 "level:\t\t%d\n",
3350 (status != 0) ? "enabled" : "disabled", status);
3351 break;
3353 case TPACPI_FAN_RD_TPEC:
3354 /* all except 570, 600e/x, 770e, 770x */
3355 if ((rc = fan_get_status_safe(&status)) < 0)
3356 return rc;
3358 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3359 if (status != fan_control_initial_status)
3360 tp_features.fan_ctrl_status_undef = 0;
3361 else
3362 /* Return most likely status. In fact, it
3363 * might be the only possible status */
3364 status = TP_EC_FAN_AUTO;
3367 len += sprintf(p + len, "status:\t\t%s\n",
3368 (status != 0) ? "enabled" : "disabled");
3370 if ((rc = fan_get_speed(&speed)) < 0)
3371 return rc;
3373 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3375 if (status & TP_EC_FAN_FULLSPEED)
3376 /* Disengaged mode takes precedence */
3377 len += sprintf(p + len, "level:\t\tdisengaged\n");
3378 else if (status & TP_EC_FAN_AUTO)
3379 len += sprintf(p + len, "level:\t\tauto\n");
3380 else
3381 len += sprintf(p + len, "level:\t\t%d\n", status);
3382 break;
3384 case TPACPI_FAN_NONE:
3385 default:
3386 len += sprintf(p + len, "status:\t\tnot supported\n");
3389 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3390 len += sprintf(p + len, "commands:\tlevel <level>");
3392 switch (fan_control_access_mode) {
3393 case TPACPI_FAN_WR_ACPI_SFAN:
3394 len += sprintf(p + len, " (<level> is 0-7)\n");
3395 break;
3397 default:
3398 len += sprintf(p + len, " (<level> is 0-7, "
3399 "auto, disengaged, full-speed)\n");
3400 break;
3404 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3405 len += sprintf(p + len, "commands:\tenable, disable\n"
3406 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3407 "1-120 (seconds))\n");
3409 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3410 len += sprintf(p + len, "commands:\tspeed <speed>"
3411 " (<speed> is 0-65535)\n");
3413 return len;
3416 static int fan_write_cmd_level(const char *cmd, int *rc)
3418 int level;
3420 if (strlencmp(cmd, "level auto") == 0)
3421 level = TP_EC_FAN_AUTO;
3422 else if ((strlencmp(cmd, "level disengaged") == 0) |
3423 (strlencmp(cmd, "level full-speed") == 0))
3424 level = TP_EC_FAN_FULLSPEED;
3425 else if (sscanf(cmd, "level %d", &level) != 1)
3426 return 0;
3428 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3429 printk(IBM_ERR "level command accepted for unsupported "
3430 "access mode %d", fan_control_access_mode);
3432 return 1;
3435 static int fan_write_cmd_enable(const char *cmd, int *rc)
3437 if (strlencmp(cmd, "enable") != 0)
3438 return 0;
3440 if ((*rc = fan_set_enable()) == -ENXIO)
3441 printk(IBM_ERR "enable command accepted for unsupported "
3442 "access mode %d", fan_control_access_mode);
3444 return 1;
3447 static int fan_write_cmd_disable(const char *cmd, int *rc)
3449 if (strlencmp(cmd, "disable") != 0)
3450 return 0;
3452 if ((*rc = fan_set_disable()) == -ENXIO)
3453 printk(IBM_ERR "disable command accepted for unsupported "
3454 "access mode %d", fan_control_access_mode);
3456 return 1;
3459 static int fan_write_cmd_speed(const char *cmd, int *rc)
3461 int speed;
3463 /* TODO:
3464 * Support speed <low> <medium> <high> ? */
3466 if (sscanf(cmd, "speed %d", &speed) != 1)
3467 return 0;
3469 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3470 printk(IBM_ERR "speed command accepted for unsupported "
3471 "access mode %d", fan_control_access_mode);
3473 return 1;
3476 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3478 int interval;
3480 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3481 return 0;
3483 if (interval < 0 || interval > 120)
3484 *rc = -EINVAL;
3485 else
3486 fan_watchdog_maxinterval = interval;
3488 return 1;
3491 static int fan_write(char *buf)
3493 char *cmd;
3494 int rc = 0;
3496 while (!rc && (cmd = next_cmd(&buf))) {
3497 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3498 fan_write_cmd_level(cmd, &rc)) &&
3499 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3500 (fan_write_cmd_enable(cmd, &rc) ||
3501 fan_write_cmd_disable(cmd, &rc) ||
3502 fan_write_cmd_watchdog(cmd, &rc))) &&
3503 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3504 fan_write_cmd_speed(cmd, &rc))
3506 rc = -EINVAL;
3507 else if (!rc)
3508 fan_watchdog_reset();
3511 return rc;
3514 static struct ibm_struct fan_driver_data = {
3515 .name = "fan",
3516 .read = fan_read,
3517 .write = fan_write,
3518 .exit = fan_exit,
3519 .flags.experimental = 1,
3522 /****************************************************************************
3523 ****************************************************************************
3525 * Infrastructure
3527 ****************************************************************************
3528 ****************************************************************************/
3530 /* /proc support */
3531 static struct proc_dir_entry *proc_dir = NULL;
3533 /* Subdriver registry */
3534 static LIST_HEAD(tpacpi_all_drivers);
3538 * Module and infrastructure proble, init and exit handling
3541 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3542 static const char * __init str_supported(int is_supported)
3544 static char text_unsupported[] __initdata = "not supported";
3546 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3548 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3550 static int __init ibm_init(struct ibm_init_struct *iibm)
3552 int ret;
3553 struct ibm_struct *ibm = iibm->data;
3554 struct proc_dir_entry *entry;
3556 BUG_ON(ibm == NULL);
3558 INIT_LIST_HEAD(&ibm->all_drivers);
3560 if (ibm->flags.experimental && !experimental)
3561 return 0;
3563 dbg_printk(TPACPI_DBG_INIT,
3564 "probing for %s\n", ibm->name);
3566 if (iibm->init) {
3567 ret = iibm->init(iibm);
3568 if (ret > 0)
3569 return 0; /* probe failed */
3570 if (ret)
3571 return ret;
3573 ibm->flags.init_called = 1;
3576 if (ibm->acpi) {
3577 if (ibm->acpi->hid) {
3578 ret = register_tpacpi_subdriver(ibm);
3579 if (ret)
3580 goto err_out;
3583 if (ibm->acpi->notify) {
3584 ret = setup_acpi_notify(ibm);
3585 if (ret == -ENODEV) {
3586 printk(IBM_NOTICE "disabling subdriver %s\n",
3587 ibm->name);
3588 ret = 0;
3589 goto err_out;
3591 if (ret < 0)
3592 goto err_out;
3596 dbg_printk(TPACPI_DBG_INIT,
3597 "%s installed\n", ibm->name);
3599 if (ibm->read) {
3600 entry = create_proc_entry(ibm->name,
3601 S_IFREG | S_IRUGO | S_IWUSR,
3602 proc_dir);
3603 if (!entry) {
3604 printk(IBM_ERR "unable to create proc entry %s\n",
3605 ibm->name);
3606 ret = -ENODEV;
3607 goto err_out;
3609 entry->owner = THIS_MODULE;
3610 entry->data = ibm;
3611 entry->read_proc = &dispatch_procfs_read;
3612 if (ibm->write)
3613 entry->write_proc = &dispatch_procfs_write;
3614 ibm->flags.proc_created = 1;
3617 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3619 return 0;
3621 err_out:
3622 dbg_printk(TPACPI_DBG_INIT,
3623 "%s: at error exit path with result %d\n",
3624 ibm->name, ret);
3626 ibm_exit(ibm);
3627 return (ret < 0)? ret : 0;
3630 static void ibm_exit(struct ibm_struct *ibm)
3632 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
3634 list_del_init(&ibm->all_drivers);
3636 if (ibm->flags.acpi_notify_installed) {
3637 dbg_printk(TPACPI_DBG_EXIT,
3638 "%s: acpi_remove_notify_handler\n", ibm->name);
3639 BUG_ON(!ibm->acpi);
3640 acpi_remove_notify_handler(*ibm->acpi->handle,
3641 ibm->acpi->type,
3642 dispatch_acpi_notify);
3643 ibm->flags.acpi_notify_installed = 0;
3644 ibm->flags.acpi_notify_installed = 0;
3647 if (ibm->flags.proc_created) {
3648 dbg_printk(TPACPI_DBG_EXIT,
3649 "%s: remove_proc_entry\n", ibm->name);
3650 remove_proc_entry(ibm->name, proc_dir);
3651 ibm->flags.proc_created = 0;
3654 if (ibm->flags.acpi_driver_registered) {
3655 dbg_printk(TPACPI_DBG_EXIT,
3656 "%s: acpi_bus_unregister_driver\n", ibm->name);
3657 BUG_ON(!ibm->acpi);
3658 acpi_bus_unregister_driver(ibm->acpi->driver);
3659 kfree(ibm->acpi->driver);
3660 ibm->acpi->driver = NULL;
3661 ibm->flags.acpi_driver_registered = 0;
3664 if (ibm->flags.init_called && ibm->exit) {
3665 ibm->exit();
3666 ibm->flags.init_called = 0;
3669 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
3672 /* Probing */
3674 static char *ibm_thinkpad_ec_found = NULL;
3676 static char* __init check_dmi_for_ec(void)
3678 struct dmi_device *dev = NULL;
3679 char ec_fw_string[18];
3682 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3683 * X32 or newer, all Z series; Some models must have an
3684 * up-to-date BIOS or they will not be detected.
3686 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3688 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
3689 if (sscanf(dev->name,
3690 "IBM ThinkPad Embedded Controller -[%17c",
3691 ec_fw_string) == 1) {
3692 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3693 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3694 return kstrdup(ec_fw_string, GFP_KERNEL);
3697 return NULL;
3700 static int __init probe_for_thinkpad(void)
3702 int is_thinkpad;
3704 if (acpi_disabled)
3705 return -ENODEV;
3708 * Non-ancient models have better DMI tagging, but very old models
3709 * don't.
3711 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3713 /* ec is required because many other handles are relative to it */
3714 IBM_ACPIHANDLE_INIT(ec);
3715 if (!ec_handle) {
3716 if (is_thinkpad)
3717 printk(IBM_ERR
3718 "Not yet supported ThinkPad detected!\n");
3719 return -ENODEV;
3723 * Risks a regression on very old machines, but reduces potential
3724 * false positives a damn great deal
3726 if (!is_thinkpad)
3727 is_thinkpad = dmi_name_in_vendors("IBM");
3729 if (!is_thinkpad && !force_load)
3730 return -ENODEV;
3732 return 0;
3736 /* Module init, exit, parameters */
3738 static struct ibm_init_struct ibms_init[] __initdata = {
3740 .init = thinkpad_acpi_driver_init,
3741 .data = &thinkpad_acpi_driver_data,
3744 .init = hotkey_init,
3745 .data = &hotkey_driver_data,
3748 .init = bluetooth_init,
3749 .data = &bluetooth_driver_data,
3752 .init = wan_init,
3753 .data = &wan_driver_data,
3756 .init = video_init,
3757 .data = &video_driver_data,
3760 .init = light_init,
3761 .data = &light_driver_data,
3763 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3765 .init = dock_init,
3766 .data = &dock_driver_data[0],
3769 .data = &dock_driver_data[1],
3771 #endif
3772 #ifdef CONFIG_THINKPAD_ACPI_BAY
3774 .init = bay_init,
3775 .data = &bay_driver_data,
3777 #endif
3779 .init = cmos_init,
3780 .data = &cmos_driver_data,
3783 .init = led_init,
3784 .data = &led_driver_data,
3787 .init = beep_init,
3788 .data = &beep_driver_data,
3791 .init = thermal_init,
3792 .data = &thermal_driver_data,
3795 .data = &ecdump_driver_data,
3798 .init = brightness_init,
3799 .data = &brightness_driver_data,
3802 .data = &volume_driver_data,
3805 .init = fan_init,
3806 .data = &fan_driver_data,
3810 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3812 unsigned int i;
3813 struct ibm_struct *ibm;
3815 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3816 ibm = ibms_init[i].data;
3817 BUG_ON(ibm == NULL);
3819 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3820 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
3821 return -ENOSPC;
3822 strcpy(ibms_init[i].param, val);
3823 strcat(ibms_init[i].param, ",");
3824 return 0;
3828 return -EINVAL;
3831 static int experimental;
3832 module_param(experimental, int, 0);
3834 static u32 dbg_level;
3835 module_param_named(debug, dbg_level, uint, 0);
3837 static int force_load;
3838 module_param(force_load, int, 0);
3840 #define IBM_PARAM(feature) \
3841 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3843 IBM_PARAM(hotkey);
3844 IBM_PARAM(bluetooth);
3845 IBM_PARAM(video);
3846 IBM_PARAM(light);
3847 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3848 IBM_PARAM(dock);
3849 #endif
3850 #ifdef CONFIG_THINKPAD_ACPI_BAY
3851 IBM_PARAM(bay);
3852 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3853 IBM_PARAM(cmos);
3854 IBM_PARAM(led);
3855 IBM_PARAM(beep);
3856 IBM_PARAM(ecdump);
3857 IBM_PARAM(brightness);
3858 IBM_PARAM(volume);
3859 IBM_PARAM(fan);
3861 static int __init thinkpad_acpi_module_init(void)
3863 int ret, i;
3865 /* Driver-level probe */
3866 ret = probe_for_thinkpad();
3867 if (ret)
3868 return ret;
3870 /* Driver initialization */
3871 ibm_thinkpad_ec_found = check_dmi_for_ec();
3872 IBM_ACPIHANDLE_INIT(ecrd);
3873 IBM_ACPIHANDLE_INIT(ecwr);
3875 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
3876 if (!proc_dir) {
3877 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
3878 thinkpad_acpi_module_exit();
3879 return -ENODEV;
3881 proc_dir->owner = THIS_MODULE;
3883 ret = platform_driver_register(&tpacpi_pdriver);
3884 if (ret) {
3885 printk(IBM_ERR "unable to register platform driver\n");
3886 thinkpad_acpi_module_exit();
3887 return ret;
3889 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3890 if (ret) {
3891 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3892 thinkpad_acpi_module_exit();
3893 return ret;
3897 /* Device initialization */
3898 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3899 NULL, 0);
3900 if (IS_ERR(tpacpi_pdev)) {
3901 ret = PTR_ERR(tpacpi_pdev);
3902 tpacpi_pdev = NULL;
3903 printk(IBM_ERR "unable to register platform device\n");
3904 thinkpad_acpi_module_exit();
3905 return ret;
3907 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3908 if (IS_ERR(tpacpi_hwmon)) {
3909 ret = PTR_ERR(tpacpi_hwmon);
3910 tpacpi_hwmon = NULL;
3911 printk(IBM_ERR "unable to register hwmon device\n");
3912 thinkpad_acpi_module_exit();
3913 return ret;
3915 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3916 ret = ibm_init(&ibms_init[i]);
3917 if (ret >= 0 && *ibms_init[i].param)
3918 ret = ibms_init[i].data->write(ibms_init[i].param);
3919 if (ret < 0) {
3920 thinkpad_acpi_module_exit();
3921 return ret;
3925 return 0;
3928 static void thinkpad_acpi_module_exit(void)
3930 struct ibm_struct *ibm, *itmp;
3932 list_for_each_entry_safe_reverse(ibm, itmp,
3933 &tpacpi_all_drivers,
3934 all_drivers) {
3935 ibm_exit(ibm);
3938 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
3940 if (tpacpi_hwmon)
3941 hwmon_device_unregister(tpacpi_hwmon);
3943 if (tpacpi_pdev)
3944 platform_device_unregister(tpacpi_pdev);
3946 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
3947 platform_driver_unregister(&tpacpi_pdriver);
3949 if (proc_dir)
3950 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
3952 kfree(ibm_thinkpad_ec_found);
3955 module_init(thinkpad_acpi_module_init);
3956 module_exit(thinkpad_acpi_module_exit);