2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 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 TPACPI_VERSION "0.19"
25 #define TPACPI_SYSFS_VERSION 0x020200
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
160 #define vdbg_printk(a_dbg_level, format, arg...)
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
174 struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
181 struct acpi_device *device;
187 int (*read) (char *);
188 int (*write) (char *);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
206 struct ibm_init_struct {
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
227 u32 bright_16levels:1;
229 u32 fan_ctrl_status_undef:1;
230 u32 input_device_registered:1;
231 u32 platform_drv_registered:1;
232 u32 platform_drv_attrs_registered:1;
233 u32 sensors_pdrv_registered:1;
234 u32 sensors_pdrv_attrs_registered:1;
235 u32 sensors_pdev_attrs_registered:1;
236 u32 hotkey_poll_active:1;
239 struct thinkpad_id_data {
240 unsigned int vendor; /* ThinkPad vendor:
241 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
243 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
244 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
246 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
251 static struct thinkpad_id_data thinkpad_id;
254 TPACPI_LIFE_INIT = 0,
259 static int experimental;
260 static u32 dbg_level;
262 /****************************************************************************
263 ****************************************************************************
265 * ACPI Helpers and device model
267 ****************************************************************************
268 ****************************************************************************/
270 /*************************************************************************
274 static acpi_handle root_handle;
276 #define TPACPI_HANDLE(object, parent, paths...) \
277 static acpi_handle object##_handle; \
278 static acpi_handle *object##_parent = &parent##_handle; \
279 static char *object##_path; \
280 static char *object##_paths[] = { paths }
282 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
283 "\\_SB.PCI.ISA.EC", /* 570 */
284 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
285 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
286 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
287 "\\_SB.PCI0.ICH3.EC0", /* R31 */
288 "\\_SB.PCI0.LPC.EC", /* all others */
291 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
292 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
294 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
296 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
297 "\\CMS", /* R40, R40e */
300 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
301 "^HKEY", /* R30, R31 */
302 "HKEY", /* all others */
305 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
306 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
307 "\\_SB.PCI0.VID0", /* 770e */
308 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
309 "\\_SB.PCI0.AGP.VID", /* all others */
313 /*************************************************************************
317 static int acpi_evalf(acpi_handle handle,
318 void *res, char *method, char *fmt, ...)
321 struct acpi_object_list params;
322 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
323 struct acpi_buffer result, *resultp;
324 union acpi_object out_obj;
332 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
345 params.pointer = &in_objs[0];
352 in_objs[params.count].integer.value = va_arg(ap, int);
353 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
355 /* add more types as needed */
357 printk(TPACPI_ERR "acpi_evalf() called "
358 "with invalid format character '%c'\n", c);
364 if (res_type != 'v') {
365 result.length = sizeof(out_obj);
366 result.pointer = &out_obj;
371 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
376 *(int *)res = out_obj.integer.value;
377 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
380 success = status == AE_OK;
382 /* add more types as needed */
384 printk(TPACPI_ERR "acpi_evalf() called "
385 "with invalid format character '%c'\n", res_type);
389 if (!success && !quiet)
390 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
391 method, fmt0, status);
396 static int acpi_ec_read(int i, u8 *p)
401 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
405 if (ec_read(i, p) < 0)
412 static int acpi_ec_write(int i, u8 v)
415 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
418 if (ec_write(i, v) < 0)
425 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
426 static int _sta(acpi_handle handle)
430 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
437 static int issue_thinkpad_cmos_command(int cmos_cmd)
442 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
448 /*************************************************************************
452 #define TPACPI_ACPIHANDLE_INIT(object) \
453 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
454 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
456 static void drv_acpi_handle_init(char *name,
457 acpi_handle *handle, acpi_handle parent,
458 char **paths, int num_paths, char **path)
463 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
466 for (i = 0; i < num_paths; i++) {
467 status = acpi_get_handle(parent, paths[i], handle);
468 if (ACPI_SUCCESS(status)) {
470 dbg_printk(TPACPI_DBG_INIT,
471 "Found ACPI handle %s for %s\n",
477 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
482 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
484 struct ibm_struct *ibm = data;
486 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
489 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
492 ibm->acpi->notify(ibm, event);
495 static int __init setup_acpi_notify(struct ibm_struct *ibm)
502 if (!*ibm->acpi->handle)
505 vdbg_printk(TPACPI_DBG_INIT,
506 "setting up ACPI notify for %s\n", ibm->name);
508 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
510 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
515 acpi_driver_data(ibm->acpi->device) = ibm;
516 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
517 TPACPI_ACPI_EVENT_PREFIX,
520 status = acpi_install_notify_handler(*ibm->acpi->handle,
521 ibm->acpi->type, dispatch_acpi_notify, ibm);
522 if (ACPI_FAILURE(status)) {
523 if (status == AE_ALREADY_EXISTS) {
525 "another device driver is already "
526 "handling %s events\n", ibm->name);
529 "acpi_install_notify_handler(%s) failed: %d\n",
534 ibm->flags.acpi_notify_installed = 1;
538 static int __init tpacpi_device_add(struct acpi_device *device)
543 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
547 dbg_printk(TPACPI_DBG_INIT,
548 "registering %s as an ACPI driver\n", ibm->name);
552 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
553 if (!ibm->acpi->driver) {
554 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
558 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
559 ibm->acpi->driver->ids = ibm->acpi->hid;
561 ibm->acpi->driver->ops.add = &tpacpi_device_add;
563 rc = acpi_bus_register_driver(ibm->acpi->driver);
565 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
567 kfree(ibm->acpi->driver);
568 ibm->acpi->driver = NULL;
570 ibm->flags.acpi_driver_registered = 1;
576 /****************************************************************************
577 ****************************************************************************
581 ****************************************************************************
582 ****************************************************************************/
584 static int dispatch_procfs_read(char *page, char **start, off_t off,
585 int count, int *eof, void *data)
587 struct ibm_struct *ibm = data;
590 if (!ibm || !ibm->read)
593 len = ibm->read(page);
597 if (len <= off + count)
609 static int dispatch_procfs_write(struct file *file,
610 const char __user *userbuf,
611 unsigned long count, void *data)
613 struct ibm_struct *ibm = data;
617 if (!ibm || !ibm->write)
620 kernbuf = kmalloc(count + 2, GFP_KERNEL);
624 if (copy_from_user(kernbuf, userbuf, count)) {
630 strcat(kernbuf, ",");
631 ret = ibm->write(kernbuf);
640 static char *next_cmd(char **cmds)
645 while ((end = strchr(start, ',')) && end == start)
657 /****************************************************************************
658 ****************************************************************************
660 * Device model: input, hwmon and platform
662 ****************************************************************************
663 ****************************************************************************/
665 static struct platform_device *tpacpi_pdev;
666 static struct platform_device *tpacpi_sensors_pdev;
667 static struct device *tpacpi_hwmon;
668 static struct input_dev *tpacpi_inputdev;
669 static struct mutex tpacpi_inputdev_send_mutex;
670 static LIST_HEAD(tpacpi_all_drivers);
672 static int tpacpi_suspend_handler(struct platform_device *pdev,
675 struct ibm_struct *ibm, *itmp;
677 list_for_each_entry_safe(ibm, itmp,
681 (ibm->suspend)(state);
687 static int tpacpi_resume_handler(struct platform_device *pdev)
689 struct ibm_struct *ibm, *itmp;
691 list_for_each_entry_safe(ibm, itmp,
701 static struct platform_driver tpacpi_pdriver = {
703 .name = TPACPI_DRVR_NAME,
704 .owner = THIS_MODULE,
706 .suspend = tpacpi_suspend_handler,
707 .resume = tpacpi_resume_handler,
710 static struct platform_driver tpacpi_hwmon_pdriver = {
712 .name = TPACPI_HWMON_DRVR_NAME,
713 .owner = THIS_MODULE,
717 /*************************************************************************
718 * sysfs support helpers
721 struct attribute_set {
722 unsigned int members, max_members;
723 struct attribute_group group;
726 struct attribute_set_obj {
727 struct attribute_set s;
729 } __attribute__((packed));
731 static struct attribute_set *create_attr_set(unsigned int max_members,
734 struct attribute_set_obj *sobj;
736 if (max_members == 0)
739 /* Allocates space for implicit NULL at the end too */
740 sobj = kzalloc(sizeof(struct attribute_set_obj) +
741 max_members * sizeof(struct attribute *),
745 sobj->s.max_members = max_members;
746 sobj->s.group.attrs = &sobj->a;
747 sobj->s.group.name = name;
752 #define destroy_attr_set(_set) \
755 /* not multi-threaded safe, use it in a single thread per set */
756 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
761 if (s->members >= s->max_members)
764 s->group.attrs[s->members] = attr;
770 static int add_many_to_attr_set(struct attribute_set *s,
771 struct attribute **attr,
776 for (i = 0; i < count; i++) {
777 res = add_to_attr_set(s, attr[i]);
785 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
787 sysfs_remove_group(kobj, &s->group);
791 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
792 sysfs_create_group(_kobj, &_attr_set->group)
794 static int parse_strtoul(const char *buf,
795 unsigned long max, unsigned long *value)
799 while (*buf && isspace(*buf))
801 *value = simple_strtoul(buf, &endp, 0);
802 while (*endp && isspace(*endp))
804 if (*endp || *value > max)
810 /*************************************************************************
811 * thinkpad-acpi driver attributes
814 /* interface_version --------------------------------------------------- */
815 static ssize_t tpacpi_driver_interface_version_show(
816 struct device_driver *drv,
819 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
822 static DRIVER_ATTR(interface_version, S_IRUGO,
823 tpacpi_driver_interface_version_show, NULL);
825 /* debug_level --------------------------------------------------------- */
826 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
829 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
832 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
833 const char *buf, size_t count)
837 if (parse_strtoul(buf, 0xffff, &t))
845 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
846 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
848 /* version ------------------------------------------------------------- */
849 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
852 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
853 TPACPI_DESC, TPACPI_VERSION);
856 static DRIVER_ATTR(version, S_IRUGO,
857 tpacpi_driver_version_show, NULL);
859 /* --------------------------------------------------------------------- */
861 static struct driver_attribute *tpacpi_driver_attributes[] = {
862 &driver_attr_debug_level, &driver_attr_version,
863 &driver_attr_interface_version,
866 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
872 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
873 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
880 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
884 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
885 driver_remove_file(drv, tpacpi_driver_attributes[i]);
888 /****************************************************************************
889 ****************************************************************************
893 ****************************************************************************
894 ****************************************************************************/
896 /*************************************************************************
897 * thinkpad-acpi init subdriver
900 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
902 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
903 printk(TPACPI_INFO "%s\n", TPACPI_URL);
905 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
906 (thinkpad_id.bios_version_str) ?
907 thinkpad_id.bios_version_str : "unknown",
908 (thinkpad_id.ec_version_str) ?
909 thinkpad_id.ec_version_str : "unknown");
911 if (thinkpad_id.vendor && thinkpad_id.model_str)
912 printk(TPACPI_INFO "%s %s\n",
913 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
914 "IBM" : ((thinkpad_id.vendor ==
915 PCI_VENDOR_ID_LENOVO) ?
916 "Lenovo" : "Unknown vendor"),
917 thinkpad_id.model_str);
922 static int thinkpad_acpi_driver_read(char *p)
926 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
927 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
932 static struct ibm_struct thinkpad_acpi_driver_data = {
934 .read = thinkpad_acpi_driver_read,
937 /*************************************************************************
941 enum { /* hot key scan codes (derived from ACPI DSDT) */
942 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
943 TP_ACPI_HOTKEYSCAN_FNF2,
944 TP_ACPI_HOTKEYSCAN_FNF3,
945 TP_ACPI_HOTKEYSCAN_FNF4,
946 TP_ACPI_HOTKEYSCAN_FNF5,
947 TP_ACPI_HOTKEYSCAN_FNF6,
948 TP_ACPI_HOTKEYSCAN_FNF7,
949 TP_ACPI_HOTKEYSCAN_FNF8,
950 TP_ACPI_HOTKEYSCAN_FNF9,
951 TP_ACPI_HOTKEYSCAN_FNF10,
952 TP_ACPI_HOTKEYSCAN_FNF11,
953 TP_ACPI_HOTKEYSCAN_FNF12,
954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
955 TP_ACPI_HOTKEYSCAN_FNINSERT,
956 TP_ACPI_HOTKEYSCAN_FNDELETE,
957 TP_ACPI_HOTKEYSCAN_FNHOME,
958 TP_ACPI_HOTKEYSCAN_FNEND,
959 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
961 TP_ACPI_HOTKEYSCAN_FNSPACE,
962 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
964 TP_ACPI_HOTKEYSCAN_MUTE,
965 TP_ACPI_HOTKEYSCAN_THINKPAD,
968 enum { /* Keys available through NVRAM polling */
969 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
970 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
973 enum { /* Positions of some of the keys in hotkey masks */
974 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
975 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
976 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
977 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
978 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
979 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
980 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
981 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
982 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
983 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
984 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
987 enum { /* NVRAM to ACPI HKEY group map */
988 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
989 TP_ACPI_HKEY_ZOOM_MASK |
990 TP_ACPI_HKEY_DISPSWTCH_MASK |
991 TP_ACPI_HKEY_HIBERNATE_MASK,
992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
993 TP_ACPI_HKEY_BRGHTDWN_MASK,
994 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
995 TP_ACPI_HKEY_VOLDWN_MASK |
996 TP_ACPI_HKEY_MUTE_MASK,
999 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1000 struct tp_nvram_state {
1001 u16 thinkpad_toggle:1;
1003 u16 display_toggle:1;
1004 u16 thinklight_toggle:1;
1005 u16 hibernate_toggle:1;
1006 u16 displayexp_toggle:1;
1007 u16 display_state:1;
1008 u16 brightness_toggle:1;
1009 u16 volume_toggle:1;
1012 u8 brightness_level;
1016 static struct task_struct *tpacpi_hotkey_task;
1017 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1018 static int hotkey_poll_freq = 10; /* Hz */
1019 static struct mutex hotkey_thread_mutex;
1020 static struct mutex hotkey_thread_data_mutex;
1021 static unsigned int hotkey_config_change;
1023 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1025 #define hotkey_source_mask 0U
1027 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1029 static struct mutex hotkey_mutex;
1031 static enum { /* Reasons for waking up */
1032 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1033 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1034 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1035 } hotkey_wakeup_reason;
1037 static int hotkey_autosleep_ack;
1039 static int hotkey_orig_status;
1040 static u32 hotkey_orig_mask;
1041 static u32 hotkey_all_mask;
1042 static u32 hotkey_reserved_mask;
1043 static u32 hotkey_mask;
1045 static unsigned int hotkey_report_mode;
1047 static u16 *hotkey_keycode_map;
1049 static struct attribute_set *hotkey_dev_attributes;
1051 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1052 #define HOTKEY_CONFIG_CRITICAL_START \
1054 mutex_lock(&hotkey_thread_data_mutex); \
1055 hotkey_config_change++; \
1057 #define HOTKEY_CONFIG_CRITICAL_END \
1058 mutex_unlock(&hotkey_thread_data_mutex);
1060 #define HOTKEY_CONFIG_CRITICAL_START
1061 #define HOTKEY_CONFIG_CRITICAL_END
1062 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1064 /* HKEY.MHKG() return bits */
1065 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1067 static int hotkey_get_wlsw(int *status)
1069 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1074 static int hotkey_get_tablet_mode(int *status)
1078 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1081 return ((s & TP_HOTKEY_TABLET_MASK) != 0);
1085 * Call with hotkey_mutex held
1087 static int hotkey_mask_get(void)
1091 if (tp_features.hotkey_mask) {
1092 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1095 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1101 * Call with hotkey_mutex held
1103 static int hotkey_mask_set(u32 mask)
1108 if (tp_features.hotkey_mask) {
1109 HOTKEY_CONFIG_CRITICAL_START
1110 for (i = 0; i < 32; i++) {
1112 /* enable in firmware mask only keys not in NVRAM
1113 * mode, but enable the key in the cached hotkey_mask
1114 * regardless of mode, or the key will end up
1115 * disabled by hotkey_mask_get() */
1116 if (!acpi_evalf(hkey_handle,
1117 NULL, "MHKM", "vdd", i + 1,
1118 !!((mask & ~hotkey_source_mask) & m))) {
1122 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1125 HOTKEY_CONFIG_CRITICAL_END
1127 /* hotkey_mask_get must be called unconditionally below */
1128 if (!hotkey_mask_get() && !rc &&
1129 (hotkey_mask & ~hotkey_source_mask) !=
1130 (mask & ~hotkey_source_mask)) {
1131 printk(TPACPI_NOTICE
1132 "requested hot key mask 0x%08x, but "
1133 "firmware forced it to 0x%08x\n",
1137 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1138 HOTKEY_CONFIG_CRITICAL_START
1139 hotkey_mask = mask & hotkey_source_mask;
1140 HOTKEY_CONFIG_CRITICAL_END
1142 if (hotkey_mask != mask) {
1143 printk(TPACPI_NOTICE
1144 "requested hot key mask 0x%08x, "
1145 "forced to 0x%08x (NVRAM poll mask is "
1146 "0x%08x): no firmware mask support\n",
1147 mask, hotkey_mask, hotkey_source_mask);
1152 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1158 static int hotkey_status_get(int *status)
1160 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1166 static int hotkey_status_set(int status)
1168 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1174 static void tpacpi_input_send_radiosw(void)
1178 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1179 mutex_lock(&tpacpi_inputdev_send_mutex);
1181 input_report_switch(tpacpi_inputdev,
1183 input_sync(tpacpi_inputdev);
1185 mutex_unlock(&tpacpi_inputdev_send_mutex);
1189 static void tpacpi_input_send_tabletsw(void)
1193 if (tp_features.hotkey_tablet &&
1194 !hotkey_get_tablet_mode(&state)) {
1195 mutex_lock(&tpacpi_inputdev_send_mutex);
1197 input_report_switch(tpacpi_inputdev,
1198 SW_TABLET_MODE, !!state);
1199 input_sync(tpacpi_inputdev);
1201 mutex_unlock(&tpacpi_inputdev_send_mutex);
1205 static void tpacpi_input_send_key(unsigned int scancode)
1207 unsigned int keycode;
1209 keycode = hotkey_keycode_map[scancode];
1211 if (keycode != KEY_RESERVED) {
1212 mutex_lock(&tpacpi_inputdev_send_mutex);
1214 input_report_key(tpacpi_inputdev, keycode, 1);
1215 if (keycode == KEY_UNKNOWN)
1216 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1218 input_sync(tpacpi_inputdev);
1220 input_report_key(tpacpi_inputdev, keycode, 0);
1221 if (keycode == KEY_UNKNOWN)
1222 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1224 input_sync(tpacpi_inputdev);
1226 mutex_unlock(&tpacpi_inputdev_send_mutex);
1230 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1231 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1233 static void tpacpi_hotkey_send_key(unsigned int scancode)
1235 tpacpi_input_send_key(scancode);
1236 if (hotkey_report_mode < 2) {
1237 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1238 0x80, 0x1001 + scancode);
1242 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1246 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1247 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1248 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1249 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1250 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1251 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1253 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1254 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1255 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1257 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1258 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1259 n->displayexp_toggle =
1260 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1262 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1263 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1264 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1265 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1266 n->brightness_toggle =
1267 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1269 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1270 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1271 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1272 >> TP_NVRAM_POS_LEVEL_VOLUME;
1273 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1274 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1278 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1280 if ((mask & (1 << __scancode)) && \
1281 oldn->__member != newn->__member) \
1282 tpacpi_hotkey_send_key(__scancode); \
1285 #define TPACPI_MAY_SEND_KEY(__scancode) \
1286 do { if (mask & (1 << __scancode)) \
1287 tpacpi_hotkey_send_key(__scancode); } while (0)
1289 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1290 struct tp_nvram_state *newn,
1293 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1294 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1295 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1296 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1298 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1300 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1303 if (oldn->volume_toggle != newn->volume_toggle) {
1304 if (oldn->mute != newn->mute) {
1305 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1307 if (oldn->volume_level > newn->volume_level) {
1308 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1309 } else if (oldn->volume_level < newn->volume_level) {
1310 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1311 } else if (oldn->mute == newn->mute) {
1312 /* repeated key presses that didn't change state */
1314 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1315 } else if (newn->volume_level != 0) {
1316 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1318 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1323 /* handle brightness */
1324 if (oldn->brightness_toggle != newn->brightness_toggle) {
1325 if (oldn->brightness_level < newn->brightness_level) {
1326 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1327 } else if (oldn->brightness_level > newn->brightness_level) {
1328 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1330 /* repeated key presses that didn't change state */
1331 if (newn->brightness_level != 0) {
1332 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1334 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1340 #undef TPACPI_COMPARE_KEY
1341 #undef TPACPI_MAY_SEND_KEY
1343 static int hotkey_kthread(void *data)
1345 struct tp_nvram_state s[2];
1347 unsigned int si, so;
1349 unsigned int change_detector, must_reset;
1351 mutex_lock(&hotkey_thread_mutex);
1353 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1362 /* Initial state for compares */
1363 mutex_lock(&hotkey_thread_data_mutex);
1364 change_detector = hotkey_config_change;
1365 mask = hotkey_source_mask & hotkey_mask;
1366 mutex_unlock(&hotkey_thread_data_mutex);
1367 hotkey_read_nvram(&s[so], mask);
1369 while (!kthread_should_stop() && hotkey_poll_freq) {
1371 t = 1000/hotkey_poll_freq;
1372 t = msleep_interruptible(t);
1373 if (unlikely(kthread_should_stop()))
1375 must_reset = try_to_freeze();
1376 if (t > 0 && !must_reset)
1379 mutex_lock(&hotkey_thread_data_mutex);
1380 if (must_reset || hotkey_config_change != change_detector) {
1381 /* forget old state on thaw or config change */
1384 change_detector = hotkey_config_change;
1386 mask = hotkey_source_mask & hotkey_mask;
1387 mutex_unlock(&hotkey_thread_data_mutex);
1390 hotkey_read_nvram(&s[si], mask);
1391 if (likely(si != so)) {
1392 hotkey_compare_and_issue_event(&s[so], &s[si],
1402 mutex_unlock(&hotkey_thread_mutex);
1406 static void hotkey_poll_stop_sync(void)
1408 if (tpacpi_hotkey_task) {
1409 if (frozen(tpacpi_hotkey_task) ||
1410 freezing(tpacpi_hotkey_task))
1411 thaw_process(tpacpi_hotkey_task);
1413 kthread_stop(tpacpi_hotkey_task);
1414 tpacpi_hotkey_task = NULL;
1415 mutex_lock(&hotkey_thread_mutex);
1416 /* at this point, the thread did exit */
1417 mutex_unlock(&hotkey_thread_mutex);
1421 /* call with hotkey_mutex held */
1422 static void hotkey_poll_setup(int may_warn)
1424 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1425 hotkey_poll_freq > 0 &&
1426 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1427 if (!tpacpi_hotkey_task) {
1428 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1431 if (IS_ERR(tpacpi_hotkey_task)) {
1432 tpacpi_hotkey_task = NULL;
1434 "could not create kernel thread "
1435 "for hotkey polling\n");
1439 hotkey_poll_stop_sync();
1441 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1442 printk(TPACPI_NOTICE
1443 "hot keys 0x%08x require polling, "
1444 "which is currently disabled\n",
1445 hotkey_source_mask);
1450 static void hotkey_poll_setup_safe(int may_warn)
1452 mutex_lock(&hotkey_mutex);
1453 hotkey_poll_setup(may_warn);
1454 mutex_unlock(&hotkey_mutex);
1457 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1459 static void hotkey_poll_setup_safe(int __unused)
1463 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1465 static int hotkey_inputdev_open(struct input_dev *dev)
1467 switch (tpacpi_lifecycle) {
1468 case TPACPI_LIFE_INIT:
1470 * hotkey_init will call hotkey_poll_setup_safe
1471 * at the appropriate moment
1474 case TPACPI_LIFE_EXITING:
1476 case TPACPI_LIFE_RUNNING:
1477 hotkey_poll_setup_safe(0);
1481 /* Should only happen if tpacpi_lifecycle is corrupt */
1486 static void hotkey_inputdev_close(struct input_dev *dev)
1488 /* disable hotkey polling when possible */
1489 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1490 hotkey_poll_setup_safe(0);
1493 /* sysfs hotkey enable ------------------------------------------------- */
1494 static ssize_t hotkey_enable_show(struct device *dev,
1495 struct device_attribute *attr,
1500 res = hotkey_status_get(&status);
1504 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1507 static ssize_t hotkey_enable_store(struct device *dev,
1508 struct device_attribute *attr,
1509 const char *buf, size_t count)
1514 if (parse_strtoul(buf, 1, &t))
1517 res = hotkey_status_set(t);
1519 return (res) ? res : count;
1522 static struct device_attribute dev_attr_hotkey_enable =
1523 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1524 hotkey_enable_show, hotkey_enable_store);
1526 /* sysfs hotkey mask --------------------------------------------------- */
1527 static ssize_t hotkey_mask_show(struct device *dev,
1528 struct device_attribute *attr,
1533 if (mutex_lock_interruptible(&hotkey_mutex))
1534 return -ERESTARTSYS;
1535 res = hotkey_mask_get();
1536 mutex_unlock(&hotkey_mutex);
1539 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1542 static ssize_t hotkey_mask_store(struct device *dev,
1543 struct device_attribute *attr,
1544 const char *buf, size_t count)
1549 if (parse_strtoul(buf, 0xffffffffUL, &t))
1552 if (mutex_lock_interruptible(&hotkey_mutex))
1553 return -ERESTARTSYS;
1555 res = hotkey_mask_set(t);
1557 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1558 hotkey_poll_setup(1);
1561 mutex_unlock(&hotkey_mutex);
1563 return (res) ? res : count;
1566 static struct device_attribute dev_attr_hotkey_mask =
1567 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1568 hotkey_mask_show, hotkey_mask_store);
1570 /* sysfs hotkey bios_enabled ------------------------------------------- */
1571 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1572 struct device_attribute *attr,
1575 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1578 static struct device_attribute dev_attr_hotkey_bios_enabled =
1579 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1581 /* sysfs hotkey bios_mask ---------------------------------------------- */
1582 static ssize_t hotkey_bios_mask_show(struct device *dev,
1583 struct device_attribute *attr,
1586 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1589 static struct device_attribute dev_attr_hotkey_bios_mask =
1590 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1592 /* sysfs hotkey all_mask ----------------------------------------------- */
1593 static ssize_t hotkey_all_mask_show(struct device *dev,
1594 struct device_attribute *attr,
1597 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1598 hotkey_all_mask | hotkey_source_mask);
1601 static struct device_attribute dev_attr_hotkey_all_mask =
1602 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1604 /* sysfs hotkey recommended_mask --------------------------------------- */
1605 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1606 struct device_attribute *attr,
1609 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1610 (hotkey_all_mask | hotkey_source_mask)
1611 & ~hotkey_reserved_mask);
1614 static struct device_attribute dev_attr_hotkey_recommended_mask =
1615 __ATTR(hotkey_recommended_mask, S_IRUGO,
1616 hotkey_recommended_mask_show, NULL);
1618 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1620 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1621 static ssize_t hotkey_source_mask_show(struct device *dev,
1622 struct device_attribute *attr,
1625 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1628 static ssize_t hotkey_source_mask_store(struct device *dev,
1629 struct device_attribute *attr,
1630 const char *buf, size_t count)
1634 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1635 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1638 if (mutex_lock_interruptible(&hotkey_mutex))
1639 return -ERESTARTSYS;
1641 HOTKEY_CONFIG_CRITICAL_START
1642 hotkey_source_mask = t;
1643 HOTKEY_CONFIG_CRITICAL_END
1645 hotkey_poll_setup(1);
1647 mutex_unlock(&hotkey_mutex);
1652 static struct device_attribute dev_attr_hotkey_source_mask =
1653 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1654 hotkey_source_mask_show, hotkey_source_mask_store);
1656 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1657 static ssize_t hotkey_poll_freq_show(struct device *dev,
1658 struct device_attribute *attr,
1661 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1664 static ssize_t hotkey_poll_freq_store(struct device *dev,
1665 struct device_attribute *attr,
1666 const char *buf, size_t count)
1670 if (parse_strtoul(buf, 25, &t))
1673 if (mutex_lock_interruptible(&hotkey_mutex))
1674 return -ERESTARTSYS;
1676 hotkey_poll_freq = t;
1678 hotkey_poll_setup(1);
1679 mutex_unlock(&hotkey_mutex);
1684 static struct device_attribute dev_attr_hotkey_poll_freq =
1685 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1686 hotkey_poll_freq_show, hotkey_poll_freq_store);
1688 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1690 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1691 static ssize_t hotkey_radio_sw_show(struct device *dev,
1692 struct device_attribute *attr,
1696 res = hotkey_get_wlsw(&s);
1700 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1703 static struct device_attribute dev_attr_hotkey_radio_sw =
1704 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1706 static void hotkey_radio_sw_notify_change(void)
1708 if (tp_features.hotkey_wlsw)
1709 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1713 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1714 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1715 struct device_attribute *attr,
1719 res = hotkey_get_tablet_mode(&s);
1723 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1726 static struct device_attribute dev_attr_hotkey_tablet_mode =
1727 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1729 static void hotkey_tablet_mode_notify_change(void)
1731 if (tp_features.hotkey_tablet)
1732 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1733 "hotkey_tablet_mode");
1736 /* sysfs hotkey report_mode -------------------------------------------- */
1737 static ssize_t hotkey_report_mode_show(struct device *dev,
1738 struct device_attribute *attr,
1741 return snprintf(buf, PAGE_SIZE, "%d\n",
1742 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1745 static struct device_attribute dev_attr_hotkey_report_mode =
1746 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1748 /* sysfs wakeup reason (pollable) -------------------------------------- */
1749 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1750 struct device_attribute *attr,
1753 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1756 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1757 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1759 static void hotkey_wakeup_reason_notify_change(void)
1761 if (tp_features.hotkey_mask)
1762 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1766 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1767 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1768 struct device_attribute *attr,
1771 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1774 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1775 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1776 hotkey_wakeup_hotunplug_complete_show, NULL);
1778 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1780 if (tp_features.hotkey_mask)
1781 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1782 "wakeup_hotunplug_complete");
1785 /* --------------------------------------------------------------------- */
1787 static struct attribute *hotkey_attributes[] __initdata = {
1788 &dev_attr_hotkey_enable.attr,
1789 &dev_attr_hotkey_bios_enabled.attr,
1790 &dev_attr_hotkey_report_mode.attr,
1791 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1792 &dev_attr_hotkey_mask.attr,
1793 &dev_attr_hotkey_all_mask.attr,
1794 &dev_attr_hotkey_recommended_mask.attr,
1795 &dev_attr_hotkey_source_mask.attr,
1796 &dev_attr_hotkey_poll_freq.attr,
1800 static struct attribute *hotkey_mask_attributes[] __initdata = {
1801 &dev_attr_hotkey_bios_mask.attr,
1802 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1803 &dev_attr_hotkey_mask.attr,
1804 &dev_attr_hotkey_all_mask.attr,
1805 &dev_attr_hotkey_recommended_mask.attr,
1807 &dev_attr_hotkey_wakeup_reason.attr,
1808 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1811 static int __init hotkey_init(struct ibm_init_struct *iibm)
1813 /* Requirements for changing the default keymaps:
1815 * 1. Many of the keys are mapped to KEY_RESERVED for very
1816 * good reasons. Do not change them unless you have deep
1817 * knowledge on the IBM and Lenovo ThinkPad firmware for
1818 * the various ThinkPad models. The driver behaves
1819 * differently for KEY_RESERVED: such keys have their
1820 * hot key mask *unset* in mask_recommended, and also
1821 * in the initial hot key mask programmed into the
1822 * firmware at driver load time, which means the firm-
1823 * ware may react very differently if you change them to
1826 * 2. You must be subscribed to the linux-thinkpad and
1827 * ibm-acpi-devel mailing lists, and you should read the
1828 * list archives since 2007 if you want to change the
1829 * keymaps. This requirement exists so that you will
1830 * know the past history of problems with the thinkpad-
1831 * acpi driver keymaps, and also that you will be
1832 * listening to any bug reports;
1834 * 3. Do not send thinkpad-acpi specific patches directly to
1835 * for merging, *ever*. Send them to the linux-acpi
1836 * mailinglist for comments. Merging is to be done only
1837 * through acpi-test and the ACPI maintainer.
1839 * If the above is too much to ask, don't change the keymap.
1840 * Ask the thinkpad-acpi maintainer to do it, instead.
1842 static u16 ibm_keycode_map[] __initdata = {
1843 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1844 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1845 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1846 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1848 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1849 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1850 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1851 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1853 /* brightness: firmware always reacts to them, unless
1854 * X.org did some tricks in the radeon BIOS scratch
1855 * registers of *some* models */
1856 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1857 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1859 /* Thinklight: firmware always react to it */
1860 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1862 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1863 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1865 /* Volume: firmware always react to it and reprograms
1866 * the built-in *extra* mixer. Never map it to control
1867 * another mixer by default. */
1868 KEY_RESERVED, /* 0x14: VOLUME UP */
1869 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1870 KEY_RESERVED, /* 0x16: MUTE */
1872 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1874 /* (assignments unknown, please report if found) */
1875 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1876 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1878 static u16 lenovo_keycode_map[] __initdata = {
1879 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1880 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1881 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1882 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1884 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1885 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1886 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1887 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1889 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1890 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1892 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1894 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1895 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1897 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1898 * react to it and reprograms the built-in *extra* mixer.
1899 * Never map it to control another mixer by default.
1901 * T60?, T61, R60?, R61: firmware and EC tries to send
1902 * these over the regular keyboard, so these are no-ops,
1903 * but there are still weird bugs re. MUTE, so do not
1904 * change unless you get test reports from all Lenovo
1905 * models. May cause the BIOS to interfere with the
1908 KEY_RESERVED, /* 0x14: VOLUME UP */
1909 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1910 KEY_RESERVED, /* 0x16: MUTE */
1912 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1914 /* (assignments unknown, please report if found) */
1915 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1916 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1919 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1920 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1921 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1927 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1929 BUG_ON(!tpacpi_inputdev);
1930 BUG_ON(tpacpi_inputdev->open != NULL ||
1931 tpacpi_inputdev->close != NULL);
1933 TPACPI_ACPIHANDLE_INIT(hkey);
1934 mutex_init(&hotkey_mutex);
1936 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1937 mutex_init(&hotkey_thread_mutex);
1938 mutex_init(&hotkey_thread_data_mutex);
1941 /* hotkey not supported on 570 */
1942 tp_features.hotkey = hkey_handle != NULL;
1944 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1945 str_supported(tp_features.hotkey));
1947 if (tp_features.hotkey) {
1948 hotkey_dev_attributes = create_attr_set(13, NULL);
1949 if (!hotkey_dev_attributes)
1951 res = add_many_to_attr_set(hotkey_dev_attributes,
1953 ARRAY_SIZE(hotkey_attributes));
1957 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1958 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1959 for HKEY interface version 0x100 */
1960 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1961 if ((hkeyv >> 8) != 1) {
1962 printk(TPACPI_ERR "unknown version of the "
1963 "HKEY interface: 0x%x\n", hkeyv);
1964 printk(TPACPI_ERR "please report this to %s\n",
1968 * MHKV 0x100 in A31, R40, R40e,
1969 * T4x, X31, and later
1971 tp_features.hotkey_mask = 1;
1975 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1976 str_supported(tp_features.hotkey_mask));
1978 if (tp_features.hotkey_mask) {
1979 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1982 "missing MHKA handler, "
1983 "please report this to %s\n",
1985 /* FN+F12, FN+F4, FN+F3 */
1986 hotkey_all_mask = 0x080cU;
1990 /* hotkey_source_mask *must* be zero for
1991 * the first hotkey_mask_get */
1992 res = hotkey_status_get(&hotkey_orig_status);
1993 if (!res && tp_features.hotkey_mask) {
1994 res = hotkey_mask_get();
1995 hotkey_orig_mask = hotkey_mask;
1997 res = add_many_to_attr_set(
1998 hotkey_dev_attributes,
1999 hotkey_mask_attributes,
2000 ARRAY_SIZE(hotkey_mask_attributes));
2004 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2005 if (tp_features.hotkey_mask) {
2006 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2009 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2012 vdbg_printk(TPACPI_DBG_INIT,
2013 "hotkey source mask 0x%08x, polling freq %d\n",
2014 hotkey_source_mask, hotkey_poll_freq);
2017 /* Not all thinkpads have a hardware radio switch */
2018 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2019 tp_features.hotkey_wlsw = 1;
2021 "radio switch found; radios are %s\n",
2022 enabled(status, 0));
2023 res = add_to_attr_set(hotkey_dev_attributes,
2024 &dev_attr_hotkey_radio_sw.attr);
2027 /* For X41t, X60t, X61t Tablets... */
2028 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2029 tp_features.hotkey_tablet = 1;
2031 "possible tablet mode switch found; "
2032 "ThinkPad in %s mode\n",
2033 (status & TP_HOTKEY_TABLET_MASK)?
2034 "tablet" : "laptop");
2035 res = add_to_attr_set(hotkey_dev_attributes,
2036 &dev_attr_hotkey_tablet_mode.attr);
2040 res = register_attr_set_with_sysfs(
2041 hotkey_dev_attributes,
2042 &tpacpi_pdev->dev.kobj);
2046 /* Set up key map */
2048 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2050 if (!hotkey_keycode_map) {
2052 "failed to allocate memory for key map\n");
2056 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2057 dbg_printk(TPACPI_DBG_INIT,
2058 "using Lenovo default hot key map\n");
2059 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2060 TPACPI_HOTKEY_MAP_SIZE);
2062 dbg_printk(TPACPI_DBG_INIT,
2063 "using IBM default hot key map\n");
2064 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2065 TPACPI_HOTKEY_MAP_SIZE);
2068 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2069 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2070 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2071 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2072 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2073 tpacpi_inputdev->keycode = hotkey_keycode_map;
2074 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2075 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2076 set_bit(hotkey_keycode_map[i],
2077 tpacpi_inputdev->keybit);
2079 if (i < sizeof(hotkey_reserved_mask)*8)
2080 hotkey_reserved_mask |= 1 << i;
2084 if (tp_features.hotkey_wlsw) {
2085 set_bit(EV_SW, tpacpi_inputdev->evbit);
2086 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2088 if (tp_features.hotkey_tablet) {
2089 set_bit(EV_SW, tpacpi_inputdev->evbit);
2090 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2093 dbg_printk(TPACPI_DBG_INIT,
2094 "enabling hot key handling\n");
2095 res = hotkey_status_set(1);
2098 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2099 & ~hotkey_reserved_mask)
2100 | hotkey_orig_mask);
2101 if (res < 0 && res != -ENXIO)
2104 dbg_printk(TPACPI_DBG_INIT,
2105 "legacy hot key reporting over procfs %s\n",
2106 (hotkey_report_mode < 2) ?
2107 "enabled" : "disabled");
2109 tpacpi_inputdev->open = &hotkey_inputdev_open;
2110 tpacpi_inputdev->close = &hotkey_inputdev_close;
2112 hotkey_poll_setup_safe(1);
2113 tpacpi_input_send_radiosw();
2114 tpacpi_input_send_tabletsw();
2117 return (tp_features.hotkey)? 0 : 1;
2120 static void hotkey_exit(void)
2122 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2123 hotkey_poll_stop_sync();
2126 if (tp_features.hotkey) {
2127 dbg_printk(TPACPI_DBG_EXIT,
2128 "restoring original hot key mask\n");
2129 /* no short-circuit boolean operator below! */
2130 if ((hotkey_mask_set(hotkey_orig_mask) |
2131 hotkey_status_set(hotkey_orig_status)) != 0)
2133 "failed to restore hot key mask "
2134 "to BIOS defaults\n");
2137 if (hotkey_dev_attributes) {
2138 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2139 hotkey_dev_attributes = NULL;
2143 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2146 unsigned int scancode;
2151 if (event != 0x80) {
2153 "unknown HKEY notification event %d\n", event);
2154 /* forward it to userspace, maybe it knows how to handle it */
2155 acpi_bus_generate_netlink_event(
2156 ibm->acpi->device->pnp.device_class,
2157 ibm->acpi->device->dev.bus_id,
2163 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2164 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2177 switch (hkey >> 12) {
2179 /* 0x1000-0x1FFF: key presses */
2180 scancode = hkey & 0xfff;
2181 if (scancode > 0 && scancode < 0x21) {
2183 if (!(hotkey_source_mask & (1 << scancode))) {
2184 tpacpi_input_send_key(scancode);
2196 case 0x2304: /* suspend, undock */
2197 case 0x2404: /* hibernation, undock */
2198 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2201 case 0x2305: /* suspend, bay eject */
2202 case 0x2405: /* hibernation, bay eject */
2203 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2209 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2211 "woke up due to a hot-unplug "
2213 hotkey_wakeup_reason_notify_change();
2217 /* bay-related wakeups */
2218 if (hkey == 0x3003) {
2219 hotkey_autosleep_ack = 1;
2222 hotkey_wakeup_hotunplug_complete_notify_change();
2228 /* dock-related wakeups */
2229 if (hkey == 0x4003) {
2230 hotkey_autosleep_ack = 1;
2233 hotkey_wakeup_hotunplug_complete_notify_change();
2239 /* 0x5000-0x5FFF: human interface helpers */
2241 case 0x5010: /* Lenovo new BIOS: brightness changed */
2242 case 0x500b: /* X61t: tablet pen inserted into bay */
2243 case 0x500c: /* X61t: tablet pen removed from bay */
2245 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2246 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2247 tpacpi_input_send_tabletsw();
2248 hotkey_tablet_mode_notify_change();
2253 /* LID switch events. Do not propagate */
2261 /* 0x7000-0x7FFF: misc */
2262 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2263 tpacpi_input_send_radiosw();
2264 hotkey_radio_sw_notify_change();
2268 /* fallthrough to default */
2273 printk(TPACPI_NOTICE
2274 "unhandled HKEY event 0x%04x\n", hkey);
2278 if (!ignore_acpi_ev &&
2279 (send_acpi_ev || hotkey_report_mode < 2)) {
2280 acpi_bus_generate_proc_event(ibm->acpi->device,
2284 /* netlink events */
2285 if (!ignore_acpi_ev && send_acpi_ev) {
2286 acpi_bus_generate_netlink_event(
2287 ibm->acpi->device->pnp.device_class,
2288 ibm->acpi->device->dev.bus_id,
2294 static void hotkey_suspend(pm_message_t state)
2296 /* Do these on suspend, we get the events on early resume! */
2297 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2298 hotkey_autosleep_ack = 0;
2301 static void hotkey_resume(void)
2303 if (hotkey_mask_get())
2305 "error while trying to read hot key mask "
2307 tpacpi_input_send_radiosw();
2308 hotkey_radio_sw_notify_change();
2309 hotkey_tablet_mode_notify_change();
2310 hotkey_wakeup_reason_notify_change();
2311 hotkey_wakeup_hotunplug_complete_notify_change();
2312 hotkey_poll_setup_safe(0);
2315 /* procfs -------------------------------------------------------------- */
2316 static int hotkey_read(char *p)
2321 if (!tp_features.hotkey) {
2322 len += sprintf(p + len, "status:\t\tnot supported\n");
2326 if (mutex_lock_interruptible(&hotkey_mutex))
2327 return -ERESTARTSYS;
2328 res = hotkey_status_get(&status);
2330 res = hotkey_mask_get();
2331 mutex_unlock(&hotkey_mutex);
2335 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2336 if (tp_features.hotkey_mask) {
2337 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2338 len += sprintf(p + len,
2339 "commands:\tenable, disable, reset, <mask>\n");
2341 len += sprintf(p + len, "mask:\t\tnot supported\n");
2342 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2348 static int hotkey_write(char *buf)
2354 if (!tp_features.hotkey)
2357 if (mutex_lock_interruptible(&hotkey_mutex))
2358 return -ERESTARTSYS;
2364 while ((cmd = next_cmd(&buf))) {
2365 if (strlencmp(cmd, "enable") == 0) {
2367 } else if (strlencmp(cmd, "disable") == 0) {
2369 } else if (strlencmp(cmd, "reset") == 0) {
2370 status = hotkey_orig_status;
2371 mask = hotkey_orig_mask;
2372 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2374 } else if (sscanf(cmd, "%x", &mask) == 1) {
2382 res = hotkey_status_set(status);
2384 if (!res && mask != hotkey_mask)
2385 res = hotkey_mask_set(mask);
2388 mutex_unlock(&hotkey_mutex);
2392 static const struct acpi_device_id ibm_htk_device_ids[] = {
2393 {TPACPI_ACPI_HKEY_HID, 0},
2397 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2398 .hid = ibm_htk_device_ids,
2399 .notify = hotkey_notify,
2400 .handle = &hkey_handle,
2401 .type = ACPI_DEVICE_NOTIFY,
2404 static struct ibm_struct hotkey_driver_data = {
2406 .read = hotkey_read,
2407 .write = hotkey_write,
2408 .exit = hotkey_exit,
2409 .resume = hotkey_resume,
2410 .suspend = hotkey_suspend,
2411 .acpi = &ibm_hotkey_acpidriver,
2414 /*************************************************************************
2415 * Bluetooth subdriver
2419 /* ACPI GBDC/SBDC bits */
2420 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2421 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2422 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2425 static int bluetooth_get_radiosw(void);
2426 static int bluetooth_set_radiosw(int radio_on);
2428 /* sysfs bluetooth enable ---------------------------------------------- */
2429 static ssize_t bluetooth_enable_show(struct device *dev,
2430 struct device_attribute *attr,
2435 status = bluetooth_get_radiosw();
2439 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2442 static ssize_t bluetooth_enable_store(struct device *dev,
2443 struct device_attribute *attr,
2444 const char *buf, size_t count)
2449 if (parse_strtoul(buf, 1, &t))
2452 res = bluetooth_set_radiosw(t);
2454 return (res) ? res : count;
2457 static struct device_attribute dev_attr_bluetooth_enable =
2458 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2459 bluetooth_enable_show, bluetooth_enable_store);
2461 /* --------------------------------------------------------------------- */
2463 static struct attribute *bluetooth_attributes[] = {
2464 &dev_attr_bluetooth_enable.attr,
2468 static const struct attribute_group bluetooth_attr_group = {
2469 .attrs = bluetooth_attributes,
2472 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2477 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2479 TPACPI_ACPIHANDLE_INIT(hkey);
2481 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2482 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2483 tp_features.bluetooth = hkey_handle &&
2484 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2486 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2487 str_supported(tp_features.bluetooth),
2490 if (tp_features.bluetooth) {
2491 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2492 /* no bluetooth hardware present in system */
2493 tp_features.bluetooth = 0;
2494 dbg_printk(TPACPI_DBG_INIT,
2495 "bluetooth hardware not installed\n");
2497 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2498 &bluetooth_attr_group);
2504 return (tp_features.bluetooth)? 0 : 1;
2507 static void bluetooth_exit(void)
2509 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2510 &bluetooth_attr_group);
2513 static int bluetooth_get_radiosw(void)
2517 if (!tp_features.bluetooth)
2520 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2523 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2526 static int bluetooth_set_radiosw(int radio_on)
2530 if (!tp_features.bluetooth)
2533 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2536 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2538 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2539 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2545 /* procfs -------------------------------------------------------------- */
2546 static int bluetooth_read(char *p)
2549 int status = bluetooth_get_radiosw();
2551 if (!tp_features.bluetooth)
2552 len += sprintf(p + len, "status:\t\tnot supported\n");
2554 len += sprintf(p + len, "status:\t\t%s\n",
2555 (status)? "enabled" : "disabled");
2556 len += sprintf(p + len, "commands:\tenable, disable\n");
2562 static int bluetooth_write(char *buf)
2566 if (!tp_features.bluetooth)
2569 while ((cmd = next_cmd(&buf))) {
2570 if (strlencmp(cmd, "enable") == 0) {
2571 bluetooth_set_radiosw(1);
2572 } else if (strlencmp(cmd, "disable") == 0) {
2573 bluetooth_set_radiosw(0);
2581 static struct ibm_struct bluetooth_driver_data = {
2582 .name = "bluetooth",
2583 .read = bluetooth_read,
2584 .write = bluetooth_write,
2585 .exit = bluetooth_exit,
2588 /*************************************************************************
2593 /* ACPI GWAN/SWAN bits */
2594 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2595 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2596 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2599 static int wan_get_radiosw(void);
2600 static int wan_set_radiosw(int radio_on);
2602 /* sysfs wan enable ---------------------------------------------------- */
2603 static ssize_t wan_enable_show(struct device *dev,
2604 struct device_attribute *attr,
2609 status = wan_get_radiosw();
2613 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2616 static ssize_t wan_enable_store(struct device *dev,
2617 struct device_attribute *attr,
2618 const char *buf, size_t count)
2623 if (parse_strtoul(buf, 1, &t))
2626 res = wan_set_radiosw(t);
2628 return (res) ? res : count;
2631 static struct device_attribute dev_attr_wan_enable =
2632 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2633 wan_enable_show, wan_enable_store);
2635 /* --------------------------------------------------------------------- */
2637 static struct attribute *wan_attributes[] = {
2638 &dev_attr_wan_enable.attr,
2642 static const struct attribute_group wan_attr_group = {
2643 .attrs = wan_attributes,
2646 static int __init wan_init(struct ibm_init_struct *iibm)
2651 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2653 TPACPI_ACPIHANDLE_INIT(hkey);
2655 tp_features.wan = hkey_handle &&
2656 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2658 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2659 str_supported(tp_features.wan),
2662 if (tp_features.wan) {
2663 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2664 /* no wan hardware present in system */
2665 tp_features.wan = 0;
2666 dbg_printk(TPACPI_DBG_INIT,
2667 "wan hardware not installed\n");
2669 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2676 return (tp_features.wan)? 0 : 1;
2679 static void wan_exit(void)
2681 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2685 static int wan_get_radiosw(void)
2689 if (!tp_features.wan)
2692 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2695 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2698 static int wan_set_radiosw(int radio_on)
2702 if (!tp_features.wan)
2705 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2708 status |= TP_ACPI_WANCARD_RADIOSSW;
2710 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2711 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2717 /* procfs -------------------------------------------------------------- */
2718 static int wan_read(char *p)
2721 int status = wan_get_radiosw();
2723 if (!tp_features.wan)
2724 len += sprintf(p + len, "status:\t\tnot supported\n");
2726 len += sprintf(p + len, "status:\t\t%s\n",
2727 (status)? "enabled" : "disabled");
2728 len += sprintf(p + len, "commands:\tenable, disable\n");
2734 static int wan_write(char *buf)
2738 if (!tp_features.wan)
2741 while ((cmd = next_cmd(&buf))) {
2742 if (strlencmp(cmd, "enable") == 0) {
2744 } else if (strlencmp(cmd, "disable") == 0) {
2753 static struct ibm_struct wan_driver_data = {
2758 .flags.experimental = 1,
2761 /*************************************************************************
2765 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2767 enum video_access_mode {
2768 TPACPI_VIDEO_NONE = 0,
2769 TPACPI_VIDEO_570, /* 570 */
2770 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2771 TPACPI_VIDEO_NEW, /* all others */
2774 enum { /* video status flags, based on VIDEO_570 */
2775 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2776 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2777 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2780 enum { /* TPACPI_VIDEO_570 constants */
2781 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2782 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2783 * video_status_flags */
2784 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2785 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2788 static enum video_access_mode video_supported;
2789 static int video_orig_autosw;
2791 static int video_autosw_get(void);
2792 static int video_autosw_set(int enable);
2794 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2796 static int __init video_init(struct ibm_init_struct *iibm)
2800 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2802 TPACPI_ACPIHANDLE_INIT(vid);
2803 TPACPI_ACPIHANDLE_INIT(vid2);
2805 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2806 /* G41, assume IVGA doesn't change */
2807 vid_handle = vid2_handle;
2810 /* video switching not supported on R30, R31 */
2811 video_supported = TPACPI_VIDEO_NONE;
2812 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2814 video_supported = TPACPI_VIDEO_570;
2815 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2816 /* 600e/x, 770e, 770x */
2817 video_supported = TPACPI_VIDEO_770;
2820 video_supported = TPACPI_VIDEO_NEW;
2822 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2823 str_supported(video_supported != TPACPI_VIDEO_NONE),
2826 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2829 static void video_exit(void)
2831 dbg_printk(TPACPI_DBG_EXIT,
2832 "restoring original video autoswitch mode\n");
2833 if (video_autosw_set(video_orig_autosw))
2834 printk(TPACPI_ERR "error while trying to restore original "
2835 "video autoswitch mode\n");
2838 static int video_outputsw_get(void)
2843 switch (video_supported) {
2844 case TPACPI_VIDEO_570:
2845 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2846 TP_ACPI_VIDEO_570_PHSCMD))
2848 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2850 case TPACPI_VIDEO_770:
2851 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2854 status |= TP_ACPI_VIDEO_S_LCD;
2855 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2858 status |= TP_ACPI_VIDEO_S_CRT;
2860 case TPACPI_VIDEO_NEW:
2861 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2862 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2865 status |= TP_ACPI_VIDEO_S_CRT;
2867 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2868 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2871 status |= TP_ACPI_VIDEO_S_LCD;
2872 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2875 status |= TP_ACPI_VIDEO_S_DVI;
2884 static int video_outputsw_set(int status)
2889 switch (video_supported) {
2890 case TPACPI_VIDEO_570:
2891 res = acpi_evalf(NULL, NULL,
2892 "\\_SB.PHS2", "vdd",
2893 TP_ACPI_VIDEO_570_PHS2CMD,
2894 status | TP_ACPI_VIDEO_570_PHS2SET);
2896 case TPACPI_VIDEO_770:
2897 autosw = video_autosw_get();
2901 res = video_autosw_set(1);
2904 res = acpi_evalf(vid_handle, NULL,
2905 "ASWT", "vdd", status * 0x100, 0);
2906 if (!autosw && video_autosw_set(autosw)) {
2908 "video auto-switch left enabled due to error\n");
2912 case TPACPI_VIDEO_NEW:
2913 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2914 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2920 return (res)? 0 : -EIO;
2923 static int video_autosw_get(void)
2927 switch (video_supported) {
2928 case TPACPI_VIDEO_570:
2929 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2932 case TPACPI_VIDEO_770:
2933 case TPACPI_VIDEO_NEW:
2934 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2944 static int video_autosw_set(int enable)
2946 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2951 static int video_outputsw_cycle(void)
2953 int autosw = video_autosw_get();
2959 switch (video_supported) {
2960 case TPACPI_VIDEO_570:
2961 res = video_autosw_set(1);
2964 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2966 case TPACPI_VIDEO_770:
2967 case TPACPI_VIDEO_NEW:
2968 res = video_autosw_set(1);
2971 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2976 if (!autosw && video_autosw_set(autosw)) {
2978 "video auto-switch left enabled due to error\n");
2982 return (res)? 0 : -EIO;
2985 static int video_expand_toggle(void)
2987 switch (video_supported) {
2988 case TPACPI_VIDEO_570:
2989 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2991 case TPACPI_VIDEO_770:
2992 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2994 case TPACPI_VIDEO_NEW:
2995 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3003 static int video_read(char *p)
3008 if (video_supported == TPACPI_VIDEO_NONE) {
3009 len += sprintf(p + len, "status:\t\tnot supported\n");
3013 status = video_outputsw_get();
3017 autosw = video_autosw_get();
3021 len += sprintf(p + len, "status:\t\tsupported\n");
3022 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3023 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3024 if (video_supported == TPACPI_VIDEO_NEW)
3025 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3026 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3027 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3028 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3029 if (video_supported == TPACPI_VIDEO_NEW)
3030 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3031 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3032 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3037 static int video_write(char *buf)
3040 int enable, disable, status;
3043 if (video_supported == TPACPI_VIDEO_NONE)
3049 while ((cmd = next_cmd(&buf))) {
3050 if (strlencmp(cmd, "lcd_enable") == 0) {
3051 enable |= TP_ACPI_VIDEO_S_LCD;
3052 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3053 disable |= TP_ACPI_VIDEO_S_LCD;
3054 } else if (strlencmp(cmd, "crt_enable") == 0) {
3055 enable |= TP_ACPI_VIDEO_S_CRT;
3056 } else if (strlencmp(cmd, "crt_disable") == 0) {
3057 disable |= TP_ACPI_VIDEO_S_CRT;
3058 } else if (video_supported == TPACPI_VIDEO_NEW &&
3059 strlencmp(cmd, "dvi_enable") == 0) {
3060 enable |= TP_ACPI_VIDEO_S_DVI;
3061 } else if (video_supported == TPACPI_VIDEO_NEW &&
3062 strlencmp(cmd, "dvi_disable") == 0) {
3063 disable |= TP_ACPI_VIDEO_S_DVI;
3064 } else if (strlencmp(cmd, "auto_enable") == 0) {
3065 res = video_autosw_set(1);
3068 } else if (strlencmp(cmd, "auto_disable") == 0) {
3069 res = video_autosw_set(0);
3072 } else if (strlencmp(cmd, "video_switch") == 0) {
3073 res = video_outputsw_cycle();
3076 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3077 res = video_expand_toggle();
3084 if (enable || disable) {
3085 status = video_outputsw_get();
3088 res = video_outputsw_set((status & ~disable) | enable);
3096 static struct ibm_struct video_driver_data = {
3099 .write = video_write,
3103 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3105 /*************************************************************************
3106 * Light (thinklight) subdriver
3109 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3110 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3112 static int __init light_init(struct ibm_init_struct *iibm)
3114 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3116 TPACPI_ACPIHANDLE_INIT(ledb);
3117 TPACPI_ACPIHANDLE_INIT(lght);
3118 TPACPI_ACPIHANDLE_INIT(cmos);
3120 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3121 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3123 if (tp_features.light)
3124 /* light status not supported on
3125 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3126 tp_features.light_status =
3127 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3129 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3130 str_supported(tp_features.light));
3132 return (tp_features.light)? 0 : 1;
3135 static int light_read(char *p)
3140 if (!tp_features.light) {
3141 len += sprintf(p + len, "status:\t\tnot supported\n");
3142 } else if (!tp_features.light_status) {
3143 len += sprintf(p + len, "status:\t\tunknown\n");
3144 len += sprintf(p + len, "commands:\ton, off\n");
3146 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3148 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3149 len += sprintf(p + len, "commands:\ton, off\n");
3155 static int light_write(char *buf)
3157 int cmos_cmd, lght_cmd;
3161 if (!tp_features.light)
3164 while ((cmd = next_cmd(&buf))) {
3165 if (strlencmp(cmd, "on") == 0) {
3168 } else if (strlencmp(cmd, "off") == 0) {
3174 success = cmos_handle ?
3175 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3176 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3184 static struct ibm_struct light_driver_data = {
3187 .write = light_write,
3190 /*************************************************************************
3194 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3196 static void dock_notify(struct ibm_struct *ibm, u32 event);
3197 static int dock_read(char *p);
3198 static int dock_write(char *buf);
3200 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3201 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3202 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3203 "\\_SB.PCI.ISA.SLCE", /* 570 */
3204 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3206 /* don't list other alternatives as we install a notify handler on the 570 */
3207 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3209 static const struct acpi_device_id ibm_pci_device_ids[] = {
3210 {PCI_ROOT_HID_STRING, 0},
3214 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3216 .notify = dock_notify,
3217 .handle = &dock_handle,
3218 .type = ACPI_SYSTEM_NOTIFY,
3221 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3222 * We just use it to get notifications of dock hotplug
3223 * in very old thinkpads */
3224 .hid = ibm_pci_device_ids,
3225 .notify = dock_notify,
3226 .handle = &pci_handle,
3227 .type = ACPI_SYSTEM_NOTIFY,
3231 static struct ibm_struct dock_driver_data[2] = {
3235 .write = dock_write,
3236 .acpi = &ibm_dock_acpidriver[0],
3240 .acpi = &ibm_dock_acpidriver[1],
3244 #define dock_docked() (_sta(dock_handle) & 1)
3246 static int __init dock_init(struct ibm_init_struct *iibm)
3248 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3250 TPACPI_ACPIHANDLE_INIT(dock);
3252 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3253 str_supported(dock_handle != NULL));
3255 return (dock_handle)? 0 : 1;
3258 static int __init dock_init2(struct ibm_init_struct *iibm)
3262 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3264 if (dock_driver_data[0].flags.acpi_driver_registered &&
3265 dock_driver_data[0].flags.acpi_notify_installed) {
3266 TPACPI_ACPIHANDLE_INIT(pci);
3267 dock2_needed = (pci_handle != NULL);
3268 vdbg_printk(TPACPI_DBG_INIT,
3269 "dock PCI handler for the TP 570 is %s\n",
3270 str_supported(dock2_needed));
3272 vdbg_printk(TPACPI_DBG_INIT,
3273 "dock subdriver part 2 not required\n");
3277 return (dock2_needed)? 0 : 1;
3280 static void dock_notify(struct ibm_struct *ibm, u32 event)
3282 int docked = dock_docked();
3283 int pci = ibm->acpi->hid && ibm->acpi->device &&
3284 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3287 if (event == 1 && !pci) /* 570 */
3288 data = 1; /* button */
3289 else if (event == 1 && pci) /* 570 */
3290 data = 3; /* dock */
3291 else if (event == 3 && docked)
3292 data = 1; /* button */
3293 else if (event == 3 && !docked)
3294 data = 2; /* undock */
3295 else if (event == 0 && docked)
3296 data = 3; /* dock */
3298 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3299 event, _sta(dock_handle));
3300 data = 0; /* unknown */
3302 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3303 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3304 ibm->acpi->device->dev.bus_id,
3308 static int dock_read(char *p)
3311 int docked = dock_docked();
3314 len += sprintf(p + len, "status:\t\tnot supported\n");
3316 len += sprintf(p + len, "status:\t\tundocked\n");
3318 len += sprintf(p + len, "status:\t\tdocked\n");
3319 len += sprintf(p + len, "commands:\tdock, undock\n");
3325 static int dock_write(char *buf)
3332 while ((cmd = next_cmd(&buf))) {
3333 if (strlencmp(cmd, "undock") == 0) {
3334 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3335 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3337 } else if (strlencmp(cmd, "dock") == 0) {
3338 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3347 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3349 /*************************************************************************
3353 #ifdef CONFIG_THINKPAD_ACPI_BAY
3355 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3356 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3357 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3358 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3359 ); /* A21e, R30, R31 */
3360 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3361 "_EJ0", /* all others */
3362 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3363 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3364 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3366 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3370 static int __init bay_init(struct ibm_init_struct *iibm)
3372 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3374 TPACPI_ACPIHANDLE_INIT(bay);
3376 TPACPI_ACPIHANDLE_INIT(bay_ej);
3377 TPACPI_ACPIHANDLE_INIT(bay2);
3379 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3381 tp_features.bay_status = bay_handle &&
3382 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3383 tp_features.bay_status2 = bay2_handle &&
3384 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3386 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3387 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3388 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3389 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3391 vdbg_printk(TPACPI_DBG_INIT,
3392 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3393 str_supported(tp_features.bay_status),
3394 str_supported(tp_features.bay_eject),
3395 str_supported(tp_features.bay_status2),
3396 str_supported(tp_features.bay_eject2));
3398 return (tp_features.bay_status || tp_features.bay_eject ||
3399 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3402 static void bay_notify(struct ibm_struct *ibm, u32 event)
3404 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3405 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3406 ibm->acpi->device->dev.bus_id,
3410 #define bay_occupied(b) (_sta(b##_handle) & 1)
3412 static int bay_read(char *p)
3415 int occupied = bay_occupied(bay);
3416 int occupied2 = bay_occupied(bay2);
3419 len += sprintf(p + len, "status:\t\t%s\n",
3420 tp_features.bay_status ?
3421 (occupied ? "occupied" : "unoccupied") :
3423 if (tp_features.bay_status2)
3424 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3425 "occupied" : "unoccupied");
3427 eject = tp_features.bay_eject && occupied;
3428 eject2 = tp_features.bay_eject2 && occupied2;
3430 if (eject && eject2)
3431 len += sprintf(p + len, "commands:\teject, eject2\n");
3433 len += sprintf(p + len, "commands:\teject\n");
3435 len += sprintf(p + len, "commands:\teject2\n");
3440 static int bay_write(char *buf)
3444 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3447 while ((cmd = next_cmd(&buf))) {
3448 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3449 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3451 } else if (tp_features.bay_eject2 &&
3452 strlencmp(cmd, "eject2") == 0) {
3453 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3462 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3463 .notify = bay_notify,
3464 .handle = &bay_handle,
3465 .type = ACPI_SYSTEM_NOTIFY,
3468 static struct ibm_struct bay_driver_data = {
3472 .acpi = &ibm_bay_acpidriver,
3475 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3477 /*************************************************************************
3481 /* sysfs cmos_command -------------------------------------------------- */
3482 static ssize_t cmos_command_store(struct device *dev,
3483 struct device_attribute *attr,
3484 const char *buf, size_t count)
3486 unsigned long cmos_cmd;
3489 if (parse_strtoul(buf, 21, &cmos_cmd))
3492 res = issue_thinkpad_cmos_command(cmos_cmd);
3493 return (res)? res : count;
3496 static struct device_attribute dev_attr_cmos_command =
3497 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3499 /* --------------------------------------------------------------------- */
3501 static int __init cmos_init(struct ibm_init_struct *iibm)
3505 vdbg_printk(TPACPI_DBG_INIT,
3506 "initializing cmos commands subdriver\n");
3508 TPACPI_ACPIHANDLE_INIT(cmos);
3510 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3511 str_supported(cmos_handle != NULL));
3513 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3517 return (cmos_handle)? 0 : 1;
3520 static void cmos_exit(void)
3522 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3525 static int cmos_read(char *p)
3529 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3530 R30, R31, T20-22, X20-21 */
3532 len += sprintf(p + len, "status:\t\tnot supported\n");
3534 len += sprintf(p + len, "status:\t\tsupported\n");
3535 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3541 static int cmos_write(char *buf)
3546 while ((cmd = next_cmd(&buf))) {
3547 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3548 cmos_cmd >= 0 && cmos_cmd <= 21) {
3553 res = issue_thinkpad_cmos_command(cmos_cmd);
3561 static struct ibm_struct cmos_driver_data = {
3564 .write = cmos_write,
3568 /*************************************************************************
3572 enum led_access_mode {
3573 TPACPI_LED_NONE = 0,
3574 TPACPI_LED_570, /* 570 */
3575 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3576 TPACPI_LED_NEW, /* all others */
3579 enum { /* For TPACPI_LED_OLD */
3580 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3581 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3582 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3585 static enum led_access_mode led_supported;
3587 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3588 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3589 /* T20-22, X20-21 */
3590 "LED", /* all others */
3593 static int __init led_init(struct ibm_init_struct *iibm)
3595 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3597 TPACPI_ACPIHANDLE_INIT(led);
3600 /* led not supported on R30, R31 */
3601 led_supported = TPACPI_LED_NONE;
3602 else if (strlencmp(led_path, "SLED") == 0)
3604 led_supported = TPACPI_LED_570;
3605 else if (strlencmp(led_path, "SYSL") == 0)
3606 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3607 led_supported = TPACPI_LED_OLD;
3610 led_supported = TPACPI_LED_NEW;
3612 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3613 str_supported(led_supported), led_supported);
3615 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3618 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3620 static int led_read(char *p)
3624 if (!led_supported) {
3625 len += sprintf(p + len, "status:\t\tnot supported\n");
3628 len += sprintf(p + len, "status:\t\tsupported\n");
3630 if (led_supported == TPACPI_LED_570) {
3633 for (i = 0; i < 8; i++) {
3634 if (!acpi_evalf(ec_handle,
3635 &status, "GLED", "dd", 1 << i))
3637 len += sprintf(p + len, "%d:\t\t%s\n",
3638 i, led_status(status));
3642 len += sprintf(p + len, "commands:\t"
3643 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3648 /* off, on, blink */
3649 static const int led_sled_arg1[] = { 0, 1, 3 };
3650 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3651 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3652 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3654 static int led_write(char *buf)
3662 while ((cmd = next_cmd(&buf))) {
3663 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3666 if (strstr(cmd, "off")) {
3668 } else if (strstr(cmd, "on")) {
3670 } else if (strstr(cmd, "blink")) {
3675 if (led_supported == TPACPI_LED_570) {
3678 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3679 led, led_sled_arg1[ind]))
3681 } else if (led_supported == TPACPI_LED_OLD) {
3682 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3684 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3686 ret = ec_write(TPACPI_LED_EC_HLBL,
3687 led * led_exp_hlbl[ind]);
3689 ret = ec_write(TPACPI_LED_EC_HLCL,
3690 led * led_exp_hlcl[ind]);
3695 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3696 led, led_led_arg1[ind]))
3704 static struct ibm_struct led_driver_data = {
3710 /*************************************************************************
3714 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3716 static int __init beep_init(struct ibm_init_struct *iibm)
3718 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3720 TPACPI_ACPIHANDLE_INIT(beep);
3722 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3723 str_supported(beep_handle != NULL));
3725 return (beep_handle)? 0 : 1;
3728 static int beep_read(char *p)
3733 len += sprintf(p + len, "status:\t\tnot supported\n");
3735 len += sprintf(p + len, "status:\t\tsupported\n");
3736 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3742 static int beep_write(char *buf)
3750 while ((cmd = next_cmd(&buf))) {
3751 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3752 beep_cmd >= 0 && beep_cmd <= 17) {
3756 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3763 static struct ibm_struct beep_driver_data = {
3766 .write = beep_write,
3769 /*************************************************************************
3773 enum thermal_access_mode {
3774 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3775 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3776 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3777 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3778 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3781 enum { /* TPACPI_THERMAL_TPEC_* */
3782 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3783 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3784 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3787 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3788 struct ibm_thermal_sensors_struct {
3789 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3792 static enum thermal_access_mode thermal_read_mode;
3794 /* idx is zero-based */
3795 static int thermal_get_sensor(int idx, s32 *value)
3801 t = TP_EC_THERMAL_TMP0;
3803 switch (thermal_read_mode) {
3804 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3805 case TPACPI_THERMAL_TPEC_16:
3806 if (idx >= 8 && idx <= 15) {
3807 t = TP_EC_THERMAL_TMP8;
3812 case TPACPI_THERMAL_TPEC_8:
3814 if (!acpi_ec_read(t + idx, &tmp))
3816 *value = tmp * 1000;
3821 case TPACPI_THERMAL_ACPI_UPDT:
3823 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3824 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3826 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3828 *value = (t - 2732) * 100;
3833 case TPACPI_THERMAL_ACPI_TMP07:
3835 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3836 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3838 if (t > 127 || t < -127)
3839 t = TP_EC_THERMAL_TMP_NA;
3845 case TPACPI_THERMAL_NONE:
3853 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3864 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3867 for (i = 0 ; i < n; i++) {
3868 res = thermal_get_sensor(i, &s->temp[i]);
3876 /* sysfs temp##_input -------------------------------------------------- */
3878 static ssize_t thermal_temp_input_show(struct device *dev,
3879 struct device_attribute *attr,
3882 struct sensor_device_attribute *sensor_attr =
3883 to_sensor_dev_attr(attr);
3884 int idx = sensor_attr->index;
3888 res = thermal_get_sensor(idx, &value);
3891 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3894 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3897 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3898 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3899 thermal_temp_input_show, NULL, _idxB)
3901 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3902 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3903 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3904 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3905 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3906 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3907 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3908 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3909 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3910 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3911 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3912 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3913 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3914 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3915 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3916 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3917 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3920 #define THERMAL_ATTRS(X) \
3921 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3923 static struct attribute *thermal_temp_input_attr[] = {
3943 static const struct attribute_group thermal_temp_input16_group = {
3944 .attrs = thermal_temp_input_attr
3947 static const struct attribute_group thermal_temp_input8_group = {
3948 .attrs = &thermal_temp_input_attr[8]
3951 #undef THERMAL_SENSOR_ATTR_TEMP
3952 #undef THERMAL_ATTRS
3954 /* --------------------------------------------------------------------- */
3956 static int __init thermal_init(struct ibm_init_struct *iibm)
3963 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3965 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3967 if (thinkpad_id.ec_model) {
3969 * Direct EC access mode: sensors at registers
3970 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3971 * non-implemented, thermal sensors return 0x80 when
3976 for (i = 0; i < 8; i++) {
3977 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3983 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3991 /* This is sheer paranoia, but we handle it anyway */
3994 "ThinkPad ACPI EC access misbehaving, "
3995 "falling back to ACPI TMPx access "
3997 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4000 "ThinkPad ACPI EC access misbehaving, "
4001 "disabling thermal sensors access\n");
4002 thermal_read_mode = TPACPI_THERMAL_NONE;
4007 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4009 } else if (acpi_tmp7) {
4010 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4011 /* 600e/x, 770e, 770x */
4012 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4014 /* Standard ACPI TMPx access, max 8 sensors */
4015 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4018 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4019 thermal_read_mode = TPACPI_THERMAL_NONE;
4022 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4023 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4026 switch (thermal_read_mode) {
4027 case TPACPI_THERMAL_TPEC_16:
4028 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4029 &thermal_temp_input16_group);
4033 case TPACPI_THERMAL_TPEC_8:
4034 case TPACPI_THERMAL_ACPI_TMP07:
4035 case TPACPI_THERMAL_ACPI_UPDT:
4036 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4037 &thermal_temp_input8_group);
4041 case TPACPI_THERMAL_NONE:
4049 static void thermal_exit(void)
4051 switch (thermal_read_mode) {
4052 case TPACPI_THERMAL_TPEC_16:
4053 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4054 &thermal_temp_input16_group);
4056 case TPACPI_THERMAL_TPEC_8:
4057 case TPACPI_THERMAL_ACPI_TMP07:
4058 case TPACPI_THERMAL_ACPI_UPDT:
4059 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4060 &thermal_temp_input16_group);
4062 case TPACPI_THERMAL_NONE:
4068 static int thermal_read(char *p)
4072 struct ibm_thermal_sensors_struct t;
4074 n = thermal_get_sensors(&t);
4075 if (unlikely(n < 0))
4078 len += sprintf(p + len, "temperatures:\t");
4081 for (i = 0; i < (n - 1); i++)
4082 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4083 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4085 len += sprintf(p + len, "not supported\n");
4090 static struct ibm_struct thermal_driver_data = {
4092 .read = thermal_read,
4093 .exit = thermal_exit,
4096 /*************************************************************************
4100 static u8 ecdump_regs[256];
4102 static int ecdump_read(char *p)
4108 len += sprintf(p + len, "EC "
4109 " +00 +01 +02 +03 +04 +05 +06 +07"
4110 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4111 for (i = 0; i < 256; i += 16) {
4112 len += sprintf(p + len, "EC 0x%02x:", i);
4113 for (j = 0; j < 16; j++) {
4114 if (!acpi_ec_read(i + j, &v))
4116 if (v != ecdump_regs[i + j])
4117 len += sprintf(p + len, " *%02x", v);
4119 len += sprintf(p + len, " %02x", v);
4120 ecdump_regs[i + j] = v;
4122 len += sprintf(p + len, "\n");
4127 /* These are way too dangerous to advertise openly... */
4129 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4130 " (<offset> is 00-ff, <value> is 00-ff)\n");
4131 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4132 " (<offset> is 00-ff, <value> is 0-255)\n");
4137 static int ecdump_write(char *buf)
4142 while ((cmd = next_cmd(&buf))) {
4143 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4145 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4149 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4150 if (!acpi_ec_write(i, v))
4159 static struct ibm_struct ecdump_driver_data = {
4161 .read = ecdump_read,
4162 .write = ecdump_write,
4163 .flags.experimental = 1,
4166 /*************************************************************************
4167 * Backlight/brightness subdriver
4170 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4172 static struct backlight_device *ibm_backlight_device;
4173 static int brightness_offset = 0x31;
4174 static int brightness_mode;
4175 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4177 static struct mutex brightness_mutex;
4180 * ThinkPads can read brightness from two places: EC 0x31, or
4181 * CMOS NVRAM byte 0x5E, bits 0-3.
4183 static int brightness_get(struct backlight_device *bd)
4185 u8 lec = 0, lcmos = 0, level = 0;
4187 if (brightness_mode & 1) {
4188 if (!acpi_ec_read(brightness_offset, &lec))
4190 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4193 if (brightness_mode & 2) {
4194 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4195 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4196 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4197 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4201 if (brightness_mode == 3 && lec != lcmos) {
4203 "CMOS NVRAM (%u) and EC (%u) do not agree "
4204 "on display brightness level\n",
4205 (unsigned int) lcmos,
4206 (unsigned int) lec);
4213 /* May return EINTR which can always be mapped to ERESTARTSYS */
4214 static int brightness_set(int value)
4216 int cmos_cmd, inc, i, res;
4219 if (value > ((tp_features.bright_16levels)? 15 : 7))
4222 res = mutex_lock_interruptible(&brightness_mutex);
4226 current_value = brightness_get(NULL);
4227 if (current_value < 0) {
4228 res = current_value;
4232 cmos_cmd = value > current_value ?
4233 TP_CMOS_BRIGHTNESS_UP :
4234 TP_CMOS_BRIGHTNESS_DOWN;
4235 inc = (value > current_value)? 1 : -1;
4238 for (i = current_value; i != value; i += inc) {
4239 if ((brightness_mode & 2) &&
4240 issue_thinkpad_cmos_command(cmos_cmd)) {
4244 if ((brightness_mode & 1) &&
4245 !acpi_ec_write(brightness_offset, i + inc)) {
4252 mutex_unlock(&brightness_mutex);
4256 /* sysfs backlight class ----------------------------------------------- */
4258 static int brightness_update_status(struct backlight_device *bd)
4260 /* it is the backlight class's job (caller) to handle
4261 * EINTR and other errors properly */
4262 return brightness_set(
4263 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4264 bd->props.power == FB_BLANK_UNBLANK) ?
4265 bd->props.brightness : 0);
4268 static struct backlight_ops ibm_backlight_data = {
4269 .get_brightness = brightness_get,
4270 .update_status = brightness_update_status,
4273 /* --------------------------------------------------------------------- */
4275 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4277 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4278 union acpi_object *obj;
4281 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4282 obj = (union acpi_object *)buffer.pointer;
4283 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4284 printk(TPACPI_ERR "Unknown BCLL data, "
4285 "please report this to %s\n", TPACPI_MAIL);
4288 rc = obj->package.count;
4294 kfree(buffer.pointer);
4298 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4299 void *context, void **rv)
4301 char name[ACPI_PATH_SEGMENT_LENGTH];
4302 struct acpi_buffer buffer = { sizeof(name), &name };
4304 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4305 !strncmp("BCLL", name, sizeof(name) - 1)) {
4306 if (tpacpi_query_bcll_levels(handle) == 16) {
4308 return AE_CTRL_TERMINATE;
4317 static int __init brightness_check_levels(void)
4320 void *found_node = NULL;
4323 TPACPI_ACPIHANDLE_INIT(vid);
4328 /* Search for a BCLL package with 16 levels */
4329 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4330 brightness_find_bcll, NULL,
4333 return (ACPI_SUCCESS(status) && found_node != NULL);
4336 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4337 void *context, void **rv)
4339 char name[ACPI_PATH_SEGMENT_LENGTH];
4340 struct acpi_buffer buffer = { sizeof(name), &name };
4342 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4343 !strncmp("_BCL", name, sizeof(name) - 1)) {
4345 return AE_CTRL_TERMINATE;
4351 static int __init brightness_check_std_acpi_support(void)
4354 void *found_node = NULL;
4357 TPACPI_ACPIHANDLE_INIT(vid);
4362 /* Search for a _BCL method, but don't execute it */
4363 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4364 brightness_find_bcl, NULL, &found_node);
4366 return (ACPI_SUCCESS(status) && found_node != NULL);
4369 static int __init brightness_init(struct ibm_init_struct *iibm)
4373 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4375 mutex_init(&brightness_mutex);
4377 if (!brightness_enable) {
4378 dbg_printk(TPACPI_DBG_INIT,
4379 "brightness support disabled by "
4380 "module parameter\n");
4382 } else if (brightness_enable > 1) {
4383 if (brightness_check_std_acpi_support()) {
4384 printk(TPACPI_NOTICE
4385 "standard ACPI backlight interface "
4386 "available, not loading native one...\n");
4391 if (!brightness_mode) {
4392 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4393 brightness_mode = 2;
4395 brightness_mode = 3;
4397 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4401 if (brightness_mode > 3)
4404 tp_features.bright_16levels =
4405 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4406 brightness_check_levels();
4408 b = brightness_get(NULL);
4412 if (tp_features.bright_16levels)
4414 "detected a 16-level brightness capable ThinkPad\n");
4416 ibm_backlight_device = backlight_device_register(
4417 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4418 &ibm_backlight_data);
4419 if (IS_ERR(ibm_backlight_device)) {
4420 printk(TPACPI_ERR "Could not register backlight device\n");
4421 return PTR_ERR(ibm_backlight_device);
4423 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4425 ibm_backlight_device->props.max_brightness =
4426 (tp_features.bright_16levels)? 15 : 7;
4427 ibm_backlight_device->props.brightness = b;
4428 backlight_update_status(ibm_backlight_device);
4433 static void brightness_exit(void)
4435 if (ibm_backlight_device) {
4436 vdbg_printk(TPACPI_DBG_EXIT,
4437 "calling backlight_device_unregister()\n");
4438 backlight_device_unregister(ibm_backlight_device);
4439 ibm_backlight_device = NULL;
4443 static int brightness_read(char *p)
4448 level = brightness_get(NULL);
4450 len += sprintf(p + len, "level:\t\tunreadable\n");
4452 len += sprintf(p + len, "level:\t\t%d\n", level);
4453 len += sprintf(p + len, "commands:\tup, down\n");
4454 len += sprintf(p + len, "commands:\tlevel <level>"
4455 " (<level> is 0-%d)\n",
4456 (tp_features.bright_16levels) ? 15 : 7);
4462 static int brightness_write(char *buf)
4467 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4469 level = brightness_get(NULL);
4473 while ((cmd = next_cmd(&buf))) {
4474 if (strlencmp(cmd, "up") == 0) {
4475 if (level < max_level)
4477 } else if (strlencmp(cmd, "down") == 0) {
4480 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4481 level >= 0 && level <= max_level) {
4488 * Now we know what the final level should be, so we try to set it.
4489 * Doing it this way makes the syscall restartable in case of EINTR
4491 rc = brightness_set(level);
4492 return (rc == -EINTR)? ERESTARTSYS : rc;
4495 static struct ibm_struct brightness_driver_data = {
4496 .name = "brightness",
4497 .read = brightness_read,
4498 .write = brightness_write,
4499 .exit = brightness_exit,
4502 /*************************************************************************
4506 static int volume_offset = 0x30;
4508 static int volume_read(char *p)
4513 if (!acpi_ec_read(volume_offset, &level)) {
4514 len += sprintf(p + len, "level:\t\tunreadable\n");
4516 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4517 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4518 len += sprintf(p + len, "commands:\tup, down, mute\n");
4519 len += sprintf(p + len, "commands:\tlevel <level>"
4520 " (<level> is 0-15)\n");
4526 static int volume_write(char *buf)
4528 int cmos_cmd, inc, i;
4530 int new_level, new_mute;
4533 while ((cmd = next_cmd(&buf))) {
4534 if (!acpi_ec_read(volume_offset, &level))
4536 new_mute = mute = level & 0x40;
4537 new_level = level = level & 0xf;
4539 if (strlencmp(cmd, "up") == 0) {
4543 new_level = level == 15 ? 15 : level + 1;
4544 } else if (strlencmp(cmd, "down") == 0) {
4548 new_level = level == 0 ? 0 : level - 1;
4549 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4550 new_level >= 0 && new_level <= 15) {
4552 } else if (strlencmp(cmd, "mute") == 0) {
4557 if (new_level != level) {
4558 /* mute doesn't change */
4560 cmos_cmd = (new_level > level) ?
4561 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4562 inc = new_level > level ? 1 : -1;
4564 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4565 !acpi_ec_write(volume_offset, level)))
4568 for (i = level; i != new_level; i += inc)
4569 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4570 !acpi_ec_write(volume_offset, i + inc))
4574 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4575 !acpi_ec_write(volume_offset, new_level + mute))) {
4580 if (new_mute != mute) {
4581 /* level doesn't change */
4583 cmos_cmd = (new_mute) ?
4584 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4586 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4587 !acpi_ec_write(volume_offset, level + new_mute))
4595 static struct ibm_struct volume_driver_data = {
4597 .read = volume_read,
4598 .write = volume_write,
4601 /*************************************************************************
4608 * TPACPI_FAN_RD_ACPI_GFAN:
4609 * ACPI GFAN method: returns fan level
4611 * see TPACPI_FAN_WR_ACPI_SFAN
4612 * EC 0x2f (HFSP) not available if GFAN exists
4614 * TPACPI_FAN_WR_ACPI_SFAN:
4615 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4617 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4620 * TPACPI_FAN_WR_TPEC:
4621 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4622 * Supported on almost all ThinkPads
4624 * Fan speed changes of any sort (including those caused by the
4625 * disengaged mode) are usually done slowly by the firmware as the
4626 * maximum ammount of fan duty cycle change per second seems to be
4629 * Reading is not available if GFAN exists.
4630 * Writing is not available if SFAN exists.
4633 * 7 automatic mode engaged;
4634 * (default operation mode of the ThinkPad)
4635 * fan level is ignored in this mode.
4636 * 6 full speed mode (takes precedence over bit 7);
4637 * not available on all thinkpads. May disable
4638 * the tachometer while the fan controller ramps up
4639 * the speed (which can take up to a few *minutes*).
4640 * Speeds up fan to 100% duty-cycle, which is far above
4641 * the standard RPM levels. It is not impossible that
4642 * it could cause hardware damage.
4643 * 5-3 unused in some models. Extra bits for fan level
4644 * in others, but still useless as all values above
4645 * 7 map to the same speed as level 7 in these models.
4646 * 2-0 fan level (0..7 usually)
4648 * 0x07 = max (set when temperatures critical)
4649 * Some ThinkPads may have other levels, see
4650 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4652 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4653 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4654 * does so, its initial value is meaningless (0x07).
4656 * For firmware bugs, refer to:
4657 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4661 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4662 * Main fan tachometer reading (in RPM)
4664 * This register is present on all ThinkPads with a new-style EC, and
4665 * it is known not to be present on the A21m/e, and T22, as there is
4666 * something else in offset 0x84 according to the ACPI DSDT. Other
4667 * ThinkPads from this same time period (and earlier) probably lack the
4668 * tachometer as well.
4670 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4671 * was never fixed by IBM to report the EC firmware version string
4672 * probably support the tachometer (like the early X models), so
4673 * detecting it is quite hard. We need more data to know for sure.
4675 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4678 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4681 * For firmware bugs, refer to:
4682 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4684 * TPACPI_FAN_WR_ACPI_FANS:
4685 * ThinkPad X31, X40, X41. Not available in the X60.
4687 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4688 * high speed. ACPI DSDT seems to map these three speeds to levels
4689 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4690 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4692 * The speeds are stored on handles
4693 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4695 * There are three default speed sets, acessible as handles:
4696 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4698 * ACPI DSDT switches which set is in use depending on various
4701 * TPACPI_FAN_WR_TPEC is also available and should be used to
4702 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4703 * but the ACPI tables just mention level 7.
4706 enum { /* Fan control constants */
4707 fan_status_offset = 0x2f, /* EC register 0x2f */
4708 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4709 * 0x84 must be read before 0x85 */
4711 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4712 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4714 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4717 enum fan_status_access_mode {
4718 TPACPI_FAN_NONE = 0, /* No fan status or control */
4719 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4720 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4723 enum fan_control_access_mode {
4724 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4725 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4726 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4727 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4730 enum fan_control_commands {
4731 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4732 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4733 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4734 * and also watchdog cmd */
4737 static int fan_control_allowed;
4739 static enum fan_status_access_mode fan_status_access_mode;
4740 static enum fan_control_access_mode fan_control_access_mode;
4741 static enum fan_control_commands fan_control_commands;
4743 static u8 fan_control_initial_status;
4744 static u8 fan_control_desired_level;
4745 static int fan_watchdog_maxinterval;
4747 static struct mutex fan_mutex;
4749 static void fan_watchdog_fire(struct work_struct *ignored);
4750 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4752 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4753 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4754 "\\FSPD", /* 600e/x, 770e, 770x */
4756 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4757 "JFNS", /* 770x-JL */
4761 * Call with fan_mutex held
4763 static void fan_update_desired_level(u8 status)
4766 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4768 fan_control_desired_level = 7;
4770 fan_control_desired_level = status;
4774 static int fan_get_status(u8 *status)
4779 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4781 switch (fan_status_access_mode) {
4782 case TPACPI_FAN_RD_ACPI_GFAN:
4783 /* 570, 600e/x, 770e, 770x */
4785 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4793 case TPACPI_FAN_RD_TPEC:
4794 /* all except 570, 600e/x, 770e, 770x */
4795 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4810 static int fan_get_status_safe(u8 *status)
4815 if (mutex_lock_interruptible(&fan_mutex))
4816 return -ERESTARTSYS;
4817 rc = fan_get_status(&s);
4819 fan_update_desired_level(s);
4820 mutex_unlock(&fan_mutex);
4828 static int fan_get_speed(unsigned int *speed)
4832 switch (fan_status_access_mode) {
4833 case TPACPI_FAN_RD_TPEC:
4834 /* all except 570, 600e/x, 770e, 770x */
4835 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4836 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4840 *speed = (hi << 8) | lo;
4851 static int fan_set_level(int level)
4853 if (!fan_control_allowed)
4856 switch (fan_control_access_mode) {
4857 case TPACPI_FAN_WR_ACPI_SFAN:
4858 if (level >= 0 && level <= 7) {
4859 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4865 case TPACPI_FAN_WR_ACPI_FANS:
4866 case TPACPI_FAN_WR_TPEC:
4867 if ((level != TP_EC_FAN_AUTO) &&
4868 (level != TP_EC_FAN_FULLSPEED) &&
4869 ((level < 0) || (level > 7)))
4872 /* safety net should the EC not support AUTO
4873 * or FULLSPEED mode bits and just ignore them */
4874 if (level & TP_EC_FAN_FULLSPEED)
4875 level |= 7; /* safety min speed 7 */
4876 else if (level & TP_EC_FAN_AUTO)
4877 level |= 4; /* safety min speed 4 */
4879 if (!acpi_ec_write(fan_status_offset, level))
4882 tp_features.fan_ctrl_status_undef = 0;
4891 static int fan_set_level_safe(int level)
4895 if (!fan_control_allowed)
4898 if (mutex_lock_interruptible(&fan_mutex))
4899 return -ERESTARTSYS;
4901 if (level == TPACPI_FAN_LAST_LEVEL)
4902 level = fan_control_desired_level;
4904 rc = fan_set_level(level);
4906 fan_update_desired_level(level);
4908 mutex_unlock(&fan_mutex);
4912 static int fan_set_enable(void)
4917 if (!fan_control_allowed)
4920 if (mutex_lock_interruptible(&fan_mutex))
4921 return -ERESTARTSYS;
4923 switch (fan_control_access_mode) {
4924 case TPACPI_FAN_WR_ACPI_FANS:
4925 case TPACPI_FAN_WR_TPEC:
4926 rc = fan_get_status(&s);
4930 /* Don't go out of emergency fan mode */
4933 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4936 if (!acpi_ec_write(fan_status_offset, s))
4939 tp_features.fan_ctrl_status_undef = 0;
4944 case TPACPI_FAN_WR_ACPI_SFAN:
4945 rc = fan_get_status(&s);
4951 /* Set fan to at least level 4 */
4954 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4964 mutex_unlock(&fan_mutex);
4968 static int fan_set_disable(void)
4972 if (!fan_control_allowed)
4975 if (mutex_lock_interruptible(&fan_mutex))
4976 return -ERESTARTSYS;
4979 switch (fan_control_access_mode) {
4980 case TPACPI_FAN_WR_ACPI_FANS:
4981 case TPACPI_FAN_WR_TPEC:
4982 if (!acpi_ec_write(fan_status_offset, 0x00))
4985 fan_control_desired_level = 0;
4986 tp_features.fan_ctrl_status_undef = 0;
4990 case TPACPI_FAN_WR_ACPI_SFAN:
4991 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4994 fan_control_desired_level = 0;
5002 mutex_unlock(&fan_mutex);
5006 static int fan_set_speed(int speed)
5010 if (!fan_control_allowed)
5013 if (mutex_lock_interruptible(&fan_mutex))
5014 return -ERESTARTSYS;
5017 switch (fan_control_access_mode) {
5018 case TPACPI_FAN_WR_ACPI_FANS:
5019 if (speed >= 0 && speed <= 65535) {
5020 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5021 speed, speed, speed))
5031 mutex_unlock(&fan_mutex);
5035 static void fan_watchdog_reset(void)
5037 static int fan_watchdog_active;
5039 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5042 if (fan_watchdog_active)
5043 cancel_delayed_work(&fan_watchdog_task);
5045 if (fan_watchdog_maxinterval > 0 &&
5046 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5047 fan_watchdog_active = 1;
5048 if (!schedule_delayed_work(&fan_watchdog_task,
5049 msecs_to_jiffies(fan_watchdog_maxinterval
5052 "failed to schedule the fan watchdog, "
5053 "watchdog will not trigger\n");
5056 fan_watchdog_active = 0;
5059 static void fan_watchdog_fire(struct work_struct *ignored)
5063 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5066 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5067 rc = fan_set_enable();
5069 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5070 "will try again later...\n", -rc);
5071 /* reschedule for later */
5072 fan_watchdog_reset();
5077 * SYSFS fan layout: hwmon compatible (device)
5080 * 0: "disengaged" mode
5082 * 2: native EC "auto" mode (recommended, hardware default)
5084 * pwm*: set speed in manual mode, ignored otherwise.
5085 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5088 * fan*_input: tachometer reading, RPM
5091 * SYSFS fan layout: extensions
5093 * fan_watchdog (driver):
5094 * fan watchdog interval in seconds, 0 disables (default), max 120
5097 /* sysfs fan pwm1_enable ----------------------------------------------- */
5098 static ssize_t fan_pwm1_enable_show(struct device *dev,
5099 struct device_attribute *attr,
5105 res = fan_get_status_safe(&status);
5109 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5110 if (status != fan_control_initial_status) {
5111 tp_features.fan_ctrl_status_undef = 0;
5113 /* Return most likely status. In fact, it
5114 * might be the only possible status */
5115 status = TP_EC_FAN_AUTO;
5119 if (status & TP_EC_FAN_FULLSPEED) {
5121 } else if (status & TP_EC_FAN_AUTO) {
5126 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5129 static ssize_t fan_pwm1_enable_store(struct device *dev,
5130 struct device_attribute *attr,
5131 const char *buf, size_t count)
5136 if (parse_strtoul(buf, 2, &t))
5141 level = TP_EC_FAN_FULLSPEED;
5144 level = TPACPI_FAN_LAST_LEVEL;
5147 level = TP_EC_FAN_AUTO;
5150 /* reserved for software-controlled auto mode */
5156 res = fan_set_level_safe(level);
5162 fan_watchdog_reset();
5167 static struct device_attribute dev_attr_fan_pwm1_enable =
5168 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5169 fan_pwm1_enable_show, fan_pwm1_enable_store);
5171 /* sysfs fan pwm1 ------------------------------------------------------ */
5172 static ssize_t fan_pwm1_show(struct device *dev,
5173 struct device_attribute *attr,
5179 res = fan_get_status_safe(&status);
5183 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5184 if (status != fan_control_initial_status) {
5185 tp_features.fan_ctrl_status_undef = 0;
5187 status = TP_EC_FAN_AUTO;
5192 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5193 status = fan_control_desired_level;
5198 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5201 static ssize_t fan_pwm1_store(struct device *dev,
5202 struct device_attribute *attr,
5203 const char *buf, size_t count)
5207 u8 status, newlevel;
5209 if (parse_strtoul(buf, 255, &s))
5212 /* scale down from 0-255 to 0-7 */
5213 newlevel = (s >> 5) & 0x07;
5215 if (mutex_lock_interruptible(&fan_mutex))
5216 return -ERESTARTSYS;
5218 rc = fan_get_status(&status);
5219 if (!rc && (status &
5220 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5221 rc = fan_set_level(newlevel);
5225 fan_update_desired_level(newlevel);
5226 fan_watchdog_reset();
5230 mutex_unlock(&fan_mutex);
5231 return (rc)? rc : count;
5234 static struct device_attribute dev_attr_fan_pwm1 =
5235 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5236 fan_pwm1_show, fan_pwm1_store);
5238 /* sysfs fan fan1_input ------------------------------------------------ */
5239 static ssize_t fan_fan1_input_show(struct device *dev,
5240 struct device_attribute *attr,
5246 res = fan_get_speed(&speed);
5250 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5253 static struct device_attribute dev_attr_fan_fan1_input =
5254 __ATTR(fan1_input, S_IRUGO,
5255 fan_fan1_input_show, NULL);
5257 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5258 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5261 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5264 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5265 const char *buf, size_t count)
5269 if (parse_strtoul(buf, 120, &t))
5272 if (!fan_control_allowed)
5275 fan_watchdog_maxinterval = t;
5276 fan_watchdog_reset();
5281 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5282 fan_fan_watchdog_show, fan_fan_watchdog_store);
5284 /* --------------------------------------------------------------------- */
5285 static struct attribute *fan_attributes[] = {
5286 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5287 &dev_attr_fan_fan1_input.attr,
5291 static const struct attribute_group fan_attr_group = {
5292 .attrs = fan_attributes,
5295 static int __init fan_init(struct ibm_init_struct *iibm)
5299 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5301 mutex_init(&fan_mutex);
5302 fan_status_access_mode = TPACPI_FAN_NONE;
5303 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5304 fan_control_commands = 0;
5305 fan_watchdog_maxinterval = 0;
5306 tp_features.fan_ctrl_status_undef = 0;
5307 fan_control_desired_level = 7;
5309 TPACPI_ACPIHANDLE_INIT(fans);
5310 TPACPI_ACPIHANDLE_INIT(gfan);
5311 TPACPI_ACPIHANDLE_INIT(sfan);
5314 /* 570, 600e/x, 770e, 770x */
5315 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5317 /* all other ThinkPads: note that even old-style
5318 * ThinkPad ECs supports the fan control register */
5319 if (likely(acpi_ec_read(fan_status_offset,
5320 &fan_control_initial_status))) {
5321 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5323 /* In some ThinkPads, neither the EC nor the ACPI
5324 * DSDT initialize the fan status, and it ends up
5325 * being set to 0x07 when it *could* be either
5328 * Enable for TP-1Y (T43), TP-78 (R51e),
5329 * TP-76 (R52), TP-70 (T43, R52), which are known
5331 if (fan_control_initial_status == 0x07) {
5332 switch (thinkpad_id.ec_model) {
5333 case 0x5931: /* TP-1Y */
5334 case 0x3837: /* TP-78 */
5335 case 0x3637: /* TP-76 */
5336 case 0x3037: /* TP-70 */
5337 printk(TPACPI_NOTICE
5338 "fan_init: initial fan status "
5339 "is unknown, assuming it is "
5341 tp_features.fan_ctrl_status_undef = 1;
5347 "ThinkPad ACPI EC access misbehaving, "
5348 "fan status and control unavailable\n");
5355 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5356 fan_control_commands |=
5357 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5360 /* gfan without sfan means no fan control */
5361 /* all other models implement TP EC 0x2f control */
5365 fan_control_access_mode =
5366 TPACPI_FAN_WR_ACPI_FANS;
5367 fan_control_commands |=
5368 TPACPI_FAN_CMD_SPEED |
5369 TPACPI_FAN_CMD_LEVEL |
5370 TPACPI_FAN_CMD_ENABLE;
5372 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5373 fan_control_commands |=
5374 TPACPI_FAN_CMD_LEVEL |
5375 TPACPI_FAN_CMD_ENABLE;
5380 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5381 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5382 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5383 fan_status_access_mode, fan_control_access_mode);
5385 /* fan control master switch */
5386 if (!fan_control_allowed) {
5387 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5388 fan_control_commands = 0;
5389 dbg_printk(TPACPI_DBG_INIT,
5390 "fan control features disabled by parameter\n");
5393 /* update fan_control_desired_level */
5394 if (fan_status_access_mode != TPACPI_FAN_NONE)
5395 fan_get_status_safe(NULL);
5397 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5398 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5399 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5402 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5403 &driver_attr_fan_watchdog);
5411 static void fan_exit(void)
5413 vdbg_printk(TPACPI_DBG_EXIT,
5414 "cancelling any pending fan watchdog tasks\n");
5416 /* FIXME: can we really do this unconditionally? */
5417 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5418 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5419 &driver_attr_fan_watchdog);
5421 cancel_delayed_work(&fan_watchdog_task);
5422 flush_scheduled_work();
5425 static int fan_read(char *p)
5430 unsigned int speed = 0;
5432 switch (fan_status_access_mode) {
5433 case TPACPI_FAN_RD_ACPI_GFAN:
5434 /* 570, 600e/x, 770e, 770x */
5435 rc = fan_get_status_safe(&status);
5439 len += sprintf(p + len, "status:\t\t%s\n"
5441 (status != 0) ? "enabled" : "disabled", status);
5444 case TPACPI_FAN_RD_TPEC:
5445 /* all except 570, 600e/x, 770e, 770x */
5446 rc = fan_get_status_safe(&status);
5450 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5451 if (status != fan_control_initial_status)
5452 tp_features.fan_ctrl_status_undef = 0;
5454 /* Return most likely status. In fact, it
5455 * might be the only possible status */
5456 status = TP_EC_FAN_AUTO;
5459 len += sprintf(p + len, "status:\t\t%s\n",
5460 (status != 0) ? "enabled" : "disabled");
5462 rc = fan_get_speed(&speed);
5466 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5468 if (status & TP_EC_FAN_FULLSPEED)
5469 /* Disengaged mode takes precedence */
5470 len += sprintf(p + len, "level:\t\tdisengaged\n");
5471 else if (status & TP_EC_FAN_AUTO)
5472 len += sprintf(p + len, "level:\t\tauto\n");
5474 len += sprintf(p + len, "level:\t\t%d\n", status);
5477 case TPACPI_FAN_NONE:
5479 len += sprintf(p + len, "status:\t\tnot supported\n");
5482 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5483 len += sprintf(p + len, "commands:\tlevel <level>");
5485 switch (fan_control_access_mode) {
5486 case TPACPI_FAN_WR_ACPI_SFAN:
5487 len += sprintf(p + len, " (<level> is 0-7)\n");
5491 len += sprintf(p + len, " (<level> is 0-7, "
5492 "auto, disengaged, full-speed)\n");
5497 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5498 len += sprintf(p + len, "commands:\tenable, disable\n"
5499 "commands:\twatchdog <timeout> (<timeout> "
5500 "is 0 (off), 1-120 (seconds))\n");
5502 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5503 len += sprintf(p + len, "commands:\tspeed <speed>"
5504 " (<speed> is 0-65535)\n");
5509 static int fan_write_cmd_level(const char *cmd, int *rc)
5513 if (strlencmp(cmd, "level auto") == 0)
5514 level = TP_EC_FAN_AUTO;
5515 else if ((strlencmp(cmd, "level disengaged") == 0) |
5516 (strlencmp(cmd, "level full-speed") == 0))
5517 level = TP_EC_FAN_FULLSPEED;
5518 else if (sscanf(cmd, "level %d", &level) != 1)
5521 *rc = fan_set_level_safe(level);
5523 printk(TPACPI_ERR "level command accepted for unsupported "
5524 "access mode %d", fan_control_access_mode);
5529 static int fan_write_cmd_enable(const char *cmd, int *rc)
5531 if (strlencmp(cmd, "enable") != 0)
5534 *rc = fan_set_enable();
5536 printk(TPACPI_ERR "enable command accepted for unsupported "
5537 "access mode %d", fan_control_access_mode);
5542 static int fan_write_cmd_disable(const char *cmd, int *rc)
5544 if (strlencmp(cmd, "disable") != 0)
5547 *rc = fan_set_disable();
5549 printk(TPACPI_ERR "disable command accepted for unsupported "
5550 "access mode %d", fan_control_access_mode);
5555 static int fan_write_cmd_speed(const char *cmd, int *rc)
5560 * Support speed <low> <medium> <high> ? */
5562 if (sscanf(cmd, "speed %d", &speed) != 1)
5565 *rc = fan_set_speed(speed);
5567 printk(TPACPI_ERR "speed command accepted for unsupported "
5568 "access mode %d", fan_control_access_mode);
5573 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5577 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5580 if (interval < 0 || interval > 120)
5583 fan_watchdog_maxinterval = interval;
5588 static int fan_write(char *buf)
5593 while (!rc && (cmd = next_cmd(&buf))) {
5594 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5595 fan_write_cmd_level(cmd, &rc)) &&
5596 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5597 (fan_write_cmd_enable(cmd, &rc) ||
5598 fan_write_cmd_disable(cmd, &rc) ||
5599 fan_write_cmd_watchdog(cmd, &rc))) &&
5600 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5601 fan_write_cmd_speed(cmd, &rc))
5605 fan_watchdog_reset();
5611 static struct ibm_struct fan_driver_data = {
5618 /****************************************************************************
5619 ****************************************************************************
5623 ****************************************************************************
5624 ****************************************************************************/
5626 /* sysfs name ---------------------------------------------------------- */
5627 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5628 struct device_attribute *attr,
5631 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5634 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5635 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5637 /* --------------------------------------------------------------------- */
5640 static struct proc_dir_entry *proc_dir;
5643 * Module and infrastructure proble, init and exit handling
5646 static int force_load;
5648 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5649 static const char * __init str_supported(int is_supported)
5651 static char text_unsupported[] __initdata = "not supported";
5653 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5655 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5657 static void ibm_exit(struct ibm_struct *ibm)
5659 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5661 list_del_init(&ibm->all_drivers);
5663 if (ibm->flags.acpi_notify_installed) {
5664 dbg_printk(TPACPI_DBG_EXIT,
5665 "%s: acpi_remove_notify_handler\n", ibm->name);
5667 acpi_remove_notify_handler(*ibm->acpi->handle,
5669 dispatch_acpi_notify);
5670 ibm->flags.acpi_notify_installed = 0;
5671 ibm->flags.acpi_notify_installed = 0;
5674 if (ibm->flags.proc_created) {
5675 dbg_printk(TPACPI_DBG_EXIT,
5676 "%s: remove_proc_entry\n", ibm->name);
5677 remove_proc_entry(ibm->name, proc_dir);
5678 ibm->flags.proc_created = 0;
5681 if (ibm->flags.acpi_driver_registered) {
5682 dbg_printk(TPACPI_DBG_EXIT,
5683 "%s: acpi_bus_unregister_driver\n", ibm->name);
5685 acpi_bus_unregister_driver(ibm->acpi->driver);
5686 kfree(ibm->acpi->driver);
5687 ibm->acpi->driver = NULL;
5688 ibm->flags.acpi_driver_registered = 0;
5691 if (ibm->flags.init_called && ibm->exit) {
5693 ibm->flags.init_called = 0;
5696 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5699 static int __init ibm_init(struct ibm_init_struct *iibm)
5702 struct ibm_struct *ibm = iibm->data;
5703 struct proc_dir_entry *entry;
5705 BUG_ON(ibm == NULL);
5707 INIT_LIST_HEAD(&ibm->all_drivers);
5709 if (ibm->flags.experimental && !experimental)
5712 dbg_printk(TPACPI_DBG_INIT,
5713 "probing for %s\n", ibm->name);
5716 ret = iibm->init(iibm);
5718 return 0; /* probe failed */
5722 ibm->flags.init_called = 1;
5726 if (ibm->acpi->hid) {
5727 ret = register_tpacpi_subdriver(ibm);
5732 if (ibm->acpi->notify) {
5733 ret = setup_acpi_notify(ibm);
5734 if (ret == -ENODEV) {
5735 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5745 dbg_printk(TPACPI_DBG_INIT,
5746 "%s installed\n", ibm->name);
5749 entry = create_proc_entry(ibm->name,
5750 S_IFREG | S_IRUGO | S_IWUSR,
5753 printk(TPACPI_ERR "unable to create proc entry %s\n",
5758 entry->owner = THIS_MODULE;
5760 entry->read_proc = &dispatch_procfs_read;
5762 entry->write_proc = &dispatch_procfs_write;
5763 ibm->flags.proc_created = 1;
5766 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5771 dbg_printk(TPACPI_DBG_INIT,
5772 "%s: at error exit path with result %d\n",
5776 return (ret < 0)? ret : 0;
5781 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5783 const struct dmi_device *dev = NULL;
5784 char ec_fw_string[18];
5789 memset(tp, 0, sizeof(*tp));
5791 if (dmi_name_in_vendors("IBM"))
5792 tp->vendor = PCI_VENDOR_ID_IBM;
5793 else if (dmi_name_in_vendors("LENOVO"))
5794 tp->vendor = PCI_VENDOR_ID_LENOVO;
5798 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5800 if (!tp->bios_version_str)
5802 tp->bios_model = tp->bios_version_str[0]
5803 | (tp->bios_version_str[1] << 8);
5806 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5807 * X32 or newer, all Z series; Some models must have an
5808 * up-to-date BIOS or they will not be detected.
5810 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5812 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5813 if (sscanf(dev->name,
5814 "IBM ThinkPad Embedded Controller -[%17c",
5815 ec_fw_string) == 1) {
5816 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5817 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5819 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5820 tp->ec_model = ec_fw_string[0]
5821 | (ec_fw_string[1] << 8);
5826 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5828 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5829 kfree(tp->model_str);
5830 tp->model_str = NULL;
5834 static int __init probe_for_thinkpad(void)
5842 * Non-ancient models have better DMI tagging, but very old models
5845 is_thinkpad = (thinkpad_id.model_str != NULL);
5847 /* ec is required because many other handles are relative to it */
5848 TPACPI_ACPIHANDLE_INIT(ec);
5852 "Not yet supported ThinkPad detected!\n");
5857 * Risks a regression on very old machines, but reduces potential
5858 * false positives a damn great deal
5861 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5863 if (!is_thinkpad && !force_load)
5870 /* Module init, exit, parameters */
5872 static struct ibm_init_struct ibms_init[] __initdata = {
5874 .init = thinkpad_acpi_driver_init,
5875 .data = &thinkpad_acpi_driver_data,
5878 .init = hotkey_init,
5879 .data = &hotkey_driver_data,
5882 .init = bluetooth_init,
5883 .data = &bluetooth_driver_data,
5887 .data = &wan_driver_data,
5889 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5892 .data = &video_driver_data,
5897 .data = &light_driver_data,
5899 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5902 .data = &dock_driver_data[0],
5906 .data = &dock_driver_data[1],
5909 #ifdef CONFIG_THINKPAD_ACPI_BAY
5912 .data = &bay_driver_data,
5917 .data = &cmos_driver_data,
5921 .data = &led_driver_data,
5925 .data = &beep_driver_data,
5928 .init = thermal_init,
5929 .data = &thermal_driver_data,
5932 .data = &ecdump_driver_data,
5935 .init = brightness_init,
5936 .data = &brightness_driver_data,
5939 .data = &volume_driver_data,
5943 .data = &fan_driver_data,
5947 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5950 struct ibm_struct *ibm;
5952 if (!kp || !kp->name || !val)
5955 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5956 ibm = ibms_init[i].data;
5957 WARN_ON(ibm == NULL);
5959 if (!ibm || !ibm->name)
5962 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5963 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5965 strcpy(ibms_init[i].param, val);
5966 strcat(ibms_init[i].param, ",");
5974 module_param(experimental, int, 0);
5975 MODULE_PARM_DESC(experimental,
5976 "Enables experimental features when non-zero");
5978 module_param_named(debug, dbg_level, uint, 0);
5979 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5981 module_param(force_load, bool, 0);
5982 MODULE_PARM_DESC(force_load,
5983 "Attempts to load the driver even on a "
5984 "mis-identified ThinkPad when true");
5986 module_param_named(fan_control, fan_control_allowed, bool, 0);
5987 MODULE_PARM_DESC(fan_control,
5988 "Enables setting fan parameters features when true");
5990 module_param_named(brightness_mode, brightness_mode, int, 0);
5991 MODULE_PARM_DESC(brightness_mode,
5992 "Selects brightness control strategy: "
5993 "0=auto, 1=EC, 2=CMOS, 3=both");
5995 module_param(brightness_enable, uint, 0);
5996 MODULE_PARM_DESC(brightness_enable,
5997 "Enables backlight control when 1, disables when 0");
5999 module_param(hotkey_report_mode, uint, 0);
6000 MODULE_PARM_DESC(hotkey_report_mode,
6001 "used for backwards compatibility with userspace, "
6002 "see documentation");
6004 #define TPACPI_PARAM(feature) \
6005 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6006 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6007 "at module load, see documentation")
6009 TPACPI_PARAM(hotkey);
6010 TPACPI_PARAM(bluetooth);
6011 TPACPI_PARAM(video);
6012 TPACPI_PARAM(light);
6013 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6016 #ifdef CONFIG_THINKPAD_ACPI_BAY
6018 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6022 TPACPI_PARAM(ecdump);
6023 TPACPI_PARAM(brightness);
6024 TPACPI_PARAM(volume);
6027 static void thinkpad_acpi_module_exit(void)
6029 struct ibm_struct *ibm, *itmp;
6031 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6033 list_for_each_entry_safe_reverse(ibm, itmp,
6034 &tpacpi_all_drivers,
6039 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6041 if (tpacpi_inputdev) {
6042 if (tp_features.input_device_registered)
6043 input_unregister_device(tpacpi_inputdev);
6045 input_free_device(tpacpi_inputdev);
6049 hwmon_device_unregister(tpacpi_hwmon);
6051 if (tp_features.sensors_pdev_attrs_registered)
6052 device_remove_file(&tpacpi_sensors_pdev->dev,
6053 &dev_attr_thinkpad_acpi_pdev_name);
6054 if (tpacpi_sensors_pdev)
6055 platform_device_unregister(tpacpi_sensors_pdev);
6057 platform_device_unregister(tpacpi_pdev);
6059 if (tp_features.sensors_pdrv_attrs_registered)
6060 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6061 if (tp_features.platform_drv_attrs_registered)
6062 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6064 if (tp_features.sensors_pdrv_registered)
6065 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6067 if (tp_features.platform_drv_registered)
6068 platform_driver_unregister(&tpacpi_pdriver);
6071 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6073 kfree(thinkpad_id.bios_version_str);
6074 kfree(thinkpad_id.ec_version_str);
6075 kfree(thinkpad_id.model_str);
6079 static int __init thinkpad_acpi_module_init(void)
6083 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6085 /* Parameter checking */
6086 if (hotkey_report_mode > 2)
6089 /* Driver-level probe */
6091 get_thinkpad_model_data(&thinkpad_id);
6092 ret = probe_for_thinkpad();
6094 thinkpad_acpi_module_exit();
6098 /* Driver initialization */
6100 TPACPI_ACPIHANDLE_INIT(ecrd);
6101 TPACPI_ACPIHANDLE_INIT(ecwr);
6103 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6106 "unable to create proc dir " TPACPI_PROC_DIR);
6107 thinkpad_acpi_module_exit();
6110 proc_dir->owner = THIS_MODULE;
6112 ret = platform_driver_register(&tpacpi_pdriver);
6115 "unable to register main platform driver\n");
6116 thinkpad_acpi_module_exit();
6119 tp_features.platform_drv_registered = 1;
6121 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6124 "unable to register hwmon platform driver\n");
6125 thinkpad_acpi_module_exit();
6128 tp_features.sensors_pdrv_registered = 1;
6130 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6132 tp_features.platform_drv_attrs_registered = 1;
6133 ret = tpacpi_create_driver_attributes(
6134 &tpacpi_hwmon_pdriver.driver);
6138 "unable to create sysfs driver attributes\n");
6139 thinkpad_acpi_module_exit();
6142 tp_features.sensors_pdrv_attrs_registered = 1;
6145 /* Device initialization */
6146 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6148 if (IS_ERR(tpacpi_pdev)) {
6149 ret = PTR_ERR(tpacpi_pdev);
6151 printk(TPACPI_ERR "unable to register platform device\n");
6152 thinkpad_acpi_module_exit();
6155 tpacpi_sensors_pdev = platform_device_register_simple(
6156 TPACPI_HWMON_DRVR_NAME,
6158 if (IS_ERR(tpacpi_sensors_pdev)) {
6159 ret = PTR_ERR(tpacpi_sensors_pdev);
6160 tpacpi_sensors_pdev = NULL;
6162 "unable to register hwmon platform device\n");
6163 thinkpad_acpi_module_exit();
6166 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6167 &dev_attr_thinkpad_acpi_pdev_name);
6170 "unable to create sysfs hwmon device attributes\n");
6171 thinkpad_acpi_module_exit();
6174 tp_features.sensors_pdev_attrs_registered = 1;
6175 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6176 if (IS_ERR(tpacpi_hwmon)) {
6177 ret = PTR_ERR(tpacpi_hwmon);
6178 tpacpi_hwmon = NULL;
6179 printk(TPACPI_ERR "unable to register hwmon device\n");
6180 thinkpad_acpi_module_exit();
6183 mutex_init(&tpacpi_inputdev_send_mutex);
6184 tpacpi_inputdev = input_allocate_device();
6185 if (!tpacpi_inputdev) {
6186 printk(TPACPI_ERR "unable to allocate input device\n");
6187 thinkpad_acpi_module_exit();
6190 /* Prepare input device, but don't register */
6191 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6192 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6193 tpacpi_inputdev->id.bustype = BUS_HOST;
6194 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6195 thinkpad_id.vendor :
6197 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6198 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6200 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6201 ret = ibm_init(&ibms_init[i]);
6202 if (ret >= 0 && *ibms_init[i].param)
6203 ret = ibms_init[i].data->write(ibms_init[i].param);
6205 thinkpad_acpi_module_exit();
6209 ret = input_register_device(tpacpi_inputdev);
6211 printk(TPACPI_ERR "unable to register input device\n");
6212 thinkpad_acpi_module_exit();
6215 tp_features.input_device_registered = 1;
6218 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6222 /* Please remove this in year 2009 */
6223 MODULE_ALIAS("ibm_acpi");
6226 * DMI matching for module autoloading
6228 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6229 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6231 * Only models listed in thinkwiki will be supported, so add yours
6232 * if it is not there yet.
6234 #define IBM_BIOS_MODULE_ALIAS(__type) \
6235 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6237 /* Non-ancient thinkpads */
6238 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6239 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6241 /* Ancient thinkpad BIOSes have to be identified by
6242 * BIOS type or model number, and there are far less
6243 * BIOS types than model numbers... */
6244 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6245 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6246 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6248 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6249 MODULE_DESCRIPTION(TPACPI_DESC);
6250 MODULE_VERSION(TPACPI_VERSION);
6251 MODULE_LICENSE("GPL");
6253 module_init(thinkpad_acpi_module_init);
6254 module_exit(thinkpad_acpi_module_exit);