2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define TPACPI_VERSION "0.18"
25 #define TPACPI_SYSFS_VERSION 0x020101
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
226 u32 bright_16levels:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 platform_drv_registered:1;
231 u32 platform_drv_attrs_registered:1;
232 u32 sensors_pdrv_registered:1;
233 u32 sensors_pdrv_attrs_registered:1;
234 u32 sensors_pdev_attrs_registered:1;
235 u32 hotkey_poll_active:1;
238 struct thinkpad_id_data {
239 unsigned int vendor; /* ThinkPad vendor:
240 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
242 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
245 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
250 static struct thinkpad_id_data thinkpad_id;
253 TPACPI_LIFE_INIT = 0,
258 static int experimental;
259 static u32 dbg_level;
261 /****************************************************************************
262 ****************************************************************************
264 * ACPI Helpers and device model
266 ****************************************************************************
267 ****************************************************************************/
269 /*************************************************************************
273 static acpi_handle root_handle;
275 #define TPACPI_HANDLE(object, parent, paths...) \
276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
278 static char *object##_path; \
279 static char *object##_paths[] = { paths }
281 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
290 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
293 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
299 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
305 /*************************************************************************
309 static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
313 struct acpi_object_list params;
314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
337 params.pointer = &in_objs[0];
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
347 /* add more types as needed */
349 printk(TPACPI_ERR "acpi_evalf() called "
350 "with invalid format character '%c'\n", c);
356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
363 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
372 success = status == AE_OK;
374 /* add more types as needed */
376 printk(TPACPI_ERR "acpi_evalf() called "
377 "with invalid format character '%c'\n", res_type);
381 if (!success && !quiet)
382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
383 method, fmt0, status);
388 static int acpi_ec_read(int i, u8 *p)
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
397 if (ec_read(i, p) < 0)
404 static int acpi_ec_write(int i, u8 v)
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
410 if (ec_write(i, v) < 0)
417 static int _sta(acpi_handle handle)
421 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
427 static int issue_thinkpad_cmos_command(int cmos_cmd)
432 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
438 /*************************************************************************
442 #define TPACPI_ACPIHANDLE_INIT(object) \
443 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
444 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
446 static void drv_acpi_handle_init(char *name,
447 acpi_handle *handle, acpi_handle parent,
448 char **paths, int num_paths, char **path)
453 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
456 for (i = 0; i < num_paths; i++) {
457 status = acpi_get_handle(parent, paths[i], handle);
458 if (ACPI_SUCCESS(status)) {
460 dbg_printk(TPACPI_DBG_INIT,
461 "Found ACPI handle %s for %s\n",
467 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
472 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
474 struct ibm_struct *ibm = data;
476 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
479 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
482 ibm->acpi->notify(ibm, event);
485 static int __init setup_acpi_notify(struct ibm_struct *ibm)
492 if (!*ibm->acpi->handle)
495 vdbg_printk(TPACPI_DBG_INIT,
496 "setting up ACPI notify for %s\n", ibm->name);
498 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
500 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
505 acpi_driver_data(ibm->acpi->device) = ibm;
506 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
507 TPACPI_ACPI_EVENT_PREFIX,
510 status = acpi_install_notify_handler(*ibm->acpi->handle,
511 ibm->acpi->type, dispatch_acpi_notify, ibm);
512 if (ACPI_FAILURE(status)) {
513 if (status == AE_ALREADY_EXISTS) {
515 "another device driver is already "
516 "handling %s events\n", ibm->name);
519 "acpi_install_notify_handler(%s) failed: %d\n",
524 ibm->flags.acpi_notify_installed = 1;
528 static int __init tpacpi_device_add(struct acpi_device *device)
533 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
537 dbg_printk(TPACPI_DBG_INIT,
538 "registering %s as an ACPI driver\n", ibm->name);
542 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
543 if (!ibm->acpi->driver) {
544 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
548 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
549 ibm->acpi->driver->ids = ibm->acpi->hid;
551 ibm->acpi->driver->ops.add = &tpacpi_device_add;
553 rc = acpi_bus_register_driver(ibm->acpi->driver);
555 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
557 kfree(ibm->acpi->driver);
558 ibm->acpi->driver = NULL;
560 ibm->flags.acpi_driver_registered = 1;
566 /****************************************************************************
567 ****************************************************************************
571 ****************************************************************************
572 ****************************************************************************/
574 static int dispatch_procfs_read(char *page, char **start, off_t off,
575 int count, int *eof, void *data)
577 struct ibm_struct *ibm = data;
580 if (!ibm || !ibm->read)
583 len = ibm->read(page);
587 if (len <= off + count)
599 static int dispatch_procfs_write(struct file *file,
600 const char __user *userbuf,
601 unsigned long count, void *data)
603 struct ibm_struct *ibm = data;
607 if (!ibm || !ibm->write)
610 kernbuf = kmalloc(count + 2, GFP_KERNEL);
614 if (copy_from_user(kernbuf, userbuf, count)) {
620 strcat(kernbuf, ",");
621 ret = ibm->write(kernbuf);
630 static char *next_cmd(char **cmds)
635 while ((end = strchr(start, ',')) && end == start)
647 /****************************************************************************
648 ****************************************************************************
650 * Device model: input, hwmon and platform
652 ****************************************************************************
653 ****************************************************************************/
655 static struct platform_device *tpacpi_pdev;
656 static struct platform_device *tpacpi_sensors_pdev;
657 static struct device *tpacpi_hwmon;
658 static struct input_dev *tpacpi_inputdev;
659 static struct mutex tpacpi_inputdev_send_mutex;
660 static LIST_HEAD(tpacpi_all_drivers);
662 static int tpacpi_suspend_handler(struct platform_device *pdev,
665 struct ibm_struct *ibm, *itmp;
667 list_for_each_entry_safe(ibm, itmp,
671 (ibm->suspend)(state);
677 static int tpacpi_resume_handler(struct platform_device *pdev)
679 struct ibm_struct *ibm, *itmp;
681 list_for_each_entry_safe(ibm, itmp,
691 static struct platform_driver tpacpi_pdriver = {
693 .name = TPACPI_DRVR_NAME,
694 .owner = THIS_MODULE,
696 .suspend = tpacpi_suspend_handler,
697 .resume = tpacpi_resume_handler,
700 static struct platform_driver tpacpi_hwmon_pdriver = {
702 .name = TPACPI_HWMON_DRVR_NAME,
703 .owner = THIS_MODULE,
707 /*************************************************************************
708 * sysfs support helpers
711 struct attribute_set {
712 unsigned int members, max_members;
713 struct attribute_group group;
716 struct attribute_set_obj {
717 struct attribute_set s;
719 } __attribute__((packed));
721 static struct attribute_set *create_attr_set(unsigned int max_members,
724 struct attribute_set_obj *sobj;
726 if (max_members == 0)
729 /* Allocates space for implicit NULL at the end too */
730 sobj = kzalloc(sizeof(struct attribute_set_obj) +
731 max_members * sizeof(struct attribute *),
735 sobj->s.max_members = max_members;
736 sobj->s.group.attrs = &sobj->a;
737 sobj->s.group.name = name;
742 #define destroy_attr_set(_set) \
745 /* not multi-threaded safe, use it in a single thread per set */
746 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
751 if (s->members >= s->max_members)
754 s->group.attrs[s->members] = attr;
760 static int add_many_to_attr_set(struct attribute_set *s,
761 struct attribute **attr,
766 for (i = 0; i < count; i++) {
767 res = add_to_attr_set(s, attr[i]);
775 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
777 sysfs_remove_group(kobj, &s->group);
781 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
782 sysfs_create_group(_kobj, &_attr_set->group)
784 static int parse_strtoul(const char *buf,
785 unsigned long max, unsigned long *value)
789 while (*buf && isspace(*buf))
791 *value = simple_strtoul(buf, &endp, 0);
792 while (*endp && isspace(*endp))
794 if (*endp || *value > max)
800 /*************************************************************************
801 * thinkpad-acpi driver attributes
804 /* interface_version --------------------------------------------------- */
805 static ssize_t tpacpi_driver_interface_version_show(
806 struct device_driver *drv,
809 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
812 static DRIVER_ATTR(interface_version, S_IRUGO,
813 tpacpi_driver_interface_version_show, NULL);
815 /* debug_level --------------------------------------------------------- */
816 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
819 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
822 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
823 const char *buf, size_t count)
827 if (parse_strtoul(buf, 0xffff, &t))
835 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
836 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
838 /* version ------------------------------------------------------------- */
839 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
842 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
843 TPACPI_DESC, TPACPI_VERSION);
846 static DRIVER_ATTR(version, S_IRUGO,
847 tpacpi_driver_version_show, NULL);
849 /* --------------------------------------------------------------------- */
851 static struct driver_attribute *tpacpi_driver_attributes[] = {
852 &driver_attr_debug_level, &driver_attr_version,
853 &driver_attr_interface_version,
856 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
862 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
863 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
870 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
874 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
875 driver_remove_file(drv, tpacpi_driver_attributes[i]);
878 /****************************************************************************
879 ****************************************************************************
883 ****************************************************************************
884 ****************************************************************************/
886 /*************************************************************************
887 * thinkpad-acpi init subdriver
890 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
892 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
893 printk(TPACPI_INFO "%s\n", TPACPI_URL);
895 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
896 (thinkpad_id.bios_version_str) ?
897 thinkpad_id.bios_version_str : "unknown",
898 (thinkpad_id.ec_version_str) ?
899 thinkpad_id.ec_version_str : "unknown");
901 if (thinkpad_id.vendor && thinkpad_id.model_str)
902 printk(TPACPI_INFO "%s %s\n",
903 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
904 "IBM" : ((thinkpad_id.vendor ==
905 PCI_VENDOR_ID_LENOVO) ?
906 "Lenovo" : "Unknown vendor"),
907 thinkpad_id.model_str);
912 static int thinkpad_acpi_driver_read(char *p)
916 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
917 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
922 static struct ibm_struct thinkpad_acpi_driver_data = {
924 .read = thinkpad_acpi_driver_read,
927 /*************************************************************************
931 enum { /* hot key scan codes (derived from ACPI DSDT) */
932 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
933 TP_ACPI_HOTKEYSCAN_FNF2,
934 TP_ACPI_HOTKEYSCAN_FNF3,
935 TP_ACPI_HOTKEYSCAN_FNF4,
936 TP_ACPI_HOTKEYSCAN_FNF5,
937 TP_ACPI_HOTKEYSCAN_FNF6,
938 TP_ACPI_HOTKEYSCAN_FNF7,
939 TP_ACPI_HOTKEYSCAN_FNF8,
940 TP_ACPI_HOTKEYSCAN_FNF9,
941 TP_ACPI_HOTKEYSCAN_FNF10,
942 TP_ACPI_HOTKEYSCAN_FNF11,
943 TP_ACPI_HOTKEYSCAN_FNF12,
944 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
945 TP_ACPI_HOTKEYSCAN_FNINSERT,
946 TP_ACPI_HOTKEYSCAN_FNDELETE,
947 TP_ACPI_HOTKEYSCAN_FNHOME,
948 TP_ACPI_HOTKEYSCAN_FNEND,
949 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
950 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
951 TP_ACPI_HOTKEYSCAN_FNSPACE,
952 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
953 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
954 TP_ACPI_HOTKEYSCAN_MUTE,
955 TP_ACPI_HOTKEYSCAN_THINKPAD,
958 enum { /* Keys available through NVRAM polling */
959 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
960 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
963 enum { /* Positions of some of the keys in hotkey masks */
964 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
965 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
966 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
967 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
968 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
969 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
970 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
971 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
972 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
973 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
974 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
977 enum { /* NVRAM to ACPI HKEY group map */
978 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
979 TP_ACPI_HKEY_ZOOM_MASK |
980 TP_ACPI_HKEY_DISPSWTCH_MASK |
981 TP_ACPI_HKEY_HIBERNATE_MASK,
982 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
983 TP_ACPI_HKEY_BRGHTDWN_MASK,
984 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
985 TP_ACPI_HKEY_VOLDWN_MASK |
986 TP_ACPI_HKEY_MUTE_MASK,
989 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
990 struct tp_nvram_state {
991 u16 thinkpad_toggle:1;
993 u16 display_toggle:1;
994 u16 thinklight_toggle:1;
995 u16 hibernate_toggle:1;
996 u16 displayexp_toggle:1;
998 u16 brightness_toggle:1;
1002 u8 brightness_level;
1006 static struct task_struct *tpacpi_hotkey_task;
1007 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1008 static int hotkey_poll_freq = 10; /* Hz */
1009 static struct mutex hotkey_thread_mutex;
1010 static struct mutex hotkey_thread_data_mutex;
1011 static unsigned int hotkey_config_change;
1013 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1015 #define hotkey_source_mask 0U
1017 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1019 static struct mutex hotkey_mutex;
1021 static enum { /* Reasons for waking up */
1022 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1023 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1024 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1025 } hotkey_wakeup_reason;
1027 static int hotkey_autosleep_ack;
1029 static int hotkey_orig_status;
1030 static u32 hotkey_orig_mask;
1031 static u32 hotkey_all_mask;
1032 static u32 hotkey_reserved_mask;
1033 static u32 hotkey_mask;
1035 static unsigned int hotkey_report_mode;
1037 static u16 *hotkey_keycode_map;
1039 static struct attribute_set *hotkey_dev_attributes;
1041 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1042 #define HOTKEY_CONFIG_CRITICAL_START \
1044 mutex_lock(&hotkey_thread_data_mutex); \
1045 hotkey_config_change++; \
1047 #define HOTKEY_CONFIG_CRITICAL_END \
1048 mutex_unlock(&hotkey_thread_data_mutex);
1050 #define HOTKEY_CONFIG_CRITICAL_START
1051 #define HOTKEY_CONFIG_CRITICAL_END
1052 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1054 static int hotkey_get_wlsw(int *status)
1056 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1062 * Call with hotkey_mutex held
1064 static int hotkey_mask_get(void)
1068 if (tp_features.hotkey_mask) {
1069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1072 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1078 * Call with hotkey_mutex held
1080 static int hotkey_mask_set(u32 mask)
1085 if (tp_features.hotkey_mask) {
1086 HOTKEY_CONFIG_CRITICAL_START
1087 for (i = 0; i < 32; i++) {
1089 /* enable in firmware mask only keys not in NVRAM
1090 * mode, but enable the key in the cached hotkey_mask
1091 * regardless of mode, or the key will end up
1092 * disabled by hotkey_mask_get() */
1093 if (!acpi_evalf(hkey_handle,
1094 NULL, "MHKM", "vdd", i + 1,
1095 !!((mask & ~hotkey_source_mask) & m))) {
1099 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1102 HOTKEY_CONFIG_CRITICAL_END
1104 /* hotkey_mask_get must be called unconditionally below */
1105 if (!hotkey_mask_get() && !rc &&
1106 (hotkey_mask & ~hotkey_source_mask) !=
1107 (mask & ~hotkey_source_mask)) {
1108 printk(TPACPI_NOTICE
1109 "requested hot key mask 0x%08x, but "
1110 "firmware forced it to 0x%08x\n",
1114 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1115 HOTKEY_CONFIG_CRITICAL_START
1116 hotkey_mask = mask & hotkey_source_mask;
1117 HOTKEY_CONFIG_CRITICAL_END
1119 if (hotkey_mask != mask) {
1120 printk(TPACPI_NOTICE
1121 "requested hot key mask 0x%08x, "
1122 "forced to 0x%08x (NVRAM poll mask is "
1123 "0x%08x): no firmware mask support\n",
1124 mask, hotkey_mask, hotkey_source_mask);
1129 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1135 static int hotkey_status_get(int *status)
1137 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1143 static int hotkey_status_set(int status)
1145 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1151 static void tpacpi_input_send_radiosw(void)
1155 mutex_lock(&tpacpi_inputdev_send_mutex);
1157 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1158 input_report_switch(tpacpi_inputdev,
1160 input_sync(tpacpi_inputdev);
1163 mutex_unlock(&tpacpi_inputdev_send_mutex);
1166 static void tpacpi_input_send_key(unsigned int scancode)
1168 unsigned int keycode;
1170 keycode = hotkey_keycode_map[scancode];
1172 if (keycode != KEY_RESERVED) {
1173 mutex_lock(&tpacpi_inputdev_send_mutex);
1175 input_report_key(tpacpi_inputdev, keycode, 1);
1176 if (keycode == KEY_UNKNOWN)
1177 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1179 input_sync(tpacpi_inputdev);
1181 input_report_key(tpacpi_inputdev, keycode, 0);
1182 if (keycode == KEY_UNKNOWN)
1183 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1185 input_sync(tpacpi_inputdev);
1187 mutex_unlock(&tpacpi_inputdev_send_mutex);
1191 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1192 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1194 static void tpacpi_hotkey_send_key(unsigned int scancode)
1196 tpacpi_input_send_key(scancode);
1197 if (hotkey_report_mode < 2) {
1198 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1199 0x80, 0x1001 + scancode);
1203 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1207 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1208 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1209 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1210 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1211 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1212 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1214 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1215 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1216 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1218 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1219 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1220 n->displayexp_toggle =
1221 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1223 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1224 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1225 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1226 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1227 n->brightness_toggle =
1228 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1230 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1231 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1232 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1233 >> TP_NVRAM_POS_LEVEL_VOLUME;
1234 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1235 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1239 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1241 if ((mask & (1 << __scancode)) && \
1242 oldn->__member != newn->__member) \
1243 tpacpi_hotkey_send_key(__scancode); \
1246 #define TPACPI_MAY_SEND_KEY(__scancode) \
1247 do { if (mask & (1 << __scancode)) \
1248 tpacpi_hotkey_send_key(__scancode); } while (0)
1250 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1251 struct tp_nvram_state *newn,
1254 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1255 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1257 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1259 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1261 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1264 if (oldn->volume_toggle != newn->volume_toggle) {
1265 if (oldn->mute != newn->mute) {
1266 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1268 if (oldn->volume_level > newn->volume_level) {
1269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1270 } else if (oldn->volume_level < newn->volume_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1272 } else if (oldn->mute == newn->mute) {
1273 /* repeated key presses that didn't change state */
1275 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1276 } else if (newn->volume_level != 0) {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1284 /* handle brightness */
1285 if (oldn->brightness_toggle != newn->brightness_toggle) {
1286 if (oldn->brightness_level < newn->brightness_level) {
1287 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1288 } else if (oldn->brightness_level > newn->brightness_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1291 /* repeated key presses that didn't change state */
1292 if (newn->brightness_level != 0) {
1293 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1301 #undef TPACPI_COMPARE_KEY
1302 #undef TPACPI_MAY_SEND_KEY
1304 static int hotkey_kthread(void *data)
1306 struct tp_nvram_state s[2];
1308 unsigned int si, so;
1310 unsigned int change_detector, must_reset;
1312 mutex_lock(&hotkey_thread_mutex);
1314 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1323 /* Initial state for compares */
1324 mutex_lock(&hotkey_thread_data_mutex);
1325 change_detector = hotkey_config_change;
1326 mask = hotkey_source_mask & hotkey_mask;
1327 mutex_unlock(&hotkey_thread_data_mutex);
1328 hotkey_read_nvram(&s[so], mask);
1330 while (!kthread_should_stop() && hotkey_poll_freq) {
1332 t = 1000/hotkey_poll_freq;
1333 t = msleep_interruptible(t);
1334 if (unlikely(kthread_should_stop()))
1336 must_reset = try_to_freeze();
1337 if (t > 0 && !must_reset)
1340 mutex_lock(&hotkey_thread_data_mutex);
1341 if (must_reset || hotkey_config_change != change_detector) {
1342 /* forget old state on thaw or config change */
1345 change_detector = hotkey_config_change;
1347 mask = hotkey_source_mask & hotkey_mask;
1348 mutex_unlock(&hotkey_thread_data_mutex);
1351 hotkey_read_nvram(&s[si], mask);
1352 if (likely(si != so)) {
1353 hotkey_compare_and_issue_event(&s[so], &s[si],
1363 mutex_unlock(&hotkey_thread_mutex);
1367 static void hotkey_poll_stop_sync(void)
1369 if (tpacpi_hotkey_task) {
1370 if (frozen(tpacpi_hotkey_task) ||
1371 freezing(tpacpi_hotkey_task))
1372 thaw_process(tpacpi_hotkey_task);
1374 kthread_stop(tpacpi_hotkey_task);
1375 tpacpi_hotkey_task = NULL;
1376 mutex_lock(&hotkey_thread_mutex);
1377 /* at this point, the thread did exit */
1378 mutex_unlock(&hotkey_thread_mutex);
1382 /* call with hotkey_mutex held */
1383 static void hotkey_poll_setup(int may_warn)
1385 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1386 hotkey_poll_freq > 0 &&
1387 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1388 if (!tpacpi_hotkey_task) {
1389 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1392 if (IS_ERR(tpacpi_hotkey_task)) {
1393 tpacpi_hotkey_task = NULL;
1395 "could not create kernel thread "
1396 "for hotkey polling\n");
1400 hotkey_poll_stop_sync();
1402 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1403 printk(TPACPI_NOTICE
1404 "hot keys 0x%08x require polling, "
1405 "which is currently disabled\n",
1406 hotkey_source_mask);
1411 static void hotkey_poll_setup_safe(int may_warn)
1413 mutex_lock(&hotkey_mutex);
1414 hotkey_poll_setup(may_warn);
1415 mutex_unlock(&hotkey_mutex);
1418 static int hotkey_inputdev_open(struct input_dev *dev)
1420 switch (tpacpi_lifecycle) {
1421 case TPACPI_LIFE_INIT:
1423 * hotkey_init will call hotkey_poll_setup_safe
1424 * at the appropriate moment
1427 case TPACPI_LIFE_EXITING:
1429 case TPACPI_LIFE_RUNNING:
1430 hotkey_poll_setup_safe(0);
1434 /* Should only happen if tpacpi_lifecycle is corrupt */
1439 static void hotkey_inputdev_close(struct input_dev *dev)
1441 /* disable hotkey polling when possible */
1442 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1443 hotkey_poll_setup_safe(0);
1445 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1447 /* sysfs hotkey enable ------------------------------------------------- */
1448 static ssize_t hotkey_enable_show(struct device *dev,
1449 struct device_attribute *attr,
1454 res = hotkey_status_get(&status);
1458 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1461 static ssize_t hotkey_enable_store(struct device *dev,
1462 struct device_attribute *attr,
1463 const char *buf, size_t count)
1468 if (parse_strtoul(buf, 1, &t))
1471 res = hotkey_status_set(t);
1473 return (res) ? res : count;
1476 static struct device_attribute dev_attr_hotkey_enable =
1477 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1478 hotkey_enable_show, hotkey_enable_store);
1480 /* sysfs hotkey mask --------------------------------------------------- */
1481 static ssize_t hotkey_mask_show(struct device *dev,
1482 struct device_attribute *attr,
1487 if (mutex_lock_interruptible(&hotkey_mutex))
1488 return -ERESTARTSYS;
1489 res = hotkey_mask_get();
1490 mutex_unlock(&hotkey_mutex);
1493 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1496 static ssize_t hotkey_mask_store(struct device *dev,
1497 struct device_attribute *attr,
1498 const char *buf, size_t count)
1503 if (parse_strtoul(buf, 0xffffffffUL, &t))
1506 if (mutex_lock_interruptible(&hotkey_mutex))
1507 return -ERESTARTSYS;
1509 res = hotkey_mask_set(t);
1511 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1512 hotkey_poll_setup(1);
1515 mutex_unlock(&hotkey_mutex);
1517 return (res) ? res : count;
1520 static struct device_attribute dev_attr_hotkey_mask =
1521 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1522 hotkey_mask_show, hotkey_mask_store);
1524 /* sysfs hotkey bios_enabled ------------------------------------------- */
1525 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1526 struct device_attribute *attr,
1529 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1532 static struct device_attribute dev_attr_hotkey_bios_enabled =
1533 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1535 /* sysfs hotkey bios_mask ---------------------------------------------- */
1536 static ssize_t hotkey_bios_mask_show(struct device *dev,
1537 struct device_attribute *attr,
1540 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1543 static struct device_attribute dev_attr_hotkey_bios_mask =
1544 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1546 /* sysfs hotkey all_mask ----------------------------------------------- */
1547 static ssize_t hotkey_all_mask_show(struct device *dev,
1548 struct device_attribute *attr,
1551 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1552 hotkey_all_mask | hotkey_source_mask);
1555 static struct device_attribute dev_attr_hotkey_all_mask =
1556 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1558 /* sysfs hotkey recommended_mask --------------------------------------- */
1559 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1560 struct device_attribute *attr,
1563 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1564 (hotkey_all_mask | hotkey_source_mask)
1565 & ~hotkey_reserved_mask);
1568 static struct device_attribute dev_attr_hotkey_recommended_mask =
1569 __ATTR(hotkey_recommended_mask, S_IRUGO,
1570 hotkey_recommended_mask_show, NULL);
1572 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1574 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1575 static ssize_t hotkey_source_mask_show(struct device *dev,
1576 struct device_attribute *attr,
1579 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1582 static ssize_t hotkey_source_mask_store(struct device *dev,
1583 struct device_attribute *attr,
1584 const char *buf, size_t count)
1588 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1589 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1592 if (mutex_lock_interruptible(&hotkey_mutex))
1593 return -ERESTARTSYS;
1595 HOTKEY_CONFIG_CRITICAL_START
1596 hotkey_source_mask = t;
1597 HOTKEY_CONFIG_CRITICAL_END
1599 hotkey_poll_setup(1);
1601 mutex_unlock(&hotkey_mutex);
1606 static struct device_attribute dev_attr_hotkey_source_mask =
1607 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1608 hotkey_source_mask_show, hotkey_source_mask_store);
1610 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1611 static ssize_t hotkey_poll_freq_show(struct device *dev,
1612 struct device_attribute *attr,
1615 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1618 static ssize_t hotkey_poll_freq_store(struct device *dev,
1619 struct device_attribute *attr,
1620 const char *buf, size_t count)
1624 if (parse_strtoul(buf, 25, &t))
1627 if (mutex_lock_interruptible(&hotkey_mutex))
1628 return -ERESTARTSYS;
1630 hotkey_poll_freq = t;
1632 hotkey_poll_setup(1);
1633 mutex_unlock(&hotkey_mutex);
1638 static struct device_attribute dev_attr_hotkey_poll_freq =
1639 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1640 hotkey_poll_freq_show, hotkey_poll_freq_store);
1642 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1644 /* sysfs hotkey radio_sw ----------------------------------------------- */
1645 static ssize_t hotkey_radio_sw_show(struct device *dev,
1646 struct device_attribute *attr,
1650 res = hotkey_get_wlsw(&s);
1654 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1657 static struct device_attribute dev_attr_hotkey_radio_sw =
1658 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1660 /* sysfs hotkey report_mode -------------------------------------------- */
1661 static ssize_t hotkey_report_mode_show(struct device *dev,
1662 struct device_attribute *attr,
1665 return snprintf(buf, PAGE_SIZE, "%d\n",
1666 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1669 static struct device_attribute dev_attr_hotkey_report_mode =
1670 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1672 /* sysfs wakeup reason ------------------------------------------------- */
1673 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1674 struct device_attribute *attr,
1677 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1680 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1681 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1683 /* sysfs wakeup hotunplug_complete ------------------------------------- */
1684 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1685 struct device_attribute *attr,
1688 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1691 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1692 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1693 hotkey_wakeup_hotunplug_complete_show, NULL);
1695 /* --------------------------------------------------------------------- */
1697 static struct attribute *hotkey_attributes[] __initdata = {
1698 &dev_attr_hotkey_enable.attr,
1699 &dev_attr_hotkey_bios_enabled.attr,
1700 &dev_attr_hotkey_report_mode.attr,
1701 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1702 &dev_attr_hotkey_mask.attr,
1703 &dev_attr_hotkey_all_mask.attr,
1704 &dev_attr_hotkey_recommended_mask.attr,
1705 &dev_attr_hotkey_source_mask.attr,
1706 &dev_attr_hotkey_poll_freq.attr,
1710 static struct attribute *hotkey_mask_attributes[] __initdata = {
1711 &dev_attr_hotkey_bios_mask.attr,
1712 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1713 &dev_attr_hotkey_mask.attr,
1714 &dev_attr_hotkey_all_mask.attr,
1715 &dev_attr_hotkey_recommended_mask.attr,
1717 &dev_attr_hotkey_wakeup_reason.attr,
1718 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1721 static int __init hotkey_init(struct ibm_init_struct *iibm)
1723 /* Requirements for changing the default keymaps:
1725 * 1. Many of the keys are mapped to KEY_RESERVED for very
1726 * good reasons. Do not change them unless you have deep
1727 * knowledge on the IBM and Lenovo ThinkPad firmware for
1728 * the various ThinkPad models. The driver behaves
1729 * differently for KEY_RESERVED: such keys have their
1730 * hot key mask *unset* in mask_recommended, and also
1731 * in the initial hot key mask programmed into the
1732 * firmware at driver load time, which means the firm-
1733 * ware may react very differently if you change them to
1736 * 2. You must be subscribed to the linux-thinkpad and
1737 * ibm-acpi-devel mailing lists, and you should read the
1738 * list archives since 2007 if you want to change the
1739 * keymaps. This requirement exists so that you will
1740 * know the past history of problems with the thinkpad-
1741 * acpi driver keymaps, and also that you will be
1742 * listening to any bug reports;
1744 * 3. Do not send thinkpad-acpi specific patches directly to
1745 * for merging, *ever*. Send them to the linux-acpi
1746 * mailinglist for comments. Merging is to be done only
1747 * through acpi-test and the ACPI maintainer.
1749 * If the above is too much to ask, don't change the keymap.
1750 * Ask the thinkpad-acpi maintainer to do it, instead.
1752 static u16 ibm_keycode_map[] __initdata = {
1753 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1754 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1755 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1756 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1758 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1759 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1760 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1761 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1763 /* brightness: firmware always reacts to them, unless
1764 * X.org did some tricks in the radeon BIOS scratch
1765 * registers of *some* models */
1766 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1767 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1769 /* Thinklight: firmware always react to it */
1770 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1772 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1773 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1775 /* Volume: firmware always react to it and reprograms
1776 * the built-in *extra* mixer. Never map it to control
1777 * another mixer by default. */
1778 KEY_RESERVED, /* 0x14: VOLUME UP */
1779 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1780 KEY_RESERVED, /* 0x16: MUTE */
1782 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1784 /* (assignments unknown, please report if found) */
1785 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1786 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1788 static u16 lenovo_keycode_map[] __initdata = {
1789 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1790 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1791 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1792 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1794 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1795 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1796 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1797 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1799 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1800 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1802 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1804 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1805 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1807 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1808 * react to it and reprograms the built-in *extra* mixer.
1809 * Never map it to control another mixer by default.
1811 * T60?, T61, R60?, R61: firmware and EC tries to send
1812 * these over the regular keyboard, so these are no-ops,
1813 * but there are still weird bugs re. MUTE, so do not
1814 * change unless you get test reports from all Lenovo
1815 * models. May cause the BIOS to interfere with the
1818 KEY_RESERVED, /* 0x14: VOLUME UP */
1819 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1820 KEY_RESERVED, /* 0x16: MUTE */
1822 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1824 /* (assignments unknown, please report if found) */
1825 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1826 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1829 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1830 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1831 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1837 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1839 BUG_ON(!tpacpi_inputdev);
1840 BUG_ON(tpacpi_inputdev->open != NULL ||
1841 tpacpi_inputdev->close != NULL);
1843 TPACPI_ACPIHANDLE_INIT(hkey);
1844 mutex_init(&hotkey_mutex);
1846 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1847 mutex_init(&hotkey_thread_mutex);
1848 mutex_init(&hotkey_thread_data_mutex);
1851 /* hotkey not supported on 570 */
1852 tp_features.hotkey = hkey_handle != NULL;
1854 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1855 str_supported(tp_features.hotkey));
1857 if (tp_features.hotkey) {
1858 hotkey_dev_attributes = create_attr_set(12, NULL);
1859 if (!hotkey_dev_attributes)
1861 res = add_many_to_attr_set(hotkey_dev_attributes,
1863 ARRAY_SIZE(hotkey_attributes));
1867 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1868 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1869 for HKEY interface version 0x100 */
1870 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1871 if ((hkeyv >> 8) != 1) {
1872 printk(TPACPI_ERR "unknown version of the "
1873 "HKEY interface: 0x%x\n", hkeyv);
1874 printk(TPACPI_ERR "please report this to %s\n",
1878 * MHKV 0x100 in A31, R40, R40e,
1879 * T4x, X31, and later
1881 tp_features.hotkey_mask = 1;
1885 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1886 str_supported(tp_features.hotkey_mask));
1888 if (tp_features.hotkey_mask) {
1889 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1892 "missing MHKA handler, "
1893 "please report this to %s\n",
1895 /* FN+F12, FN+F4, FN+F3 */
1896 hotkey_all_mask = 0x080cU;
1900 /* hotkey_source_mask *must* be zero for
1901 * the first hotkey_mask_get */
1902 res = hotkey_status_get(&hotkey_orig_status);
1903 if (!res && tp_features.hotkey_mask) {
1904 res = hotkey_mask_get();
1905 hotkey_orig_mask = hotkey_mask;
1907 res = add_many_to_attr_set(
1908 hotkey_dev_attributes,
1909 hotkey_mask_attributes,
1910 ARRAY_SIZE(hotkey_mask_attributes));
1914 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 if (tp_features.hotkey_mask) {
1916 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1919 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1922 vdbg_printk(TPACPI_DBG_INIT,
1923 "hotkey source mask 0x%08x, polling freq %d\n",
1924 hotkey_source_mask, hotkey_poll_freq);
1927 /* Not all thinkpads have a hardware radio switch */
1928 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1929 tp_features.hotkey_wlsw = 1;
1931 "radio switch found; radios are %s\n",
1932 enabled(status, 0));
1933 res = add_to_attr_set(hotkey_dev_attributes,
1934 &dev_attr_hotkey_radio_sw.attr);
1938 res = register_attr_set_with_sysfs(
1939 hotkey_dev_attributes,
1940 &tpacpi_pdev->dev.kobj);
1944 /* Set up key map */
1946 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1948 if (!hotkey_keycode_map) {
1950 "failed to allocate memory for key map\n");
1954 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1955 dbg_printk(TPACPI_DBG_INIT,
1956 "using Lenovo default hot key map\n");
1957 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1958 TPACPI_HOTKEY_MAP_SIZE);
1960 dbg_printk(TPACPI_DBG_INIT,
1961 "using IBM default hot key map\n");
1962 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1963 TPACPI_HOTKEY_MAP_SIZE);
1966 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1967 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1968 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1969 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1970 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1971 tpacpi_inputdev->keycode = hotkey_keycode_map;
1972 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1973 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1974 set_bit(hotkey_keycode_map[i],
1975 tpacpi_inputdev->keybit);
1977 if (i < sizeof(hotkey_reserved_mask)*8)
1978 hotkey_reserved_mask |= 1 << i;
1982 if (tp_features.hotkey_wlsw) {
1983 set_bit(EV_SW, tpacpi_inputdev->evbit);
1984 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1987 dbg_printk(TPACPI_DBG_INIT,
1988 "enabling hot key handling\n");
1989 res = hotkey_status_set(1);
1992 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1993 & ~hotkey_reserved_mask)
1994 | hotkey_orig_mask);
1995 if (res < 0 && res != -ENXIO)
1998 dbg_printk(TPACPI_DBG_INIT,
1999 "legacy hot key reporting over procfs %s\n",
2000 (hotkey_report_mode < 2) ?
2001 "enabled" : "disabled");
2003 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2004 tpacpi_inputdev->open = &hotkey_inputdev_open;
2005 tpacpi_inputdev->close = &hotkey_inputdev_close;
2007 hotkey_poll_setup_safe(1);
2011 return (tp_features.hotkey)? 0 : 1;
2014 static void hotkey_exit(void)
2016 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2017 hotkey_poll_stop_sync();
2020 if (tp_features.hotkey) {
2021 dbg_printk(TPACPI_DBG_EXIT,
2022 "restoring original hot key mask\n");
2023 /* no short-circuit boolean operator below! */
2024 if ((hotkey_mask_set(hotkey_orig_mask) |
2025 hotkey_status_set(hotkey_orig_status)) != 0)
2027 "failed to restore hot key mask "
2028 "to BIOS defaults\n");
2031 if (hotkey_dev_attributes) {
2032 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2033 hotkey_dev_attributes = NULL;
2037 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2040 unsigned int scancode;
2045 if (event != 0x80) {
2047 "unknown HKEY notification event %d\n", event);
2048 /* forward it to userspace, maybe it knows how to handle it */
2049 acpi_bus_generate_netlink_event(
2050 ibm->acpi->device->pnp.device_class,
2051 ibm->acpi->device->dev.bus_id,
2057 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2058 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2071 switch (hkey >> 12) {
2073 /* 0x1000-0x1FFF: key presses */
2074 scancode = hkey & 0xfff;
2075 if (scancode > 0 && scancode < 0x21) {
2077 if (!(hotkey_source_mask & (1 << scancode))) {
2078 tpacpi_input_send_key(scancode);
2090 case 0x2304: /* suspend, undock */
2091 case 0x2404: /* hibernation, undock */
2092 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2095 case 0x2305: /* suspend, bay eject */
2096 case 0x2405: /* hibernation, bay eject */
2097 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2103 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2105 "woke up due to a hot-unplug "
2110 /* bay-related wakeups */
2111 if (hkey == 0x3003) {
2112 hotkey_autosleep_ack = 1;
2120 /* dock-related wakeups */
2121 if (hkey == 0x4003) {
2122 hotkey_autosleep_ack = 1;
2130 /* 0x5000-0x5FFF: On screen display helpers */
2133 /* Lenovo Vista BIOS: brightness changed */
2137 /* LID switch events. Do not propagate */
2145 /* 0x7000-0x7FFF: misc */
2146 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2147 tpacpi_input_send_radiosw();
2151 /* fallthrough to default */
2156 printk(TPACPI_NOTICE
2157 "unhandled HKEY event 0x%04x\n", hkey);
2161 if (!ignore_acpi_ev &&
2162 (send_acpi_ev || hotkey_report_mode < 2)) {
2163 acpi_bus_generate_proc_event(ibm->acpi->device,
2167 /* netlink events */
2168 if (!ignore_acpi_ev && send_acpi_ev) {
2169 acpi_bus_generate_netlink_event(
2170 ibm->acpi->device->pnp.device_class,
2171 ibm->acpi->device->dev.bus_id,
2177 static void hotkey_suspend(pm_message_t state)
2179 /* Do these on suspend, we get the events on early resume! */
2180 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2181 hotkey_autosleep_ack = 0;
2184 static void hotkey_resume(void)
2186 if (hotkey_mask_get())
2188 "error while trying to read hot key mask "
2190 tpacpi_input_send_radiosw();
2191 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2192 hotkey_poll_setup_safe(0);
2196 /* procfs -------------------------------------------------------------- */
2197 static int hotkey_read(char *p)
2202 if (!tp_features.hotkey) {
2203 len += sprintf(p + len, "status:\t\tnot supported\n");
2207 if (mutex_lock_interruptible(&hotkey_mutex))
2208 return -ERESTARTSYS;
2209 res = hotkey_status_get(&status);
2211 res = hotkey_mask_get();
2212 mutex_unlock(&hotkey_mutex);
2216 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2217 if (tp_features.hotkey_mask) {
2218 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2219 len += sprintf(p + len,
2220 "commands:\tenable, disable, reset, <mask>\n");
2222 len += sprintf(p + len, "mask:\t\tnot supported\n");
2223 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2229 static int hotkey_write(char *buf)
2235 if (!tp_features.hotkey)
2238 if (mutex_lock_interruptible(&hotkey_mutex))
2239 return -ERESTARTSYS;
2245 while ((cmd = next_cmd(&buf))) {
2246 if (strlencmp(cmd, "enable") == 0) {
2248 } else if (strlencmp(cmd, "disable") == 0) {
2250 } else if (strlencmp(cmd, "reset") == 0) {
2251 status = hotkey_orig_status;
2252 mask = hotkey_orig_mask;
2253 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2255 } else if (sscanf(cmd, "%x", &mask) == 1) {
2263 res = hotkey_status_set(status);
2265 if (!res && mask != hotkey_mask)
2266 res = hotkey_mask_set(mask);
2269 mutex_unlock(&hotkey_mutex);
2273 static const struct acpi_device_id ibm_htk_device_ids[] = {
2274 {TPACPI_ACPI_HKEY_HID, 0},
2278 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2279 .hid = ibm_htk_device_ids,
2280 .notify = hotkey_notify,
2281 .handle = &hkey_handle,
2282 .type = ACPI_DEVICE_NOTIFY,
2285 static struct ibm_struct hotkey_driver_data = {
2287 .read = hotkey_read,
2288 .write = hotkey_write,
2289 .exit = hotkey_exit,
2290 .resume = hotkey_resume,
2291 .suspend = hotkey_suspend,
2292 .acpi = &ibm_hotkey_acpidriver,
2295 /*************************************************************************
2296 * Bluetooth subdriver
2300 /* ACPI GBDC/SBDC bits */
2301 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2302 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2303 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2306 static int bluetooth_get_radiosw(void);
2307 static int bluetooth_set_radiosw(int radio_on);
2309 /* sysfs bluetooth enable ---------------------------------------------- */
2310 static ssize_t bluetooth_enable_show(struct device *dev,
2311 struct device_attribute *attr,
2316 status = bluetooth_get_radiosw();
2320 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2323 static ssize_t bluetooth_enable_store(struct device *dev,
2324 struct device_attribute *attr,
2325 const char *buf, size_t count)
2330 if (parse_strtoul(buf, 1, &t))
2333 res = bluetooth_set_radiosw(t);
2335 return (res) ? res : count;
2338 static struct device_attribute dev_attr_bluetooth_enable =
2339 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2340 bluetooth_enable_show, bluetooth_enable_store);
2342 /* --------------------------------------------------------------------- */
2344 static struct attribute *bluetooth_attributes[] = {
2345 &dev_attr_bluetooth_enable.attr,
2349 static const struct attribute_group bluetooth_attr_group = {
2350 .attrs = bluetooth_attributes,
2353 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2358 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2360 TPACPI_ACPIHANDLE_INIT(hkey);
2362 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2363 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2364 tp_features.bluetooth = hkey_handle &&
2365 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2367 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2368 str_supported(tp_features.bluetooth),
2371 if (tp_features.bluetooth) {
2372 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2373 /* no bluetooth hardware present in system */
2374 tp_features.bluetooth = 0;
2375 dbg_printk(TPACPI_DBG_INIT,
2376 "bluetooth hardware not installed\n");
2378 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2379 &bluetooth_attr_group);
2385 return (tp_features.bluetooth)? 0 : 1;
2388 static void bluetooth_exit(void)
2390 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2391 &bluetooth_attr_group);
2394 static int bluetooth_get_radiosw(void)
2398 if (!tp_features.bluetooth)
2401 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2404 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2407 static int bluetooth_set_radiosw(int radio_on)
2411 if (!tp_features.bluetooth)
2414 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2417 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2419 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2420 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2426 /* procfs -------------------------------------------------------------- */
2427 static int bluetooth_read(char *p)
2430 int status = bluetooth_get_radiosw();
2432 if (!tp_features.bluetooth)
2433 len += sprintf(p + len, "status:\t\tnot supported\n");
2435 len += sprintf(p + len, "status:\t\t%s\n",
2436 (status)? "enabled" : "disabled");
2437 len += sprintf(p + len, "commands:\tenable, disable\n");
2443 static int bluetooth_write(char *buf)
2447 if (!tp_features.bluetooth)
2450 while ((cmd = next_cmd(&buf))) {
2451 if (strlencmp(cmd, "enable") == 0) {
2452 bluetooth_set_radiosw(1);
2453 } else if (strlencmp(cmd, "disable") == 0) {
2454 bluetooth_set_radiosw(0);
2462 static struct ibm_struct bluetooth_driver_data = {
2463 .name = "bluetooth",
2464 .read = bluetooth_read,
2465 .write = bluetooth_write,
2466 .exit = bluetooth_exit,
2469 /*************************************************************************
2474 /* ACPI GWAN/SWAN bits */
2475 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2476 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2477 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2480 static int wan_get_radiosw(void);
2481 static int wan_set_radiosw(int radio_on);
2483 /* sysfs wan enable ---------------------------------------------------- */
2484 static ssize_t wan_enable_show(struct device *dev,
2485 struct device_attribute *attr,
2490 status = wan_get_radiosw();
2494 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2497 static ssize_t wan_enable_store(struct device *dev,
2498 struct device_attribute *attr,
2499 const char *buf, size_t count)
2504 if (parse_strtoul(buf, 1, &t))
2507 res = wan_set_radiosw(t);
2509 return (res) ? res : count;
2512 static struct device_attribute dev_attr_wan_enable =
2513 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2514 wan_enable_show, wan_enable_store);
2516 /* --------------------------------------------------------------------- */
2518 static struct attribute *wan_attributes[] = {
2519 &dev_attr_wan_enable.attr,
2523 static const struct attribute_group wan_attr_group = {
2524 .attrs = wan_attributes,
2527 static int __init wan_init(struct ibm_init_struct *iibm)
2532 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2534 TPACPI_ACPIHANDLE_INIT(hkey);
2536 tp_features.wan = hkey_handle &&
2537 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2539 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2540 str_supported(tp_features.wan),
2543 if (tp_features.wan) {
2544 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2545 /* no wan hardware present in system */
2546 tp_features.wan = 0;
2547 dbg_printk(TPACPI_DBG_INIT,
2548 "wan hardware not installed\n");
2550 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2557 return (tp_features.wan)? 0 : 1;
2560 static void wan_exit(void)
2562 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2566 static int wan_get_radiosw(void)
2570 if (!tp_features.wan)
2573 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2576 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2579 static int wan_set_radiosw(int radio_on)
2583 if (!tp_features.wan)
2586 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2589 status |= TP_ACPI_WANCARD_RADIOSSW;
2591 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2592 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2598 /* procfs -------------------------------------------------------------- */
2599 static int wan_read(char *p)
2602 int status = wan_get_radiosw();
2604 if (!tp_features.wan)
2605 len += sprintf(p + len, "status:\t\tnot supported\n");
2607 len += sprintf(p + len, "status:\t\t%s\n",
2608 (status)? "enabled" : "disabled");
2609 len += sprintf(p + len, "commands:\tenable, disable\n");
2615 static int wan_write(char *buf)
2619 if (!tp_features.wan)
2622 while ((cmd = next_cmd(&buf))) {
2623 if (strlencmp(cmd, "enable") == 0) {
2625 } else if (strlencmp(cmd, "disable") == 0) {
2634 static struct ibm_struct wan_driver_data = {
2639 .flags.experimental = 1,
2642 /*************************************************************************
2646 enum video_access_mode {
2647 TPACPI_VIDEO_NONE = 0,
2648 TPACPI_VIDEO_570, /* 570 */
2649 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2650 TPACPI_VIDEO_NEW, /* all others */
2653 enum { /* video status flags, based on VIDEO_570 */
2654 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2655 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2656 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2659 enum { /* TPACPI_VIDEO_570 constants */
2660 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2661 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2662 * video_status_flags */
2663 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2664 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2667 static enum video_access_mode video_supported;
2668 static int video_orig_autosw;
2670 static int video_autosw_get(void);
2671 static int video_autosw_set(int enable);
2673 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2674 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2675 "\\_SB.PCI0.VID0", /* 770e */
2676 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2677 "\\_SB.PCI0.AGP.VID", /* all others */
2680 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2682 static int __init video_init(struct ibm_init_struct *iibm)
2686 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2688 TPACPI_ACPIHANDLE_INIT(vid);
2689 TPACPI_ACPIHANDLE_INIT(vid2);
2691 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2692 /* G41, assume IVGA doesn't change */
2693 vid_handle = vid2_handle;
2696 /* video switching not supported on R30, R31 */
2697 video_supported = TPACPI_VIDEO_NONE;
2698 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2700 video_supported = TPACPI_VIDEO_570;
2701 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2702 /* 600e/x, 770e, 770x */
2703 video_supported = TPACPI_VIDEO_770;
2706 video_supported = TPACPI_VIDEO_NEW;
2708 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2709 str_supported(video_supported != TPACPI_VIDEO_NONE),
2712 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2715 static void video_exit(void)
2717 dbg_printk(TPACPI_DBG_EXIT,
2718 "restoring original video autoswitch mode\n");
2719 if (video_autosw_set(video_orig_autosw))
2720 printk(TPACPI_ERR "error while trying to restore original "
2721 "video autoswitch mode\n");
2724 static int video_outputsw_get(void)
2729 switch (video_supported) {
2730 case TPACPI_VIDEO_570:
2731 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2732 TP_ACPI_VIDEO_570_PHSCMD))
2734 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2736 case TPACPI_VIDEO_770:
2737 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2740 status |= TP_ACPI_VIDEO_S_LCD;
2741 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2744 status |= TP_ACPI_VIDEO_S_CRT;
2746 case TPACPI_VIDEO_NEW:
2747 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2748 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2751 status |= TP_ACPI_VIDEO_S_CRT;
2753 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2754 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2757 status |= TP_ACPI_VIDEO_S_LCD;
2758 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2761 status |= TP_ACPI_VIDEO_S_DVI;
2770 static int video_outputsw_set(int status)
2775 switch (video_supported) {
2776 case TPACPI_VIDEO_570:
2777 res = acpi_evalf(NULL, NULL,
2778 "\\_SB.PHS2", "vdd",
2779 TP_ACPI_VIDEO_570_PHS2CMD,
2780 status | TP_ACPI_VIDEO_570_PHS2SET);
2782 case TPACPI_VIDEO_770:
2783 autosw = video_autosw_get();
2787 res = video_autosw_set(1);
2790 res = acpi_evalf(vid_handle, NULL,
2791 "ASWT", "vdd", status * 0x100, 0);
2792 if (!autosw && video_autosw_set(autosw)) {
2794 "video auto-switch left enabled due to error\n");
2798 case TPACPI_VIDEO_NEW:
2799 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2800 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2806 return (res)? 0 : -EIO;
2809 static int video_autosw_get(void)
2813 switch (video_supported) {
2814 case TPACPI_VIDEO_570:
2815 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2818 case TPACPI_VIDEO_770:
2819 case TPACPI_VIDEO_NEW:
2820 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2830 static int video_autosw_set(int enable)
2832 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2837 static int video_outputsw_cycle(void)
2839 int autosw = video_autosw_get();
2845 switch (video_supported) {
2846 case TPACPI_VIDEO_570:
2847 res = video_autosw_set(1);
2850 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2852 case TPACPI_VIDEO_770:
2853 case TPACPI_VIDEO_NEW:
2854 res = video_autosw_set(1);
2857 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2862 if (!autosw && video_autosw_set(autosw)) {
2864 "video auto-switch left enabled due to error\n");
2868 return (res)? 0 : -EIO;
2871 static int video_expand_toggle(void)
2873 switch (video_supported) {
2874 case TPACPI_VIDEO_570:
2875 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2877 case TPACPI_VIDEO_770:
2878 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2880 case TPACPI_VIDEO_NEW:
2881 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2889 static int video_read(char *p)
2894 if (video_supported == TPACPI_VIDEO_NONE) {
2895 len += sprintf(p + len, "status:\t\tnot supported\n");
2899 status = video_outputsw_get();
2903 autosw = video_autosw_get();
2907 len += sprintf(p + len, "status:\t\tsupported\n");
2908 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2909 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2910 if (video_supported == TPACPI_VIDEO_NEW)
2911 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2912 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2913 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2914 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2915 if (video_supported == TPACPI_VIDEO_NEW)
2916 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2917 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2918 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2923 static int video_write(char *buf)
2926 int enable, disable, status;
2929 if (video_supported == TPACPI_VIDEO_NONE)
2935 while ((cmd = next_cmd(&buf))) {
2936 if (strlencmp(cmd, "lcd_enable") == 0) {
2937 enable |= TP_ACPI_VIDEO_S_LCD;
2938 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2939 disable |= TP_ACPI_VIDEO_S_LCD;
2940 } else if (strlencmp(cmd, "crt_enable") == 0) {
2941 enable |= TP_ACPI_VIDEO_S_CRT;
2942 } else if (strlencmp(cmd, "crt_disable") == 0) {
2943 disable |= TP_ACPI_VIDEO_S_CRT;
2944 } else if (video_supported == TPACPI_VIDEO_NEW &&
2945 strlencmp(cmd, "dvi_enable") == 0) {
2946 enable |= TP_ACPI_VIDEO_S_DVI;
2947 } else if (video_supported == TPACPI_VIDEO_NEW &&
2948 strlencmp(cmd, "dvi_disable") == 0) {
2949 disable |= TP_ACPI_VIDEO_S_DVI;
2950 } else if (strlencmp(cmd, "auto_enable") == 0) {
2951 res = video_autosw_set(1);
2954 } else if (strlencmp(cmd, "auto_disable") == 0) {
2955 res = video_autosw_set(0);
2958 } else if (strlencmp(cmd, "video_switch") == 0) {
2959 res = video_outputsw_cycle();
2962 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2963 res = video_expand_toggle();
2970 if (enable || disable) {
2971 status = video_outputsw_get();
2974 res = video_outputsw_set((status & ~disable) | enable);
2982 static struct ibm_struct video_driver_data = {
2985 .write = video_write,
2989 /*************************************************************************
2990 * Light (thinklight) subdriver
2993 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2994 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
2996 static int __init light_init(struct ibm_init_struct *iibm)
2998 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3000 TPACPI_ACPIHANDLE_INIT(ledb);
3001 TPACPI_ACPIHANDLE_INIT(lght);
3002 TPACPI_ACPIHANDLE_INIT(cmos);
3004 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3005 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3007 if (tp_features.light)
3008 /* light status not supported on
3009 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3010 tp_features.light_status =
3011 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3013 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3014 str_supported(tp_features.light));
3016 return (tp_features.light)? 0 : 1;
3019 static int light_read(char *p)
3024 if (!tp_features.light) {
3025 len += sprintf(p + len, "status:\t\tnot supported\n");
3026 } else if (!tp_features.light_status) {
3027 len += sprintf(p + len, "status:\t\tunknown\n");
3028 len += sprintf(p + len, "commands:\ton, off\n");
3030 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3032 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3033 len += sprintf(p + len, "commands:\ton, off\n");
3039 static int light_write(char *buf)
3041 int cmos_cmd, lght_cmd;
3045 if (!tp_features.light)
3048 while ((cmd = next_cmd(&buf))) {
3049 if (strlencmp(cmd, "on") == 0) {
3052 } else if (strlencmp(cmd, "off") == 0) {
3058 success = cmos_handle ?
3059 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3060 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3068 static struct ibm_struct light_driver_data = {
3071 .write = light_write,
3074 /*************************************************************************
3078 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3080 static void dock_notify(struct ibm_struct *ibm, u32 event);
3081 static int dock_read(char *p);
3082 static int dock_write(char *buf);
3084 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3085 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3086 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3087 "\\_SB.PCI.ISA.SLCE", /* 570 */
3088 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3090 /* don't list other alternatives as we install a notify handler on the 570 */
3091 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3093 static const struct acpi_device_id ibm_pci_device_ids[] = {
3094 {PCI_ROOT_HID_STRING, 0},
3098 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3100 .notify = dock_notify,
3101 .handle = &dock_handle,
3102 .type = ACPI_SYSTEM_NOTIFY,
3105 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3106 * We just use it to get notifications of dock hotplug
3107 * in very old thinkpads */
3108 .hid = ibm_pci_device_ids,
3109 .notify = dock_notify,
3110 .handle = &pci_handle,
3111 .type = ACPI_SYSTEM_NOTIFY,
3115 static struct ibm_struct dock_driver_data[2] = {
3119 .write = dock_write,
3120 .acpi = &ibm_dock_acpidriver[0],
3124 .acpi = &ibm_dock_acpidriver[1],
3128 #define dock_docked() (_sta(dock_handle) & 1)
3130 static int __init dock_init(struct ibm_init_struct *iibm)
3132 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3134 TPACPI_ACPIHANDLE_INIT(dock);
3136 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3137 str_supported(dock_handle != NULL));
3139 return (dock_handle)? 0 : 1;
3142 static int __init dock_init2(struct ibm_init_struct *iibm)
3146 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3148 if (dock_driver_data[0].flags.acpi_driver_registered &&
3149 dock_driver_data[0].flags.acpi_notify_installed) {
3150 TPACPI_ACPIHANDLE_INIT(pci);
3151 dock2_needed = (pci_handle != NULL);
3152 vdbg_printk(TPACPI_DBG_INIT,
3153 "dock PCI handler for the TP 570 is %s\n",
3154 str_supported(dock2_needed));
3156 vdbg_printk(TPACPI_DBG_INIT,
3157 "dock subdriver part 2 not required\n");
3161 return (dock2_needed)? 0 : 1;
3164 static void dock_notify(struct ibm_struct *ibm, u32 event)
3166 int docked = dock_docked();
3167 int pci = ibm->acpi->hid && ibm->acpi->device &&
3168 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3171 if (event == 1 && !pci) /* 570 */
3172 data = 1; /* button */
3173 else if (event == 1 && pci) /* 570 */
3174 data = 3; /* dock */
3175 else if (event == 3 && docked)
3176 data = 1; /* button */
3177 else if (event == 3 && !docked)
3178 data = 2; /* undock */
3179 else if (event == 0 && docked)
3180 data = 3; /* dock */
3182 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3183 event, _sta(dock_handle));
3184 data = 0; /* unknown */
3186 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3187 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3188 ibm->acpi->device->dev.bus_id,
3192 static int dock_read(char *p)
3195 int docked = dock_docked();
3198 len += sprintf(p + len, "status:\t\tnot supported\n");
3200 len += sprintf(p + len, "status:\t\tundocked\n");
3202 len += sprintf(p + len, "status:\t\tdocked\n");
3203 len += sprintf(p + len, "commands:\tdock, undock\n");
3209 static int dock_write(char *buf)
3216 while ((cmd = next_cmd(&buf))) {
3217 if (strlencmp(cmd, "undock") == 0) {
3218 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3219 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3221 } else if (strlencmp(cmd, "dock") == 0) {
3222 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3231 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3233 /*************************************************************************
3237 #ifdef CONFIG_THINKPAD_ACPI_BAY
3239 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3240 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3241 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3242 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3243 ); /* A21e, R30, R31 */
3244 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3245 "_EJ0", /* all others */
3246 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3247 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3248 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3250 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3254 static int __init bay_init(struct ibm_init_struct *iibm)
3256 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3258 TPACPI_ACPIHANDLE_INIT(bay);
3260 TPACPI_ACPIHANDLE_INIT(bay_ej);
3261 TPACPI_ACPIHANDLE_INIT(bay2);
3263 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3265 tp_features.bay_status = bay_handle &&
3266 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3267 tp_features.bay_status2 = bay2_handle &&
3268 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3270 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3271 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3272 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3273 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3275 vdbg_printk(TPACPI_DBG_INIT,
3276 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3277 str_supported(tp_features.bay_status),
3278 str_supported(tp_features.bay_eject),
3279 str_supported(tp_features.bay_status2),
3280 str_supported(tp_features.bay_eject2));
3282 return (tp_features.bay_status || tp_features.bay_eject ||
3283 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3286 static void bay_notify(struct ibm_struct *ibm, u32 event)
3288 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3289 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3290 ibm->acpi->device->dev.bus_id,
3294 #define bay_occupied(b) (_sta(b##_handle) & 1)
3296 static int bay_read(char *p)
3299 int occupied = bay_occupied(bay);
3300 int occupied2 = bay_occupied(bay2);
3303 len += sprintf(p + len, "status:\t\t%s\n",
3304 tp_features.bay_status ?
3305 (occupied ? "occupied" : "unoccupied") :
3307 if (tp_features.bay_status2)
3308 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3309 "occupied" : "unoccupied");
3311 eject = tp_features.bay_eject && occupied;
3312 eject2 = tp_features.bay_eject2 && occupied2;
3314 if (eject && eject2)
3315 len += sprintf(p + len, "commands:\teject, eject2\n");
3317 len += sprintf(p + len, "commands:\teject\n");
3319 len += sprintf(p + len, "commands:\teject2\n");
3324 static int bay_write(char *buf)
3328 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3331 while ((cmd = next_cmd(&buf))) {
3332 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3333 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3335 } else if (tp_features.bay_eject2 &&
3336 strlencmp(cmd, "eject2") == 0) {
3337 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3346 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3347 .notify = bay_notify,
3348 .handle = &bay_handle,
3349 .type = ACPI_SYSTEM_NOTIFY,
3352 static struct ibm_struct bay_driver_data = {
3356 .acpi = &ibm_bay_acpidriver,
3359 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3361 /*************************************************************************
3365 /* sysfs cmos_command -------------------------------------------------- */
3366 static ssize_t cmos_command_store(struct device *dev,
3367 struct device_attribute *attr,
3368 const char *buf, size_t count)
3370 unsigned long cmos_cmd;
3373 if (parse_strtoul(buf, 21, &cmos_cmd))
3376 res = issue_thinkpad_cmos_command(cmos_cmd);
3377 return (res)? res : count;
3380 static struct device_attribute dev_attr_cmos_command =
3381 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3383 /* --------------------------------------------------------------------- */
3385 static int __init cmos_init(struct ibm_init_struct *iibm)
3389 vdbg_printk(TPACPI_DBG_INIT,
3390 "initializing cmos commands subdriver\n");
3392 TPACPI_ACPIHANDLE_INIT(cmos);
3394 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3395 str_supported(cmos_handle != NULL));
3397 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3401 return (cmos_handle)? 0 : 1;
3404 static void cmos_exit(void)
3406 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3409 static int cmos_read(char *p)
3413 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3414 R30, R31, T20-22, X20-21 */
3416 len += sprintf(p + len, "status:\t\tnot supported\n");
3418 len += sprintf(p + len, "status:\t\tsupported\n");
3419 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3425 static int cmos_write(char *buf)
3430 while ((cmd = next_cmd(&buf))) {
3431 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3432 cmos_cmd >= 0 && cmos_cmd <= 21) {
3437 res = issue_thinkpad_cmos_command(cmos_cmd);
3445 static struct ibm_struct cmos_driver_data = {
3448 .write = cmos_write,
3452 /*************************************************************************
3456 enum led_access_mode {
3457 TPACPI_LED_NONE = 0,
3458 TPACPI_LED_570, /* 570 */
3459 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3460 TPACPI_LED_NEW, /* all others */
3463 enum { /* For TPACPI_LED_OLD */
3464 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3465 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3466 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3469 static enum led_access_mode led_supported;
3471 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3472 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3473 /* T20-22, X20-21 */
3474 "LED", /* all others */
3477 static int __init led_init(struct ibm_init_struct *iibm)
3479 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3481 TPACPI_ACPIHANDLE_INIT(led);
3484 /* led not supported on R30, R31 */
3485 led_supported = TPACPI_LED_NONE;
3486 else if (strlencmp(led_path, "SLED") == 0)
3488 led_supported = TPACPI_LED_570;
3489 else if (strlencmp(led_path, "SYSL") == 0)
3490 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3491 led_supported = TPACPI_LED_OLD;
3494 led_supported = TPACPI_LED_NEW;
3496 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3497 str_supported(led_supported), led_supported);
3499 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3502 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3504 static int led_read(char *p)
3508 if (!led_supported) {
3509 len += sprintf(p + len, "status:\t\tnot supported\n");
3512 len += sprintf(p + len, "status:\t\tsupported\n");
3514 if (led_supported == TPACPI_LED_570) {
3517 for (i = 0; i < 8; i++) {
3518 if (!acpi_evalf(ec_handle,
3519 &status, "GLED", "dd", 1 << i))
3521 len += sprintf(p + len, "%d:\t\t%s\n",
3522 i, led_status(status));
3526 len += sprintf(p + len, "commands:\t"
3527 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3532 /* off, on, blink */
3533 static const int led_sled_arg1[] = { 0, 1, 3 };
3534 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3535 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3536 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3538 static int led_write(char *buf)
3546 while ((cmd = next_cmd(&buf))) {
3547 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3550 if (strstr(cmd, "off")) {
3552 } else if (strstr(cmd, "on")) {
3554 } else if (strstr(cmd, "blink")) {
3559 if (led_supported == TPACPI_LED_570) {
3562 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3563 led, led_sled_arg1[ind]))
3565 } else if (led_supported == TPACPI_LED_OLD) {
3566 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3568 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3570 ret = ec_write(TPACPI_LED_EC_HLBL,
3571 led * led_exp_hlbl[ind]);
3573 ret = ec_write(TPACPI_LED_EC_HLCL,
3574 led * led_exp_hlcl[ind]);
3579 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3580 led, led_led_arg1[ind]))
3588 static struct ibm_struct led_driver_data = {
3594 /*************************************************************************
3598 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3600 static int __init beep_init(struct ibm_init_struct *iibm)
3602 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3604 TPACPI_ACPIHANDLE_INIT(beep);
3606 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3607 str_supported(beep_handle != NULL));
3609 return (beep_handle)? 0 : 1;
3612 static int beep_read(char *p)
3617 len += sprintf(p + len, "status:\t\tnot supported\n");
3619 len += sprintf(p + len, "status:\t\tsupported\n");
3620 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3626 static int beep_write(char *buf)
3634 while ((cmd = next_cmd(&buf))) {
3635 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3636 beep_cmd >= 0 && beep_cmd <= 17) {
3640 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3647 static struct ibm_struct beep_driver_data = {
3650 .write = beep_write,
3653 /*************************************************************************
3657 enum thermal_access_mode {
3658 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3659 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3660 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3661 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3662 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3665 enum { /* TPACPI_THERMAL_TPEC_* */
3666 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3667 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3668 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3671 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3672 struct ibm_thermal_sensors_struct {
3673 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3676 static enum thermal_access_mode thermal_read_mode;
3678 /* idx is zero-based */
3679 static int thermal_get_sensor(int idx, s32 *value)
3685 t = TP_EC_THERMAL_TMP0;
3687 switch (thermal_read_mode) {
3688 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3689 case TPACPI_THERMAL_TPEC_16:
3690 if (idx >= 8 && idx <= 15) {
3691 t = TP_EC_THERMAL_TMP8;
3696 case TPACPI_THERMAL_TPEC_8:
3698 if (!acpi_ec_read(t + idx, &tmp))
3700 *value = tmp * 1000;
3705 case TPACPI_THERMAL_ACPI_UPDT:
3707 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3708 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3710 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3712 *value = (t - 2732) * 100;
3717 case TPACPI_THERMAL_ACPI_TMP07:
3719 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3720 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3722 if (t > 127 || t < -127)
3723 t = TP_EC_THERMAL_TMP_NA;
3729 case TPACPI_THERMAL_NONE:
3737 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3748 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3751 for (i = 0 ; i < n; i++) {
3752 res = thermal_get_sensor(i, &s->temp[i]);
3760 /* sysfs temp##_input -------------------------------------------------- */
3762 static ssize_t thermal_temp_input_show(struct device *dev,
3763 struct device_attribute *attr,
3766 struct sensor_device_attribute *sensor_attr =
3767 to_sensor_dev_attr(attr);
3768 int idx = sensor_attr->index;
3772 res = thermal_get_sensor(idx, &value);
3775 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3778 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3781 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3782 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3783 thermal_temp_input_show, NULL, _idxB)
3785 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3786 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3787 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3788 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3789 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3790 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3791 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3792 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3793 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3794 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3795 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3796 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3797 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3798 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3799 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3800 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3801 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3804 #define THERMAL_ATTRS(X) \
3805 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3807 static struct attribute *thermal_temp_input_attr[] = {
3827 static const struct attribute_group thermal_temp_input16_group = {
3828 .attrs = thermal_temp_input_attr
3831 static const struct attribute_group thermal_temp_input8_group = {
3832 .attrs = &thermal_temp_input_attr[8]
3835 #undef THERMAL_SENSOR_ATTR_TEMP
3836 #undef THERMAL_ATTRS
3838 /* --------------------------------------------------------------------- */
3840 static int __init thermal_init(struct ibm_init_struct *iibm)
3847 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3849 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3851 if (thinkpad_id.ec_model) {
3853 * Direct EC access mode: sensors at registers
3854 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3855 * non-implemented, thermal sensors return 0x80 when
3860 for (i = 0; i < 8; i++) {
3861 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3867 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3875 /* This is sheer paranoia, but we handle it anyway */
3878 "ThinkPad ACPI EC access misbehaving, "
3879 "falling back to ACPI TMPx access "
3881 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3884 "ThinkPad ACPI EC access misbehaving, "
3885 "disabling thermal sensors access\n");
3886 thermal_read_mode = TPACPI_THERMAL_NONE;
3891 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3893 } else if (acpi_tmp7) {
3894 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3895 /* 600e/x, 770e, 770x */
3896 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3898 /* Standard ACPI TMPx access, max 8 sensors */
3899 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3902 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3903 thermal_read_mode = TPACPI_THERMAL_NONE;
3906 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3907 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3910 switch (thermal_read_mode) {
3911 case TPACPI_THERMAL_TPEC_16:
3912 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3913 &thermal_temp_input16_group);
3917 case TPACPI_THERMAL_TPEC_8:
3918 case TPACPI_THERMAL_ACPI_TMP07:
3919 case TPACPI_THERMAL_ACPI_UPDT:
3920 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3921 &thermal_temp_input8_group);
3925 case TPACPI_THERMAL_NONE:
3933 static void thermal_exit(void)
3935 switch (thermal_read_mode) {
3936 case TPACPI_THERMAL_TPEC_16:
3937 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3938 &thermal_temp_input16_group);
3940 case TPACPI_THERMAL_TPEC_8:
3941 case TPACPI_THERMAL_ACPI_TMP07:
3942 case TPACPI_THERMAL_ACPI_UPDT:
3943 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3944 &thermal_temp_input16_group);
3946 case TPACPI_THERMAL_NONE:
3952 static int thermal_read(char *p)
3956 struct ibm_thermal_sensors_struct t;
3958 n = thermal_get_sensors(&t);
3959 if (unlikely(n < 0))
3962 len += sprintf(p + len, "temperatures:\t");
3965 for (i = 0; i < (n - 1); i++)
3966 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3967 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3969 len += sprintf(p + len, "not supported\n");
3974 static struct ibm_struct thermal_driver_data = {
3976 .read = thermal_read,
3977 .exit = thermal_exit,
3980 /*************************************************************************
3984 static u8 ecdump_regs[256];
3986 static int ecdump_read(char *p)
3992 len += sprintf(p + len, "EC "
3993 " +00 +01 +02 +03 +04 +05 +06 +07"
3994 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3995 for (i = 0; i < 256; i += 16) {
3996 len += sprintf(p + len, "EC 0x%02x:", i);
3997 for (j = 0; j < 16; j++) {
3998 if (!acpi_ec_read(i + j, &v))
4000 if (v != ecdump_regs[i + j])
4001 len += sprintf(p + len, " *%02x", v);
4003 len += sprintf(p + len, " %02x", v);
4004 ecdump_regs[i + j] = v;
4006 len += sprintf(p + len, "\n");
4011 /* These are way too dangerous to advertise openly... */
4013 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4014 " (<offset> is 00-ff, <value> is 00-ff)\n");
4015 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4016 " (<offset> is 00-ff, <value> is 0-255)\n");
4021 static int ecdump_write(char *buf)
4026 while ((cmd = next_cmd(&buf))) {
4027 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4029 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4033 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4034 if (!acpi_ec_write(i, v))
4043 static struct ibm_struct ecdump_driver_data = {
4045 .read = ecdump_read,
4046 .write = ecdump_write,
4047 .flags.experimental = 1,
4050 /*************************************************************************
4051 * Backlight/brightness subdriver
4054 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4056 static struct backlight_device *ibm_backlight_device;
4057 static int brightness_offset = 0x31;
4058 static int brightness_mode;
4059 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4061 static struct mutex brightness_mutex;
4064 * ThinkPads can read brightness from two places: EC 0x31, or
4065 * CMOS NVRAM byte 0x5E, bits 0-3.
4067 static int brightness_get(struct backlight_device *bd)
4069 u8 lec = 0, lcmos = 0, level = 0;
4071 if (brightness_mode & 1) {
4072 if (!acpi_ec_read(brightness_offset, &lec))
4074 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4077 if (brightness_mode & 2) {
4078 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4079 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4080 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4081 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4085 if (brightness_mode == 3 && lec != lcmos) {
4087 "CMOS NVRAM (%u) and EC (%u) do not agree "
4088 "on display brightness level\n",
4089 (unsigned int) lcmos,
4090 (unsigned int) lec);
4097 /* May return EINTR which can always be mapped to ERESTARTSYS */
4098 static int brightness_set(int value)
4100 int cmos_cmd, inc, i, res;
4103 if (value > ((tp_features.bright_16levels)? 15 : 7))
4106 res = mutex_lock_interruptible(&brightness_mutex);
4110 current_value = brightness_get(NULL);
4111 if (current_value < 0) {
4112 res = current_value;
4116 cmos_cmd = value > current_value ?
4117 TP_CMOS_BRIGHTNESS_UP :
4118 TP_CMOS_BRIGHTNESS_DOWN;
4119 inc = (value > current_value)? 1 : -1;
4122 for (i = current_value; i != value; i += inc) {
4123 if ((brightness_mode & 2) &&
4124 issue_thinkpad_cmos_command(cmos_cmd)) {
4128 if ((brightness_mode & 1) &&
4129 !acpi_ec_write(brightness_offset, i + inc)) {
4136 mutex_unlock(&brightness_mutex);
4140 /* sysfs backlight class ----------------------------------------------- */
4142 static int brightness_update_status(struct backlight_device *bd)
4144 /* it is the backlight class's job (caller) to handle
4145 * EINTR and other errors properly */
4146 return brightness_set(
4147 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4148 bd->props.power == FB_BLANK_UNBLANK) ?
4149 bd->props.brightness : 0);
4152 static struct backlight_ops ibm_backlight_data = {
4153 .get_brightness = brightness_get,
4154 .update_status = brightness_update_status,
4157 /* --------------------------------------------------------------------- */
4159 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4161 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4162 union acpi_object *obj;
4165 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4166 obj = (union acpi_object *)buffer.pointer;
4167 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4168 printk(TPACPI_ERR "Unknown BCLL data, "
4169 "please report this to %s\n", TPACPI_MAIL);
4172 rc = obj->package.count;
4178 kfree(buffer.pointer);
4182 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4183 void *context, void **rv)
4185 char name[ACPI_PATH_SEGMENT_LENGTH];
4186 struct acpi_buffer buffer = { sizeof(name), &name };
4188 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4189 !strncmp("BCLL", name, sizeof(name) - 1)) {
4190 if (tpacpi_query_bcll_levels(handle) == 16) {
4192 return AE_CTRL_TERMINATE;
4201 static int __init brightness_check_levels(void)
4204 void *found_node = NULL;
4207 TPACPI_ACPIHANDLE_INIT(vid);
4212 /* Search for a BCLL package with 16 levels */
4213 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4214 brightness_find_bcll, NULL,
4217 return (ACPI_SUCCESS(status) && found_node != NULL);
4220 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4221 void *context, void **rv)
4223 char name[ACPI_PATH_SEGMENT_LENGTH];
4224 struct acpi_buffer buffer = { sizeof(name), &name };
4226 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4227 !strncmp("_BCL", name, sizeof(name) - 1)) {
4229 return AE_CTRL_TERMINATE;
4235 static int __init brightness_check_std_acpi_support(void)
4238 void *found_node = NULL;
4241 TPACPI_ACPIHANDLE_INIT(vid);
4246 /* Search for a _BCL method, but don't execute it */
4247 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4248 brightness_find_bcl, NULL, &found_node);
4250 return (ACPI_SUCCESS(status) && found_node != NULL);
4253 static int __init brightness_init(struct ibm_init_struct *iibm)
4257 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4259 mutex_init(&brightness_mutex);
4261 if (!brightness_enable) {
4262 dbg_printk(TPACPI_DBG_INIT,
4263 "brightness support disabled by "
4264 "module parameter\n");
4266 } else if (brightness_enable > 1) {
4267 if (brightness_check_std_acpi_support()) {
4268 printk(TPACPI_NOTICE
4269 "standard ACPI backlight interface "
4270 "available, not loading native one...\n");
4275 if (!brightness_mode) {
4276 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4277 brightness_mode = 2;
4279 brightness_mode = 3;
4281 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4285 if (brightness_mode > 3)
4288 tp_features.bright_16levels =
4289 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4290 brightness_check_levels();
4292 b = brightness_get(NULL);
4296 if (tp_features.bright_16levels)
4298 "detected a 16-level brightness capable ThinkPad\n");
4300 ibm_backlight_device = backlight_device_register(
4301 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4302 &ibm_backlight_data);
4303 if (IS_ERR(ibm_backlight_device)) {
4304 printk(TPACPI_ERR "Could not register backlight device\n");
4305 return PTR_ERR(ibm_backlight_device);
4307 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4309 ibm_backlight_device->props.max_brightness =
4310 (tp_features.bright_16levels)? 15 : 7;
4311 ibm_backlight_device->props.brightness = b;
4312 backlight_update_status(ibm_backlight_device);
4317 static void brightness_exit(void)
4319 if (ibm_backlight_device) {
4320 vdbg_printk(TPACPI_DBG_EXIT,
4321 "calling backlight_device_unregister()\n");
4322 backlight_device_unregister(ibm_backlight_device);
4323 ibm_backlight_device = NULL;
4327 static int brightness_read(char *p)
4332 level = brightness_get(NULL);
4334 len += sprintf(p + len, "level:\t\tunreadable\n");
4336 len += sprintf(p + len, "level:\t\t%d\n", level);
4337 len += sprintf(p + len, "commands:\tup, down\n");
4338 len += sprintf(p + len, "commands:\tlevel <level>"
4339 " (<level> is 0-%d)\n",
4340 (tp_features.bright_16levels) ? 15 : 7);
4346 static int brightness_write(char *buf)
4351 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4353 level = brightness_get(NULL);
4357 while ((cmd = next_cmd(&buf))) {
4358 if (strlencmp(cmd, "up") == 0) {
4359 if (level < max_level)
4361 } else if (strlencmp(cmd, "down") == 0) {
4364 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4365 level >= 0 && level <= max_level) {
4372 * Now we know what the final level should be, so we try to set it.
4373 * Doing it this way makes the syscall restartable in case of EINTR
4375 rc = brightness_set(level);
4376 return (rc == -EINTR)? ERESTARTSYS : rc;
4379 static struct ibm_struct brightness_driver_data = {
4380 .name = "brightness",
4381 .read = brightness_read,
4382 .write = brightness_write,
4383 .exit = brightness_exit,
4386 /*************************************************************************
4390 static int volume_offset = 0x30;
4392 static int volume_read(char *p)
4397 if (!acpi_ec_read(volume_offset, &level)) {
4398 len += sprintf(p + len, "level:\t\tunreadable\n");
4400 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4401 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4402 len += sprintf(p + len, "commands:\tup, down, mute\n");
4403 len += sprintf(p + len, "commands:\tlevel <level>"
4404 " (<level> is 0-15)\n");
4410 static int volume_write(char *buf)
4412 int cmos_cmd, inc, i;
4414 int new_level, new_mute;
4417 while ((cmd = next_cmd(&buf))) {
4418 if (!acpi_ec_read(volume_offset, &level))
4420 new_mute = mute = level & 0x40;
4421 new_level = level = level & 0xf;
4423 if (strlencmp(cmd, "up") == 0) {
4427 new_level = level == 15 ? 15 : level + 1;
4428 } else if (strlencmp(cmd, "down") == 0) {
4432 new_level = level == 0 ? 0 : level - 1;
4433 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4434 new_level >= 0 && new_level <= 15) {
4436 } else if (strlencmp(cmd, "mute") == 0) {
4441 if (new_level != level) {
4442 /* mute doesn't change */
4444 cmos_cmd = (new_level > level) ?
4445 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4446 inc = new_level > level ? 1 : -1;
4448 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4449 !acpi_ec_write(volume_offset, level)))
4452 for (i = level; i != new_level; i += inc)
4453 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4454 !acpi_ec_write(volume_offset, i + inc))
4458 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4459 !acpi_ec_write(volume_offset, new_level + mute))) {
4464 if (new_mute != mute) {
4465 /* level doesn't change */
4467 cmos_cmd = (new_mute) ?
4468 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4470 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4471 !acpi_ec_write(volume_offset, level + new_mute))
4479 static struct ibm_struct volume_driver_data = {
4481 .read = volume_read,
4482 .write = volume_write,
4485 /*************************************************************************
4492 * TPACPI_FAN_RD_ACPI_GFAN:
4493 * ACPI GFAN method: returns fan level
4495 * see TPACPI_FAN_WR_ACPI_SFAN
4496 * EC 0x2f (HFSP) not available if GFAN exists
4498 * TPACPI_FAN_WR_ACPI_SFAN:
4499 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4501 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4504 * TPACPI_FAN_WR_TPEC:
4505 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4506 * Supported on almost all ThinkPads
4508 * Fan speed changes of any sort (including those caused by the
4509 * disengaged mode) are usually done slowly by the firmware as the
4510 * maximum ammount of fan duty cycle change per second seems to be
4513 * Reading is not available if GFAN exists.
4514 * Writing is not available if SFAN exists.
4517 * 7 automatic mode engaged;
4518 * (default operation mode of the ThinkPad)
4519 * fan level is ignored in this mode.
4520 * 6 full speed mode (takes precedence over bit 7);
4521 * not available on all thinkpads. May disable
4522 * the tachometer while the fan controller ramps up
4523 * the speed (which can take up to a few *minutes*).
4524 * Speeds up fan to 100% duty-cycle, which is far above
4525 * the standard RPM levels. It is not impossible that
4526 * it could cause hardware damage.
4527 * 5-3 unused in some models. Extra bits for fan level
4528 * in others, but still useless as all values above
4529 * 7 map to the same speed as level 7 in these models.
4530 * 2-0 fan level (0..7 usually)
4532 * 0x07 = max (set when temperatures critical)
4533 * Some ThinkPads may have other levels, see
4534 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4536 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4537 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4538 * does so, its initial value is meaningless (0x07).
4540 * For firmware bugs, refer to:
4541 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4545 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4546 * Main fan tachometer reading (in RPM)
4548 * This register is present on all ThinkPads with a new-style EC, and
4549 * it is known not to be present on the A21m/e, and T22, as there is
4550 * something else in offset 0x84 according to the ACPI DSDT. Other
4551 * ThinkPads from this same time period (and earlier) probably lack the
4552 * tachometer as well.
4554 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4555 * was never fixed by IBM to report the EC firmware version string
4556 * probably support the tachometer (like the early X models), so
4557 * detecting it is quite hard. We need more data to know for sure.
4559 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4562 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4565 * For firmware bugs, refer to:
4566 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4568 * TPACPI_FAN_WR_ACPI_FANS:
4569 * ThinkPad X31, X40, X41. Not available in the X60.
4571 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4572 * high speed. ACPI DSDT seems to map these three speeds to levels
4573 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4574 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4576 * The speeds are stored on handles
4577 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4579 * There are three default speed sets, acessible as handles:
4580 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4582 * ACPI DSDT switches which set is in use depending on various
4585 * TPACPI_FAN_WR_TPEC is also available and should be used to
4586 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4587 * but the ACPI tables just mention level 7.
4590 enum { /* Fan control constants */
4591 fan_status_offset = 0x2f, /* EC register 0x2f */
4592 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4593 * 0x84 must be read before 0x85 */
4595 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4596 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4598 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4601 enum fan_status_access_mode {
4602 TPACPI_FAN_NONE = 0, /* No fan status or control */
4603 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4604 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4607 enum fan_control_access_mode {
4608 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4609 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4610 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4611 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4614 enum fan_control_commands {
4615 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4616 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4617 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4618 * and also watchdog cmd */
4621 static int fan_control_allowed;
4623 static enum fan_status_access_mode fan_status_access_mode;
4624 static enum fan_control_access_mode fan_control_access_mode;
4625 static enum fan_control_commands fan_control_commands;
4627 static u8 fan_control_initial_status;
4628 static u8 fan_control_desired_level;
4629 static int fan_watchdog_maxinterval;
4631 static struct mutex fan_mutex;
4633 static void fan_watchdog_fire(struct work_struct *ignored);
4634 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4636 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4637 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4638 "\\FSPD", /* 600e/x, 770e, 770x */
4640 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4641 "JFNS", /* 770x-JL */
4645 * Call with fan_mutex held
4647 static void fan_update_desired_level(u8 status)
4650 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4652 fan_control_desired_level = 7;
4654 fan_control_desired_level = status;
4658 static int fan_get_status(u8 *status)
4663 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4665 switch (fan_status_access_mode) {
4666 case TPACPI_FAN_RD_ACPI_GFAN:
4667 /* 570, 600e/x, 770e, 770x */
4669 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4677 case TPACPI_FAN_RD_TPEC:
4678 /* all except 570, 600e/x, 770e, 770x */
4679 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4694 static int fan_get_status_safe(u8 *status)
4699 if (mutex_lock_interruptible(&fan_mutex))
4700 return -ERESTARTSYS;
4701 rc = fan_get_status(&s);
4703 fan_update_desired_level(s);
4704 mutex_unlock(&fan_mutex);
4712 static int fan_get_speed(unsigned int *speed)
4716 switch (fan_status_access_mode) {
4717 case TPACPI_FAN_RD_TPEC:
4718 /* all except 570, 600e/x, 770e, 770x */
4719 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4720 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4724 *speed = (hi << 8) | lo;
4735 static int fan_set_level(int level)
4737 if (!fan_control_allowed)
4740 switch (fan_control_access_mode) {
4741 case TPACPI_FAN_WR_ACPI_SFAN:
4742 if (level >= 0 && level <= 7) {
4743 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4749 case TPACPI_FAN_WR_ACPI_FANS:
4750 case TPACPI_FAN_WR_TPEC:
4751 if ((level != TP_EC_FAN_AUTO) &&
4752 (level != TP_EC_FAN_FULLSPEED) &&
4753 ((level < 0) || (level > 7)))
4756 /* safety net should the EC not support AUTO
4757 * or FULLSPEED mode bits and just ignore them */
4758 if (level & TP_EC_FAN_FULLSPEED)
4759 level |= 7; /* safety min speed 7 */
4760 else if (level & TP_EC_FAN_FULLSPEED)
4761 level |= 4; /* safety min speed 4 */
4763 if (!acpi_ec_write(fan_status_offset, level))
4766 tp_features.fan_ctrl_status_undef = 0;
4775 static int fan_set_level_safe(int level)
4779 if (!fan_control_allowed)
4782 if (mutex_lock_interruptible(&fan_mutex))
4783 return -ERESTARTSYS;
4785 if (level == TPACPI_FAN_LAST_LEVEL)
4786 level = fan_control_desired_level;
4788 rc = fan_set_level(level);
4790 fan_update_desired_level(level);
4792 mutex_unlock(&fan_mutex);
4796 static int fan_set_enable(void)
4801 if (!fan_control_allowed)
4804 if (mutex_lock_interruptible(&fan_mutex))
4805 return -ERESTARTSYS;
4807 switch (fan_control_access_mode) {
4808 case TPACPI_FAN_WR_ACPI_FANS:
4809 case TPACPI_FAN_WR_TPEC:
4810 rc = fan_get_status(&s);
4814 /* Don't go out of emergency fan mode */
4817 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4820 if (!acpi_ec_write(fan_status_offset, s))
4823 tp_features.fan_ctrl_status_undef = 0;
4828 case TPACPI_FAN_WR_ACPI_SFAN:
4829 rc = fan_get_status(&s);
4835 /* Set fan to at least level 4 */
4838 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4848 mutex_unlock(&fan_mutex);
4852 static int fan_set_disable(void)
4856 if (!fan_control_allowed)
4859 if (mutex_lock_interruptible(&fan_mutex))
4860 return -ERESTARTSYS;
4863 switch (fan_control_access_mode) {
4864 case TPACPI_FAN_WR_ACPI_FANS:
4865 case TPACPI_FAN_WR_TPEC:
4866 if (!acpi_ec_write(fan_status_offset, 0x00))
4869 fan_control_desired_level = 0;
4870 tp_features.fan_ctrl_status_undef = 0;
4874 case TPACPI_FAN_WR_ACPI_SFAN:
4875 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4878 fan_control_desired_level = 0;
4886 mutex_unlock(&fan_mutex);
4890 static int fan_set_speed(int speed)
4894 if (!fan_control_allowed)
4897 if (mutex_lock_interruptible(&fan_mutex))
4898 return -ERESTARTSYS;
4901 switch (fan_control_access_mode) {
4902 case TPACPI_FAN_WR_ACPI_FANS:
4903 if (speed >= 0 && speed <= 65535) {
4904 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4905 speed, speed, speed))
4915 mutex_unlock(&fan_mutex);
4919 static void fan_watchdog_reset(void)
4921 static int fan_watchdog_active;
4923 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4926 if (fan_watchdog_active)
4927 cancel_delayed_work(&fan_watchdog_task);
4929 if (fan_watchdog_maxinterval > 0 &&
4930 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4931 fan_watchdog_active = 1;
4932 if (!schedule_delayed_work(&fan_watchdog_task,
4933 msecs_to_jiffies(fan_watchdog_maxinterval
4936 "failed to schedule the fan watchdog, "
4937 "watchdog will not trigger\n");
4940 fan_watchdog_active = 0;
4943 static void fan_watchdog_fire(struct work_struct *ignored)
4947 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4950 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4951 rc = fan_set_enable();
4953 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4954 "will try again later...\n", -rc);
4955 /* reschedule for later */
4956 fan_watchdog_reset();
4961 * SYSFS fan layout: hwmon compatible (device)
4964 * 0: "disengaged" mode
4966 * 2: native EC "auto" mode (recommended, hardware default)
4968 * pwm*: set speed in manual mode, ignored otherwise.
4969 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4972 * fan*_input: tachometer reading, RPM
4975 * SYSFS fan layout: extensions
4977 * fan_watchdog (driver):
4978 * fan watchdog interval in seconds, 0 disables (default), max 120
4981 /* sysfs fan pwm1_enable ----------------------------------------------- */
4982 static ssize_t fan_pwm1_enable_show(struct device *dev,
4983 struct device_attribute *attr,
4989 res = fan_get_status_safe(&status);
4993 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4994 if (status != fan_control_initial_status) {
4995 tp_features.fan_ctrl_status_undef = 0;
4997 /* Return most likely status. In fact, it
4998 * might be the only possible status */
4999 status = TP_EC_FAN_AUTO;
5003 if (status & TP_EC_FAN_FULLSPEED) {
5005 } else if (status & TP_EC_FAN_AUTO) {
5010 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5013 static ssize_t fan_pwm1_enable_store(struct device *dev,
5014 struct device_attribute *attr,
5015 const char *buf, size_t count)
5020 if (parse_strtoul(buf, 2, &t))
5025 level = TP_EC_FAN_FULLSPEED;
5028 level = TPACPI_FAN_LAST_LEVEL;
5031 level = TP_EC_FAN_AUTO;
5034 /* reserved for software-controlled auto mode */
5040 res = fan_set_level_safe(level);
5046 fan_watchdog_reset();
5051 static struct device_attribute dev_attr_fan_pwm1_enable =
5052 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5053 fan_pwm1_enable_show, fan_pwm1_enable_store);
5055 /* sysfs fan pwm1 ------------------------------------------------------ */
5056 static ssize_t fan_pwm1_show(struct device *dev,
5057 struct device_attribute *attr,
5063 res = fan_get_status_safe(&status);
5067 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5068 if (status != fan_control_initial_status) {
5069 tp_features.fan_ctrl_status_undef = 0;
5071 status = TP_EC_FAN_AUTO;
5076 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5077 status = fan_control_desired_level;
5082 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5085 static ssize_t fan_pwm1_store(struct device *dev,
5086 struct device_attribute *attr,
5087 const char *buf, size_t count)
5091 u8 status, newlevel;
5093 if (parse_strtoul(buf, 255, &s))
5096 /* scale down from 0-255 to 0-7 */
5097 newlevel = (s >> 5) & 0x07;
5099 if (mutex_lock_interruptible(&fan_mutex))
5100 return -ERESTARTSYS;
5102 rc = fan_get_status(&status);
5103 if (!rc && (status &
5104 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5105 rc = fan_set_level(newlevel);
5109 fan_update_desired_level(newlevel);
5110 fan_watchdog_reset();
5114 mutex_unlock(&fan_mutex);
5115 return (rc)? rc : count;
5118 static struct device_attribute dev_attr_fan_pwm1 =
5119 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5120 fan_pwm1_show, fan_pwm1_store);
5122 /* sysfs fan fan1_input ------------------------------------------------ */
5123 static ssize_t fan_fan1_input_show(struct device *dev,
5124 struct device_attribute *attr,
5130 res = fan_get_speed(&speed);
5134 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5137 static struct device_attribute dev_attr_fan_fan1_input =
5138 __ATTR(fan1_input, S_IRUGO,
5139 fan_fan1_input_show, NULL);
5141 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5142 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5145 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5148 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5149 const char *buf, size_t count)
5153 if (parse_strtoul(buf, 120, &t))
5156 if (!fan_control_allowed)
5159 fan_watchdog_maxinterval = t;
5160 fan_watchdog_reset();
5165 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5166 fan_fan_watchdog_show, fan_fan_watchdog_store);
5168 /* --------------------------------------------------------------------- */
5169 static struct attribute *fan_attributes[] = {
5170 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5171 &dev_attr_fan_fan1_input.attr,
5175 static const struct attribute_group fan_attr_group = {
5176 .attrs = fan_attributes,
5179 static int __init fan_init(struct ibm_init_struct *iibm)
5183 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5185 mutex_init(&fan_mutex);
5186 fan_status_access_mode = TPACPI_FAN_NONE;
5187 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5188 fan_control_commands = 0;
5189 fan_watchdog_maxinterval = 0;
5190 tp_features.fan_ctrl_status_undef = 0;
5191 fan_control_desired_level = 7;
5193 TPACPI_ACPIHANDLE_INIT(fans);
5194 TPACPI_ACPIHANDLE_INIT(gfan);
5195 TPACPI_ACPIHANDLE_INIT(sfan);
5198 /* 570, 600e/x, 770e, 770x */
5199 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5201 /* all other ThinkPads: note that even old-style
5202 * ThinkPad ECs supports the fan control register */
5203 if (likely(acpi_ec_read(fan_status_offset,
5204 &fan_control_initial_status))) {
5205 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5207 /* In some ThinkPads, neither the EC nor the ACPI
5208 * DSDT initialize the fan status, and it ends up
5209 * being set to 0x07 when it *could* be either
5212 * Enable for TP-1Y (T43), TP-78 (R51e),
5213 * TP-76 (R52), TP-70 (T43, R52), which are known
5215 if (fan_control_initial_status == 0x07) {
5216 switch (thinkpad_id.ec_model) {
5217 case 0x5931: /* TP-1Y */
5218 case 0x3837: /* TP-78 */
5219 case 0x3637: /* TP-76 */
5220 case 0x3037: /* TP-70 */
5221 printk(TPACPI_NOTICE
5222 "fan_init: initial fan status "
5223 "is unknown, assuming it is "
5225 tp_features.fan_ctrl_status_undef = 1;
5231 "ThinkPad ACPI EC access misbehaving, "
5232 "fan status and control unavailable\n");
5239 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5240 fan_control_commands |=
5241 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5244 /* gfan without sfan means no fan control */
5245 /* all other models implement TP EC 0x2f control */
5249 fan_control_access_mode =
5250 TPACPI_FAN_WR_ACPI_FANS;
5251 fan_control_commands |=
5252 TPACPI_FAN_CMD_SPEED |
5253 TPACPI_FAN_CMD_LEVEL |
5254 TPACPI_FAN_CMD_ENABLE;
5256 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5257 fan_control_commands |=
5258 TPACPI_FAN_CMD_LEVEL |
5259 TPACPI_FAN_CMD_ENABLE;
5264 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5265 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5266 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5267 fan_status_access_mode, fan_control_access_mode);
5269 /* fan control master switch */
5270 if (!fan_control_allowed) {
5271 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5272 fan_control_commands = 0;
5273 dbg_printk(TPACPI_DBG_INIT,
5274 "fan control features disabled by parameter\n");
5277 /* update fan_control_desired_level */
5278 if (fan_status_access_mode != TPACPI_FAN_NONE)
5279 fan_get_status_safe(NULL);
5281 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5282 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5283 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5286 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5287 &driver_attr_fan_watchdog);
5295 static void fan_exit(void)
5297 vdbg_printk(TPACPI_DBG_EXIT,
5298 "cancelling any pending fan watchdog tasks\n");
5300 /* FIXME: can we really do this unconditionally? */
5301 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5302 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5303 &driver_attr_fan_watchdog);
5305 cancel_delayed_work(&fan_watchdog_task);
5306 flush_scheduled_work();
5309 static int fan_read(char *p)
5314 unsigned int speed = 0;
5316 switch (fan_status_access_mode) {
5317 case TPACPI_FAN_RD_ACPI_GFAN:
5318 /* 570, 600e/x, 770e, 770x */
5319 rc = fan_get_status_safe(&status);
5323 len += sprintf(p + len, "status:\t\t%s\n"
5325 (status != 0) ? "enabled" : "disabled", status);
5328 case TPACPI_FAN_RD_TPEC:
5329 /* all except 570, 600e/x, 770e, 770x */
5330 rc = fan_get_status_safe(&status);
5334 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5335 if (status != fan_control_initial_status)
5336 tp_features.fan_ctrl_status_undef = 0;
5338 /* Return most likely status. In fact, it
5339 * might be the only possible status */
5340 status = TP_EC_FAN_AUTO;
5343 len += sprintf(p + len, "status:\t\t%s\n",
5344 (status != 0) ? "enabled" : "disabled");
5346 rc = fan_get_speed(&speed);
5350 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5352 if (status & TP_EC_FAN_FULLSPEED)
5353 /* Disengaged mode takes precedence */
5354 len += sprintf(p + len, "level:\t\tdisengaged\n");
5355 else if (status & TP_EC_FAN_AUTO)
5356 len += sprintf(p + len, "level:\t\tauto\n");
5358 len += sprintf(p + len, "level:\t\t%d\n", status);
5361 case TPACPI_FAN_NONE:
5363 len += sprintf(p + len, "status:\t\tnot supported\n");
5366 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5367 len += sprintf(p + len, "commands:\tlevel <level>");
5369 switch (fan_control_access_mode) {
5370 case TPACPI_FAN_WR_ACPI_SFAN:
5371 len += sprintf(p + len, " (<level> is 0-7)\n");
5375 len += sprintf(p + len, " (<level> is 0-7, "
5376 "auto, disengaged, full-speed)\n");
5381 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5382 len += sprintf(p + len, "commands:\tenable, disable\n"
5383 "commands:\twatchdog <timeout> (<timeout> "
5384 "is 0 (off), 1-120 (seconds))\n");
5386 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5387 len += sprintf(p + len, "commands:\tspeed <speed>"
5388 " (<speed> is 0-65535)\n");
5393 static int fan_write_cmd_level(const char *cmd, int *rc)
5397 if (strlencmp(cmd, "level auto") == 0)
5398 level = TP_EC_FAN_AUTO;
5399 else if ((strlencmp(cmd, "level disengaged") == 0) |
5400 (strlencmp(cmd, "level full-speed") == 0))
5401 level = TP_EC_FAN_FULLSPEED;
5402 else if (sscanf(cmd, "level %d", &level) != 1)
5405 *rc = fan_set_level_safe(level);
5407 printk(TPACPI_ERR "level command accepted for unsupported "
5408 "access mode %d", fan_control_access_mode);
5413 static int fan_write_cmd_enable(const char *cmd, int *rc)
5415 if (strlencmp(cmd, "enable") != 0)
5418 *rc = fan_set_enable();
5420 printk(TPACPI_ERR "enable command accepted for unsupported "
5421 "access mode %d", fan_control_access_mode);
5426 static int fan_write_cmd_disable(const char *cmd, int *rc)
5428 if (strlencmp(cmd, "disable") != 0)
5431 *rc = fan_set_disable();
5433 printk(TPACPI_ERR "disable command accepted for unsupported "
5434 "access mode %d", fan_control_access_mode);
5439 static int fan_write_cmd_speed(const char *cmd, int *rc)
5444 * Support speed <low> <medium> <high> ? */
5446 if (sscanf(cmd, "speed %d", &speed) != 1)
5449 *rc = fan_set_speed(speed);
5451 printk(TPACPI_ERR "speed command accepted for unsupported "
5452 "access mode %d", fan_control_access_mode);
5457 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5461 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5464 if (interval < 0 || interval > 120)
5467 fan_watchdog_maxinterval = interval;
5472 static int fan_write(char *buf)
5477 while (!rc && (cmd = next_cmd(&buf))) {
5478 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5479 fan_write_cmd_level(cmd, &rc)) &&
5480 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5481 (fan_write_cmd_enable(cmd, &rc) ||
5482 fan_write_cmd_disable(cmd, &rc) ||
5483 fan_write_cmd_watchdog(cmd, &rc))) &&
5484 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5485 fan_write_cmd_speed(cmd, &rc))
5489 fan_watchdog_reset();
5495 static struct ibm_struct fan_driver_data = {
5502 /****************************************************************************
5503 ****************************************************************************
5507 ****************************************************************************
5508 ****************************************************************************/
5510 /* sysfs name ---------------------------------------------------------- */
5511 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5512 struct device_attribute *attr,
5515 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5518 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5519 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5521 /* --------------------------------------------------------------------- */
5524 static struct proc_dir_entry *proc_dir;
5527 * Module and infrastructure proble, init and exit handling
5530 static int force_load;
5532 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5533 static const char * __init str_supported(int is_supported)
5535 static char text_unsupported[] __initdata = "not supported";
5537 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5539 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5541 static void ibm_exit(struct ibm_struct *ibm)
5543 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5545 list_del_init(&ibm->all_drivers);
5547 if (ibm->flags.acpi_notify_installed) {
5548 dbg_printk(TPACPI_DBG_EXIT,
5549 "%s: acpi_remove_notify_handler\n", ibm->name);
5551 acpi_remove_notify_handler(*ibm->acpi->handle,
5553 dispatch_acpi_notify);
5554 ibm->flags.acpi_notify_installed = 0;
5555 ibm->flags.acpi_notify_installed = 0;
5558 if (ibm->flags.proc_created) {
5559 dbg_printk(TPACPI_DBG_EXIT,
5560 "%s: remove_proc_entry\n", ibm->name);
5561 remove_proc_entry(ibm->name, proc_dir);
5562 ibm->flags.proc_created = 0;
5565 if (ibm->flags.acpi_driver_registered) {
5566 dbg_printk(TPACPI_DBG_EXIT,
5567 "%s: acpi_bus_unregister_driver\n", ibm->name);
5569 acpi_bus_unregister_driver(ibm->acpi->driver);
5570 kfree(ibm->acpi->driver);
5571 ibm->acpi->driver = NULL;
5572 ibm->flags.acpi_driver_registered = 0;
5575 if (ibm->flags.init_called && ibm->exit) {
5577 ibm->flags.init_called = 0;
5580 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5583 static int __init ibm_init(struct ibm_init_struct *iibm)
5586 struct ibm_struct *ibm = iibm->data;
5587 struct proc_dir_entry *entry;
5589 BUG_ON(ibm == NULL);
5591 INIT_LIST_HEAD(&ibm->all_drivers);
5593 if (ibm->flags.experimental && !experimental)
5596 dbg_printk(TPACPI_DBG_INIT,
5597 "probing for %s\n", ibm->name);
5600 ret = iibm->init(iibm);
5602 return 0; /* probe failed */
5606 ibm->flags.init_called = 1;
5610 if (ibm->acpi->hid) {
5611 ret = register_tpacpi_subdriver(ibm);
5616 if (ibm->acpi->notify) {
5617 ret = setup_acpi_notify(ibm);
5618 if (ret == -ENODEV) {
5619 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5629 dbg_printk(TPACPI_DBG_INIT,
5630 "%s installed\n", ibm->name);
5633 entry = create_proc_entry(ibm->name,
5634 S_IFREG | S_IRUGO | S_IWUSR,
5637 printk(TPACPI_ERR "unable to create proc entry %s\n",
5642 entry->owner = THIS_MODULE;
5644 entry->read_proc = &dispatch_procfs_read;
5646 entry->write_proc = &dispatch_procfs_write;
5647 ibm->flags.proc_created = 1;
5650 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5655 dbg_printk(TPACPI_DBG_INIT,
5656 "%s: at error exit path with result %d\n",
5660 return (ret < 0)? ret : 0;
5665 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5667 const struct dmi_device *dev = NULL;
5668 char ec_fw_string[18];
5673 memset(tp, 0, sizeof(*tp));
5675 if (dmi_name_in_vendors("IBM"))
5676 tp->vendor = PCI_VENDOR_ID_IBM;
5677 else if (dmi_name_in_vendors("LENOVO"))
5678 tp->vendor = PCI_VENDOR_ID_LENOVO;
5682 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5684 if (!tp->bios_version_str)
5686 tp->bios_model = tp->bios_version_str[0]
5687 | (tp->bios_version_str[1] << 8);
5690 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5691 * X32 or newer, all Z series; Some models must have an
5692 * up-to-date BIOS or they will not be detected.
5694 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5696 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5697 if (sscanf(dev->name,
5698 "IBM ThinkPad Embedded Controller -[%17c",
5699 ec_fw_string) == 1) {
5700 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5701 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5703 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5704 tp->ec_model = ec_fw_string[0]
5705 | (ec_fw_string[1] << 8);
5710 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5712 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5713 kfree(tp->model_str);
5714 tp->model_str = NULL;
5718 static int __init probe_for_thinkpad(void)
5726 * Non-ancient models have better DMI tagging, but very old models
5729 is_thinkpad = (thinkpad_id.model_str != NULL);
5731 /* ec is required because many other handles are relative to it */
5732 TPACPI_ACPIHANDLE_INIT(ec);
5736 "Not yet supported ThinkPad detected!\n");
5741 * Risks a regression on very old machines, but reduces potential
5742 * false positives a damn great deal
5745 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5747 if (!is_thinkpad && !force_load)
5754 /* Module init, exit, parameters */
5756 static struct ibm_init_struct ibms_init[] __initdata = {
5758 .init = thinkpad_acpi_driver_init,
5759 .data = &thinkpad_acpi_driver_data,
5762 .init = hotkey_init,
5763 .data = &hotkey_driver_data,
5766 .init = bluetooth_init,
5767 .data = &bluetooth_driver_data,
5771 .data = &wan_driver_data,
5775 .data = &video_driver_data,
5779 .data = &light_driver_data,
5781 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5784 .data = &dock_driver_data[0],
5788 .data = &dock_driver_data[1],
5791 #ifdef CONFIG_THINKPAD_ACPI_BAY
5794 .data = &bay_driver_data,
5799 .data = &cmos_driver_data,
5803 .data = &led_driver_data,
5807 .data = &beep_driver_data,
5810 .init = thermal_init,
5811 .data = &thermal_driver_data,
5814 .data = &ecdump_driver_data,
5817 .init = brightness_init,
5818 .data = &brightness_driver_data,
5821 .data = &volume_driver_data,
5825 .data = &fan_driver_data,
5829 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5832 struct ibm_struct *ibm;
5834 if (!kp || !kp->name || !val)
5837 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5838 ibm = ibms_init[i].data;
5839 WARN_ON(ibm == NULL);
5841 if (!ibm || !ibm->name)
5844 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5845 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5847 strcpy(ibms_init[i].param, val);
5848 strcat(ibms_init[i].param, ",");
5856 module_param(experimental, int, 0);
5857 MODULE_PARM_DESC(experimental,
5858 "Enables experimental features when non-zero");
5860 module_param_named(debug, dbg_level, uint, 0);
5861 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5863 module_param(force_load, bool, 0);
5864 MODULE_PARM_DESC(force_load,
5865 "Attempts to load the driver even on a "
5866 "mis-identified ThinkPad when true");
5868 module_param_named(fan_control, fan_control_allowed, bool, 0);
5869 MODULE_PARM_DESC(fan_control,
5870 "Enables setting fan parameters features when true");
5872 module_param_named(brightness_mode, brightness_mode, int, 0);
5873 MODULE_PARM_DESC(brightness_mode,
5874 "Selects brightness control strategy: "
5875 "0=auto, 1=EC, 2=CMOS, 3=both");
5877 module_param(brightness_enable, uint, 0);
5878 MODULE_PARM_DESC(brightness_enable,
5879 "Enables backlight control when 1, disables when 0");
5881 module_param(hotkey_report_mode, uint, 0);
5882 MODULE_PARM_DESC(hotkey_report_mode,
5883 "used for backwards compatibility with userspace, "
5884 "see documentation");
5886 #define TPACPI_PARAM(feature) \
5887 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5888 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5889 "at module load, see documentation")
5891 TPACPI_PARAM(hotkey);
5892 TPACPI_PARAM(bluetooth);
5893 TPACPI_PARAM(video);
5894 TPACPI_PARAM(light);
5895 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5898 #ifdef CONFIG_THINKPAD_ACPI_BAY
5900 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5904 TPACPI_PARAM(ecdump);
5905 TPACPI_PARAM(brightness);
5906 TPACPI_PARAM(volume);
5909 static void thinkpad_acpi_module_exit(void)
5911 struct ibm_struct *ibm, *itmp;
5913 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5915 list_for_each_entry_safe_reverse(ibm, itmp,
5916 &tpacpi_all_drivers,
5921 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5923 if (tpacpi_inputdev) {
5924 if (tp_features.input_device_registered)
5925 input_unregister_device(tpacpi_inputdev);
5927 input_free_device(tpacpi_inputdev);
5931 hwmon_device_unregister(tpacpi_hwmon);
5933 if (tp_features.sensors_pdev_attrs_registered)
5934 device_remove_file(&tpacpi_sensors_pdev->dev,
5935 &dev_attr_thinkpad_acpi_pdev_name);
5936 if (tpacpi_sensors_pdev)
5937 platform_device_unregister(tpacpi_sensors_pdev);
5939 platform_device_unregister(tpacpi_pdev);
5941 if (tp_features.sensors_pdrv_attrs_registered)
5942 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5943 if (tp_features.platform_drv_attrs_registered)
5944 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5946 if (tp_features.sensors_pdrv_registered)
5947 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5949 if (tp_features.platform_drv_registered)
5950 platform_driver_unregister(&tpacpi_pdriver);
5953 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5955 kfree(thinkpad_id.bios_version_str);
5956 kfree(thinkpad_id.ec_version_str);
5957 kfree(thinkpad_id.model_str);
5961 static int __init thinkpad_acpi_module_init(void)
5965 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5967 /* Parameter checking */
5968 if (hotkey_report_mode > 2)
5971 /* Driver-level probe */
5973 get_thinkpad_model_data(&thinkpad_id);
5974 ret = probe_for_thinkpad();
5976 thinkpad_acpi_module_exit();
5980 /* Driver initialization */
5982 TPACPI_ACPIHANDLE_INIT(ecrd);
5983 TPACPI_ACPIHANDLE_INIT(ecwr);
5985 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5988 "unable to create proc dir " TPACPI_PROC_DIR);
5989 thinkpad_acpi_module_exit();
5992 proc_dir->owner = THIS_MODULE;
5994 ret = platform_driver_register(&tpacpi_pdriver);
5997 "unable to register main platform driver\n");
5998 thinkpad_acpi_module_exit();
6001 tp_features.platform_drv_registered = 1;
6003 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6006 "unable to register hwmon platform driver\n");
6007 thinkpad_acpi_module_exit();
6010 tp_features.sensors_pdrv_registered = 1;
6012 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6014 tp_features.platform_drv_attrs_registered = 1;
6015 ret = tpacpi_create_driver_attributes(
6016 &tpacpi_hwmon_pdriver.driver);
6020 "unable to create sysfs driver attributes\n");
6021 thinkpad_acpi_module_exit();
6024 tp_features.sensors_pdrv_attrs_registered = 1;
6027 /* Device initialization */
6028 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6030 if (IS_ERR(tpacpi_pdev)) {
6031 ret = PTR_ERR(tpacpi_pdev);
6033 printk(TPACPI_ERR "unable to register platform device\n");
6034 thinkpad_acpi_module_exit();
6037 tpacpi_sensors_pdev = platform_device_register_simple(
6038 TPACPI_HWMON_DRVR_NAME,
6040 if (IS_ERR(tpacpi_sensors_pdev)) {
6041 ret = PTR_ERR(tpacpi_sensors_pdev);
6042 tpacpi_sensors_pdev = NULL;
6044 "unable to register hwmon platform device\n");
6045 thinkpad_acpi_module_exit();
6048 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6049 &dev_attr_thinkpad_acpi_pdev_name);
6052 "unable to create sysfs hwmon device attributes\n");
6053 thinkpad_acpi_module_exit();
6056 tp_features.sensors_pdev_attrs_registered = 1;
6057 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6058 if (IS_ERR(tpacpi_hwmon)) {
6059 ret = PTR_ERR(tpacpi_hwmon);
6060 tpacpi_hwmon = NULL;
6061 printk(TPACPI_ERR "unable to register hwmon device\n");
6062 thinkpad_acpi_module_exit();
6065 mutex_init(&tpacpi_inputdev_send_mutex);
6066 tpacpi_inputdev = input_allocate_device();
6067 if (!tpacpi_inputdev) {
6068 printk(TPACPI_ERR "unable to allocate input device\n");
6069 thinkpad_acpi_module_exit();
6072 /* Prepare input device, but don't register */
6073 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6074 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6075 tpacpi_inputdev->id.bustype = BUS_HOST;
6076 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6077 thinkpad_id.vendor :
6079 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6080 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6082 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6083 ret = ibm_init(&ibms_init[i]);
6084 if (ret >= 0 && *ibms_init[i].param)
6085 ret = ibms_init[i].data->write(ibms_init[i].param);
6087 thinkpad_acpi_module_exit();
6091 ret = input_register_device(tpacpi_inputdev);
6093 printk(TPACPI_ERR "unable to register input device\n");
6094 thinkpad_acpi_module_exit();
6097 tp_features.input_device_registered = 1;
6100 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6104 /* Please remove this in year 2009 */
6105 MODULE_ALIAS("ibm_acpi");
6108 * DMI matching for module autoloading
6110 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6111 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6113 * Only models listed in thinkwiki will be supported, so add yours
6114 * if it is not there yet.
6116 #define IBM_BIOS_MODULE_ALIAS(__type) \
6117 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6119 /* Non-ancient thinkpads */
6120 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6121 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6123 /* Ancient thinkpad BIOSes have to be identified by
6124 * BIOS type or model number, and there are far less
6125 * BIOS types than model numbers... */
6126 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6127 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6128 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6130 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6131 MODULE_DESCRIPTION(TPACPI_DESC);
6132 MODULE_VERSION(TPACPI_VERSION);
6133 MODULE_LICENSE("GPL");
6135 module_init(thinkpad_acpi_module_init);
6136 module_exit(thinkpad_acpi_module_exit);