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
24 #define IBM_VERSION "0.14"
25 #define TPACPI_SYSFS_VERSION 0x000200
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
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
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
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 /*************************************************************************
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
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 */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
168 /*************************************************************************
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *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;
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
200 params.pointer = &in_objs[0];
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
210 /* add more types as needed */
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
226 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
235 success = status == AE_OK;
237 /* add more types as needed */
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
270 if (ec_read(i, p) < 0)
277 static int acpi_ec_write(int i, u8 v)
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
283 if (ec_write(i, v) < 0)
290 static int _sta(acpi_handle handle)
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
311 /*************************************************************************
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
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)
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
358 if (!*ibm->acpi->handle)
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);
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
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",
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
388 ibm->flags.acpi_notify_installed = 1;
392 static int __init tpacpi_device_add(struct acpi_device *device)
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
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");
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);
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
423 ibm->flags.acpi_driver_registered = 1;
429 /****************************************************************************
430 ****************************************************************************
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;
443 if (!ibm || !ibm->read)
446 len = ibm->read(page);
450 if (len <= off + count)
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;
470 if (!ibm || !ibm->write)
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
477 if (copy_from_user(kernbuf, userbuf, count)) {
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
493 static char *next_cmd(char **cmds)
498 while ((end = strchr(start, ',')) && end == start)
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: input, hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520 static struct input_dev *tpacpi_inputdev;
522 static struct platform_driver tpacpi_pdriver = {
524 .name = IBM_DRVR_NAME,
525 .owner = THIS_MODULE,
530 /*************************************************************************
531 * thinkpad-acpi driver attributes
534 /* interface_version --------------------------------------------------- */
535 static ssize_t tpacpi_driver_interface_version_show(
536 struct device_driver *drv,
539 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
542 static DRIVER_ATTR(interface_version, S_IRUGO,
543 tpacpi_driver_interface_version_show, NULL);
545 /* debug_level --------------------------------------------------------- */
546 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
549 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
552 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
553 const char *buf, size_t count)
557 if (parse_strtoul(buf, 0xffff, &t))
565 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
566 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
568 /* version ------------------------------------------------------------- */
569 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
572 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
575 static DRIVER_ATTR(version, S_IRUGO,
576 tpacpi_driver_version_show, NULL);
578 /* --------------------------------------------------------------------- */
580 static struct driver_attribute* tpacpi_driver_attributes[] = {
581 &driver_attr_debug_level, &driver_attr_version,
582 &driver_attr_interface_version,
585 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
591 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
592 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
599 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
603 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
604 driver_remove_file(drv, tpacpi_driver_attributes[i]);
607 /*************************************************************************
608 * sysfs support helpers
611 struct attribute_set_obj {
612 struct attribute_set s;
614 } __attribute__((packed));
616 static struct attribute_set *create_attr_set(unsigned int max_members,
619 struct attribute_set_obj *sobj;
621 if (max_members == 0)
624 /* Allocates space for implicit NULL at the end too */
625 sobj = kzalloc(sizeof(struct attribute_set_obj) +
626 max_members * sizeof(struct attribute *),
630 sobj->s.max_members = max_members;
631 sobj->s.group.attrs = &sobj->a;
632 sobj->s.group.name = name;
637 /* not multi-threaded safe, use it in a single thread per set */
638 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
643 if (s->members >= s->max_members)
646 s->group.attrs[s->members] = attr;
652 static int add_many_to_attr_set(struct attribute_set* s,
653 struct attribute **attr,
658 for (i = 0; i < count; i++) {
659 res = add_to_attr_set(s, attr[i]);
667 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
669 sysfs_remove_group(kobj, &s->group);
673 static int parse_strtoul(const char *buf,
674 unsigned long max, unsigned long *value)
678 *value = simple_strtoul(buf, &endp, 0);
679 while (*endp && isspace(*endp))
681 if (*endp || *value > max)
687 /****************************************************************************
688 ****************************************************************************
692 ****************************************************************************
693 ****************************************************************************/
695 /*************************************************************************
696 * thinkpad-acpi init subdriver
699 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
701 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
702 printk(IBM_INFO "%s\n", IBM_URL);
704 if (ibm_thinkpad_ec_found)
705 printk(IBM_INFO "ThinkPad EC firmware %s\n",
706 ibm_thinkpad_ec_found);
711 static int thinkpad_acpi_driver_read(char *p)
715 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
716 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
721 static struct ibm_struct thinkpad_acpi_driver_data = {
723 .read = thinkpad_acpi_driver_read,
726 /*************************************************************************
730 static int hotkey_orig_status;
731 static u32 hotkey_orig_mask;
732 static u32 hotkey_all_mask;
733 static u32 hotkey_reserved_mask;
735 static u16 hotkey_keycode_map[] = {
736 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
737 KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
738 KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
739 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
740 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
741 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
742 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
743 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
744 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
745 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
746 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
747 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
748 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
749 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
750 KEY_RESERVED, /* 0x14: VOLUME UP */
751 KEY_RESERVED, /* 0x15: VOLUME DOWN */
752 KEY_RESERVED, /* 0x16: MUTE */
753 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
754 /* (assignments unknown, please report if found) */
755 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
756 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
759 static struct attribute_set *hotkey_dev_attributes;
761 static int hotkey_get_wlsw(int *status)
763 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
768 /* sysfs hotkey enable ------------------------------------------------- */
769 static ssize_t hotkey_enable_show(struct device *dev,
770 struct device_attribute *attr,
776 res = hotkey_get(&status, &mask);
780 return snprintf(buf, PAGE_SIZE, "%d\n", status);
783 static ssize_t hotkey_enable_store(struct device *dev,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
791 if (parse_strtoul(buf, 1, &t))
794 res = hotkey_get(&status, &mask);
796 res = hotkey_set(t, mask);
798 return (res) ? res : count;
801 static struct device_attribute dev_attr_hotkey_enable =
802 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
803 hotkey_enable_show, hotkey_enable_store);
805 /* sysfs hotkey mask --------------------------------------------------- */
806 static ssize_t hotkey_mask_show(struct device *dev,
807 struct device_attribute *attr,
813 res = hotkey_get(&status, &mask);
817 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
820 static ssize_t hotkey_mask_store(struct device *dev,
821 struct device_attribute *attr,
822 const char *buf, size_t count)
828 if (parse_strtoul(buf, 0xffffffffUL, &t))
831 res = hotkey_get(&status, &mask);
833 hotkey_set(status, t);
835 return (res) ? res : count;
838 static struct device_attribute dev_attr_hotkey_mask =
839 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
840 hotkey_mask_show, hotkey_mask_store);
842 /* sysfs hotkey bios_enabled ------------------------------------------- */
843 static ssize_t hotkey_bios_enabled_show(struct device *dev,
844 struct device_attribute *attr,
847 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
850 static struct device_attribute dev_attr_hotkey_bios_enabled =
851 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
853 /* sysfs hotkey bios_mask ---------------------------------------------- */
854 static ssize_t hotkey_bios_mask_show(struct device *dev,
855 struct device_attribute *attr,
858 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
861 static struct device_attribute dev_attr_hotkey_bios_mask =
862 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
864 /* sysfs hotkey all_mask ----------------------------------------------- */
865 static ssize_t hotkey_all_mask_show(struct device *dev,
866 struct device_attribute *attr,
869 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
872 static struct device_attribute dev_attr_hotkey_all_mask =
873 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
875 /* sysfs hotkey recommended_mask --------------------------------------- */
876 static ssize_t hotkey_recommended_mask_show(struct device *dev,
877 struct device_attribute *attr,
880 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
881 hotkey_all_mask & ~hotkey_reserved_mask);
884 static struct device_attribute dev_attr_hotkey_recommended_mask =
885 __ATTR(hotkey_recommended_mask, S_IRUGO,
886 hotkey_recommended_mask_show, NULL);
888 /* sysfs hotkey radio_sw ----------------------------------------------- */
889 static ssize_t hotkey_radio_sw_show(struct device *dev,
890 struct device_attribute *attr,
894 res = hotkey_get_wlsw(&s);
898 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
901 static struct device_attribute dev_attr_hotkey_radio_sw =
902 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
904 /* --------------------------------------------------------------------- */
906 static struct attribute *hotkey_mask_attributes[] = {
907 &dev_attr_hotkey_mask.attr,
908 &dev_attr_hotkey_bios_enabled.attr,
909 &dev_attr_hotkey_bios_mask.attr,
910 &dev_attr_hotkey_all_mask.attr,
911 &dev_attr_hotkey_recommended_mask.attr,
914 static int __init hotkey_init(struct ibm_init_struct *iibm)
919 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
921 BUG_ON(!tpacpi_inputdev);
923 IBM_ACPIHANDLE_INIT(hkey);
924 mutex_init(&hotkey_mutex);
926 /* hotkey not supported on 570 */
927 tp_features.hotkey = hkey_handle != NULL;
929 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
930 str_supported(tp_features.hotkey));
932 if (tp_features.hotkey) {
933 hotkey_dev_attributes = create_attr_set(7, NULL);
934 if (!hotkey_dev_attributes)
936 res = add_to_attr_set(hotkey_dev_attributes,
937 &dev_attr_hotkey_enable.attr);
941 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
942 A30, R30, R31, T20-22, X20-21, X22-24 */
943 tp_features.hotkey_mask =
944 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
946 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
947 str_supported(tp_features.hotkey_mask));
949 if (tp_features.hotkey_mask) {
950 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
951 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
953 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
956 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
957 if (!res && tp_features.hotkey_mask) {
958 res = add_many_to_attr_set(hotkey_dev_attributes,
959 hotkey_mask_attributes,
960 ARRAY_SIZE(hotkey_mask_attributes));
963 /* Not all thinkpads have a hardware radio switch */
964 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
965 tp_features.hotkey_wlsw = 1;
967 "radio switch found; radios are %s\n",
969 res = add_to_attr_set(hotkey_dev_attributes,
970 &dev_attr_hotkey_radio_sw.attr);
974 res = register_attr_set_with_sysfs(
975 hotkey_dev_attributes,
976 &tpacpi_pdev->dev.kobj);
980 #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
981 for (i = 0; i < 12; i++)
982 hotkey_keycode_map[i] = KEY_UNKNOWN;
983 #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
985 set_bit(EV_KEY, tpacpi_inputdev->evbit);
986 set_bit(EV_MSC, tpacpi_inputdev->evbit);
987 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
988 tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
989 tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
990 tpacpi_inputdev->keycode = &hotkey_keycode_map;
991 for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
992 if (hotkey_keycode_map[i] != KEY_RESERVED) {
993 set_bit(hotkey_keycode_map[i],
994 tpacpi_inputdev->keybit);
996 if (i < sizeof(hotkey_reserved_mask)*8)
997 hotkey_reserved_mask |= 1 << i;
1001 #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1002 dbg_printk(TPACPI_DBG_INIT,
1003 "enabling hot key handling\n");
1004 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1005 | hotkey_orig_mask);
1008 #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1011 return (tp_features.hotkey)? 0 : 1;
1014 static void hotkey_exit(void)
1018 if (tp_features.hotkey) {
1019 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1020 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1022 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1025 if (hotkey_dev_attributes) {
1026 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1027 hotkey_dev_attributes = NULL;
1031 static void tpacpi_input_send_key(unsigned int scancode,
1032 unsigned int keycode)
1034 if (keycode != KEY_RESERVED) {
1035 input_report_key(tpacpi_inputdev, keycode, 1);
1036 if (keycode == KEY_UNKNOWN)
1037 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1039 input_sync(tpacpi_inputdev);
1041 input_report_key(tpacpi_inputdev, keycode, 0);
1042 if (keycode == KEY_UNKNOWN)
1043 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1045 input_sync(tpacpi_inputdev);
1049 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1052 unsigned int keycode, scancode;
1055 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1056 if (tpacpi_inputdev->users > 0) {
1057 switch (hkey >> 12) {
1059 /* 0x1000-0x1FFF: key presses */
1060 scancode = hkey & 0xfff;
1061 if (scancode > 0 && scancode < 0x21) {
1063 keycode = hotkey_keycode_map[scancode];
1064 tpacpi_input_send_key(scancode, keycode);
1065 sendacpi = (keycode == KEY_RESERVED
1066 || keycode == KEY_UNKNOWN);
1069 "hotkey 0x%04x out of range for keyboard map\n",
1074 /* 0x5000-0x5FFF: LID */
1075 /* we don't handle it through this path, just
1076 * eat up known LID events */
1077 if (hkey != 0x5001 && hkey != 0x5002) {
1079 "unknown LID-related hotkey event: 0x%04x\n",
1084 /* case 2: dock-related */
1085 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1086 /* case 3: ultra-bay related. maybe bay in dock? */
1087 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1088 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1093 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1095 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1096 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1101 * Call with hotkey_mutex held
1103 static int hotkey_get(int *status, u32 *mask)
1105 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1108 if (tp_features.hotkey_mask)
1109 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1116 * Call with hotkey_mutex held
1118 static int hotkey_set(int status, u32 mask)
1122 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1125 if (tp_features.hotkey_mask)
1126 for (i = 0; i < 32; i++) {
1127 int bit = ((1 << i) & mask) != 0;
1128 if (!acpi_evalf(hkey_handle,
1129 NULL, "MHKM", "vdd", i + 1, bit))
1136 /* procfs -------------------------------------------------------------- */
1137 static int hotkey_read(char *p)
1143 if (!tp_features.hotkey) {
1144 len += sprintf(p + len, "status:\t\tnot supported\n");
1148 res = mutex_lock_interruptible(&hotkey_mutex);
1151 res = hotkey_get(&status, &mask);
1152 mutex_unlock(&hotkey_mutex);
1156 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1157 if (tp_features.hotkey_mask) {
1158 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1159 len += sprintf(p + len,
1160 "commands:\tenable, disable, reset, <mask>\n");
1162 len += sprintf(p + len, "mask:\t\tnot supported\n");
1163 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1169 static int hotkey_write(char *buf)
1176 if (!tp_features.hotkey)
1179 res = mutex_lock_interruptible(&hotkey_mutex);
1183 res = hotkey_get(&status, &mask);
1188 while ((cmd = next_cmd(&buf))) {
1189 if (strlencmp(cmd, "enable") == 0) {
1191 } else if (strlencmp(cmd, "disable") == 0) {
1193 } else if (strlencmp(cmd, "reset") == 0) {
1194 status = hotkey_orig_status;
1195 mask = hotkey_orig_mask;
1196 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1198 } else if (sscanf(cmd, "%x", &mask) == 1) {
1208 res = hotkey_set(status, mask);
1211 mutex_unlock(&hotkey_mutex);
1215 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1216 .hid = IBM_HKEY_HID,
1217 .notify = hotkey_notify,
1218 .handle = &hkey_handle,
1219 .type = ACPI_DEVICE_NOTIFY,
1222 static struct ibm_struct hotkey_driver_data = {
1224 .read = hotkey_read,
1225 .write = hotkey_write,
1226 .exit = hotkey_exit,
1227 .acpi = &ibm_hotkey_acpidriver,
1230 /*************************************************************************
1231 * Bluetooth subdriver
1234 /* sysfs bluetooth enable ---------------------------------------------- */
1235 static ssize_t bluetooth_enable_show(struct device *dev,
1236 struct device_attribute *attr,
1241 status = bluetooth_get_radiosw();
1245 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1248 static ssize_t bluetooth_enable_store(struct device *dev,
1249 struct device_attribute *attr,
1250 const char *buf, size_t count)
1255 if (parse_strtoul(buf, 1, &t))
1258 res = bluetooth_set_radiosw(t);
1260 return (res) ? res : count;
1263 static struct device_attribute dev_attr_bluetooth_enable =
1264 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1265 bluetooth_enable_show, bluetooth_enable_store);
1267 /* --------------------------------------------------------------------- */
1269 static struct attribute *bluetooth_attributes[] = {
1270 &dev_attr_bluetooth_enable.attr,
1274 static const struct attribute_group bluetooth_attr_group = {
1275 .attrs = bluetooth_attributes,
1278 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1283 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1285 IBM_ACPIHANDLE_INIT(hkey);
1287 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1288 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1289 tp_features.bluetooth = hkey_handle &&
1290 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1292 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1293 str_supported(tp_features.bluetooth),
1296 if (tp_features.bluetooth) {
1297 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1298 /* no bluetooth hardware present in system */
1299 tp_features.bluetooth = 0;
1300 dbg_printk(TPACPI_DBG_INIT,
1301 "bluetooth hardware not installed\n");
1303 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1304 &bluetooth_attr_group);
1310 return (tp_features.bluetooth)? 0 : 1;
1313 static void bluetooth_exit(void)
1315 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1316 &bluetooth_attr_group);
1319 static int bluetooth_get_radiosw(void)
1323 if (!tp_features.bluetooth)
1326 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1329 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1332 static int bluetooth_set_radiosw(int radio_on)
1336 if (!tp_features.bluetooth)
1339 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1342 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1344 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1345 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1351 /* procfs -------------------------------------------------------------- */
1352 static int bluetooth_read(char *p)
1355 int status = bluetooth_get_radiosw();
1357 if (!tp_features.bluetooth)
1358 len += sprintf(p + len, "status:\t\tnot supported\n");
1360 len += sprintf(p + len, "status:\t\t%s\n",
1361 (status)? "enabled" : "disabled");
1362 len += sprintf(p + len, "commands:\tenable, disable\n");
1368 static int bluetooth_write(char *buf)
1372 if (!tp_features.bluetooth)
1375 while ((cmd = next_cmd(&buf))) {
1376 if (strlencmp(cmd, "enable") == 0) {
1377 bluetooth_set_radiosw(1);
1378 } else if (strlencmp(cmd, "disable") == 0) {
1379 bluetooth_set_radiosw(0);
1387 static struct ibm_struct bluetooth_driver_data = {
1388 .name = "bluetooth",
1389 .read = bluetooth_read,
1390 .write = bluetooth_write,
1391 .exit = bluetooth_exit,
1394 /*************************************************************************
1398 /* sysfs wan enable ---------------------------------------------------- */
1399 static ssize_t wan_enable_show(struct device *dev,
1400 struct device_attribute *attr,
1405 status = wan_get_radiosw();
1409 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1412 static ssize_t wan_enable_store(struct device *dev,
1413 struct device_attribute *attr,
1414 const char *buf, size_t count)
1419 if (parse_strtoul(buf, 1, &t))
1422 res = wan_set_radiosw(t);
1424 return (res) ? res : count;
1427 static struct device_attribute dev_attr_wan_enable =
1428 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1429 wan_enable_show, wan_enable_store);
1431 /* --------------------------------------------------------------------- */
1433 static struct attribute *wan_attributes[] = {
1434 &dev_attr_wan_enable.attr,
1438 static const struct attribute_group wan_attr_group = {
1439 .attrs = wan_attributes,
1442 static int __init wan_init(struct ibm_init_struct *iibm)
1447 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1449 IBM_ACPIHANDLE_INIT(hkey);
1451 tp_features.wan = hkey_handle &&
1452 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1454 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1455 str_supported(tp_features.wan),
1458 if (tp_features.wan) {
1459 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1460 /* no wan hardware present in system */
1461 tp_features.wan = 0;
1462 dbg_printk(TPACPI_DBG_INIT,
1463 "wan hardware not installed\n");
1465 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1472 return (tp_features.wan)? 0 : 1;
1475 static void wan_exit(void)
1477 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1481 static int wan_get_radiosw(void)
1485 if (!tp_features.wan)
1488 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1491 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1494 static int wan_set_radiosw(int radio_on)
1498 if (!tp_features.wan)
1501 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1504 status |= TP_ACPI_WANCARD_RADIOSSW;
1506 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1507 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1513 /* procfs -------------------------------------------------------------- */
1514 static int wan_read(char *p)
1517 int status = wan_get_radiosw();
1519 if (!tp_features.wan)
1520 len += sprintf(p + len, "status:\t\tnot supported\n");
1522 len += sprintf(p + len, "status:\t\t%s\n",
1523 (status)? "enabled" : "disabled");
1524 len += sprintf(p + len, "commands:\tenable, disable\n");
1530 static int wan_write(char *buf)
1534 if (!tp_features.wan)
1537 while ((cmd = next_cmd(&buf))) {
1538 if (strlencmp(cmd, "enable") == 0) {
1540 } else if (strlencmp(cmd, "disable") == 0) {
1549 static struct ibm_struct wan_driver_data = {
1554 .flags.experimental = 1,
1557 /*************************************************************************
1561 static enum video_access_mode video_supported;
1562 static int video_orig_autosw;
1564 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1565 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1566 "\\_SB.PCI0.VID0", /* 770e */
1567 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1568 "\\_SB.PCI0.AGP.VID", /* all others */
1571 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1573 static int __init video_init(struct ibm_init_struct *iibm)
1577 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1579 IBM_ACPIHANDLE_INIT(vid);
1580 IBM_ACPIHANDLE_INIT(vid2);
1582 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1583 /* G41, assume IVGA doesn't change */
1584 vid_handle = vid2_handle;
1587 /* video switching not supported on R30, R31 */
1588 video_supported = TPACPI_VIDEO_NONE;
1589 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1591 video_supported = TPACPI_VIDEO_570;
1592 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1593 /* 600e/x, 770e, 770x */
1594 video_supported = TPACPI_VIDEO_770;
1597 video_supported = TPACPI_VIDEO_NEW;
1599 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1600 str_supported(video_supported != TPACPI_VIDEO_NONE),
1603 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1606 static void video_exit(void)
1608 dbg_printk(TPACPI_DBG_EXIT,
1609 "restoring original video autoswitch mode\n");
1610 if (video_autosw_set(video_orig_autosw))
1611 printk(IBM_ERR "error while trying to restore original "
1612 "video autoswitch mode\n");
1615 static int video_outputsw_get(void)
1620 switch (video_supported) {
1621 case TPACPI_VIDEO_570:
1622 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1623 TP_ACPI_VIDEO_570_PHSCMD))
1625 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1627 case TPACPI_VIDEO_770:
1628 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1631 status |= TP_ACPI_VIDEO_S_LCD;
1632 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1635 status |= TP_ACPI_VIDEO_S_CRT;
1637 case TPACPI_VIDEO_NEW:
1638 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1639 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1642 status |= TP_ACPI_VIDEO_S_CRT;
1644 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1645 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1648 status |= TP_ACPI_VIDEO_S_LCD;
1649 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1652 status |= TP_ACPI_VIDEO_S_DVI;
1661 static int video_outputsw_set(int status)
1666 switch (video_supported) {
1667 case TPACPI_VIDEO_570:
1668 res = acpi_evalf(NULL, NULL,
1669 "\\_SB.PHS2", "vdd",
1670 TP_ACPI_VIDEO_570_PHS2CMD,
1671 status | TP_ACPI_VIDEO_570_PHS2SET);
1673 case TPACPI_VIDEO_770:
1674 autosw = video_autosw_get();
1678 res = video_autosw_set(1);
1681 res = acpi_evalf(vid_handle, NULL,
1682 "ASWT", "vdd", status * 0x100, 0);
1683 if (!autosw && video_autosw_set(autosw)) {
1684 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1688 case TPACPI_VIDEO_NEW:
1689 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1690 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1696 return (res)? 0 : -EIO;
1699 static int video_autosw_get(void)
1703 switch (video_supported) {
1704 case TPACPI_VIDEO_570:
1705 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1708 case TPACPI_VIDEO_770:
1709 case TPACPI_VIDEO_NEW:
1710 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1720 static int video_autosw_set(int enable)
1722 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1727 static int video_outputsw_cycle(void)
1729 int autosw = video_autosw_get();
1735 switch (video_supported) {
1736 case TPACPI_VIDEO_570:
1737 res = video_autosw_set(1);
1740 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1742 case TPACPI_VIDEO_770:
1743 case TPACPI_VIDEO_NEW:
1744 res = video_autosw_set(1);
1747 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1752 if (!autosw && video_autosw_set(autosw)) {
1753 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1757 return (res)? 0 : -EIO;
1760 static int video_expand_toggle(void)
1762 switch (video_supported) {
1763 case TPACPI_VIDEO_570:
1764 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1766 case TPACPI_VIDEO_770:
1767 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1769 case TPACPI_VIDEO_NEW:
1770 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1778 static int video_read(char *p)
1783 if (video_supported == TPACPI_VIDEO_NONE) {
1784 len += sprintf(p + len, "status:\t\tnot supported\n");
1788 status = video_outputsw_get();
1792 autosw = video_autosw_get();
1796 len += sprintf(p + len, "status:\t\tsupported\n");
1797 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1798 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1799 if (video_supported == TPACPI_VIDEO_NEW)
1800 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1801 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1802 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1803 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1804 if (video_supported == TPACPI_VIDEO_NEW)
1805 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1806 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1807 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1812 static int video_write(char *buf)
1815 int enable, disable, status;
1818 if (video_supported == TPACPI_VIDEO_NONE)
1824 while ((cmd = next_cmd(&buf))) {
1825 if (strlencmp(cmd, "lcd_enable") == 0) {
1826 enable |= TP_ACPI_VIDEO_S_LCD;
1827 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1828 disable |= TP_ACPI_VIDEO_S_LCD;
1829 } else if (strlencmp(cmd, "crt_enable") == 0) {
1830 enable |= TP_ACPI_VIDEO_S_CRT;
1831 } else if (strlencmp(cmd, "crt_disable") == 0) {
1832 disable |= TP_ACPI_VIDEO_S_CRT;
1833 } else if (video_supported == TPACPI_VIDEO_NEW &&
1834 strlencmp(cmd, "dvi_enable") == 0) {
1835 enable |= TP_ACPI_VIDEO_S_DVI;
1836 } else if (video_supported == TPACPI_VIDEO_NEW &&
1837 strlencmp(cmd, "dvi_disable") == 0) {
1838 disable |= TP_ACPI_VIDEO_S_DVI;
1839 } else if (strlencmp(cmd, "auto_enable") == 0) {
1840 res = video_autosw_set(1);
1843 } else if (strlencmp(cmd, "auto_disable") == 0) {
1844 res = video_autosw_set(0);
1847 } else if (strlencmp(cmd, "video_switch") == 0) {
1848 res = video_outputsw_cycle();
1851 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1852 res = video_expand_toggle();
1859 if (enable || disable) {
1860 status = video_outputsw_get();
1863 res = video_outputsw_set((status & ~disable) | enable);
1871 static struct ibm_struct video_driver_data = {
1874 .write = video_write,
1878 /*************************************************************************
1879 * Light (thinklight) subdriver
1882 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1883 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1885 static int __init light_init(struct ibm_init_struct *iibm)
1887 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1889 IBM_ACPIHANDLE_INIT(ledb);
1890 IBM_ACPIHANDLE_INIT(lght);
1891 IBM_ACPIHANDLE_INIT(cmos);
1893 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1894 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1896 if (tp_features.light)
1897 /* light status not supported on
1898 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1899 tp_features.light_status =
1900 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1902 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1903 str_supported(tp_features.light));
1905 return (tp_features.light)? 0 : 1;
1908 static int light_read(char *p)
1913 if (!tp_features.light) {
1914 len += sprintf(p + len, "status:\t\tnot supported\n");
1915 } else if (!tp_features.light_status) {
1916 len += sprintf(p + len, "status:\t\tunknown\n");
1917 len += sprintf(p + len, "commands:\ton, off\n");
1919 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1921 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1922 len += sprintf(p + len, "commands:\ton, off\n");
1928 static int light_write(char *buf)
1930 int cmos_cmd, lght_cmd;
1934 if (!tp_features.light)
1937 while ((cmd = next_cmd(&buf))) {
1938 if (strlencmp(cmd, "on") == 0) {
1941 } else if (strlencmp(cmd, "off") == 0) {
1947 success = cmos_handle ?
1948 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1949 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1957 static struct ibm_struct light_driver_data = {
1960 .write = light_write,
1963 /*************************************************************************
1967 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1969 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1970 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1971 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1972 "\\_SB.PCI.ISA.SLCE", /* 570 */
1973 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1975 /* don't list other alternatives as we install a notify handler on the 570 */
1976 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1978 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1980 .notify = dock_notify,
1981 .handle = &dock_handle,
1982 .type = ACPI_SYSTEM_NOTIFY,
1986 .notify = dock_notify,
1987 .handle = &pci_handle,
1988 .type = ACPI_SYSTEM_NOTIFY,
1992 static struct ibm_struct dock_driver_data[2] = {
1996 .write = dock_write,
1997 .acpi = &ibm_dock_acpidriver[0],
2001 .acpi = &ibm_dock_acpidriver[1],
2005 #define dock_docked() (_sta(dock_handle) & 1)
2007 static int __init dock_init(struct ibm_init_struct *iibm)
2009 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2011 IBM_ACPIHANDLE_INIT(dock);
2013 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2014 str_supported(dock_handle != NULL));
2016 return (dock_handle)? 0 : 1;
2019 static int __init dock_init2(struct ibm_init_struct *iibm)
2023 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2025 if (dock_driver_data[0].flags.acpi_driver_registered &&
2026 dock_driver_data[0].flags.acpi_notify_installed) {
2027 IBM_ACPIHANDLE_INIT(pci);
2028 dock2_needed = (pci_handle != NULL);
2029 vdbg_printk(TPACPI_DBG_INIT,
2030 "dock PCI handler for the TP 570 is %s\n",
2031 str_supported(dock2_needed));
2033 vdbg_printk(TPACPI_DBG_INIT,
2034 "dock subdriver part 2 not required\n");
2038 return (dock2_needed)? 0 : 1;
2041 static void dock_notify(struct ibm_struct *ibm, u32 event)
2043 int docked = dock_docked();
2044 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
2046 if (event == 1 && !pci) /* 570 */
2047 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2048 else if (event == 1 && pci) /* 570 */
2049 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2050 else if (event == 3 && docked)
2051 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2052 else if (event == 3 && !docked)
2053 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2054 else if (event == 0 && docked)
2055 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2057 printk(IBM_ERR "unknown dock event %d, status %d\n",
2058 event, _sta(dock_handle));
2059 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2063 static int dock_read(char *p)
2066 int docked = dock_docked();
2069 len += sprintf(p + len, "status:\t\tnot supported\n");
2071 len += sprintf(p + len, "status:\t\tundocked\n");
2073 len += sprintf(p + len, "status:\t\tdocked\n");
2074 len += sprintf(p + len, "commands:\tdock, undock\n");
2080 static int dock_write(char *buf)
2087 while ((cmd = next_cmd(&buf))) {
2088 if (strlencmp(cmd, "undock") == 0) {
2089 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2090 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2092 } else if (strlencmp(cmd, "dock") == 0) {
2093 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2102 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2104 /*************************************************************************
2108 #ifdef CONFIG_THINKPAD_ACPI_BAY
2109 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2110 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2111 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2112 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2113 ); /* A21e, R30, R31 */
2114 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2115 "_EJ0", /* all others */
2116 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2117 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2118 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2120 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2124 static int __init bay_init(struct ibm_init_struct *iibm)
2126 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2128 IBM_ACPIHANDLE_INIT(bay);
2130 IBM_ACPIHANDLE_INIT(bay_ej);
2131 IBM_ACPIHANDLE_INIT(bay2);
2133 IBM_ACPIHANDLE_INIT(bay2_ej);
2135 tp_features.bay_status = bay_handle &&
2136 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2137 tp_features.bay_status2 = bay2_handle &&
2138 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2140 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2141 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2142 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2143 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2145 vdbg_printk(TPACPI_DBG_INIT,
2146 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2147 str_supported(tp_features.bay_status),
2148 str_supported(tp_features.bay_eject),
2149 str_supported(tp_features.bay_status2),
2150 str_supported(tp_features.bay_eject2));
2152 return (tp_features.bay_status || tp_features.bay_eject ||
2153 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2156 static void bay_notify(struct ibm_struct *ibm, u32 event)
2158 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2161 #define bay_occupied(b) (_sta(b##_handle) & 1)
2163 static int bay_read(char *p)
2166 int occupied = bay_occupied(bay);
2167 int occupied2 = bay_occupied(bay2);
2170 len += sprintf(p + len, "status:\t\t%s\n",
2171 tp_features.bay_status ?
2172 (occupied ? "occupied" : "unoccupied") :
2174 if (tp_features.bay_status2)
2175 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2176 "occupied" : "unoccupied");
2178 eject = tp_features.bay_eject && occupied;
2179 eject2 = tp_features.bay_eject2 && occupied2;
2181 if (eject && eject2)
2182 len += sprintf(p + len, "commands:\teject, eject2\n");
2184 len += sprintf(p + len, "commands:\teject\n");
2186 len += sprintf(p + len, "commands:\teject2\n");
2191 static int bay_write(char *buf)
2195 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2198 while ((cmd = next_cmd(&buf))) {
2199 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2200 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2202 } else if (tp_features.bay_eject2 &&
2203 strlencmp(cmd, "eject2") == 0) {
2204 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2213 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2214 .notify = bay_notify,
2215 .handle = &bay_handle,
2216 .type = ACPI_SYSTEM_NOTIFY,
2219 static struct ibm_struct bay_driver_data = {
2223 .acpi = &ibm_bay_acpidriver,
2226 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2228 /*************************************************************************
2232 /* sysfs cmos_command -------------------------------------------------- */
2233 static ssize_t cmos_command_store(struct device *dev,
2234 struct device_attribute *attr,
2235 const char *buf, size_t count)
2237 unsigned long cmos_cmd;
2240 if (parse_strtoul(buf, 21, &cmos_cmd))
2243 res = issue_thinkpad_cmos_command(cmos_cmd);
2244 return (res)? res : count;
2247 static struct device_attribute dev_attr_cmos_command =
2248 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2250 /* --------------------------------------------------------------------- */
2252 static int __init cmos_init(struct ibm_init_struct *iibm)
2256 vdbg_printk(TPACPI_DBG_INIT,
2257 "initializing cmos commands subdriver\n");
2259 IBM_ACPIHANDLE_INIT(cmos);
2261 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2262 str_supported(cmos_handle != NULL));
2264 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2268 return (cmos_handle)? 0 : 1;
2271 static void cmos_exit(void)
2273 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2276 static int cmos_read(char *p)
2280 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2281 R30, R31, T20-22, X20-21 */
2283 len += sprintf(p + len, "status:\t\tnot supported\n");
2285 len += sprintf(p + len, "status:\t\tsupported\n");
2286 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2292 static int cmos_write(char *buf)
2297 while ((cmd = next_cmd(&buf))) {
2298 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2299 cmos_cmd >= 0 && cmos_cmd <= 21) {
2304 res = issue_thinkpad_cmos_command(cmos_cmd);
2312 static struct ibm_struct cmos_driver_data = {
2315 .write = cmos_write,
2319 /*************************************************************************
2323 static enum led_access_mode led_supported;
2325 IBM_HANDLE(led, ec, "SLED", /* 570 */
2326 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2327 "LED", /* all others */
2330 static int __init led_init(struct ibm_init_struct *iibm)
2332 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2334 IBM_ACPIHANDLE_INIT(led);
2337 /* led not supported on R30, R31 */
2338 led_supported = TPACPI_LED_NONE;
2339 else if (strlencmp(led_path, "SLED") == 0)
2341 led_supported = TPACPI_LED_570;
2342 else if (strlencmp(led_path, "SYSL") == 0)
2343 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2344 led_supported = TPACPI_LED_OLD;
2347 led_supported = TPACPI_LED_NEW;
2349 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2350 str_supported(led_supported), led_supported);
2352 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2355 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2357 static int led_read(char *p)
2361 if (!led_supported) {
2362 len += sprintf(p + len, "status:\t\tnot supported\n");
2365 len += sprintf(p + len, "status:\t\tsupported\n");
2367 if (led_supported == TPACPI_LED_570) {
2370 for (i = 0; i < 8; i++) {
2371 if (!acpi_evalf(ec_handle,
2372 &status, "GLED", "dd", 1 << i))
2374 len += sprintf(p + len, "%d:\t\t%s\n",
2375 i, led_status(status));
2379 len += sprintf(p + len, "commands:\t"
2380 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2385 /* off, on, blink */
2386 static const int led_sled_arg1[] = { 0, 1, 3 };
2387 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2388 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2389 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2391 static int led_write(char *buf)
2399 while ((cmd = next_cmd(&buf))) {
2400 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2403 if (strstr(cmd, "off")) {
2405 } else if (strstr(cmd, "on")) {
2407 } else if (strstr(cmd, "blink")) {
2412 if (led_supported == TPACPI_LED_570) {
2415 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2416 led, led_sled_arg1[ind]))
2418 } else if (led_supported == TPACPI_LED_OLD) {
2419 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2421 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2424 ec_write(TPACPI_LED_EC_HLBL,
2425 led * led_exp_hlbl[ind]);
2428 ec_write(TPACPI_LED_EC_HLCL,
2429 led * led_exp_hlcl[ind]);
2434 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2435 led, led_led_arg1[ind]))
2443 static struct ibm_struct led_driver_data = {
2449 /*************************************************************************
2453 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2455 static int __init beep_init(struct ibm_init_struct *iibm)
2457 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2459 IBM_ACPIHANDLE_INIT(beep);
2461 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2462 str_supported(beep_handle != NULL));
2464 return (beep_handle)? 0 : 1;
2467 static int beep_read(char *p)
2472 len += sprintf(p + len, "status:\t\tnot supported\n");
2474 len += sprintf(p + len, "status:\t\tsupported\n");
2475 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2481 static int beep_write(char *buf)
2489 while ((cmd = next_cmd(&buf))) {
2490 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2491 beep_cmd >= 0 && beep_cmd <= 17) {
2495 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2502 static struct ibm_struct beep_driver_data = {
2505 .write = beep_write,
2508 /*************************************************************************
2512 static enum thermal_access_mode thermal_read_mode;
2514 /* sysfs temp##_input -------------------------------------------------- */
2516 static ssize_t thermal_temp_input_show(struct device *dev,
2517 struct device_attribute *attr,
2520 struct sensor_device_attribute *sensor_attr =
2521 to_sensor_dev_attr(attr);
2522 int idx = sensor_attr->index;
2526 res = thermal_get_sensor(idx, &value);
2529 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2532 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2535 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2536 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2538 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2539 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2540 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2541 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2542 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2543 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2544 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2545 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2546 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2547 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2548 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2549 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2550 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2551 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2552 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2553 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2554 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2557 #define THERMAL_ATTRS(X) \
2558 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2560 static struct attribute *thermal_temp_input_attr[] = {
2580 static const struct attribute_group thermal_temp_input16_group = {
2581 .attrs = thermal_temp_input_attr
2584 static const struct attribute_group thermal_temp_input8_group = {
2585 .attrs = &thermal_temp_input_attr[8]
2588 #undef THERMAL_SENSOR_ATTR_TEMP
2589 #undef THERMAL_ATTRS
2591 /* --------------------------------------------------------------------- */
2593 static int __init thermal_init(struct ibm_init_struct *iibm)
2600 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2602 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2604 if (ibm_thinkpad_ec_found && experimental) {
2606 * Direct EC access mode: sensors at registers
2607 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2608 * non-implemented, thermal sensors return 0x80 when
2613 for (i = 0; i < 8; i++) {
2614 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2620 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2628 /* This is sheer paranoia, but we handle it anyway */
2631 "ThinkPad ACPI EC access misbehaving, "
2632 "falling back to ACPI TMPx access mode\n");
2633 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2636 "ThinkPad ACPI EC access misbehaving, "
2637 "disabling thermal sensors access\n");
2638 thermal_read_mode = TPACPI_THERMAL_NONE;
2643 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2645 } else if (acpi_tmp7) {
2646 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2647 /* 600e/x, 770e, 770x */
2648 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2650 /* Standard ACPI TMPx access, max 8 sensors */
2651 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2654 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2655 thermal_read_mode = TPACPI_THERMAL_NONE;
2658 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2659 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2662 switch(thermal_read_mode) {
2663 case TPACPI_THERMAL_TPEC_16:
2664 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2665 &thermal_temp_input16_group);
2669 case TPACPI_THERMAL_TPEC_8:
2670 case TPACPI_THERMAL_ACPI_TMP07:
2671 case TPACPI_THERMAL_ACPI_UPDT:
2672 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2673 &thermal_temp_input8_group);
2677 case TPACPI_THERMAL_NONE:
2685 static void thermal_exit(void)
2687 switch(thermal_read_mode) {
2688 case TPACPI_THERMAL_TPEC_16:
2689 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2690 &thermal_temp_input16_group);
2692 case TPACPI_THERMAL_TPEC_8:
2693 case TPACPI_THERMAL_ACPI_TMP07:
2694 case TPACPI_THERMAL_ACPI_UPDT:
2695 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2696 &thermal_temp_input16_group);
2698 case TPACPI_THERMAL_NONE:
2704 /* idx is zero-based */
2705 static int thermal_get_sensor(int idx, s32 *value)
2711 t = TP_EC_THERMAL_TMP0;
2713 switch (thermal_read_mode) {
2714 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2715 case TPACPI_THERMAL_TPEC_16:
2716 if (idx >= 8 && idx <= 15) {
2717 t = TP_EC_THERMAL_TMP8;
2722 case TPACPI_THERMAL_TPEC_8:
2724 if (!acpi_ec_read(t + idx, &tmp))
2726 *value = tmp * 1000;
2731 case TPACPI_THERMAL_ACPI_UPDT:
2733 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2734 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2736 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2738 *value = (t - 2732) * 100;
2743 case TPACPI_THERMAL_ACPI_TMP07:
2745 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2746 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2753 case TPACPI_THERMAL_NONE:
2761 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2772 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2775 for(i = 0 ; i < n; i++) {
2776 res = thermal_get_sensor(i, &s->temp[i]);
2784 static int thermal_read(char *p)
2788 struct ibm_thermal_sensors_struct t;
2790 n = thermal_get_sensors(&t);
2791 if (unlikely(n < 0))
2794 len += sprintf(p + len, "temperatures:\t");
2797 for (i = 0; i < (n - 1); i++)
2798 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2799 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2801 len += sprintf(p + len, "not supported\n");
2806 static struct ibm_struct thermal_driver_data = {
2808 .read = thermal_read,
2809 .exit = thermal_exit,
2812 /*************************************************************************
2816 static u8 ecdump_regs[256];
2818 static int ecdump_read(char *p)
2824 len += sprintf(p + len, "EC "
2825 " +00 +01 +02 +03 +04 +05 +06 +07"
2826 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2827 for (i = 0; i < 256; i += 16) {
2828 len += sprintf(p + len, "EC 0x%02x:", i);
2829 for (j = 0; j < 16; j++) {
2830 if (!acpi_ec_read(i + j, &v))
2832 if (v != ecdump_regs[i + j])
2833 len += sprintf(p + len, " *%02x", v);
2835 len += sprintf(p + len, " %02x", v);
2836 ecdump_regs[i + j] = v;
2838 len += sprintf(p + len, "\n");
2843 /* These are way too dangerous to advertise openly... */
2845 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2846 " (<offset> is 00-ff, <value> is 00-ff)\n");
2847 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2848 " (<offset> is 00-ff, <value> is 0-255)\n");
2853 static int ecdump_write(char *buf)
2858 while ((cmd = next_cmd(&buf))) {
2859 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2861 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2865 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2866 if (!acpi_ec_write(i, v))
2875 static struct ibm_struct ecdump_driver_data = {
2877 .read = ecdump_read,
2878 .write = ecdump_write,
2879 .flags.experimental = 1,
2882 /*************************************************************************
2883 * Backlight/brightness subdriver
2886 static struct backlight_device *ibm_backlight_device;
2888 static struct backlight_ops ibm_backlight_data = {
2889 .get_brightness = brightness_get,
2890 .update_status = brightness_update_status,
2893 static int __init brightness_init(struct ibm_init_struct *iibm)
2897 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2899 b = brightness_get(NULL);
2903 ibm_backlight_device = backlight_device_register(
2904 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2905 &ibm_backlight_data);
2906 if (IS_ERR(ibm_backlight_device)) {
2907 printk(IBM_ERR "Could not register backlight device\n");
2908 return PTR_ERR(ibm_backlight_device);
2910 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2912 ibm_backlight_device->props.max_brightness = 7;
2913 ibm_backlight_device->props.brightness = b;
2914 backlight_update_status(ibm_backlight_device);
2919 static void brightness_exit(void)
2921 if (ibm_backlight_device) {
2922 vdbg_printk(TPACPI_DBG_EXIT,
2923 "calling backlight_device_unregister()\n");
2924 backlight_device_unregister(ibm_backlight_device);
2925 ibm_backlight_device = NULL;
2929 static int brightness_update_status(struct backlight_device *bd)
2931 return brightness_set(
2932 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2933 bd->props.power == FB_BLANK_UNBLANK) ?
2934 bd->props.brightness : 0);
2937 static int brightness_get(struct backlight_device *bd)
2940 if (!acpi_ec_read(brightness_offset, &level))
2948 static int brightness_set(int value)
2950 int cmos_cmd, inc, i;
2951 int current_value = brightness_get(NULL);
2955 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2956 inc = value > current_value ? 1 : -1;
2957 for (i = current_value; i != value; i += inc) {
2958 if (issue_thinkpad_cmos_command(cmos_cmd))
2960 if (!acpi_ec_write(brightness_offset, i + inc))
2967 static int brightness_read(char *p)
2972 if ((level = brightness_get(NULL)) < 0) {
2973 len += sprintf(p + len, "level:\t\tunreadable\n");
2975 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2976 len += sprintf(p + len, "commands:\tup, down\n");
2977 len += sprintf(p + len, "commands:\tlevel <level>"
2978 " (<level> is 0-7)\n");
2984 static int brightness_write(char *buf)
2990 while ((cmd = next_cmd(&buf))) {
2991 if ((level = brightness_get(NULL)) < 0)
2995 if (strlencmp(cmd, "up") == 0) {
2996 new_level = level == 7 ? 7 : level + 1;
2997 } else if (strlencmp(cmd, "down") == 0) {
2998 new_level = level == 0 ? 0 : level - 1;
2999 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3000 new_level >= 0 && new_level <= 7) {
3005 brightness_set(new_level);
3011 static struct ibm_struct brightness_driver_data = {
3012 .name = "brightness",
3013 .read = brightness_read,
3014 .write = brightness_write,
3015 .exit = brightness_exit,
3018 /*************************************************************************
3022 static int volume_read(char *p)
3027 if (!acpi_ec_read(volume_offset, &level)) {
3028 len += sprintf(p + len, "level:\t\tunreadable\n");
3030 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3031 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3032 len += sprintf(p + len, "commands:\tup, down, mute\n");
3033 len += sprintf(p + len, "commands:\tlevel <level>"
3034 " (<level> is 0-15)\n");
3040 static int volume_write(char *buf)
3042 int cmos_cmd, inc, i;
3044 int new_level, new_mute;
3047 while ((cmd = next_cmd(&buf))) {
3048 if (!acpi_ec_read(volume_offset, &level))
3050 new_mute = mute = level & 0x40;
3051 new_level = level = level & 0xf;
3053 if (strlencmp(cmd, "up") == 0) {
3057 new_level = level == 15 ? 15 : level + 1;
3058 } else if (strlencmp(cmd, "down") == 0) {
3062 new_level = level == 0 ? 0 : level - 1;
3063 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3064 new_level >= 0 && new_level <= 15) {
3066 } else if (strlencmp(cmd, "mute") == 0) {
3071 if (new_level != level) { /* mute doesn't change */
3072 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3073 inc = new_level > level ? 1 : -1;
3075 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3076 !acpi_ec_write(volume_offset, level)))
3079 for (i = level; i != new_level; i += inc)
3080 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3081 !acpi_ec_write(volume_offset, i + inc))
3084 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3085 !acpi_ec_write(volume_offset,
3090 if (new_mute != mute) { /* level doesn't change */
3091 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3093 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3094 !acpi_ec_write(volume_offset, level + new_mute))
3102 static struct ibm_struct volume_driver_data = {
3104 .read = volume_read,
3105 .write = volume_write,
3108 /*************************************************************************
3115 * TPACPI_FAN_RD_ACPI_GFAN:
3116 * ACPI GFAN method: returns fan level
3118 * see TPACPI_FAN_WR_ACPI_SFAN
3119 * EC 0x2f (HFSP) not available if GFAN exists
3121 * TPACPI_FAN_WR_ACPI_SFAN:
3122 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3124 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3127 * TPACPI_FAN_WR_TPEC:
3128 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3129 * Supported on almost all ThinkPads
3131 * Fan speed changes of any sort (including those caused by the
3132 * disengaged mode) are usually done slowly by the firmware as the
3133 * maximum ammount of fan duty cycle change per second seems to be
3136 * Reading is not available if GFAN exists.
3137 * Writing is not available if SFAN exists.
3140 * 7 automatic mode engaged;
3141 * (default operation mode of the ThinkPad)
3142 * fan level is ignored in this mode.
3143 * 6 full speed mode (takes precedence over bit 7);
3144 * not available on all thinkpads. May disable
3145 * the tachometer while the fan controller ramps up
3146 * the speed (which can take up to a few *minutes*).
3147 * Speeds up fan to 100% duty-cycle, which is far above
3148 * the standard RPM levels. It is not impossible that
3149 * it could cause hardware damage.
3150 * 5-3 unused in some models. Extra bits for fan level
3151 * in others, but still useless as all values above
3152 * 7 map to the same speed as level 7 in these models.
3153 * 2-0 fan level (0..7 usually)
3155 * 0x07 = max (set when temperatures critical)
3156 * Some ThinkPads may have other levels, see
3157 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3159 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3160 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3161 * does so, its initial value is meaningless (0x07).
3163 * For firmware bugs, refer to:
3164 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3168 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3169 * Main fan tachometer reading (in RPM)
3171 * This register is present on all ThinkPads with a new-style EC, and
3172 * it is known not to be present on the A21m/e, and T22, as there is
3173 * something else in offset 0x84 according to the ACPI DSDT. Other
3174 * ThinkPads from this same time period (and earlier) probably lack the
3175 * tachometer as well.
3177 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3178 * was never fixed by IBM to report the EC firmware version string
3179 * probably support the tachometer (like the early X models), so
3180 * detecting it is quite hard. We need more data to know for sure.
3182 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3185 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3188 * For firmware bugs, refer to:
3189 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3191 * TPACPI_FAN_WR_ACPI_FANS:
3192 * ThinkPad X31, X40, X41. Not available in the X60.
3194 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3195 * high speed. ACPI DSDT seems to map these three speeds to levels
3196 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3197 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3199 * The speeds are stored on handles
3200 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3202 * There are three default speed sets, acessible as handles:
3203 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3205 * ACPI DSDT switches which set is in use depending on various
3208 * TPACPI_FAN_WR_TPEC is also available and should be used to
3209 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3210 * but the ACPI tables just mention level 7.
3213 static enum fan_status_access_mode fan_status_access_mode;
3214 static enum fan_control_access_mode fan_control_access_mode;
3215 static enum fan_control_commands fan_control_commands;
3217 static u8 fan_control_initial_status;
3218 static u8 fan_control_desired_level;
3220 static void fan_watchdog_fire(struct work_struct *ignored);
3221 static int fan_watchdog_maxinterval;
3222 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3224 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3225 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3226 "\\FSPD", /* 600e/x, 770e, 770x */
3228 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3229 "JFNS", /* 770x-JL */
3233 * SYSFS fan layout: hwmon compatible (device)
3236 * 0: "disengaged" mode
3238 * 2: native EC "auto" mode (recommended, hardware default)
3240 * pwm*: set speed in manual mode, ignored otherwise.
3241 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3244 * fan*_input: tachometer reading, RPM
3247 * SYSFS fan layout: extensions
3249 * fan_watchdog (driver):
3250 * fan watchdog interval in seconds, 0 disables (default), max 120
3253 /* sysfs fan pwm1_enable ----------------------------------------------- */
3254 static ssize_t fan_pwm1_enable_show(struct device *dev,
3255 struct device_attribute *attr,
3261 res = fan_get_status_safe(&status);
3265 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3266 if (status != fan_control_initial_status) {
3267 tp_features.fan_ctrl_status_undef = 0;
3269 /* Return most likely status. In fact, it
3270 * might be the only possible status */
3271 status = TP_EC_FAN_AUTO;
3275 if (status & TP_EC_FAN_FULLSPEED) {
3277 } else if (status & TP_EC_FAN_AUTO) {
3282 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3285 static ssize_t fan_pwm1_enable_store(struct device *dev,
3286 struct device_attribute *attr,
3287 const char *buf, size_t count)
3292 if (parse_strtoul(buf, 2, &t))
3297 level = TP_EC_FAN_FULLSPEED;
3300 level = TPACPI_FAN_LAST_LEVEL;
3303 level = TP_EC_FAN_AUTO;
3306 /* reserved for software-controlled auto mode */
3312 res = fan_set_level_safe(level);
3318 fan_watchdog_reset();
3323 static struct device_attribute dev_attr_fan_pwm1_enable =
3324 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3325 fan_pwm1_enable_show, fan_pwm1_enable_store);
3327 /* sysfs fan pwm1 ------------------------------------------------------ */
3328 static ssize_t fan_pwm1_show(struct device *dev,
3329 struct device_attribute *attr,
3335 res = fan_get_status_safe(&status);
3339 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3340 if (status != fan_control_initial_status) {
3341 tp_features.fan_ctrl_status_undef = 0;
3343 status = TP_EC_FAN_AUTO;
3348 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3349 status = fan_control_desired_level;
3354 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3357 static ssize_t fan_pwm1_store(struct device *dev,
3358 struct device_attribute *attr,
3359 const char *buf, size_t count)
3363 u8 status, newlevel;
3365 if (parse_strtoul(buf, 255, &s))
3368 /* scale down from 0-255 to 0-7 */
3369 newlevel = (s >> 5) & 0x07;
3371 rc = mutex_lock_interruptible(&fan_mutex);
3375 rc = fan_get_status(&status);
3376 if (!rc && (status &
3377 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3378 rc = fan_set_level(newlevel);
3382 fan_update_desired_level(newlevel);
3383 fan_watchdog_reset();
3387 mutex_unlock(&fan_mutex);
3388 return (rc)? rc : count;
3391 static struct device_attribute dev_attr_fan_pwm1 =
3392 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3393 fan_pwm1_show, fan_pwm1_store);
3395 /* sysfs fan fan1_input ------------------------------------------------ */
3396 static ssize_t fan_fan1_input_show(struct device *dev,
3397 struct device_attribute *attr,
3403 res = fan_get_speed(&speed);
3407 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3410 static struct device_attribute dev_attr_fan_fan1_input =
3411 __ATTR(fan1_input, S_IRUGO,
3412 fan_fan1_input_show, NULL);
3414 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3415 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3418 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3421 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3422 const char *buf, size_t count)
3426 if (parse_strtoul(buf, 120, &t))
3429 if (!fan_control_allowed)
3432 fan_watchdog_maxinterval = t;
3433 fan_watchdog_reset();
3438 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3439 fan_fan_watchdog_show, fan_fan_watchdog_store);
3441 /* --------------------------------------------------------------------- */
3442 static struct attribute *fan_attributes[] = {
3443 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3444 &dev_attr_fan_fan1_input.attr,
3448 static const struct attribute_group fan_attr_group = {
3449 .attrs = fan_attributes,
3452 static int __init fan_init(struct ibm_init_struct *iibm)
3456 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3458 mutex_init(&fan_mutex);
3459 fan_status_access_mode = TPACPI_FAN_NONE;
3460 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3461 fan_control_commands = 0;
3462 fan_watchdog_maxinterval = 0;
3463 tp_features.fan_ctrl_status_undef = 0;
3464 fan_control_desired_level = 7;
3466 IBM_ACPIHANDLE_INIT(fans);
3467 IBM_ACPIHANDLE_INIT(gfan);
3468 IBM_ACPIHANDLE_INIT(sfan);
3471 /* 570, 600e/x, 770e, 770x */
3472 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3474 /* all other ThinkPads: note that even old-style
3475 * ThinkPad ECs supports the fan control register */
3476 if (likely(acpi_ec_read(fan_status_offset,
3477 &fan_control_initial_status))) {
3478 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3480 /* In some ThinkPads, neither the EC nor the ACPI
3481 * DSDT initialize the fan status, and it ends up
3482 * being set to 0x07 when it *could* be either
3485 * Enable for TP-1Y (T43), TP-78 (R51e),
3486 * TP-76 (R52), TP-70 (T43, R52), which are known
3488 if (fan_control_initial_status == 0x07 &&
3489 ibm_thinkpad_ec_found &&
3490 ((ibm_thinkpad_ec_found[0] == '1' &&
3491 ibm_thinkpad_ec_found[1] == 'Y') ||
3492 (ibm_thinkpad_ec_found[0] == '7' &&
3493 (ibm_thinkpad_ec_found[1] == '6' ||
3494 ibm_thinkpad_ec_found[1] == '8' ||
3495 ibm_thinkpad_ec_found[1] == '0'))
3498 "fan_init: initial fan status is "
3499 "unknown, assuming it is in auto "
3501 tp_features.fan_ctrl_status_undef = 1;
3505 "ThinkPad ACPI EC access misbehaving, "
3506 "fan status and control unavailable\n");
3513 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3514 fan_control_commands |=
3515 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3518 /* gfan without sfan means no fan control */
3519 /* all other models implement TP EC 0x2f control */
3523 fan_control_access_mode =
3524 TPACPI_FAN_WR_ACPI_FANS;
3525 fan_control_commands |=
3526 TPACPI_FAN_CMD_SPEED |
3527 TPACPI_FAN_CMD_LEVEL |
3528 TPACPI_FAN_CMD_ENABLE;
3530 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3531 fan_control_commands |=
3532 TPACPI_FAN_CMD_LEVEL |
3533 TPACPI_FAN_CMD_ENABLE;
3538 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3539 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3540 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3541 fan_status_access_mode, fan_control_access_mode);
3543 /* fan control master switch */
3544 if (!fan_control_allowed) {
3545 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3546 fan_control_commands = 0;
3547 dbg_printk(TPACPI_DBG_INIT,
3548 "fan control features disabled by parameter\n");
3551 /* update fan_control_desired_level */
3552 if (fan_status_access_mode != TPACPI_FAN_NONE)
3553 fan_get_status_safe(NULL);
3555 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3556 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3557 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3560 rc = driver_create_file(&tpacpi_pdriver.driver,
3561 &driver_attr_fan_watchdog);
3570 * Call with fan_mutex held
3572 static void fan_update_desired_level(u8 status)
3575 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3577 fan_control_desired_level = 7;
3579 fan_control_desired_level = status;
3583 static int fan_get_status(u8 *status)
3588 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3590 switch (fan_status_access_mode) {
3591 case TPACPI_FAN_RD_ACPI_GFAN:
3592 /* 570, 600e/x, 770e, 770x */
3594 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3602 case TPACPI_FAN_RD_TPEC:
3603 /* all except 570, 600e/x, 770e, 770x */
3604 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3619 static int fan_get_status_safe(u8 *status)
3624 rc = mutex_lock_interruptible(&fan_mutex);
3627 rc = fan_get_status(&s);
3629 fan_update_desired_level(s);
3630 mutex_unlock(&fan_mutex);
3638 static void fan_exit(void)
3640 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3642 /* FIXME: can we really do this unconditionally? */
3643 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3644 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3646 cancel_delayed_work(&fan_watchdog_task);
3647 flush_scheduled_work();
3650 static int fan_get_speed(unsigned int *speed)
3654 switch (fan_status_access_mode) {
3655 case TPACPI_FAN_RD_TPEC:
3656 /* all except 570, 600e/x, 770e, 770x */
3657 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3658 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3662 *speed = (hi << 8) | lo;
3673 static void fan_watchdog_fire(struct work_struct *ignored)
3677 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3678 rc = fan_set_enable();
3680 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3681 "will try again later...\n", -rc);
3682 /* reschedule for later */
3683 fan_watchdog_reset();
3687 static void fan_watchdog_reset(void)
3689 static int fan_watchdog_active;
3691 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3694 if (fan_watchdog_active)
3695 cancel_delayed_work(&fan_watchdog_task);
3697 if (fan_watchdog_maxinterval > 0) {
3698 fan_watchdog_active = 1;
3699 if (!schedule_delayed_work(&fan_watchdog_task,
3700 msecs_to_jiffies(fan_watchdog_maxinterval
3702 printk(IBM_ERR "failed to schedule the fan watchdog, "
3703 "watchdog will not trigger\n");
3706 fan_watchdog_active = 0;
3709 static int fan_set_level(int level)
3711 if (!fan_control_allowed)
3714 switch (fan_control_access_mode) {
3715 case TPACPI_FAN_WR_ACPI_SFAN:
3716 if (level >= 0 && level <= 7) {
3717 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3723 case TPACPI_FAN_WR_ACPI_FANS:
3724 case TPACPI_FAN_WR_TPEC:
3725 if ((level != TP_EC_FAN_AUTO) &&
3726 (level != TP_EC_FAN_FULLSPEED) &&
3727 ((level < 0) || (level > 7)))
3730 /* safety net should the EC not support AUTO
3731 * or FULLSPEED mode bits and just ignore them */
3732 if (level & TP_EC_FAN_FULLSPEED)
3733 level |= 7; /* safety min speed 7 */
3734 else if (level & TP_EC_FAN_FULLSPEED)
3735 level |= 4; /* safety min speed 4 */
3737 if (!acpi_ec_write(fan_status_offset, level))
3740 tp_features.fan_ctrl_status_undef = 0;
3749 static int fan_set_level_safe(int level)
3753 if (!fan_control_allowed)
3756 rc = mutex_lock_interruptible(&fan_mutex);
3760 if (level == TPACPI_FAN_LAST_LEVEL)
3761 level = fan_control_desired_level;
3763 rc = fan_set_level(level);
3765 fan_update_desired_level(level);
3767 mutex_unlock(&fan_mutex);
3771 static int fan_set_enable(void)
3776 if (!fan_control_allowed)
3779 rc = mutex_lock_interruptible(&fan_mutex);
3783 switch (fan_control_access_mode) {
3784 case TPACPI_FAN_WR_ACPI_FANS:
3785 case TPACPI_FAN_WR_TPEC:
3786 rc = fan_get_status(&s);
3790 /* Don't go out of emergency fan mode */
3793 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3796 if (!acpi_ec_write(fan_status_offset, s))
3799 tp_features.fan_ctrl_status_undef = 0;
3804 case TPACPI_FAN_WR_ACPI_SFAN:
3805 rc = fan_get_status(&s);
3811 /* Set fan to at least level 4 */
3814 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3824 mutex_unlock(&fan_mutex);
3828 static int fan_set_disable(void)
3832 if (!fan_control_allowed)
3835 rc = mutex_lock_interruptible(&fan_mutex);
3840 switch (fan_control_access_mode) {
3841 case TPACPI_FAN_WR_ACPI_FANS:
3842 case TPACPI_FAN_WR_TPEC:
3843 if (!acpi_ec_write(fan_status_offset, 0x00))
3846 fan_control_desired_level = 0;
3847 tp_features.fan_ctrl_status_undef = 0;
3851 case TPACPI_FAN_WR_ACPI_SFAN:
3852 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3855 fan_control_desired_level = 0;
3863 mutex_unlock(&fan_mutex);
3867 static int fan_set_speed(int speed)
3871 if (!fan_control_allowed)
3874 rc = mutex_lock_interruptible(&fan_mutex);
3879 switch (fan_control_access_mode) {
3880 case TPACPI_FAN_WR_ACPI_FANS:
3881 if (speed >= 0 && speed <= 65535) {
3882 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3883 speed, speed, speed))
3893 mutex_unlock(&fan_mutex);
3897 static int fan_read(char *p)
3902 unsigned int speed = 0;
3904 switch (fan_status_access_mode) {
3905 case TPACPI_FAN_RD_ACPI_GFAN:
3906 /* 570, 600e/x, 770e, 770x */
3907 if ((rc = fan_get_status_safe(&status)) < 0)
3910 len += sprintf(p + len, "status:\t\t%s\n"
3912 (status != 0) ? "enabled" : "disabled", status);
3915 case TPACPI_FAN_RD_TPEC:
3916 /* all except 570, 600e/x, 770e, 770x */
3917 if ((rc = fan_get_status_safe(&status)) < 0)
3920 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3921 if (status != fan_control_initial_status)
3922 tp_features.fan_ctrl_status_undef = 0;
3924 /* Return most likely status. In fact, it
3925 * might be the only possible status */
3926 status = TP_EC_FAN_AUTO;
3929 len += sprintf(p + len, "status:\t\t%s\n",
3930 (status != 0) ? "enabled" : "disabled");
3932 if ((rc = fan_get_speed(&speed)) < 0)
3935 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3937 if (status & TP_EC_FAN_FULLSPEED)
3938 /* Disengaged mode takes precedence */
3939 len += sprintf(p + len, "level:\t\tdisengaged\n");
3940 else if (status & TP_EC_FAN_AUTO)
3941 len += sprintf(p + len, "level:\t\tauto\n");
3943 len += sprintf(p + len, "level:\t\t%d\n", status);
3946 case TPACPI_FAN_NONE:
3948 len += sprintf(p + len, "status:\t\tnot supported\n");
3951 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3952 len += sprintf(p + len, "commands:\tlevel <level>");
3954 switch (fan_control_access_mode) {
3955 case TPACPI_FAN_WR_ACPI_SFAN:
3956 len += sprintf(p + len, " (<level> is 0-7)\n");
3960 len += sprintf(p + len, " (<level> is 0-7, "
3961 "auto, disengaged, full-speed)\n");
3966 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3967 len += sprintf(p + len, "commands:\tenable, disable\n"
3968 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3969 "1-120 (seconds))\n");
3971 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3972 len += sprintf(p + len, "commands:\tspeed <speed>"
3973 " (<speed> is 0-65535)\n");
3978 static int fan_write_cmd_level(const char *cmd, int *rc)
3982 if (strlencmp(cmd, "level auto") == 0)
3983 level = TP_EC_FAN_AUTO;
3984 else if ((strlencmp(cmd, "level disengaged") == 0) |
3985 (strlencmp(cmd, "level full-speed") == 0))
3986 level = TP_EC_FAN_FULLSPEED;
3987 else if (sscanf(cmd, "level %d", &level) != 1)
3990 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3991 printk(IBM_ERR "level command accepted for unsupported "
3992 "access mode %d", fan_control_access_mode);
3997 static int fan_write_cmd_enable(const char *cmd, int *rc)
3999 if (strlencmp(cmd, "enable") != 0)
4002 if ((*rc = fan_set_enable()) == -ENXIO)
4003 printk(IBM_ERR "enable command accepted for unsupported "
4004 "access mode %d", fan_control_access_mode);
4009 static int fan_write_cmd_disable(const char *cmd, int *rc)
4011 if (strlencmp(cmd, "disable") != 0)
4014 if ((*rc = fan_set_disable()) == -ENXIO)
4015 printk(IBM_ERR "disable command accepted for unsupported "
4016 "access mode %d", fan_control_access_mode);
4021 static int fan_write_cmd_speed(const char *cmd, int *rc)
4026 * Support speed <low> <medium> <high> ? */
4028 if (sscanf(cmd, "speed %d", &speed) != 1)
4031 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4032 printk(IBM_ERR "speed command accepted for unsupported "
4033 "access mode %d", fan_control_access_mode);
4038 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4042 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4045 if (interval < 0 || interval > 120)
4048 fan_watchdog_maxinterval = interval;
4053 static int fan_write(char *buf)
4058 while (!rc && (cmd = next_cmd(&buf))) {
4059 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4060 fan_write_cmd_level(cmd, &rc)) &&
4061 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4062 (fan_write_cmd_enable(cmd, &rc) ||
4063 fan_write_cmd_disable(cmd, &rc) ||
4064 fan_write_cmd_watchdog(cmd, &rc))) &&
4065 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4066 fan_write_cmd_speed(cmd, &rc))
4070 fan_watchdog_reset();
4076 static struct ibm_struct fan_driver_data = {
4083 /****************************************************************************
4084 ****************************************************************************
4088 ****************************************************************************
4089 ****************************************************************************/
4092 static struct proc_dir_entry *proc_dir;
4094 /* Subdriver registry */
4095 static LIST_HEAD(tpacpi_all_drivers);
4099 * Module and infrastructure proble, init and exit handling
4102 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4103 static const char * __init str_supported(int is_supported)
4105 static char text_unsupported[] __initdata = "not supported";
4107 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4109 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4111 static int __init ibm_init(struct ibm_init_struct *iibm)
4114 struct ibm_struct *ibm = iibm->data;
4115 struct proc_dir_entry *entry;
4117 BUG_ON(ibm == NULL);
4119 INIT_LIST_HEAD(&ibm->all_drivers);
4121 if (ibm->flags.experimental && !experimental)
4124 dbg_printk(TPACPI_DBG_INIT,
4125 "probing for %s\n", ibm->name);
4128 ret = iibm->init(iibm);
4130 return 0; /* probe failed */
4134 ibm->flags.init_called = 1;
4138 if (ibm->acpi->hid) {
4139 ret = register_tpacpi_subdriver(ibm);
4144 if (ibm->acpi->notify) {
4145 ret = setup_acpi_notify(ibm);
4146 if (ret == -ENODEV) {
4147 printk(IBM_NOTICE "disabling subdriver %s\n",
4157 dbg_printk(TPACPI_DBG_INIT,
4158 "%s installed\n", ibm->name);
4161 entry = create_proc_entry(ibm->name,
4162 S_IFREG | S_IRUGO | S_IWUSR,
4165 printk(IBM_ERR "unable to create proc entry %s\n",
4170 entry->owner = THIS_MODULE;
4172 entry->read_proc = &dispatch_procfs_read;
4174 entry->write_proc = &dispatch_procfs_write;
4175 ibm->flags.proc_created = 1;
4178 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4183 dbg_printk(TPACPI_DBG_INIT,
4184 "%s: at error exit path with result %d\n",
4188 return (ret < 0)? ret : 0;
4191 static void ibm_exit(struct ibm_struct *ibm)
4193 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4195 list_del_init(&ibm->all_drivers);
4197 if (ibm->flags.acpi_notify_installed) {
4198 dbg_printk(TPACPI_DBG_EXIT,
4199 "%s: acpi_remove_notify_handler\n", ibm->name);
4201 acpi_remove_notify_handler(*ibm->acpi->handle,
4203 dispatch_acpi_notify);
4204 ibm->flags.acpi_notify_installed = 0;
4205 ibm->flags.acpi_notify_installed = 0;
4208 if (ibm->flags.proc_created) {
4209 dbg_printk(TPACPI_DBG_EXIT,
4210 "%s: remove_proc_entry\n", ibm->name);
4211 remove_proc_entry(ibm->name, proc_dir);
4212 ibm->flags.proc_created = 0;
4215 if (ibm->flags.acpi_driver_registered) {
4216 dbg_printk(TPACPI_DBG_EXIT,
4217 "%s: acpi_bus_unregister_driver\n", ibm->name);
4219 acpi_bus_unregister_driver(ibm->acpi->driver);
4220 kfree(ibm->acpi->driver);
4221 ibm->acpi->driver = NULL;
4222 ibm->flags.acpi_driver_registered = 0;
4225 if (ibm->flags.init_called && ibm->exit) {
4227 ibm->flags.init_called = 0;
4230 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4235 static char *ibm_thinkpad_ec_found;
4237 static char* __init check_dmi_for_ec(void)
4239 struct dmi_device *dev = NULL;
4240 char ec_fw_string[18];
4243 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4244 * X32 or newer, all Z series; Some models must have an
4245 * up-to-date BIOS or they will not be detected.
4247 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4249 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4250 if (sscanf(dev->name,
4251 "IBM ThinkPad Embedded Controller -[%17c",
4252 ec_fw_string) == 1) {
4253 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4254 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4255 return kstrdup(ec_fw_string, GFP_KERNEL);
4261 static int __init probe_for_thinkpad(void)
4269 * Non-ancient models have better DMI tagging, but very old models
4272 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4274 /* ec is required because many other handles are relative to it */
4275 IBM_ACPIHANDLE_INIT(ec);
4279 "Not yet supported ThinkPad detected!\n");
4284 * Risks a regression on very old machines, but reduces potential
4285 * false positives a damn great deal
4288 is_thinkpad = dmi_name_in_vendors("IBM");
4290 if (!is_thinkpad && !force_load)
4297 /* Module init, exit, parameters */
4299 static struct ibm_init_struct ibms_init[] __initdata = {
4301 .init = thinkpad_acpi_driver_init,
4302 .data = &thinkpad_acpi_driver_data,
4305 .init = hotkey_init,
4306 .data = &hotkey_driver_data,
4309 .init = bluetooth_init,
4310 .data = &bluetooth_driver_data,
4314 .data = &wan_driver_data,
4318 .data = &video_driver_data,
4322 .data = &light_driver_data,
4324 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4327 .data = &dock_driver_data[0],
4331 .data = &dock_driver_data[1],
4334 #ifdef CONFIG_THINKPAD_ACPI_BAY
4337 .data = &bay_driver_data,
4342 .data = &cmos_driver_data,
4346 .data = &led_driver_data,
4350 .data = &beep_driver_data,
4353 .init = thermal_init,
4354 .data = &thermal_driver_data,
4357 .data = &ecdump_driver_data,
4360 .init = brightness_init,
4361 .data = &brightness_driver_data,
4364 .data = &volume_driver_data,
4368 .data = &fan_driver_data,
4372 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4375 struct ibm_struct *ibm;
4377 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4378 ibm = ibms_init[i].data;
4379 BUG_ON(ibm == NULL);
4381 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4382 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4384 strcpy(ibms_init[i].param, val);
4385 strcat(ibms_init[i].param, ",");
4393 static int experimental;
4394 module_param(experimental, int, 0);
4396 static u32 dbg_level;
4397 module_param_named(debug, dbg_level, uint, 0);
4399 static int force_load;
4400 module_param(force_load, int, 0);
4402 static int fan_control_allowed;
4403 module_param_named(fan_control, fan_control_allowed, int, 0);
4405 #define IBM_PARAM(feature) \
4406 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4409 IBM_PARAM(bluetooth);
4412 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4415 #ifdef CONFIG_THINKPAD_ACPI_BAY
4417 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4422 IBM_PARAM(brightness);
4426 static int __init thinkpad_acpi_module_init(void)
4430 /* Driver-level probe */
4431 ret = probe_for_thinkpad();
4435 /* Driver initialization */
4436 ibm_thinkpad_ec_found = check_dmi_for_ec();
4437 IBM_ACPIHANDLE_INIT(ecrd);
4438 IBM_ACPIHANDLE_INIT(ecwr);
4440 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4442 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4443 thinkpad_acpi_module_exit();
4446 proc_dir->owner = THIS_MODULE;
4448 ret = platform_driver_register(&tpacpi_pdriver);
4450 printk(IBM_ERR "unable to register platform driver\n");
4451 thinkpad_acpi_module_exit();
4454 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4456 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4457 thinkpad_acpi_module_exit();
4462 /* Device initialization */
4463 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4465 if (IS_ERR(tpacpi_pdev)) {
4466 ret = PTR_ERR(tpacpi_pdev);
4468 printk(IBM_ERR "unable to register platform device\n");
4469 thinkpad_acpi_module_exit();
4472 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4473 if (IS_ERR(tpacpi_hwmon)) {
4474 ret = PTR_ERR(tpacpi_hwmon);
4475 tpacpi_hwmon = NULL;
4476 printk(IBM_ERR "unable to register hwmon device\n");
4477 thinkpad_acpi_module_exit();
4480 tpacpi_inputdev = input_allocate_device();
4481 if (!tpacpi_inputdev) {
4482 printk(IBM_ERR "unable to allocate input device\n");
4483 thinkpad_acpi_module_exit();
4486 /* Prepare input device, but don't register */
4487 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4488 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4489 tpacpi_inputdev->id.bustype = BUS_HOST;
4490 tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
4491 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4492 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4494 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4495 ret = ibm_init(&ibms_init[i]);
4496 if (ret >= 0 && *ibms_init[i].param)
4497 ret = ibms_init[i].data->write(ibms_init[i].param);
4499 thinkpad_acpi_module_exit();
4503 ret = input_register_device(tpacpi_inputdev);
4505 printk(IBM_ERR "unable to register input device\n");
4506 thinkpad_acpi_module_exit();
4509 tp_features.input_device_registered = 1;
4515 static void thinkpad_acpi_module_exit(void)
4517 struct ibm_struct *ibm, *itmp;
4519 list_for_each_entry_safe_reverse(ibm, itmp,
4520 &tpacpi_all_drivers,
4525 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4527 if (tpacpi_inputdev) {
4528 if (tp_features.input_device_registered)
4529 input_unregister_device(tpacpi_inputdev);
4531 input_free_device(tpacpi_inputdev);
4535 hwmon_device_unregister(tpacpi_hwmon);
4538 platform_device_unregister(tpacpi_pdev);
4540 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4541 platform_driver_unregister(&tpacpi_pdriver);
4544 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4546 kfree(ibm_thinkpad_ec_found);
4549 module_init(thinkpad_acpi_module_init);
4550 module_exit(thinkpad_acpi_module_exit);