ACPI: thinkpad-acpi: enable more hotkeys
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob450b1e5cd6810154b5fb5403e191ca424b9f1743
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;
732 static struct attribute_set *hotkey_dev_attributes;
734 /* sysfs hotkey enable ------------------------------------------------- */
735 static ssize_t hotkey_enable_show(struct device *dev,
736 struct device_attribute *attr,
737 char *buf)
739 int res, status;
740 u32 mask;
742 res = hotkey_get(&status, &mask);
743 if (res)
744 return res;
746 return snprintf(buf, PAGE_SIZE, "%d\n", status);
749 static ssize_t hotkey_enable_store(struct device *dev,
750 struct device_attribute *attr,
751 const char *buf, size_t count)
753 unsigned long t;
754 int res, status;
755 u32 mask;
757 if (parse_strtoul(buf, 1, &t))
758 return -EINVAL;
760 res = hotkey_get(&status, &mask);
761 if (!res)
762 res = hotkey_set(t, mask);
764 return (res) ? res : count;
767 static struct device_attribute dev_attr_hotkey_enable =
768 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
769 hotkey_enable_show, hotkey_enable_store);
771 /* sysfs hotkey mask --------------------------------------------------- */
772 static ssize_t hotkey_mask_show(struct device *dev,
773 struct device_attribute *attr,
774 char *buf)
776 int res, status;
777 u32 mask;
779 res = hotkey_get(&status, &mask);
780 if (res)
781 return res;
783 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
786 static ssize_t hotkey_mask_store(struct device *dev,
787 struct device_attribute *attr,
788 const char *buf, size_t count)
790 unsigned long t;
791 int res, status;
792 u32 mask;
794 if (parse_strtoul(buf, 0xffffffffUL, &t))
795 return -EINVAL;
797 res = hotkey_get(&status, &mask);
798 if (!res)
799 hotkey_set(status, t);
801 return (res) ? res : count;
804 static struct device_attribute dev_attr_hotkey_mask =
805 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
806 hotkey_mask_show, hotkey_mask_store);
808 /* sysfs hotkey bios_enabled ------------------------------------------- */
809 static ssize_t hotkey_bios_enabled_show(struct device *dev,
810 struct device_attribute *attr,
811 char *buf)
813 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
816 static struct device_attribute dev_attr_hotkey_bios_enabled =
817 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
819 /* sysfs hotkey bios_mask ---------------------------------------------- */
820 static ssize_t hotkey_bios_mask_show(struct device *dev,
821 struct device_attribute *attr,
822 char *buf)
824 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
827 static struct device_attribute dev_attr_hotkey_bios_mask =
828 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
830 /* --------------------------------------------------------------------- */
832 static struct attribute *hotkey_mask_attributes[] = {
833 &dev_attr_hotkey_mask.attr,
834 &dev_attr_hotkey_bios_enabled.attr,
835 &dev_attr_hotkey_bios_mask.attr,
838 static int __init hotkey_init(struct ibm_init_struct *iibm)
840 int res;
842 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
844 IBM_ACPIHANDLE_INIT(hkey);
845 mutex_init(&hotkey_mutex);
847 /* hotkey not supported on 570 */
848 tp_features.hotkey = hkey_handle != NULL;
850 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
851 str_supported(tp_features.hotkey));
853 if (tp_features.hotkey) {
854 hotkey_dev_attributes = create_attr_set(4, NULL);
855 if (!hotkey_dev_attributes)
856 return -ENOMEM;
857 res = add_to_attr_set(hotkey_dev_attributes,
858 &dev_attr_hotkey_enable.attr);
859 if (res)
860 return res;
862 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
863 A30, R30, R31, T20-22, X20-21, X22-24 */
864 tp_features.hotkey_mask =
865 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
867 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
868 str_supported(tp_features.hotkey_mask));
870 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
871 if (!res && tp_features.hotkey_mask) {
872 res = add_many_to_attr_set(hotkey_dev_attributes,
873 hotkey_mask_attributes,
874 ARRAY_SIZE(hotkey_mask_attributes));
876 if (!res)
877 res = register_attr_set_with_sysfs(
878 hotkey_dev_attributes,
879 &tpacpi_pdev->dev.kobj);
881 if (res)
882 return res;
885 return (tp_features.hotkey)? 0 : 1;
888 static void hotkey_exit(void)
890 int res;
892 if (tp_features.hotkey) {
893 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
894 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
895 if (res)
896 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
899 if (hotkey_dev_attributes) {
900 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
901 hotkey_dev_attributes = NULL;
905 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
907 int hkey;
909 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
910 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
911 } else {
912 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
913 acpi_bus_generate_event(ibm->acpi->device, event, 0);
918 * Call with hotkey_mutex held
920 static int hotkey_get(int *status, u32 *mask)
922 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
923 return -EIO;
925 if (tp_features.hotkey_mask)
926 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
927 return -EIO;
929 return 0;
933 * Call with hotkey_mutex held
935 static int hotkey_set(int status, u32 mask)
937 int i;
939 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
940 return -EIO;
942 if (tp_features.hotkey_mask)
943 for (i = 0; i < 32; i++) {
944 int bit = ((1 << i) & mask) != 0;
945 if (!acpi_evalf(hkey_handle,
946 NULL, "MHKM", "vdd", i + 1, bit))
947 return -EIO;
950 return 0;
953 /* procfs -------------------------------------------------------------- */
954 static int hotkey_read(char *p)
956 int res, status;
957 u32 mask;
958 int len = 0;
960 if (!tp_features.hotkey) {
961 len += sprintf(p + len, "status:\t\tnot supported\n");
962 return len;
965 res = mutex_lock_interruptible(&hotkey_mutex);
966 if (res < 0)
967 return res;
968 res = hotkey_get(&status, &mask);
969 mutex_unlock(&hotkey_mutex);
970 if (res)
971 return res;
973 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
974 if (tp_features.hotkey_mask) {
975 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
976 len += sprintf(p + len,
977 "commands:\tenable, disable, reset, <mask>\n");
978 } else {
979 len += sprintf(p + len, "mask:\t\tnot supported\n");
980 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
983 return len;
986 static int hotkey_write(char *buf)
988 int res, status;
989 u32 mask;
990 char *cmd;
991 int do_cmd = 0;
993 if (!tp_features.hotkey)
994 return -ENODEV;
996 res = mutex_lock_interruptible(&hotkey_mutex);
997 if (res < 0)
998 return res;
1000 res = hotkey_get(&status, &mask);
1001 if (res)
1002 goto errexit;
1004 res = 0;
1005 while ((cmd = next_cmd(&buf))) {
1006 if (strlencmp(cmd, "enable") == 0) {
1007 status = 1;
1008 } else if (strlencmp(cmd, "disable") == 0) {
1009 status = 0;
1010 } else if (strlencmp(cmd, "reset") == 0) {
1011 status = hotkey_orig_status;
1012 mask = hotkey_orig_mask;
1013 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1014 /* mask set */
1015 } else if (sscanf(cmd, "%x", &mask) == 1) {
1016 /* mask set */
1017 } else {
1018 res = -EINVAL;
1019 goto errexit;
1021 do_cmd = 1;
1024 if (do_cmd)
1025 res = hotkey_set(status, mask);
1027 errexit:
1028 mutex_unlock(&hotkey_mutex);
1029 return res;
1032 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1033 .hid = IBM_HKEY_HID,
1034 .notify = hotkey_notify,
1035 .handle = &hkey_handle,
1036 .type = ACPI_DEVICE_NOTIFY,
1039 static struct ibm_struct hotkey_driver_data = {
1040 .name = "hotkey",
1041 .read = hotkey_read,
1042 .write = hotkey_write,
1043 .exit = hotkey_exit,
1044 .acpi = &ibm_hotkey_acpidriver,
1047 /*************************************************************************
1048 * Bluetooth subdriver
1051 /* sysfs bluetooth enable ---------------------------------------------- */
1052 static ssize_t bluetooth_enable_show(struct device *dev,
1053 struct device_attribute *attr,
1054 char *buf)
1056 int status;
1058 status = bluetooth_get_radiosw();
1059 if (status < 0)
1060 return status;
1062 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1065 static ssize_t bluetooth_enable_store(struct device *dev,
1066 struct device_attribute *attr,
1067 const char *buf, size_t count)
1069 unsigned long t;
1070 int res;
1072 if (parse_strtoul(buf, 1, &t))
1073 return -EINVAL;
1075 res = bluetooth_set_radiosw(t);
1077 return (res) ? res : count;
1080 static struct device_attribute dev_attr_bluetooth_enable =
1081 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1082 bluetooth_enable_show, bluetooth_enable_store);
1084 /* --------------------------------------------------------------------- */
1086 static struct attribute *bluetooth_attributes[] = {
1087 &dev_attr_bluetooth_enable.attr,
1088 NULL
1091 static const struct attribute_group bluetooth_attr_group = {
1092 .attrs = bluetooth_attributes,
1095 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1097 int res;
1098 int status = 0;
1100 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1102 IBM_ACPIHANDLE_INIT(hkey);
1104 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1105 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1106 tp_features.bluetooth = hkey_handle &&
1107 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1109 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1110 str_supported(tp_features.bluetooth),
1111 status);
1113 if (tp_features.bluetooth) {
1114 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1115 /* no bluetooth hardware present in system */
1116 tp_features.bluetooth = 0;
1117 dbg_printk(TPACPI_DBG_INIT,
1118 "bluetooth hardware not installed\n");
1119 } else {
1120 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1121 &bluetooth_attr_group);
1122 if (res)
1123 return res;
1127 return (tp_features.bluetooth)? 0 : 1;
1130 static void bluetooth_exit(void)
1132 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1133 &bluetooth_attr_group);
1136 static int bluetooth_get_radiosw(void)
1138 int status;
1140 if (!tp_features.bluetooth)
1141 return -ENODEV;
1143 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1144 return -EIO;
1146 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1149 static int bluetooth_set_radiosw(int radio_on)
1151 int status;
1153 if (!tp_features.bluetooth)
1154 return -ENODEV;
1156 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1157 return -EIO;
1158 if (radio_on)
1159 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1160 else
1161 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1162 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1163 return -EIO;
1165 return 0;
1168 /* procfs -------------------------------------------------------------- */
1169 static int bluetooth_read(char *p)
1171 int len = 0;
1172 int status = bluetooth_get_radiosw();
1174 if (!tp_features.bluetooth)
1175 len += sprintf(p + len, "status:\t\tnot supported\n");
1176 else {
1177 len += sprintf(p + len, "status:\t\t%s\n",
1178 (status)? "enabled" : "disabled");
1179 len += sprintf(p + len, "commands:\tenable, disable\n");
1182 return len;
1185 static int bluetooth_write(char *buf)
1187 char *cmd;
1189 if (!tp_features.bluetooth)
1190 return -ENODEV;
1192 while ((cmd = next_cmd(&buf))) {
1193 if (strlencmp(cmd, "enable") == 0) {
1194 bluetooth_set_radiosw(1);
1195 } else if (strlencmp(cmd, "disable") == 0) {
1196 bluetooth_set_radiosw(0);
1197 } else
1198 return -EINVAL;
1201 return 0;
1204 static struct ibm_struct bluetooth_driver_data = {
1205 .name = "bluetooth",
1206 .read = bluetooth_read,
1207 .write = bluetooth_write,
1208 .exit = bluetooth_exit,
1211 /*************************************************************************
1212 * Wan subdriver
1215 /* sysfs wan enable ---------------------------------------------------- */
1216 static ssize_t wan_enable_show(struct device *dev,
1217 struct device_attribute *attr,
1218 char *buf)
1220 int status;
1222 status = wan_get_radiosw();
1223 if (status < 0)
1224 return status;
1226 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1229 static ssize_t wan_enable_store(struct device *dev,
1230 struct device_attribute *attr,
1231 const char *buf, size_t count)
1233 unsigned long t;
1234 int res;
1236 if (parse_strtoul(buf, 1, &t))
1237 return -EINVAL;
1239 res = wan_set_radiosw(t);
1241 return (res) ? res : count;
1244 static struct device_attribute dev_attr_wan_enable =
1245 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1246 wan_enable_show, wan_enable_store);
1248 /* --------------------------------------------------------------------- */
1250 static struct attribute *wan_attributes[] = {
1251 &dev_attr_wan_enable.attr,
1252 NULL
1255 static const struct attribute_group wan_attr_group = {
1256 .attrs = wan_attributes,
1259 static int __init wan_init(struct ibm_init_struct *iibm)
1261 int res;
1262 int status = 0;
1264 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1266 IBM_ACPIHANDLE_INIT(hkey);
1268 tp_features.wan = hkey_handle &&
1269 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1271 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1272 str_supported(tp_features.wan),
1273 status);
1275 if (tp_features.wan) {
1276 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1277 /* no wan hardware present in system */
1278 tp_features.wan = 0;
1279 dbg_printk(TPACPI_DBG_INIT,
1280 "wan hardware not installed\n");
1281 } else {
1282 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1283 &wan_attr_group);
1284 if (res)
1285 return res;
1289 return (tp_features.wan)? 0 : 1;
1292 static void wan_exit(void)
1294 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1295 &wan_attr_group);
1298 static int wan_get_radiosw(void)
1300 int status;
1302 if (!tp_features.wan)
1303 return -ENODEV;
1305 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1306 return -EIO;
1308 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1311 static int wan_set_radiosw(int radio_on)
1313 int status;
1315 if (!tp_features.wan)
1316 return -ENODEV;
1318 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1319 return -EIO;
1320 if (radio_on)
1321 status |= TP_ACPI_WANCARD_RADIOSSW;
1322 else
1323 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1324 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1325 return -EIO;
1327 return 0;
1330 /* procfs -------------------------------------------------------------- */
1331 static int wan_read(char *p)
1333 int len = 0;
1334 int status = wan_get_radiosw();
1336 if (!tp_features.wan)
1337 len += sprintf(p + len, "status:\t\tnot supported\n");
1338 else {
1339 len += sprintf(p + len, "status:\t\t%s\n",
1340 (status)? "enabled" : "disabled");
1341 len += sprintf(p + len, "commands:\tenable, disable\n");
1344 return len;
1347 static int wan_write(char *buf)
1349 char *cmd;
1351 if (!tp_features.wan)
1352 return -ENODEV;
1354 while ((cmd = next_cmd(&buf))) {
1355 if (strlencmp(cmd, "enable") == 0) {
1356 wan_set_radiosw(1);
1357 } else if (strlencmp(cmd, "disable") == 0) {
1358 wan_set_radiosw(0);
1359 } else
1360 return -EINVAL;
1363 return 0;
1366 static struct ibm_struct wan_driver_data = {
1367 .name = "wan",
1368 .read = wan_read,
1369 .write = wan_write,
1370 .exit = wan_exit,
1371 .flags.experimental = 1,
1374 /*************************************************************************
1375 * Video subdriver
1378 static enum video_access_mode video_supported;
1379 static int video_orig_autosw;
1381 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1382 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1383 "\\_SB.PCI0.VID0", /* 770e */
1384 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1385 "\\_SB.PCI0.AGP.VID", /* all others */
1386 ); /* R30, R31 */
1388 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1390 static int __init video_init(struct ibm_init_struct *iibm)
1392 int ivga;
1394 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1396 IBM_ACPIHANDLE_INIT(vid);
1397 IBM_ACPIHANDLE_INIT(vid2);
1399 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1400 /* G41, assume IVGA doesn't change */
1401 vid_handle = vid2_handle;
1403 if (!vid_handle)
1404 /* video switching not supported on R30, R31 */
1405 video_supported = TPACPI_VIDEO_NONE;
1406 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1407 /* 570 */
1408 video_supported = TPACPI_VIDEO_570;
1409 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1410 /* 600e/x, 770e, 770x */
1411 video_supported = TPACPI_VIDEO_770;
1412 else
1413 /* all others */
1414 video_supported = TPACPI_VIDEO_NEW;
1416 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1417 str_supported(video_supported != TPACPI_VIDEO_NONE),
1418 video_supported);
1420 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1423 static void video_exit(void)
1425 dbg_printk(TPACPI_DBG_EXIT,
1426 "restoring original video autoswitch mode\n");
1427 if (video_autosw_set(video_orig_autosw))
1428 printk(IBM_ERR "error while trying to restore original "
1429 "video autoswitch mode\n");
1432 static int video_outputsw_get(void)
1434 int status = 0;
1435 int i;
1437 switch (video_supported) {
1438 case TPACPI_VIDEO_570:
1439 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1440 TP_ACPI_VIDEO_570_PHSCMD))
1441 return -EIO;
1442 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1443 break;
1444 case TPACPI_VIDEO_770:
1445 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1446 return -EIO;
1447 if (i)
1448 status |= TP_ACPI_VIDEO_S_LCD;
1449 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1450 return -EIO;
1451 if (i)
1452 status |= TP_ACPI_VIDEO_S_CRT;
1453 break;
1454 case TPACPI_VIDEO_NEW:
1455 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1456 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1457 return -EIO;
1458 if (i)
1459 status |= TP_ACPI_VIDEO_S_CRT;
1461 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1462 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1463 return -EIO;
1464 if (i)
1465 status |= TP_ACPI_VIDEO_S_LCD;
1466 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1467 return -EIO;
1468 if (i)
1469 status |= TP_ACPI_VIDEO_S_DVI;
1470 break;
1471 default:
1472 return -ENOSYS;
1475 return status;
1478 static int video_outputsw_set(int status)
1480 int autosw;
1481 int res = 0;
1483 switch (video_supported) {
1484 case TPACPI_VIDEO_570:
1485 res = acpi_evalf(NULL, NULL,
1486 "\\_SB.PHS2", "vdd",
1487 TP_ACPI_VIDEO_570_PHS2CMD,
1488 status | TP_ACPI_VIDEO_570_PHS2SET);
1489 break;
1490 case TPACPI_VIDEO_770:
1491 autosw = video_autosw_get();
1492 if (autosw < 0)
1493 return autosw;
1495 res = video_autosw_set(1);
1496 if (res)
1497 return res;
1498 res = acpi_evalf(vid_handle, NULL,
1499 "ASWT", "vdd", status * 0x100, 0);
1500 if (!autosw && video_autosw_set(autosw)) {
1501 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1502 return -EIO;
1504 break;
1505 case TPACPI_VIDEO_NEW:
1506 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1507 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1508 break;
1509 default:
1510 return -ENOSYS;
1513 return (res)? 0 : -EIO;
1516 static int video_autosw_get(void)
1518 int autosw = 0;
1520 switch (video_supported) {
1521 case TPACPI_VIDEO_570:
1522 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1523 return -EIO;
1524 break;
1525 case TPACPI_VIDEO_770:
1526 case TPACPI_VIDEO_NEW:
1527 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1528 return -EIO;
1529 break;
1530 default:
1531 return -ENOSYS;
1534 return autosw & 1;
1537 static int video_autosw_set(int enable)
1539 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1540 return -EIO;
1541 return 0;
1544 static int video_outputsw_cycle(void)
1546 int autosw = video_autosw_get();
1547 int res;
1549 if (autosw < 0)
1550 return autosw;
1552 switch (video_supported) {
1553 case TPACPI_VIDEO_570:
1554 res = video_autosw_set(1);
1555 if (res)
1556 return res;
1557 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1558 break;
1559 case TPACPI_VIDEO_770:
1560 case TPACPI_VIDEO_NEW:
1561 res = video_autosw_set(1);
1562 if (res)
1563 return res;
1564 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1565 break;
1566 default:
1567 return -ENOSYS;
1569 if (!autosw && video_autosw_set(autosw)) {
1570 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1571 return -EIO;
1574 return (res)? 0 : -EIO;
1577 static int video_expand_toggle(void)
1579 switch (video_supported) {
1580 case TPACPI_VIDEO_570:
1581 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1582 0 : -EIO;
1583 case TPACPI_VIDEO_770:
1584 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1585 0 : -EIO;
1586 case TPACPI_VIDEO_NEW:
1587 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1588 0 : -EIO;
1589 default:
1590 return -ENOSYS;
1592 /* not reached */
1595 static int video_read(char *p)
1597 int status, autosw;
1598 int len = 0;
1600 if (video_supported == TPACPI_VIDEO_NONE) {
1601 len += sprintf(p + len, "status:\t\tnot supported\n");
1602 return len;
1605 status = video_outputsw_get();
1606 if (status < 0)
1607 return status;
1609 autosw = video_autosw_get();
1610 if (autosw < 0)
1611 return autosw;
1613 len += sprintf(p + len, "status:\t\tsupported\n");
1614 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1615 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1616 if (video_supported == TPACPI_VIDEO_NEW)
1617 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1618 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1619 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1620 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1621 if (video_supported == TPACPI_VIDEO_NEW)
1622 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1623 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1624 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1626 return len;
1629 static int video_write(char *buf)
1631 char *cmd;
1632 int enable, disable, status;
1633 int res;
1635 if (video_supported == TPACPI_VIDEO_NONE)
1636 return -ENODEV;
1638 enable = 0;
1639 disable = 0;
1641 while ((cmd = next_cmd(&buf))) {
1642 if (strlencmp(cmd, "lcd_enable") == 0) {
1643 enable |= TP_ACPI_VIDEO_S_LCD;
1644 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1645 disable |= TP_ACPI_VIDEO_S_LCD;
1646 } else if (strlencmp(cmd, "crt_enable") == 0) {
1647 enable |= TP_ACPI_VIDEO_S_CRT;
1648 } else if (strlencmp(cmd, "crt_disable") == 0) {
1649 disable |= TP_ACPI_VIDEO_S_CRT;
1650 } else if (video_supported == TPACPI_VIDEO_NEW &&
1651 strlencmp(cmd, "dvi_enable") == 0) {
1652 enable |= TP_ACPI_VIDEO_S_DVI;
1653 } else if (video_supported == TPACPI_VIDEO_NEW &&
1654 strlencmp(cmd, "dvi_disable") == 0) {
1655 disable |= TP_ACPI_VIDEO_S_DVI;
1656 } else if (strlencmp(cmd, "auto_enable") == 0) {
1657 res = video_autosw_set(1);
1658 if (res)
1659 return res;
1660 } else if (strlencmp(cmd, "auto_disable") == 0) {
1661 res = video_autosw_set(0);
1662 if (res)
1663 return res;
1664 } else if (strlencmp(cmd, "video_switch") == 0) {
1665 res = video_outputsw_cycle();
1666 if (res)
1667 return res;
1668 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1669 res = video_expand_toggle();
1670 if (res)
1671 return res;
1672 } else
1673 return -EINVAL;
1676 if (enable || disable) {
1677 status = video_outputsw_get();
1678 if (status < 0)
1679 return status;
1680 res = video_outputsw_set((status & ~disable) | enable);
1681 if (res)
1682 return res;
1685 return 0;
1688 static struct ibm_struct video_driver_data = {
1689 .name = "video",
1690 .read = video_read,
1691 .write = video_write,
1692 .exit = video_exit,
1695 /*************************************************************************
1696 * Light (thinklight) subdriver
1699 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1700 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1702 static int __init light_init(struct ibm_init_struct *iibm)
1704 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1706 IBM_ACPIHANDLE_INIT(ledb);
1707 IBM_ACPIHANDLE_INIT(lght);
1708 IBM_ACPIHANDLE_INIT(cmos);
1710 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1711 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1713 if (tp_features.light)
1714 /* light status not supported on
1715 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1716 tp_features.light_status =
1717 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1719 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1720 str_supported(tp_features.light));
1722 return (tp_features.light)? 0 : 1;
1725 static int light_read(char *p)
1727 int len = 0;
1728 int status = 0;
1730 if (!tp_features.light) {
1731 len += sprintf(p + len, "status:\t\tnot supported\n");
1732 } else if (!tp_features.light_status) {
1733 len += sprintf(p + len, "status:\t\tunknown\n");
1734 len += sprintf(p + len, "commands:\ton, off\n");
1735 } else {
1736 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1737 return -EIO;
1738 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1739 len += sprintf(p + len, "commands:\ton, off\n");
1742 return len;
1745 static int light_write(char *buf)
1747 int cmos_cmd, lght_cmd;
1748 char *cmd;
1749 int success;
1751 if (!tp_features.light)
1752 return -ENODEV;
1754 while ((cmd = next_cmd(&buf))) {
1755 if (strlencmp(cmd, "on") == 0) {
1756 cmos_cmd = 0x0c;
1757 lght_cmd = 1;
1758 } else if (strlencmp(cmd, "off") == 0) {
1759 cmos_cmd = 0x0d;
1760 lght_cmd = 0;
1761 } else
1762 return -EINVAL;
1764 success = cmos_handle ?
1765 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1766 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1767 if (!success)
1768 return -EIO;
1771 return 0;
1774 static struct ibm_struct light_driver_data = {
1775 .name = "light",
1776 .read = light_read,
1777 .write = light_write,
1780 /*************************************************************************
1781 * Dock subdriver
1784 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1786 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1787 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1788 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1789 "\\_SB.PCI.ISA.SLCE", /* 570 */
1790 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1792 /* don't list other alternatives as we install a notify handler on the 570 */
1793 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1795 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1797 .notify = dock_notify,
1798 .handle = &dock_handle,
1799 .type = ACPI_SYSTEM_NOTIFY,
1802 .hid = IBM_PCI_HID,
1803 .notify = dock_notify,
1804 .handle = &pci_handle,
1805 .type = ACPI_SYSTEM_NOTIFY,
1809 static struct ibm_struct dock_driver_data[2] = {
1811 .name = "dock",
1812 .read = dock_read,
1813 .write = dock_write,
1814 .acpi = &ibm_dock_acpidriver[0],
1817 .name = "dock",
1818 .acpi = &ibm_dock_acpidriver[1],
1822 #define dock_docked() (_sta(dock_handle) & 1)
1824 static int __init dock_init(struct ibm_init_struct *iibm)
1826 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1828 IBM_ACPIHANDLE_INIT(dock);
1830 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1831 str_supported(dock_handle != NULL));
1833 return (dock_handle)? 0 : 1;
1836 static int __init dock_init2(struct ibm_init_struct *iibm)
1838 int dock2_needed;
1840 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1842 if (dock_driver_data[0].flags.acpi_driver_registered &&
1843 dock_driver_data[0].flags.acpi_notify_installed) {
1844 IBM_ACPIHANDLE_INIT(pci);
1845 dock2_needed = (pci_handle != NULL);
1846 vdbg_printk(TPACPI_DBG_INIT,
1847 "dock PCI handler for the TP 570 is %s\n",
1848 str_supported(dock2_needed));
1849 } else {
1850 vdbg_printk(TPACPI_DBG_INIT,
1851 "dock subdriver part 2 not required\n");
1852 dock2_needed = 0;
1855 return (dock2_needed)? 0 : 1;
1858 static void dock_notify(struct ibm_struct *ibm, u32 event)
1860 int docked = dock_docked();
1861 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1863 if (event == 1 && !pci) /* 570 */
1864 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1865 else if (event == 1 && pci) /* 570 */
1866 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1867 else if (event == 3 && docked)
1868 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1869 else if (event == 3 && !docked)
1870 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1871 else if (event == 0 && docked)
1872 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1873 else {
1874 printk(IBM_ERR "unknown dock event %d, status %d\n",
1875 event, _sta(dock_handle));
1876 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1880 static int dock_read(char *p)
1882 int len = 0;
1883 int docked = dock_docked();
1885 if (!dock_handle)
1886 len += sprintf(p + len, "status:\t\tnot supported\n");
1887 else if (!docked)
1888 len += sprintf(p + len, "status:\t\tundocked\n");
1889 else {
1890 len += sprintf(p + len, "status:\t\tdocked\n");
1891 len += sprintf(p + len, "commands:\tdock, undock\n");
1894 return len;
1897 static int dock_write(char *buf)
1899 char *cmd;
1901 if (!dock_docked())
1902 return -ENODEV;
1904 while ((cmd = next_cmd(&buf))) {
1905 if (strlencmp(cmd, "undock") == 0) {
1906 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1907 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1908 return -EIO;
1909 } else if (strlencmp(cmd, "dock") == 0) {
1910 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1911 return -EIO;
1912 } else
1913 return -EINVAL;
1916 return 0;
1919 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1921 /*************************************************************************
1922 * Bay subdriver
1925 #ifdef CONFIG_THINKPAD_ACPI_BAY
1926 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1927 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1928 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1929 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1930 ); /* A21e, R30, R31 */
1931 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1932 "_EJ0", /* all others */
1933 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1934 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1935 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1936 ); /* all others */
1937 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1938 "_EJ0", /* 770x */
1939 ); /* all others */
1941 static int __init bay_init(struct ibm_init_struct *iibm)
1943 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1945 IBM_ACPIHANDLE_INIT(bay);
1946 if (bay_handle)
1947 IBM_ACPIHANDLE_INIT(bay_ej);
1948 IBM_ACPIHANDLE_INIT(bay2);
1949 if (bay2_handle)
1950 IBM_ACPIHANDLE_INIT(bay2_ej);
1952 tp_features.bay_status = bay_handle &&
1953 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1954 tp_features.bay_status2 = bay2_handle &&
1955 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1957 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1958 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1959 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1960 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1962 vdbg_printk(TPACPI_DBG_INIT,
1963 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1964 str_supported(tp_features.bay_status),
1965 str_supported(tp_features.bay_eject),
1966 str_supported(tp_features.bay_status2),
1967 str_supported(tp_features.bay_eject2));
1969 return (tp_features.bay_status || tp_features.bay_eject ||
1970 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1973 static void bay_notify(struct ibm_struct *ibm, u32 event)
1975 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1978 #define bay_occupied(b) (_sta(b##_handle) & 1)
1980 static int bay_read(char *p)
1982 int len = 0;
1983 int occupied = bay_occupied(bay);
1984 int occupied2 = bay_occupied(bay2);
1985 int eject, eject2;
1987 len += sprintf(p + len, "status:\t\t%s\n",
1988 tp_features.bay_status ?
1989 (occupied ? "occupied" : "unoccupied") :
1990 "not supported");
1991 if (tp_features.bay_status2)
1992 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1993 "occupied" : "unoccupied");
1995 eject = tp_features.bay_eject && occupied;
1996 eject2 = tp_features.bay_eject2 && occupied2;
1998 if (eject && eject2)
1999 len += sprintf(p + len, "commands:\teject, eject2\n");
2000 else if (eject)
2001 len += sprintf(p + len, "commands:\teject\n");
2002 else if (eject2)
2003 len += sprintf(p + len, "commands:\teject2\n");
2005 return len;
2008 static int bay_write(char *buf)
2010 char *cmd;
2012 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2013 return -ENODEV;
2015 while ((cmd = next_cmd(&buf))) {
2016 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2017 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2018 return -EIO;
2019 } else if (tp_features.bay_eject2 &&
2020 strlencmp(cmd, "eject2") == 0) {
2021 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2022 return -EIO;
2023 } else
2024 return -EINVAL;
2027 return 0;
2030 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2031 .notify = bay_notify,
2032 .handle = &bay_handle,
2033 .type = ACPI_SYSTEM_NOTIFY,
2036 static struct ibm_struct bay_driver_data = {
2037 .name = "bay",
2038 .read = bay_read,
2039 .write = bay_write,
2040 .acpi = &ibm_bay_acpidriver,
2043 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2045 /*************************************************************************
2046 * CMOS subdriver
2049 /* sysfs cmos_command -------------------------------------------------- */
2050 static ssize_t cmos_command_store(struct device *dev,
2051 struct device_attribute *attr,
2052 const char *buf, size_t count)
2054 unsigned long cmos_cmd;
2055 int res;
2057 if (parse_strtoul(buf, 21, &cmos_cmd))
2058 return -EINVAL;
2060 res = issue_thinkpad_cmos_command(cmos_cmd);
2061 return (res)? res : count;
2064 static struct device_attribute dev_attr_cmos_command =
2065 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2067 /* --------------------------------------------------------------------- */
2069 static int __init cmos_init(struct ibm_init_struct *iibm)
2071 int res;
2073 vdbg_printk(TPACPI_DBG_INIT,
2074 "initializing cmos commands subdriver\n");
2076 IBM_ACPIHANDLE_INIT(cmos);
2078 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2079 str_supported(cmos_handle != NULL));
2081 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2082 if (res)
2083 return res;
2085 return (cmos_handle)? 0 : 1;
2088 static void cmos_exit(void)
2090 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2093 static int cmos_read(char *p)
2095 int len = 0;
2097 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2098 R30, R31, T20-22, X20-21 */
2099 if (!cmos_handle)
2100 len += sprintf(p + len, "status:\t\tnot supported\n");
2101 else {
2102 len += sprintf(p + len, "status:\t\tsupported\n");
2103 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2106 return len;
2109 static int cmos_write(char *buf)
2111 char *cmd;
2112 int cmos_cmd, res;
2114 while ((cmd = next_cmd(&buf))) {
2115 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2116 cmos_cmd >= 0 && cmos_cmd <= 21) {
2117 /* cmos_cmd set */
2118 } else
2119 return -EINVAL;
2121 res = issue_thinkpad_cmos_command(cmos_cmd);
2122 if (res)
2123 return res;
2126 return 0;
2129 static struct ibm_struct cmos_driver_data = {
2130 .name = "cmos",
2131 .read = cmos_read,
2132 .write = cmos_write,
2133 .exit = cmos_exit,
2136 /*************************************************************************
2137 * LED subdriver
2140 static enum led_access_mode led_supported;
2142 IBM_HANDLE(led, ec, "SLED", /* 570 */
2143 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2144 "LED", /* all others */
2145 ); /* R30, R31 */
2147 static int __init led_init(struct ibm_init_struct *iibm)
2149 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2151 IBM_ACPIHANDLE_INIT(led);
2153 if (!led_handle)
2154 /* led not supported on R30, R31 */
2155 led_supported = TPACPI_LED_NONE;
2156 else if (strlencmp(led_path, "SLED") == 0)
2157 /* 570 */
2158 led_supported = TPACPI_LED_570;
2159 else if (strlencmp(led_path, "SYSL") == 0)
2160 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2161 led_supported = TPACPI_LED_OLD;
2162 else
2163 /* all others */
2164 led_supported = TPACPI_LED_NEW;
2166 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2167 str_supported(led_supported), led_supported);
2169 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2172 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2174 static int led_read(char *p)
2176 int len = 0;
2178 if (!led_supported) {
2179 len += sprintf(p + len, "status:\t\tnot supported\n");
2180 return len;
2182 len += sprintf(p + len, "status:\t\tsupported\n");
2184 if (led_supported == TPACPI_LED_570) {
2185 /* 570 */
2186 int i, status;
2187 for (i = 0; i < 8; i++) {
2188 if (!acpi_evalf(ec_handle,
2189 &status, "GLED", "dd", 1 << i))
2190 return -EIO;
2191 len += sprintf(p + len, "%d:\t\t%s\n",
2192 i, led_status(status));
2196 len += sprintf(p + len, "commands:\t"
2197 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2199 return len;
2202 /* off, on, blink */
2203 static const int led_sled_arg1[] = { 0, 1, 3 };
2204 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2205 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2206 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2208 static int led_write(char *buf)
2210 char *cmd;
2211 int led, ind, ret;
2213 if (!led_supported)
2214 return -ENODEV;
2216 while ((cmd = next_cmd(&buf))) {
2217 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2218 return -EINVAL;
2220 if (strstr(cmd, "off")) {
2221 ind = 0;
2222 } else if (strstr(cmd, "on")) {
2223 ind = 1;
2224 } else if (strstr(cmd, "blink")) {
2225 ind = 2;
2226 } else
2227 return -EINVAL;
2229 if (led_supported == TPACPI_LED_570) {
2230 /* 570 */
2231 led = 1 << led;
2232 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2233 led, led_sled_arg1[ind]))
2234 return -EIO;
2235 } else if (led_supported == TPACPI_LED_OLD) {
2236 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2237 led = 1 << led;
2238 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2239 if (ret >= 0)
2240 ret =
2241 ec_write(TPACPI_LED_EC_HLBL,
2242 led * led_exp_hlbl[ind]);
2243 if (ret >= 0)
2244 ret =
2245 ec_write(TPACPI_LED_EC_HLCL,
2246 led * led_exp_hlcl[ind]);
2247 if (ret < 0)
2248 return ret;
2249 } else {
2250 /* all others */
2251 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2252 led, led_led_arg1[ind]))
2253 return -EIO;
2257 return 0;
2260 static struct ibm_struct led_driver_data = {
2261 .name = "led",
2262 .read = led_read,
2263 .write = led_write,
2266 /*************************************************************************
2267 * Beep subdriver
2270 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2272 static int __init beep_init(struct ibm_init_struct *iibm)
2274 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2276 IBM_ACPIHANDLE_INIT(beep);
2278 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2279 str_supported(beep_handle != NULL));
2281 return (beep_handle)? 0 : 1;
2284 static int beep_read(char *p)
2286 int len = 0;
2288 if (!beep_handle)
2289 len += sprintf(p + len, "status:\t\tnot supported\n");
2290 else {
2291 len += sprintf(p + len, "status:\t\tsupported\n");
2292 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2295 return len;
2298 static int beep_write(char *buf)
2300 char *cmd;
2301 int beep_cmd;
2303 if (!beep_handle)
2304 return -ENODEV;
2306 while ((cmd = next_cmd(&buf))) {
2307 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2308 beep_cmd >= 0 && beep_cmd <= 17) {
2309 /* beep_cmd set */
2310 } else
2311 return -EINVAL;
2312 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2313 return -EIO;
2316 return 0;
2319 static struct ibm_struct beep_driver_data = {
2320 .name = "beep",
2321 .read = beep_read,
2322 .write = beep_write,
2325 /*************************************************************************
2326 * Thermal subdriver
2329 static enum thermal_access_mode thermal_read_mode;
2331 /* sysfs temp##_input -------------------------------------------------- */
2333 static ssize_t thermal_temp_input_show(struct device *dev,
2334 struct device_attribute *attr,
2335 char *buf)
2337 struct sensor_device_attribute *sensor_attr =
2338 to_sensor_dev_attr(attr);
2339 int idx = sensor_attr->index;
2340 s32 value;
2341 int res;
2343 res = thermal_get_sensor(idx, &value);
2344 if (res)
2345 return res;
2346 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2347 return -ENXIO;
2349 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2352 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2353 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2355 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2356 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2357 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2358 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2359 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2360 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2361 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2362 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2363 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2364 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2365 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2366 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2367 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2368 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2369 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2370 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2371 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2374 #define THERMAL_ATTRS(X) \
2375 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2377 static struct attribute *thermal_temp_input_attr[] = {
2378 THERMAL_ATTRS(8),
2379 THERMAL_ATTRS(9),
2380 THERMAL_ATTRS(10),
2381 THERMAL_ATTRS(11),
2382 THERMAL_ATTRS(12),
2383 THERMAL_ATTRS(13),
2384 THERMAL_ATTRS(14),
2385 THERMAL_ATTRS(15),
2386 THERMAL_ATTRS(0),
2387 THERMAL_ATTRS(1),
2388 THERMAL_ATTRS(2),
2389 THERMAL_ATTRS(3),
2390 THERMAL_ATTRS(4),
2391 THERMAL_ATTRS(5),
2392 THERMAL_ATTRS(6),
2393 THERMAL_ATTRS(7),
2394 NULL
2397 static const struct attribute_group thermal_temp_input16_group = {
2398 .attrs = thermal_temp_input_attr
2401 static const struct attribute_group thermal_temp_input8_group = {
2402 .attrs = &thermal_temp_input_attr[8]
2405 #undef THERMAL_SENSOR_ATTR_TEMP
2406 #undef THERMAL_ATTRS
2408 /* --------------------------------------------------------------------- */
2410 static int __init thermal_init(struct ibm_init_struct *iibm)
2412 u8 t, ta1, ta2;
2413 int i;
2414 int acpi_tmp7;
2415 int res;
2417 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2419 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2421 if (ibm_thinkpad_ec_found && experimental) {
2423 * Direct EC access mode: sensors at registers
2424 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2425 * non-implemented, thermal sensors return 0x80 when
2426 * not available
2429 ta1 = ta2 = 0;
2430 for (i = 0; i < 8; i++) {
2431 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2432 ta1 |= t;
2433 } else {
2434 ta1 = 0;
2435 break;
2437 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2438 ta2 |= t;
2439 } else {
2440 ta1 = 0;
2441 break;
2444 if (ta1 == 0) {
2445 /* This is sheer paranoia, but we handle it anyway */
2446 if (acpi_tmp7) {
2447 printk(IBM_ERR
2448 "ThinkPad ACPI EC access misbehaving, "
2449 "falling back to ACPI TMPx access mode\n");
2450 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2451 } else {
2452 printk(IBM_ERR
2453 "ThinkPad ACPI EC access misbehaving, "
2454 "disabling thermal sensors access\n");
2455 thermal_read_mode = TPACPI_THERMAL_NONE;
2457 } else {
2458 thermal_read_mode =
2459 (ta2 != 0) ?
2460 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2462 } else if (acpi_tmp7) {
2463 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2464 /* 600e/x, 770e, 770x */
2465 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2466 } else {
2467 /* Standard ACPI TMPx access, max 8 sensors */
2468 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2470 } else {
2471 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2472 thermal_read_mode = TPACPI_THERMAL_NONE;
2475 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2476 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2477 thermal_read_mode);
2479 switch(thermal_read_mode) {
2480 case TPACPI_THERMAL_TPEC_16:
2481 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2482 &thermal_temp_input16_group);
2483 if (res)
2484 return res;
2485 break;
2486 case TPACPI_THERMAL_TPEC_8:
2487 case TPACPI_THERMAL_ACPI_TMP07:
2488 case TPACPI_THERMAL_ACPI_UPDT:
2489 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2490 &thermal_temp_input8_group);
2491 if (res)
2492 return res;
2493 break;
2494 case TPACPI_THERMAL_NONE:
2495 default:
2496 return 1;
2499 return 0;
2502 static void thermal_exit(void)
2504 switch(thermal_read_mode) {
2505 case TPACPI_THERMAL_TPEC_16:
2506 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2507 &thermal_temp_input16_group);
2508 break;
2509 case TPACPI_THERMAL_TPEC_8:
2510 case TPACPI_THERMAL_ACPI_TMP07:
2511 case TPACPI_THERMAL_ACPI_UPDT:
2512 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2513 &thermal_temp_input16_group);
2514 break;
2515 case TPACPI_THERMAL_NONE:
2516 default:
2517 break;
2521 /* idx is zero-based */
2522 static int thermal_get_sensor(int idx, s32 *value)
2524 int t;
2525 s8 tmp;
2526 char tmpi[5];
2528 t = TP_EC_THERMAL_TMP0;
2530 switch (thermal_read_mode) {
2531 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2532 case TPACPI_THERMAL_TPEC_16:
2533 if (idx >= 8 && idx <= 15) {
2534 t = TP_EC_THERMAL_TMP8;
2535 idx -= 8;
2537 /* fallthrough */
2538 #endif
2539 case TPACPI_THERMAL_TPEC_8:
2540 if (idx <= 7) {
2541 if (!acpi_ec_read(t + idx, &tmp))
2542 return -EIO;
2543 *value = tmp * 1000;
2544 return 0;
2546 break;
2548 case TPACPI_THERMAL_ACPI_UPDT:
2549 if (idx <= 7) {
2550 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2551 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2552 return -EIO;
2553 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2554 return -EIO;
2555 *value = (t - 2732) * 100;
2556 return 0;
2558 break;
2560 case TPACPI_THERMAL_ACPI_TMP07:
2561 if (idx <= 7) {
2562 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2563 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2564 return -EIO;
2565 *value = t * 1000;
2566 return 0;
2568 break;
2570 case TPACPI_THERMAL_NONE:
2571 default:
2572 return -ENOSYS;
2575 return -EINVAL;
2578 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2580 int res, i;
2581 int n;
2583 n = 8;
2584 i = 0;
2586 if (!s)
2587 return -EINVAL;
2589 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2590 n = 16;
2592 for(i = 0 ; i < n; i++) {
2593 res = thermal_get_sensor(i, &s->temp[i]);
2594 if (res)
2595 return res;
2598 return n;
2601 static int thermal_read(char *p)
2603 int len = 0;
2604 int n, i;
2605 struct ibm_thermal_sensors_struct t;
2607 n = thermal_get_sensors(&t);
2608 if (unlikely(n < 0))
2609 return n;
2611 len += sprintf(p + len, "temperatures:\t");
2613 if (n > 0) {
2614 for (i = 0; i < (n - 1); i++)
2615 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2616 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2617 } else
2618 len += sprintf(p + len, "not supported\n");
2620 return len;
2623 static struct ibm_struct thermal_driver_data = {
2624 .name = "thermal",
2625 .read = thermal_read,
2626 .exit = thermal_exit,
2629 /*************************************************************************
2630 * EC Dump subdriver
2633 static u8 ecdump_regs[256];
2635 static int ecdump_read(char *p)
2637 int len = 0;
2638 int i, j;
2639 u8 v;
2641 len += sprintf(p + len, "EC "
2642 " +00 +01 +02 +03 +04 +05 +06 +07"
2643 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2644 for (i = 0; i < 256; i += 16) {
2645 len += sprintf(p + len, "EC 0x%02x:", i);
2646 for (j = 0; j < 16; j++) {
2647 if (!acpi_ec_read(i + j, &v))
2648 break;
2649 if (v != ecdump_regs[i + j])
2650 len += sprintf(p + len, " *%02x", v);
2651 else
2652 len += sprintf(p + len, " %02x", v);
2653 ecdump_regs[i + j] = v;
2655 len += sprintf(p + len, "\n");
2656 if (j != 16)
2657 break;
2660 /* These are way too dangerous to advertise openly... */
2661 #if 0
2662 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2663 " (<offset> is 00-ff, <value> is 00-ff)\n");
2664 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2665 " (<offset> is 00-ff, <value> is 0-255)\n");
2666 #endif
2667 return len;
2670 static int ecdump_write(char *buf)
2672 char *cmd;
2673 int i, v;
2675 while ((cmd = next_cmd(&buf))) {
2676 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2677 /* i and v set */
2678 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2679 /* i and v set */
2680 } else
2681 return -EINVAL;
2682 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2683 if (!acpi_ec_write(i, v))
2684 return -EIO;
2685 } else
2686 return -EINVAL;
2689 return 0;
2692 static struct ibm_struct ecdump_driver_data = {
2693 .name = "ecdump",
2694 .read = ecdump_read,
2695 .write = ecdump_write,
2696 .flags.experimental = 1,
2699 /*************************************************************************
2700 * Backlight/brightness subdriver
2703 static struct backlight_device *ibm_backlight_device;
2705 static struct backlight_ops ibm_backlight_data = {
2706 .get_brightness = brightness_get,
2707 .update_status = brightness_update_status,
2710 static int __init brightness_init(struct ibm_init_struct *iibm)
2712 int b;
2714 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2716 b = brightness_get(NULL);
2717 if (b < 0)
2718 return b;
2720 ibm_backlight_device = backlight_device_register(
2721 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2722 &ibm_backlight_data);
2723 if (IS_ERR(ibm_backlight_device)) {
2724 printk(IBM_ERR "Could not register backlight device\n");
2725 return PTR_ERR(ibm_backlight_device);
2727 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2729 ibm_backlight_device->props.max_brightness = 7;
2730 ibm_backlight_device->props.brightness = b;
2731 backlight_update_status(ibm_backlight_device);
2733 return 0;
2736 static void brightness_exit(void)
2738 if (ibm_backlight_device) {
2739 vdbg_printk(TPACPI_DBG_EXIT,
2740 "calling backlight_device_unregister()\n");
2741 backlight_device_unregister(ibm_backlight_device);
2742 ibm_backlight_device = NULL;
2746 static int brightness_update_status(struct backlight_device *bd)
2748 return brightness_set(
2749 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2750 bd->props.power == FB_BLANK_UNBLANK) ?
2751 bd->props.brightness : 0);
2754 static int brightness_get(struct backlight_device *bd)
2756 u8 level;
2757 if (!acpi_ec_read(brightness_offset, &level))
2758 return -EIO;
2760 level &= 0x7;
2762 return level;
2765 static int brightness_set(int value)
2767 int cmos_cmd, inc, i;
2768 int current_value = brightness_get(NULL);
2770 value &= 7;
2772 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2773 inc = value > current_value ? 1 : -1;
2774 for (i = current_value; i != value; i += inc) {
2775 if (issue_thinkpad_cmos_command(cmos_cmd))
2776 return -EIO;
2777 if (!acpi_ec_write(brightness_offset, i + inc))
2778 return -EIO;
2781 return 0;
2784 static int brightness_read(char *p)
2786 int len = 0;
2787 int level;
2789 if ((level = brightness_get(NULL)) < 0) {
2790 len += sprintf(p + len, "level:\t\tunreadable\n");
2791 } else {
2792 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2793 len += sprintf(p + len, "commands:\tup, down\n");
2794 len += sprintf(p + len, "commands:\tlevel <level>"
2795 " (<level> is 0-7)\n");
2798 return len;
2801 static int brightness_write(char *buf)
2803 int level;
2804 int new_level;
2805 char *cmd;
2807 while ((cmd = next_cmd(&buf))) {
2808 if ((level = brightness_get(NULL)) < 0)
2809 return level;
2810 level &= 7;
2812 if (strlencmp(cmd, "up") == 0) {
2813 new_level = level == 7 ? 7 : level + 1;
2814 } else if (strlencmp(cmd, "down") == 0) {
2815 new_level = level == 0 ? 0 : level - 1;
2816 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2817 new_level >= 0 && new_level <= 7) {
2818 /* new_level set */
2819 } else
2820 return -EINVAL;
2822 brightness_set(new_level);
2825 return 0;
2828 static struct ibm_struct brightness_driver_data = {
2829 .name = "brightness",
2830 .read = brightness_read,
2831 .write = brightness_write,
2832 .exit = brightness_exit,
2835 /*************************************************************************
2836 * Volume subdriver
2839 static int volume_read(char *p)
2841 int len = 0;
2842 u8 level;
2844 if (!acpi_ec_read(volume_offset, &level)) {
2845 len += sprintf(p + len, "level:\t\tunreadable\n");
2846 } else {
2847 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2848 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2849 len += sprintf(p + len, "commands:\tup, down, mute\n");
2850 len += sprintf(p + len, "commands:\tlevel <level>"
2851 " (<level> is 0-15)\n");
2854 return len;
2857 static int volume_write(char *buf)
2859 int cmos_cmd, inc, i;
2860 u8 level, mute;
2861 int new_level, new_mute;
2862 char *cmd;
2864 while ((cmd = next_cmd(&buf))) {
2865 if (!acpi_ec_read(volume_offset, &level))
2866 return -EIO;
2867 new_mute = mute = level & 0x40;
2868 new_level = level = level & 0xf;
2870 if (strlencmp(cmd, "up") == 0) {
2871 if (mute)
2872 new_mute = 0;
2873 else
2874 new_level = level == 15 ? 15 : level + 1;
2875 } else if (strlencmp(cmd, "down") == 0) {
2876 if (mute)
2877 new_mute = 0;
2878 else
2879 new_level = level == 0 ? 0 : level - 1;
2880 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2881 new_level >= 0 && new_level <= 15) {
2882 /* new_level set */
2883 } else if (strlencmp(cmd, "mute") == 0) {
2884 new_mute = 0x40;
2885 } else
2886 return -EINVAL;
2888 if (new_level != level) { /* mute doesn't change */
2889 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2890 inc = new_level > level ? 1 : -1;
2892 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2893 !acpi_ec_write(volume_offset, level)))
2894 return -EIO;
2896 for (i = level; i != new_level; i += inc)
2897 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2898 !acpi_ec_write(volume_offset, i + inc))
2899 return -EIO;
2901 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2902 !acpi_ec_write(volume_offset,
2903 new_level + mute)))
2904 return -EIO;
2907 if (new_mute != mute) { /* level doesn't change */
2908 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2910 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2911 !acpi_ec_write(volume_offset, level + new_mute))
2912 return -EIO;
2916 return 0;
2919 static struct ibm_struct volume_driver_data = {
2920 .name = "volume",
2921 .read = volume_read,
2922 .write = volume_write,
2925 /*************************************************************************
2926 * Fan subdriver
2930 * FAN ACCESS MODES
2932 * TPACPI_FAN_RD_ACPI_GFAN:
2933 * ACPI GFAN method: returns fan level
2935 * see TPACPI_FAN_WR_ACPI_SFAN
2936 * EC 0x2f (HFSP) not available if GFAN exists
2938 * TPACPI_FAN_WR_ACPI_SFAN:
2939 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2941 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2942 * it for writing.
2944 * TPACPI_FAN_WR_TPEC:
2945 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2946 * Supported on almost all ThinkPads
2948 * Fan speed changes of any sort (including those caused by the
2949 * disengaged mode) are usually done slowly by the firmware as the
2950 * maximum ammount of fan duty cycle change per second seems to be
2951 * limited.
2953 * Reading is not available if GFAN exists.
2954 * Writing is not available if SFAN exists.
2956 * Bits
2957 * 7 automatic mode engaged;
2958 * (default operation mode of the ThinkPad)
2959 * fan level is ignored in this mode.
2960 * 6 full speed mode (takes precedence over bit 7);
2961 * not available on all thinkpads. May disable
2962 * the tachometer while the fan controller ramps up
2963 * the speed (which can take up to a few *minutes*).
2964 * Speeds up fan to 100% duty-cycle, which is far above
2965 * the standard RPM levels. It is not impossible that
2966 * it could cause hardware damage.
2967 * 5-3 unused in some models. Extra bits for fan level
2968 * in others, but still useless as all values above
2969 * 7 map to the same speed as level 7 in these models.
2970 * 2-0 fan level (0..7 usually)
2971 * 0x00 = stop
2972 * 0x07 = max (set when temperatures critical)
2973 * Some ThinkPads may have other levels, see
2974 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2976 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2977 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2978 * does so, its initial value is meaningless (0x07).
2980 * For firmware bugs, refer to:
2981 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2983 * ----
2985 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2986 * Main fan tachometer reading (in RPM)
2988 * This register is present on all ThinkPads with a new-style EC, and
2989 * it is known not to be present on the A21m/e, and T22, as there is
2990 * something else in offset 0x84 according to the ACPI DSDT. Other
2991 * ThinkPads from this same time period (and earlier) probably lack the
2992 * tachometer as well.
2994 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2995 * was never fixed by IBM to report the EC firmware version string
2996 * probably support the tachometer (like the early X models), so
2997 * detecting it is quite hard. We need more data to know for sure.
2999 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3000 * might result.
3002 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3003 * mode.
3005 * For firmware bugs, refer to:
3006 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3008 * TPACPI_FAN_WR_ACPI_FANS:
3009 * ThinkPad X31, X40, X41. Not available in the X60.
3011 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3012 * high speed. ACPI DSDT seems to map these three speeds to levels
3013 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3014 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3016 * The speeds are stored on handles
3017 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3019 * There are three default speed sets, acessible as handles:
3020 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3022 * ACPI DSDT switches which set is in use depending on various
3023 * factors.
3025 * TPACPI_FAN_WR_TPEC is also available and should be used to
3026 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3027 * but the ACPI tables just mention level 7.
3030 static enum fan_status_access_mode fan_status_access_mode;
3031 static enum fan_control_access_mode fan_control_access_mode;
3032 static enum fan_control_commands fan_control_commands;
3034 static u8 fan_control_initial_status;
3035 static u8 fan_control_desired_level;
3037 static void fan_watchdog_fire(struct work_struct *ignored);
3038 static int fan_watchdog_maxinterval;
3039 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3041 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3042 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3043 "\\FSPD", /* 600e/x, 770e, 770x */
3044 ); /* all others */
3045 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3046 "JFNS", /* 770x-JL */
3047 ); /* all others */
3050 * SYSFS fan layout: hwmon compatible (device)
3052 * pwm*_enable:
3053 * 0: "disengaged" mode
3054 * 1: manual mode
3055 * 2: native EC "auto" mode (recommended, hardware default)
3057 * pwm*: set speed in manual mode, ignored otherwise.
3058 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3059 * interpolation.
3061 * fan*_input: tachometer reading, RPM
3064 * SYSFS fan layout: extensions
3066 * fan_watchdog (driver):
3067 * fan watchdog interval in seconds, 0 disables (default), max 120
3070 /* sysfs fan pwm1_enable ----------------------------------------------- */
3071 static ssize_t fan_pwm1_enable_show(struct device *dev,
3072 struct device_attribute *attr,
3073 char *buf)
3075 int res, mode;
3076 u8 status;
3078 res = fan_get_status_safe(&status);
3079 if (res)
3080 return res;
3082 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3083 if (status != fan_control_initial_status) {
3084 tp_features.fan_ctrl_status_undef = 0;
3085 } else {
3086 /* Return most likely status. In fact, it
3087 * might be the only possible status */
3088 status = TP_EC_FAN_AUTO;
3092 if (status & TP_EC_FAN_FULLSPEED) {
3093 mode = 0;
3094 } else if (status & TP_EC_FAN_AUTO) {
3095 mode = 2;
3096 } else
3097 mode = 1;
3099 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3102 static ssize_t fan_pwm1_enable_store(struct device *dev,
3103 struct device_attribute *attr,
3104 const char *buf, size_t count)
3106 unsigned long t;
3107 int res, level;
3109 if (parse_strtoul(buf, 2, &t))
3110 return -EINVAL;
3112 switch (t) {
3113 case 0:
3114 level = TP_EC_FAN_FULLSPEED;
3115 break;
3116 case 1:
3117 level = TPACPI_FAN_LAST_LEVEL;
3118 break;
3119 case 2:
3120 level = TP_EC_FAN_AUTO;
3121 break;
3122 case 3:
3123 /* reserved for software-controlled auto mode */
3124 return -ENOSYS;
3125 default:
3126 return -EINVAL;
3129 res = fan_set_level_safe(level);
3130 if (res == -ENXIO)
3131 return -EINVAL;
3132 else if (res < 0)
3133 return res;
3135 fan_watchdog_reset();
3137 return count;
3140 static struct device_attribute dev_attr_fan_pwm1_enable =
3141 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3142 fan_pwm1_enable_show, fan_pwm1_enable_store);
3144 /* sysfs fan pwm1 ------------------------------------------------------ */
3145 static ssize_t fan_pwm1_show(struct device *dev,
3146 struct device_attribute *attr,
3147 char *buf)
3149 int res;
3150 u8 status;
3152 res = fan_get_status_safe(&status);
3153 if (res)
3154 return res;
3156 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3157 if (status != fan_control_initial_status) {
3158 tp_features.fan_ctrl_status_undef = 0;
3159 } else {
3160 status = TP_EC_FAN_AUTO;
3164 if ((status &
3165 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3166 status = fan_control_desired_level;
3168 if (status > 7)
3169 status = 7;
3171 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3174 static ssize_t fan_pwm1_store(struct device *dev,
3175 struct device_attribute *attr,
3176 const char *buf, size_t count)
3178 unsigned long s;
3179 int rc;
3180 u8 status, newlevel;
3182 if (parse_strtoul(buf, 255, &s))
3183 return -EINVAL;
3185 /* scale down from 0-255 to 0-7 */
3186 newlevel = (s >> 5) & 0x07;
3188 rc = mutex_lock_interruptible(&fan_mutex);
3189 if (rc < 0)
3190 return rc;
3192 rc = fan_get_status(&status);
3193 if (!rc && (status &
3194 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3195 rc = fan_set_level(newlevel);
3196 if (rc == -ENXIO)
3197 rc = -EINVAL;
3198 else if (!rc) {
3199 fan_update_desired_level(newlevel);
3200 fan_watchdog_reset();
3204 mutex_unlock(&fan_mutex);
3205 return (rc)? rc : count;
3208 static struct device_attribute dev_attr_fan_pwm1 =
3209 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3210 fan_pwm1_show, fan_pwm1_store);
3212 /* sysfs fan fan1_input ------------------------------------------------ */
3213 static ssize_t fan_fan1_input_show(struct device *dev,
3214 struct device_attribute *attr,
3215 char *buf)
3217 int res;
3218 unsigned int speed;
3220 res = fan_get_speed(&speed);
3221 if (res < 0)
3222 return res;
3224 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3227 static struct device_attribute dev_attr_fan_fan1_input =
3228 __ATTR(fan1_input, S_IRUGO,
3229 fan_fan1_input_show, NULL);
3231 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3232 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3233 char *buf)
3235 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3238 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3239 const char *buf, size_t count)
3241 unsigned long t;
3243 if (parse_strtoul(buf, 120, &t))
3244 return -EINVAL;
3246 if (!fan_control_allowed)
3247 return -EPERM;
3249 fan_watchdog_maxinterval = t;
3250 fan_watchdog_reset();
3252 return count;
3255 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3256 fan_fan_watchdog_show, fan_fan_watchdog_store);
3258 /* --------------------------------------------------------------------- */
3259 static struct attribute *fan_attributes[] = {
3260 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3261 &dev_attr_fan_fan1_input.attr,
3262 NULL
3265 static const struct attribute_group fan_attr_group = {
3266 .attrs = fan_attributes,
3269 static int __init fan_init(struct ibm_init_struct *iibm)
3271 int rc;
3273 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3275 mutex_init(&fan_mutex);
3276 fan_status_access_mode = TPACPI_FAN_NONE;
3277 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3278 fan_control_commands = 0;
3279 fan_watchdog_maxinterval = 0;
3280 tp_features.fan_ctrl_status_undef = 0;
3281 fan_control_desired_level = 7;
3283 IBM_ACPIHANDLE_INIT(fans);
3284 IBM_ACPIHANDLE_INIT(gfan);
3285 IBM_ACPIHANDLE_INIT(sfan);
3287 if (gfan_handle) {
3288 /* 570, 600e/x, 770e, 770x */
3289 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3290 } else {
3291 /* all other ThinkPads: note that even old-style
3292 * ThinkPad ECs supports the fan control register */
3293 if (likely(acpi_ec_read(fan_status_offset,
3294 &fan_control_initial_status))) {
3295 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3297 /* In some ThinkPads, neither the EC nor the ACPI
3298 * DSDT initialize the fan status, and it ends up
3299 * being set to 0x07 when it *could* be either
3300 * 0x07 or 0x80.
3302 * Enable for TP-1Y (T43), TP-78 (R51e),
3303 * TP-76 (R52), TP-70 (T43, R52), which are known
3304 * to be buggy. */
3305 if (fan_control_initial_status == 0x07 &&
3306 ibm_thinkpad_ec_found &&
3307 ((ibm_thinkpad_ec_found[0] == '1' &&
3308 ibm_thinkpad_ec_found[1] == 'Y') ||
3309 (ibm_thinkpad_ec_found[0] == '7' &&
3310 (ibm_thinkpad_ec_found[1] == '6' ||
3311 ibm_thinkpad_ec_found[1] == '8' ||
3312 ibm_thinkpad_ec_found[1] == '0'))
3313 )) {
3314 printk(IBM_NOTICE
3315 "fan_init: initial fan status is "
3316 "unknown, assuming it is in auto "
3317 "mode\n");
3318 tp_features.fan_ctrl_status_undef = 1;
3320 } else {
3321 printk(IBM_ERR
3322 "ThinkPad ACPI EC access misbehaving, "
3323 "fan status and control unavailable\n");
3324 return 1;
3328 if (sfan_handle) {
3329 /* 570, 770x-JL */
3330 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3331 fan_control_commands |=
3332 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3333 } else {
3334 if (!gfan_handle) {
3335 /* gfan without sfan means no fan control */
3336 /* all other models implement TP EC 0x2f control */
3338 if (fans_handle) {
3339 /* X31, X40, X41 */
3340 fan_control_access_mode =
3341 TPACPI_FAN_WR_ACPI_FANS;
3342 fan_control_commands |=
3343 TPACPI_FAN_CMD_SPEED |
3344 TPACPI_FAN_CMD_LEVEL |
3345 TPACPI_FAN_CMD_ENABLE;
3346 } else {
3347 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3348 fan_control_commands |=
3349 TPACPI_FAN_CMD_LEVEL |
3350 TPACPI_FAN_CMD_ENABLE;
3355 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3356 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3357 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3358 fan_status_access_mode, fan_control_access_mode);
3360 /* fan control master switch */
3361 if (!fan_control_allowed) {
3362 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3363 fan_control_commands = 0;
3364 dbg_printk(TPACPI_DBG_INIT,
3365 "fan control features disabled by parameter\n");
3368 /* update fan_control_desired_level */
3369 if (fan_status_access_mode != TPACPI_FAN_NONE)
3370 fan_get_status_safe(NULL);
3372 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3373 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3374 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3375 &fan_attr_group);
3376 if (!(rc < 0))
3377 rc = driver_create_file(&tpacpi_pdriver.driver,
3378 &driver_attr_fan_watchdog);
3379 if (rc < 0)
3380 return rc;
3381 return 0;
3382 } else
3383 return 1;
3387 * Call with fan_mutex held
3389 static void fan_update_desired_level(u8 status)
3391 if ((status &
3392 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3393 if (status > 7)
3394 fan_control_desired_level = 7;
3395 else
3396 fan_control_desired_level = status;
3400 static int fan_get_status(u8 *status)
3402 u8 s;
3404 /* TODO:
3405 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3407 switch (fan_status_access_mode) {
3408 case TPACPI_FAN_RD_ACPI_GFAN:
3409 /* 570, 600e/x, 770e, 770x */
3411 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3412 return -EIO;
3414 if (likely(status))
3415 *status = s & 0x07;
3417 break;
3419 case TPACPI_FAN_RD_TPEC:
3420 /* all except 570, 600e/x, 770e, 770x */
3421 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3422 return -EIO;
3424 if (likely(status))
3425 *status = s;
3427 break;
3429 default:
3430 return -ENXIO;
3433 return 0;
3436 static int fan_get_status_safe(u8 *status)
3438 int rc;
3439 u8 s;
3441 rc = mutex_lock_interruptible(&fan_mutex);
3442 if (rc < 0)
3443 return rc;
3444 rc = fan_get_status(&s);
3445 if (!rc)
3446 fan_update_desired_level(s);
3447 mutex_unlock(&fan_mutex);
3449 if (status)
3450 *status = s;
3452 return rc;
3455 static void fan_exit(void)
3457 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3459 /* FIXME: can we really do this unconditionally? */
3460 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3461 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3463 cancel_delayed_work(&fan_watchdog_task);
3464 flush_scheduled_work();
3467 static int fan_get_speed(unsigned int *speed)
3469 u8 hi, lo;
3471 switch (fan_status_access_mode) {
3472 case TPACPI_FAN_RD_TPEC:
3473 /* all except 570, 600e/x, 770e, 770x */
3474 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3475 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3476 return -EIO;
3478 if (likely(speed))
3479 *speed = (hi << 8) | lo;
3481 break;
3483 default:
3484 return -ENXIO;
3487 return 0;
3490 static void fan_watchdog_fire(struct work_struct *ignored)
3492 int rc;
3494 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3495 rc = fan_set_enable();
3496 if (rc < 0) {
3497 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3498 "will try again later...\n", -rc);
3499 /* reschedule for later */
3500 fan_watchdog_reset();
3504 static void fan_watchdog_reset(void)
3506 static int fan_watchdog_active;
3508 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3509 return;
3511 if (fan_watchdog_active)
3512 cancel_delayed_work(&fan_watchdog_task);
3514 if (fan_watchdog_maxinterval > 0) {
3515 fan_watchdog_active = 1;
3516 if (!schedule_delayed_work(&fan_watchdog_task,
3517 msecs_to_jiffies(fan_watchdog_maxinterval
3518 * 1000))) {
3519 printk(IBM_ERR "failed to schedule the fan watchdog, "
3520 "watchdog will not trigger\n");
3522 } else
3523 fan_watchdog_active = 0;
3526 static int fan_set_level(int level)
3528 if (!fan_control_allowed)
3529 return -EPERM;
3531 switch (fan_control_access_mode) {
3532 case TPACPI_FAN_WR_ACPI_SFAN:
3533 if (level >= 0 && level <= 7) {
3534 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3535 return -EIO;
3536 } else
3537 return -EINVAL;
3538 break;
3540 case TPACPI_FAN_WR_ACPI_FANS:
3541 case TPACPI_FAN_WR_TPEC:
3542 if ((level != TP_EC_FAN_AUTO) &&
3543 (level != TP_EC_FAN_FULLSPEED) &&
3544 ((level < 0) || (level > 7)))
3545 return -EINVAL;
3547 /* safety net should the EC not support AUTO
3548 * or FULLSPEED mode bits and just ignore them */
3549 if (level & TP_EC_FAN_FULLSPEED)
3550 level |= 7; /* safety min speed 7 */
3551 else if (level & TP_EC_FAN_FULLSPEED)
3552 level |= 4; /* safety min speed 4 */
3554 if (!acpi_ec_write(fan_status_offset, level))
3555 return -EIO;
3556 else
3557 tp_features.fan_ctrl_status_undef = 0;
3558 break;
3560 default:
3561 return -ENXIO;
3563 return 0;
3566 static int fan_set_level_safe(int level)
3568 int rc;
3570 if (!fan_control_allowed)
3571 return -EPERM;
3573 rc = mutex_lock_interruptible(&fan_mutex);
3574 if (rc < 0)
3575 return rc;
3577 if (level == TPACPI_FAN_LAST_LEVEL)
3578 level = fan_control_desired_level;
3580 rc = fan_set_level(level);
3581 if (!rc)
3582 fan_update_desired_level(level);
3584 mutex_unlock(&fan_mutex);
3585 return rc;
3588 static int fan_set_enable(void)
3590 u8 s;
3591 int rc;
3593 if (!fan_control_allowed)
3594 return -EPERM;
3596 rc = mutex_lock_interruptible(&fan_mutex);
3597 if (rc < 0)
3598 return rc;
3600 switch (fan_control_access_mode) {
3601 case TPACPI_FAN_WR_ACPI_FANS:
3602 case TPACPI_FAN_WR_TPEC:
3603 rc = fan_get_status(&s);
3604 if (rc < 0)
3605 break;
3607 /* Don't go out of emergency fan mode */
3608 if (s != 7) {
3609 s &= 0x07;
3610 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3613 if (!acpi_ec_write(fan_status_offset, s))
3614 rc = -EIO;
3615 else {
3616 tp_features.fan_ctrl_status_undef = 0;
3617 rc = 0;
3619 break;
3621 case TPACPI_FAN_WR_ACPI_SFAN:
3622 rc = fan_get_status(&s);
3623 if (rc < 0)
3624 break;
3626 s &= 0x07;
3628 /* Set fan to at least level 4 */
3629 s |= 4;
3631 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3632 rc= -EIO;
3633 else
3634 rc = 0;
3635 break;
3637 default:
3638 rc = -ENXIO;
3641 mutex_unlock(&fan_mutex);
3642 return rc;
3645 static int fan_set_disable(void)
3647 int rc;
3649 if (!fan_control_allowed)
3650 return -EPERM;
3652 rc = mutex_lock_interruptible(&fan_mutex);
3653 if (rc < 0)
3654 return rc;
3656 rc = 0;
3657 switch (fan_control_access_mode) {
3658 case TPACPI_FAN_WR_ACPI_FANS:
3659 case TPACPI_FAN_WR_TPEC:
3660 if (!acpi_ec_write(fan_status_offset, 0x00))
3661 rc = -EIO;
3662 else {
3663 fan_control_desired_level = 0;
3664 tp_features.fan_ctrl_status_undef = 0;
3666 break;
3668 case TPACPI_FAN_WR_ACPI_SFAN:
3669 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3670 rc = -EIO;
3671 else
3672 fan_control_desired_level = 0;
3673 break;
3675 default:
3676 rc = -ENXIO;
3680 mutex_unlock(&fan_mutex);
3681 return rc;
3684 static int fan_set_speed(int speed)
3686 int rc;
3688 if (!fan_control_allowed)
3689 return -EPERM;
3691 rc = mutex_lock_interruptible(&fan_mutex);
3692 if (rc < 0)
3693 return rc;
3695 rc = 0;
3696 switch (fan_control_access_mode) {
3697 case TPACPI_FAN_WR_ACPI_FANS:
3698 if (speed >= 0 && speed <= 65535) {
3699 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3700 speed, speed, speed))
3701 rc = -EIO;
3702 } else
3703 rc = -EINVAL;
3704 break;
3706 default:
3707 rc = -ENXIO;
3710 mutex_unlock(&fan_mutex);
3711 return rc;
3714 static int fan_read(char *p)
3716 int len = 0;
3717 int rc;
3718 u8 status;
3719 unsigned int speed = 0;
3721 switch (fan_status_access_mode) {
3722 case TPACPI_FAN_RD_ACPI_GFAN:
3723 /* 570, 600e/x, 770e, 770x */
3724 if ((rc = fan_get_status_safe(&status)) < 0)
3725 return rc;
3727 len += sprintf(p + len, "status:\t\t%s\n"
3728 "level:\t\t%d\n",
3729 (status != 0) ? "enabled" : "disabled", status);
3730 break;
3732 case TPACPI_FAN_RD_TPEC:
3733 /* all except 570, 600e/x, 770e, 770x */
3734 if ((rc = fan_get_status_safe(&status)) < 0)
3735 return rc;
3737 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3738 if (status != fan_control_initial_status)
3739 tp_features.fan_ctrl_status_undef = 0;
3740 else
3741 /* Return most likely status. In fact, it
3742 * might be the only possible status */
3743 status = TP_EC_FAN_AUTO;
3746 len += sprintf(p + len, "status:\t\t%s\n",
3747 (status != 0) ? "enabled" : "disabled");
3749 if ((rc = fan_get_speed(&speed)) < 0)
3750 return rc;
3752 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3754 if (status & TP_EC_FAN_FULLSPEED)
3755 /* Disengaged mode takes precedence */
3756 len += sprintf(p + len, "level:\t\tdisengaged\n");
3757 else if (status & TP_EC_FAN_AUTO)
3758 len += sprintf(p + len, "level:\t\tauto\n");
3759 else
3760 len += sprintf(p + len, "level:\t\t%d\n", status);
3761 break;
3763 case TPACPI_FAN_NONE:
3764 default:
3765 len += sprintf(p + len, "status:\t\tnot supported\n");
3768 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3769 len += sprintf(p + len, "commands:\tlevel <level>");
3771 switch (fan_control_access_mode) {
3772 case TPACPI_FAN_WR_ACPI_SFAN:
3773 len += sprintf(p + len, " (<level> is 0-7)\n");
3774 break;
3776 default:
3777 len += sprintf(p + len, " (<level> is 0-7, "
3778 "auto, disengaged, full-speed)\n");
3779 break;
3783 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3784 len += sprintf(p + len, "commands:\tenable, disable\n"
3785 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3786 "1-120 (seconds))\n");
3788 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3789 len += sprintf(p + len, "commands:\tspeed <speed>"
3790 " (<speed> is 0-65535)\n");
3792 return len;
3795 static int fan_write_cmd_level(const char *cmd, int *rc)
3797 int level;
3799 if (strlencmp(cmd, "level auto") == 0)
3800 level = TP_EC_FAN_AUTO;
3801 else if ((strlencmp(cmd, "level disengaged") == 0) |
3802 (strlencmp(cmd, "level full-speed") == 0))
3803 level = TP_EC_FAN_FULLSPEED;
3804 else if (sscanf(cmd, "level %d", &level) != 1)
3805 return 0;
3807 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3808 printk(IBM_ERR "level command accepted for unsupported "
3809 "access mode %d", fan_control_access_mode);
3811 return 1;
3814 static int fan_write_cmd_enable(const char *cmd, int *rc)
3816 if (strlencmp(cmd, "enable") != 0)
3817 return 0;
3819 if ((*rc = fan_set_enable()) == -ENXIO)
3820 printk(IBM_ERR "enable command accepted for unsupported "
3821 "access mode %d", fan_control_access_mode);
3823 return 1;
3826 static int fan_write_cmd_disable(const char *cmd, int *rc)
3828 if (strlencmp(cmd, "disable") != 0)
3829 return 0;
3831 if ((*rc = fan_set_disable()) == -ENXIO)
3832 printk(IBM_ERR "disable command accepted for unsupported "
3833 "access mode %d", fan_control_access_mode);
3835 return 1;
3838 static int fan_write_cmd_speed(const char *cmd, int *rc)
3840 int speed;
3842 /* TODO:
3843 * Support speed <low> <medium> <high> ? */
3845 if (sscanf(cmd, "speed %d", &speed) != 1)
3846 return 0;
3848 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3849 printk(IBM_ERR "speed command accepted for unsupported "
3850 "access mode %d", fan_control_access_mode);
3852 return 1;
3855 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3857 int interval;
3859 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3860 return 0;
3862 if (interval < 0 || interval > 120)
3863 *rc = -EINVAL;
3864 else
3865 fan_watchdog_maxinterval = interval;
3867 return 1;
3870 static int fan_write(char *buf)
3872 char *cmd;
3873 int rc = 0;
3875 while (!rc && (cmd = next_cmd(&buf))) {
3876 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3877 fan_write_cmd_level(cmd, &rc)) &&
3878 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3879 (fan_write_cmd_enable(cmd, &rc) ||
3880 fan_write_cmd_disable(cmd, &rc) ||
3881 fan_write_cmd_watchdog(cmd, &rc))) &&
3882 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3883 fan_write_cmd_speed(cmd, &rc))
3885 rc = -EINVAL;
3886 else if (!rc)
3887 fan_watchdog_reset();
3890 return rc;
3893 static struct ibm_struct fan_driver_data = {
3894 .name = "fan",
3895 .read = fan_read,
3896 .write = fan_write,
3897 .exit = fan_exit,
3900 /****************************************************************************
3901 ****************************************************************************
3903 * Infrastructure
3905 ****************************************************************************
3906 ****************************************************************************/
3908 /* /proc support */
3909 static struct proc_dir_entry *proc_dir;
3911 /* Subdriver registry */
3912 static LIST_HEAD(tpacpi_all_drivers);
3916 * Module and infrastructure proble, init and exit handling
3919 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3920 static const char * __init str_supported(int is_supported)
3922 static char text_unsupported[] __initdata = "not supported";
3924 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3926 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3928 static int __init ibm_init(struct ibm_init_struct *iibm)
3930 int ret;
3931 struct ibm_struct *ibm = iibm->data;
3932 struct proc_dir_entry *entry;
3934 BUG_ON(ibm == NULL);
3936 INIT_LIST_HEAD(&ibm->all_drivers);
3938 if (ibm->flags.experimental && !experimental)
3939 return 0;
3941 dbg_printk(TPACPI_DBG_INIT,
3942 "probing for %s\n", ibm->name);
3944 if (iibm->init) {
3945 ret = iibm->init(iibm);
3946 if (ret > 0)
3947 return 0; /* probe failed */
3948 if (ret)
3949 return ret;
3951 ibm->flags.init_called = 1;
3954 if (ibm->acpi) {
3955 if (ibm->acpi->hid) {
3956 ret = register_tpacpi_subdriver(ibm);
3957 if (ret)
3958 goto err_out;
3961 if (ibm->acpi->notify) {
3962 ret = setup_acpi_notify(ibm);
3963 if (ret == -ENODEV) {
3964 printk(IBM_NOTICE "disabling subdriver %s\n",
3965 ibm->name);
3966 ret = 0;
3967 goto err_out;
3969 if (ret < 0)
3970 goto err_out;
3974 dbg_printk(TPACPI_DBG_INIT,
3975 "%s installed\n", ibm->name);
3977 if (ibm->read) {
3978 entry = create_proc_entry(ibm->name,
3979 S_IFREG | S_IRUGO | S_IWUSR,
3980 proc_dir);
3981 if (!entry) {
3982 printk(IBM_ERR "unable to create proc entry %s\n",
3983 ibm->name);
3984 ret = -ENODEV;
3985 goto err_out;
3987 entry->owner = THIS_MODULE;
3988 entry->data = ibm;
3989 entry->read_proc = &dispatch_procfs_read;
3990 if (ibm->write)
3991 entry->write_proc = &dispatch_procfs_write;
3992 ibm->flags.proc_created = 1;
3995 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3997 return 0;
3999 err_out:
4000 dbg_printk(TPACPI_DBG_INIT,
4001 "%s: at error exit path with result %d\n",
4002 ibm->name, ret);
4004 ibm_exit(ibm);
4005 return (ret < 0)? ret : 0;
4008 static void ibm_exit(struct ibm_struct *ibm)
4010 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4012 list_del_init(&ibm->all_drivers);
4014 if (ibm->flags.acpi_notify_installed) {
4015 dbg_printk(TPACPI_DBG_EXIT,
4016 "%s: acpi_remove_notify_handler\n", ibm->name);
4017 BUG_ON(!ibm->acpi);
4018 acpi_remove_notify_handler(*ibm->acpi->handle,
4019 ibm->acpi->type,
4020 dispatch_acpi_notify);
4021 ibm->flags.acpi_notify_installed = 0;
4022 ibm->flags.acpi_notify_installed = 0;
4025 if (ibm->flags.proc_created) {
4026 dbg_printk(TPACPI_DBG_EXIT,
4027 "%s: remove_proc_entry\n", ibm->name);
4028 remove_proc_entry(ibm->name, proc_dir);
4029 ibm->flags.proc_created = 0;
4032 if (ibm->flags.acpi_driver_registered) {
4033 dbg_printk(TPACPI_DBG_EXIT,
4034 "%s: acpi_bus_unregister_driver\n", ibm->name);
4035 BUG_ON(!ibm->acpi);
4036 acpi_bus_unregister_driver(ibm->acpi->driver);
4037 kfree(ibm->acpi->driver);
4038 ibm->acpi->driver = NULL;
4039 ibm->flags.acpi_driver_registered = 0;
4042 if (ibm->flags.init_called && ibm->exit) {
4043 ibm->exit();
4044 ibm->flags.init_called = 0;
4047 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4050 /* Probing */
4052 static char *ibm_thinkpad_ec_found;
4054 static char* __init check_dmi_for_ec(void)
4056 struct dmi_device *dev = NULL;
4057 char ec_fw_string[18];
4060 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4061 * X32 or newer, all Z series; Some models must have an
4062 * up-to-date BIOS or they will not be detected.
4064 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4066 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4067 if (sscanf(dev->name,
4068 "IBM ThinkPad Embedded Controller -[%17c",
4069 ec_fw_string) == 1) {
4070 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4071 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4072 return kstrdup(ec_fw_string, GFP_KERNEL);
4075 return NULL;
4078 static int __init probe_for_thinkpad(void)
4080 int is_thinkpad;
4082 if (acpi_disabled)
4083 return -ENODEV;
4086 * Non-ancient models have better DMI tagging, but very old models
4087 * don't.
4089 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4091 /* ec is required because many other handles are relative to it */
4092 IBM_ACPIHANDLE_INIT(ec);
4093 if (!ec_handle) {
4094 if (is_thinkpad)
4095 printk(IBM_ERR
4096 "Not yet supported ThinkPad detected!\n");
4097 return -ENODEV;
4101 * Risks a regression on very old machines, but reduces potential
4102 * false positives a damn great deal
4104 if (!is_thinkpad)
4105 is_thinkpad = dmi_name_in_vendors("IBM");
4107 if (!is_thinkpad && !force_load)
4108 return -ENODEV;
4110 return 0;
4114 /* Module init, exit, parameters */
4116 static struct ibm_init_struct ibms_init[] __initdata = {
4118 .init = thinkpad_acpi_driver_init,
4119 .data = &thinkpad_acpi_driver_data,
4122 .init = hotkey_init,
4123 .data = &hotkey_driver_data,
4126 .init = bluetooth_init,
4127 .data = &bluetooth_driver_data,
4130 .init = wan_init,
4131 .data = &wan_driver_data,
4134 .init = video_init,
4135 .data = &video_driver_data,
4138 .init = light_init,
4139 .data = &light_driver_data,
4141 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4143 .init = dock_init,
4144 .data = &dock_driver_data[0],
4147 .init = dock_init2,
4148 .data = &dock_driver_data[1],
4150 #endif
4151 #ifdef CONFIG_THINKPAD_ACPI_BAY
4153 .init = bay_init,
4154 .data = &bay_driver_data,
4156 #endif
4158 .init = cmos_init,
4159 .data = &cmos_driver_data,
4162 .init = led_init,
4163 .data = &led_driver_data,
4166 .init = beep_init,
4167 .data = &beep_driver_data,
4170 .init = thermal_init,
4171 .data = &thermal_driver_data,
4174 .data = &ecdump_driver_data,
4177 .init = brightness_init,
4178 .data = &brightness_driver_data,
4181 .data = &volume_driver_data,
4184 .init = fan_init,
4185 .data = &fan_driver_data,
4189 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4191 unsigned int i;
4192 struct ibm_struct *ibm;
4194 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4195 ibm = ibms_init[i].data;
4196 BUG_ON(ibm == NULL);
4198 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4199 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4200 return -ENOSPC;
4201 strcpy(ibms_init[i].param, val);
4202 strcat(ibms_init[i].param, ",");
4203 return 0;
4207 return -EINVAL;
4210 static int experimental;
4211 module_param(experimental, int, 0);
4213 static u32 dbg_level;
4214 module_param_named(debug, dbg_level, uint, 0);
4216 static int force_load;
4217 module_param(force_load, int, 0);
4219 static int fan_control_allowed;
4220 module_param_named(fan_control, fan_control_allowed, int, 0);
4222 #define IBM_PARAM(feature) \
4223 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4225 IBM_PARAM(hotkey);
4226 IBM_PARAM(bluetooth);
4227 IBM_PARAM(video);
4228 IBM_PARAM(light);
4229 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4230 IBM_PARAM(dock);
4231 #endif
4232 #ifdef CONFIG_THINKPAD_ACPI_BAY
4233 IBM_PARAM(bay);
4234 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4235 IBM_PARAM(cmos);
4236 IBM_PARAM(led);
4237 IBM_PARAM(beep);
4238 IBM_PARAM(ecdump);
4239 IBM_PARAM(brightness);
4240 IBM_PARAM(volume);
4241 IBM_PARAM(fan);
4243 static int __init thinkpad_acpi_module_init(void)
4245 int ret, i;
4247 /* Driver-level probe */
4248 ret = probe_for_thinkpad();
4249 if (ret)
4250 return ret;
4252 /* Driver initialization */
4253 ibm_thinkpad_ec_found = check_dmi_for_ec();
4254 IBM_ACPIHANDLE_INIT(ecrd);
4255 IBM_ACPIHANDLE_INIT(ecwr);
4257 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4258 if (!proc_dir) {
4259 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4260 thinkpad_acpi_module_exit();
4261 return -ENODEV;
4263 proc_dir->owner = THIS_MODULE;
4265 ret = platform_driver_register(&tpacpi_pdriver);
4266 if (ret) {
4267 printk(IBM_ERR "unable to register platform driver\n");
4268 thinkpad_acpi_module_exit();
4269 return ret;
4271 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4272 if (ret) {
4273 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4274 thinkpad_acpi_module_exit();
4275 return ret;
4279 /* Device initialization */
4280 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4281 NULL, 0);
4282 if (IS_ERR(tpacpi_pdev)) {
4283 ret = PTR_ERR(tpacpi_pdev);
4284 tpacpi_pdev = NULL;
4285 printk(IBM_ERR "unable to register platform device\n");
4286 thinkpad_acpi_module_exit();
4287 return ret;
4289 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4290 if (IS_ERR(tpacpi_hwmon)) {
4291 ret = PTR_ERR(tpacpi_hwmon);
4292 tpacpi_hwmon = NULL;
4293 printk(IBM_ERR "unable to register hwmon device\n");
4294 thinkpad_acpi_module_exit();
4295 return ret;
4297 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4298 ret = ibm_init(&ibms_init[i]);
4299 if (ret >= 0 && *ibms_init[i].param)
4300 ret = ibms_init[i].data->write(ibms_init[i].param);
4301 if (ret < 0) {
4302 thinkpad_acpi_module_exit();
4303 return ret;
4307 return 0;
4310 static void thinkpad_acpi_module_exit(void)
4312 struct ibm_struct *ibm, *itmp;
4314 list_for_each_entry_safe_reverse(ibm, itmp,
4315 &tpacpi_all_drivers,
4316 all_drivers) {
4317 ibm_exit(ibm);
4320 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4322 if (tpacpi_hwmon)
4323 hwmon_device_unregister(tpacpi_hwmon);
4325 if (tpacpi_pdev)
4326 platform_device_unregister(tpacpi_pdev);
4328 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4329 platform_driver_unregister(&tpacpi_pdriver);
4331 if (proc_dir)
4332 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4334 kfree(ibm_thinkpad_ec_found);
4337 module_init(thinkpad_acpi_module_init);
4338 module_exit(thinkpad_acpi_module_exit);