ACPI: thinkpad-acpi: export hotkey maximum masks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob5495ab55bea97410534ceb5a6f4474d8c35ea358
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");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 /****************************************************************************
121 ****************************************************************************
123 * ACPI Helpers and device model
125 ****************************************************************************
126 ****************************************************************************/
128 /*************************************************************************
129 * ACPI basic handles
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
154 * Misc ACPI handles
157 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
160 ); /* all others */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
165 ); /* 570 */
168 /*************************************************************************
169 * ACPI helpers
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
175 char *fmt0 = fmt;
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
197 res_type = *(fmt++);
199 params.count = 0;
200 params.pointer = &in_objs[0];
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
210 /* add more types as needed */
211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
217 va_end(ap);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
226 status = acpi_evaluate_object(handle, method, &params, resultp);
228 switch (res_type) {
229 case 'd': /* int */
230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
234 case 'v': /* void */
235 success = status == AE_OK;
236 break;
237 /* add more types as needed */
238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
248 return success;
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
253 int i;
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
263 int v;
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
274 return 1;
277 static int acpi_ec_write(int i, u8 v)
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
287 return 1;
290 static int _sta(acpi_handle handle)
292 int status;
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
297 return status;
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
302 if (!cmos_handle)
303 return -ENXIO;
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
308 return 0;
311 /*************************************************************************
312 * ACPI device model
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
319 int i;
320 acpi_status status;
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
332 return;
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
338 *handle = NULL;
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
343 struct ibm_struct *ibm = data;
345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
346 return;
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
353 acpi_status status;
354 int rc;
356 BUG_ON(!ibm->acpi);
358 if (!*ibm->acpi->handle)
359 return 0;
361 vdbg_printk(TPACPI_DBG_INIT,
362 "setting up ACPI notify for %s\n", ibm->name);
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
368 return -ENODEV;
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
386 return -ENODEV;
388 ibm->flags.acpi_notify_installed = 1;
389 return 0;
392 static int __init tpacpi_device_add(struct acpi_device *device)
394 return 0;
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
399 int rc;
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
404 BUG_ON(!ibm->acpi);
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
419 ibm->acpi->hid, rc);
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
422 } else if (!rc)
423 ibm->flags.acpi_driver_registered = 1;
425 return rc;
429 /****************************************************************************
430 ****************************************************************************
432 * Procfs Helpers
434 ****************************************************************************
435 ****************************************************************************/
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
440 struct ibm_struct *ibm = data;
441 int len;
443 if (!ibm || !ibm->read)
444 return -EINVAL;
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
459 return len;
462 static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
470 if (!ibm || !ibm->write)
471 return -EINVAL;
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
488 kfree(kernbuf);
490 return ret;
493 static char *next_cmd(char **cmds)
495 char *start = *cmds;
496 char *end;
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
501 if (!end)
502 return NULL;
504 *end = 0;
505 *cmds = end + 1;
506 return start;
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
521 static struct platform_driver tpacpi_pdriver = {
522 .driver = {
523 .name = IBM_DRVR_NAME,
524 .owner = THIS_MODULE,
529 /*************************************************************************
530 * thinkpad-acpi driver attributes
533 /* interface_version --------------------------------------------------- */
534 static ssize_t tpacpi_driver_interface_version_show(
535 struct device_driver *drv,
536 char *buf)
538 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
541 static DRIVER_ATTR(interface_version, S_IRUGO,
542 tpacpi_driver_interface_version_show, NULL);
544 /* debug_level --------------------------------------------------------- */
545 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
546 char *buf)
548 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
551 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
552 const char *buf, size_t count)
554 unsigned long t;
556 if (parse_strtoul(buf, 0xffff, &t))
557 return -EINVAL;
559 dbg_level = t;
561 return count;
564 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
565 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
567 /* version ------------------------------------------------------------- */
568 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
569 char *buf)
571 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
574 static DRIVER_ATTR(version, S_IRUGO,
575 tpacpi_driver_version_show, NULL);
577 /* --------------------------------------------------------------------- */
579 static struct driver_attribute* tpacpi_driver_attributes[] = {
580 &driver_attr_debug_level, &driver_attr_version,
581 &driver_attr_interface_version,
584 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
586 int i, res;
588 i = 0;
589 res = 0;
590 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
591 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
592 i++;
595 return res;
598 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
600 int i;
602 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
603 driver_remove_file(drv, tpacpi_driver_attributes[i]);
606 /*************************************************************************
607 * sysfs support helpers
610 struct attribute_set_obj {
611 struct attribute_set s;
612 struct attribute *a;
613 } __attribute__((packed));
615 static struct attribute_set *create_attr_set(unsigned int max_members,
616 const char* name)
618 struct attribute_set_obj *sobj;
620 if (max_members == 0)
621 return NULL;
623 /* Allocates space for implicit NULL at the end too */
624 sobj = kzalloc(sizeof(struct attribute_set_obj) +
625 max_members * sizeof(struct attribute *),
626 GFP_KERNEL);
627 if (!sobj)
628 return NULL;
629 sobj->s.max_members = max_members;
630 sobj->s.group.attrs = &sobj->a;
631 sobj->s.group.name = name;
633 return &sobj->s;
636 /* not multi-threaded safe, use it in a single thread per set */
637 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
639 if (!s || !attr)
640 return -EINVAL;
642 if (s->members >= s->max_members)
643 return -ENOMEM;
645 s->group.attrs[s->members] = attr;
646 s->members++;
648 return 0;
651 static int add_many_to_attr_set(struct attribute_set* s,
652 struct attribute **attr,
653 unsigned int count)
655 int i, res;
657 for (i = 0; i < count; i++) {
658 res = add_to_attr_set(s, attr[i]);
659 if (res)
660 return res;
663 return 0;
666 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
668 sysfs_remove_group(kobj, &s->group);
669 destroy_attr_set(s);
672 static int parse_strtoul(const char *buf,
673 unsigned long max, unsigned long *value)
675 char *endp;
677 *value = simple_strtoul(buf, &endp, 0);
678 while (*endp && isspace(*endp))
679 endp++;
680 if (*endp || *value > max)
681 return -EINVAL;
683 return 0;
686 /****************************************************************************
687 ****************************************************************************
689 * Subdrivers
691 ****************************************************************************
692 ****************************************************************************/
694 /*************************************************************************
695 * thinkpad-acpi init subdriver
698 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
700 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
701 printk(IBM_INFO "%s\n", IBM_URL);
703 if (ibm_thinkpad_ec_found)
704 printk(IBM_INFO "ThinkPad EC firmware %s\n",
705 ibm_thinkpad_ec_found);
707 return 0;
710 static int thinkpad_acpi_driver_read(char *p)
712 int len = 0;
714 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
715 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
717 return len;
720 static struct ibm_struct thinkpad_acpi_driver_data = {
721 .name = "driver",
722 .read = thinkpad_acpi_driver_read,
725 /*************************************************************************
726 * Hotkey subdriver
729 static int hotkey_orig_status;
730 static u32 hotkey_orig_mask;
731 static u32 hotkey_all_mask;
732 static u32 hotkey_reserved_mask = 0x00778000;
734 static struct attribute_set *hotkey_dev_attributes;
736 /* sysfs hotkey enable ------------------------------------------------- */
737 static ssize_t hotkey_enable_show(struct device *dev,
738 struct device_attribute *attr,
739 char *buf)
741 int res, status;
742 u32 mask;
744 res = hotkey_get(&status, &mask);
745 if (res)
746 return res;
748 return snprintf(buf, PAGE_SIZE, "%d\n", status);
751 static ssize_t hotkey_enable_store(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
755 unsigned long t;
756 int res, status;
757 u32 mask;
759 if (parse_strtoul(buf, 1, &t))
760 return -EINVAL;
762 res = hotkey_get(&status, &mask);
763 if (!res)
764 res = hotkey_set(t, mask);
766 return (res) ? res : count;
769 static struct device_attribute dev_attr_hotkey_enable =
770 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
771 hotkey_enable_show, hotkey_enable_store);
773 /* sysfs hotkey mask --------------------------------------------------- */
774 static ssize_t hotkey_mask_show(struct device *dev,
775 struct device_attribute *attr,
776 char *buf)
778 int res, status;
779 u32 mask;
781 res = hotkey_get(&status, &mask);
782 if (res)
783 return res;
785 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
788 static ssize_t hotkey_mask_store(struct device *dev,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
792 unsigned long t;
793 int res, status;
794 u32 mask;
796 if (parse_strtoul(buf, 0xffffffffUL, &t))
797 return -EINVAL;
799 res = hotkey_get(&status, &mask);
800 if (!res)
801 hotkey_set(status, t);
803 return (res) ? res : count;
806 static struct device_attribute dev_attr_hotkey_mask =
807 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
808 hotkey_mask_show, hotkey_mask_store);
810 /* sysfs hotkey bios_enabled ------------------------------------------- */
811 static ssize_t hotkey_bios_enabled_show(struct device *dev,
812 struct device_attribute *attr,
813 char *buf)
815 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
818 static struct device_attribute dev_attr_hotkey_bios_enabled =
819 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
821 /* sysfs hotkey bios_mask ---------------------------------------------- */
822 static ssize_t hotkey_bios_mask_show(struct device *dev,
823 struct device_attribute *attr,
824 char *buf)
826 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
829 static struct device_attribute dev_attr_hotkey_bios_mask =
830 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
832 /* sysfs hotkey all_mask ----------------------------------------------- */
833 static ssize_t hotkey_all_mask_show(struct device *dev,
834 struct device_attribute *attr,
835 char *buf)
837 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
840 static struct device_attribute dev_attr_hotkey_all_mask =
841 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
843 /* sysfs hotkey recommended_mask --------------------------------------- */
844 static ssize_t hotkey_recommended_mask_show(struct device *dev,
845 struct device_attribute *attr,
846 char *buf)
848 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
849 hotkey_all_mask & ~hotkey_reserved_mask);
852 static struct device_attribute dev_attr_hotkey_recommended_mask =
853 __ATTR(hotkey_recommended_mask, S_IRUGO,
854 hotkey_recommended_mask_show, NULL);
856 /* --------------------------------------------------------------------- */
858 static struct attribute *hotkey_mask_attributes[] = {
859 &dev_attr_hotkey_mask.attr,
860 &dev_attr_hotkey_bios_enabled.attr,
861 &dev_attr_hotkey_bios_mask.attr,
862 &dev_attr_hotkey_all_mask.attr,
863 &dev_attr_hotkey_recommended_mask.attr,
866 static int __init hotkey_init(struct ibm_init_struct *iibm)
868 int res;
870 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
872 IBM_ACPIHANDLE_INIT(hkey);
873 mutex_init(&hotkey_mutex);
875 /* hotkey not supported on 570 */
876 tp_features.hotkey = hkey_handle != NULL;
878 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
879 str_supported(tp_features.hotkey));
881 if (tp_features.hotkey) {
882 hotkey_dev_attributes = create_attr_set(6, NULL);
883 if (!hotkey_dev_attributes)
884 return -ENOMEM;
885 res = add_to_attr_set(hotkey_dev_attributes,
886 &dev_attr_hotkey_enable.attr);
887 if (res)
888 return res;
890 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
891 A30, R30, R31, T20-22, X20-21, X22-24 */
892 tp_features.hotkey_mask =
893 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
895 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
896 str_supported(tp_features.hotkey_mask));
898 if (tp_features.hotkey_mask) {
899 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
900 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
901 "MHKA", "qd"))
902 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
905 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
906 if (!res && tp_features.hotkey_mask) {
907 res = add_many_to_attr_set(hotkey_dev_attributes,
908 hotkey_mask_attributes,
909 ARRAY_SIZE(hotkey_mask_attributes));
911 if (!res)
912 res = register_attr_set_with_sysfs(
913 hotkey_dev_attributes,
914 &tpacpi_pdev->dev.kobj);
916 if (res)
917 return res;
920 return (tp_features.hotkey)? 0 : 1;
923 static void hotkey_exit(void)
925 int res;
927 if (tp_features.hotkey) {
928 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
929 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
930 if (res)
931 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
934 if (hotkey_dev_attributes) {
935 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
936 hotkey_dev_attributes = NULL;
940 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
942 int hkey;
944 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
945 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
946 } else {
947 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
948 acpi_bus_generate_event(ibm->acpi->device, event, 0);
953 * Call with hotkey_mutex held
955 static int hotkey_get(int *status, u32 *mask)
957 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
958 return -EIO;
960 if (tp_features.hotkey_mask)
961 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
962 return -EIO;
964 return 0;
968 * Call with hotkey_mutex held
970 static int hotkey_set(int status, u32 mask)
972 int i;
974 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
975 return -EIO;
977 if (tp_features.hotkey_mask)
978 for (i = 0; i < 32; i++) {
979 int bit = ((1 << i) & mask) != 0;
980 if (!acpi_evalf(hkey_handle,
981 NULL, "MHKM", "vdd", i + 1, bit))
982 return -EIO;
985 return 0;
988 /* procfs -------------------------------------------------------------- */
989 static int hotkey_read(char *p)
991 int res, status;
992 u32 mask;
993 int len = 0;
995 if (!tp_features.hotkey) {
996 len += sprintf(p + len, "status:\t\tnot supported\n");
997 return len;
1000 res = mutex_lock_interruptible(&hotkey_mutex);
1001 if (res < 0)
1002 return res;
1003 res = hotkey_get(&status, &mask);
1004 mutex_unlock(&hotkey_mutex);
1005 if (res)
1006 return res;
1008 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1009 if (tp_features.hotkey_mask) {
1010 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1011 len += sprintf(p + len,
1012 "commands:\tenable, disable, reset, <mask>\n");
1013 } else {
1014 len += sprintf(p + len, "mask:\t\tnot supported\n");
1015 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1018 return len;
1021 static int hotkey_write(char *buf)
1023 int res, status;
1024 u32 mask;
1025 char *cmd;
1026 int do_cmd = 0;
1028 if (!tp_features.hotkey)
1029 return -ENODEV;
1031 res = mutex_lock_interruptible(&hotkey_mutex);
1032 if (res < 0)
1033 return res;
1035 res = hotkey_get(&status, &mask);
1036 if (res)
1037 goto errexit;
1039 res = 0;
1040 while ((cmd = next_cmd(&buf))) {
1041 if (strlencmp(cmd, "enable") == 0) {
1042 status = 1;
1043 } else if (strlencmp(cmd, "disable") == 0) {
1044 status = 0;
1045 } else if (strlencmp(cmd, "reset") == 0) {
1046 status = hotkey_orig_status;
1047 mask = hotkey_orig_mask;
1048 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1049 /* mask set */
1050 } else if (sscanf(cmd, "%x", &mask) == 1) {
1051 /* mask set */
1052 } else {
1053 res = -EINVAL;
1054 goto errexit;
1056 do_cmd = 1;
1059 if (do_cmd)
1060 res = hotkey_set(status, mask);
1062 errexit:
1063 mutex_unlock(&hotkey_mutex);
1064 return res;
1067 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1068 .hid = IBM_HKEY_HID,
1069 .notify = hotkey_notify,
1070 .handle = &hkey_handle,
1071 .type = ACPI_DEVICE_NOTIFY,
1074 static struct ibm_struct hotkey_driver_data = {
1075 .name = "hotkey",
1076 .read = hotkey_read,
1077 .write = hotkey_write,
1078 .exit = hotkey_exit,
1079 .acpi = &ibm_hotkey_acpidriver,
1082 /*************************************************************************
1083 * Bluetooth subdriver
1086 /* sysfs bluetooth enable ---------------------------------------------- */
1087 static ssize_t bluetooth_enable_show(struct device *dev,
1088 struct device_attribute *attr,
1089 char *buf)
1091 int status;
1093 status = bluetooth_get_radiosw();
1094 if (status < 0)
1095 return status;
1097 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1100 static ssize_t bluetooth_enable_store(struct device *dev,
1101 struct device_attribute *attr,
1102 const char *buf, size_t count)
1104 unsigned long t;
1105 int res;
1107 if (parse_strtoul(buf, 1, &t))
1108 return -EINVAL;
1110 res = bluetooth_set_radiosw(t);
1112 return (res) ? res : count;
1115 static struct device_attribute dev_attr_bluetooth_enable =
1116 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1117 bluetooth_enable_show, bluetooth_enable_store);
1119 /* --------------------------------------------------------------------- */
1121 static struct attribute *bluetooth_attributes[] = {
1122 &dev_attr_bluetooth_enable.attr,
1123 NULL
1126 static const struct attribute_group bluetooth_attr_group = {
1127 .attrs = bluetooth_attributes,
1130 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1132 int res;
1133 int status = 0;
1135 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1137 IBM_ACPIHANDLE_INIT(hkey);
1139 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1140 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1141 tp_features.bluetooth = hkey_handle &&
1142 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1144 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1145 str_supported(tp_features.bluetooth),
1146 status);
1148 if (tp_features.bluetooth) {
1149 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1150 /* no bluetooth hardware present in system */
1151 tp_features.bluetooth = 0;
1152 dbg_printk(TPACPI_DBG_INIT,
1153 "bluetooth hardware not installed\n");
1154 } else {
1155 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1156 &bluetooth_attr_group);
1157 if (res)
1158 return res;
1162 return (tp_features.bluetooth)? 0 : 1;
1165 static void bluetooth_exit(void)
1167 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1168 &bluetooth_attr_group);
1171 static int bluetooth_get_radiosw(void)
1173 int status;
1175 if (!tp_features.bluetooth)
1176 return -ENODEV;
1178 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1179 return -EIO;
1181 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1184 static int bluetooth_set_radiosw(int radio_on)
1186 int status;
1188 if (!tp_features.bluetooth)
1189 return -ENODEV;
1191 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1192 return -EIO;
1193 if (radio_on)
1194 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1195 else
1196 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1197 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1198 return -EIO;
1200 return 0;
1203 /* procfs -------------------------------------------------------------- */
1204 static int bluetooth_read(char *p)
1206 int len = 0;
1207 int status = bluetooth_get_radiosw();
1209 if (!tp_features.bluetooth)
1210 len += sprintf(p + len, "status:\t\tnot supported\n");
1211 else {
1212 len += sprintf(p + len, "status:\t\t%s\n",
1213 (status)? "enabled" : "disabled");
1214 len += sprintf(p + len, "commands:\tenable, disable\n");
1217 return len;
1220 static int bluetooth_write(char *buf)
1222 char *cmd;
1224 if (!tp_features.bluetooth)
1225 return -ENODEV;
1227 while ((cmd = next_cmd(&buf))) {
1228 if (strlencmp(cmd, "enable") == 0) {
1229 bluetooth_set_radiosw(1);
1230 } else if (strlencmp(cmd, "disable") == 0) {
1231 bluetooth_set_radiosw(0);
1232 } else
1233 return -EINVAL;
1236 return 0;
1239 static struct ibm_struct bluetooth_driver_data = {
1240 .name = "bluetooth",
1241 .read = bluetooth_read,
1242 .write = bluetooth_write,
1243 .exit = bluetooth_exit,
1246 /*************************************************************************
1247 * Wan subdriver
1250 /* sysfs wan enable ---------------------------------------------------- */
1251 static ssize_t wan_enable_show(struct device *dev,
1252 struct device_attribute *attr,
1253 char *buf)
1255 int status;
1257 status = wan_get_radiosw();
1258 if (status < 0)
1259 return status;
1261 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1264 static ssize_t wan_enable_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1268 unsigned long t;
1269 int res;
1271 if (parse_strtoul(buf, 1, &t))
1272 return -EINVAL;
1274 res = wan_set_radiosw(t);
1276 return (res) ? res : count;
1279 static struct device_attribute dev_attr_wan_enable =
1280 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1281 wan_enable_show, wan_enable_store);
1283 /* --------------------------------------------------------------------- */
1285 static struct attribute *wan_attributes[] = {
1286 &dev_attr_wan_enable.attr,
1287 NULL
1290 static const struct attribute_group wan_attr_group = {
1291 .attrs = wan_attributes,
1294 static int __init wan_init(struct ibm_init_struct *iibm)
1296 int res;
1297 int status = 0;
1299 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1301 IBM_ACPIHANDLE_INIT(hkey);
1303 tp_features.wan = hkey_handle &&
1304 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1306 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1307 str_supported(tp_features.wan),
1308 status);
1310 if (tp_features.wan) {
1311 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1312 /* no wan hardware present in system */
1313 tp_features.wan = 0;
1314 dbg_printk(TPACPI_DBG_INIT,
1315 "wan hardware not installed\n");
1316 } else {
1317 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1318 &wan_attr_group);
1319 if (res)
1320 return res;
1324 return (tp_features.wan)? 0 : 1;
1327 static void wan_exit(void)
1329 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1330 &wan_attr_group);
1333 static int wan_get_radiosw(void)
1335 int status;
1337 if (!tp_features.wan)
1338 return -ENODEV;
1340 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1341 return -EIO;
1343 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1346 static int wan_set_radiosw(int radio_on)
1348 int status;
1350 if (!tp_features.wan)
1351 return -ENODEV;
1353 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1354 return -EIO;
1355 if (radio_on)
1356 status |= TP_ACPI_WANCARD_RADIOSSW;
1357 else
1358 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1359 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1360 return -EIO;
1362 return 0;
1365 /* procfs -------------------------------------------------------------- */
1366 static int wan_read(char *p)
1368 int len = 0;
1369 int status = wan_get_radiosw();
1371 if (!tp_features.wan)
1372 len += sprintf(p + len, "status:\t\tnot supported\n");
1373 else {
1374 len += sprintf(p + len, "status:\t\t%s\n",
1375 (status)? "enabled" : "disabled");
1376 len += sprintf(p + len, "commands:\tenable, disable\n");
1379 return len;
1382 static int wan_write(char *buf)
1384 char *cmd;
1386 if (!tp_features.wan)
1387 return -ENODEV;
1389 while ((cmd = next_cmd(&buf))) {
1390 if (strlencmp(cmd, "enable") == 0) {
1391 wan_set_radiosw(1);
1392 } else if (strlencmp(cmd, "disable") == 0) {
1393 wan_set_radiosw(0);
1394 } else
1395 return -EINVAL;
1398 return 0;
1401 static struct ibm_struct wan_driver_data = {
1402 .name = "wan",
1403 .read = wan_read,
1404 .write = wan_write,
1405 .exit = wan_exit,
1406 .flags.experimental = 1,
1409 /*************************************************************************
1410 * Video subdriver
1413 static enum video_access_mode video_supported;
1414 static int video_orig_autosw;
1416 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1417 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1418 "\\_SB.PCI0.VID0", /* 770e */
1419 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1420 "\\_SB.PCI0.AGP.VID", /* all others */
1421 ); /* R30, R31 */
1423 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1425 static int __init video_init(struct ibm_init_struct *iibm)
1427 int ivga;
1429 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1431 IBM_ACPIHANDLE_INIT(vid);
1432 IBM_ACPIHANDLE_INIT(vid2);
1434 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1435 /* G41, assume IVGA doesn't change */
1436 vid_handle = vid2_handle;
1438 if (!vid_handle)
1439 /* video switching not supported on R30, R31 */
1440 video_supported = TPACPI_VIDEO_NONE;
1441 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1442 /* 570 */
1443 video_supported = TPACPI_VIDEO_570;
1444 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1445 /* 600e/x, 770e, 770x */
1446 video_supported = TPACPI_VIDEO_770;
1447 else
1448 /* all others */
1449 video_supported = TPACPI_VIDEO_NEW;
1451 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1452 str_supported(video_supported != TPACPI_VIDEO_NONE),
1453 video_supported);
1455 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1458 static void video_exit(void)
1460 dbg_printk(TPACPI_DBG_EXIT,
1461 "restoring original video autoswitch mode\n");
1462 if (video_autosw_set(video_orig_autosw))
1463 printk(IBM_ERR "error while trying to restore original "
1464 "video autoswitch mode\n");
1467 static int video_outputsw_get(void)
1469 int status = 0;
1470 int i;
1472 switch (video_supported) {
1473 case TPACPI_VIDEO_570:
1474 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1475 TP_ACPI_VIDEO_570_PHSCMD))
1476 return -EIO;
1477 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1478 break;
1479 case TPACPI_VIDEO_770:
1480 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1481 return -EIO;
1482 if (i)
1483 status |= TP_ACPI_VIDEO_S_LCD;
1484 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1485 return -EIO;
1486 if (i)
1487 status |= TP_ACPI_VIDEO_S_CRT;
1488 break;
1489 case TPACPI_VIDEO_NEW:
1490 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1491 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1492 return -EIO;
1493 if (i)
1494 status |= TP_ACPI_VIDEO_S_CRT;
1496 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1497 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1498 return -EIO;
1499 if (i)
1500 status |= TP_ACPI_VIDEO_S_LCD;
1501 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1502 return -EIO;
1503 if (i)
1504 status |= TP_ACPI_VIDEO_S_DVI;
1505 break;
1506 default:
1507 return -ENOSYS;
1510 return status;
1513 static int video_outputsw_set(int status)
1515 int autosw;
1516 int res = 0;
1518 switch (video_supported) {
1519 case TPACPI_VIDEO_570:
1520 res = acpi_evalf(NULL, NULL,
1521 "\\_SB.PHS2", "vdd",
1522 TP_ACPI_VIDEO_570_PHS2CMD,
1523 status | TP_ACPI_VIDEO_570_PHS2SET);
1524 break;
1525 case TPACPI_VIDEO_770:
1526 autosw = video_autosw_get();
1527 if (autosw < 0)
1528 return autosw;
1530 res = video_autosw_set(1);
1531 if (res)
1532 return res;
1533 res = acpi_evalf(vid_handle, NULL,
1534 "ASWT", "vdd", status * 0x100, 0);
1535 if (!autosw && video_autosw_set(autosw)) {
1536 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1537 return -EIO;
1539 break;
1540 case TPACPI_VIDEO_NEW:
1541 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1542 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1543 break;
1544 default:
1545 return -ENOSYS;
1548 return (res)? 0 : -EIO;
1551 static int video_autosw_get(void)
1553 int autosw = 0;
1555 switch (video_supported) {
1556 case TPACPI_VIDEO_570:
1557 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1558 return -EIO;
1559 break;
1560 case TPACPI_VIDEO_770:
1561 case TPACPI_VIDEO_NEW:
1562 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1563 return -EIO;
1564 break;
1565 default:
1566 return -ENOSYS;
1569 return autosw & 1;
1572 static int video_autosw_set(int enable)
1574 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1575 return -EIO;
1576 return 0;
1579 static int video_outputsw_cycle(void)
1581 int autosw = video_autosw_get();
1582 int res;
1584 if (autosw < 0)
1585 return autosw;
1587 switch (video_supported) {
1588 case TPACPI_VIDEO_570:
1589 res = video_autosw_set(1);
1590 if (res)
1591 return res;
1592 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1593 break;
1594 case TPACPI_VIDEO_770:
1595 case TPACPI_VIDEO_NEW:
1596 res = video_autosw_set(1);
1597 if (res)
1598 return res;
1599 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1600 break;
1601 default:
1602 return -ENOSYS;
1604 if (!autosw && video_autosw_set(autosw)) {
1605 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1606 return -EIO;
1609 return (res)? 0 : -EIO;
1612 static int video_expand_toggle(void)
1614 switch (video_supported) {
1615 case TPACPI_VIDEO_570:
1616 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1617 0 : -EIO;
1618 case TPACPI_VIDEO_770:
1619 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1620 0 : -EIO;
1621 case TPACPI_VIDEO_NEW:
1622 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1623 0 : -EIO;
1624 default:
1625 return -ENOSYS;
1627 /* not reached */
1630 static int video_read(char *p)
1632 int status, autosw;
1633 int len = 0;
1635 if (video_supported == TPACPI_VIDEO_NONE) {
1636 len += sprintf(p + len, "status:\t\tnot supported\n");
1637 return len;
1640 status = video_outputsw_get();
1641 if (status < 0)
1642 return status;
1644 autosw = video_autosw_get();
1645 if (autosw < 0)
1646 return autosw;
1648 len += sprintf(p + len, "status:\t\tsupported\n");
1649 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1650 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1651 if (video_supported == TPACPI_VIDEO_NEW)
1652 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1653 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1654 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1655 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1656 if (video_supported == TPACPI_VIDEO_NEW)
1657 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1658 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1659 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1661 return len;
1664 static int video_write(char *buf)
1666 char *cmd;
1667 int enable, disable, status;
1668 int res;
1670 if (video_supported == TPACPI_VIDEO_NONE)
1671 return -ENODEV;
1673 enable = 0;
1674 disable = 0;
1676 while ((cmd = next_cmd(&buf))) {
1677 if (strlencmp(cmd, "lcd_enable") == 0) {
1678 enable |= TP_ACPI_VIDEO_S_LCD;
1679 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1680 disable |= TP_ACPI_VIDEO_S_LCD;
1681 } else if (strlencmp(cmd, "crt_enable") == 0) {
1682 enable |= TP_ACPI_VIDEO_S_CRT;
1683 } else if (strlencmp(cmd, "crt_disable") == 0) {
1684 disable |= TP_ACPI_VIDEO_S_CRT;
1685 } else if (video_supported == TPACPI_VIDEO_NEW &&
1686 strlencmp(cmd, "dvi_enable") == 0) {
1687 enable |= TP_ACPI_VIDEO_S_DVI;
1688 } else if (video_supported == TPACPI_VIDEO_NEW &&
1689 strlencmp(cmd, "dvi_disable") == 0) {
1690 disable |= TP_ACPI_VIDEO_S_DVI;
1691 } else if (strlencmp(cmd, "auto_enable") == 0) {
1692 res = video_autosw_set(1);
1693 if (res)
1694 return res;
1695 } else if (strlencmp(cmd, "auto_disable") == 0) {
1696 res = video_autosw_set(0);
1697 if (res)
1698 return res;
1699 } else if (strlencmp(cmd, "video_switch") == 0) {
1700 res = video_outputsw_cycle();
1701 if (res)
1702 return res;
1703 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1704 res = video_expand_toggle();
1705 if (res)
1706 return res;
1707 } else
1708 return -EINVAL;
1711 if (enable || disable) {
1712 status = video_outputsw_get();
1713 if (status < 0)
1714 return status;
1715 res = video_outputsw_set((status & ~disable) | enable);
1716 if (res)
1717 return res;
1720 return 0;
1723 static struct ibm_struct video_driver_data = {
1724 .name = "video",
1725 .read = video_read,
1726 .write = video_write,
1727 .exit = video_exit,
1730 /*************************************************************************
1731 * Light (thinklight) subdriver
1734 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1735 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1737 static int __init light_init(struct ibm_init_struct *iibm)
1739 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1741 IBM_ACPIHANDLE_INIT(ledb);
1742 IBM_ACPIHANDLE_INIT(lght);
1743 IBM_ACPIHANDLE_INIT(cmos);
1745 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1746 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1748 if (tp_features.light)
1749 /* light status not supported on
1750 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1751 tp_features.light_status =
1752 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1754 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1755 str_supported(tp_features.light));
1757 return (tp_features.light)? 0 : 1;
1760 static int light_read(char *p)
1762 int len = 0;
1763 int status = 0;
1765 if (!tp_features.light) {
1766 len += sprintf(p + len, "status:\t\tnot supported\n");
1767 } else if (!tp_features.light_status) {
1768 len += sprintf(p + len, "status:\t\tunknown\n");
1769 len += sprintf(p + len, "commands:\ton, off\n");
1770 } else {
1771 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1772 return -EIO;
1773 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1774 len += sprintf(p + len, "commands:\ton, off\n");
1777 return len;
1780 static int light_write(char *buf)
1782 int cmos_cmd, lght_cmd;
1783 char *cmd;
1784 int success;
1786 if (!tp_features.light)
1787 return -ENODEV;
1789 while ((cmd = next_cmd(&buf))) {
1790 if (strlencmp(cmd, "on") == 0) {
1791 cmos_cmd = 0x0c;
1792 lght_cmd = 1;
1793 } else if (strlencmp(cmd, "off") == 0) {
1794 cmos_cmd = 0x0d;
1795 lght_cmd = 0;
1796 } else
1797 return -EINVAL;
1799 success = cmos_handle ?
1800 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1801 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1802 if (!success)
1803 return -EIO;
1806 return 0;
1809 static struct ibm_struct light_driver_data = {
1810 .name = "light",
1811 .read = light_read,
1812 .write = light_write,
1815 /*************************************************************************
1816 * Dock subdriver
1819 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1821 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1822 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1823 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1824 "\\_SB.PCI.ISA.SLCE", /* 570 */
1825 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1827 /* don't list other alternatives as we install a notify handler on the 570 */
1828 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1830 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1832 .notify = dock_notify,
1833 .handle = &dock_handle,
1834 .type = ACPI_SYSTEM_NOTIFY,
1837 .hid = IBM_PCI_HID,
1838 .notify = dock_notify,
1839 .handle = &pci_handle,
1840 .type = ACPI_SYSTEM_NOTIFY,
1844 static struct ibm_struct dock_driver_data[2] = {
1846 .name = "dock",
1847 .read = dock_read,
1848 .write = dock_write,
1849 .acpi = &ibm_dock_acpidriver[0],
1852 .name = "dock",
1853 .acpi = &ibm_dock_acpidriver[1],
1857 #define dock_docked() (_sta(dock_handle) & 1)
1859 static int __init dock_init(struct ibm_init_struct *iibm)
1861 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1863 IBM_ACPIHANDLE_INIT(dock);
1865 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1866 str_supported(dock_handle != NULL));
1868 return (dock_handle)? 0 : 1;
1871 static int __init dock_init2(struct ibm_init_struct *iibm)
1873 int dock2_needed;
1875 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1877 if (dock_driver_data[0].flags.acpi_driver_registered &&
1878 dock_driver_data[0].flags.acpi_notify_installed) {
1879 IBM_ACPIHANDLE_INIT(pci);
1880 dock2_needed = (pci_handle != NULL);
1881 vdbg_printk(TPACPI_DBG_INIT,
1882 "dock PCI handler for the TP 570 is %s\n",
1883 str_supported(dock2_needed));
1884 } else {
1885 vdbg_printk(TPACPI_DBG_INIT,
1886 "dock subdriver part 2 not required\n");
1887 dock2_needed = 0;
1890 return (dock2_needed)? 0 : 1;
1893 static void dock_notify(struct ibm_struct *ibm, u32 event)
1895 int docked = dock_docked();
1896 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1898 if (event == 1 && !pci) /* 570 */
1899 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1900 else if (event == 1 && pci) /* 570 */
1901 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1902 else if (event == 3 && docked)
1903 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1904 else if (event == 3 && !docked)
1905 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1906 else if (event == 0 && docked)
1907 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1908 else {
1909 printk(IBM_ERR "unknown dock event %d, status %d\n",
1910 event, _sta(dock_handle));
1911 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1915 static int dock_read(char *p)
1917 int len = 0;
1918 int docked = dock_docked();
1920 if (!dock_handle)
1921 len += sprintf(p + len, "status:\t\tnot supported\n");
1922 else if (!docked)
1923 len += sprintf(p + len, "status:\t\tundocked\n");
1924 else {
1925 len += sprintf(p + len, "status:\t\tdocked\n");
1926 len += sprintf(p + len, "commands:\tdock, undock\n");
1929 return len;
1932 static int dock_write(char *buf)
1934 char *cmd;
1936 if (!dock_docked())
1937 return -ENODEV;
1939 while ((cmd = next_cmd(&buf))) {
1940 if (strlencmp(cmd, "undock") == 0) {
1941 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1942 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1943 return -EIO;
1944 } else if (strlencmp(cmd, "dock") == 0) {
1945 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1946 return -EIO;
1947 } else
1948 return -EINVAL;
1951 return 0;
1954 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1956 /*************************************************************************
1957 * Bay subdriver
1960 #ifdef CONFIG_THINKPAD_ACPI_BAY
1961 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1962 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1963 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1964 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1965 ); /* A21e, R30, R31 */
1966 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1967 "_EJ0", /* all others */
1968 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1969 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1970 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1971 ); /* all others */
1972 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1973 "_EJ0", /* 770x */
1974 ); /* all others */
1976 static int __init bay_init(struct ibm_init_struct *iibm)
1978 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1980 IBM_ACPIHANDLE_INIT(bay);
1981 if (bay_handle)
1982 IBM_ACPIHANDLE_INIT(bay_ej);
1983 IBM_ACPIHANDLE_INIT(bay2);
1984 if (bay2_handle)
1985 IBM_ACPIHANDLE_INIT(bay2_ej);
1987 tp_features.bay_status = bay_handle &&
1988 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1989 tp_features.bay_status2 = bay2_handle &&
1990 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1992 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1993 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1994 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1995 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1997 vdbg_printk(TPACPI_DBG_INIT,
1998 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1999 str_supported(tp_features.bay_status),
2000 str_supported(tp_features.bay_eject),
2001 str_supported(tp_features.bay_status2),
2002 str_supported(tp_features.bay_eject2));
2004 return (tp_features.bay_status || tp_features.bay_eject ||
2005 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2008 static void bay_notify(struct ibm_struct *ibm, u32 event)
2010 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2013 #define bay_occupied(b) (_sta(b##_handle) & 1)
2015 static int bay_read(char *p)
2017 int len = 0;
2018 int occupied = bay_occupied(bay);
2019 int occupied2 = bay_occupied(bay2);
2020 int eject, eject2;
2022 len += sprintf(p + len, "status:\t\t%s\n",
2023 tp_features.bay_status ?
2024 (occupied ? "occupied" : "unoccupied") :
2025 "not supported");
2026 if (tp_features.bay_status2)
2027 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2028 "occupied" : "unoccupied");
2030 eject = tp_features.bay_eject && occupied;
2031 eject2 = tp_features.bay_eject2 && occupied2;
2033 if (eject && eject2)
2034 len += sprintf(p + len, "commands:\teject, eject2\n");
2035 else if (eject)
2036 len += sprintf(p + len, "commands:\teject\n");
2037 else if (eject2)
2038 len += sprintf(p + len, "commands:\teject2\n");
2040 return len;
2043 static int bay_write(char *buf)
2045 char *cmd;
2047 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2048 return -ENODEV;
2050 while ((cmd = next_cmd(&buf))) {
2051 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2052 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2053 return -EIO;
2054 } else if (tp_features.bay_eject2 &&
2055 strlencmp(cmd, "eject2") == 0) {
2056 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2057 return -EIO;
2058 } else
2059 return -EINVAL;
2062 return 0;
2065 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2066 .notify = bay_notify,
2067 .handle = &bay_handle,
2068 .type = ACPI_SYSTEM_NOTIFY,
2071 static struct ibm_struct bay_driver_data = {
2072 .name = "bay",
2073 .read = bay_read,
2074 .write = bay_write,
2075 .acpi = &ibm_bay_acpidriver,
2078 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2080 /*************************************************************************
2081 * CMOS subdriver
2084 /* sysfs cmos_command -------------------------------------------------- */
2085 static ssize_t cmos_command_store(struct device *dev,
2086 struct device_attribute *attr,
2087 const char *buf, size_t count)
2089 unsigned long cmos_cmd;
2090 int res;
2092 if (parse_strtoul(buf, 21, &cmos_cmd))
2093 return -EINVAL;
2095 res = issue_thinkpad_cmos_command(cmos_cmd);
2096 return (res)? res : count;
2099 static struct device_attribute dev_attr_cmos_command =
2100 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2102 /* --------------------------------------------------------------------- */
2104 static int __init cmos_init(struct ibm_init_struct *iibm)
2106 int res;
2108 vdbg_printk(TPACPI_DBG_INIT,
2109 "initializing cmos commands subdriver\n");
2111 IBM_ACPIHANDLE_INIT(cmos);
2113 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2114 str_supported(cmos_handle != NULL));
2116 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2117 if (res)
2118 return res;
2120 return (cmos_handle)? 0 : 1;
2123 static void cmos_exit(void)
2125 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2128 static int cmos_read(char *p)
2130 int len = 0;
2132 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2133 R30, R31, T20-22, X20-21 */
2134 if (!cmos_handle)
2135 len += sprintf(p + len, "status:\t\tnot supported\n");
2136 else {
2137 len += sprintf(p + len, "status:\t\tsupported\n");
2138 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2141 return len;
2144 static int cmos_write(char *buf)
2146 char *cmd;
2147 int cmos_cmd, res;
2149 while ((cmd = next_cmd(&buf))) {
2150 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2151 cmos_cmd >= 0 && cmos_cmd <= 21) {
2152 /* cmos_cmd set */
2153 } else
2154 return -EINVAL;
2156 res = issue_thinkpad_cmos_command(cmos_cmd);
2157 if (res)
2158 return res;
2161 return 0;
2164 static struct ibm_struct cmos_driver_data = {
2165 .name = "cmos",
2166 .read = cmos_read,
2167 .write = cmos_write,
2168 .exit = cmos_exit,
2171 /*************************************************************************
2172 * LED subdriver
2175 static enum led_access_mode led_supported;
2177 IBM_HANDLE(led, ec, "SLED", /* 570 */
2178 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2179 "LED", /* all others */
2180 ); /* R30, R31 */
2182 static int __init led_init(struct ibm_init_struct *iibm)
2184 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2186 IBM_ACPIHANDLE_INIT(led);
2188 if (!led_handle)
2189 /* led not supported on R30, R31 */
2190 led_supported = TPACPI_LED_NONE;
2191 else if (strlencmp(led_path, "SLED") == 0)
2192 /* 570 */
2193 led_supported = TPACPI_LED_570;
2194 else if (strlencmp(led_path, "SYSL") == 0)
2195 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2196 led_supported = TPACPI_LED_OLD;
2197 else
2198 /* all others */
2199 led_supported = TPACPI_LED_NEW;
2201 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2202 str_supported(led_supported), led_supported);
2204 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2207 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2209 static int led_read(char *p)
2211 int len = 0;
2213 if (!led_supported) {
2214 len += sprintf(p + len, "status:\t\tnot supported\n");
2215 return len;
2217 len += sprintf(p + len, "status:\t\tsupported\n");
2219 if (led_supported == TPACPI_LED_570) {
2220 /* 570 */
2221 int i, status;
2222 for (i = 0; i < 8; i++) {
2223 if (!acpi_evalf(ec_handle,
2224 &status, "GLED", "dd", 1 << i))
2225 return -EIO;
2226 len += sprintf(p + len, "%d:\t\t%s\n",
2227 i, led_status(status));
2231 len += sprintf(p + len, "commands:\t"
2232 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2234 return len;
2237 /* off, on, blink */
2238 static const int led_sled_arg1[] = { 0, 1, 3 };
2239 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2240 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2241 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2243 static int led_write(char *buf)
2245 char *cmd;
2246 int led, ind, ret;
2248 if (!led_supported)
2249 return -ENODEV;
2251 while ((cmd = next_cmd(&buf))) {
2252 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2253 return -EINVAL;
2255 if (strstr(cmd, "off")) {
2256 ind = 0;
2257 } else if (strstr(cmd, "on")) {
2258 ind = 1;
2259 } else if (strstr(cmd, "blink")) {
2260 ind = 2;
2261 } else
2262 return -EINVAL;
2264 if (led_supported == TPACPI_LED_570) {
2265 /* 570 */
2266 led = 1 << led;
2267 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2268 led, led_sled_arg1[ind]))
2269 return -EIO;
2270 } else if (led_supported == TPACPI_LED_OLD) {
2271 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2272 led = 1 << led;
2273 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2274 if (ret >= 0)
2275 ret =
2276 ec_write(TPACPI_LED_EC_HLBL,
2277 led * led_exp_hlbl[ind]);
2278 if (ret >= 0)
2279 ret =
2280 ec_write(TPACPI_LED_EC_HLCL,
2281 led * led_exp_hlcl[ind]);
2282 if (ret < 0)
2283 return ret;
2284 } else {
2285 /* all others */
2286 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2287 led, led_led_arg1[ind]))
2288 return -EIO;
2292 return 0;
2295 static struct ibm_struct led_driver_data = {
2296 .name = "led",
2297 .read = led_read,
2298 .write = led_write,
2301 /*************************************************************************
2302 * Beep subdriver
2305 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2307 static int __init beep_init(struct ibm_init_struct *iibm)
2309 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2311 IBM_ACPIHANDLE_INIT(beep);
2313 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2314 str_supported(beep_handle != NULL));
2316 return (beep_handle)? 0 : 1;
2319 static int beep_read(char *p)
2321 int len = 0;
2323 if (!beep_handle)
2324 len += sprintf(p + len, "status:\t\tnot supported\n");
2325 else {
2326 len += sprintf(p + len, "status:\t\tsupported\n");
2327 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2330 return len;
2333 static int beep_write(char *buf)
2335 char *cmd;
2336 int beep_cmd;
2338 if (!beep_handle)
2339 return -ENODEV;
2341 while ((cmd = next_cmd(&buf))) {
2342 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2343 beep_cmd >= 0 && beep_cmd <= 17) {
2344 /* beep_cmd set */
2345 } else
2346 return -EINVAL;
2347 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2348 return -EIO;
2351 return 0;
2354 static struct ibm_struct beep_driver_data = {
2355 .name = "beep",
2356 .read = beep_read,
2357 .write = beep_write,
2360 /*************************************************************************
2361 * Thermal subdriver
2364 static enum thermal_access_mode thermal_read_mode;
2366 /* sysfs temp##_input -------------------------------------------------- */
2368 static ssize_t thermal_temp_input_show(struct device *dev,
2369 struct device_attribute *attr,
2370 char *buf)
2372 struct sensor_device_attribute *sensor_attr =
2373 to_sensor_dev_attr(attr);
2374 int idx = sensor_attr->index;
2375 s32 value;
2376 int res;
2378 res = thermal_get_sensor(idx, &value);
2379 if (res)
2380 return res;
2381 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2382 return -ENXIO;
2384 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2387 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2388 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2390 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2391 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2392 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2393 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2394 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2395 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2396 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2397 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2398 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2399 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2400 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2401 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2402 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2403 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2404 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2405 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2406 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2409 #define THERMAL_ATTRS(X) \
2410 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2412 static struct attribute *thermal_temp_input_attr[] = {
2413 THERMAL_ATTRS(8),
2414 THERMAL_ATTRS(9),
2415 THERMAL_ATTRS(10),
2416 THERMAL_ATTRS(11),
2417 THERMAL_ATTRS(12),
2418 THERMAL_ATTRS(13),
2419 THERMAL_ATTRS(14),
2420 THERMAL_ATTRS(15),
2421 THERMAL_ATTRS(0),
2422 THERMAL_ATTRS(1),
2423 THERMAL_ATTRS(2),
2424 THERMAL_ATTRS(3),
2425 THERMAL_ATTRS(4),
2426 THERMAL_ATTRS(5),
2427 THERMAL_ATTRS(6),
2428 THERMAL_ATTRS(7),
2429 NULL
2432 static const struct attribute_group thermal_temp_input16_group = {
2433 .attrs = thermal_temp_input_attr
2436 static const struct attribute_group thermal_temp_input8_group = {
2437 .attrs = &thermal_temp_input_attr[8]
2440 #undef THERMAL_SENSOR_ATTR_TEMP
2441 #undef THERMAL_ATTRS
2443 /* --------------------------------------------------------------------- */
2445 static int __init thermal_init(struct ibm_init_struct *iibm)
2447 u8 t, ta1, ta2;
2448 int i;
2449 int acpi_tmp7;
2450 int res;
2452 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2454 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2456 if (ibm_thinkpad_ec_found && experimental) {
2458 * Direct EC access mode: sensors at registers
2459 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2460 * non-implemented, thermal sensors return 0x80 when
2461 * not available
2464 ta1 = ta2 = 0;
2465 for (i = 0; i < 8; i++) {
2466 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2467 ta1 |= t;
2468 } else {
2469 ta1 = 0;
2470 break;
2472 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2473 ta2 |= t;
2474 } else {
2475 ta1 = 0;
2476 break;
2479 if (ta1 == 0) {
2480 /* This is sheer paranoia, but we handle it anyway */
2481 if (acpi_tmp7) {
2482 printk(IBM_ERR
2483 "ThinkPad ACPI EC access misbehaving, "
2484 "falling back to ACPI TMPx access mode\n");
2485 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2486 } else {
2487 printk(IBM_ERR
2488 "ThinkPad ACPI EC access misbehaving, "
2489 "disabling thermal sensors access\n");
2490 thermal_read_mode = TPACPI_THERMAL_NONE;
2492 } else {
2493 thermal_read_mode =
2494 (ta2 != 0) ?
2495 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2497 } else if (acpi_tmp7) {
2498 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2499 /* 600e/x, 770e, 770x */
2500 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2501 } else {
2502 /* Standard ACPI TMPx access, max 8 sensors */
2503 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2505 } else {
2506 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2507 thermal_read_mode = TPACPI_THERMAL_NONE;
2510 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2511 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2512 thermal_read_mode);
2514 switch(thermal_read_mode) {
2515 case TPACPI_THERMAL_TPEC_16:
2516 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2517 &thermal_temp_input16_group);
2518 if (res)
2519 return res;
2520 break;
2521 case TPACPI_THERMAL_TPEC_8:
2522 case TPACPI_THERMAL_ACPI_TMP07:
2523 case TPACPI_THERMAL_ACPI_UPDT:
2524 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2525 &thermal_temp_input8_group);
2526 if (res)
2527 return res;
2528 break;
2529 case TPACPI_THERMAL_NONE:
2530 default:
2531 return 1;
2534 return 0;
2537 static void thermal_exit(void)
2539 switch(thermal_read_mode) {
2540 case TPACPI_THERMAL_TPEC_16:
2541 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2542 &thermal_temp_input16_group);
2543 break;
2544 case TPACPI_THERMAL_TPEC_8:
2545 case TPACPI_THERMAL_ACPI_TMP07:
2546 case TPACPI_THERMAL_ACPI_UPDT:
2547 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2548 &thermal_temp_input16_group);
2549 break;
2550 case TPACPI_THERMAL_NONE:
2551 default:
2552 break;
2556 /* idx is zero-based */
2557 static int thermal_get_sensor(int idx, s32 *value)
2559 int t;
2560 s8 tmp;
2561 char tmpi[5];
2563 t = TP_EC_THERMAL_TMP0;
2565 switch (thermal_read_mode) {
2566 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2567 case TPACPI_THERMAL_TPEC_16:
2568 if (idx >= 8 && idx <= 15) {
2569 t = TP_EC_THERMAL_TMP8;
2570 idx -= 8;
2572 /* fallthrough */
2573 #endif
2574 case TPACPI_THERMAL_TPEC_8:
2575 if (idx <= 7) {
2576 if (!acpi_ec_read(t + idx, &tmp))
2577 return -EIO;
2578 *value = tmp * 1000;
2579 return 0;
2581 break;
2583 case TPACPI_THERMAL_ACPI_UPDT:
2584 if (idx <= 7) {
2585 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2586 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2587 return -EIO;
2588 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2589 return -EIO;
2590 *value = (t - 2732) * 100;
2591 return 0;
2593 break;
2595 case TPACPI_THERMAL_ACPI_TMP07:
2596 if (idx <= 7) {
2597 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2598 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2599 return -EIO;
2600 *value = t * 1000;
2601 return 0;
2603 break;
2605 case TPACPI_THERMAL_NONE:
2606 default:
2607 return -ENOSYS;
2610 return -EINVAL;
2613 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2615 int res, i;
2616 int n;
2618 n = 8;
2619 i = 0;
2621 if (!s)
2622 return -EINVAL;
2624 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2625 n = 16;
2627 for(i = 0 ; i < n; i++) {
2628 res = thermal_get_sensor(i, &s->temp[i]);
2629 if (res)
2630 return res;
2633 return n;
2636 static int thermal_read(char *p)
2638 int len = 0;
2639 int n, i;
2640 struct ibm_thermal_sensors_struct t;
2642 n = thermal_get_sensors(&t);
2643 if (unlikely(n < 0))
2644 return n;
2646 len += sprintf(p + len, "temperatures:\t");
2648 if (n > 0) {
2649 for (i = 0; i < (n - 1); i++)
2650 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2651 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2652 } else
2653 len += sprintf(p + len, "not supported\n");
2655 return len;
2658 static struct ibm_struct thermal_driver_data = {
2659 .name = "thermal",
2660 .read = thermal_read,
2661 .exit = thermal_exit,
2664 /*************************************************************************
2665 * EC Dump subdriver
2668 static u8 ecdump_regs[256];
2670 static int ecdump_read(char *p)
2672 int len = 0;
2673 int i, j;
2674 u8 v;
2676 len += sprintf(p + len, "EC "
2677 " +00 +01 +02 +03 +04 +05 +06 +07"
2678 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2679 for (i = 0; i < 256; i += 16) {
2680 len += sprintf(p + len, "EC 0x%02x:", i);
2681 for (j = 0; j < 16; j++) {
2682 if (!acpi_ec_read(i + j, &v))
2683 break;
2684 if (v != ecdump_regs[i + j])
2685 len += sprintf(p + len, " *%02x", v);
2686 else
2687 len += sprintf(p + len, " %02x", v);
2688 ecdump_regs[i + j] = v;
2690 len += sprintf(p + len, "\n");
2691 if (j != 16)
2692 break;
2695 /* These are way too dangerous to advertise openly... */
2696 #if 0
2697 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2698 " (<offset> is 00-ff, <value> is 00-ff)\n");
2699 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2700 " (<offset> is 00-ff, <value> is 0-255)\n");
2701 #endif
2702 return len;
2705 static int ecdump_write(char *buf)
2707 char *cmd;
2708 int i, v;
2710 while ((cmd = next_cmd(&buf))) {
2711 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2712 /* i and v set */
2713 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2714 /* i and v set */
2715 } else
2716 return -EINVAL;
2717 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2718 if (!acpi_ec_write(i, v))
2719 return -EIO;
2720 } else
2721 return -EINVAL;
2724 return 0;
2727 static struct ibm_struct ecdump_driver_data = {
2728 .name = "ecdump",
2729 .read = ecdump_read,
2730 .write = ecdump_write,
2731 .flags.experimental = 1,
2734 /*************************************************************************
2735 * Backlight/brightness subdriver
2738 static struct backlight_device *ibm_backlight_device;
2740 static struct backlight_properties ibm_backlight_data = {
2741 .owner = THIS_MODULE,
2742 .get_brightness = brightness_get,
2743 .update_status = brightness_update_status,
2744 .max_brightness = 7,
2747 static int __init brightness_init(struct ibm_init_struct *iibm)
2749 int b;
2751 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2753 b = brightness_get(NULL);
2754 if (b < 0)
2755 return b;
2757 ibm_backlight_device = backlight_device_register(
2758 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2759 &ibm_backlight_data);
2760 if (IS_ERR(ibm_backlight_device)) {
2761 printk(IBM_ERR "Could not register backlight device\n");
2762 return PTR_ERR(ibm_backlight_device);
2764 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2766 ibm_backlight_device->props->brightness = b;
2767 brightness_update_status(ibm_backlight_device);
2769 return 0;
2772 static void brightness_exit(void)
2774 if (ibm_backlight_device) {
2775 vdbg_printk(TPACPI_DBG_EXIT,
2776 "calling backlight_device_unregister()\n");
2777 backlight_device_unregister(ibm_backlight_device);
2778 ibm_backlight_device = NULL;
2782 static int brightness_update_status(struct backlight_device *bd)
2784 return brightness_set(
2785 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
2786 bd->props->power == FB_BLANK_UNBLANK) ?
2787 bd->props->brightness : 0);
2790 static int brightness_get(struct backlight_device *bd)
2792 u8 level;
2793 if (!acpi_ec_read(brightness_offset, &level))
2794 return -EIO;
2796 level &= 0x7;
2798 return level;
2801 static int brightness_set(int value)
2803 int cmos_cmd, inc, i;
2804 int current_value = brightness_get(NULL);
2806 value &= 7;
2808 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2809 inc = value > current_value ? 1 : -1;
2810 for (i = current_value; i != value; i += inc) {
2811 if (issue_thinkpad_cmos_command(cmos_cmd))
2812 return -EIO;
2813 if (!acpi_ec_write(brightness_offset, i + inc))
2814 return -EIO;
2817 return 0;
2820 static int brightness_read(char *p)
2822 int len = 0;
2823 int level;
2825 if ((level = brightness_get(NULL)) < 0) {
2826 len += sprintf(p + len, "level:\t\tunreadable\n");
2827 } else {
2828 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2829 len += sprintf(p + len, "commands:\tup, down\n");
2830 len += sprintf(p + len, "commands:\tlevel <level>"
2831 " (<level> is 0-7)\n");
2834 return len;
2837 static int brightness_write(char *buf)
2839 int level;
2840 int new_level;
2841 char *cmd;
2843 while ((cmd = next_cmd(&buf))) {
2844 if ((level = brightness_get(NULL)) < 0)
2845 return level;
2846 level &= 7;
2848 if (strlencmp(cmd, "up") == 0) {
2849 new_level = level == 7 ? 7 : level + 1;
2850 } else if (strlencmp(cmd, "down") == 0) {
2851 new_level = level == 0 ? 0 : level - 1;
2852 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2853 new_level >= 0 && new_level <= 7) {
2854 /* new_level set */
2855 } else
2856 return -EINVAL;
2858 brightness_set(new_level);
2861 return 0;
2864 static struct ibm_struct brightness_driver_data = {
2865 .name = "brightness",
2866 .read = brightness_read,
2867 .write = brightness_write,
2868 .exit = brightness_exit,
2871 /*************************************************************************
2872 * Volume subdriver
2875 static int volume_read(char *p)
2877 int len = 0;
2878 u8 level;
2880 if (!acpi_ec_read(volume_offset, &level)) {
2881 len += sprintf(p + len, "level:\t\tunreadable\n");
2882 } else {
2883 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2884 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2885 len += sprintf(p + len, "commands:\tup, down, mute\n");
2886 len += sprintf(p + len, "commands:\tlevel <level>"
2887 " (<level> is 0-15)\n");
2890 return len;
2893 static int volume_write(char *buf)
2895 int cmos_cmd, inc, i;
2896 u8 level, mute;
2897 int new_level, new_mute;
2898 char *cmd;
2900 while ((cmd = next_cmd(&buf))) {
2901 if (!acpi_ec_read(volume_offset, &level))
2902 return -EIO;
2903 new_mute = mute = level & 0x40;
2904 new_level = level = level & 0xf;
2906 if (strlencmp(cmd, "up") == 0) {
2907 if (mute)
2908 new_mute = 0;
2909 else
2910 new_level = level == 15 ? 15 : level + 1;
2911 } else if (strlencmp(cmd, "down") == 0) {
2912 if (mute)
2913 new_mute = 0;
2914 else
2915 new_level = level == 0 ? 0 : level - 1;
2916 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2917 new_level >= 0 && new_level <= 15) {
2918 /* new_level set */
2919 } else if (strlencmp(cmd, "mute") == 0) {
2920 new_mute = 0x40;
2921 } else
2922 return -EINVAL;
2924 if (new_level != level) { /* mute doesn't change */
2925 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2926 inc = new_level > level ? 1 : -1;
2928 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2929 !acpi_ec_write(volume_offset, level)))
2930 return -EIO;
2932 for (i = level; i != new_level; i += inc)
2933 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2934 !acpi_ec_write(volume_offset, i + inc))
2935 return -EIO;
2937 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2938 !acpi_ec_write(volume_offset,
2939 new_level + mute)))
2940 return -EIO;
2943 if (new_mute != mute) { /* level doesn't change */
2944 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2946 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2947 !acpi_ec_write(volume_offset, level + new_mute))
2948 return -EIO;
2952 return 0;
2955 static struct ibm_struct volume_driver_data = {
2956 .name = "volume",
2957 .read = volume_read,
2958 .write = volume_write,
2961 /*************************************************************************
2962 * Fan subdriver
2966 * FAN ACCESS MODES
2968 * TPACPI_FAN_RD_ACPI_GFAN:
2969 * ACPI GFAN method: returns fan level
2971 * see TPACPI_FAN_WR_ACPI_SFAN
2972 * EC 0x2f (HFSP) not available if GFAN exists
2974 * TPACPI_FAN_WR_ACPI_SFAN:
2975 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2977 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2978 * it for writing.
2980 * TPACPI_FAN_WR_TPEC:
2981 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2982 * Supported on almost all ThinkPads
2984 * Fan speed changes of any sort (including those caused by the
2985 * disengaged mode) are usually done slowly by the firmware as the
2986 * maximum ammount of fan duty cycle change per second seems to be
2987 * limited.
2989 * Reading is not available if GFAN exists.
2990 * Writing is not available if SFAN exists.
2992 * Bits
2993 * 7 automatic mode engaged;
2994 * (default operation mode of the ThinkPad)
2995 * fan level is ignored in this mode.
2996 * 6 full speed mode (takes precedence over bit 7);
2997 * not available on all thinkpads. May disable
2998 * the tachometer while the fan controller ramps up
2999 * the speed (which can take up to a few *minutes*).
3000 * Speeds up fan to 100% duty-cycle, which is far above
3001 * the standard RPM levels. It is not impossible that
3002 * it could cause hardware damage.
3003 * 5-3 unused in some models. Extra bits for fan level
3004 * in others, but still useless as all values above
3005 * 7 map to the same speed as level 7 in these models.
3006 * 2-0 fan level (0..7 usually)
3007 * 0x00 = stop
3008 * 0x07 = max (set when temperatures critical)
3009 * Some ThinkPads may have other levels, see
3010 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3012 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3013 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3014 * does so, its initial value is meaningless (0x07).
3016 * For firmware bugs, refer to:
3017 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3019 * ----
3021 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3022 * Main fan tachometer reading (in RPM)
3024 * This register is present on all ThinkPads with a new-style EC, and
3025 * it is known not to be present on the A21m/e, and T22, as there is
3026 * something else in offset 0x84 according to the ACPI DSDT. Other
3027 * ThinkPads from this same time period (and earlier) probably lack the
3028 * tachometer as well.
3030 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3031 * was never fixed by IBM to report the EC firmware version string
3032 * probably support the tachometer (like the early X models), so
3033 * detecting it is quite hard. We need more data to know for sure.
3035 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3036 * might result.
3038 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3039 * mode.
3041 * For firmware bugs, refer to:
3042 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3044 * TPACPI_FAN_WR_ACPI_FANS:
3045 * ThinkPad X31, X40, X41. Not available in the X60.
3047 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3048 * high speed. ACPI DSDT seems to map these three speeds to levels
3049 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3050 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3052 * The speeds are stored on handles
3053 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3055 * There are three default speed sets, acessible as handles:
3056 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3058 * ACPI DSDT switches which set is in use depending on various
3059 * factors.
3061 * TPACPI_FAN_WR_TPEC is also available and should be used to
3062 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3063 * but the ACPI tables just mention level 7.
3066 static enum fan_status_access_mode fan_status_access_mode;
3067 static enum fan_control_access_mode fan_control_access_mode;
3068 static enum fan_control_commands fan_control_commands;
3070 static u8 fan_control_initial_status;
3071 static u8 fan_control_desired_level;
3073 static void fan_watchdog_fire(struct work_struct *ignored);
3074 static int fan_watchdog_maxinterval;
3075 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3077 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3078 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3079 "\\FSPD", /* 600e/x, 770e, 770x */
3080 ); /* all others */
3081 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3082 "JFNS", /* 770x-JL */
3083 ); /* all others */
3086 * SYSFS fan layout: hwmon compatible (device)
3088 * pwm*_enable:
3089 * 0: "disengaged" mode
3090 * 1: manual mode
3091 * 2: native EC "auto" mode (recommended, hardware default)
3093 * pwm*: set speed in manual mode, ignored otherwise.
3094 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3095 * interpolation.
3097 * fan*_input: tachometer reading, RPM
3100 * SYSFS fan layout: extensions
3102 * fan_watchdog (driver):
3103 * fan watchdog interval in seconds, 0 disables (default), max 120
3106 /* sysfs fan pwm1_enable ----------------------------------------------- */
3107 static ssize_t fan_pwm1_enable_show(struct device *dev,
3108 struct device_attribute *attr,
3109 char *buf)
3111 int res, mode;
3112 u8 status;
3114 res = fan_get_status_safe(&status);
3115 if (res)
3116 return res;
3118 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3119 if (status != fan_control_initial_status) {
3120 tp_features.fan_ctrl_status_undef = 0;
3121 } else {
3122 /* Return most likely status. In fact, it
3123 * might be the only possible status */
3124 status = TP_EC_FAN_AUTO;
3128 if (status & TP_EC_FAN_FULLSPEED) {
3129 mode = 0;
3130 } else if (status & TP_EC_FAN_AUTO) {
3131 mode = 2;
3132 } else
3133 mode = 1;
3135 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3138 static ssize_t fan_pwm1_enable_store(struct device *dev,
3139 struct device_attribute *attr,
3140 const char *buf, size_t count)
3142 unsigned long t;
3143 int res, level;
3145 if (parse_strtoul(buf, 2, &t))
3146 return -EINVAL;
3148 switch (t) {
3149 case 0:
3150 level = TP_EC_FAN_FULLSPEED;
3151 break;
3152 case 1:
3153 level = TPACPI_FAN_LAST_LEVEL;
3154 break;
3155 case 2:
3156 level = TP_EC_FAN_AUTO;
3157 break;
3158 case 3:
3159 /* reserved for software-controlled auto mode */
3160 return -ENOSYS;
3161 default:
3162 return -EINVAL;
3165 res = fan_set_level_safe(level);
3166 if (res == -ENXIO)
3167 return -EINVAL;
3168 else if (res < 0)
3169 return res;
3171 fan_watchdog_reset();
3173 return count;
3176 static struct device_attribute dev_attr_fan_pwm1_enable =
3177 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3178 fan_pwm1_enable_show, fan_pwm1_enable_store);
3180 /* sysfs fan pwm1 ------------------------------------------------------ */
3181 static ssize_t fan_pwm1_show(struct device *dev,
3182 struct device_attribute *attr,
3183 char *buf)
3185 int res;
3186 u8 status;
3188 res = fan_get_status_safe(&status);
3189 if (res)
3190 return res;
3192 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3193 if (status != fan_control_initial_status) {
3194 tp_features.fan_ctrl_status_undef = 0;
3195 } else {
3196 status = TP_EC_FAN_AUTO;
3200 if ((status &
3201 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3202 status = fan_control_desired_level;
3204 if (status > 7)
3205 status = 7;
3207 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3210 static ssize_t fan_pwm1_store(struct device *dev,
3211 struct device_attribute *attr,
3212 const char *buf, size_t count)
3214 unsigned long s;
3215 int rc;
3216 u8 status, newlevel;
3218 if (parse_strtoul(buf, 255, &s))
3219 return -EINVAL;
3221 /* scale down from 0-255 to 0-7 */
3222 newlevel = (s >> 5) & 0x07;
3224 rc = mutex_lock_interruptible(&fan_mutex);
3225 if (rc < 0)
3226 return rc;
3228 rc = fan_get_status(&status);
3229 if (!rc && (status &
3230 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3231 rc = fan_set_level(newlevel);
3232 if (rc == -ENXIO)
3233 rc = -EINVAL;
3234 else if (!rc) {
3235 fan_update_desired_level(newlevel);
3236 fan_watchdog_reset();
3240 mutex_unlock(&fan_mutex);
3241 return (rc)? rc : count;
3244 static struct device_attribute dev_attr_fan_pwm1 =
3245 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3246 fan_pwm1_show, fan_pwm1_store);
3248 /* sysfs fan fan1_input ------------------------------------------------ */
3249 static ssize_t fan_fan1_input_show(struct device *dev,
3250 struct device_attribute *attr,
3251 char *buf)
3253 int res;
3254 unsigned int speed;
3256 res = fan_get_speed(&speed);
3257 if (res < 0)
3258 return res;
3260 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3263 static struct device_attribute dev_attr_fan_fan1_input =
3264 __ATTR(fan1_input, S_IRUGO,
3265 fan_fan1_input_show, NULL);
3267 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3268 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3269 char *buf)
3271 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3274 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3275 const char *buf, size_t count)
3277 unsigned long t;
3279 if (parse_strtoul(buf, 120, &t))
3280 return -EINVAL;
3282 if (!fan_control_allowed)
3283 return -EPERM;
3285 fan_watchdog_maxinterval = t;
3286 fan_watchdog_reset();
3288 return count;
3291 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3292 fan_fan_watchdog_show, fan_fan_watchdog_store);
3294 /* --------------------------------------------------------------------- */
3295 static struct attribute *fan_attributes[] = {
3296 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3297 &dev_attr_fan_fan1_input.attr,
3298 NULL
3301 static const struct attribute_group fan_attr_group = {
3302 .attrs = fan_attributes,
3305 static int __init fan_init(struct ibm_init_struct *iibm)
3307 int rc;
3309 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3311 mutex_init(&fan_mutex);
3312 fan_status_access_mode = TPACPI_FAN_NONE;
3313 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3314 fan_control_commands = 0;
3315 fan_watchdog_maxinterval = 0;
3316 tp_features.fan_ctrl_status_undef = 0;
3317 fan_control_desired_level = 7;
3319 IBM_ACPIHANDLE_INIT(fans);
3320 IBM_ACPIHANDLE_INIT(gfan);
3321 IBM_ACPIHANDLE_INIT(sfan);
3323 if (gfan_handle) {
3324 /* 570, 600e/x, 770e, 770x */
3325 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3326 } else {
3327 /* all other ThinkPads: note that even old-style
3328 * ThinkPad ECs supports the fan control register */
3329 if (likely(acpi_ec_read(fan_status_offset,
3330 &fan_control_initial_status))) {
3331 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3333 /* In some ThinkPads, neither the EC nor the ACPI
3334 * DSDT initialize the fan status, and it ends up
3335 * being set to 0x07 when it *could* be either
3336 * 0x07 or 0x80.
3338 * Enable for TP-1Y (T43), TP-78 (R51e),
3339 * TP-76 (R52), TP-70 (T43, R52), which are known
3340 * to be buggy. */
3341 if (fan_control_initial_status == 0x07 &&
3342 ibm_thinkpad_ec_found &&
3343 ((ibm_thinkpad_ec_found[0] == '1' &&
3344 ibm_thinkpad_ec_found[1] == 'Y') ||
3345 (ibm_thinkpad_ec_found[0] == '7' &&
3346 (ibm_thinkpad_ec_found[1] == '6' ||
3347 ibm_thinkpad_ec_found[1] == '8' ||
3348 ibm_thinkpad_ec_found[1] == '0'))
3349 )) {
3350 printk(IBM_NOTICE
3351 "fan_init: initial fan status is "
3352 "unknown, assuming it is in auto "
3353 "mode\n");
3354 tp_features.fan_ctrl_status_undef = 1;
3356 } else {
3357 printk(IBM_ERR
3358 "ThinkPad ACPI EC access misbehaving, "
3359 "fan status and control unavailable\n");
3360 return 1;
3364 if (sfan_handle) {
3365 /* 570, 770x-JL */
3366 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3367 fan_control_commands |=
3368 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3369 } else {
3370 if (!gfan_handle) {
3371 /* gfan without sfan means no fan control */
3372 /* all other models implement TP EC 0x2f control */
3374 if (fans_handle) {
3375 /* X31, X40, X41 */
3376 fan_control_access_mode =
3377 TPACPI_FAN_WR_ACPI_FANS;
3378 fan_control_commands |=
3379 TPACPI_FAN_CMD_SPEED |
3380 TPACPI_FAN_CMD_LEVEL |
3381 TPACPI_FAN_CMD_ENABLE;
3382 } else {
3383 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3384 fan_control_commands |=
3385 TPACPI_FAN_CMD_LEVEL |
3386 TPACPI_FAN_CMD_ENABLE;
3391 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3392 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3393 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3394 fan_status_access_mode, fan_control_access_mode);
3396 /* fan control master switch */
3397 if (!fan_control_allowed) {
3398 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3399 fan_control_commands = 0;
3400 dbg_printk(TPACPI_DBG_INIT,
3401 "fan control features disabled by parameter\n");
3404 /* update fan_control_desired_level */
3405 if (fan_status_access_mode != TPACPI_FAN_NONE)
3406 fan_get_status_safe(NULL);
3408 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3409 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3410 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3411 &fan_attr_group);
3412 if (!(rc < 0))
3413 rc = driver_create_file(&tpacpi_pdriver.driver,
3414 &driver_attr_fan_watchdog);
3415 if (rc < 0)
3416 return rc;
3417 return 0;
3418 } else
3419 return 1;
3423 * Call with fan_mutex held
3425 static void fan_update_desired_level(u8 status)
3427 if ((status &
3428 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3429 if (status > 7)
3430 fan_control_desired_level = 7;
3431 else
3432 fan_control_desired_level = status;
3436 static int fan_get_status(u8 *status)
3438 u8 s;
3440 /* TODO:
3441 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3443 switch (fan_status_access_mode) {
3444 case TPACPI_FAN_RD_ACPI_GFAN:
3445 /* 570, 600e/x, 770e, 770x */
3447 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3448 return -EIO;
3450 if (likely(status))
3451 *status = s & 0x07;
3453 break;
3455 case TPACPI_FAN_RD_TPEC:
3456 /* all except 570, 600e/x, 770e, 770x */
3457 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3458 return -EIO;
3460 if (likely(status))
3461 *status = s;
3463 break;
3465 default:
3466 return -ENXIO;
3469 return 0;
3472 static int fan_get_status_safe(u8 *status)
3474 int rc;
3475 u8 s;
3477 rc = mutex_lock_interruptible(&fan_mutex);
3478 if (rc < 0)
3479 return rc;
3480 rc = fan_get_status(&s);
3481 if (!rc)
3482 fan_update_desired_level(s);
3483 mutex_unlock(&fan_mutex);
3485 if (status)
3486 *status = s;
3488 return rc;
3491 static void fan_exit(void)
3493 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3495 /* FIXME: can we really do this unconditionally? */
3496 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3497 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3499 cancel_delayed_work(&fan_watchdog_task);
3500 flush_scheduled_work();
3503 static int fan_get_speed(unsigned int *speed)
3505 u8 hi, lo;
3507 switch (fan_status_access_mode) {
3508 case TPACPI_FAN_RD_TPEC:
3509 /* all except 570, 600e/x, 770e, 770x */
3510 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3511 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3512 return -EIO;
3514 if (likely(speed))
3515 *speed = (hi << 8) | lo;
3517 break;
3519 default:
3520 return -ENXIO;
3523 return 0;
3526 static void fan_watchdog_fire(struct work_struct *ignored)
3528 int rc;
3530 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3531 rc = fan_set_enable();
3532 if (rc < 0) {
3533 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3534 "will try again later...\n", -rc);
3535 /* reschedule for later */
3536 fan_watchdog_reset();
3540 static void fan_watchdog_reset(void)
3542 static int fan_watchdog_active;
3544 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3545 return;
3547 if (fan_watchdog_active)
3548 cancel_delayed_work(&fan_watchdog_task);
3550 if (fan_watchdog_maxinterval > 0) {
3551 fan_watchdog_active = 1;
3552 if (!schedule_delayed_work(&fan_watchdog_task,
3553 msecs_to_jiffies(fan_watchdog_maxinterval
3554 * 1000))) {
3555 printk(IBM_ERR "failed to schedule the fan watchdog, "
3556 "watchdog will not trigger\n");
3558 } else
3559 fan_watchdog_active = 0;
3562 static int fan_set_level(int level)
3564 if (!fan_control_allowed)
3565 return -EPERM;
3567 switch (fan_control_access_mode) {
3568 case TPACPI_FAN_WR_ACPI_SFAN:
3569 if (level >= 0 && level <= 7) {
3570 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3571 return -EIO;
3572 } else
3573 return -EINVAL;
3574 break;
3576 case TPACPI_FAN_WR_ACPI_FANS:
3577 case TPACPI_FAN_WR_TPEC:
3578 if ((level != TP_EC_FAN_AUTO) &&
3579 (level != TP_EC_FAN_FULLSPEED) &&
3580 ((level < 0) || (level > 7)))
3581 return -EINVAL;
3583 /* safety net should the EC not support AUTO
3584 * or FULLSPEED mode bits and just ignore them */
3585 if (level & TP_EC_FAN_FULLSPEED)
3586 level |= 7; /* safety min speed 7 */
3587 else if (level & TP_EC_FAN_FULLSPEED)
3588 level |= 4; /* safety min speed 4 */
3590 if (!acpi_ec_write(fan_status_offset, level))
3591 return -EIO;
3592 else
3593 tp_features.fan_ctrl_status_undef = 0;
3594 break;
3596 default:
3597 return -ENXIO;
3599 return 0;
3602 static int fan_set_level_safe(int level)
3604 int rc;
3606 if (!fan_control_allowed)
3607 return -EPERM;
3609 rc = mutex_lock_interruptible(&fan_mutex);
3610 if (rc < 0)
3611 return rc;
3613 if (level == TPACPI_FAN_LAST_LEVEL)
3614 level = fan_control_desired_level;
3616 rc = fan_set_level(level);
3617 if (!rc)
3618 fan_update_desired_level(level);
3620 mutex_unlock(&fan_mutex);
3621 return rc;
3624 static int fan_set_enable(void)
3626 u8 s;
3627 int rc;
3629 if (!fan_control_allowed)
3630 return -EPERM;
3632 rc = mutex_lock_interruptible(&fan_mutex);
3633 if (rc < 0)
3634 return rc;
3636 switch (fan_control_access_mode) {
3637 case TPACPI_FAN_WR_ACPI_FANS:
3638 case TPACPI_FAN_WR_TPEC:
3639 rc = fan_get_status(&s);
3640 if (rc < 0)
3641 break;
3643 /* Don't go out of emergency fan mode */
3644 if (s != 7) {
3645 s &= 0x07;
3646 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3649 if (!acpi_ec_write(fan_status_offset, s))
3650 rc = -EIO;
3651 else {
3652 tp_features.fan_ctrl_status_undef = 0;
3653 rc = 0;
3655 break;
3657 case TPACPI_FAN_WR_ACPI_SFAN:
3658 rc = fan_get_status(&s);
3659 if (rc < 0)
3660 break;
3662 s &= 0x07;
3664 /* Set fan to at least level 4 */
3665 s |= 4;
3667 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3668 rc= -EIO;
3669 else
3670 rc = 0;
3671 break;
3673 default:
3674 rc = -ENXIO;
3677 mutex_unlock(&fan_mutex);
3678 return rc;
3681 static int fan_set_disable(void)
3683 int rc;
3685 if (!fan_control_allowed)
3686 return -EPERM;
3688 rc = mutex_lock_interruptible(&fan_mutex);
3689 if (rc < 0)
3690 return rc;
3692 rc = 0;
3693 switch (fan_control_access_mode) {
3694 case TPACPI_FAN_WR_ACPI_FANS:
3695 case TPACPI_FAN_WR_TPEC:
3696 if (!acpi_ec_write(fan_status_offset, 0x00))
3697 rc = -EIO;
3698 else {
3699 fan_control_desired_level = 0;
3700 tp_features.fan_ctrl_status_undef = 0;
3702 break;
3704 case TPACPI_FAN_WR_ACPI_SFAN:
3705 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3706 rc = -EIO;
3707 else
3708 fan_control_desired_level = 0;
3709 break;
3711 default:
3712 rc = -ENXIO;
3716 mutex_unlock(&fan_mutex);
3717 return rc;
3720 static int fan_set_speed(int speed)
3722 int rc;
3724 if (!fan_control_allowed)
3725 return -EPERM;
3727 rc = mutex_lock_interruptible(&fan_mutex);
3728 if (rc < 0)
3729 return rc;
3731 rc = 0;
3732 switch (fan_control_access_mode) {
3733 case TPACPI_FAN_WR_ACPI_FANS:
3734 if (speed >= 0 && speed <= 65535) {
3735 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3736 speed, speed, speed))
3737 rc = -EIO;
3738 } else
3739 rc = -EINVAL;
3740 break;
3742 default:
3743 rc = -ENXIO;
3746 mutex_unlock(&fan_mutex);
3747 return rc;
3750 static int fan_read(char *p)
3752 int len = 0;
3753 int rc;
3754 u8 status;
3755 unsigned int speed = 0;
3757 switch (fan_status_access_mode) {
3758 case TPACPI_FAN_RD_ACPI_GFAN:
3759 /* 570, 600e/x, 770e, 770x */
3760 if ((rc = fan_get_status_safe(&status)) < 0)
3761 return rc;
3763 len += sprintf(p + len, "status:\t\t%s\n"
3764 "level:\t\t%d\n",
3765 (status != 0) ? "enabled" : "disabled", status);
3766 break;
3768 case TPACPI_FAN_RD_TPEC:
3769 /* all except 570, 600e/x, 770e, 770x */
3770 if ((rc = fan_get_status_safe(&status)) < 0)
3771 return rc;
3773 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3774 if (status != fan_control_initial_status)
3775 tp_features.fan_ctrl_status_undef = 0;
3776 else
3777 /* Return most likely status. In fact, it
3778 * might be the only possible status */
3779 status = TP_EC_FAN_AUTO;
3782 len += sprintf(p + len, "status:\t\t%s\n",
3783 (status != 0) ? "enabled" : "disabled");
3785 if ((rc = fan_get_speed(&speed)) < 0)
3786 return rc;
3788 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3790 if (status & TP_EC_FAN_FULLSPEED)
3791 /* Disengaged mode takes precedence */
3792 len += sprintf(p + len, "level:\t\tdisengaged\n");
3793 else if (status & TP_EC_FAN_AUTO)
3794 len += sprintf(p + len, "level:\t\tauto\n");
3795 else
3796 len += sprintf(p + len, "level:\t\t%d\n", status);
3797 break;
3799 case TPACPI_FAN_NONE:
3800 default:
3801 len += sprintf(p + len, "status:\t\tnot supported\n");
3804 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3805 len += sprintf(p + len, "commands:\tlevel <level>");
3807 switch (fan_control_access_mode) {
3808 case TPACPI_FAN_WR_ACPI_SFAN:
3809 len += sprintf(p + len, " (<level> is 0-7)\n");
3810 break;
3812 default:
3813 len += sprintf(p + len, " (<level> is 0-7, "
3814 "auto, disengaged, full-speed)\n");
3815 break;
3819 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3820 len += sprintf(p + len, "commands:\tenable, disable\n"
3821 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3822 "1-120 (seconds))\n");
3824 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3825 len += sprintf(p + len, "commands:\tspeed <speed>"
3826 " (<speed> is 0-65535)\n");
3828 return len;
3831 static int fan_write_cmd_level(const char *cmd, int *rc)
3833 int level;
3835 if (strlencmp(cmd, "level auto") == 0)
3836 level = TP_EC_FAN_AUTO;
3837 else if ((strlencmp(cmd, "level disengaged") == 0) |
3838 (strlencmp(cmd, "level full-speed") == 0))
3839 level = TP_EC_FAN_FULLSPEED;
3840 else if (sscanf(cmd, "level %d", &level) != 1)
3841 return 0;
3843 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3844 printk(IBM_ERR "level command accepted for unsupported "
3845 "access mode %d", fan_control_access_mode);
3847 return 1;
3850 static int fan_write_cmd_enable(const char *cmd, int *rc)
3852 if (strlencmp(cmd, "enable") != 0)
3853 return 0;
3855 if ((*rc = fan_set_enable()) == -ENXIO)
3856 printk(IBM_ERR "enable command accepted for unsupported "
3857 "access mode %d", fan_control_access_mode);
3859 return 1;
3862 static int fan_write_cmd_disable(const char *cmd, int *rc)
3864 if (strlencmp(cmd, "disable") != 0)
3865 return 0;
3867 if ((*rc = fan_set_disable()) == -ENXIO)
3868 printk(IBM_ERR "disable command accepted for unsupported "
3869 "access mode %d", fan_control_access_mode);
3871 return 1;
3874 static int fan_write_cmd_speed(const char *cmd, int *rc)
3876 int speed;
3878 /* TODO:
3879 * Support speed <low> <medium> <high> ? */
3881 if (sscanf(cmd, "speed %d", &speed) != 1)
3882 return 0;
3884 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3885 printk(IBM_ERR "speed command accepted for unsupported "
3886 "access mode %d", fan_control_access_mode);
3888 return 1;
3891 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3893 int interval;
3895 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3896 return 0;
3898 if (interval < 0 || interval > 120)
3899 *rc = -EINVAL;
3900 else
3901 fan_watchdog_maxinterval = interval;
3903 return 1;
3906 static int fan_write(char *buf)
3908 char *cmd;
3909 int rc = 0;
3911 while (!rc && (cmd = next_cmd(&buf))) {
3912 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3913 fan_write_cmd_level(cmd, &rc)) &&
3914 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3915 (fan_write_cmd_enable(cmd, &rc) ||
3916 fan_write_cmd_disable(cmd, &rc) ||
3917 fan_write_cmd_watchdog(cmd, &rc))) &&
3918 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3919 fan_write_cmd_speed(cmd, &rc))
3921 rc = -EINVAL;
3922 else if (!rc)
3923 fan_watchdog_reset();
3926 return rc;
3929 static struct ibm_struct fan_driver_data = {
3930 .name = "fan",
3931 .read = fan_read,
3932 .write = fan_write,
3933 .exit = fan_exit,
3936 /****************************************************************************
3937 ****************************************************************************
3939 * Infrastructure
3941 ****************************************************************************
3942 ****************************************************************************/
3944 /* /proc support */
3945 static struct proc_dir_entry *proc_dir;
3947 /* Subdriver registry */
3948 static LIST_HEAD(tpacpi_all_drivers);
3952 * Module and infrastructure proble, init and exit handling
3955 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3956 static const char * __init str_supported(int is_supported)
3958 static char text_unsupported[] __initdata = "not supported";
3960 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3962 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3964 static int __init ibm_init(struct ibm_init_struct *iibm)
3966 int ret;
3967 struct ibm_struct *ibm = iibm->data;
3968 struct proc_dir_entry *entry;
3970 BUG_ON(ibm == NULL);
3972 INIT_LIST_HEAD(&ibm->all_drivers);
3974 if (ibm->flags.experimental && !experimental)
3975 return 0;
3977 dbg_printk(TPACPI_DBG_INIT,
3978 "probing for %s\n", ibm->name);
3980 if (iibm->init) {
3981 ret = iibm->init(iibm);
3982 if (ret > 0)
3983 return 0; /* probe failed */
3984 if (ret)
3985 return ret;
3987 ibm->flags.init_called = 1;
3990 if (ibm->acpi) {
3991 if (ibm->acpi->hid) {
3992 ret = register_tpacpi_subdriver(ibm);
3993 if (ret)
3994 goto err_out;
3997 if (ibm->acpi->notify) {
3998 ret = setup_acpi_notify(ibm);
3999 if (ret == -ENODEV) {
4000 printk(IBM_NOTICE "disabling subdriver %s\n",
4001 ibm->name);
4002 ret = 0;
4003 goto err_out;
4005 if (ret < 0)
4006 goto err_out;
4010 dbg_printk(TPACPI_DBG_INIT,
4011 "%s installed\n", ibm->name);
4013 if (ibm->read) {
4014 entry = create_proc_entry(ibm->name,
4015 S_IFREG | S_IRUGO | S_IWUSR,
4016 proc_dir);
4017 if (!entry) {
4018 printk(IBM_ERR "unable to create proc entry %s\n",
4019 ibm->name);
4020 ret = -ENODEV;
4021 goto err_out;
4023 entry->owner = THIS_MODULE;
4024 entry->data = ibm;
4025 entry->read_proc = &dispatch_procfs_read;
4026 if (ibm->write)
4027 entry->write_proc = &dispatch_procfs_write;
4028 ibm->flags.proc_created = 1;
4031 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4033 return 0;
4035 err_out:
4036 dbg_printk(TPACPI_DBG_INIT,
4037 "%s: at error exit path with result %d\n",
4038 ibm->name, ret);
4040 ibm_exit(ibm);
4041 return (ret < 0)? ret : 0;
4044 static void ibm_exit(struct ibm_struct *ibm)
4046 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4048 list_del_init(&ibm->all_drivers);
4050 if (ibm->flags.acpi_notify_installed) {
4051 dbg_printk(TPACPI_DBG_EXIT,
4052 "%s: acpi_remove_notify_handler\n", ibm->name);
4053 BUG_ON(!ibm->acpi);
4054 acpi_remove_notify_handler(*ibm->acpi->handle,
4055 ibm->acpi->type,
4056 dispatch_acpi_notify);
4057 ibm->flags.acpi_notify_installed = 0;
4058 ibm->flags.acpi_notify_installed = 0;
4061 if (ibm->flags.proc_created) {
4062 dbg_printk(TPACPI_DBG_EXIT,
4063 "%s: remove_proc_entry\n", ibm->name);
4064 remove_proc_entry(ibm->name, proc_dir);
4065 ibm->flags.proc_created = 0;
4068 if (ibm->flags.acpi_driver_registered) {
4069 dbg_printk(TPACPI_DBG_EXIT,
4070 "%s: acpi_bus_unregister_driver\n", ibm->name);
4071 BUG_ON(!ibm->acpi);
4072 acpi_bus_unregister_driver(ibm->acpi->driver);
4073 kfree(ibm->acpi->driver);
4074 ibm->acpi->driver = NULL;
4075 ibm->flags.acpi_driver_registered = 0;
4078 if (ibm->flags.init_called && ibm->exit) {
4079 ibm->exit();
4080 ibm->flags.init_called = 0;
4083 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4086 /* Probing */
4088 static char *ibm_thinkpad_ec_found;
4090 static char* __init check_dmi_for_ec(void)
4092 struct dmi_device *dev = NULL;
4093 char ec_fw_string[18];
4096 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4097 * X32 or newer, all Z series; Some models must have an
4098 * up-to-date BIOS or they will not be detected.
4100 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4102 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4103 if (sscanf(dev->name,
4104 "IBM ThinkPad Embedded Controller -[%17c",
4105 ec_fw_string) == 1) {
4106 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4107 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4108 return kstrdup(ec_fw_string, GFP_KERNEL);
4111 return NULL;
4114 static int __init probe_for_thinkpad(void)
4116 int is_thinkpad;
4118 if (acpi_disabled)
4119 return -ENODEV;
4122 * Non-ancient models have better DMI tagging, but very old models
4123 * don't.
4125 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4127 /* ec is required because many other handles are relative to it */
4128 IBM_ACPIHANDLE_INIT(ec);
4129 if (!ec_handle) {
4130 if (is_thinkpad)
4131 printk(IBM_ERR
4132 "Not yet supported ThinkPad detected!\n");
4133 return -ENODEV;
4137 * Risks a regression on very old machines, but reduces potential
4138 * false positives a damn great deal
4140 if (!is_thinkpad)
4141 is_thinkpad = dmi_name_in_vendors("IBM");
4143 if (!is_thinkpad && !force_load)
4144 return -ENODEV;
4146 return 0;
4150 /* Module init, exit, parameters */
4152 static struct ibm_init_struct ibms_init[] __initdata = {
4154 .init = thinkpad_acpi_driver_init,
4155 .data = &thinkpad_acpi_driver_data,
4158 .init = hotkey_init,
4159 .data = &hotkey_driver_data,
4162 .init = bluetooth_init,
4163 .data = &bluetooth_driver_data,
4166 .init = wan_init,
4167 .data = &wan_driver_data,
4170 .init = video_init,
4171 .data = &video_driver_data,
4174 .init = light_init,
4175 .data = &light_driver_data,
4177 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4179 .init = dock_init,
4180 .data = &dock_driver_data[0],
4183 .init = dock_init2,
4184 .data = &dock_driver_data[1],
4186 #endif
4187 #ifdef CONFIG_THINKPAD_ACPI_BAY
4189 .init = bay_init,
4190 .data = &bay_driver_data,
4192 #endif
4194 .init = cmos_init,
4195 .data = &cmos_driver_data,
4198 .init = led_init,
4199 .data = &led_driver_data,
4202 .init = beep_init,
4203 .data = &beep_driver_data,
4206 .init = thermal_init,
4207 .data = &thermal_driver_data,
4210 .data = &ecdump_driver_data,
4213 .init = brightness_init,
4214 .data = &brightness_driver_data,
4217 .data = &volume_driver_data,
4220 .init = fan_init,
4221 .data = &fan_driver_data,
4225 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4227 unsigned int i;
4228 struct ibm_struct *ibm;
4230 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4231 ibm = ibms_init[i].data;
4232 BUG_ON(ibm == NULL);
4234 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4235 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4236 return -ENOSPC;
4237 strcpy(ibms_init[i].param, val);
4238 strcat(ibms_init[i].param, ",");
4239 return 0;
4243 return -EINVAL;
4246 static int experimental;
4247 module_param(experimental, int, 0);
4249 static u32 dbg_level;
4250 module_param_named(debug, dbg_level, uint, 0);
4252 static int force_load;
4253 module_param(force_load, int, 0);
4255 static int fan_control_allowed;
4256 module_param_named(fan_control, fan_control_allowed, int, 0);
4258 #define IBM_PARAM(feature) \
4259 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4261 IBM_PARAM(hotkey);
4262 IBM_PARAM(bluetooth);
4263 IBM_PARAM(video);
4264 IBM_PARAM(light);
4265 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4266 IBM_PARAM(dock);
4267 #endif
4268 #ifdef CONFIG_THINKPAD_ACPI_BAY
4269 IBM_PARAM(bay);
4270 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4271 IBM_PARAM(cmos);
4272 IBM_PARAM(led);
4273 IBM_PARAM(beep);
4274 IBM_PARAM(ecdump);
4275 IBM_PARAM(brightness);
4276 IBM_PARAM(volume);
4277 IBM_PARAM(fan);
4279 static int __init thinkpad_acpi_module_init(void)
4281 int ret, i;
4283 /* Driver-level probe */
4284 ret = probe_for_thinkpad();
4285 if (ret)
4286 return ret;
4288 /* Driver initialization */
4289 ibm_thinkpad_ec_found = check_dmi_for_ec();
4290 IBM_ACPIHANDLE_INIT(ecrd);
4291 IBM_ACPIHANDLE_INIT(ecwr);
4293 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4294 if (!proc_dir) {
4295 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4296 thinkpad_acpi_module_exit();
4297 return -ENODEV;
4299 proc_dir->owner = THIS_MODULE;
4301 ret = platform_driver_register(&tpacpi_pdriver);
4302 if (ret) {
4303 printk(IBM_ERR "unable to register platform driver\n");
4304 thinkpad_acpi_module_exit();
4305 return ret;
4307 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4308 if (ret) {
4309 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4310 thinkpad_acpi_module_exit();
4311 return ret;
4315 /* Device initialization */
4316 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4317 NULL, 0);
4318 if (IS_ERR(tpacpi_pdev)) {
4319 ret = PTR_ERR(tpacpi_pdev);
4320 tpacpi_pdev = NULL;
4321 printk(IBM_ERR "unable to register platform device\n");
4322 thinkpad_acpi_module_exit();
4323 return ret;
4325 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4326 if (IS_ERR(tpacpi_hwmon)) {
4327 ret = PTR_ERR(tpacpi_hwmon);
4328 tpacpi_hwmon = NULL;
4329 printk(IBM_ERR "unable to register hwmon device\n");
4330 thinkpad_acpi_module_exit();
4331 return ret;
4333 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4334 ret = ibm_init(&ibms_init[i]);
4335 if (ret >= 0 && *ibms_init[i].param)
4336 ret = ibms_init[i].data->write(ibms_init[i].param);
4337 if (ret < 0) {
4338 thinkpad_acpi_module_exit();
4339 return ret;
4343 return 0;
4346 static void thinkpad_acpi_module_exit(void)
4348 struct ibm_struct *ibm, *itmp;
4350 list_for_each_entry_safe_reverse(ibm, itmp,
4351 &tpacpi_all_drivers,
4352 all_drivers) {
4353 ibm_exit(ibm);
4356 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4358 if (tpacpi_hwmon)
4359 hwmon_device_unregister(tpacpi_hwmon);
4361 if (tpacpi_pdev)
4362 platform_device_unregister(tpacpi_pdev);
4364 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4365 platform_driver_unregister(&tpacpi_pdriver);
4367 if (proc_dir)
4368 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4370 kfree(ibm_thinkpad_ec_found);
4373 module_init(thinkpad_acpi_module_init);
4374 module_exit(thinkpad_acpi_module_exit);