ACPI: thinkpad-acpi: add infrastructure for the sysfs device attributes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobcdb8b94f5531b7c6dd053491107e1b8c84d49700
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);
708 /* hotkey not supported on 570 */
709 tp_features.hotkey = hkey_handle != NULL;
711 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
712 str_supported(tp_features.hotkey));
714 if (tp_features.hotkey) {
715 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
716 A30, R30, R31, T20-22, X20-21, X22-24 */
717 tp_features.hotkey_mask =
718 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
720 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
721 str_supported(tp_features.hotkey_mask));
723 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
724 if (res)
725 return res;
728 return (tp_features.hotkey)? 0 : 1;
731 static void hotkey_exit(void)
733 int res;
735 if (tp_features.hotkey) {
736 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
737 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
738 if (res)
739 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
743 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
745 int hkey;
747 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
748 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
749 else {
750 printk(IBM_ERR "unknown hotkey event %d\n", event);
751 acpi_bus_generate_event(ibm->acpi->device, event, 0);
755 static int hotkey_get(int *status, int *mask)
757 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
758 return -EIO;
760 if (tp_features.hotkey_mask)
761 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
762 return -EIO;
764 return 0;
767 static int hotkey_set(int status, int mask)
769 int i;
771 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
772 return -EIO;
774 if (tp_features.hotkey_mask)
775 for (i = 0; i < 32; i++) {
776 int bit = ((1 << i) & mask) != 0;
777 if (!acpi_evalf(hkey_handle,
778 NULL, "MHKM", "vdd", i + 1, bit))
779 return -EIO;
782 return 0;
785 static int hotkey_read(char *p)
787 int res, status, mask;
788 int len = 0;
790 if (!tp_features.hotkey) {
791 len += sprintf(p + len, "status:\t\tnot supported\n");
792 return len;
795 res = hotkey_get(&status, &mask);
796 if (res)
797 return res;
799 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
800 if (tp_features.hotkey_mask) {
801 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
802 len += sprintf(p + len,
803 "commands:\tenable, disable, reset, <mask>\n");
804 } else {
805 len += sprintf(p + len, "mask:\t\tnot supported\n");
806 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
809 return len;
812 static int hotkey_write(char *buf)
814 int res, status, mask;
815 char *cmd;
816 int do_cmd = 0;
818 if (!tp_features.hotkey)
819 return -ENODEV;
821 res = hotkey_get(&status, &mask);
822 if (res)
823 return res;
825 while ((cmd = next_cmd(&buf))) {
826 if (strlencmp(cmd, "enable") == 0) {
827 status = 1;
828 } else if (strlencmp(cmd, "disable") == 0) {
829 status = 0;
830 } else if (strlencmp(cmd, "reset") == 0) {
831 status = hotkey_orig_status;
832 mask = hotkey_orig_mask;
833 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
834 /* mask set */
835 } else if (sscanf(cmd, "%x", &mask) == 1) {
836 /* mask set */
837 } else
838 return -EINVAL;
839 do_cmd = 1;
842 if (do_cmd) {
843 res = hotkey_set(status, mask);
844 if (res)
845 return res;
848 return 0;
851 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
852 .hid = IBM_HKEY_HID,
853 .notify = hotkey_notify,
854 .handle = &hkey_handle,
855 .type = ACPI_DEVICE_NOTIFY,
858 static struct ibm_struct hotkey_driver_data = {
859 .name = "hotkey",
860 .read = hotkey_read,
861 .write = hotkey_write,
862 .exit = hotkey_exit,
863 .acpi = &ibm_hotkey_acpidriver,
866 /*************************************************************************
867 * Bluetooth subdriver
870 static int __init bluetooth_init(struct ibm_init_struct *iibm)
872 int status = 0;
874 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
876 IBM_ACPIHANDLE_INIT(hkey);
878 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
879 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
880 tp_features.bluetooth = hkey_handle &&
881 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
883 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
884 str_supported(tp_features.bluetooth),
885 status);
887 if (tp_features.bluetooth &&
888 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
889 /* no bluetooth hardware present in system */
890 tp_features.bluetooth = 0;
891 dbg_printk(TPACPI_DBG_INIT,
892 "bluetooth hardware not installed\n");
895 return (tp_features.bluetooth)? 0 : 1;
898 static int bluetooth_get_radiosw(void)
900 int status;
902 if (!tp_features.bluetooth)
903 return -ENODEV;
905 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
906 return -EIO;
908 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
911 static int bluetooth_set_radiosw(int radio_on)
913 int status;
915 if (!tp_features.bluetooth)
916 return -ENODEV;
918 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
919 return -EIO;
920 if (radio_on)
921 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
922 else
923 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
925 return -EIO;
927 return 0;
930 static int bluetooth_read(char *p)
932 int len = 0;
933 int status = bluetooth_get_radiosw();
935 if (!tp_features.bluetooth)
936 len += sprintf(p + len, "status:\t\tnot supported\n");
937 else {
938 len += sprintf(p + len, "status:\t\t%s\n",
939 (status)? "enabled" : "disabled");
940 len += sprintf(p + len, "commands:\tenable, disable\n");
943 return len;
946 static int bluetooth_write(char *buf)
948 char *cmd;
950 if (!tp_features.bluetooth)
951 return -ENODEV;
953 while ((cmd = next_cmd(&buf))) {
954 if (strlencmp(cmd, "enable") == 0) {
955 bluetooth_set_radiosw(1);
956 } else if (strlencmp(cmd, "disable") == 0) {
957 bluetooth_set_radiosw(0);
958 } else
959 return -EINVAL;
962 return 0;
965 static struct ibm_struct bluetooth_driver_data = {
966 .name = "bluetooth",
967 .read = bluetooth_read,
968 .write = bluetooth_write,
971 /*************************************************************************
972 * Wan subdriver
975 static int __init wan_init(struct ibm_init_struct *iibm)
977 int status = 0;
979 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
981 IBM_ACPIHANDLE_INIT(hkey);
983 tp_features.wan = hkey_handle &&
984 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
986 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
987 str_supported(tp_features.wan),
988 status);
990 if (tp_features.wan &&
991 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
992 /* no wan hardware present in system */
993 tp_features.wan = 0;
994 dbg_printk(TPACPI_DBG_INIT,
995 "wan hardware not installed\n");
998 return (tp_features.wan)? 0 : 1;
1001 static int wan_get_radiosw(void)
1003 int status;
1005 if (!tp_features.wan)
1006 return -ENODEV;
1008 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1009 return -EIO;
1011 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1014 static int wan_set_radiosw(int radio_on)
1016 int status;
1018 if (!tp_features.wan)
1019 return -ENODEV;
1021 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1022 return -EIO;
1023 if (radio_on)
1024 status |= TP_ACPI_WANCARD_RADIOSSW;
1025 else
1026 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1027 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1028 return -EIO;
1030 return 0;
1033 static int wan_read(char *p)
1035 int len = 0;
1036 int status = wan_get_radiosw();
1038 if (!tp_features.wan)
1039 len += sprintf(p + len, "status:\t\tnot supported\n");
1040 else {
1041 len += sprintf(p + len, "status:\t\t%s\n",
1042 (status)? "enabled" : "disabled");
1043 len += sprintf(p + len, "commands:\tenable, disable\n");
1046 return len;
1049 static int wan_write(char *buf)
1051 char *cmd;
1053 if (!tp_features.wan)
1054 return -ENODEV;
1056 while ((cmd = next_cmd(&buf))) {
1057 if (strlencmp(cmd, "enable") == 0) {
1058 wan_set_radiosw(1);
1059 } else if (strlencmp(cmd, "disable") == 0) {
1060 wan_set_radiosw(0);
1061 } else
1062 return -EINVAL;
1065 return 0;
1068 static struct ibm_struct wan_driver_data = {
1069 .name = "wan",
1070 .read = wan_read,
1071 .write = wan_write,
1072 .flags.experimental = 1,
1075 /*************************************************************************
1076 * Video subdriver
1079 static enum video_access_mode video_supported;
1080 static int video_orig_autosw;
1082 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1083 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1084 "\\_SB.PCI0.VID0", /* 770e */
1085 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1086 "\\_SB.PCI0.AGP.VID", /* all others */
1087 ); /* R30, R31 */
1089 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1091 static int __init video_init(struct ibm_init_struct *iibm)
1093 int ivga;
1095 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1097 IBM_ACPIHANDLE_INIT(vid);
1098 IBM_ACPIHANDLE_INIT(vid2);
1100 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1101 /* G41, assume IVGA doesn't change */
1102 vid_handle = vid2_handle;
1104 if (!vid_handle)
1105 /* video switching not supported on R30, R31 */
1106 video_supported = TPACPI_VIDEO_NONE;
1107 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1108 /* 570 */
1109 video_supported = TPACPI_VIDEO_570;
1110 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1111 /* 600e/x, 770e, 770x */
1112 video_supported = TPACPI_VIDEO_770;
1113 else
1114 /* all others */
1115 video_supported = TPACPI_VIDEO_NEW;
1117 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1118 str_supported(video_supported != TPACPI_VIDEO_NONE),
1119 video_supported);
1121 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1124 static void video_exit(void)
1126 dbg_printk(TPACPI_DBG_EXIT,
1127 "restoring original video autoswitch mode\n");
1128 if (video_autosw_set(video_orig_autosw))
1129 printk(IBM_ERR "error while trying to restore original "
1130 "video autoswitch mode\n");
1133 static int video_outputsw_get(void)
1135 int status = 0;
1136 int i;
1138 switch (video_supported) {
1139 case TPACPI_VIDEO_570:
1140 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1141 TP_ACPI_VIDEO_570_PHSCMD))
1142 return -EIO;
1143 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1144 break;
1145 case TPACPI_VIDEO_770:
1146 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1147 return -EIO;
1148 if (i)
1149 status |= TP_ACPI_VIDEO_S_LCD;
1150 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1151 return -EIO;
1152 if (i)
1153 status |= TP_ACPI_VIDEO_S_CRT;
1154 break;
1155 case TPACPI_VIDEO_NEW:
1156 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1157 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1158 return -EIO;
1159 if (i)
1160 status |= TP_ACPI_VIDEO_S_CRT;
1162 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1163 !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, "\\VCDD", "d"))
1168 return -EIO;
1169 if (i)
1170 status |= TP_ACPI_VIDEO_S_DVI;
1171 break;
1172 default:
1173 return -ENOSYS;
1176 return status;
1179 static int video_outputsw_set(int status)
1181 int autosw;
1182 int res = 0;
1184 switch (video_supported) {
1185 case TPACPI_VIDEO_570:
1186 res = acpi_evalf(NULL, NULL,
1187 "\\_SB.PHS2", "vdd",
1188 TP_ACPI_VIDEO_570_PHS2CMD,
1189 status | TP_ACPI_VIDEO_570_PHS2SET);
1190 break;
1191 case TPACPI_VIDEO_770:
1192 autosw = video_autosw_get();
1193 if (autosw < 0)
1194 return autosw;
1196 res = video_autosw_set(1);
1197 if (res)
1198 return res;
1199 res = acpi_evalf(vid_handle, NULL,
1200 "ASWT", "vdd", status * 0x100, 0);
1201 if (!autosw && video_autosw_set(autosw)) {
1202 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1203 return -EIO;
1205 break;
1206 case TPACPI_VIDEO_NEW:
1207 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1208 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1209 break;
1210 default:
1211 return -ENOSYS;
1214 return (res)? 0 : -EIO;
1217 static int video_autosw_get(void)
1219 int autosw = 0;
1221 switch (video_supported) {
1222 case TPACPI_VIDEO_570:
1223 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1224 return -EIO;
1225 break;
1226 case TPACPI_VIDEO_770:
1227 case TPACPI_VIDEO_NEW:
1228 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1229 return -EIO;
1230 break;
1231 default:
1232 return -ENOSYS;
1235 return autosw & 1;
1238 static int video_autosw_set(int enable)
1240 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1241 return -EIO;
1242 return 0;
1245 static int video_outputsw_cycle(void)
1247 int autosw = video_autosw_get();
1248 int res;
1250 if (autosw < 0)
1251 return autosw;
1253 switch (video_supported) {
1254 case TPACPI_VIDEO_570:
1255 res = video_autosw_set(1);
1256 if (res)
1257 return res;
1258 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1259 break;
1260 case TPACPI_VIDEO_770:
1261 case TPACPI_VIDEO_NEW:
1262 res = video_autosw_set(1);
1263 if (res)
1264 return res;
1265 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1266 break;
1267 default:
1268 return -ENOSYS;
1270 if (!autosw && video_autosw_set(autosw)) {
1271 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1272 return -EIO;
1275 return (res)? 0 : -EIO;
1278 static int video_expand_toggle(void)
1280 switch (video_supported) {
1281 case TPACPI_VIDEO_570:
1282 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1283 0 : -EIO;
1284 case TPACPI_VIDEO_770:
1285 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1286 0 : -EIO;
1287 case TPACPI_VIDEO_NEW:
1288 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1289 0 : -EIO;
1290 default:
1291 return -ENOSYS;
1293 /* not reached */
1296 static int video_read(char *p)
1298 int status, autosw;
1299 int len = 0;
1301 if (video_supported == TPACPI_VIDEO_NONE) {
1302 len += sprintf(p + len, "status:\t\tnot supported\n");
1303 return len;
1306 status = video_outputsw_get();
1307 if (status < 0)
1308 return status;
1310 autosw = video_autosw_get();
1311 if (autosw < 0)
1312 return autosw;
1314 len += sprintf(p + len, "status:\t\tsupported\n");
1315 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1316 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1317 if (video_supported == TPACPI_VIDEO_NEW)
1318 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1319 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1320 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1321 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1322 if (video_supported == TPACPI_VIDEO_NEW)
1323 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1324 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1325 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1327 return len;
1330 static int video_write(char *buf)
1332 char *cmd;
1333 int enable, disable, status;
1334 int res;
1336 if (video_supported == TPACPI_VIDEO_NONE)
1337 return -ENODEV;
1339 enable = 0;
1340 disable = 0;
1342 while ((cmd = next_cmd(&buf))) {
1343 if (strlencmp(cmd, "lcd_enable") == 0) {
1344 enable |= TP_ACPI_VIDEO_S_LCD;
1345 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1346 disable |= TP_ACPI_VIDEO_S_LCD;
1347 } else if (strlencmp(cmd, "crt_enable") == 0) {
1348 enable |= TP_ACPI_VIDEO_S_CRT;
1349 } else if (strlencmp(cmd, "crt_disable") == 0) {
1350 disable |= TP_ACPI_VIDEO_S_CRT;
1351 } else if (video_supported == TPACPI_VIDEO_NEW &&
1352 strlencmp(cmd, "dvi_enable") == 0) {
1353 enable |= TP_ACPI_VIDEO_S_DVI;
1354 } else if (video_supported == TPACPI_VIDEO_NEW &&
1355 strlencmp(cmd, "dvi_disable") == 0) {
1356 disable |= TP_ACPI_VIDEO_S_DVI;
1357 } else if (strlencmp(cmd, "auto_enable") == 0) {
1358 res = video_autosw_set(1);
1359 if (res)
1360 return res;
1361 } else if (strlencmp(cmd, "auto_disable") == 0) {
1362 res = video_autosw_set(0);
1363 if (res)
1364 return res;
1365 } else if (strlencmp(cmd, "video_switch") == 0) {
1366 res = video_outputsw_cycle();
1367 if (res)
1368 return res;
1369 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1370 res = video_expand_toggle();
1371 if (res)
1372 return res;
1373 } else
1374 return -EINVAL;
1377 if (enable || disable) {
1378 status = video_outputsw_get();
1379 if (status < 0)
1380 return status;
1381 res = video_outputsw_set((status & ~disable) | enable);
1382 if (res)
1383 return res;
1386 return 0;
1389 static struct ibm_struct video_driver_data = {
1390 .name = "video",
1391 .read = video_read,
1392 .write = video_write,
1393 .exit = video_exit,
1396 /*************************************************************************
1397 * Light (thinklight) subdriver
1400 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1401 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1403 static int __init light_init(struct ibm_init_struct *iibm)
1405 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1407 IBM_ACPIHANDLE_INIT(ledb);
1408 IBM_ACPIHANDLE_INIT(lght);
1409 IBM_ACPIHANDLE_INIT(cmos);
1411 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1412 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1414 if (tp_features.light)
1415 /* light status not supported on
1416 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1417 tp_features.light_status =
1418 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1420 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1421 str_supported(tp_features.light));
1423 return (tp_features.light)? 0 : 1;
1426 static int light_read(char *p)
1428 int len = 0;
1429 int status = 0;
1431 if (!tp_features.light) {
1432 len += sprintf(p + len, "status:\t\tnot supported\n");
1433 } else if (!tp_features.light_status) {
1434 len += sprintf(p + len, "status:\t\tunknown\n");
1435 len += sprintf(p + len, "commands:\ton, off\n");
1436 } else {
1437 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1438 return -EIO;
1439 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1440 len += sprintf(p + len, "commands:\ton, off\n");
1443 return len;
1446 static int light_write(char *buf)
1448 int cmos_cmd, lght_cmd;
1449 char *cmd;
1450 int success;
1452 if (!tp_features.light)
1453 return -ENODEV;
1455 while ((cmd = next_cmd(&buf))) {
1456 if (strlencmp(cmd, "on") == 0) {
1457 cmos_cmd = 0x0c;
1458 lght_cmd = 1;
1459 } else if (strlencmp(cmd, "off") == 0) {
1460 cmos_cmd = 0x0d;
1461 lght_cmd = 0;
1462 } else
1463 return -EINVAL;
1465 success = cmos_handle ?
1466 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1467 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1468 if (!success)
1469 return -EIO;
1472 return 0;
1475 static struct ibm_struct light_driver_data = {
1476 .name = "light",
1477 .read = light_read,
1478 .write = light_write,
1481 /*************************************************************************
1482 * Dock subdriver
1485 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1487 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1488 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1489 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1490 "\\_SB.PCI.ISA.SLCE", /* 570 */
1491 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1493 /* don't list other alternatives as we install a notify handler on the 570 */
1494 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1496 #define dock_docked() (_sta(dock_handle) & 1)
1498 static int __init dock_init(struct ibm_init_struct *iibm)
1500 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1502 IBM_ACPIHANDLE_INIT(dock);
1503 IBM_ACPIHANDLE_INIT(pci);
1505 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1506 str_supported(dock_handle != NULL));
1508 return (dock_handle)? 0 : 1;
1511 static void dock_notify(struct ibm_struct *ibm, u32 event)
1513 int docked = dock_docked();
1514 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1516 if (event == 1 && !pci) /* 570 */
1517 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1518 else if (event == 1 && pci) /* 570 */
1519 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1520 else if (event == 3 && docked)
1521 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1522 else if (event == 3 && !docked)
1523 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1524 else if (event == 0 && docked)
1525 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1526 else {
1527 printk(IBM_ERR "unknown dock event %d, status %d\n",
1528 event, _sta(dock_handle));
1529 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1533 static int dock_read(char *p)
1535 int len = 0;
1536 int docked = dock_docked();
1538 if (!dock_handle)
1539 len += sprintf(p + len, "status:\t\tnot supported\n");
1540 else if (!docked)
1541 len += sprintf(p + len, "status:\t\tundocked\n");
1542 else {
1543 len += sprintf(p + len, "status:\t\tdocked\n");
1544 len += sprintf(p + len, "commands:\tdock, undock\n");
1547 return len;
1550 static int dock_write(char *buf)
1552 char *cmd;
1554 if (!dock_docked())
1555 return -ENODEV;
1557 while ((cmd = next_cmd(&buf))) {
1558 if (strlencmp(cmd, "undock") == 0) {
1559 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1560 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1561 return -EIO;
1562 } else if (strlencmp(cmd, "dock") == 0) {
1563 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1564 return -EIO;
1565 } else
1566 return -EINVAL;
1569 return 0;
1572 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1574 .notify = dock_notify,
1575 .handle = &dock_handle,
1576 .type = ACPI_SYSTEM_NOTIFY,
1579 .hid = IBM_PCI_HID,
1580 .notify = dock_notify,
1581 .handle = &pci_handle,
1582 .type = ACPI_SYSTEM_NOTIFY,
1586 static struct ibm_struct dock_driver_data[2] = {
1588 .name = "dock",
1589 .read = dock_read,
1590 .write = dock_write,
1591 .acpi = &ibm_dock_acpidriver[0],
1594 .name = "dock",
1595 .acpi = &ibm_dock_acpidriver[1],
1599 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1601 /*************************************************************************
1602 * Bay subdriver
1605 #ifdef CONFIG_THINKPAD_ACPI_BAY
1606 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1607 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1608 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1609 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1610 ); /* A21e, R30, R31 */
1611 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1612 "_EJ0", /* all others */
1613 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1614 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1615 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1616 ); /* all others */
1617 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1618 "_EJ0", /* 770x */
1619 ); /* all others */
1621 static int __init bay_init(struct ibm_init_struct *iibm)
1623 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1625 IBM_ACPIHANDLE_INIT(bay);
1626 if (bay_handle)
1627 IBM_ACPIHANDLE_INIT(bay_ej);
1628 IBM_ACPIHANDLE_INIT(bay2);
1629 if (bay2_handle)
1630 IBM_ACPIHANDLE_INIT(bay2_ej);
1632 tp_features.bay_status = bay_handle &&
1633 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1634 tp_features.bay_status2 = bay2_handle &&
1635 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1637 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1638 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1639 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1640 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1642 vdbg_printk(TPACPI_DBG_INIT,
1643 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1644 str_supported(tp_features.bay_status),
1645 str_supported(tp_features.bay_eject),
1646 str_supported(tp_features.bay_status2),
1647 str_supported(tp_features.bay_eject2));
1649 return (tp_features.bay_status || tp_features.bay_eject ||
1650 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1653 static void bay_notify(struct ibm_struct *ibm, u32 event)
1655 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1658 #define bay_occupied(b) (_sta(b##_handle) & 1)
1660 static int bay_read(char *p)
1662 int len = 0;
1663 int occupied = bay_occupied(bay);
1664 int occupied2 = bay_occupied(bay2);
1665 int eject, eject2;
1667 len += sprintf(p + len, "status:\t\t%s\n",
1668 tp_features.bay_status ?
1669 (occupied ? "occupied" : "unoccupied") :
1670 "not supported");
1671 if (tp_features.bay_status2)
1672 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1673 "occupied" : "unoccupied");
1675 eject = tp_features.bay_eject && occupied;
1676 eject2 = tp_features.bay_eject2 && occupied2;
1678 if (eject && eject2)
1679 len += sprintf(p + len, "commands:\teject, eject2\n");
1680 else if (eject)
1681 len += sprintf(p + len, "commands:\teject\n");
1682 else if (eject2)
1683 len += sprintf(p + len, "commands:\teject2\n");
1685 return len;
1688 static int bay_write(char *buf)
1690 char *cmd;
1692 if (!tp_features.bay_eject && !tp_features.bay_eject2)
1693 return -ENODEV;
1695 while ((cmd = next_cmd(&buf))) {
1696 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
1697 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1698 return -EIO;
1699 } else if (tp_features.bay_eject2 &&
1700 strlencmp(cmd, "eject2") == 0) {
1701 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1702 return -EIO;
1703 } else
1704 return -EINVAL;
1707 return 0;
1710 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1711 .notify = bay_notify,
1712 .handle = &bay_handle,
1713 .type = ACPI_SYSTEM_NOTIFY,
1716 static struct ibm_struct bay_driver_data = {
1717 .name = "bay",
1718 .read = bay_read,
1719 .write = bay_write,
1720 .acpi = &ibm_bay_acpidriver,
1723 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1725 /*************************************************************************
1726 * CMOS subdriver
1729 static int __init cmos_init(struct ibm_init_struct *iibm)
1731 vdbg_printk(TPACPI_DBG_INIT,
1732 "initializing cmos commands subdriver\n");
1734 IBM_ACPIHANDLE_INIT(cmos);
1736 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1737 str_supported(cmos_handle != NULL));
1738 return (cmos_handle)? 0 : 1;
1741 static int cmos_read(char *p)
1743 int len = 0;
1745 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1746 R30, R31, T20-22, X20-21 */
1747 if (!cmos_handle)
1748 len += sprintf(p + len, "status:\t\tnot supported\n");
1749 else {
1750 len += sprintf(p + len, "status:\t\tsupported\n");
1751 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1754 return len;
1757 static int cmos_write(char *buf)
1759 char *cmd;
1760 int cmos_cmd, res;
1762 while ((cmd = next_cmd(&buf))) {
1763 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1764 cmos_cmd >= 0 && cmos_cmd <= 21) {
1765 /* cmos_cmd set */
1766 } else
1767 return -EINVAL;
1769 res = issue_thinkpad_cmos_command(cmos_cmd);
1770 if (res)
1771 return res;
1774 return 0;
1777 static struct ibm_struct cmos_driver_data = {
1778 .name = "cmos",
1779 .read = cmos_read,
1780 .write = cmos_write,
1783 /*************************************************************************
1784 * LED subdriver
1787 static enum led_access_mode led_supported;
1789 IBM_HANDLE(led, ec, "SLED", /* 570 */
1790 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1791 "LED", /* all others */
1792 ); /* R30, R31 */
1794 static int __init led_init(struct ibm_init_struct *iibm)
1796 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1798 IBM_ACPIHANDLE_INIT(led);
1800 if (!led_handle)
1801 /* led not supported on R30, R31 */
1802 led_supported = TPACPI_LED_NONE;
1803 else if (strlencmp(led_path, "SLED") == 0)
1804 /* 570 */
1805 led_supported = TPACPI_LED_570;
1806 else if (strlencmp(led_path, "SYSL") == 0)
1807 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1808 led_supported = TPACPI_LED_OLD;
1809 else
1810 /* all others */
1811 led_supported = TPACPI_LED_NEW;
1813 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1814 str_supported(led_supported), led_supported);
1816 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
1819 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1821 static int led_read(char *p)
1823 int len = 0;
1825 if (!led_supported) {
1826 len += sprintf(p + len, "status:\t\tnot supported\n");
1827 return len;
1829 len += sprintf(p + len, "status:\t\tsupported\n");
1831 if (led_supported == TPACPI_LED_570) {
1832 /* 570 */
1833 int i, status;
1834 for (i = 0; i < 8; i++) {
1835 if (!acpi_evalf(ec_handle,
1836 &status, "GLED", "dd", 1 << i))
1837 return -EIO;
1838 len += sprintf(p + len, "%d:\t\t%s\n",
1839 i, led_status(status));
1843 len += sprintf(p + len, "commands:\t"
1844 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1846 return len;
1849 /* off, on, blink */
1850 static const int led_sled_arg1[] = { 0, 1, 3 };
1851 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1852 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1853 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1855 static int led_write(char *buf)
1857 char *cmd;
1858 int led, ind, ret;
1860 if (!led_supported)
1861 return -ENODEV;
1863 while ((cmd = next_cmd(&buf))) {
1864 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1865 return -EINVAL;
1867 if (strstr(cmd, "off")) {
1868 ind = 0;
1869 } else if (strstr(cmd, "on")) {
1870 ind = 1;
1871 } else if (strstr(cmd, "blink")) {
1872 ind = 2;
1873 } else
1874 return -EINVAL;
1876 if (led_supported == TPACPI_LED_570) {
1877 /* 570 */
1878 led = 1 << led;
1879 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1880 led, led_sled_arg1[ind]))
1881 return -EIO;
1882 } else if (led_supported == TPACPI_LED_OLD) {
1883 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1884 led = 1 << led;
1885 ret = ec_write(TPACPI_LED_EC_HLMS, led);
1886 if (ret >= 0)
1887 ret =
1888 ec_write(TPACPI_LED_EC_HLBL,
1889 led * led_exp_hlbl[ind]);
1890 if (ret >= 0)
1891 ret =
1892 ec_write(TPACPI_LED_EC_HLCL,
1893 led * led_exp_hlcl[ind]);
1894 if (ret < 0)
1895 return ret;
1896 } else {
1897 /* all others */
1898 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1899 led, led_led_arg1[ind]))
1900 return -EIO;
1904 return 0;
1907 static struct ibm_struct led_driver_data = {
1908 .name = "led",
1909 .read = led_read,
1910 .write = led_write,
1913 /*************************************************************************
1914 * Beep subdriver
1917 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1919 static int __init beep_init(struct ibm_init_struct *iibm)
1921 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1923 IBM_ACPIHANDLE_INIT(beep);
1925 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1926 str_supported(beep_handle != NULL));
1928 return (beep_handle)? 0 : 1;
1931 static int beep_read(char *p)
1933 int len = 0;
1935 if (!beep_handle)
1936 len += sprintf(p + len, "status:\t\tnot supported\n");
1937 else {
1938 len += sprintf(p + len, "status:\t\tsupported\n");
1939 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1942 return len;
1945 static int beep_write(char *buf)
1947 char *cmd;
1948 int beep_cmd;
1950 if (!beep_handle)
1951 return -ENODEV;
1953 while ((cmd = next_cmd(&buf))) {
1954 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1955 beep_cmd >= 0 && beep_cmd <= 17) {
1956 /* beep_cmd set */
1957 } else
1958 return -EINVAL;
1959 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1960 return -EIO;
1963 return 0;
1966 static struct ibm_struct beep_driver_data = {
1967 .name = "beep",
1968 .read = beep_read,
1969 .write = beep_write,
1972 /*************************************************************************
1973 * Thermal subdriver
1976 static enum thermal_access_mode thermal_read_mode;
1978 static int __init thermal_init(struct ibm_init_struct *iibm)
1980 u8 t, ta1, ta2;
1981 int i;
1982 int acpi_tmp7;
1984 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1986 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1988 if (ibm_thinkpad_ec_found && experimental) {
1990 * Direct EC access mode: sensors at registers
1991 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1992 * non-implemented, thermal sensors return 0x80 when
1993 * not available
1996 ta1 = ta2 = 0;
1997 for (i = 0; i < 8; i++) {
1998 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
1999 ta1 |= t;
2000 } else {
2001 ta1 = 0;
2002 break;
2004 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2005 ta2 |= t;
2006 } else {
2007 ta1 = 0;
2008 break;
2011 if (ta1 == 0) {
2012 /* This is sheer paranoia, but we handle it anyway */
2013 if (acpi_tmp7) {
2014 printk(IBM_ERR
2015 "ThinkPad ACPI EC access misbehaving, "
2016 "falling back to ACPI TMPx access mode\n");
2017 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2018 } else {
2019 printk(IBM_ERR
2020 "ThinkPad ACPI EC access misbehaving, "
2021 "disabling thermal sensors access\n");
2022 thermal_read_mode = TPACPI_THERMAL_NONE;
2024 } else {
2025 thermal_read_mode =
2026 (ta2 != 0) ?
2027 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2029 } else if (acpi_tmp7) {
2030 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2031 /* 600e/x, 770e, 770x */
2032 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2033 } else {
2034 /* Standard ACPI TMPx access, max 8 sensors */
2035 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2037 } else {
2038 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2039 thermal_read_mode = TPACPI_THERMAL_NONE;
2042 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2043 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2044 thermal_read_mode);
2046 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
2049 /* idx is zero-based */
2050 static int thermal_get_sensor(int idx, s32 *value)
2052 int t;
2053 s8 tmp;
2054 char tmpi[5];
2056 t = TP_EC_THERMAL_TMP0;
2058 switch (thermal_read_mode) {
2059 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2060 case TPACPI_THERMAL_TPEC_16:
2061 if (idx >= 8 && idx <= 15) {
2062 t = TP_EC_THERMAL_TMP8;
2063 idx -= 8;
2065 /* fallthrough */
2066 #endif
2067 case TPACPI_THERMAL_TPEC_8:
2068 if (idx <= 7) {
2069 if (!acpi_ec_read(t + idx, &tmp))
2070 return -EIO;
2071 *value = tmp * 1000;
2072 return 0;
2074 break;
2076 case TPACPI_THERMAL_ACPI_UPDT:
2077 if (idx <= 7) {
2078 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2079 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2080 return -EIO;
2081 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2082 return -EIO;
2083 *value = (t - 2732) * 100;
2084 return 0;
2086 break;
2088 case TPACPI_THERMAL_ACPI_TMP07:
2089 if (idx <= 7) {
2090 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2091 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2092 return -EIO;
2093 *value = t * 1000;
2094 return 0;
2096 break;
2098 case TPACPI_THERMAL_NONE:
2099 default:
2100 return -ENOSYS;
2103 return -EINVAL;
2106 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2108 int res, i;
2109 int n;
2111 n = 8;
2112 i = 0;
2114 if (!s)
2115 return -EINVAL;
2117 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2118 n = 16;
2120 for(i = 0 ; i < n; i++) {
2121 res = thermal_get_sensor(i, &s->temp[i]);
2122 if (res)
2123 return res;
2126 return n;
2129 static int thermal_read(char *p)
2131 int len = 0;
2132 int n, i;
2133 struct ibm_thermal_sensors_struct t;
2135 n = thermal_get_sensors(&t);
2136 if (unlikely(n < 0))
2137 return n;
2139 len += sprintf(p + len, "temperatures:\t");
2141 if (n > 0) {
2142 for (i = 0; i < (n - 1); i++)
2143 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2144 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2145 } else
2146 len += sprintf(p + len, "not supported\n");
2148 return len;
2151 static struct ibm_struct thermal_driver_data = {
2152 .name = "thermal",
2153 .read = thermal_read,
2156 /*************************************************************************
2157 * EC Dump subdriver
2160 static u8 ecdump_regs[256];
2162 static int ecdump_read(char *p)
2164 int len = 0;
2165 int i, j;
2166 u8 v;
2168 len += sprintf(p + len, "EC "
2169 " +00 +01 +02 +03 +04 +05 +06 +07"
2170 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2171 for (i = 0; i < 256; i += 16) {
2172 len += sprintf(p + len, "EC 0x%02x:", i);
2173 for (j = 0; j < 16; j++) {
2174 if (!acpi_ec_read(i + j, &v))
2175 break;
2176 if (v != ecdump_regs[i + j])
2177 len += sprintf(p + len, " *%02x", v);
2178 else
2179 len += sprintf(p + len, " %02x", v);
2180 ecdump_regs[i + j] = v;
2182 len += sprintf(p + len, "\n");
2183 if (j != 16)
2184 break;
2187 /* These are way too dangerous to advertise openly... */
2188 #if 0
2189 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2190 " (<offset> is 00-ff, <value> is 00-ff)\n");
2191 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2192 " (<offset> is 00-ff, <value> is 0-255)\n");
2193 #endif
2194 return len;
2197 static int ecdump_write(char *buf)
2199 char *cmd;
2200 int i, v;
2202 while ((cmd = next_cmd(&buf))) {
2203 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2204 /* i and v set */
2205 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2206 /* i and v set */
2207 } else
2208 return -EINVAL;
2209 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2210 if (!acpi_ec_write(i, v))
2211 return -EIO;
2212 } else
2213 return -EINVAL;
2216 return 0;
2219 static struct ibm_struct ecdump_driver_data = {
2220 .name = "ecdump",
2221 .read = ecdump_read,
2222 .write = ecdump_write,
2223 .flags.experimental = 1,
2226 /*************************************************************************
2227 * Backlight/brightness subdriver
2230 static struct backlight_device *ibm_backlight_device = NULL;
2232 static struct backlight_properties ibm_backlight_data = {
2233 .owner = THIS_MODULE,
2234 .get_brightness = brightness_get,
2235 .update_status = brightness_update_status,
2236 .max_brightness = 7,
2239 static int __init brightness_init(struct ibm_init_struct *iibm)
2241 int b;
2243 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2245 b = brightness_get(NULL);
2246 if (b < 0)
2247 return b;
2249 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
2250 &ibm_backlight_data);
2251 if (IS_ERR(ibm_backlight_device)) {
2252 printk(IBM_ERR "Could not register backlight device\n");
2253 return PTR_ERR(ibm_backlight_device);
2255 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2257 ibm_backlight_device->props->brightness = b;
2258 brightness_update_status(ibm_backlight_device);
2260 return 0;
2263 static void brightness_exit(void)
2265 if (ibm_backlight_device) {
2266 vdbg_printk(TPACPI_DBG_EXIT,
2267 "calling backlight_device_unregister()\n");
2268 backlight_device_unregister(ibm_backlight_device);
2269 ibm_backlight_device = NULL;
2273 static int brightness_update_status(struct backlight_device *bd)
2275 return brightness_set(
2276 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
2277 bd->props->power == FB_BLANK_UNBLANK) ?
2278 bd->props->brightness : 0);
2281 static int brightness_get(struct backlight_device *bd)
2283 u8 level;
2284 if (!acpi_ec_read(brightness_offset, &level))
2285 return -EIO;
2287 level &= 0x7;
2289 return level;
2292 static int brightness_set(int value)
2294 int cmos_cmd, inc, i;
2295 int current_value = brightness_get(NULL);
2297 value &= 7;
2299 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2300 inc = value > current_value ? 1 : -1;
2301 for (i = current_value; i != value; i += inc) {
2302 if (issue_thinkpad_cmos_command(cmos_cmd))
2303 return -EIO;
2304 if (!acpi_ec_write(brightness_offset, i + inc))
2305 return -EIO;
2308 return 0;
2311 static int brightness_read(char *p)
2313 int len = 0;
2314 int level;
2316 if ((level = brightness_get(NULL)) < 0) {
2317 len += sprintf(p + len, "level:\t\tunreadable\n");
2318 } else {
2319 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2320 len += sprintf(p + len, "commands:\tup, down\n");
2321 len += sprintf(p + len, "commands:\tlevel <level>"
2322 " (<level> is 0-7)\n");
2325 return len;
2328 static int brightness_write(char *buf)
2330 int level;
2331 int new_level;
2332 char *cmd;
2334 while ((cmd = next_cmd(&buf))) {
2335 if ((level = brightness_get(NULL)) < 0)
2336 return level;
2337 level &= 7;
2339 if (strlencmp(cmd, "up") == 0) {
2340 new_level = level == 7 ? 7 : level + 1;
2341 } else if (strlencmp(cmd, "down") == 0) {
2342 new_level = level == 0 ? 0 : level - 1;
2343 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2344 new_level >= 0 && new_level <= 7) {
2345 /* new_level set */
2346 } else
2347 return -EINVAL;
2349 brightness_set(new_level);
2352 return 0;
2355 static struct ibm_struct brightness_driver_data = {
2356 .name = "brightness",
2357 .read = brightness_read,
2358 .write = brightness_write,
2359 .exit = brightness_exit,
2362 /*************************************************************************
2363 * Volume subdriver
2366 static int volume_read(char *p)
2368 int len = 0;
2369 u8 level;
2371 if (!acpi_ec_read(volume_offset, &level)) {
2372 len += sprintf(p + len, "level:\t\tunreadable\n");
2373 } else {
2374 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2375 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2376 len += sprintf(p + len, "commands:\tup, down, mute\n");
2377 len += sprintf(p + len, "commands:\tlevel <level>"
2378 " (<level> is 0-15)\n");
2381 return len;
2384 static int volume_write(char *buf)
2386 int cmos_cmd, inc, i;
2387 u8 level, mute;
2388 int new_level, new_mute;
2389 char *cmd;
2391 while ((cmd = next_cmd(&buf))) {
2392 if (!acpi_ec_read(volume_offset, &level))
2393 return -EIO;
2394 new_mute = mute = level & 0x40;
2395 new_level = level = level & 0xf;
2397 if (strlencmp(cmd, "up") == 0) {
2398 if (mute)
2399 new_mute = 0;
2400 else
2401 new_level = level == 15 ? 15 : level + 1;
2402 } else if (strlencmp(cmd, "down") == 0) {
2403 if (mute)
2404 new_mute = 0;
2405 else
2406 new_level = level == 0 ? 0 : level - 1;
2407 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2408 new_level >= 0 && new_level <= 15) {
2409 /* new_level set */
2410 } else if (strlencmp(cmd, "mute") == 0) {
2411 new_mute = 0x40;
2412 } else
2413 return -EINVAL;
2415 if (new_level != level) { /* mute doesn't change */
2416 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2417 inc = new_level > level ? 1 : -1;
2419 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2420 !acpi_ec_write(volume_offset, level)))
2421 return -EIO;
2423 for (i = level; i != new_level; i += inc)
2424 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2425 !acpi_ec_write(volume_offset, i + inc))
2426 return -EIO;
2428 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2429 !acpi_ec_write(volume_offset,
2430 new_level + mute)))
2431 return -EIO;
2434 if (new_mute != mute) { /* level doesn't change */
2435 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2437 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2438 !acpi_ec_write(volume_offset, level + new_mute))
2439 return -EIO;
2443 return 0;
2446 static struct ibm_struct volume_driver_data = {
2447 .name = "volume",
2448 .read = volume_read,
2449 .write = volume_write,
2452 /*************************************************************************
2453 * Fan subdriver
2457 * FAN ACCESS MODES
2459 * TPACPI_FAN_RD_ACPI_GFAN:
2460 * ACPI GFAN method: returns fan level
2462 * see TPACPI_FAN_WR_ACPI_SFAN
2463 * EC 0x2f (HFSP) not available if GFAN exists
2465 * TPACPI_FAN_WR_ACPI_SFAN:
2466 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2468 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2469 * it for writing.
2471 * TPACPI_FAN_WR_TPEC:
2472 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2473 * Supported on almost all ThinkPads
2475 * Fan speed changes of any sort (including those caused by the
2476 * disengaged mode) are usually done slowly by the firmware as the
2477 * maximum ammount of fan duty cycle change per second seems to be
2478 * limited.
2480 * Reading is not available if GFAN exists.
2481 * Writing is not available if SFAN exists.
2483 * Bits
2484 * 7 automatic mode engaged;
2485 * (default operation mode of the ThinkPad)
2486 * fan level is ignored in this mode.
2487 * 6 full speed mode (takes precedence over bit 7);
2488 * not available on all thinkpads. May disable
2489 * the tachometer while the fan controller ramps up
2490 * the speed (which can take up to a few *minutes*).
2491 * Speeds up fan to 100% duty-cycle, which is far above
2492 * the standard RPM levels. It is not impossible that
2493 * it could cause hardware damage.
2494 * 5-3 unused in some models. Extra bits for fan level
2495 * in others, but still useless as all values above
2496 * 7 map to the same speed as level 7 in these models.
2497 * 2-0 fan level (0..7 usually)
2498 * 0x00 = stop
2499 * 0x07 = max (set when temperatures critical)
2500 * Some ThinkPads may have other levels, see
2501 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2503 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2504 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2505 * does so, its initial value is meaningless (0x07).
2507 * For firmware bugs, refer to:
2508 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2510 * ----
2512 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2513 * Main fan tachometer reading (in RPM)
2515 * This register is present on all ThinkPads with a new-style EC, and
2516 * it is known not to be present on the A21m/e, and T22, as there is
2517 * something else in offset 0x84 according to the ACPI DSDT. Other
2518 * ThinkPads from this same time period (and earlier) probably lack the
2519 * tachometer as well.
2521 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2522 * was never fixed by IBM to report the EC firmware version string
2523 * probably support the tachometer (like the early X models), so
2524 * detecting it is quite hard. We need more data to know for sure.
2526 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2527 * might result.
2529 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2530 * mode.
2532 * For firmware bugs, refer to:
2533 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2535 * TPACPI_FAN_WR_ACPI_FANS:
2536 * ThinkPad X31, X40, X41. Not available in the X60.
2538 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2539 * high speed. ACPI DSDT seems to map these three speeds to levels
2540 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2541 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2543 * The speeds are stored on handles
2544 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2546 * There are three default speed sets, acessible as handles:
2547 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2549 * ACPI DSDT switches which set is in use depending on various
2550 * factors.
2552 * TPACPI_FAN_WR_TPEC is also available and should be used to
2553 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2554 * but the ACPI tables just mention level 7.
2557 static enum fan_status_access_mode fan_status_access_mode;
2558 static enum fan_control_access_mode fan_control_access_mode;
2559 static enum fan_control_commands fan_control_commands;
2561 static u8 fan_control_initial_status;
2563 static void fan_watchdog_fire(struct work_struct *ignored);
2564 static int fan_watchdog_maxinterval;
2565 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
2567 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2568 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2569 "\\FSPD", /* 600e/x, 770e, 770x */
2570 ); /* all others */
2571 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2572 "JFNS", /* 770x-JL */
2573 ); /* all others */
2575 static int __init fan_init(struct ibm_init_struct *iibm)
2577 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2579 fan_status_access_mode = TPACPI_FAN_NONE;
2580 fan_control_access_mode = TPACPI_FAN_WR_NONE;
2581 fan_control_commands = 0;
2582 fan_watchdog_maxinterval = 0;
2583 tp_features.fan_ctrl_status_undef = 0;
2585 IBM_ACPIHANDLE_INIT(fans);
2586 IBM_ACPIHANDLE_INIT(gfan);
2587 IBM_ACPIHANDLE_INIT(sfan);
2589 if (gfan_handle) {
2590 /* 570, 600e/x, 770e, 770x */
2591 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
2592 } else {
2593 /* all other ThinkPads: note that even old-style
2594 * ThinkPad ECs supports the fan control register */
2595 if (likely(acpi_ec_read(fan_status_offset,
2596 &fan_control_initial_status))) {
2597 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
2599 /* In some ThinkPads, neither the EC nor the ACPI
2600 * DSDT initialize the fan status, and it ends up
2601 * being set to 0x07 when it *could* be either
2602 * 0x07 or 0x80.
2604 * Enable for TP-1Y (T43), TP-78 (R51e),
2605 * TP-76 (R52), TP-70 (T43, R52), which are known
2606 * to be buggy. */
2607 if (fan_control_initial_status == 0x07 &&
2608 ibm_thinkpad_ec_found &&
2609 ((ibm_thinkpad_ec_found[0] == '1' &&
2610 ibm_thinkpad_ec_found[1] == 'Y') ||
2611 (ibm_thinkpad_ec_found[0] == '7' &&
2612 (ibm_thinkpad_ec_found[1] == '6' ||
2613 ibm_thinkpad_ec_found[1] == '8' ||
2614 ibm_thinkpad_ec_found[1] == '0'))
2615 )) {
2616 printk(IBM_NOTICE
2617 "fan_init: initial fan status is "
2618 "unknown, assuming it is in auto "
2619 "mode\n");
2620 tp_features.fan_ctrl_status_undef = 1;
2622 } else {
2623 printk(IBM_ERR
2624 "ThinkPad ACPI EC access misbehaving, "
2625 "fan status and control unavailable\n");
2626 return 1;
2630 if (sfan_handle) {
2631 /* 570, 770x-JL */
2632 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
2633 fan_control_commands |=
2634 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
2635 } else {
2636 if (!gfan_handle) {
2637 /* gfan without sfan means no fan control */
2638 /* all other models implement TP EC 0x2f control */
2640 if (fans_handle) {
2641 /* X31, X40, X41 */
2642 fan_control_access_mode =
2643 TPACPI_FAN_WR_ACPI_FANS;
2644 fan_control_commands |=
2645 TPACPI_FAN_CMD_SPEED |
2646 TPACPI_FAN_CMD_LEVEL |
2647 TPACPI_FAN_CMD_ENABLE;
2648 } else {
2649 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
2650 fan_control_commands |=
2651 TPACPI_FAN_CMD_LEVEL |
2652 TPACPI_FAN_CMD_ENABLE;
2657 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2658 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2659 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2660 fan_status_access_mode, fan_control_access_mode);
2662 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2663 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2664 0 : 1;
2667 static int fan_get_status(u8 *status)
2669 u8 s;
2671 /* TODO:
2672 * Add TPACPI_FAN_RD_ACPI_FANS ? */
2674 switch (fan_status_access_mode) {
2675 case TPACPI_FAN_RD_ACPI_GFAN:
2676 /* 570, 600e/x, 770e, 770x */
2678 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2679 return -EIO;
2681 if (likely(status))
2682 *status = s & 0x07;
2684 break;
2686 case TPACPI_FAN_RD_TPEC:
2687 /* all except 570, 600e/x, 770e, 770x */
2688 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2689 return -EIO;
2691 if (likely(status))
2692 *status = s;
2694 break;
2696 default:
2697 return -ENXIO;
2700 return 0;
2703 static void fan_exit(void)
2705 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
2706 cancel_delayed_work(&fan_watchdog_task);
2707 flush_scheduled_work();
2710 static int fan_get_speed(unsigned int *speed)
2712 u8 hi, lo;
2714 switch (fan_status_access_mode) {
2715 case TPACPI_FAN_RD_TPEC:
2716 /* all except 570, 600e/x, 770e, 770x */
2717 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2718 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2719 return -EIO;
2721 if (likely(speed))
2722 *speed = (hi << 8) | lo;
2724 break;
2726 default:
2727 return -ENXIO;
2730 return 0;
2733 static void fan_watchdog_fire(struct work_struct *ignored)
2735 int rc;
2737 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2738 rc = fan_set_enable();
2739 if (rc < 0) {
2740 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
2741 "will try again later...\n", -rc);
2742 /* reschedule for later */
2743 fan_watchdog_reset();
2747 static void fan_watchdog_reset(void)
2749 static int fan_watchdog_active = 0;
2751 if (fan_watchdog_active)
2752 cancel_delayed_work(&fan_watchdog_task);
2754 if (fan_watchdog_maxinterval > 0) {
2755 fan_watchdog_active = 1;
2756 if (!schedule_delayed_work(&fan_watchdog_task,
2757 msecs_to_jiffies(fan_watchdog_maxinterval
2758 * 1000))) {
2759 printk(IBM_ERR "failed to schedule the fan watchdog, "
2760 "watchdog will not trigger\n");
2762 } else
2763 fan_watchdog_active = 0;
2766 static int fan_set_level(int level)
2768 switch (fan_control_access_mode) {
2769 case TPACPI_FAN_WR_ACPI_SFAN:
2770 if (level >= 0 && level <= 7) {
2771 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2772 return -EIO;
2773 } else
2774 return -EINVAL;
2775 break;
2777 case TPACPI_FAN_WR_ACPI_FANS:
2778 case TPACPI_FAN_WR_TPEC:
2779 if ((level != TP_EC_FAN_AUTO) &&
2780 (level != TP_EC_FAN_FULLSPEED) &&
2781 ((level < 0) || (level > 7)))
2782 return -EINVAL;
2784 if (!acpi_ec_write(fan_status_offset, level))
2785 return -EIO;
2786 else
2787 tp_features.fan_ctrl_status_undef = 0;
2788 break;
2790 default:
2791 return -ENXIO;
2793 return 0;
2796 static int fan_set_enable(void)
2798 u8 s;
2799 int rc;
2801 switch (fan_control_access_mode) {
2802 case TPACPI_FAN_WR_ACPI_FANS:
2803 case TPACPI_FAN_WR_TPEC:
2804 if ((rc = fan_get_status(&s)) < 0)
2805 return rc;
2807 /* Don't go out of emergency fan mode */
2808 if (s != 7)
2809 s = TP_EC_FAN_AUTO;
2811 if (!acpi_ec_write(fan_status_offset, s))
2812 return -EIO;
2813 else
2814 tp_features.fan_ctrl_status_undef = 0;
2815 break;
2817 case TPACPI_FAN_WR_ACPI_SFAN:
2818 if ((rc = fan_get_status(&s)) < 0)
2819 return rc;
2821 s &= 0x07;
2823 /* Set fan to at least level 4 */
2824 if (s < 4)
2825 s = 4;
2827 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2828 return -EIO;
2829 break;
2831 default:
2832 return -ENXIO;
2834 return 0;
2837 static int fan_set_disable(void)
2839 switch (fan_control_access_mode) {
2840 case TPACPI_FAN_WR_ACPI_FANS:
2841 case TPACPI_FAN_WR_TPEC:
2842 if (!acpi_ec_write(fan_status_offset, 0x00))
2843 return -EIO;
2844 else
2845 tp_features.fan_ctrl_status_undef = 0;
2846 break;
2848 case TPACPI_FAN_WR_ACPI_SFAN:
2849 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2850 return -EIO;
2851 break;
2853 default:
2854 return -ENXIO;
2856 return 0;
2859 static int fan_set_speed(int speed)
2861 switch (fan_control_access_mode) {
2862 case TPACPI_FAN_WR_ACPI_FANS:
2863 if (speed >= 0 && speed <= 65535) {
2864 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2865 speed, speed, speed))
2866 return -EIO;
2867 } else
2868 return -EINVAL;
2869 break;
2871 default:
2872 return -ENXIO;
2874 return 0;
2877 static int fan_read(char *p)
2879 int len = 0;
2880 int rc;
2881 u8 status;
2882 unsigned int speed = 0;
2884 switch (fan_status_access_mode) {
2885 case TPACPI_FAN_RD_ACPI_GFAN:
2886 /* 570, 600e/x, 770e, 770x */
2887 if ((rc = fan_get_status(&status)) < 0)
2888 return rc;
2890 len += sprintf(p + len, "status:\t\t%s\n"
2891 "level:\t\t%d\n",
2892 (status != 0) ? "enabled" : "disabled", status);
2893 break;
2895 case TPACPI_FAN_RD_TPEC:
2896 /* all except 570, 600e/x, 770e, 770x */
2897 if ((rc = fan_get_status(&status)) < 0)
2898 return rc;
2900 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2901 if (status != fan_control_initial_status)
2902 tp_features.fan_ctrl_status_undef = 0;
2903 else
2904 /* Return most likely status. In fact, it
2905 * might be the only possible status */
2906 status = TP_EC_FAN_AUTO;
2909 len += sprintf(p + len, "status:\t\t%s\n",
2910 (status != 0) ? "enabled" : "disabled");
2912 if ((rc = fan_get_speed(&speed)) < 0)
2913 return rc;
2915 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2917 if (status & TP_EC_FAN_FULLSPEED)
2918 /* Disengaged mode takes precedence */
2919 len += sprintf(p + len, "level:\t\tdisengaged\n");
2920 else if (status & TP_EC_FAN_AUTO)
2921 len += sprintf(p + len, "level:\t\tauto\n");
2922 else
2923 len += sprintf(p + len, "level:\t\t%d\n", status);
2924 break;
2926 case TPACPI_FAN_NONE:
2927 default:
2928 len += sprintf(p + len, "status:\t\tnot supported\n");
2931 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
2932 len += sprintf(p + len, "commands:\tlevel <level>");
2934 switch (fan_control_access_mode) {
2935 case TPACPI_FAN_WR_ACPI_SFAN:
2936 len += sprintf(p + len, " (<level> is 0-7)\n");
2937 break;
2939 default:
2940 len += sprintf(p + len, " (<level> is 0-7, "
2941 "auto, disengaged)\n");
2942 break;
2946 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
2947 len += sprintf(p + len, "commands:\tenable, disable\n"
2948 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2949 "1-120 (seconds))\n");
2951 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
2952 len += sprintf(p + len, "commands:\tspeed <speed>"
2953 " (<speed> is 0-65535)\n");
2955 return len;
2958 static int fan_write_cmd_level(const char *cmd, int *rc)
2960 int level;
2962 if (strlencmp(cmd, "level auto") == 0)
2963 level = TP_EC_FAN_AUTO;
2964 else if (strlencmp(cmd, "level disengaged") == 0)
2965 level = TP_EC_FAN_FULLSPEED;
2966 else if (sscanf(cmd, "level %d", &level) != 1)
2967 return 0;
2969 if ((*rc = fan_set_level(level)) == -ENXIO)
2970 printk(IBM_ERR "level command accepted for unsupported "
2971 "access mode %d", fan_control_access_mode);
2973 return 1;
2976 static int fan_write_cmd_enable(const char *cmd, int *rc)
2978 if (strlencmp(cmd, "enable") != 0)
2979 return 0;
2981 if ((*rc = fan_set_enable()) == -ENXIO)
2982 printk(IBM_ERR "enable command accepted for unsupported "
2983 "access mode %d", fan_control_access_mode);
2985 return 1;
2988 static int fan_write_cmd_disable(const char *cmd, int *rc)
2990 if (strlencmp(cmd, "disable") != 0)
2991 return 0;
2993 if ((*rc = fan_set_disable()) == -ENXIO)
2994 printk(IBM_ERR "disable command accepted for unsupported "
2995 "access mode %d", fan_control_access_mode);
2997 return 1;
3000 static int fan_write_cmd_speed(const char *cmd, int *rc)
3002 int speed;
3004 /* TODO:
3005 * Support speed <low> <medium> <high> ? */
3007 if (sscanf(cmd, "speed %d", &speed) != 1)
3008 return 0;
3010 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3011 printk(IBM_ERR "speed command accepted for unsupported "
3012 "access mode %d", fan_control_access_mode);
3014 return 1;
3017 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3019 int interval;
3021 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3022 return 0;
3024 if (interval < 0 || interval > 120)
3025 *rc = -EINVAL;
3026 else
3027 fan_watchdog_maxinterval = interval;
3029 return 1;
3032 static int fan_write(char *buf)
3034 char *cmd;
3035 int rc = 0;
3037 while (!rc && (cmd = next_cmd(&buf))) {
3038 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3039 fan_write_cmd_level(cmd, &rc)) &&
3040 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3041 (fan_write_cmd_enable(cmd, &rc) ||
3042 fan_write_cmd_disable(cmd, &rc) ||
3043 fan_write_cmd_watchdog(cmd, &rc))) &&
3044 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3045 fan_write_cmd_speed(cmd, &rc))
3047 rc = -EINVAL;
3048 else if (!rc)
3049 fan_watchdog_reset();
3052 return rc;
3055 static struct ibm_struct fan_driver_data = {
3056 .name = "fan",
3057 .read = fan_read,
3058 .write = fan_write,
3059 .exit = fan_exit,
3060 .flags.experimental = 1,
3063 /****************************************************************************
3064 ****************************************************************************
3066 * Infrastructure
3068 ****************************************************************************
3069 ****************************************************************************/
3071 /* /proc support */
3072 static struct proc_dir_entry *proc_dir = NULL;
3074 /* Subdriver registry */
3075 static LIST_HEAD(tpacpi_all_drivers);
3079 * Module and infrastructure proble, init and exit handling
3082 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3083 static const char * __init str_supported(int is_supported)
3085 static char text_unsupported[] __initdata = "not supported";
3087 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3089 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3091 static int __init ibm_init(struct ibm_init_struct *iibm)
3093 int ret;
3094 struct ibm_struct *ibm = iibm->data;
3095 struct proc_dir_entry *entry;
3097 BUG_ON(ibm == NULL);
3099 INIT_LIST_HEAD(&ibm->all_drivers);
3101 if (ibm->flags.experimental && !experimental)
3102 return 0;
3104 dbg_printk(TPACPI_DBG_INIT,
3105 "probing for %s\n", ibm->name);
3107 if (iibm->init) {
3108 ret = iibm->init(iibm);
3109 if (ret > 0)
3110 return 0; /* probe failed */
3111 if (ret)
3112 return ret;
3114 ibm->flags.init_called = 1;
3117 if (ibm->acpi) {
3118 if (ibm->acpi->hid) {
3119 ret = register_tpacpi_subdriver(ibm);
3120 if (ret)
3121 goto err_out;
3124 if (ibm->acpi->notify) {
3125 ret = setup_acpi_notify(ibm);
3126 if (ret == -ENODEV) {
3127 printk(IBM_NOTICE "disabling subdriver %s\n",
3128 ibm->name);
3129 ret = 0;
3130 goto err_out;
3132 if (ret < 0)
3133 goto err_out;
3137 dbg_printk(TPACPI_DBG_INIT,
3138 "%s installed\n", ibm->name);
3140 if (ibm->read) {
3141 entry = create_proc_entry(ibm->name,
3142 S_IFREG | S_IRUGO | S_IWUSR,
3143 proc_dir);
3144 if (!entry) {
3145 printk(IBM_ERR "unable to create proc entry %s\n",
3146 ibm->name);
3147 ret = -ENODEV;
3148 goto err_out;
3150 entry->owner = THIS_MODULE;
3151 entry->data = ibm;
3152 entry->read_proc = &dispatch_procfs_read;
3153 if (ibm->write)
3154 entry->write_proc = &dispatch_procfs_write;
3155 ibm->flags.proc_created = 1;
3158 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3160 return 0;
3162 err_out:
3163 dbg_printk(TPACPI_DBG_INIT,
3164 "%s: at error exit path with result %d\n",
3165 ibm->name, ret);
3167 ibm_exit(ibm);
3168 return (ret < 0)? ret : 0;
3171 static void ibm_exit(struct ibm_struct *ibm)
3173 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
3175 list_del_init(&ibm->all_drivers);
3177 if (ibm->flags.acpi_notify_installed) {
3178 dbg_printk(TPACPI_DBG_EXIT,
3179 "%s: acpi_remove_notify_handler\n", ibm->name);
3180 BUG_ON(!ibm->acpi);
3181 acpi_remove_notify_handler(*ibm->acpi->handle,
3182 ibm->acpi->type,
3183 dispatch_acpi_notify);
3184 ibm->flags.acpi_notify_installed = 0;
3185 ibm->flags.acpi_notify_installed = 0;
3188 if (ibm->flags.proc_created) {
3189 dbg_printk(TPACPI_DBG_EXIT,
3190 "%s: remove_proc_entry\n", ibm->name);
3191 remove_proc_entry(ibm->name, proc_dir);
3192 ibm->flags.proc_created = 0;
3195 if (ibm->flags.acpi_driver_registered) {
3196 dbg_printk(TPACPI_DBG_EXIT,
3197 "%s: acpi_bus_unregister_driver\n", ibm->name);
3198 BUG_ON(!ibm->acpi);
3199 acpi_bus_unregister_driver(ibm->acpi->driver);
3200 kfree(ibm->acpi->driver);
3201 ibm->acpi->driver = NULL;
3202 ibm->flags.acpi_driver_registered = 0;
3205 if (ibm->flags.init_called && ibm->exit) {
3206 ibm->exit();
3207 ibm->flags.init_called = 0;
3210 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
3213 /* Probing */
3215 static char *ibm_thinkpad_ec_found = NULL;
3217 static char* __init check_dmi_for_ec(void)
3219 struct dmi_device *dev = NULL;
3220 char ec_fw_string[18];
3223 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3224 * X32 or newer, all Z series; Some models must have an
3225 * up-to-date BIOS or they will not be detected.
3227 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3229 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
3230 if (sscanf(dev->name,
3231 "IBM ThinkPad Embedded Controller -[%17c",
3232 ec_fw_string) == 1) {
3233 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3234 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3235 return kstrdup(ec_fw_string, GFP_KERNEL);
3238 return NULL;
3241 static int __init probe_for_thinkpad(void)
3243 int is_thinkpad;
3245 if (acpi_disabled)
3246 return -ENODEV;
3249 * Non-ancient models have better DMI tagging, but very old models
3250 * don't.
3252 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3254 /* ec is required because many other handles are relative to it */
3255 IBM_ACPIHANDLE_INIT(ec);
3256 if (!ec_handle) {
3257 if (is_thinkpad)
3258 printk(IBM_ERR
3259 "Not yet supported ThinkPad detected!\n");
3260 return -ENODEV;
3264 * Risks a regression on very old machines, but reduces potential
3265 * false positives a damn great deal
3267 if (!is_thinkpad)
3268 is_thinkpad = dmi_name_in_vendors("IBM");
3270 if (!is_thinkpad && !force_load)
3271 return -ENODEV;
3273 return 0;
3277 /* Module init, exit, parameters */
3279 static struct ibm_init_struct ibms_init[] __initdata = {
3281 .init = thinkpad_acpi_driver_init,
3282 .data = &thinkpad_acpi_driver_data,
3285 .init = hotkey_init,
3286 .data = &hotkey_driver_data,
3289 .init = bluetooth_init,
3290 .data = &bluetooth_driver_data,
3293 .init = wan_init,
3294 .data = &wan_driver_data,
3297 .init = video_init,
3298 .data = &video_driver_data,
3301 .init = light_init,
3302 .data = &light_driver_data,
3304 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3306 .init = dock_init,
3307 .data = &dock_driver_data[0],
3310 .data = &dock_driver_data[1],
3312 #endif
3313 #ifdef CONFIG_THINKPAD_ACPI_BAY
3315 .init = bay_init,
3316 .data = &bay_driver_data,
3318 #endif
3320 .init = cmos_init,
3321 .data = &cmos_driver_data,
3324 .init = led_init,
3325 .data = &led_driver_data,
3328 .init = beep_init,
3329 .data = &beep_driver_data,
3332 .init = thermal_init,
3333 .data = &thermal_driver_data,
3336 .data = &ecdump_driver_data,
3339 .init = brightness_init,
3340 .data = &brightness_driver_data,
3343 .data = &volume_driver_data,
3346 .init = fan_init,
3347 .data = &fan_driver_data,
3351 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3353 unsigned int i;
3354 struct ibm_struct *ibm;
3356 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3357 ibm = ibms_init[i].data;
3358 BUG_ON(ibm == NULL);
3360 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3361 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
3362 return -ENOSPC;
3363 strcpy(ibms_init[i].param, val);
3364 strcat(ibms_init[i].param, ",");
3365 return 0;
3369 return -EINVAL;
3372 static int experimental;
3373 module_param(experimental, int, 0);
3375 static u32 dbg_level;
3376 module_param_named(debug, dbg_level, uint, 0);
3378 static int force_load;
3379 module_param(force_load, int, 0);
3381 #define IBM_PARAM(feature) \
3382 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3384 IBM_PARAM(hotkey);
3385 IBM_PARAM(bluetooth);
3386 IBM_PARAM(video);
3387 IBM_PARAM(light);
3388 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3389 IBM_PARAM(dock);
3390 #endif
3391 #ifdef CONFIG_THINKPAD_ACPI_BAY
3392 IBM_PARAM(bay);
3393 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3394 IBM_PARAM(cmos);
3395 IBM_PARAM(led);
3396 IBM_PARAM(beep);
3397 IBM_PARAM(ecdump);
3398 IBM_PARAM(brightness);
3399 IBM_PARAM(volume);
3400 IBM_PARAM(fan);
3402 static int __init thinkpad_acpi_module_init(void)
3404 int ret, i;
3406 /* Driver-level probe */
3407 ret = probe_for_thinkpad();
3408 if (ret)
3409 return ret;
3411 /* Driver initialization */
3412 ibm_thinkpad_ec_found = check_dmi_for_ec();
3413 IBM_ACPIHANDLE_INIT(ecrd);
3414 IBM_ACPIHANDLE_INIT(ecwr);
3416 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
3417 if (!proc_dir) {
3418 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
3419 thinkpad_acpi_module_exit();
3420 return -ENODEV;
3422 proc_dir->owner = THIS_MODULE;
3424 ret = platform_driver_register(&tpacpi_pdriver);
3425 if (ret) {
3426 printk(IBM_ERR "unable to register platform driver\n");
3427 thinkpad_acpi_module_exit();
3428 return ret;
3430 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3431 if (ret) {
3432 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3433 thinkpad_acpi_module_exit();
3434 return ret;
3438 /* Device initialization */
3439 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3440 NULL, 0);
3441 if (IS_ERR(tpacpi_pdev)) {
3442 ret = PTR_ERR(tpacpi_pdev);
3443 tpacpi_pdev = NULL;
3444 printk(IBM_ERR "unable to register platform device\n");
3445 thinkpad_acpi_module_exit();
3446 return ret;
3448 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3449 if (IS_ERR(tpacpi_hwmon)) {
3450 ret = PTR_ERR(tpacpi_hwmon);
3451 tpacpi_hwmon = NULL;
3452 printk(IBM_ERR "unable to register hwmon device\n");
3453 thinkpad_acpi_module_exit();
3454 return ret;
3456 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3457 ret = ibm_init(&ibms_init[i]);
3458 if (ret >= 0 && *ibms_init[i].param)
3459 ret = ibms_init[i].data->write(ibms_init[i].param);
3460 if (ret < 0) {
3461 thinkpad_acpi_module_exit();
3462 return ret;
3466 return 0;
3469 static void thinkpad_acpi_module_exit(void)
3471 struct ibm_struct *ibm, *itmp;
3473 list_for_each_entry_safe_reverse(ibm, itmp,
3474 &tpacpi_all_drivers,
3475 all_drivers) {
3476 ibm_exit(ibm);
3479 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
3481 if (tpacpi_hwmon)
3482 hwmon_device_unregister(tpacpi_hwmon);
3484 if (tpacpi_pdev)
3485 platform_device_unregister(tpacpi_pdev);
3487 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
3488 platform_driver_unregister(&tpacpi_pdriver);
3490 if (proc_dir)
3491 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
3493 kfree(ibm_thinkpad_ec_found);
3496 module_init(thinkpad_acpi_module_init);
3497 module_exit(thinkpad_acpi_module_exit);