ACPI: thinkpad-acpi: add DMI-based modalias
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob22a5f228b55a0218fc6732af94acfc7811980602
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 = NULL;
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 = NULL;
519 static struct class_device *tpacpi_hwmon = NULL;
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 int hotkey_orig_mask;
732 static struct attribute_set *hotkey_dev_attributes = NULL;
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, mask;
741 res = hotkey_get(&status, &mask);
742 if (res)
743 return res;
745 return snprintf(buf, PAGE_SIZE, "%d\n", status);
748 static ssize_t hotkey_enable_store(struct device *dev,
749 struct device_attribute *attr,
750 const char *buf, size_t count)
752 unsigned long t;
753 int res, status, mask;
755 if (parse_strtoul(buf, 1, &t))
756 return -EINVAL;
758 res = hotkey_get(&status, &mask);
759 if (!res)
760 res = hotkey_set(t, mask);
762 return (res) ? res : count;
765 static struct device_attribute dev_attr_hotkey_enable =
766 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
767 hotkey_enable_show, hotkey_enable_store);
769 /* sysfs hotkey mask --------------------------------------------------- */
770 static ssize_t hotkey_mask_show(struct device *dev,
771 struct device_attribute *attr,
772 char *buf)
774 int res, status, mask;
776 res = hotkey_get(&status, &mask);
777 if (res)
778 return res;
780 return snprintf(buf, PAGE_SIZE, "0x%04x\n", mask);
783 static ssize_t hotkey_mask_store(struct device *dev,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
787 unsigned long t;
788 int res, status, mask;
790 if (parse_strtoul(buf, 0xffff, &t))
791 return -EINVAL;
793 res = hotkey_get(&status, &mask);
794 if (!res)
795 hotkey_set(status, t);
797 return (res) ? res : count;
800 static struct device_attribute dev_attr_hotkey_mask =
801 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
802 hotkey_mask_show, hotkey_mask_store);
804 /* sysfs hotkey bios_enabled ------------------------------------------- */
805 static ssize_t hotkey_bios_enabled_show(struct device *dev,
806 struct device_attribute *attr,
807 char *buf)
809 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
812 static struct device_attribute dev_attr_hotkey_bios_enabled =
813 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
815 /* sysfs hotkey bios_mask ---------------------------------------------- */
816 static ssize_t hotkey_bios_mask_show(struct device *dev,
817 struct device_attribute *attr,
818 char *buf)
820 return snprintf(buf, PAGE_SIZE, "0x%04x\n", hotkey_orig_mask);
823 static struct device_attribute dev_attr_hotkey_bios_mask =
824 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
826 /* --------------------------------------------------------------------- */
828 static struct attribute *hotkey_mask_attributes[] = {
829 &dev_attr_hotkey_mask.attr,
830 &dev_attr_hotkey_bios_enabled.attr,
831 &dev_attr_hotkey_bios_mask.attr,
834 static int __init hotkey_init(struct ibm_init_struct *iibm)
836 int res;
838 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
840 IBM_ACPIHANDLE_INIT(hkey);
841 mutex_init(&hotkey_mutex);
843 /* hotkey not supported on 570 */
844 tp_features.hotkey = hkey_handle != NULL;
846 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
847 str_supported(tp_features.hotkey));
849 if (tp_features.hotkey) {
850 hotkey_dev_attributes = create_attr_set(4, NULL);
851 if (!hotkey_dev_attributes)
852 return -ENOMEM;
853 res = add_to_attr_set(hotkey_dev_attributes,
854 &dev_attr_hotkey_enable.attr);
855 if (res)
856 return res;
858 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
859 A30, R30, R31, T20-22, X20-21, X22-24 */
860 tp_features.hotkey_mask =
861 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
863 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
864 str_supported(tp_features.hotkey_mask));
866 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
867 if (!res && tp_features.hotkey_mask) {
868 res = add_many_to_attr_set(hotkey_dev_attributes,
869 hotkey_mask_attributes,
870 ARRAY_SIZE(hotkey_mask_attributes));
872 if (!res)
873 res = register_attr_set_with_sysfs(
874 hotkey_dev_attributes,
875 &tpacpi_pdev->dev.kobj);
877 if (res)
878 return res;
881 return (tp_features.hotkey)? 0 : 1;
884 static void hotkey_exit(void)
886 int res;
888 if (tp_features.hotkey) {
889 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
890 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
891 if (res)
892 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
895 if (hotkey_dev_attributes) {
896 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
897 hotkey_dev_attributes = NULL;
901 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
903 int hkey;
905 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
906 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
907 else {
908 printk(IBM_ERR "unknown hotkey event %d\n", event);
909 acpi_bus_generate_event(ibm->acpi->device, event, 0);
914 * Call with hotkey_mutex held
916 static int hotkey_get(int *status, int *mask)
918 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
919 return -EIO;
921 if (tp_features.hotkey_mask)
922 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
923 return -EIO;
925 return 0;
929 * Call with hotkey_mutex held
931 static int hotkey_set(int status, int mask)
933 int i;
935 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
936 return -EIO;
938 if (tp_features.hotkey_mask)
939 for (i = 0; i < 32; i++) {
940 int bit = ((1 << i) & mask) != 0;
941 if (!acpi_evalf(hkey_handle,
942 NULL, "MHKM", "vdd", i + 1, bit))
943 return -EIO;
946 return 0;
949 /* procfs -------------------------------------------------------------- */
950 static int hotkey_read(char *p)
952 int res, status, mask;
953 int len = 0;
955 if (!tp_features.hotkey) {
956 len += sprintf(p + len, "status:\t\tnot supported\n");
957 return len;
960 res = mutex_lock_interruptible(&hotkey_mutex);
961 if (res < 0)
962 return res;
963 res = hotkey_get(&status, &mask);
964 mutex_unlock(&hotkey_mutex);
965 if (res)
966 return res;
968 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
969 if (tp_features.hotkey_mask) {
970 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
971 len += sprintf(p + len,
972 "commands:\tenable, disable, reset, <mask>\n");
973 } else {
974 len += sprintf(p + len, "mask:\t\tnot supported\n");
975 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
978 return len;
981 static int hotkey_write(char *buf)
983 int res, status, mask;
984 char *cmd;
985 int do_cmd = 0;
987 if (!tp_features.hotkey)
988 return -ENODEV;
990 res = mutex_lock_interruptible(&hotkey_mutex);
991 if (res < 0)
992 return res;
994 res = hotkey_get(&status, &mask);
995 if (res)
996 goto errexit;
998 res = 0;
999 while ((cmd = next_cmd(&buf))) {
1000 if (strlencmp(cmd, "enable") == 0) {
1001 status = 1;
1002 } else if (strlencmp(cmd, "disable") == 0) {
1003 status = 0;
1004 } else if (strlencmp(cmd, "reset") == 0) {
1005 status = hotkey_orig_status;
1006 mask = hotkey_orig_mask;
1007 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1008 /* mask set */
1009 } else if (sscanf(cmd, "%x", &mask) == 1) {
1010 /* mask set */
1011 } else {
1012 res = -EINVAL;
1013 goto errexit;
1015 do_cmd = 1;
1018 if (do_cmd)
1019 res = hotkey_set(status, mask);
1021 errexit:
1022 mutex_unlock(&hotkey_mutex);
1023 return res;
1026 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1027 .hid = IBM_HKEY_HID,
1028 .notify = hotkey_notify,
1029 .handle = &hkey_handle,
1030 .type = ACPI_DEVICE_NOTIFY,
1033 static struct ibm_struct hotkey_driver_data = {
1034 .name = "hotkey",
1035 .read = hotkey_read,
1036 .write = hotkey_write,
1037 .exit = hotkey_exit,
1038 .acpi = &ibm_hotkey_acpidriver,
1041 /*************************************************************************
1042 * Bluetooth subdriver
1045 /* sysfs bluetooth enable ---------------------------------------------- */
1046 static ssize_t bluetooth_enable_show(struct device *dev,
1047 struct device_attribute *attr,
1048 char *buf)
1050 int status;
1052 status = bluetooth_get_radiosw();
1053 if (status < 0)
1054 return status;
1056 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1059 static ssize_t bluetooth_enable_store(struct device *dev,
1060 struct device_attribute *attr,
1061 const char *buf, size_t count)
1063 unsigned long t;
1064 int res;
1066 if (parse_strtoul(buf, 1, &t))
1067 return -EINVAL;
1069 res = bluetooth_set_radiosw(t);
1071 return (res) ? res : count;
1074 static struct device_attribute dev_attr_bluetooth_enable =
1075 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1076 bluetooth_enable_show, bluetooth_enable_store);
1078 /* --------------------------------------------------------------------- */
1080 static struct attribute *bluetooth_attributes[] = {
1081 &dev_attr_bluetooth_enable.attr,
1082 NULL
1085 static const struct attribute_group bluetooth_attr_group = {
1086 .attrs = bluetooth_attributes,
1089 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1091 int res;
1092 int status = 0;
1094 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1096 IBM_ACPIHANDLE_INIT(hkey);
1098 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1099 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1100 tp_features.bluetooth = hkey_handle &&
1101 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1103 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1104 str_supported(tp_features.bluetooth),
1105 status);
1107 if (tp_features.bluetooth) {
1108 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1109 /* no bluetooth hardware present in system */
1110 tp_features.bluetooth = 0;
1111 dbg_printk(TPACPI_DBG_INIT,
1112 "bluetooth hardware not installed\n");
1113 } else {
1114 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1115 &bluetooth_attr_group);
1116 if (res)
1117 return res;
1121 return (tp_features.bluetooth)? 0 : 1;
1124 static void bluetooth_exit(void)
1126 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1127 &bluetooth_attr_group);
1130 static int bluetooth_get_radiosw(void)
1132 int status;
1134 if (!tp_features.bluetooth)
1135 return -ENODEV;
1137 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1138 return -EIO;
1140 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1143 static int bluetooth_set_radiosw(int radio_on)
1145 int status;
1147 if (!tp_features.bluetooth)
1148 return -ENODEV;
1150 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1151 return -EIO;
1152 if (radio_on)
1153 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1154 else
1155 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1156 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1157 return -EIO;
1159 return 0;
1162 /* procfs -------------------------------------------------------------- */
1163 static int bluetooth_read(char *p)
1165 int len = 0;
1166 int status = bluetooth_get_radiosw();
1168 if (!tp_features.bluetooth)
1169 len += sprintf(p + len, "status:\t\tnot supported\n");
1170 else {
1171 len += sprintf(p + len, "status:\t\t%s\n",
1172 (status)? "enabled" : "disabled");
1173 len += sprintf(p + len, "commands:\tenable, disable\n");
1176 return len;
1179 static int bluetooth_write(char *buf)
1181 char *cmd;
1183 if (!tp_features.bluetooth)
1184 return -ENODEV;
1186 while ((cmd = next_cmd(&buf))) {
1187 if (strlencmp(cmd, "enable") == 0) {
1188 bluetooth_set_radiosw(1);
1189 } else if (strlencmp(cmd, "disable") == 0) {
1190 bluetooth_set_radiosw(0);
1191 } else
1192 return -EINVAL;
1195 return 0;
1198 static struct ibm_struct bluetooth_driver_data = {
1199 .name = "bluetooth",
1200 .read = bluetooth_read,
1201 .write = bluetooth_write,
1202 .exit = bluetooth_exit,
1205 /*************************************************************************
1206 * Wan subdriver
1209 /* sysfs wan enable ---------------------------------------------------- */
1210 static ssize_t wan_enable_show(struct device *dev,
1211 struct device_attribute *attr,
1212 char *buf)
1214 int status;
1216 status = wan_get_radiosw();
1217 if (status < 0)
1218 return status;
1220 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1223 static ssize_t wan_enable_store(struct device *dev,
1224 struct device_attribute *attr,
1225 const char *buf, size_t count)
1227 unsigned long t;
1228 int res;
1230 if (parse_strtoul(buf, 1, &t))
1231 return -EINVAL;
1233 res = wan_set_radiosw(t);
1235 return (res) ? res : count;
1238 static struct device_attribute dev_attr_wan_enable =
1239 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1240 wan_enable_show, wan_enable_store);
1242 /* --------------------------------------------------------------------- */
1244 static struct attribute *wan_attributes[] = {
1245 &dev_attr_wan_enable.attr,
1246 NULL
1249 static const struct attribute_group wan_attr_group = {
1250 .attrs = wan_attributes,
1253 static int __init wan_init(struct ibm_init_struct *iibm)
1255 int res;
1256 int status = 0;
1258 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1260 IBM_ACPIHANDLE_INIT(hkey);
1262 tp_features.wan = hkey_handle &&
1263 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1265 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1266 str_supported(tp_features.wan),
1267 status);
1269 if (tp_features.wan) {
1270 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1271 /* no wan hardware present in system */
1272 tp_features.wan = 0;
1273 dbg_printk(TPACPI_DBG_INIT,
1274 "wan hardware not installed\n");
1275 } else {
1276 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1277 &wan_attr_group);
1278 if (res)
1279 return res;
1283 return (tp_features.wan)? 0 : 1;
1286 static void wan_exit(void)
1288 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1289 &wan_attr_group);
1292 static int wan_get_radiosw(void)
1294 int status;
1296 if (!tp_features.wan)
1297 return -ENODEV;
1299 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1300 return -EIO;
1302 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1305 static int wan_set_radiosw(int radio_on)
1307 int status;
1309 if (!tp_features.wan)
1310 return -ENODEV;
1312 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1313 return -EIO;
1314 if (radio_on)
1315 status |= TP_ACPI_WANCARD_RADIOSSW;
1316 else
1317 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1318 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1319 return -EIO;
1321 return 0;
1324 /* procfs -------------------------------------------------------------- */
1325 static int wan_read(char *p)
1327 int len = 0;
1328 int status = wan_get_radiosw();
1330 if (!tp_features.wan)
1331 len += sprintf(p + len, "status:\t\tnot supported\n");
1332 else {
1333 len += sprintf(p + len, "status:\t\t%s\n",
1334 (status)? "enabled" : "disabled");
1335 len += sprintf(p + len, "commands:\tenable, disable\n");
1338 return len;
1341 static int wan_write(char *buf)
1343 char *cmd;
1345 if (!tp_features.wan)
1346 return -ENODEV;
1348 while ((cmd = next_cmd(&buf))) {
1349 if (strlencmp(cmd, "enable") == 0) {
1350 wan_set_radiosw(1);
1351 } else if (strlencmp(cmd, "disable") == 0) {
1352 wan_set_radiosw(0);
1353 } else
1354 return -EINVAL;
1357 return 0;
1360 static struct ibm_struct wan_driver_data = {
1361 .name = "wan",
1362 .read = wan_read,
1363 .write = wan_write,
1364 .exit = wan_exit,
1365 .flags.experimental = 1,
1368 /*************************************************************************
1369 * Video subdriver
1372 static enum video_access_mode video_supported;
1373 static int video_orig_autosw;
1375 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1376 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1377 "\\_SB.PCI0.VID0", /* 770e */
1378 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1379 "\\_SB.PCI0.AGP.VID", /* all others */
1380 ); /* R30, R31 */
1382 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1384 static int __init video_init(struct ibm_init_struct *iibm)
1386 int ivga;
1388 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1390 IBM_ACPIHANDLE_INIT(vid);
1391 IBM_ACPIHANDLE_INIT(vid2);
1393 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1394 /* G41, assume IVGA doesn't change */
1395 vid_handle = vid2_handle;
1397 if (!vid_handle)
1398 /* video switching not supported on R30, R31 */
1399 video_supported = TPACPI_VIDEO_NONE;
1400 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1401 /* 570 */
1402 video_supported = TPACPI_VIDEO_570;
1403 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1404 /* 600e/x, 770e, 770x */
1405 video_supported = TPACPI_VIDEO_770;
1406 else
1407 /* all others */
1408 video_supported = TPACPI_VIDEO_NEW;
1410 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1411 str_supported(video_supported != TPACPI_VIDEO_NONE),
1412 video_supported);
1414 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1417 static void video_exit(void)
1419 dbg_printk(TPACPI_DBG_EXIT,
1420 "restoring original video autoswitch mode\n");
1421 if (video_autosw_set(video_orig_autosw))
1422 printk(IBM_ERR "error while trying to restore original "
1423 "video autoswitch mode\n");
1426 static int video_outputsw_get(void)
1428 int status = 0;
1429 int i;
1431 switch (video_supported) {
1432 case TPACPI_VIDEO_570:
1433 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1434 TP_ACPI_VIDEO_570_PHSCMD))
1435 return -EIO;
1436 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1437 break;
1438 case TPACPI_VIDEO_770:
1439 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1440 return -EIO;
1441 if (i)
1442 status |= TP_ACPI_VIDEO_S_LCD;
1443 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1444 return -EIO;
1445 if (i)
1446 status |= TP_ACPI_VIDEO_S_CRT;
1447 break;
1448 case TPACPI_VIDEO_NEW:
1449 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1450 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1451 return -EIO;
1452 if (i)
1453 status |= TP_ACPI_VIDEO_S_CRT;
1455 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1456 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1457 return -EIO;
1458 if (i)
1459 status |= TP_ACPI_VIDEO_S_LCD;
1460 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1461 return -EIO;
1462 if (i)
1463 status |= TP_ACPI_VIDEO_S_DVI;
1464 break;
1465 default:
1466 return -ENOSYS;
1469 return status;
1472 static int video_outputsw_set(int status)
1474 int autosw;
1475 int res = 0;
1477 switch (video_supported) {
1478 case TPACPI_VIDEO_570:
1479 res = acpi_evalf(NULL, NULL,
1480 "\\_SB.PHS2", "vdd",
1481 TP_ACPI_VIDEO_570_PHS2CMD,
1482 status | TP_ACPI_VIDEO_570_PHS2SET);
1483 break;
1484 case TPACPI_VIDEO_770:
1485 autosw = video_autosw_get();
1486 if (autosw < 0)
1487 return autosw;
1489 res = video_autosw_set(1);
1490 if (res)
1491 return res;
1492 res = acpi_evalf(vid_handle, NULL,
1493 "ASWT", "vdd", status * 0x100, 0);
1494 if (!autosw && video_autosw_set(autosw)) {
1495 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1496 return -EIO;
1498 break;
1499 case TPACPI_VIDEO_NEW:
1500 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1501 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1502 break;
1503 default:
1504 return -ENOSYS;
1507 return (res)? 0 : -EIO;
1510 static int video_autosw_get(void)
1512 int autosw = 0;
1514 switch (video_supported) {
1515 case TPACPI_VIDEO_570:
1516 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1517 return -EIO;
1518 break;
1519 case TPACPI_VIDEO_770:
1520 case TPACPI_VIDEO_NEW:
1521 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1522 return -EIO;
1523 break;
1524 default:
1525 return -ENOSYS;
1528 return autosw & 1;
1531 static int video_autosw_set(int enable)
1533 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1534 return -EIO;
1535 return 0;
1538 static int video_outputsw_cycle(void)
1540 int autosw = video_autosw_get();
1541 int res;
1543 if (autosw < 0)
1544 return autosw;
1546 switch (video_supported) {
1547 case TPACPI_VIDEO_570:
1548 res = video_autosw_set(1);
1549 if (res)
1550 return res;
1551 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1552 break;
1553 case TPACPI_VIDEO_770:
1554 case TPACPI_VIDEO_NEW:
1555 res = video_autosw_set(1);
1556 if (res)
1557 return res;
1558 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1559 break;
1560 default:
1561 return -ENOSYS;
1563 if (!autosw && video_autosw_set(autosw)) {
1564 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1565 return -EIO;
1568 return (res)? 0 : -EIO;
1571 static int video_expand_toggle(void)
1573 switch (video_supported) {
1574 case TPACPI_VIDEO_570:
1575 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1576 0 : -EIO;
1577 case TPACPI_VIDEO_770:
1578 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1579 0 : -EIO;
1580 case TPACPI_VIDEO_NEW:
1581 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1582 0 : -EIO;
1583 default:
1584 return -ENOSYS;
1586 /* not reached */
1589 static int video_read(char *p)
1591 int status, autosw;
1592 int len = 0;
1594 if (video_supported == TPACPI_VIDEO_NONE) {
1595 len += sprintf(p + len, "status:\t\tnot supported\n");
1596 return len;
1599 status = video_outputsw_get();
1600 if (status < 0)
1601 return status;
1603 autosw = video_autosw_get();
1604 if (autosw < 0)
1605 return autosw;
1607 len += sprintf(p + len, "status:\t\tsupported\n");
1608 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1609 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1610 if (video_supported == TPACPI_VIDEO_NEW)
1611 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1612 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1613 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1614 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1615 if (video_supported == TPACPI_VIDEO_NEW)
1616 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1617 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1618 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1620 return len;
1623 static int video_write(char *buf)
1625 char *cmd;
1626 int enable, disable, status;
1627 int res;
1629 if (video_supported == TPACPI_VIDEO_NONE)
1630 return -ENODEV;
1632 enable = 0;
1633 disable = 0;
1635 while ((cmd = next_cmd(&buf))) {
1636 if (strlencmp(cmd, "lcd_enable") == 0) {
1637 enable |= TP_ACPI_VIDEO_S_LCD;
1638 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1639 disable |= TP_ACPI_VIDEO_S_LCD;
1640 } else if (strlencmp(cmd, "crt_enable") == 0) {
1641 enable |= TP_ACPI_VIDEO_S_CRT;
1642 } else if (strlencmp(cmd, "crt_disable") == 0) {
1643 disable |= TP_ACPI_VIDEO_S_CRT;
1644 } else if (video_supported == TPACPI_VIDEO_NEW &&
1645 strlencmp(cmd, "dvi_enable") == 0) {
1646 enable |= TP_ACPI_VIDEO_S_DVI;
1647 } else if (video_supported == TPACPI_VIDEO_NEW &&
1648 strlencmp(cmd, "dvi_disable") == 0) {
1649 disable |= TP_ACPI_VIDEO_S_DVI;
1650 } else if (strlencmp(cmd, "auto_enable") == 0) {
1651 res = video_autosw_set(1);
1652 if (res)
1653 return res;
1654 } else if (strlencmp(cmd, "auto_disable") == 0) {
1655 res = video_autosw_set(0);
1656 if (res)
1657 return res;
1658 } else if (strlencmp(cmd, "video_switch") == 0) {
1659 res = video_outputsw_cycle();
1660 if (res)
1661 return res;
1662 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1663 res = video_expand_toggle();
1664 if (res)
1665 return res;
1666 } else
1667 return -EINVAL;
1670 if (enable || disable) {
1671 status = video_outputsw_get();
1672 if (status < 0)
1673 return status;
1674 res = video_outputsw_set((status & ~disable) | enable);
1675 if (res)
1676 return res;
1679 return 0;
1682 static struct ibm_struct video_driver_data = {
1683 .name = "video",
1684 .read = video_read,
1685 .write = video_write,
1686 .exit = video_exit,
1689 /*************************************************************************
1690 * Light (thinklight) subdriver
1693 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1694 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1696 static int __init light_init(struct ibm_init_struct *iibm)
1698 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1700 IBM_ACPIHANDLE_INIT(ledb);
1701 IBM_ACPIHANDLE_INIT(lght);
1702 IBM_ACPIHANDLE_INIT(cmos);
1704 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1705 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1707 if (tp_features.light)
1708 /* light status not supported on
1709 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1710 tp_features.light_status =
1711 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1713 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1714 str_supported(tp_features.light));
1716 return (tp_features.light)? 0 : 1;
1719 static int light_read(char *p)
1721 int len = 0;
1722 int status = 0;
1724 if (!tp_features.light) {
1725 len += sprintf(p + len, "status:\t\tnot supported\n");
1726 } else if (!tp_features.light_status) {
1727 len += sprintf(p + len, "status:\t\tunknown\n");
1728 len += sprintf(p + len, "commands:\ton, off\n");
1729 } else {
1730 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1731 return -EIO;
1732 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1733 len += sprintf(p + len, "commands:\ton, off\n");
1736 return len;
1739 static int light_write(char *buf)
1741 int cmos_cmd, lght_cmd;
1742 char *cmd;
1743 int success;
1745 if (!tp_features.light)
1746 return -ENODEV;
1748 while ((cmd = next_cmd(&buf))) {
1749 if (strlencmp(cmd, "on") == 0) {
1750 cmos_cmd = 0x0c;
1751 lght_cmd = 1;
1752 } else if (strlencmp(cmd, "off") == 0) {
1753 cmos_cmd = 0x0d;
1754 lght_cmd = 0;
1755 } else
1756 return -EINVAL;
1758 success = cmos_handle ?
1759 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1760 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1761 if (!success)
1762 return -EIO;
1765 return 0;
1768 static struct ibm_struct light_driver_data = {
1769 .name = "light",
1770 .read = light_read,
1771 .write = light_write,
1774 /*************************************************************************
1775 * Dock subdriver
1778 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1780 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1781 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1782 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1783 "\\_SB.PCI.ISA.SLCE", /* 570 */
1784 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1786 /* don't list other alternatives as we install a notify handler on the 570 */
1787 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1789 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1791 .notify = dock_notify,
1792 .handle = &dock_handle,
1793 .type = ACPI_SYSTEM_NOTIFY,
1796 .hid = IBM_PCI_HID,
1797 .notify = dock_notify,
1798 .handle = &pci_handle,
1799 .type = ACPI_SYSTEM_NOTIFY,
1803 static struct ibm_struct dock_driver_data[2] = {
1805 .name = "dock",
1806 .read = dock_read,
1807 .write = dock_write,
1808 .acpi = &ibm_dock_acpidriver[0],
1811 .name = "dock",
1812 .acpi = &ibm_dock_acpidriver[1],
1816 #define dock_docked() (_sta(dock_handle) & 1)
1818 static int __init dock_init(struct ibm_init_struct *iibm)
1820 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1822 IBM_ACPIHANDLE_INIT(dock);
1824 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1825 str_supported(dock_handle != NULL));
1827 return (dock_handle)? 0 : 1;
1830 static int __init dock_init2(struct ibm_init_struct *iibm)
1832 int dock2_needed;
1834 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1836 if (dock_driver_data[0].flags.acpi_driver_registered &&
1837 dock_driver_data[0].flags.acpi_notify_installed) {
1838 IBM_ACPIHANDLE_INIT(pci);
1839 dock2_needed = (pci_handle != NULL);
1840 vdbg_printk(TPACPI_DBG_INIT,
1841 "dock PCI handler for the TP 570 is %s\n",
1842 str_supported(dock2_needed));
1843 } else {
1844 vdbg_printk(TPACPI_DBG_INIT,
1845 "dock subdriver part 2 not required\n");
1846 dock2_needed = 0;
1849 return (dock2_needed)? 0 : 1;
1852 static void dock_notify(struct ibm_struct *ibm, u32 event)
1854 int docked = dock_docked();
1855 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1857 if (event == 1 && !pci) /* 570 */
1858 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1859 else if (event == 1 && pci) /* 570 */
1860 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1861 else if (event == 3 && docked)
1862 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1863 else if (event == 3 && !docked)
1864 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1865 else if (event == 0 && docked)
1866 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1867 else {
1868 printk(IBM_ERR "unknown dock event %d, status %d\n",
1869 event, _sta(dock_handle));
1870 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1874 static int dock_read(char *p)
1876 int len = 0;
1877 int docked = dock_docked();
1879 if (!dock_handle)
1880 len += sprintf(p + len, "status:\t\tnot supported\n");
1881 else if (!docked)
1882 len += sprintf(p + len, "status:\t\tundocked\n");
1883 else {
1884 len += sprintf(p + len, "status:\t\tdocked\n");
1885 len += sprintf(p + len, "commands:\tdock, undock\n");
1888 return len;
1891 static int dock_write(char *buf)
1893 char *cmd;
1895 if (!dock_docked())
1896 return -ENODEV;
1898 while ((cmd = next_cmd(&buf))) {
1899 if (strlencmp(cmd, "undock") == 0) {
1900 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1901 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1902 return -EIO;
1903 } else if (strlencmp(cmd, "dock") == 0) {
1904 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1905 return -EIO;
1906 } else
1907 return -EINVAL;
1910 return 0;
1913 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1915 /*************************************************************************
1916 * Bay subdriver
1919 #ifdef CONFIG_THINKPAD_ACPI_BAY
1920 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1921 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1922 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1923 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1924 ); /* A21e, R30, R31 */
1925 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1926 "_EJ0", /* all others */
1927 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1928 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1929 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1930 ); /* all others */
1931 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1932 "_EJ0", /* 770x */
1933 ); /* all others */
1935 static int __init bay_init(struct ibm_init_struct *iibm)
1937 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1939 IBM_ACPIHANDLE_INIT(bay);
1940 if (bay_handle)
1941 IBM_ACPIHANDLE_INIT(bay_ej);
1942 IBM_ACPIHANDLE_INIT(bay2);
1943 if (bay2_handle)
1944 IBM_ACPIHANDLE_INIT(bay2_ej);
1946 tp_features.bay_status = bay_handle &&
1947 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1948 tp_features.bay_status2 = bay2_handle &&
1949 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1951 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1952 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1953 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1954 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1956 vdbg_printk(TPACPI_DBG_INIT,
1957 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1958 str_supported(tp_features.bay_status),
1959 str_supported(tp_features.bay_eject),
1960 str_supported(tp_features.bay_status2),
1961 str_supported(tp_features.bay_eject2));
1963 return (tp_features.bay_status || tp_features.bay_eject ||
1964 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1967 static void bay_notify(struct ibm_struct *ibm, u32 event)
1969 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1972 #define bay_occupied(b) (_sta(b##_handle) & 1)
1974 static int bay_read(char *p)
1976 int len = 0;
1977 int occupied = bay_occupied(bay);
1978 int occupied2 = bay_occupied(bay2);
1979 int eject, eject2;
1981 len += sprintf(p + len, "status:\t\t%s\n",
1982 tp_features.bay_status ?
1983 (occupied ? "occupied" : "unoccupied") :
1984 "not supported");
1985 if (tp_features.bay_status2)
1986 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1987 "occupied" : "unoccupied");
1989 eject = tp_features.bay_eject && occupied;
1990 eject2 = tp_features.bay_eject2 && occupied2;
1992 if (eject && eject2)
1993 len += sprintf(p + len, "commands:\teject, eject2\n");
1994 else if (eject)
1995 len += sprintf(p + len, "commands:\teject\n");
1996 else if (eject2)
1997 len += sprintf(p + len, "commands:\teject2\n");
1999 return len;
2002 static int bay_write(char *buf)
2004 char *cmd;
2006 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2007 return -ENODEV;
2009 while ((cmd = next_cmd(&buf))) {
2010 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2011 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2012 return -EIO;
2013 } else if (tp_features.bay_eject2 &&
2014 strlencmp(cmd, "eject2") == 0) {
2015 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2016 return -EIO;
2017 } else
2018 return -EINVAL;
2021 return 0;
2024 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2025 .notify = bay_notify,
2026 .handle = &bay_handle,
2027 .type = ACPI_SYSTEM_NOTIFY,
2030 static struct ibm_struct bay_driver_data = {
2031 .name = "bay",
2032 .read = bay_read,
2033 .write = bay_write,
2034 .acpi = &ibm_bay_acpidriver,
2037 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2039 /*************************************************************************
2040 * CMOS subdriver
2043 /* sysfs cmos_command -------------------------------------------------- */
2044 static ssize_t cmos_command_store(struct device *dev,
2045 struct device_attribute *attr,
2046 const char *buf, size_t count)
2048 unsigned long cmos_cmd;
2049 int res;
2051 if (parse_strtoul(buf, 21, &cmos_cmd))
2052 return -EINVAL;
2054 res = issue_thinkpad_cmos_command(cmos_cmd);
2055 return (res)? res : count;
2058 static struct device_attribute dev_attr_cmos_command =
2059 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2061 /* --------------------------------------------------------------------- */
2063 static int __init cmos_init(struct ibm_init_struct *iibm)
2065 int res;
2067 vdbg_printk(TPACPI_DBG_INIT,
2068 "initializing cmos commands subdriver\n");
2070 IBM_ACPIHANDLE_INIT(cmos);
2072 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2073 str_supported(cmos_handle != NULL));
2075 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2076 if (res)
2077 return res;
2079 return (cmos_handle)? 0 : 1;
2082 static void cmos_exit(void)
2084 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2087 static int cmos_read(char *p)
2089 int len = 0;
2091 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2092 R30, R31, T20-22, X20-21 */
2093 if (!cmos_handle)
2094 len += sprintf(p + len, "status:\t\tnot supported\n");
2095 else {
2096 len += sprintf(p + len, "status:\t\tsupported\n");
2097 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2100 return len;
2103 static int cmos_write(char *buf)
2105 char *cmd;
2106 int cmos_cmd, res;
2108 while ((cmd = next_cmd(&buf))) {
2109 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2110 cmos_cmd >= 0 && cmos_cmd <= 21) {
2111 /* cmos_cmd set */
2112 } else
2113 return -EINVAL;
2115 res = issue_thinkpad_cmos_command(cmos_cmd);
2116 if (res)
2117 return res;
2120 return 0;
2123 static struct ibm_struct cmos_driver_data = {
2124 .name = "cmos",
2125 .read = cmos_read,
2126 .write = cmos_write,
2127 .exit = cmos_exit,
2130 /*************************************************************************
2131 * LED subdriver
2134 static enum led_access_mode led_supported;
2136 IBM_HANDLE(led, ec, "SLED", /* 570 */
2137 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2138 "LED", /* all others */
2139 ); /* R30, R31 */
2141 static int __init led_init(struct ibm_init_struct *iibm)
2143 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2145 IBM_ACPIHANDLE_INIT(led);
2147 if (!led_handle)
2148 /* led not supported on R30, R31 */
2149 led_supported = TPACPI_LED_NONE;
2150 else if (strlencmp(led_path, "SLED") == 0)
2151 /* 570 */
2152 led_supported = TPACPI_LED_570;
2153 else if (strlencmp(led_path, "SYSL") == 0)
2154 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2155 led_supported = TPACPI_LED_OLD;
2156 else
2157 /* all others */
2158 led_supported = TPACPI_LED_NEW;
2160 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2161 str_supported(led_supported), led_supported);
2163 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2166 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2168 static int led_read(char *p)
2170 int len = 0;
2172 if (!led_supported) {
2173 len += sprintf(p + len, "status:\t\tnot supported\n");
2174 return len;
2176 len += sprintf(p + len, "status:\t\tsupported\n");
2178 if (led_supported == TPACPI_LED_570) {
2179 /* 570 */
2180 int i, status;
2181 for (i = 0; i < 8; i++) {
2182 if (!acpi_evalf(ec_handle,
2183 &status, "GLED", "dd", 1 << i))
2184 return -EIO;
2185 len += sprintf(p + len, "%d:\t\t%s\n",
2186 i, led_status(status));
2190 len += sprintf(p + len, "commands:\t"
2191 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2193 return len;
2196 /* off, on, blink */
2197 static const int led_sled_arg1[] = { 0, 1, 3 };
2198 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2199 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2200 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2202 static int led_write(char *buf)
2204 char *cmd;
2205 int led, ind, ret;
2207 if (!led_supported)
2208 return -ENODEV;
2210 while ((cmd = next_cmd(&buf))) {
2211 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2212 return -EINVAL;
2214 if (strstr(cmd, "off")) {
2215 ind = 0;
2216 } else if (strstr(cmd, "on")) {
2217 ind = 1;
2218 } else if (strstr(cmd, "blink")) {
2219 ind = 2;
2220 } else
2221 return -EINVAL;
2223 if (led_supported == TPACPI_LED_570) {
2224 /* 570 */
2225 led = 1 << led;
2226 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2227 led, led_sled_arg1[ind]))
2228 return -EIO;
2229 } else if (led_supported == TPACPI_LED_OLD) {
2230 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2231 led = 1 << led;
2232 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2233 if (ret >= 0)
2234 ret =
2235 ec_write(TPACPI_LED_EC_HLBL,
2236 led * led_exp_hlbl[ind]);
2237 if (ret >= 0)
2238 ret =
2239 ec_write(TPACPI_LED_EC_HLCL,
2240 led * led_exp_hlcl[ind]);
2241 if (ret < 0)
2242 return ret;
2243 } else {
2244 /* all others */
2245 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2246 led, led_led_arg1[ind]))
2247 return -EIO;
2251 return 0;
2254 static struct ibm_struct led_driver_data = {
2255 .name = "led",
2256 .read = led_read,
2257 .write = led_write,
2260 /*************************************************************************
2261 * Beep subdriver
2264 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2266 static int __init beep_init(struct ibm_init_struct *iibm)
2268 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2270 IBM_ACPIHANDLE_INIT(beep);
2272 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2273 str_supported(beep_handle != NULL));
2275 return (beep_handle)? 0 : 1;
2278 static int beep_read(char *p)
2280 int len = 0;
2282 if (!beep_handle)
2283 len += sprintf(p + len, "status:\t\tnot supported\n");
2284 else {
2285 len += sprintf(p + len, "status:\t\tsupported\n");
2286 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2289 return len;
2292 static int beep_write(char *buf)
2294 char *cmd;
2295 int beep_cmd;
2297 if (!beep_handle)
2298 return -ENODEV;
2300 while ((cmd = next_cmd(&buf))) {
2301 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2302 beep_cmd >= 0 && beep_cmd <= 17) {
2303 /* beep_cmd set */
2304 } else
2305 return -EINVAL;
2306 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2307 return -EIO;
2310 return 0;
2313 static struct ibm_struct beep_driver_data = {
2314 .name = "beep",
2315 .read = beep_read,
2316 .write = beep_write,
2319 /*************************************************************************
2320 * Thermal subdriver
2323 static enum thermal_access_mode thermal_read_mode;
2325 /* sysfs temp##_input -------------------------------------------------- */
2327 static ssize_t thermal_temp_input_show(struct device *dev,
2328 struct device_attribute *attr,
2329 char *buf)
2331 struct sensor_device_attribute *sensor_attr =
2332 to_sensor_dev_attr(attr);
2333 int idx = sensor_attr->index;
2334 s32 value;
2335 int res;
2337 res = thermal_get_sensor(idx, &value);
2338 if (res)
2339 return res;
2340 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2341 return -ENXIO;
2343 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2346 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2347 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2349 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2350 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2351 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2352 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2353 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2354 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2355 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2356 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2357 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2358 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2359 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2360 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2361 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2362 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2363 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2364 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2365 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2368 #define THERMAL_ATTRS(X) \
2369 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2371 static struct attribute *thermal_temp_input_attr[] = {
2372 THERMAL_ATTRS(8),
2373 THERMAL_ATTRS(9),
2374 THERMAL_ATTRS(10),
2375 THERMAL_ATTRS(11),
2376 THERMAL_ATTRS(12),
2377 THERMAL_ATTRS(13),
2378 THERMAL_ATTRS(14),
2379 THERMAL_ATTRS(15),
2380 THERMAL_ATTRS(0),
2381 THERMAL_ATTRS(1),
2382 THERMAL_ATTRS(2),
2383 THERMAL_ATTRS(3),
2384 THERMAL_ATTRS(4),
2385 THERMAL_ATTRS(5),
2386 THERMAL_ATTRS(6),
2387 THERMAL_ATTRS(7),
2388 NULL
2391 static const struct attribute_group thermal_temp_input16_group = {
2392 .attrs = thermal_temp_input_attr
2395 static const struct attribute_group thermal_temp_input8_group = {
2396 .attrs = &thermal_temp_input_attr[8]
2399 #undef THERMAL_SENSOR_ATTR_TEMP
2400 #undef THERMAL_ATTRS
2402 /* --------------------------------------------------------------------- */
2404 static int __init thermal_init(struct ibm_init_struct *iibm)
2406 u8 t, ta1, ta2;
2407 int i;
2408 int acpi_tmp7;
2409 int res;
2411 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2413 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2415 if (ibm_thinkpad_ec_found && experimental) {
2417 * Direct EC access mode: sensors at registers
2418 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2419 * non-implemented, thermal sensors return 0x80 when
2420 * not available
2423 ta1 = ta2 = 0;
2424 for (i = 0; i < 8; i++) {
2425 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2426 ta1 |= t;
2427 } else {
2428 ta1 = 0;
2429 break;
2431 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2432 ta2 |= t;
2433 } else {
2434 ta1 = 0;
2435 break;
2438 if (ta1 == 0) {
2439 /* This is sheer paranoia, but we handle it anyway */
2440 if (acpi_tmp7) {
2441 printk(IBM_ERR
2442 "ThinkPad ACPI EC access misbehaving, "
2443 "falling back to ACPI TMPx access mode\n");
2444 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2445 } else {
2446 printk(IBM_ERR
2447 "ThinkPad ACPI EC access misbehaving, "
2448 "disabling thermal sensors access\n");
2449 thermal_read_mode = TPACPI_THERMAL_NONE;
2451 } else {
2452 thermal_read_mode =
2453 (ta2 != 0) ?
2454 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2456 } else if (acpi_tmp7) {
2457 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2458 /* 600e/x, 770e, 770x */
2459 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2460 } else {
2461 /* Standard ACPI TMPx access, max 8 sensors */
2462 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2464 } else {
2465 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2466 thermal_read_mode = TPACPI_THERMAL_NONE;
2469 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2470 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2471 thermal_read_mode);
2473 switch(thermal_read_mode) {
2474 case TPACPI_THERMAL_TPEC_16:
2475 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2476 &thermal_temp_input16_group);
2477 if (res)
2478 return res;
2479 break;
2480 case TPACPI_THERMAL_TPEC_8:
2481 case TPACPI_THERMAL_ACPI_TMP07:
2482 case TPACPI_THERMAL_ACPI_UPDT:
2483 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2484 &thermal_temp_input8_group);
2485 if (res)
2486 return res;
2487 break;
2488 case TPACPI_THERMAL_NONE:
2489 default:
2490 return 1;
2493 return 0;
2496 static void thermal_exit(void)
2498 switch(thermal_read_mode) {
2499 case TPACPI_THERMAL_TPEC_16:
2500 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2501 &thermal_temp_input16_group);
2502 break;
2503 case TPACPI_THERMAL_TPEC_8:
2504 case TPACPI_THERMAL_ACPI_TMP07:
2505 case TPACPI_THERMAL_ACPI_UPDT:
2506 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2507 &thermal_temp_input16_group);
2508 break;
2509 case TPACPI_THERMAL_NONE:
2510 default:
2511 break;
2515 /* idx is zero-based */
2516 static int thermal_get_sensor(int idx, s32 *value)
2518 int t;
2519 s8 tmp;
2520 char tmpi[5];
2522 t = TP_EC_THERMAL_TMP0;
2524 switch (thermal_read_mode) {
2525 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2526 case TPACPI_THERMAL_TPEC_16:
2527 if (idx >= 8 && idx <= 15) {
2528 t = TP_EC_THERMAL_TMP8;
2529 idx -= 8;
2531 /* fallthrough */
2532 #endif
2533 case TPACPI_THERMAL_TPEC_8:
2534 if (idx <= 7) {
2535 if (!acpi_ec_read(t + idx, &tmp))
2536 return -EIO;
2537 *value = tmp * 1000;
2538 return 0;
2540 break;
2542 case TPACPI_THERMAL_ACPI_UPDT:
2543 if (idx <= 7) {
2544 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2545 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2546 return -EIO;
2547 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2548 return -EIO;
2549 *value = (t - 2732) * 100;
2550 return 0;
2552 break;
2554 case TPACPI_THERMAL_ACPI_TMP07:
2555 if (idx <= 7) {
2556 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2557 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2558 return -EIO;
2559 *value = t * 1000;
2560 return 0;
2562 break;
2564 case TPACPI_THERMAL_NONE:
2565 default:
2566 return -ENOSYS;
2569 return -EINVAL;
2572 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2574 int res, i;
2575 int n;
2577 n = 8;
2578 i = 0;
2580 if (!s)
2581 return -EINVAL;
2583 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2584 n = 16;
2586 for(i = 0 ; i < n; i++) {
2587 res = thermal_get_sensor(i, &s->temp[i]);
2588 if (res)
2589 return res;
2592 return n;
2595 static int thermal_read(char *p)
2597 int len = 0;
2598 int n, i;
2599 struct ibm_thermal_sensors_struct t;
2601 n = thermal_get_sensors(&t);
2602 if (unlikely(n < 0))
2603 return n;
2605 len += sprintf(p + len, "temperatures:\t");
2607 if (n > 0) {
2608 for (i = 0; i < (n - 1); i++)
2609 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2610 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2611 } else
2612 len += sprintf(p + len, "not supported\n");
2614 return len;
2617 static struct ibm_struct thermal_driver_data = {
2618 .name = "thermal",
2619 .read = thermal_read,
2620 .exit = thermal_exit,
2623 /*************************************************************************
2624 * EC Dump subdriver
2627 static u8 ecdump_regs[256];
2629 static int ecdump_read(char *p)
2631 int len = 0;
2632 int i, j;
2633 u8 v;
2635 len += sprintf(p + len, "EC "
2636 " +00 +01 +02 +03 +04 +05 +06 +07"
2637 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2638 for (i = 0; i < 256; i += 16) {
2639 len += sprintf(p + len, "EC 0x%02x:", i);
2640 for (j = 0; j < 16; j++) {
2641 if (!acpi_ec_read(i + j, &v))
2642 break;
2643 if (v != ecdump_regs[i + j])
2644 len += sprintf(p + len, " *%02x", v);
2645 else
2646 len += sprintf(p + len, " %02x", v);
2647 ecdump_regs[i + j] = v;
2649 len += sprintf(p + len, "\n");
2650 if (j != 16)
2651 break;
2654 /* These are way too dangerous to advertise openly... */
2655 #if 0
2656 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2657 " (<offset> is 00-ff, <value> is 00-ff)\n");
2658 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2659 " (<offset> is 00-ff, <value> is 0-255)\n");
2660 #endif
2661 return len;
2664 static int ecdump_write(char *buf)
2666 char *cmd;
2667 int i, v;
2669 while ((cmd = next_cmd(&buf))) {
2670 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2671 /* i and v set */
2672 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2673 /* i and v set */
2674 } else
2675 return -EINVAL;
2676 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2677 if (!acpi_ec_write(i, v))
2678 return -EIO;
2679 } else
2680 return -EINVAL;
2683 return 0;
2686 static struct ibm_struct ecdump_driver_data = {
2687 .name = "ecdump",
2688 .read = ecdump_read,
2689 .write = ecdump_write,
2690 .flags.experimental = 1,
2693 /*************************************************************************
2694 * Backlight/brightness subdriver
2697 static struct backlight_device *ibm_backlight_device = NULL;
2699 static struct backlight_ops ibm_backlight_data = {
2700 .get_brightness = brightness_get,
2701 .update_status = brightness_update_status,
2704 static int __init brightness_init(struct ibm_init_struct *iibm)
2706 int b;
2708 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2710 b = brightness_get(NULL);
2711 if (b < 0)
2712 return b;
2714 ibm_backlight_device = backlight_device_register(
2715 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2716 &ibm_backlight_data);
2717 if (IS_ERR(ibm_backlight_device)) {
2718 printk(IBM_ERR "Could not register backlight device\n");
2719 return PTR_ERR(ibm_backlight_device);
2721 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2723 ibm_backlight_device->props.max_brightness = 7;
2724 ibm_backlight_device->props.brightness = b;
2725 backlight_update_status(ibm_backlight_device);
2727 return 0;
2730 static void brightness_exit(void)
2732 if (ibm_backlight_device) {
2733 vdbg_printk(TPACPI_DBG_EXIT,
2734 "calling backlight_device_unregister()\n");
2735 backlight_device_unregister(ibm_backlight_device);
2736 ibm_backlight_device = NULL;
2740 static int brightness_update_status(struct backlight_device *bd)
2742 return brightness_set(
2743 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2744 bd->props.power == FB_BLANK_UNBLANK) ?
2745 bd->props.brightness : 0);
2748 static int brightness_get(struct backlight_device *bd)
2750 u8 level;
2751 if (!acpi_ec_read(brightness_offset, &level))
2752 return -EIO;
2754 level &= 0x7;
2756 return level;
2759 static int brightness_set(int value)
2761 int cmos_cmd, inc, i;
2762 int current_value = brightness_get(NULL);
2764 value &= 7;
2766 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2767 inc = value > current_value ? 1 : -1;
2768 for (i = current_value; i != value; i += inc) {
2769 if (issue_thinkpad_cmos_command(cmos_cmd))
2770 return -EIO;
2771 if (!acpi_ec_write(brightness_offset, i + inc))
2772 return -EIO;
2775 return 0;
2778 static int brightness_read(char *p)
2780 int len = 0;
2781 int level;
2783 if ((level = brightness_get(NULL)) < 0) {
2784 len += sprintf(p + len, "level:\t\tunreadable\n");
2785 } else {
2786 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2787 len += sprintf(p + len, "commands:\tup, down\n");
2788 len += sprintf(p + len, "commands:\tlevel <level>"
2789 " (<level> is 0-7)\n");
2792 return len;
2795 static int brightness_write(char *buf)
2797 int level;
2798 int new_level;
2799 char *cmd;
2801 while ((cmd = next_cmd(&buf))) {
2802 if ((level = brightness_get(NULL)) < 0)
2803 return level;
2804 level &= 7;
2806 if (strlencmp(cmd, "up") == 0) {
2807 new_level = level == 7 ? 7 : level + 1;
2808 } else if (strlencmp(cmd, "down") == 0) {
2809 new_level = level == 0 ? 0 : level - 1;
2810 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2811 new_level >= 0 && new_level <= 7) {
2812 /* new_level set */
2813 } else
2814 return -EINVAL;
2816 brightness_set(new_level);
2819 return 0;
2822 static struct ibm_struct brightness_driver_data = {
2823 .name = "brightness",
2824 .read = brightness_read,
2825 .write = brightness_write,
2826 .exit = brightness_exit,
2829 /*************************************************************************
2830 * Volume subdriver
2833 static int volume_read(char *p)
2835 int len = 0;
2836 u8 level;
2838 if (!acpi_ec_read(volume_offset, &level)) {
2839 len += sprintf(p + len, "level:\t\tunreadable\n");
2840 } else {
2841 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2842 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2843 len += sprintf(p + len, "commands:\tup, down, mute\n");
2844 len += sprintf(p + len, "commands:\tlevel <level>"
2845 " (<level> is 0-15)\n");
2848 return len;
2851 static int volume_write(char *buf)
2853 int cmos_cmd, inc, i;
2854 u8 level, mute;
2855 int new_level, new_mute;
2856 char *cmd;
2858 while ((cmd = next_cmd(&buf))) {
2859 if (!acpi_ec_read(volume_offset, &level))
2860 return -EIO;
2861 new_mute = mute = level & 0x40;
2862 new_level = level = level & 0xf;
2864 if (strlencmp(cmd, "up") == 0) {
2865 if (mute)
2866 new_mute = 0;
2867 else
2868 new_level = level == 15 ? 15 : level + 1;
2869 } else if (strlencmp(cmd, "down") == 0) {
2870 if (mute)
2871 new_mute = 0;
2872 else
2873 new_level = level == 0 ? 0 : level - 1;
2874 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2875 new_level >= 0 && new_level <= 15) {
2876 /* new_level set */
2877 } else if (strlencmp(cmd, "mute") == 0) {
2878 new_mute = 0x40;
2879 } else
2880 return -EINVAL;
2882 if (new_level != level) { /* mute doesn't change */
2883 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2884 inc = new_level > level ? 1 : -1;
2886 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2887 !acpi_ec_write(volume_offset, level)))
2888 return -EIO;
2890 for (i = level; i != new_level; i += inc)
2891 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2892 !acpi_ec_write(volume_offset, i + inc))
2893 return -EIO;
2895 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2896 !acpi_ec_write(volume_offset,
2897 new_level + mute)))
2898 return -EIO;
2901 if (new_mute != mute) { /* level doesn't change */
2902 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2904 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2905 !acpi_ec_write(volume_offset, level + new_mute))
2906 return -EIO;
2910 return 0;
2913 static struct ibm_struct volume_driver_data = {
2914 .name = "volume",
2915 .read = volume_read,
2916 .write = volume_write,
2919 /*************************************************************************
2920 * Fan subdriver
2924 * FAN ACCESS MODES
2926 * TPACPI_FAN_RD_ACPI_GFAN:
2927 * ACPI GFAN method: returns fan level
2929 * see TPACPI_FAN_WR_ACPI_SFAN
2930 * EC 0x2f (HFSP) not available if GFAN exists
2932 * TPACPI_FAN_WR_ACPI_SFAN:
2933 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2935 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2936 * it for writing.
2938 * TPACPI_FAN_WR_TPEC:
2939 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2940 * Supported on almost all ThinkPads
2942 * Fan speed changes of any sort (including those caused by the
2943 * disengaged mode) are usually done slowly by the firmware as the
2944 * maximum ammount of fan duty cycle change per second seems to be
2945 * limited.
2947 * Reading is not available if GFAN exists.
2948 * Writing is not available if SFAN exists.
2950 * Bits
2951 * 7 automatic mode engaged;
2952 * (default operation mode of the ThinkPad)
2953 * fan level is ignored in this mode.
2954 * 6 full speed mode (takes precedence over bit 7);
2955 * not available on all thinkpads. May disable
2956 * the tachometer while the fan controller ramps up
2957 * the speed (which can take up to a few *minutes*).
2958 * Speeds up fan to 100% duty-cycle, which is far above
2959 * the standard RPM levels. It is not impossible that
2960 * it could cause hardware damage.
2961 * 5-3 unused in some models. Extra bits for fan level
2962 * in others, but still useless as all values above
2963 * 7 map to the same speed as level 7 in these models.
2964 * 2-0 fan level (0..7 usually)
2965 * 0x00 = stop
2966 * 0x07 = max (set when temperatures critical)
2967 * Some ThinkPads may have other levels, see
2968 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2970 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2971 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2972 * does so, its initial value is meaningless (0x07).
2974 * For firmware bugs, refer to:
2975 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2977 * ----
2979 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2980 * Main fan tachometer reading (in RPM)
2982 * This register is present on all ThinkPads with a new-style EC, and
2983 * it is known not to be present on the A21m/e, and T22, as there is
2984 * something else in offset 0x84 according to the ACPI DSDT. Other
2985 * ThinkPads from this same time period (and earlier) probably lack the
2986 * tachometer as well.
2988 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2989 * was never fixed by IBM to report the EC firmware version string
2990 * probably support the tachometer (like the early X models), so
2991 * detecting it is quite hard. We need more data to know for sure.
2993 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2994 * might result.
2996 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2997 * mode.
2999 * For firmware bugs, refer to:
3000 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3002 * TPACPI_FAN_WR_ACPI_FANS:
3003 * ThinkPad X31, X40, X41. Not available in the X60.
3005 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3006 * high speed. ACPI DSDT seems to map these three speeds to levels
3007 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3008 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3010 * The speeds are stored on handles
3011 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3013 * There are three default speed sets, acessible as handles:
3014 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3016 * ACPI DSDT switches which set is in use depending on various
3017 * factors.
3019 * TPACPI_FAN_WR_TPEC is also available and should be used to
3020 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3021 * but the ACPI tables just mention level 7.
3024 static enum fan_status_access_mode fan_status_access_mode;
3025 static enum fan_control_access_mode fan_control_access_mode;
3026 static enum fan_control_commands fan_control_commands;
3028 static u8 fan_control_initial_status;
3029 static u8 fan_control_desired_level;
3031 static void fan_watchdog_fire(struct work_struct *ignored);
3032 static int fan_watchdog_maxinterval;
3033 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3035 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3036 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3037 "\\FSPD", /* 600e/x, 770e, 770x */
3038 ); /* all others */
3039 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3040 "JFNS", /* 770x-JL */
3041 ); /* all others */
3044 * SYSFS fan layout: hwmon compatible (device)
3046 * pwm*_enable:
3047 * 0: "disengaged" mode
3048 * 1: manual mode
3049 * 2: native EC "auto" mode (recommended, hardware default)
3051 * pwm*: set speed in manual mode, ignored otherwise.
3052 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3053 * interpolation.
3055 * fan*_input: tachometer reading, RPM
3058 * SYSFS fan layout: extensions
3060 * fan_watchdog (driver):
3061 * fan watchdog interval in seconds, 0 disables (default), max 120
3064 /* sysfs fan pwm1_enable ----------------------------------------------- */
3065 static ssize_t fan_pwm1_enable_show(struct device *dev,
3066 struct device_attribute *attr,
3067 char *buf)
3069 int res, mode;
3070 u8 status;
3072 res = fan_get_status_safe(&status);
3073 if (res)
3074 return res;
3076 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3077 if (status != fan_control_initial_status) {
3078 tp_features.fan_ctrl_status_undef = 0;
3079 } else {
3080 /* Return most likely status. In fact, it
3081 * might be the only possible status */
3082 status = TP_EC_FAN_AUTO;
3086 if (status & TP_EC_FAN_FULLSPEED) {
3087 mode = 0;
3088 } else if (status & TP_EC_FAN_AUTO) {
3089 mode = 2;
3090 } else
3091 mode = 1;
3093 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3096 static ssize_t fan_pwm1_enable_store(struct device *dev,
3097 struct device_attribute *attr,
3098 const char *buf, size_t count)
3100 unsigned long t;
3101 int res, level;
3103 if (parse_strtoul(buf, 2, &t))
3104 return -EINVAL;
3106 switch (t) {
3107 case 0:
3108 level = TP_EC_FAN_FULLSPEED;
3109 break;
3110 case 1:
3111 level = TPACPI_FAN_LAST_LEVEL;
3112 break;
3113 case 2:
3114 level = TP_EC_FAN_AUTO;
3115 break;
3116 case 3:
3117 /* reserved for software-controlled auto mode */
3118 return -ENOSYS;
3119 default:
3120 return -EINVAL;
3123 res = fan_set_level_safe(level);
3124 if (res == -ENXIO)
3125 return -EINVAL;
3126 else if (res < 0)
3127 return res;
3129 fan_watchdog_reset();
3131 return count;
3134 static struct device_attribute dev_attr_fan_pwm1_enable =
3135 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3136 fan_pwm1_enable_show, fan_pwm1_enable_store);
3138 /* sysfs fan pwm1 ------------------------------------------------------ */
3139 static ssize_t fan_pwm1_show(struct device *dev,
3140 struct device_attribute *attr,
3141 char *buf)
3143 int res;
3144 u8 status;
3146 res = fan_get_status_safe(&status);
3147 if (res)
3148 return res;
3150 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3151 if (status != fan_control_initial_status) {
3152 tp_features.fan_ctrl_status_undef = 0;
3153 } else {
3154 status = TP_EC_FAN_AUTO;
3158 if ((status &
3159 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3160 status = fan_control_desired_level;
3162 if (status > 7)
3163 status = 7;
3165 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3168 static ssize_t fan_pwm1_store(struct device *dev,
3169 struct device_attribute *attr,
3170 const char *buf, size_t count)
3172 unsigned long s;
3173 int rc;
3174 u8 status, newlevel;
3176 if (parse_strtoul(buf, 255, &s))
3177 return -EINVAL;
3179 /* scale down from 0-255 to 0-7 */
3180 newlevel = (s >> 5) & 0x07;
3182 rc = mutex_lock_interruptible(&fan_mutex);
3183 if (rc < 0)
3184 return rc;
3186 rc = fan_get_status(&status);
3187 if (!rc && (status &
3188 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3189 rc = fan_set_level(newlevel);
3190 if (rc == -ENXIO)
3191 rc = -EINVAL;
3192 else if (!rc) {
3193 fan_update_desired_level(newlevel);
3194 fan_watchdog_reset();
3198 mutex_unlock(&fan_mutex);
3199 return (rc)? rc : count;
3202 static struct device_attribute dev_attr_fan_pwm1 =
3203 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3204 fan_pwm1_show, fan_pwm1_store);
3206 /* sysfs fan fan1_input ------------------------------------------------ */
3207 static ssize_t fan_fan1_input_show(struct device *dev,
3208 struct device_attribute *attr,
3209 char *buf)
3211 int res;
3212 unsigned int speed;
3214 res = fan_get_speed(&speed);
3215 if (res < 0)
3216 return res;
3218 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3221 static struct device_attribute dev_attr_fan_fan1_input =
3222 __ATTR(fan1_input, S_IRUGO,
3223 fan_fan1_input_show, NULL);
3225 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3226 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3227 char *buf)
3229 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3232 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3233 const char *buf, size_t count)
3235 unsigned long t;
3237 if (parse_strtoul(buf, 120, &t))
3238 return -EINVAL;
3240 if (!fan_control_allowed)
3241 return -EPERM;
3243 fan_watchdog_maxinterval = t;
3244 fan_watchdog_reset();
3246 return count;
3249 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3250 fan_fan_watchdog_show, fan_fan_watchdog_store);
3252 /* --------------------------------------------------------------------- */
3253 static struct attribute *fan_attributes[] = {
3254 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3255 &dev_attr_fan_fan1_input.attr,
3256 NULL
3259 static const struct attribute_group fan_attr_group = {
3260 .attrs = fan_attributes,
3263 static int __init fan_init(struct ibm_init_struct *iibm)
3265 int rc;
3267 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3269 mutex_init(&fan_mutex);
3270 fan_status_access_mode = TPACPI_FAN_NONE;
3271 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3272 fan_control_commands = 0;
3273 fan_watchdog_maxinterval = 0;
3274 tp_features.fan_ctrl_status_undef = 0;
3275 fan_control_desired_level = 7;
3277 IBM_ACPIHANDLE_INIT(fans);
3278 IBM_ACPIHANDLE_INIT(gfan);
3279 IBM_ACPIHANDLE_INIT(sfan);
3281 if (gfan_handle) {
3282 /* 570, 600e/x, 770e, 770x */
3283 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3284 } else {
3285 /* all other ThinkPads: note that even old-style
3286 * ThinkPad ECs supports the fan control register */
3287 if (likely(acpi_ec_read(fan_status_offset,
3288 &fan_control_initial_status))) {
3289 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3291 /* In some ThinkPads, neither the EC nor the ACPI
3292 * DSDT initialize the fan status, and it ends up
3293 * being set to 0x07 when it *could* be either
3294 * 0x07 or 0x80.
3296 * Enable for TP-1Y (T43), TP-78 (R51e),
3297 * TP-76 (R52), TP-70 (T43, R52), which are known
3298 * to be buggy. */
3299 if (fan_control_initial_status == 0x07 &&
3300 ibm_thinkpad_ec_found &&
3301 ((ibm_thinkpad_ec_found[0] == '1' &&
3302 ibm_thinkpad_ec_found[1] == 'Y') ||
3303 (ibm_thinkpad_ec_found[0] == '7' &&
3304 (ibm_thinkpad_ec_found[1] == '6' ||
3305 ibm_thinkpad_ec_found[1] == '8' ||
3306 ibm_thinkpad_ec_found[1] == '0'))
3307 )) {
3308 printk(IBM_NOTICE
3309 "fan_init: initial fan status is "
3310 "unknown, assuming it is in auto "
3311 "mode\n");
3312 tp_features.fan_ctrl_status_undef = 1;
3314 } else {
3315 printk(IBM_ERR
3316 "ThinkPad ACPI EC access misbehaving, "
3317 "fan status and control unavailable\n");
3318 return 1;
3322 if (sfan_handle) {
3323 /* 570, 770x-JL */
3324 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3325 fan_control_commands |=
3326 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3327 } else {
3328 if (!gfan_handle) {
3329 /* gfan without sfan means no fan control */
3330 /* all other models implement TP EC 0x2f control */
3332 if (fans_handle) {
3333 /* X31, X40, X41 */
3334 fan_control_access_mode =
3335 TPACPI_FAN_WR_ACPI_FANS;
3336 fan_control_commands |=
3337 TPACPI_FAN_CMD_SPEED |
3338 TPACPI_FAN_CMD_LEVEL |
3339 TPACPI_FAN_CMD_ENABLE;
3340 } else {
3341 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3342 fan_control_commands |=
3343 TPACPI_FAN_CMD_LEVEL |
3344 TPACPI_FAN_CMD_ENABLE;
3349 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3350 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3351 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3352 fan_status_access_mode, fan_control_access_mode);
3354 /* fan control master switch */
3355 if (!fan_control_allowed) {
3356 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3357 fan_control_commands = 0;
3358 dbg_printk(TPACPI_DBG_INIT,
3359 "fan control features disabled by parameter\n");
3362 /* update fan_control_desired_level */
3363 if (fan_status_access_mode != TPACPI_FAN_NONE)
3364 fan_get_status_safe(NULL);
3366 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3367 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3368 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3369 &fan_attr_group);
3370 if (!(rc < 0))
3371 rc = driver_create_file(&tpacpi_pdriver.driver,
3372 &driver_attr_fan_watchdog);
3373 if (rc < 0)
3374 return rc;
3375 return 0;
3376 } else
3377 return 1;
3381 * Call with fan_mutex held
3383 static void fan_update_desired_level(u8 status)
3385 if ((status &
3386 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3387 if (status > 7)
3388 fan_control_desired_level = 7;
3389 else
3390 fan_control_desired_level = status;
3394 static int fan_get_status(u8 *status)
3396 u8 s;
3398 /* TODO:
3399 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3401 switch (fan_status_access_mode) {
3402 case TPACPI_FAN_RD_ACPI_GFAN:
3403 /* 570, 600e/x, 770e, 770x */
3405 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3406 return -EIO;
3408 if (likely(status))
3409 *status = s & 0x07;
3411 break;
3413 case TPACPI_FAN_RD_TPEC:
3414 /* all except 570, 600e/x, 770e, 770x */
3415 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3416 return -EIO;
3418 if (likely(status))
3419 *status = s;
3421 break;
3423 default:
3424 return -ENXIO;
3427 return 0;
3430 static int fan_get_status_safe(u8 *status)
3432 int rc;
3433 u8 s;
3435 rc = mutex_lock_interruptible(&fan_mutex);
3436 if (rc < 0)
3437 return rc;
3438 rc = fan_get_status(&s);
3439 if (!rc)
3440 fan_update_desired_level(s);
3441 mutex_unlock(&fan_mutex);
3443 if (status)
3444 *status = s;
3446 return rc;
3449 static void fan_exit(void)
3451 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3453 /* FIXME: can we really do this unconditionally? */
3454 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3455 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3457 cancel_delayed_work(&fan_watchdog_task);
3458 flush_scheduled_work();
3461 static int fan_get_speed(unsigned int *speed)
3463 u8 hi, lo;
3465 switch (fan_status_access_mode) {
3466 case TPACPI_FAN_RD_TPEC:
3467 /* all except 570, 600e/x, 770e, 770x */
3468 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3469 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3470 return -EIO;
3472 if (likely(speed))
3473 *speed = (hi << 8) | lo;
3475 break;
3477 default:
3478 return -ENXIO;
3481 return 0;
3484 static void fan_watchdog_fire(struct work_struct *ignored)
3486 int rc;
3488 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3489 rc = fan_set_enable();
3490 if (rc < 0) {
3491 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3492 "will try again later...\n", -rc);
3493 /* reschedule for later */
3494 fan_watchdog_reset();
3498 static void fan_watchdog_reset(void)
3500 static int fan_watchdog_active = 0;
3502 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3503 return;
3505 if (fan_watchdog_active)
3506 cancel_delayed_work(&fan_watchdog_task);
3508 if (fan_watchdog_maxinterval > 0) {
3509 fan_watchdog_active = 1;
3510 if (!schedule_delayed_work(&fan_watchdog_task,
3511 msecs_to_jiffies(fan_watchdog_maxinterval
3512 * 1000))) {
3513 printk(IBM_ERR "failed to schedule the fan watchdog, "
3514 "watchdog will not trigger\n");
3516 } else
3517 fan_watchdog_active = 0;
3520 static int fan_set_level(int level)
3522 if (!fan_control_allowed)
3523 return -EPERM;
3525 switch (fan_control_access_mode) {
3526 case TPACPI_FAN_WR_ACPI_SFAN:
3527 if (level >= 0 && level <= 7) {
3528 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3529 return -EIO;
3530 } else
3531 return -EINVAL;
3532 break;
3534 case TPACPI_FAN_WR_ACPI_FANS:
3535 case TPACPI_FAN_WR_TPEC:
3536 if ((level != TP_EC_FAN_AUTO) &&
3537 (level != TP_EC_FAN_FULLSPEED) &&
3538 ((level < 0) || (level > 7)))
3539 return -EINVAL;
3541 /* safety net should the EC not support AUTO
3542 * or FULLSPEED mode bits and just ignore them */
3543 if (level & TP_EC_FAN_FULLSPEED)
3544 level |= 7; /* safety min speed 7 */
3545 else if (level & TP_EC_FAN_FULLSPEED)
3546 level |= 4; /* safety min speed 4 */
3548 if (!acpi_ec_write(fan_status_offset, level))
3549 return -EIO;
3550 else
3551 tp_features.fan_ctrl_status_undef = 0;
3552 break;
3554 default:
3555 return -ENXIO;
3557 return 0;
3560 static int fan_set_level_safe(int level)
3562 int rc;
3564 if (!fan_control_allowed)
3565 return -EPERM;
3567 rc = mutex_lock_interruptible(&fan_mutex);
3568 if (rc < 0)
3569 return rc;
3571 if (level == TPACPI_FAN_LAST_LEVEL)
3572 level = fan_control_desired_level;
3574 rc = fan_set_level(level);
3575 if (!rc)
3576 fan_update_desired_level(level);
3578 mutex_unlock(&fan_mutex);
3579 return rc;
3582 static int fan_set_enable(void)
3584 u8 s;
3585 int rc;
3587 if (!fan_control_allowed)
3588 return -EPERM;
3590 rc = mutex_lock_interruptible(&fan_mutex);
3591 if (rc < 0)
3592 return rc;
3594 switch (fan_control_access_mode) {
3595 case TPACPI_FAN_WR_ACPI_FANS:
3596 case TPACPI_FAN_WR_TPEC:
3597 rc = fan_get_status(&s);
3598 if (rc < 0)
3599 break;
3601 /* Don't go out of emergency fan mode */
3602 if (s != 7) {
3603 s &= 0x07;
3604 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3607 if (!acpi_ec_write(fan_status_offset, s))
3608 rc = -EIO;
3609 else {
3610 tp_features.fan_ctrl_status_undef = 0;
3611 rc = 0;
3613 break;
3615 case TPACPI_FAN_WR_ACPI_SFAN:
3616 rc = fan_get_status(&s);
3617 if (rc < 0)
3618 break;
3620 s &= 0x07;
3622 /* Set fan to at least level 4 */
3623 s |= 4;
3625 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3626 rc= -EIO;
3627 else
3628 rc = 0;
3629 break;
3631 default:
3632 rc = -ENXIO;
3635 mutex_unlock(&fan_mutex);
3636 return rc;
3639 static int fan_set_disable(void)
3641 int rc;
3643 if (!fan_control_allowed)
3644 return -EPERM;
3646 rc = mutex_lock_interruptible(&fan_mutex);
3647 if (rc < 0)
3648 return rc;
3650 rc = 0;
3651 switch (fan_control_access_mode) {
3652 case TPACPI_FAN_WR_ACPI_FANS:
3653 case TPACPI_FAN_WR_TPEC:
3654 if (!acpi_ec_write(fan_status_offset, 0x00))
3655 rc = -EIO;
3656 else {
3657 fan_control_desired_level = 0;
3658 tp_features.fan_ctrl_status_undef = 0;
3660 break;
3662 case TPACPI_FAN_WR_ACPI_SFAN:
3663 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3664 rc = -EIO;
3665 else
3666 fan_control_desired_level = 0;
3667 break;
3669 default:
3670 rc = -ENXIO;
3674 mutex_unlock(&fan_mutex);
3675 return rc;
3678 static int fan_set_speed(int speed)
3680 int rc;
3682 if (!fan_control_allowed)
3683 return -EPERM;
3685 rc = mutex_lock_interruptible(&fan_mutex);
3686 if (rc < 0)
3687 return rc;
3689 rc = 0;
3690 switch (fan_control_access_mode) {
3691 case TPACPI_FAN_WR_ACPI_FANS:
3692 if (speed >= 0 && speed <= 65535) {
3693 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3694 speed, speed, speed))
3695 rc = -EIO;
3696 } else
3697 rc = -EINVAL;
3698 break;
3700 default:
3701 rc = -ENXIO;
3704 mutex_unlock(&fan_mutex);
3705 return rc;
3708 static int fan_read(char *p)
3710 int len = 0;
3711 int rc;
3712 u8 status;
3713 unsigned int speed = 0;
3715 switch (fan_status_access_mode) {
3716 case TPACPI_FAN_RD_ACPI_GFAN:
3717 /* 570, 600e/x, 770e, 770x */
3718 if ((rc = fan_get_status_safe(&status)) < 0)
3719 return rc;
3721 len += sprintf(p + len, "status:\t\t%s\n"
3722 "level:\t\t%d\n",
3723 (status != 0) ? "enabled" : "disabled", status);
3724 break;
3726 case TPACPI_FAN_RD_TPEC:
3727 /* all except 570, 600e/x, 770e, 770x */
3728 if ((rc = fan_get_status_safe(&status)) < 0)
3729 return rc;
3731 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3732 if (status != fan_control_initial_status)
3733 tp_features.fan_ctrl_status_undef = 0;
3734 else
3735 /* Return most likely status. In fact, it
3736 * might be the only possible status */
3737 status = TP_EC_FAN_AUTO;
3740 len += sprintf(p + len, "status:\t\t%s\n",
3741 (status != 0) ? "enabled" : "disabled");
3743 if ((rc = fan_get_speed(&speed)) < 0)
3744 return rc;
3746 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3748 if (status & TP_EC_FAN_FULLSPEED)
3749 /* Disengaged mode takes precedence */
3750 len += sprintf(p + len, "level:\t\tdisengaged\n");
3751 else if (status & TP_EC_FAN_AUTO)
3752 len += sprintf(p + len, "level:\t\tauto\n");
3753 else
3754 len += sprintf(p + len, "level:\t\t%d\n", status);
3755 break;
3757 case TPACPI_FAN_NONE:
3758 default:
3759 len += sprintf(p + len, "status:\t\tnot supported\n");
3762 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3763 len += sprintf(p + len, "commands:\tlevel <level>");
3765 switch (fan_control_access_mode) {
3766 case TPACPI_FAN_WR_ACPI_SFAN:
3767 len += sprintf(p + len, " (<level> is 0-7)\n");
3768 break;
3770 default:
3771 len += sprintf(p + len, " (<level> is 0-7, "
3772 "auto, disengaged, full-speed)\n");
3773 break;
3777 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3778 len += sprintf(p + len, "commands:\tenable, disable\n"
3779 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3780 "1-120 (seconds))\n");
3782 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3783 len += sprintf(p + len, "commands:\tspeed <speed>"
3784 " (<speed> is 0-65535)\n");
3786 return len;
3789 static int fan_write_cmd_level(const char *cmd, int *rc)
3791 int level;
3793 if (strlencmp(cmd, "level auto") == 0)
3794 level = TP_EC_FAN_AUTO;
3795 else if ((strlencmp(cmd, "level disengaged") == 0) |
3796 (strlencmp(cmd, "level full-speed") == 0))
3797 level = TP_EC_FAN_FULLSPEED;
3798 else if (sscanf(cmd, "level %d", &level) != 1)
3799 return 0;
3801 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3802 printk(IBM_ERR "level command accepted for unsupported "
3803 "access mode %d", fan_control_access_mode);
3805 return 1;
3808 static int fan_write_cmd_enable(const char *cmd, int *rc)
3810 if (strlencmp(cmd, "enable") != 0)
3811 return 0;
3813 if ((*rc = fan_set_enable()) == -ENXIO)
3814 printk(IBM_ERR "enable command accepted for unsupported "
3815 "access mode %d", fan_control_access_mode);
3817 return 1;
3820 static int fan_write_cmd_disable(const char *cmd, int *rc)
3822 if (strlencmp(cmd, "disable") != 0)
3823 return 0;
3825 if ((*rc = fan_set_disable()) == -ENXIO)
3826 printk(IBM_ERR "disable command accepted for unsupported "
3827 "access mode %d", fan_control_access_mode);
3829 return 1;
3832 static int fan_write_cmd_speed(const char *cmd, int *rc)
3834 int speed;
3836 /* TODO:
3837 * Support speed <low> <medium> <high> ? */
3839 if (sscanf(cmd, "speed %d", &speed) != 1)
3840 return 0;
3842 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3843 printk(IBM_ERR "speed command accepted for unsupported "
3844 "access mode %d", fan_control_access_mode);
3846 return 1;
3849 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3851 int interval;
3853 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3854 return 0;
3856 if (interval < 0 || interval > 120)
3857 *rc = -EINVAL;
3858 else
3859 fan_watchdog_maxinterval = interval;
3861 return 1;
3864 static int fan_write(char *buf)
3866 char *cmd;
3867 int rc = 0;
3869 while (!rc && (cmd = next_cmd(&buf))) {
3870 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3871 fan_write_cmd_level(cmd, &rc)) &&
3872 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3873 (fan_write_cmd_enable(cmd, &rc) ||
3874 fan_write_cmd_disable(cmd, &rc) ||
3875 fan_write_cmd_watchdog(cmd, &rc))) &&
3876 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3877 fan_write_cmd_speed(cmd, &rc))
3879 rc = -EINVAL;
3880 else if (!rc)
3881 fan_watchdog_reset();
3884 return rc;
3887 static struct ibm_struct fan_driver_data = {
3888 .name = "fan",
3889 .read = fan_read,
3890 .write = fan_write,
3891 .exit = fan_exit,
3894 /****************************************************************************
3895 ****************************************************************************
3897 * Infrastructure
3899 ****************************************************************************
3900 ****************************************************************************/
3902 /* /proc support */
3903 static struct proc_dir_entry *proc_dir = NULL;
3905 /* Subdriver registry */
3906 static LIST_HEAD(tpacpi_all_drivers);
3910 * Module and infrastructure proble, init and exit handling
3913 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3914 static const char * __init str_supported(int is_supported)
3916 static char text_unsupported[] __initdata = "not supported";
3918 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3920 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3922 static int __init ibm_init(struct ibm_init_struct *iibm)
3924 int ret;
3925 struct ibm_struct *ibm = iibm->data;
3926 struct proc_dir_entry *entry;
3928 BUG_ON(ibm == NULL);
3930 INIT_LIST_HEAD(&ibm->all_drivers);
3932 if (ibm->flags.experimental && !experimental)
3933 return 0;
3935 dbg_printk(TPACPI_DBG_INIT,
3936 "probing for %s\n", ibm->name);
3938 if (iibm->init) {
3939 ret = iibm->init(iibm);
3940 if (ret > 0)
3941 return 0; /* probe failed */
3942 if (ret)
3943 return ret;
3945 ibm->flags.init_called = 1;
3948 if (ibm->acpi) {
3949 if (ibm->acpi->hid) {
3950 ret = register_tpacpi_subdriver(ibm);
3951 if (ret)
3952 goto err_out;
3955 if (ibm->acpi->notify) {
3956 ret = setup_acpi_notify(ibm);
3957 if (ret == -ENODEV) {
3958 printk(IBM_NOTICE "disabling subdriver %s\n",
3959 ibm->name);
3960 ret = 0;
3961 goto err_out;
3963 if (ret < 0)
3964 goto err_out;
3968 dbg_printk(TPACPI_DBG_INIT,
3969 "%s installed\n", ibm->name);
3971 if (ibm->read) {
3972 entry = create_proc_entry(ibm->name,
3973 S_IFREG | S_IRUGO | S_IWUSR,
3974 proc_dir);
3975 if (!entry) {
3976 printk(IBM_ERR "unable to create proc entry %s\n",
3977 ibm->name);
3978 ret = -ENODEV;
3979 goto err_out;
3981 entry->owner = THIS_MODULE;
3982 entry->data = ibm;
3983 entry->read_proc = &dispatch_procfs_read;
3984 if (ibm->write)
3985 entry->write_proc = &dispatch_procfs_write;
3986 ibm->flags.proc_created = 1;
3989 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3991 return 0;
3993 err_out:
3994 dbg_printk(TPACPI_DBG_INIT,
3995 "%s: at error exit path with result %d\n",
3996 ibm->name, ret);
3998 ibm_exit(ibm);
3999 return (ret < 0)? ret : 0;
4002 static void ibm_exit(struct ibm_struct *ibm)
4004 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4006 list_del_init(&ibm->all_drivers);
4008 if (ibm->flags.acpi_notify_installed) {
4009 dbg_printk(TPACPI_DBG_EXIT,
4010 "%s: acpi_remove_notify_handler\n", ibm->name);
4011 BUG_ON(!ibm->acpi);
4012 acpi_remove_notify_handler(*ibm->acpi->handle,
4013 ibm->acpi->type,
4014 dispatch_acpi_notify);
4015 ibm->flags.acpi_notify_installed = 0;
4016 ibm->flags.acpi_notify_installed = 0;
4019 if (ibm->flags.proc_created) {
4020 dbg_printk(TPACPI_DBG_EXIT,
4021 "%s: remove_proc_entry\n", ibm->name);
4022 remove_proc_entry(ibm->name, proc_dir);
4023 ibm->flags.proc_created = 0;
4026 if (ibm->flags.acpi_driver_registered) {
4027 dbg_printk(TPACPI_DBG_EXIT,
4028 "%s: acpi_bus_unregister_driver\n", ibm->name);
4029 BUG_ON(!ibm->acpi);
4030 acpi_bus_unregister_driver(ibm->acpi->driver);
4031 kfree(ibm->acpi->driver);
4032 ibm->acpi->driver = NULL;
4033 ibm->flags.acpi_driver_registered = 0;
4036 if (ibm->flags.init_called && ibm->exit) {
4037 ibm->exit();
4038 ibm->flags.init_called = 0;
4041 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4044 /* Probing */
4046 static char *ibm_thinkpad_ec_found = NULL;
4048 static char* __init check_dmi_for_ec(void)
4050 struct dmi_device *dev = NULL;
4051 char ec_fw_string[18];
4054 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4055 * X32 or newer, all Z series; Some models must have an
4056 * up-to-date BIOS or they will not be detected.
4058 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4060 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4061 if (sscanf(dev->name,
4062 "IBM ThinkPad Embedded Controller -[%17c",
4063 ec_fw_string) == 1) {
4064 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4065 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4066 return kstrdup(ec_fw_string, GFP_KERNEL);
4069 return NULL;
4072 static int __init probe_for_thinkpad(void)
4074 int is_thinkpad;
4076 if (acpi_disabled)
4077 return -ENODEV;
4080 * Non-ancient models have better DMI tagging, but very old models
4081 * don't.
4083 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4085 /* ec is required because many other handles are relative to it */
4086 IBM_ACPIHANDLE_INIT(ec);
4087 if (!ec_handle) {
4088 if (is_thinkpad)
4089 printk(IBM_ERR
4090 "Not yet supported ThinkPad detected!\n");
4091 return -ENODEV;
4095 * Risks a regression on very old machines, but reduces potential
4096 * false positives a damn great deal
4098 if (!is_thinkpad)
4099 is_thinkpad = dmi_name_in_vendors("IBM");
4101 if (!is_thinkpad && !force_load)
4102 return -ENODEV;
4104 return 0;
4108 /* Module init, exit, parameters */
4110 static struct ibm_init_struct ibms_init[] __initdata = {
4112 .init = thinkpad_acpi_driver_init,
4113 .data = &thinkpad_acpi_driver_data,
4116 .init = hotkey_init,
4117 .data = &hotkey_driver_data,
4120 .init = bluetooth_init,
4121 .data = &bluetooth_driver_data,
4124 .init = wan_init,
4125 .data = &wan_driver_data,
4128 .init = video_init,
4129 .data = &video_driver_data,
4132 .init = light_init,
4133 .data = &light_driver_data,
4135 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4137 .init = dock_init,
4138 .data = &dock_driver_data[0],
4141 .init = dock_init2,
4142 .data = &dock_driver_data[1],
4144 #endif
4145 #ifdef CONFIG_THINKPAD_ACPI_BAY
4147 .init = bay_init,
4148 .data = &bay_driver_data,
4150 #endif
4152 .init = cmos_init,
4153 .data = &cmos_driver_data,
4156 .init = led_init,
4157 .data = &led_driver_data,
4160 .init = beep_init,
4161 .data = &beep_driver_data,
4164 .init = thermal_init,
4165 .data = &thermal_driver_data,
4168 .data = &ecdump_driver_data,
4171 .init = brightness_init,
4172 .data = &brightness_driver_data,
4175 .data = &volume_driver_data,
4178 .init = fan_init,
4179 .data = &fan_driver_data,
4183 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4185 unsigned int i;
4186 struct ibm_struct *ibm;
4188 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4189 ibm = ibms_init[i].data;
4190 BUG_ON(ibm == NULL);
4192 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4193 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4194 return -ENOSPC;
4195 strcpy(ibms_init[i].param, val);
4196 strcat(ibms_init[i].param, ",");
4197 return 0;
4201 return -EINVAL;
4204 static int experimental;
4205 module_param(experimental, int, 0);
4207 static u32 dbg_level;
4208 module_param_named(debug, dbg_level, uint, 0);
4210 static int force_load;
4211 module_param(force_load, int, 0);
4213 static int fan_control_allowed;
4214 module_param_named(fan_control, fan_control_allowed, int, 0);
4216 #define IBM_PARAM(feature) \
4217 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4219 IBM_PARAM(hotkey);
4220 IBM_PARAM(bluetooth);
4221 IBM_PARAM(video);
4222 IBM_PARAM(light);
4223 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4224 IBM_PARAM(dock);
4225 #endif
4226 #ifdef CONFIG_THINKPAD_ACPI_BAY
4227 IBM_PARAM(bay);
4228 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4229 IBM_PARAM(cmos);
4230 IBM_PARAM(led);
4231 IBM_PARAM(beep);
4232 IBM_PARAM(ecdump);
4233 IBM_PARAM(brightness);
4234 IBM_PARAM(volume);
4235 IBM_PARAM(fan);
4237 static int __init thinkpad_acpi_module_init(void)
4239 int ret, i;
4241 /* Driver-level probe */
4242 ret = probe_for_thinkpad();
4243 if (ret)
4244 return ret;
4246 /* Driver initialization */
4247 ibm_thinkpad_ec_found = check_dmi_for_ec();
4248 IBM_ACPIHANDLE_INIT(ecrd);
4249 IBM_ACPIHANDLE_INIT(ecwr);
4251 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4252 if (!proc_dir) {
4253 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4254 thinkpad_acpi_module_exit();
4255 return -ENODEV;
4257 proc_dir->owner = THIS_MODULE;
4259 ret = platform_driver_register(&tpacpi_pdriver);
4260 if (ret) {
4261 printk(IBM_ERR "unable to register platform driver\n");
4262 thinkpad_acpi_module_exit();
4263 return ret;
4265 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4266 if (ret) {
4267 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4268 thinkpad_acpi_module_exit();
4269 return ret;
4273 /* Device initialization */
4274 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4275 NULL, 0);
4276 if (IS_ERR(tpacpi_pdev)) {
4277 ret = PTR_ERR(tpacpi_pdev);
4278 tpacpi_pdev = NULL;
4279 printk(IBM_ERR "unable to register platform device\n");
4280 thinkpad_acpi_module_exit();
4281 return ret;
4283 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4284 if (IS_ERR(tpacpi_hwmon)) {
4285 ret = PTR_ERR(tpacpi_hwmon);
4286 tpacpi_hwmon = NULL;
4287 printk(IBM_ERR "unable to register hwmon device\n");
4288 thinkpad_acpi_module_exit();
4289 return ret;
4291 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4292 ret = ibm_init(&ibms_init[i]);
4293 if (ret >= 0 && *ibms_init[i].param)
4294 ret = ibms_init[i].data->write(ibms_init[i].param);
4295 if (ret < 0) {
4296 thinkpad_acpi_module_exit();
4297 return ret;
4301 return 0;
4304 static void thinkpad_acpi_module_exit(void)
4306 struct ibm_struct *ibm, *itmp;
4308 list_for_each_entry_safe_reverse(ibm, itmp,
4309 &tpacpi_all_drivers,
4310 all_drivers) {
4311 ibm_exit(ibm);
4314 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4316 if (tpacpi_hwmon)
4317 hwmon_device_unregister(tpacpi_hwmon);
4319 if (tpacpi_pdev)
4320 platform_device_unregister(tpacpi_pdev);
4322 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4323 platform_driver_unregister(&tpacpi_pdriver);
4325 if (proc_dir)
4326 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4328 kfree(ibm_thinkpad_ec_found);
4331 module_init(thinkpad_acpi_module_init);
4332 module_exit(thinkpad_acpi_module_exit);