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 int hotkey_orig_status;
1022 static u32 hotkey_orig_mask;
1023 static u32 hotkey_all_mask;
1024 static u32 hotkey_reserved_mask;
1025 static u32 hotkey_mask;
1027 static unsigned int hotkey_report_mode;
1029 static u16 *hotkey_keycode_map;
1031 static struct attribute_set *hotkey_dev_attributes;
1033 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1034 #define HOTKEY_CONFIG_CRITICAL_START \
1036 mutex_lock(&hotkey_thread_data_mutex); \
1037 hotkey_config_change++; \
1039 #define HOTKEY_CONFIG_CRITICAL_END \
1040 mutex_unlock(&hotkey_thread_data_mutex);
1042 #define HOTKEY_CONFIG_CRITICAL_START
1043 #define HOTKEY_CONFIG_CRITICAL_END
1044 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1046 static int hotkey_get_wlsw(int *status)
1048 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1054 * Call with hotkey_mutex held
1056 static int hotkey_mask_get(void)
1060 if (tp_features.hotkey_mask) {
1061 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1064 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1070 * Call with hotkey_mutex held
1072 static int hotkey_mask_set(u32 mask)
1077 if (tp_features.hotkey_mask) {
1078 HOTKEY_CONFIG_CRITICAL_START
1079 for (i = 0; i < 32; i++) {
1081 /* enable in firmware mask only keys not in NVRAM
1082 * mode, but enable the key in the cached hotkey_mask
1083 * regardless of mode, or the key will end up
1084 * disabled by hotkey_mask_get() */
1085 if (!acpi_evalf(hkey_handle,
1086 NULL, "MHKM", "vdd", i + 1,
1087 !!((mask & ~hotkey_source_mask) & m))) {
1091 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1094 HOTKEY_CONFIG_CRITICAL_END
1096 /* hotkey_mask_get must be called unconditionally below */
1097 if (!hotkey_mask_get() && !rc &&
1098 (hotkey_mask & ~hotkey_source_mask) !=
1099 (mask & ~hotkey_source_mask)) {
1100 printk(TPACPI_NOTICE
1101 "requested hot key mask 0x%08x, but "
1102 "firmware forced it to 0x%08x\n",
1106 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1107 HOTKEY_CONFIG_CRITICAL_START
1108 hotkey_mask = mask & hotkey_source_mask;
1109 HOTKEY_CONFIG_CRITICAL_END
1111 if (hotkey_mask != mask) {
1112 printk(TPACPI_NOTICE
1113 "requested hot key mask 0x%08x, "
1114 "forced to 0x%08x (NVRAM poll mask is "
1115 "0x%08x): no firmware mask support\n",
1116 mask, hotkey_mask, hotkey_source_mask);
1121 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1127 static int hotkey_status_get(int *status)
1129 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1135 static int hotkey_status_set(int status)
1137 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1143 static void tpacpi_input_send_radiosw(void)
1147 mutex_lock(&tpacpi_inputdev_send_mutex);
1149 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1150 input_report_switch(tpacpi_inputdev,
1152 input_sync(tpacpi_inputdev);
1155 mutex_unlock(&tpacpi_inputdev_send_mutex);
1158 static void tpacpi_input_send_key(unsigned int scancode)
1160 unsigned int keycode;
1162 keycode = hotkey_keycode_map[scancode];
1164 if (keycode != KEY_RESERVED) {
1165 mutex_lock(&tpacpi_inputdev_send_mutex);
1167 input_report_key(tpacpi_inputdev, keycode, 1);
1168 if (keycode == KEY_UNKNOWN)
1169 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1171 input_sync(tpacpi_inputdev);
1173 input_report_key(tpacpi_inputdev, keycode, 0);
1174 if (keycode == KEY_UNKNOWN)
1175 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1177 input_sync(tpacpi_inputdev);
1179 mutex_unlock(&tpacpi_inputdev_send_mutex);
1183 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1184 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1186 static void tpacpi_hotkey_send_key(unsigned int scancode)
1188 tpacpi_input_send_key(scancode);
1189 if (hotkey_report_mode < 2) {
1190 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1191 0x80, 0x1001 + scancode);
1195 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1199 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1200 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1201 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1202 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1203 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1204 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1206 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1207 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1208 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1210 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1211 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1212 n->displayexp_toggle =
1213 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1215 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1216 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1217 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1218 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1219 n->brightness_toggle =
1220 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1222 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1223 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1224 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1225 >> TP_NVRAM_POS_LEVEL_VOLUME;
1226 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1227 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1231 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1233 if ((mask & (1 << __scancode)) && \
1234 oldn->__member != newn->__member) \
1235 tpacpi_hotkey_send_key(__scancode); \
1238 #define TPACPI_MAY_SEND_KEY(__scancode) \
1239 do { if (mask & (1 << __scancode)) \
1240 tpacpi_hotkey_send_key(__scancode); } while (0)
1242 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1243 struct tp_nvram_state *newn,
1246 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1247 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1248 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1249 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1251 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1253 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1256 if (oldn->volume_toggle != newn->volume_toggle) {
1257 if (oldn->mute != newn->mute) {
1258 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1260 if (oldn->volume_level > newn->volume_level) {
1261 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1262 } else if (oldn->volume_level < newn->volume_level) {
1263 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1264 } else if (oldn->mute == newn->mute) {
1265 /* repeated key presses that didn't change state */
1267 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1268 } else if (newn->volume_level != 0) {
1269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1276 /* handle brightness */
1277 if (oldn->brightness_toggle != newn->brightness_toggle) {
1278 if (oldn->brightness_level < newn->brightness_level) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1280 } else if (oldn->brightness_level > newn->brightness_level) {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1283 /* repeated key presses that didn't change state */
1284 if (newn->brightness_level != 0) {
1285 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1287 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1293 #undef TPACPI_COMPARE_KEY
1294 #undef TPACPI_MAY_SEND_KEY
1296 static int hotkey_kthread(void *data)
1298 struct tp_nvram_state s[2];
1300 unsigned int si, so;
1302 unsigned int change_detector, must_reset;
1304 mutex_lock(&hotkey_thread_mutex);
1306 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1315 /* Initial state for compares */
1316 mutex_lock(&hotkey_thread_data_mutex);
1317 change_detector = hotkey_config_change;
1318 mask = hotkey_source_mask & hotkey_mask;
1319 mutex_unlock(&hotkey_thread_data_mutex);
1320 hotkey_read_nvram(&s[so], mask);
1322 while (!kthread_should_stop() && hotkey_poll_freq) {
1324 t = 1000/hotkey_poll_freq;
1325 t = msleep_interruptible(t);
1326 if (unlikely(kthread_should_stop()))
1328 must_reset = try_to_freeze();
1329 if (t > 0 && !must_reset)
1332 mutex_lock(&hotkey_thread_data_mutex);
1333 if (must_reset || hotkey_config_change != change_detector) {
1334 /* forget old state on thaw or config change */
1337 change_detector = hotkey_config_change;
1339 mask = hotkey_source_mask & hotkey_mask;
1340 mutex_unlock(&hotkey_thread_data_mutex);
1343 hotkey_read_nvram(&s[si], mask);
1344 if (likely(si != so)) {
1345 hotkey_compare_and_issue_event(&s[so], &s[si],
1355 mutex_unlock(&hotkey_thread_mutex);
1359 static void hotkey_poll_stop_sync(void)
1361 if (tpacpi_hotkey_task) {
1362 if (frozen(tpacpi_hotkey_task) ||
1363 freezing(tpacpi_hotkey_task))
1364 thaw_process(tpacpi_hotkey_task);
1366 kthread_stop(tpacpi_hotkey_task);
1367 tpacpi_hotkey_task = NULL;
1368 mutex_lock(&hotkey_thread_mutex);
1369 /* at this point, the thread did exit */
1370 mutex_unlock(&hotkey_thread_mutex);
1374 /* call with hotkey_mutex held */
1375 static void hotkey_poll_setup(int may_warn)
1377 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1378 hotkey_poll_freq > 0 &&
1379 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1380 if (!tpacpi_hotkey_task) {
1381 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1384 if (IS_ERR(tpacpi_hotkey_task)) {
1385 tpacpi_hotkey_task = NULL;
1387 "could not create kernel thread "
1388 "for hotkey polling\n");
1392 hotkey_poll_stop_sync();
1394 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1395 printk(TPACPI_NOTICE
1396 "hot keys 0x%08x require polling, "
1397 "which is currently disabled\n",
1398 hotkey_source_mask);
1403 static void hotkey_poll_setup_safe(int may_warn)
1405 mutex_lock(&hotkey_mutex);
1406 hotkey_poll_setup(may_warn);
1407 mutex_unlock(&hotkey_mutex);
1410 static int hotkey_inputdev_open(struct input_dev *dev)
1412 switch (tpacpi_lifecycle) {
1413 case TPACPI_LIFE_INIT:
1415 * hotkey_init will call hotkey_poll_setup_safe
1416 * at the appropriate moment
1419 case TPACPI_LIFE_EXITING:
1421 case TPACPI_LIFE_RUNNING:
1422 hotkey_poll_setup_safe(0);
1426 /* Should only happen if tpacpi_lifecycle is corrupt */
1431 static void hotkey_inputdev_close(struct input_dev *dev)
1433 /* disable hotkey polling when possible */
1434 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1435 hotkey_poll_setup_safe(0);
1437 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1439 /* sysfs hotkey enable ------------------------------------------------- */
1440 static ssize_t hotkey_enable_show(struct device *dev,
1441 struct device_attribute *attr,
1446 res = hotkey_status_get(&status);
1450 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1453 static ssize_t hotkey_enable_store(struct device *dev,
1454 struct device_attribute *attr,
1455 const char *buf, size_t count)
1460 if (parse_strtoul(buf, 1, &t))
1463 res = hotkey_status_set(t);
1465 return (res) ? res : count;
1468 static struct device_attribute dev_attr_hotkey_enable =
1469 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1470 hotkey_enable_show, hotkey_enable_store);
1472 /* sysfs hotkey mask --------------------------------------------------- */
1473 static ssize_t hotkey_mask_show(struct device *dev,
1474 struct device_attribute *attr,
1479 if (mutex_lock_interruptible(&hotkey_mutex))
1480 return -ERESTARTSYS;
1481 res = hotkey_mask_get();
1482 mutex_unlock(&hotkey_mutex);
1485 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1488 static ssize_t hotkey_mask_store(struct device *dev,
1489 struct device_attribute *attr,
1490 const char *buf, size_t count)
1495 if (parse_strtoul(buf, 0xffffffffUL, &t))
1498 if (mutex_lock_interruptible(&hotkey_mutex))
1499 return -ERESTARTSYS;
1501 res = hotkey_mask_set(t);
1503 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1504 hotkey_poll_setup(1);
1507 mutex_unlock(&hotkey_mutex);
1509 return (res) ? res : count;
1512 static struct device_attribute dev_attr_hotkey_mask =
1513 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1514 hotkey_mask_show, hotkey_mask_store);
1516 /* sysfs hotkey bios_enabled ------------------------------------------- */
1517 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1518 struct device_attribute *attr,
1521 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1524 static struct device_attribute dev_attr_hotkey_bios_enabled =
1525 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1527 /* sysfs hotkey bios_mask ---------------------------------------------- */
1528 static ssize_t hotkey_bios_mask_show(struct device *dev,
1529 struct device_attribute *attr,
1532 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1535 static struct device_attribute dev_attr_hotkey_bios_mask =
1536 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1538 /* sysfs hotkey all_mask ----------------------------------------------- */
1539 static ssize_t hotkey_all_mask_show(struct device *dev,
1540 struct device_attribute *attr,
1543 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1544 hotkey_all_mask | hotkey_source_mask);
1547 static struct device_attribute dev_attr_hotkey_all_mask =
1548 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1550 /* sysfs hotkey recommended_mask --------------------------------------- */
1551 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1552 struct device_attribute *attr,
1555 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1556 (hotkey_all_mask | hotkey_source_mask)
1557 & ~hotkey_reserved_mask);
1560 static struct device_attribute dev_attr_hotkey_recommended_mask =
1561 __ATTR(hotkey_recommended_mask, S_IRUGO,
1562 hotkey_recommended_mask_show, NULL);
1564 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1566 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1567 static ssize_t hotkey_source_mask_show(struct device *dev,
1568 struct device_attribute *attr,
1571 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1574 static ssize_t hotkey_source_mask_store(struct device *dev,
1575 struct device_attribute *attr,
1576 const char *buf, size_t count)
1580 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1581 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1584 if (mutex_lock_interruptible(&hotkey_mutex))
1585 return -ERESTARTSYS;
1587 HOTKEY_CONFIG_CRITICAL_START
1588 hotkey_source_mask = t;
1589 HOTKEY_CONFIG_CRITICAL_END
1591 hotkey_poll_setup(1);
1593 mutex_unlock(&hotkey_mutex);
1598 static struct device_attribute dev_attr_hotkey_source_mask =
1599 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1600 hotkey_source_mask_show, hotkey_source_mask_store);
1602 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1603 static ssize_t hotkey_poll_freq_show(struct device *dev,
1604 struct device_attribute *attr,
1607 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1610 static ssize_t hotkey_poll_freq_store(struct device *dev,
1611 struct device_attribute *attr,
1612 const char *buf, size_t count)
1616 if (parse_strtoul(buf, 25, &t))
1619 if (mutex_lock_interruptible(&hotkey_mutex))
1620 return -ERESTARTSYS;
1622 hotkey_poll_freq = t;
1624 hotkey_poll_setup(1);
1625 mutex_unlock(&hotkey_mutex);
1630 static struct device_attribute dev_attr_hotkey_poll_freq =
1631 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1632 hotkey_poll_freq_show, hotkey_poll_freq_store);
1634 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1636 /* sysfs hotkey radio_sw ----------------------------------------------- */
1637 static ssize_t hotkey_radio_sw_show(struct device *dev,
1638 struct device_attribute *attr,
1642 res = hotkey_get_wlsw(&s);
1646 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1649 static struct device_attribute dev_attr_hotkey_radio_sw =
1650 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1652 /* sysfs hotkey report_mode -------------------------------------------- */
1653 static ssize_t hotkey_report_mode_show(struct device *dev,
1654 struct device_attribute *attr,
1657 return snprintf(buf, PAGE_SIZE, "%d\n",
1658 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1661 static struct device_attribute dev_attr_hotkey_report_mode =
1662 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1664 /* --------------------------------------------------------------------- */
1666 static struct attribute *hotkey_attributes[] __initdata = {
1667 &dev_attr_hotkey_enable.attr,
1668 &dev_attr_hotkey_bios_enabled.attr,
1669 &dev_attr_hotkey_report_mode.attr,
1670 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1671 &dev_attr_hotkey_mask.attr,
1672 &dev_attr_hotkey_all_mask.attr,
1673 &dev_attr_hotkey_recommended_mask.attr,
1674 &dev_attr_hotkey_source_mask.attr,
1675 &dev_attr_hotkey_poll_freq.attr,
1679 static struct attribute *hotkey_mask_attributes[] __initdata = {
1680 &dev_attr_hotkey_bios_mask.attr,
1681 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1682 &dev_attr_hotkey_mask.attr,
1683 &dev_attr_hotkey_all_mask.attr,
1684 &dev_attr_hotkey_recommended_mask.attr,
1688 static int __init hotkey_init(struct ibm_init_struct *iibm)
1690 /* Requirements for changing the default keymaps:
1692 * 1. Many of the keys are mapped to KEY_RESERVED for very
1693 * good reasons. Do not change them unless you have deep
1694 * knowledge on the IBM and Lenovo ThinkPad firmware for
1695 * the various ThinkPad models. The driver behaves
1696 * differently for KEY_RESERVED: such keys have their
1697 * hot key mask *unset* in mask_recommended, and also
1698 * in the initial hot key mask programmed into the
1699 * firmware at driver load time, which means the firm-
1700 * ware may react very differently if you change them to
1703 * 2. You must be subscribed to the linux-thinkpad and
1704 * ibm-acpi-devel mailing lists, and you should read the
1705 * list archives since 2007 if you want to change the
1706 * keymaps. This requirement exists so that you will
1707 * know the past history of problems with the thinkpad-
1708 * acpi driver keymaps, and also that you will be
1709 * listening to any bug reports;
1711 * 3. Do not send thinkpad-acpi specific patches directly to
1712 * for merging, *ever*. Send them to the linux-acpi
1713 * mailinglist for comments. Merging is to be done only
1714 * through acpi-test and the ACPI maintainer.
1716 * If the above is too much to ask, don't change the keymap.
1717 * Ask the thinkpad-acpi maintainer to do it, instead.
1719 static u16 ibm_keycode_map[] __initdata = {
1720 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1721 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1722 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1723 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1725 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1726 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1727 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1728 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1730 /* brightness: firmware always reacts to them, unless
1731 * X.org did some tricks in the radeon BIOS scratch
1732 * registers of *some* models */
1733 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1734 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1736 /* Thinklight: firmware always react to it */
1737 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1739 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1740 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1742 /* Volume: firmware always react to it and reprograms
1743 * the built-in *extra* mixer. Never map it to control
1744 * another mixer by default. */
1745 KEY_RESERVED, /* 0x14: VOLUME UP */
1746 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1747 KEY_RESERVED, /* 0x16: MUTE */
1749 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1751 /* (assignments unknown, please report if found) */
1752 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1753 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1755 static u16 lenovo_keycode_map[] __initdata = {
1756 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1757 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1758 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1759 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1761 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1762 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1763 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1764 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1766 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1767 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1769 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1771 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1772 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1774 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1775 * react to it and reprograms the built-in *extra* mixer.
1776 * Never map it to control another mixer by default.
1778 * T60?, T61, R60?, R61: firmware and EC tries to send
1779 * these over the regular keyboard, so these are no-ops,
1780 * but there are still weird bugs re. MUTE, so do not
1781 * change unless you get test reports from all Lenovo
1782 * models. May cause the BIOS to interfere with the
1785 KEY_RESERVED, /* 0x14: VOLUME UP */
1786 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1787 KEY_RESERVED, /* 0x16: MUTE */
1789 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1791 /* (assignments unknown, please report if found) */
1792 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1793 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1796 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1797 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1798 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1804 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1806 BUG_ON(!tpacpi_inputdev);
1807 BUG_ON(tpacpi_inputdev->open != NULL ||
1808 tpacpi_inputdev->close != NULL);
1810 TPACPI_ACPIHANDLE_INIT(hkey);
1811 mutex_init(&hotkey_mutex);
1813 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1814 mutex_init(&hotkey_thread_mutex);
1815 mutex_init(&hotkey_thread_data_mutex);
1818 /* hotkey not supported on 570 */
1819 tp_features.hotkey = hkey_handle != NULL;
1821 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1822 str_supported(tp_features.hotkey));
1824 if (tp_features.hotkey) {
1825 hotkey_dev_attributes = create_attr_set(10, NULL);
1826 if (!hotkey_dev_attributes)
1828 res = add_many_to_attr_set(hotkey_dev_attributes,
1830 ARRAY_SIZE(hotkey_attributes));
1834 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1835 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1836 for HKEY interface version 0x100 */
1837 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1838 if ((hkeyv >> 8) != 1) {
1839 printk(TPACPI_ERR "unknown version of the "
1840 "HKEY interface: 0x%x\n", hkeyv);
1841 printk(TPACPI_ERR "please report this to %s\n",
1845 * MHKV 0x100 in A31, R40, R40e,
1846 * T4x, X31, and later
1848 tp_features.hotkey_mask = 1;
1852 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1853 str_supported(tp_features.hotkey_mask));
1855 if (tp_features.hotkey_mask) {
1856 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1859 "missing MHKA handler, "
1860 "please report this to %s\n",
1862 /* FN+F12, FN+F4, FN+F3 */
1863 hotkey_all_mask = 0x080cU;
1867 /* hotkey_source_mask *must* be zero for
1868 * the first hotkey_mask_get */
1869 res = hotkey_status_get(&hotkey_orig_status);
1870 if (!res && tp_features.hotkey_mask) {
1871 res = hotkey_mask_get();
1872 hotkey_orig_mask = hotkey_mask;
1874 res = add_many_to_attr_set(
1875 hotkey_dev_attributes,
1876 hotkey_mask_attributes,
1877 ARRAY_SIZE(hotkey_mask_attributes));
1881 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1882 if (tp_features.hotkey_mask) {
1883 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1886 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1889 vdbg_printk(TPACPI_DBG_INIT,
1890 "hotkey source mask 0x%08x, polling freq %d\n",
1891 hotkey_source_mask, hotkey_poll_freq);
1894 /* Not all thinkpads have a hardware radio switch */
1895 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1896 tp_features.hotkey_wlsw = 1;
1898 "radio switch found; radios are %s\n",
1899 enabled(status, 0));
1900 res = add_to_attr_set(hotkey_dev_attributes,
1901 &dev_attr_hotkey_radio_sw.attr);
1905 res = register_attr_set_with_sysfs(
1906 hotkey_dev_attributes,
1907 &tpacpi_pdev->dev.kobj);
1911 /* Set up key map */
1913 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1915 if (!hotkey_keycode_map) {
1917 "failed to allocate memory for key map\n");
1921 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1922 dbg_printk(TPACPI_DBG_INIT,
1923 "using Lenovo default hot key map\n");
1924 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1925 TPACPI_HOTKEY_MAP_SIZE);
1927 dbg_printk(TPACPI_DBG_INIT,
1928 "using IBM default hot key map\n");
1929 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1930 TPACPI_HOTKEY_MAP_SIZE);
1933 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1934 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1935 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1936 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1937 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1938 tpacpi_inputdev->keycode = hotkey_keycode_map;
1939 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1940 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1941 set_bit(hotkey_keycode_map[i],
1942 tpacpi_inputdev->keybit);
1944 if (i < sizeof(hotkey_reserved_mask)*8)
1945 hotkey_reserved_mask |= 1 << i;
1949 if (tp_features.hotkey_wlsw) {
1950 set_bit(EV_SW, tpacpi_inputdev->evbit);
1951 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1954 dbg_printk(TPACPI_DBG_INIT,
1955 "enabling hot key handling\n");
1956 res = hotkey_status_set(1);
1959 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1960 & ~hotkey_reserved_mask)
1961 | hotkey_orig_mask);
1962 if (res < 0 && res != -ENXIO)
1965 dbg_printk(TPACPI_DBG_INIT,
1966 "legacy hot key reporting over procfs %s\n",
1967 (hotkey_report_mode < 2) ?
1968 "enabled" : "disabled");
1970 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971 tpacpi_inputdev->open = &hotkey_inputdev_open;
1972 tpacpi_inputdev->close = &hotkey_inputdev_close;
1974 hotkey_poll_setup_safe(1);
1978 return (tp_features.hotkey)? 0 : 1;
1981 static void hotkey_exit(void)
1983 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1984 hotkey_poll_stop_sync();
1987 if (tp_features.hotkey) {
1988 dbg_printk(TPACPI_DBG_EXIT,
1989 "restoring original hot key mask\n");
1990 /* no short-circuit boolean operator below! */
1991 if ((hotkey_mask_set(hotkey_orig_mask) |
1992 hotkey_status_set(hotkey_orig_status)) != 0)
1994 "failed to restore hot key mask "
1995 "to BIOS defaults\n");
1998 if (hotkey_dev_attributes) {
1999 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2000 hotkey_dev_attributes = NULL;
2004 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2007 unsigned int scancode;
2011 if (event != 0x80) {
2013 "unknown HKEY notification event %d\n", event);
2014 /* forward it to userspace, maybe it knows how to handle it */
2015 acpi_bus_generate_netlink_event(
2016 ibm->acpi->device->pnp.device_class,
2017 ibm->acpi->device->dev.bus_id,
2023 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2024 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2036 switch (hkey >> 12) {
2038 /* 0x1000-0x1FFF: key presses */
2039 scancode = hkey & 0xfff;
2040 if (scancode > 0 && scancode < 0x21) {
2042 if (!(hotkey_source_mask & (1 << scancode))) {
2043 tpacpi_input_send_key(scancode);
2049 "hotkey 0x%04x out of range "
2050 "for keyboard map\n", hkey);
2055 /* 0x5000-0x5FFF: LID */
2056 /* we don't handle it through this path, just
2057 * eat up known LID events */
2058 if (hkey != 0x5001 && hkey != 0x5002) {
2060 "unknown LID-related HKEY event: "
2068 /* 0x7000-0x7FFF: misc */
2069 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2070 tpacpi_input_send_radiosw();
2073 /* fallthrough to default */
2075 /* case 2: dock-related */
2076 /* 0x2305 - T43 waking up due to bay lever
2077 * eject while aslept */
2078 /* case 3: ultra-bay related. maybe bay in dock? */
2079 /* 0x3003 - T43 after wake up by bay lever
2081 printk(TPACPI_NOTICE
2082 "unhandled HKEY event 0x%04x\n", hkey);
2087 if (!ignore_acpi_ev &&
2088 (send_acpi_ev || hotkey_report_mode < 2)) {
2089 acpi_bus_generate_proc_event(ibm->acpi->device,
2093 /* netlink events */
2094 if (!ignore_acpi_ev && send_acpi_ev) {
2095 acpi_bus_generate_netlink_event(
2096 ibm->acpi->device->pnp.device_class,
2097 ibm->acpi->device->dev.bus_id,
2103 static void hotkey_resume(void)
2105 if (hotkey_mask_get())
2107 "error while trying to read hot key mask "
2109 tpacpi_input_send_radiosw();
2110 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2111 hotkey_poll_setup_safe(0);
2115 /* procfs -------------------------------------------------------------- */
2116 static int hotkey_read(char *p)
2121 if (!tp_features.hotkey) {
2122 len += sprintf(p + len, "status:\t\tnot supported\n");
2126 if (mutex_lock_interruptible(&hotkey_mutex))
2127 return -ERESTARTSYS;
2128 res = hotkey_status_get(&status);
2130 res = hotkey_mask_get();
2131 mutex_unlock(&hotkey_mutex);
2135 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2136 if (tp_features.hotkey_mask) {
2137 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2138 len += sprintf(p + len,
2139 "commands:\tenable, disable, reset, <mask>\n");
2141 len += sprintf(p + len, "mask:\t\tnot supported\n");
2142 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2148 static int hotkey_write(char *buf)
2154 if (!tp_features.hotkey)
2157 if (mutex_lock_interruptible(&hotkey_mutex))
2158 return -ERESTARTSYS;
2164 while ((cmd = next_cmd(&buf))) {
2165 if (strlencmp(cmd, "enable") == 0) {
2167 } else if (strlencmp(cmd, "disable") == 0) {
2169 } else if (strlencmp(cmd, "reset") == 0) {
2170 status = hotkey_orig_status;
2171 mask = hotkey_orig_mask;
2172 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2174 } else if (sscanf(cmd, "%x", &mask) == 1) {
2182 res = hotkey_status_set(status);
2184 if (!res && mask != hotkey_mask)
2185 res = hotkey_mask_set(mask);
2188 mutex_unlock(&hotkey_mutex);
2192 static const struct acpi_device_id ibm_htk_device_ids[] = {
2193 {TPACPI_ACPI_HKEY_HID, 0},
2197 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2198 .hid = ibm_htk_device_ids,
2199 .notify = hotkey_notify,
2200 .handle = &hkey_handle,
2201 .type = ACPI_DEVICE_NOTIFY,
2204 static struct ibm_struct hotkey_driver_data = {
2206 .read = hotkey_read,
2207 .write = hotkey_write,
2208 .exit = hotkey_exit,
2209 .resume = hotkey_resume,
2210 .acpi = &ibm_hotkey_acpidriver,
2213 /*************************************************************************
2214 * Bluetooth subdriver
2218 /* ACPI GBDC/SBDC bits */
2219 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2220 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2221 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2224 static int bluetooth_get_radiosw(void);
2225 static int bluetooth_set_radiosw(int radio_on);
2227 /* sysfs bluetooth enable ---------------------------------------------- */
2228 static ssize_t bluetooth_enable_show(struct device *dev,
2229 struct device_attribute *attr,
2234 status = bluetooth_get_radiosw();
2238 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2241 static ssize_t bluetooth_enable_store(struct device *dev,
2242 struct device_attribute *attr,
2243 const char *buf, size_t count)
2248 if (parse_strtoul(buf, 1, &t))
2251 res = bluetooth_set_radiosw(t);
2253 return (res) ? res : count;
2256 static struct device_attribute dev_attr_bluetooth_enable =
2257 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2258 bluetooth_enable_show, bluetooth_enable_store);
2260 /* --------------------------------------------------------------------- */
2262 static struct attribute *bluetooth_attributes[] = {
2263 &dev_attr_bluetooth_enable.attr,
2267 static const struct attribute_group bluetooth_attr_group = {
2268 .attrs = bluetooth_attributes,
2271 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2276 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2278 TPACPI_ACPIHANDLE_INIT(hkey);
2280 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2281 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2282 tp_features.bluetooth = hkey_handle &&
2283 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2285 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2286 str_supported(tp_features.bluetooth),
2289 if (tp_features.bluetooth) {
2290 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2291 /* no bluetooth hardware present in system */
2292 tp_features.bluetooth = 0;
2293 dbg_printk(TPACPI_DBG_INIT,
2294 "bluetooth hardware not installed\n");
2296 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2297 &bluetooth_attr_group);
2303 return (tp_features.bluetooth)? 0 : 1;
2306 static void bluetooth_exit(void)
2308 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2309 &bluetooth_attr_group);
2312 static int bluetooth_get_radiosw(void)
2316 if (!tp_features.bluetooth)
2319 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2322 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2325 static int bluetooth_set_radiosw(int radio_on)
2329 if (!tp_features.bluetooth)
2332 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2335 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2337 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2338 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2344 /* procfs -------------------------------------------------------------- */
2345 static int bluetooth_read(char *p)
2348 int status = bluetooth_get_radiosw();
2350 if (!tp_features.bluetooth)
2351 len += sprintf(p + len, "status:\t\tnot supported\n");
2353 len += sprintf(p + len, "status:\t\t%s\n",
2354 (status)? "enabled" : "disabled");
2355 len += sprintf(p + len, "commands:\tenable, disable\n");
2361 static int bluetooth_write(char *buf)
2365 if (!tp_features.bluetooth)
2368 while ((cmd = next_cmd(&buf))) {
2369 if (strlencmp(cmd, "enable") == 0) {
2370 bluetooth_set_radiosw(1);
2371 } else if (strlencmp(cmd, "disable") == 0) {
2372 bluetooth_set_radiosw(0);
2380 static struct ibm_struct bluetooth_driver_data = {
2381 .name = "bluetooth",
2382 .read = bluetooth_read,
2383 .write = bluetooth_write,
2384 .exit = bluetooth_exit,
2387 /*************************************************************************
2392 /* ACPI GWAN/SWAN bits */
2393 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2394 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2395 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2398 static int wan_get_radiosw(void);
2399 static int wan_set_radiosw(int radio_on);
2401 /* sysfs wan enable ---------------------------------------------------- */
2402 static ssize_t wan_enable_show(struct device *dev,
2403 struct device_attribute *attr,
2408 status = wan_get_radiosw();
2412 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2415 static ssize_t wan_enable_store(struct device *dev,
2416 struct device_attribute *attr,
2417 const char *buf, size_t count)
2422 if (parse_strtoul(buf, 1, &t))
2425 res = wan_set_radiosw(t);
2427 return (res) ? res : count;
2430 static struct device_attribute dev_attr_wan_enable =
2431 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2432 wan_enable_show, wan_enable_store);
2434 /* --------------------------------------------------------------------- */
2436 static struct attribute *wan_attributes[] = {
2437 &dev_attr_wan_enable.attr,
2441 static const struct attribute_group wan_attr_group = {
2442 .attrs = wan_attributes,
2445 static int __init wan_init(struct ibm_init_struct *iibm)
2450 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2452 TPACPI_ACPIHANDLE_INIT(hkey);
2454 tp_features.wan = hkey_handle &&
2455 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2457 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2458 str_supported(tp_features.wan),
2461 if (tp_features.wan) {
2462 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2463 /* no wan hardware present in system */
2464 tp_features.wan = 0;
2465 dbg_printk(TPACPI_DBG_INIT,
2466 "wan hardware not installed\n");
2468 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2475 return (tp_features.wan)? 0 : 1;
2478 static void wan_exit(void)
2480 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2484 static int wan_get_radiosw(void)
2488 if (!tp_features.wan)
2491 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2494 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2497 static int wan_set_radiosw(int radio_on)
2501 if (!tp_features.wan)
2504 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2507 status |= TP_ACPI_WANCARD_RADIOSSW;
2509 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2510 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2516 /* procfs -------------------------------------------------------------- */
2517 static int wan_read(char *p)
2520 int status = wan_get_radiosw();
2522 if (!tp_features.wan)
2523 len += sprintf(p + len, "status:\t\tnot supported\n");
2525 len += sprintf(p + len, "status:\t\t%s\n",
2526 (status)? "enabled" : "disabled");
2527 len += sprintf(p + len, "commands:\tenable, disable\n");
2533 static int wan_write(char *buf)
2537 if (!tp_features.wan)
2540 while ((cmd = next_cmd(&buf))) {
2541 if (strlencmp(cmd, "enable") == 0) {
2543 } else if (strlencmp(cmd, "disable") == 0) {
2552 static struct ibm_struct wan_driver_data = {
2557 .flags.experimental = 1,
2560 /*************************************************************************
2564 enum video_access_mode {
2565 TPACPI_VIDEO_NONE = 0,
2566 TPACPI_VIDEO_570, /* 570 */
2567 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2568 TPACPI_VIDEO_NEW, /* all others */
2571 enum { /* video status flags, based on VIDEO_570 */
2572 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2573 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2574 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2577 enum { /* TPACPI_VIDEO_570 constants */
2578 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2579 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2580 * video_status_flags */
2581 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2582 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2585 static enum video_access_mode video_supported;
2586 static int video_orig_autosw;
2588 static int video_autosw_get(void);
2589 static int video_autosw_set(int enable);
2591 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2592 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2593 "\\_SB.PCI0.VID0", /* 770e */
2594 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2595 "\\_SB.PCI0.AGP.VID", /* all others */
2598 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2600 static int __init video_init(struct ibm_init_struct *iibm)
2604 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2606 TPACPI_ACPIHANDLE_INIT(vid);
2607 TPACPI_ACPIHANDLE_INIT(vid2);
2609 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2610 /* G41, assume IVGA doesn't change */
2611 vid_handle = vid2_handle;
2614 /* video switching not supported on R30, R31 */
2615 video_supported = TPACPI_VIDEO_NONE;
2616 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2618 video_supported = TPACPI_VIDEO_570;
2619 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2620 /* 600e/x, 770e, 770x */
2621 video_supported = TPACPI_VIDEO_770;
2624 video_supported = TPACPI_VIDEO_NEW;
2626 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2627 str_supported(video_supported != TPACPI_VIDEO_NONE),
2630 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2633 static void video_exit(void)
2635 dbg_printk(TPACPI_DBG_EXIT,
2636 "restoring original video autoswitch mode\n");
2637 if (video_autosw_set(video_orig_autosw))
2638 printk(TPACPI_ERR "error while trying to restore original "
2639 "video autoswitch mode\n");
2642 static int video_outputsw_get(void)
2647 switch (video_supported) {
2648 case TPACPI_VIDEO_570:
2649 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2650 TP_ACPI_VIDEO_570_PHSCMD))
2652 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2654 case TPACPI_VIDEO_770:
2655 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2658 status |= TP_ACPI_VIDEO_S_LCD;
2659 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2662 status |= TP_ACPI_VIDEO_S_CRT;
2664 case TPACPI_VIDEO_NEW:
2665 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2666 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2669 status |= TP_ACPI_VIDEO_S_CRT;
2671 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2672 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2675 status |= TP_ACPI_VIDEO_S_LCD;
2676 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2679 status |= TP_ACPI_VIDEO_S_DVI;
2688 static int video_outputsw_set(int status)
2693 switch (video_supported) {
2694 case TPACPI_VIDEO_570:
2695 res = acpi_evalf(NULL, NULL,
2696 "\\_SB.PHS2", "vdd",
2697 TP_ACPI_VIDEO_570_PHS2CMD,
2698 status | TP_ACPI_VIDEO_570_PHS2SET);
2700 case TPACPI_VIDEO_770:
2701 autosw = video_autosw_get();
2705 res = video_autosw_set(1);
2708 res = acpi_evalf(vid_handle, NULL,
2709 "ASWT", "vdd", status * 0x100, 0);
2710 if (!autosw && video_autosw_set(autosw)) {
2712 "video auto-switch left enabled due to error\n");
2716 case TPACPI_VIDEO_NEW:
2717 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2718 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2724 return (res)? 0 : -EIO;
2727 static int video_autosw_get(void)
2731 switch (video_supported) {
2732 case TPACPI_VIDEO_570:
2733 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2736 case TPACPI_VIDEO_770:
2737 case TPACPI_VIDEO_NEW:
2738 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2748 static int video_autosw_set(int enable)
2750 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2755 static int video_outputsw_cycle(void)
2757 int autosw = video_autosw_get();
2763 switch (video_supported) {
2764 case TPACPI_VIDEO_570:
2765 res = video_autosw_set(1);
2768 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2770 case TPACPI_VIDEO_770:
2771 case TPACPI_VIDEO_NEW:
2772 res = video_autosw_set(1);
2775 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2780 if (!autosw && video_autosw_set(autosw)) {
2782 "video auto-switch left enabled due to error\n");
2786 return (res)? 0 : -EIO;
2789 static int video_expand_toggle(void)
2791 switch (video_supported) {
2792 case TPACPI_VIDEO_570:
2793 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2795 case TPACPI_VIDEO_770:
2796 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2798 case TPACPI_VIDEO_NEW:
2799 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2807 static int video_read(char *p)
2812 if (video_supported == TPACPI_VIDEO_NONE) {
2813 len += sprintf(p + len, "status:\t\tnot supported\n");
2817 status = video_outputsw_get();
2821 autosw = video_autosw_get();
2825 len += sprintf(p + len, "status:\t\tsupported\n");
2826 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2827 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2828 if (video_supported == TPACPI_VIDEO_NEW)
2829 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2830 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2831 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2832 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2833 if (video_supported == TPACPI_VIDEO_NEW)
2834 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2835 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2836 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2841 static int video_write(char *buf)
2844 int enable, disable, status;
2847 if (video_supported == TPACPI_VIDEO_NONE)
2853 while ((cmd = next_cmd(&buf))) {
2854 if (strlencmp(cmd, "lcd_enable") == 0) {
2855 enable |= TP_ACPI_VIDEO_S_LCD;
2856 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2857 disable |= TP_ACPI_VIDEO_S_LCD;
2858 } else if (strlencmp(cmd, "crt_enable") == 0) {
2859 enable |= TP_ACPI_VIDEO_S_CRT;
2860 } else if (strlencmp(cmd, "crt_disable") == 0) {
2861 disable |= TP_ACPI_VIDEO_S_CRT;
2862 } else if (video_supported == TPACPI_VIDEO_NEW &&
2863 strlencmp(cmd, "dvi_enable") == 0) {
2864 enable |= TP_ACPI_VIDEO_S_DVI;
2865 } else if (video_supported == TPACPI_VIDEO_NEW &&
2866 strlencmp(cmd, "dvi_disable") == 0) {
2867 disable |= TP_ACPI_VIDEO_S_DVI;
2868 } else if (strlencmp(cmd, "auto_enable") == 0) {
2869 res = video_autosw_set(1);
2872 } else if (strlencmp(cmd, "auto_disable") == 0) {
2873 res = video_autosw_set(0);
2876 } else if (strlencmp(cmd, "video_switch") == 0) {
2877 res = video_outputsw_cycle();
2880 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2881 res = video_expand_toggle();
2888 if (enable || disable) {
2889 status = video_outputsw_get();
2892 res = video_outputsw_set((status & ~disable) | enable);
2900 static struct ibm_struct video_driver_data = {
2903 .write = video_write,
2907 /*************************************************************************
2908 * Light (thinklight) subdriver
2911 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2912 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
2914 static int __init light_init(struct ibm_init_struct *iibm)
2916 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2918 TPACPI_ACPIHANDLE_INIT(ledb);
2919 TPACPI_ACPIHANDLE_INIT(lght);
2920 TPACPI_ACPIHANDLE_INIT(cmos);
2922 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2923 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2925 if (tp_features.light)
2926 /* light status not supported on
2927 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2928 tp_features.light_status =
2929 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2931 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2932 str_supported(tp_features.light));
2934 return (tp_features.light)? 0 : 1;
2937 static int light_read(char *p)
2942 if (!tp_features.light) {
2943 len += sprintf(p + len, "status:\t\tnot supported\n");
2944 } else if (!tp_features.light_status) {
2945 len += sprintf(p + len, "status:\t\tunknown\n");
2946 len += sprintf(p + len, "commands:\ton, off\n");
2948 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2950 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2951 len += sprintf(p + len, "commands:\ton, off\n");
2957 static int light_write(char *buf)
2959 int cmos_cmd, lght_cmd;
2963 if (!tp_features.light)
2966 while ((cmd = next_cmd(&buf))) {
2967 if (strlencmp(cmd, "on") == 0) {
2970 } else if (strlencmp(cmd, "off") == 0) {
2976 success = cmos_handle ?
2977 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2978 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2986 static struct ibm_struct light_driver_data = {
2989 .write = light_write,
2992 /*************************************************************************
2996 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2998 static void dock_notify(struct ibm_struct *ibm, u32 event);
2999 static int dock_read(char *p);
3000 static int dock_write(char *buf);
3002 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3003 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3004 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3005 "\\_SB.PCI.ISA.SLCE", /* 570 */
3006 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3008 /* don't list other alternatives as we install a notify handler on the 570 */
3009 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3011 static const struct acpi_device_id ibm_pci_device_ids[] = {
3012 {PCI_ROOT_HID_STRING, 0},
3016 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3018 .notify = dock_notify,
3019 .handle = &dock_handle,
3020 .type = ACPI_SYSTEM_NOTIFY,
3023 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3024 * We just use it to get notifications of dock hotplug
3025 * in very old thinkpads */
3026 .hid = ibm_pci_device_ids,
3027 .notify = dock_notify,
3028 .handle = &pci_handle,
3029 .type = ACPI_SYSTEM_NOTIFY,
3033 static struct ibm_struct dock_driver_data[2] = {
3037 .write = dock_write,
3038 .acpi = &ibm_dock_acpidriver[0],
3042 .acpi = &ibm_dock_acpidriver[1],
3046 #define dock_docked() (_sta(dock_handle) & 1)
3048 static int __init dock_init(struct ibm_init_struct *iibm)
3050 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3052 TPACPI_ACPIHANDLE_INIT(dock);
3054 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3055 str_supported(dock_handle != NULL));
3057 return (dock_handle)? 0 : 1;
3060 static int __init dock_init2(struct ibm_init_struct *iibm)
3064 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3066 if (dock_driver_data[0].flags.acpi_driver_registered &&
3067 dock_driver_data[0].flags.acpi_notify_installed) {
3068 TPACPI_ACPIHANDLE_INIT(pci);
3069 dock2_needed = (pci_handle != NULL);
3070 vdbg_printk(TPACPI_DBG_INIT,
3071 "dock PCI handler for the TP 570 is %s\n",
3072 str_supported(dock2_needed));
3074 vdbg_printk(TPACPI_DBG_INIT,
3075 "dock subdriver part 2 not required\n");
3079 return (dock2_needed)? 0 : 1;
3082 static void dock_notify(struct ibm_struct *ibm, u32 event)
3084 int docked = dock_docked();
3085 int pci = ibm->acpi->hid && ibm->acpi->device &&
3086 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3089 if (event == 1 && !pci) /* 570 */
3090 data = 1; /* button */
3091 else if (event == 1 && pci) /* 570 */
3092 data = 3; /* dock */
3093 else if (event == 3 && docked)
3094 data = 1; /* button */
3095 else if (event == 3 && !docked)
3096 data = 2; /* undock */
3097 else if (event == 0 && docked)
3098 data = 3; /* dock */
3100 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3101 event, _sta(dock_handle));
3102 data = 0; /* unknown */
3104 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3105 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3106 ibm->acpi->device->dev.bus_id,
3110 static int dock_read(char *p)
3113 int docked = dock_docked();
3116 len += sprintf(p + len, "status:\t\tnot supported\n");
3118 len += sprintf(p + len, "status:\t\tundocked\n");
3120 len += sprintf(p + len, "status:\t\tdocked\n");
3121 len += sprintf(p + len, "commands:\tdock, undock\n");
3127 static int dock_write(char *buf)
3134 while ((cmd = next_cmd(&buf))) {
3135 if (strlencmp(cmd, "undock") == 0) {
3136 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3137 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3139 } else if (strlencmp(cmd, "dock") == 0) {
3140 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3149 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3151 /*************************************************************************
3155 #ifdef CONFIG_THINKPAD_ACPI_BAY
3157 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3158 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3159 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3160 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3161 ); /* A21e, R30, R31 */
3162 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3163 "_EJ0", /* all others */
3164 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3165 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3166 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3168 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3172 static int __init bay_init(struct ibm_init_struct *iibm)
3174 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3176 TPACPI_ACPIHANDLE_INIT(bay);
3178 TPACPI_ACPIHANDLE_INIT(bay_ej);
3179 TPACPI_ACPIHANDLE_INIT(bay2);
3181 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3183 tp_features.bay_status = bay_handle &&
3184 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3185 tp_features.bay_status2 = bay2_handle &&
3186 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3188 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3189 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3190 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3191 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3193 vdbg_printk(TPACPI_DBG_INIT,
3194 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3195 str_supported(tp_features.bay_status),
3196 str_supported(tp_features.bay_eject),
3197 str_supported(tp_features.bay_status2),
3198 str_supported(tp_features.bay_eject2));
3200 return (tp_features.bay_status || tp_features.bay_eject ||
3201 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3204 static void bay_notify(struct ibm_struct *ibm, u32 event)
3206 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3207 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3208 ibm->acpi->device->dev.bus_id,
3212 #define bay_occupied(b) (_sta(b##_handle) & 1)
3214 static int bay_read(char *p)
3217 int occupied = bay_occupied(bay);
3218 int occupied2 = bay_occupied(bay2);
3221 len += sprintf(p + len, "status:\t\t%s\n",
3222 tp_features.bay_status ?
3223 (occupied ? "occupied" : "unoccupied") :
3225 if (tp_features.bay_status2)
3226 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3227 "occupied" : "unoccupied");
3229 eject = tp_features.bay_eject && occupied;
3230 eject2 = tp_features.bay_eject2 && occupied2;
3232 if (eject && eject2)
3233 len += sprintf(p + len, "commands:\teject, eject2\n");
3235 len += sprintf(p + len, "commands:\teject\n");
3237 len += sprintf(p + len, "commands:\teject2\n");
3242 static int bay_write(char *buf)
3246 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3249 while ((cmd = next_cmd(&buf))) {
3250 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3251 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3253 } else if (tp_features.bay_eject2 &&
3254 strlencmp(cmd, "eject2") == 0) {
3255 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3264 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3265 .notify = bay_notify,
3266 .handle = &bay_handle,
3267 .type = ACPI_SYSTEM_NOTIFY,
3270 static struct ibm_struct bay_driver_data = {
3274 .acpi = &ibm_bay_acpidriver,
3277 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3279 /*************************************************************************
3283 /* sysfs cmos_command -------------------------------------------------- */
3284 static ssize_t cmos_command_store(struct device *dev,
3285 struct device_attribute *attr,
3286 const char *buf, size_t count)
3288 unsigned long cmos_cmd;
3291 if (parse_strtoul(buf, 21, &cmos_cmd))
3294 res = issue_thinkpad_cmos_command(cmos_cmd);
3295 return (res)? res : count;
3298 static struct device_attribute dev_attr_cmos_command =
3299 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3301 /* --------------------------------------------------------------------- */
3303 static int __init cmos_init(struct ibm_init_struct *iibm)
3307 vdbg_printk(TPACPI_DBG_INIT,
3308 "initializing cmos commands subdriver\n");
3310 TPACPI_ACPIHANDLE_INIT(cmos);
3312 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3313 str_supported(cmos_handle != NULL));
3315 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3319 return (cmos_handle)? 0 : 1;
3322 static void cmos_exit(void)
3324 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3327 static int cmos_read(char *p)
3331 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3332 R30, R31, T20-22, X20-21 */
3334 len += sprintf(p + len, "status:\t\tnot supported\n");
3336 len += sprintf(p + len, "status:\t\tsupported\n");
3337 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3343 static int cmos_write(char *buf)
3348 while ((cmd = next_cmd(&buf))) {
3349 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3350 cmos_cmd >= 0 && cmos_cmd <= 21) {
3355 res = issue_thinkpad_cmos_command(cmos_cmd);
3363 static struct ibm_struct cmos_driver_data = {
3366 .write = cmos_write,
3370 /*************************************************************************
3374 enum led_access_mode {
3375 TPACPI_LED_NONE = 0,
3376 TPACPI_LED_570, /* 570 */
3377 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3378 TPACPI_LED_NEW, /* all others */
3381 enum { /* For TPACPI_LED_OLD */
3382 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3383 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3384 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3387 static enum led_access_mode led_supported;
3389 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3390 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3391 /* T20-22, X20-21 */
3392 "LED", /* all others */
3395 static int __init led_init(struct ibm_init_struct *iibm)
3397 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3399 TPACPI_ACPIHANDLE_INIT(led);
3402 /* led not supported on R30, R31 */
3403 led_supported = TPACPI_LED_NONE;
3404 else if (strlencmp(led_path, "SLED") == 0)
3406 led_supported = TPACPI_LED_570;
3407 else if (strlencmp(led_path, "SYSL") == 0)
3408 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3409 led_supported = TPACPI_LED_OLD;
3412 led_supported = TPACPI_LED_NEW;
3414 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3415 str_supported(led_supported), led_supported);
3417 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3420 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3422 static int led_read(char *p)
3426 if (!led_supported) {
3427 len += sprintf(p + len, "status:\t\tnot supported\n");
3430 len += sprintf(p + len, "status:\t\tsupported\n");
3432 if (led_supported == TPACPI_LED_570) {
3435 for (i = 0; i < 8; i++) {
3436 if (!acpi_evalf(ec_handle,
3437 &status, "GLED", "dd", 1 << i))
3439 len += sprintf(p + len, "%d:\t\t%s\n",
3440 i, led_status(status));
3444 len += sprintf(p + len, "commands:\t"
3445 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3450 /* off, on, blink */
3451 static const int led_sled_arg1[] = { 0, 1, 3 };
3452 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3453 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3454 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3456 static int led_write(char *buf)
3464 while ((cmd = next_cmd(&buf))) {
3465 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3468 if (strstr(cmd, "off")) {
3470 } else if (strstr(cmd, "on")) {
3472 } else if (strstr(cmd, "blink")) {
3477 if (led_supported == TPACPI_LED_570) {
3480 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3481 led, led_sled_arg1[ind]))
3483 } else if (led_supported == TPACPI_LED_OLD) {
3484 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3486 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3488 ret = ec_write(TPACPI_LED_EC_HLBL,
3489 led * led_exp_hlbl[ind]);
3491 ret = ec_write(TPACPI_LED_EC_HLCL,
3492 led * led_exp_hlcl[ind]);
3497 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3498 led, led_led_arg1[ind]))
3506 static struct ibm_struct led_driver_data = {
3512 /*************************************************************************
3516 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3518 static int __init beep_init(struct ibm_init_struct *iibm)
3520 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3522 TPACPI_ACPIHANDLE_INIT(beep);
3524 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3525 str_supported(beep_handle != NULL));
3527 return (beep_handle)? 0 : 1;
3530 static int beep_read(char *p)
3535 len += sprintf(p + len, "status:\t\tnot supported\n");
3537 len += sprintf(p + len, "status:\t\tsupported\n");
3538 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3544 static int beep_write(char *buf)
3552 while ((cmd = next_cmd(&buf))) {
3553 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3554 beep_cmd >= 0 && beep_cmd <= 17) {
3558 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3565 static struct ibm_struct beep_driver_data = {
3568 .write = beep_write,
3571 /*************************************************************************
3575 enum thermal_access_mode {
3576 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3577 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3578 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3579 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3580 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3583 enum { /* TPACPI_THERMAL_TPEC_* */
3584 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3585 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3586 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3589 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3590 struct ibm_thermal_sensors_struct {
3591 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3594 static enum thermal_access_mode thermal_read_mode;
3596 /* idx is zero-based */
3597 static int thermal_get_sensor(int idx, s32 *value)
3603 t = TP_EC_THERMAL_TMP0;
3605 switch (thermal_read_mode) {
3606 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3607 case TPACPI_THERMAL_TPEC_16:
3608 if (idx >= 8 && idx <= 15) {
3609 t = TP_EC_THERMAL_TMP8;
3614 case TPACPI_THERMAL_TPEC_8:
3616 if (!acpi_ec_read(t + idx, &tmp))
3618 *value = tmp * 1000;
3623 case TPACPI_THERMAL_ACPI_UPDT:
3625 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3626 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3628 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3630 *value = (t - 2732) * 100;
3635 case TPACPI_THERMAL_ACPI_TMP07:
3637 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3638 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3640 if (t > 127 || t < -127)
3641 t = TP_EC_THERMAL_TMP_NA;
3647 case TPACPI_THERMAL_NONE:
3655 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3666 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3669 for (i = 0 ; i < n; i++) {
3670 res = thermal_get_sensor(i, &s->temp[i]);
3678 /* sysfs temp##_input -------------------------------------------------- */
3680 static ssize_t thermal_temp_input_show(struct device *dev,
3681 struct device_attribute *attr,
3684 struct sensor_device_attribute *sensor_attr =
3685 to_sensor_dev_attr(attr);
3686 int idx = sensor_attr->index;
3690 res = thermal_get_sensor(idx, &value);
3693 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3696 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3699 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3700 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3701 thermal_temp_input_show, NULL, _idxB)
3703 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3704 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3705 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3706 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3707 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3708 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3709 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3710 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3711 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3712 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3713 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3714 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3715 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3716 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3717 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3718 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3719 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3722 #define THERMAL_ATTRS(X) \
3723 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3725 static struct attribute *thermal_temp_input_attr[] = {
3745 static const struct attribute_group thermal_temp_input16_group = {
3746 .attrs = thermal_temp_input_attr
3749 static const struct attribute_group thermal_temp_input8_group = {
3750 .attrs = &thermal_temp_input_attr[8]
3753 #undef THERMAL_SENSOR_ATTR_TEMP
3754 #undef THERMAL_ATTRS
3756 /* --------------------------------------------------------------------- */
3758 static int __init thermal_init(struct ibm_init_struct *iibm)
3765 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3767 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3769 if (thinkpad_id.ec_model) {
3771 * Direct EC access mode: sensors at registers
3772 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3773 * non-implemented, thermal sensors return 0x80 when
3778 for (i = 0; i < 8; i++) {
3779 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3785 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3793 /* This is sheer paranoia, but we handle it anyway */
3796 "ThinkPad ACPI EC access misbehaving, "
3797 "falling back to ACPI TMPx access "
3799 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3802 "ThinkPad ACPI EC access misbehaving, "
3803 "disabling thermal sensors access\n");
3804 thermal_read_mode = TPACPI_THERMAL_NONE;
3809 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3811 } else if (acpi_tmp7) {
3812 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3813 /* 600e/x, 770e, 770x */
3814 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3816 /* Standard ACPI TMPx access, max 8 sensors */
3817 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3820 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3821 thermal_read_mode = TPACPI_THERMAL_NONE;
3824 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3825 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3828 switch (thermal_read_mode) {
3829 case TPACPI_THERMAL_TPEC_16:
3830 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3831 &thermal_temp_input16_group);
3835 case TPACPI_THERMAL_TPEC_8:
3836 case TPACPI_THERMAL_ACPI_TMP07:
3837 case TPACPI_THERMAL_ACPI_UPDT:
3838 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3839 &thermal_temp_input8_group);
3843 case TPACPI_THERMAL_NONE:
3851 static void thermal_exit(void)
3853 switch (thermal_read_mode) {
3854 case TPACPI_THERMAL_TPEC_16:
3855 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3856 &thermal_temp_input16_group);
3858 case TPACPI_THERMAL_TPEC_8:
3859 case TPACPI_THERMAL_ACPI_TMP07:
3860 case TPACPI_THERMAL_ACPI_UPDT:
3861 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3862 &thermal_temp_input16_group);
3864 case TPACPI_THERMAL_NONE:
3870 static int thermal_read(char *p)
3874 struct ibm_thermal_sensors_struct t;
3876 n = thermal_get_sensors(&t);
3877 if (unlikely(n < 0))
3880 len += sprintf(p + len, "temperatures:\t");
3883 for (i = 0; i < (n - 1); i++)
3884 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3885 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3887 len += sprintf(p + len, "not supported\n");
3892 static struct ibm_struct thermal_driver_data = {
3894 .read = thermal_read,
3895 .exit = thermal_exit,
3898 /*************************************************************************
3902 static u8 ecdump_regs[256];
3904 static int ecdump_read(char *p)
3910 len += sprintf(p + len, "EC "
3911 " +00 +01 +02 +03 +04 +05 +06 +07"
3912 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3913 for (i = 0; i < 256; i += 16) {
3914 len += sprintf(p + len, "EC 0x%02x:", i);
3915 for (j = 0; j < 16; j++) {
3916 if (!acpi_ec_read(i + j, &v))
3918 if (v != ecdump_regs[i + j])
3919 len += sprintf(p + len, " *%02x", v);
3921 len += sprintf(p + len, " %02x", v);
3922 ecdump_regs[i + j] = v;
3924 len += sprintf(p + len, "\n");
3929 /* These are way too dangerous to advertise openly... */
3931 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3932 " (<offset> is 00-ff, <value> is 00-ff)\n");
3933 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3934 " (<offset> is 00-ff, <value> is 0-255)\n");
3939 static int ecdump_write(char *buf)
3944 while ((cmd = next_cmd(&buf))) {
3945 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3947 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3951 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3952 if (!acpi_ec_write(i, v))
3961 static struct ibm_struct ecdump_driver_data = {
3963 .read = ecdump_read,
3964 .write = ecdump_write,
3965 .flags.experimental = 1,
3968 /*************************************************************************
3969 * Backlight/brightness subdriver
3972 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3974 static struct backlight_device *ibm_backlight_device;
3975 static int brightness_offset = 0x31;
3976 static int brightness_mode;
3977 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3979 static struct mutex brightness_mutex;
3982 * ThinkPads can read brightness from two places: EC 0x31, or
3983 * CMOS NVRAM byte 0x5E, bits 0-3.
3985 static int brightness_get(struct backlight_device *bd)
3987 u8 lec = 0, lcmos = 0, level = 0;
3989 if (brightness_mode & 1) {
3990 if (!acpi_ec_read(brightness_offset, &lec))
3992 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3995 if (brightness_mode & 2) {
3996 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3997 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3998 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3999 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4003 if (brightness_mode == 3 && lec != lcmos) {
4005 "CMOS NVRAM (%u) and EC (%u) do not agree "
4006 "on display brightness level\n",
4007 (unsigned int) lcmos,
4008 (unsigned int) lec);
4015 /* May return EINTR which can always be mapped to ERESTARTSYS */
4016 static int brightness_set(int value)
4018 int cmos_cmd, inc, i, res;
4021 if (value > ((tp_features.bright_16levels)? 15 : 7))
4024 res = mutex_lock_interruptible(&brightness_mutex);
4028 current_value = brightness_get(NULL);
4029 if (current_value < 0) {
4030 res = current_value;
4034 cmos_cmd = value > current_value ?
4035 TP_CMOS_BRIGHTNESS_UP :
4036 TP_CMOS_BRIGHTNESS_DOWN;
4037 inc = (value > current_value)? 1 : -1;
4040 for (i = current_value; i != value; i += inc) {
4041 if ((brightness_mode & 2) &&
4042 issue_thinkpad_cmos_command(cmos_cmd)) {
4046 if ((brightness_mode & 1) &&
4047 !acpi_ec_write(brightness_offset, i + inc)) {
4054 mutex_unlock(&brightness_mutex);
4058 /* sysfs backlight class ----------------------------------------------- */
4060 static int brightness_update_status(struct backlight_device *bd)
4062 /* it is the backlight class's job (caller) to handle
4063 * EINTR and other errors properly */
4064 return brightness_set(
4065 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4066 bd->props.power == FB_BLANK_UNBLANK) ?
4067 bd->props.brightness : 0);
4070 static struct backlight_ops ibm_backlight_data = {
4071 .get_brightness = brightness_get,
4072 .update_status = brightness_update_status,
4075 /* --------------------------------------------------------------------- */
4077 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4079 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4080 union acpi_object *obj;
4083 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4084 obj = (union acpi_object *)buffer.pointer;
4085 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4086 printk(TPACPI_ERR "Unknown BCLL data, "
4087 "please report this to %s\n", TPACPI_MAIL);
4090 rc = obj->package.count;
4096 kfree(buffer.pointer);
4100 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4101 void *context, void **rv)
4103 char name[ACPI_PATH_SEGMENT_LENGTH];
4104 struct acpi_buffer buffer = { sizeof(name), &name };
4106 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4107 !strncmp("BCLL", name, sizeof(name) - 1)) {
4108 if (tpacpi_query_bcll_levels(handle) == 16) {
4110 return AE_CTRL_TERMINATE;
4119 static int __init brightness_check_levels(void)
4122 void *found_node = NULL;
4125 TPACPI_ACPIHANDLE_INIT(vid);
4130 /* Search for a BCLL package with 16 levels */
4131 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4132 brightness_find_bcll, NULL,
4135 return (ACPI_SUCCESS(status) && found_node != NULL);
4138 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4139 void *context, void **rv)
4141 char name[ACPI_PATH_SEGMENT_LENGTH];
4142 struct acpi_buffer buffer = { sizeof(name), &name };
4144 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4145 !strncmp("_BCL", name, sizeof(name) - 1)) {
4147 return AE_CTRL_TERMINATE;
4153 static int __init brightness_check_std_acpi_support(void)
4156 void *found_node = NULL;
4159 TPACPI_ACPIHANDLE_INIT(vid);
4164 /* Search for a _BCL method, but don't execute it */
4165 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4166 brightness_find_bcl, NULL, &found_node);
4168 return (ACPI_SUCCESS(status) && found_node != NULL);
4171 static int __init brightness_init(struct ibm_init_struct *iibm)
4175 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4177 mutex_init(&brightness_mutex);
4179 if (!brightness_enable) {
4180 dbg_printk(TPACPI_DBG_INIT,
4181 "brightness support disabled by "
4182 "module parameter\n");
4184 } else if (brightness_enable > 1) {
4185 if (brightness_check_std_acpi_support()) {
4186 printk(TPACPI_NOTICE
4187 "standard ACPI backlight interface "
4188 "available, not loading native one...\n");
4193 if (!brightness_mode) {
4194 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4195 brightness_mode = 2;
4197 brightness_mode = 3;
4199 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4203 if (brightness_mode > 3)
4206 tp_features.bright_16levels =
4207 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4208 brightness_check_levels();
4210 b = brightness_get(NULL);
4214 if (tp_features.bright_16levels)
4216 "detected a 16-level brightness capable ThinkPad\n");
4218 ibm_backlight_device = backlight_device_register(
4219 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4220 &ibm_backlight_data);
4221 if (IS_ERR(ibm_backlight_device)) {
4222 printk(TPACPI_ERR "Could not register backlight device\n");
4223 return PTR_ERR(ibm_backlight_device);
4225 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4227 ibm_backlight_device->props.max_brightness =
4228 (tp_features.bright_16levels)? 15 : 7;
4229 ibm_backlight_device->props.brightness = b;
4230 backlight_update_status(ibm_backlight_device);
4235 static void brightness_exit(void)
4237 if (ibm_backlight_device) {
4238 vdbg_printk(TPACPI_DBG_EXIT,
4239 "calling backlight_device_unregister()\n");
4240 backlight_device_unregister(ibm_backlight_device);
4241 ibm_backlight_device = NULL;
4245 static int brightness_read(char *p)
4250 level = brightness_get(NULL);
4252 len += sprintf(p + len, "level:\t\tunreadable\n");
4254 len += sprintf(p + len, "level:\t\t%d\n", level);
4255 len += sprintf(p + len, "commands:\tup, down\n");
4256 len += sprintf(p + len, "commands:\tlevel <level>"
4257 " (<level> is 0-%d)\n",
4258 (tp_features.bright_16levels) ? 15 : 7);
4264 static int brightness_write(char *buf)
4269 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4271 level = brightness_get(NULL);
4275 while ((cmd = next_cmd(&buf))) {
4276 if (strlencmp(cmd, "up") == 0) {
4277 if (level < max_level)
4279 } else if (strlencmp(cmd, "down") == 0) {
4282 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4283 level >= 0 && level <= max_level) {
4290 * Now we know what the final level should be, so we try to set it.
4291 * Doing it this way makes the syscall restartable in case of EINTR
4293 rc = brightness_set(level);
4294 return (rc == -EINTR)? ERESTARTSYS : rc;
4297 static struct ibm_struct brightness_driver_data = {
4298 .name = "brightness",
4299 .read = brightness_read,
4300 .write = brightness_write,
4301 .exit = brightness_exit,
4304 /*************************************************************************
4308 static int volume_offset = 0x30;
4310 static int volume_read(char *p)
4315 if (!acpi_ec_read(volume_offset, &level)) {
4316 len += sprintf(p + len, "level:\t\tunreadable\n");
4318 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4319 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4320 len += sprintf(p + len, "commands:\tup, down, mute\n");
4321 len += sprintf(p + len, "commands:\tlevel <level>"
4322 " (<level> is 0-15)\n");
4328 static int volume_write(char *buf)
4330 int cmos_cmd, inc, i;
4332 int new_level, new_mute;
4335 while ((cmd = next_cmd(&buf))) {
4336 if (!acpi_ec_read(volume_offset, &level))
4338 new_mute = mute = level & 0x40;
4339 new_level = level = level & 0xf;
4341 if (strlencmp(cmd, "up") == 0) {
4345 new_level = level == 15 ? 15 : level + 1;
4346 } else if (strlencmp(cmd, "down") == 0) {
4350 new_level = level == 0 ? 0 : level - 1;
4351 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4352 new_level >= 0 && new_level <= 15) {
4354 } else if (strlencmp(cmd, "mute") == 0) {
4359 if (new_level != level) {
4360 /* mute doesn't change */
4362 cmos_cmd = (new_level > level) ?
4363 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4364 inc = new_level > level ? 1 : -1;
4366 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4367 !acpi_ec_write(volume_offset, level)))
4370 for (i = level; i != new_level; i += inc)
4371 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4372 !acpi_ec_write(volume_offset, i + inc))
4376 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4377 !acpi_ec_write(volume_offset, new_level + mute))) {
4382 if (new_mute != mute) {
4383 /* level doesn't change */
4385 cmos_cmd = (new_mute) ?
4386 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4388 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4389 !acpi_ec_write(volume_offset, level + new_mute))
4397 static struct ibm_struct volume_driver_data = {
4399 .read = volume_read,
4400 .write = volume_write,
4403 /*************************************************************************
4410 * TPACPI_FAN_RD_ACPI_GFAN:
4411 * ACPI GFAN method: returns fan level
4413 * see TPACPI_FAN_WR_ACPI_SFAN
4414 * EC 0x2f (HFSP) not available if GFAN exists
4416 * TPACPI_FAN_WR_ACPI_SFAN:
4417 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4419 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4422 * TPACPI_FAN_WR_TPEC:
4423 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4424 * Supported on almost all ThinkPads
4426 * Fan speed changes of any sort (including those caused by the
4427 * disengaged mode) are usually done slowly by the firmware as the
4428 * maximum ammount of fan duty cycle change per second seems to be
4431 * Reading is not available if GFAN exists.
4432 * Writing is not available if SFAN exists.
4435 * 7 automatic mode engaged;
4436 * (default operation mode of the ThinkPad)
4437 * fan level is ignored in this mode.
4438 * 6 full speed mode (takes precedence over bit 7);
4439 * not available on all thinkpads. May disable
4440 * the tachometer while the fan controller ramps up
4441 * the speed (which can take up to a few *minutes*).
4442 * Speeds up fan to 100% duty-cycle, which is far above
4443 * the standard RPM levels. It is not impossible that
4444 * it could cause hardware damage.
4445 * 5-3 unused in some models. Extra bits for fan level
4446 * in others, but still useless as all values above
4447 * 7 map to the same speed as level 7 in these models.
4448 * 2-0 fan level (0..7 usually)
4450 * 0x07 = max (set when temperatures critical)
4451 * Some ThinkPads may have other levels, see
4452 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4454 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4455 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4456 * does so, its initial value is meaningless (0x07).
4458 * For firmware bugs, refer to:
4459 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4463 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4464 * Main fan tachometer reading (in RPM)
4466 * This register is present on all ThinkPads with a new-style EC, and
4467 * it is known not to be present on the A21m/e, and T22, as there is
4468 * something else in offset 0x84 according to the ACPI DSDT. Other
4469 * ThinkPads from this same time period (and earlier) probably lack the
4470 * tachometer as well.
4472 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4473 * was never fixed by IBM to report the EC firmware version string
4474 * probably support the tachometer (like the early X models), so
4475 * detecting it is quite hard. We need more data to know for sure.
4477 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4480 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4483 * For firmware bugs, refer to:
4484 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4486 * TPACPI_FAN_WR_ACPI_FANS:
4487 * ThinkPad X31, X40, X41. Not available in the X60.
4489 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4490 * high speed. ACPI DSDT seems to map these three speeds to levels
4491 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4492 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4494 * The speeds are stored on handles
4495 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4497 * There are three default speed sets, acessible as handles:
4498 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4500 * ACPI DSDT switches which set is in use depending on various
4503 * TPACPI_FAN_WR_TPEC is also available and should be used to
4504 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4505 * but the ACPI tables just mention level 7.
4508 enum { /* Fan control constants */
4509 fan_status_offset = 0x2f, /* EC register 0x2f */
4510 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4511 * 0x84 must be read before 0x85 */
4513 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4514 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4516 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4519 enum fan_status_access_mode {
4520 TPACPI_FAN_NONE = 0, /* No fan status or control */
4521 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4522 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4525 enum fan_control_access_mode {
4526 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4527 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4528 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4529 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4532 enum fan_control_commands {
4533 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4534 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4535 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4536 * and also watchdog cmd */
4539 static int fan_control_allowed;
4541 static enum fan_status_access_mode fan_status_access_mode;
4542 static enum fan_control_access_mode fan_control_access_mode;
4543 static enum fan_control_commands fan_control_commands;
4545 static u8 fan_control_initial_status;
4546 static u8 fan_control_desired_level;
4547 static int fan_watchdog_maxinterval;
4549 static struct mutex fan_mutex;
4551 static void fan_watchdog_fire(struct work_struct *ignored);
4552 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4554 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4555 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4556 "\\FSPD", /* 600e/x, 770e, 770x */
4558 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4559 "JFNS", /* 770x-JL */
4563 * Call with fan_mutex held
4565 static void fan_update_desired_level(u8 status)
4568 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4570 fan_control_desired_level = 7;
4572 fan_control_desired_level = status;
4576 static int fan_get_status(u8 *status)
4581 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4583 switch (fan_status_access_mode) {
4584 case TPACPI_FAN_RD_ACPI_GFAN:
4585 /* 570, 600e/x, 770e, 770x */
4587 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4595 case TPACPI_FAN_RD_TPEC:
4596 /* all except 570, 600e/x, 770e, 770x */
4597 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4612 static int fan_get_status_safe(u8 *status)
4617 if (mutex_lock_interruptible(&fan_mutex))
4618 return -ERESTARTSYS;
4619 rc = fan_get_status(&s);
4621 fan_update_desired_level(s);
4622 mutex_unlock(&fan_mutex);
4630 static int fan_get_speed(unsigned int *speed)
4634 switch (fan_status_access_mode) {
4635 case TPACPI_FAN_RD_TPEC:
4636 /* all except 570, 600e/x, 770e, 770x */
4637 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4638 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4642 *speed = (hi << 8) | lo;
4653 static int fan_set_level(int level)
4655 if (!fan_control_allowed)
4658 switch (fan_control_access_mode) {
4659 case TPACPI_FAN_WR_ACPI_SFAN:
4660 if (level >= 0 && level <= 7) {
4661 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4667 case TPACPI_FAN_WR_ACPI_FANS:
4668 case TPACPI_FAN_WR_TPEC:
4669 if ((level != TP_EC_FAN_AUTO) &&
4670 (level != TP_EC_FAN_FULLSPEED) &&
4671 ((level < 0) || (level > 7)))
4674 /* safety net should the EC not support AUTO
4675 * or FULLSPEED mode bits and just ignore them */
4676 if (level & TP_EC_FAN_FULLSPEED)
4677 level |= 7; /* safety min speed 7 */
4678 else if (level & TP_EC_FAN_FULLSPEED)
4679 level |= 4; /* safety min speed 4 */
4681 if (!acpi_ec_write(fan_status_offset, level))
4684 tp_features.fan_ctrl_status_undef = 0;
4693 static int fan_set_level_safe(int level)
4697 if (!fan_control_allowed)
4700 if (mutex_lock_interruptible(&fan_mutex))
4701 return -ERESTARTSYS;
4703 if (level == TPACPI_FAN_LAST_LEVEL)
4704 level = fan_control_desired_level;
4706 rc = fan_set_level(level);
4708 fan_update_desired_level(level);
4710 mutex_unlock(&fan_mutex);
4714 static int fan_set_enable(void)
4719 if (!fan_control_allowed)
4722 if (mutex_lock_interruptible(&fan_mutex))
4723 return -ERESTARTSYS;
4725 switch (fan_control_access_mode) {
4726 case TPACPI_FAN_WR_ACPI_FANS:
4727 case TPACPI_FAN_WR_TPEC:
4728 rc = fan_get_status(&s);
4732 /* Don't go out of emergency fan mode */
4735 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4738 if (!acpi_ec_write(fan_status_offset, s))
4741 tp_features.fan_ctrl_status_undef = 0;
4746 case TPACPI_FAN_WR_ACPI_SFAN:
4747 rc = fan_get_status(&s);
4753 /* Set fan to at least level 4 */
4756 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4766 mutex_unlock(&fan_mutex);
4770 static int fan_set_disable(void)
4774 if (!fan_control_allowed)
4777 if (mutex_lock_interruptible(&fan_mutex))
4778 return -ERESTARTSYS;
4781 switch (fan_control_access_mode) {
4782 case TPACPI_FAN_WR_ACPI_FANS:
4783 case TPACPI_FAN_WR_TPEC:
4784 if (!acpi_ec_write(fan_status_offset, 0x00))
4787 fan_control_desired_level = 0;
4788 tp_features.fan_ctrl_status_undef = 0;
4792 case TPACPI_FAN_WR_ACPI_SFAN:
4793 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4796 fan_control_desired_level = 0;
4804 mutex_unlock(&fan_mutex);
4808 static int fan_set_speed(int speed)
4812 if (!fan_control_allowed)
4815 if (mutex_lock_interruptible(&fan_mutex))
4816 return -ERESTARTSYS;
4819 switch (fan_control_access_mode) {
4820 case TPACPI_FAN_WR_ACPI_FANS:
4821 if (speed >= 0 && speed <= 65535) {
4822 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4823 speed, speed, speed))
4833 mutex_unlock(&fan_mutex);
4837 static void fan_watchdog_reset(void)
4839 static int fan_watchdog_active;
4841 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4844 if (fan_watchdog_active)
4845 cancel_delayed_work(&fan_watchdog_task);
4847 if (fan_watchdog_maxinterval > 0 &&
4848 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4849 fan_watchdog_active = 1;
4850 if (!schedule_delayed_work(&fan_watchdog_task,
4851 msecs_to_jiffies(fan_watchdog_maxinterval
4854 "failed to schedule the fan watchdog, "
4855 "watchdog will not trigger\n");
4858 fan_watchdog_active = 0;
4861 static void fan_watchdog_fire(struct work_struct *ignored)
4865 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4868 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4869 rc = fan_set_enable();
4871 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4872 "will try again later...\n", -rc);
4873 /* reschedule for later */
4874 fan_watchdog_reset();
4879 * SYSFS fan layout: hwmon compatible (device)
4882 * 0: "disengaged" mode
4884 * 2: native EC "auto" mode (recommended, hardware default)
4886 * pwm*: set speed in manual mode, ignored otherwise.
4887 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4890 * fan*_input: tachometer reading, RPM
4893 * SYSFS fan layout: extensions
4895 * fan_watchdog (driver):
4896 * fan watchdog interval in seconds, 0 disables (default), max 120
4899 /* sysfs fan pwm1_enable ----------------------------------------------- */
4900 static ssize_t fan_pwm1_enable_show(struct device *dev,
4901 struct device_attribute *attr,
4907 res = fan_get_status_safe(&status);
4911 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4912 if (status != fan_control_initial_status) {
4913 tp_features.fan_ctrl_status_undef = 0;
4915 /* Return most likely status. In fact, it
4916 * might be the only possible status */
4917 status = TP_EC_FAN_AUTO;
4921 if (status & TP_EC_FAN_FULLSPEED) {
4923 } else if (status & TP_EC_FAN_AUTO) {
4928 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4931 static ssize_t fan_pwm1_enable_store(struct device *dev,
4932 struct device_attribute *attr,
4933 const char *buf, size_t count)
4938 if (parse_strtoul(buf, 2, &t))
4943 level = TP_EC_FAN_FULLSPEED;
4946 level = TPACPI_FAN_LAST_LEVEL;
4949 level = TP_EC_FAN_AUTO;
4952 /* reserved for software-controlled auto mode */
4958 res = fan_set_level_safe(level);
4964 fan_watchdog_reset();
4969 static struct device_attribute dev_attr_fan_pwm1_enable =
4970 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4971 fan_pwm1_enable_show, fan_pwm1_enable_store);
4973 /* sysfs fan pwm1 ------------------------------------------------------ */
4974 static ssize_t fan_pwm1_show(struct device *dev,
4975 struct device_attribute *attr,
4981 res = fan_get_status_safe(&status);
4985 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4986 if (status != fan_control_initial_status) {
4987 tp_features.fan_ctrl_status_undef = 0;
4989 status = TP_EC_FAN_AUTO;
4994 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4995 status = fan_control_desired_level;
5000 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5003 static ssize_t fan_pwm1_store(struct device *dev,
5004 struct device_attribute *attr,
5005 const char *buf, size_t count)
5009 u8 status, newlevel;
5011 if (parse_strtoul(buf, 255, &s))
5014 /* scale down from 0-255 to 0-7 */
5015 newlevel = (s >> 5) & 0x07;
5017 if (mutex_lock_interruptible(&fan_mutex))
5018 return -ERESTARTSYS;
5020 rc = fan_get_status(&status);
5021 if (!rc && (status &
5022 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5023 rc = fan_set_level(newlevel);
5027 fan_update_desired_level(newlevel);
5028 fan_watchdog_reset();
5032 mutex_unlock(&fan_mutex);
5033 return (rc)? rc : count;
5036 static struct device_attribute dev_attr_fan_pwm1 =
5037 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5038 fan_pwm1_show, fan_pwm1_store);
5040 /* sysfs fan fan1_input ------------------------------------------------ */
5041 static ssize_t fan_fan1_input_show(struct device *dev,
5042 struct device_attribute *attr,
5048 res = fan_get_speed(&speed);
5052 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5055 static struct device_attribute dev_attr_fan_fan1_input =
5056 __ATTR(fan1_input, S_IRUGO,
5057 fan_fan1_input_show, NULL);
5059 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5060 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5063 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5066 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5067 const char *buf, size_t count)
5071 if (parse_strtoul(buf, 120, &t))
5074 if (!fan_control_allowed)
5077 fan_watchdog_maxinterval = t;
5078 fan_watchdog_reset();
5083 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5084 fan_fan_watchdog_show, fan_fan_watchdog_store);
5086 /* --------------------------------------------------------------------- */
5087 static struct attribute *fan_attributes[] = {
5088 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5089 &dev_attr_fan_fan1_input.attr,
5093 static const struct attribute_group fan_attr_group = {
5094 .attrs = fan_attributes,
5097 static int __init fan_init(struct ibm_init_struct *iibm)
5101 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5103 mutex_init(&fan_mutex);
5104 fan_status_access_mode = TPACPI_FAN_NONE;
5105 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5106 fan_control_commands = 0;
5107 fan_watchdog_maxinterval = 0;
5108 tp_features.fan_ctrl_status_undef = 0;
5109 fan_control_desired_level = 7;
5111 TPACPI_ACPIHANDLE_INIT(fans);
5112 TPACPI_ACPIHANDLE_INIT(gfan);
5113 TPACPI_ACPIHANDLE_INIT(sfan);
5116 /* 570, 600e/x, 770e, 770x */
5117 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5119 /* all other ThinkPads: note that even old-style
5120 * ThinkPad ECs supports the fan control register */
5121 if (likely(acpi_ec_read(fan_status_offset,
5122 &fan_control_initial_status))) {
5123 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5125 /* In some ThinkPads, neither the EC nor the ACPI
5126 * DSDT initialize the fan status, and it ends up
5127 * being set to 0x07 when it *could* be either
5130 * Enable for TP-1Y (T43), TP-78 (R51e),
5131 * TP-76 (R52), TP-70 (T43, R52), which are known
5133 if (fan_control_initial_status == 0x07) {
5134 switch (thinkpad_id.ec_model) {
5135 case 0x5931: /* TP-1Y */
5136 case 0x3837: /* TP-78 */
5137 case 0x3637: /* TP-76 */
5138 case 0x3037: /* TP-70 */
5139 printk(TPACPI_NOTICE
5140 "fan_init: initial fan status "
5141 "is unknown, assuming it is "
5143 tp_features.fan_ctrl_status_undef = 1;
5149 "ThinkPad ACPI EC access misbehaving, "
5150 "fan status and control unavailable\n");
5157 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5158 fan_control_commands |=
5159 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5162 /* gfan without sfan means no fan control */
5163 /* all other models implement TP EC 0x2f control */
5167 fan_control_access_mode =
5168 TPACPI_FAN_WR_ACPI_FANS;
5169 fan_control_commands |=
5170 TPACPI_FAN_CMD_SPEED |
5171 TPACPI_FAN_CMD_LEVEL |
5172 TPACPI_FAN_CMD_ENABLE;
5174 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5175 fan_control_commands |=
5176 TPACPI_FAN_CMD_LEVEL |
5177 TPACPI_FAN_CMD_ENABLE;
5182 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5183 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5184 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5185 fan_status_access_mode, fan_control_access_mode);
5187 /* fan control master switch */
5188 if (!fan_control_allowed) {
5189 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5190 fan_control_commands = 0;
5191 dbg_printk(TPACPI_DBG_INIT,
5192 "fan control features disabled by parameter\n");
5195 /* update fan_control_desired_level */
5196 if (fan_status_access_mode != TPACPI_FAN_NONE)
5197 fan_get_status_safe(NULL);
5199 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5200 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5201 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5204 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5205 &driver_attr_fan_watchdog);
5213 static void fan_exit(void)
5215 vdbg_printk(TPACPI_DBG_EXIT,
5216 "cancelling any pending fan watchdog tasks\n");
5218 /* FIXME: can we really do this unconditionally? */
5219 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5220 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5221 &driver_attr_fan_watchdog);
5223 cancel_delayed_work(&fan_watchdog_task);
5224 flush_scheduled_work();
5227 static int fan_read(char *p)
5232 unsigned int speed = 0;
5234 switch (fan_status_access_mode) {
5235 case TPACPI_FAN_RD_ACPI_GFAN:
5236 /* 570, 600e/x, 770e, 770x */
5237 rc = fan_get_status_safe(&status);
5241 len += sprintf(p + len, "status:\t\t%s\n"
5243 (status != 0) ? "enabled" : "disabled", status);
5246 case TPACPI_FAN_RD_TPEC:
5247 /* all except 570, 600e/x, 770e, 770x */
5248 rc = fan_get_status_safe(&status);
5252 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5253 if (status != fan_control_initial_status)
5254 tp_features.fan_ctrl_status_undef = 0;
5256 /* Return most likely status. In fact, it
5257 * might be the only possible status */
5258 status = TP_EC_FAN_AUTO;
5261 len += sprintf(p + len, "status:\t\t%s\n",
5262 (status != 0) ? "enabled" : "disabled");
5264 rc = fan_get_speed(&speed);
5268 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5270 if (status & TP_EC_FAN_FULLSPEED)
5271 /* Disengaged mode takes precedence */
5272 len += sprintf(p + len, "level:\t\tdisengaged\n");
5273 else if (status & TP_EC_FAN_AUTO)
5274 len += sprintf(p + len, "level:\t\tauto\n");
5276 len += sprintf(p + len, "level:\t\t%d\n", status);
5279 case TPACPI_FAN_NONE:
5281 len += sprintf(p + len, "status:\t\tnot supported\n");
5284 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5285 len += sprintf(p + len, "commands:\tlevel <level>");
5287 switch (fan_control_access_mode) {
5288 case TPACPI_FAN_WR_ACPI_SFAN:
5289 len += sprintf(p + len, " (<level> is 0-7)\n");
5293 len += sprintf(p + len, " (<level> is 0-7, "
5294 "auto, disengaged, full-speed)\n");
5299 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5300 len += sprintf(p + len, "commands:\tenable, disable\n"
5301 "commands:\twatchdog <timeout> (<timeout> "
5302 "is 0 (off), 1-120 (seconds))\n");
5304 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5305 len += sprintf(p + len, "commands:\tspeed <speed>"
5306 " (<speed> is 0-65535)\n");
5311 static int fan_write_cmd_level(const char *cmd, int *rc)
5315 if (strlencmp(cmd, "level auto") == 0)
5316 level = TP_EC_FAN_AUTO;
5317 else if ((strlencmp(cmd, "level disengaged") == 0) |
5318 (strlencmp(cmd, "level full-speed") == 0))
5319 level = TP_EC_FAN_FULLSPEED;
5320 else if (sscanf(cmd, "level %d", &level) != 1)
5323 *rc = fan_set_level_safe(level);
5325 printk(TPACPI_ERR "level command accepted for unsupported "
5326 "access mode %d", fan_control_access_mode);
5331 static int fan_write_cmd_enable(const char *cmd, int *rc)
5333 if (strlencmp(cmd, "enable") != 0)
5336 *rc = fan_set_enable();
5338 printk(TPACPI_ERR "enable command accepted for unsupported "
5339 "access mode %d", fan_control_access_mode);
5344 static int fan_write_cmd_disable(const char *cmd, int *rc)
5346 if (strlencmp(cmd, "disable") != 0)
5349 *rc = fan_set_disable();
5351 printk(TPACPI_ERR "disable command accepted for unsupported "
5352 "access mode %d", fan_control_access_mode);
5357 static int fan_write_cmd_speed(const char *cmd, int *rc)
5362 * Support speed <low> <medium> <high> ? */
5364 if (sscanf(cmd, "speed %d", &speed) != 1)
5367 *rc = fan_set_speed(speed);
5369 printk(TPACPI_ERR "speed command accepted for unsupported "
5370 "access mode %d", fan_control_access_mode);
5375 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5379 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5382 if (interval < 0 || interval > 120)
5385 fan_watchdog_maxinterval = interval;
5390 static int fan_write(char *buf)
5395 while (!rc && (cmd = next_cmd(&buf))) {
5396 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5397 fan_write_cmd_level(cmd, &rc)) &&
5398 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5399 (fan_write_cmd_enable(cmd, &rc) ||
5400 fan_write_cmd_disable(cmd, &rc) ||
5401 fan_write_cmd_watchdog(cmd, &rc))) &&
5402 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5403 fan_write_cmd_speed(cmd, &rc))
5407 fan_watchdog_reset();
5413 static struct ibm_struct fan_driver_data = {
5420 /****************************************************************************
5421 ****************************************************************************
5425 ****************************************************************************
5426 ****************************************************************************/
5428 /* sysfs name ---------------------------------------------------------- */
5429 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5430 struct device_attribute *attr,
5433 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5436 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5437 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5439 /* --------------------------------------------------------------------- */
5442 static struct proc_dir_entry *proc_dir;
5445 * Module and infrastructure proble, init and exit handling
5448 static int force_load;
5450 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5451 static const char * __init str_supported(int is_supported)
5453 static char text_unsupported[] __initdata = "not supported";
5455 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5457 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5459 static void ibm_exit(struct ibm_struct *ibm)
5461 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5463 list_del_init(&ibm->all_drivers);
5465 if (ibm->flags.acpi_notify_installed) {
5466 dbg_printk(TPACPI_DBG_EXIT,
5467 "%s: acpi_remove_notify_handler\n", ibm->name);
5469 acpi_remove_notify_handler(*ibm->acpi->handle,
5471 dispatch_acpi_notify);
5472 ibm->flags.acpi_notify_installed = 0;
5473 ibm->flags.acpi_notify_installed = 0;
5476 if (ibm->flags.proc_created) {
5477 dbg_printk(TPACPI_DBG_EXIT,
5478 "%s: remove_proc_entry\n", ibm->name);
5479 remove_proc_entry(ibm->name, proc_dir);
5480 ibm->flags.proc_created = 0;
5483 if (ibm->flags.acpi_driver_registered) {
5484 dbg_printk(TPACPI_DBG_EXIT,
5485 "%s: acpi_bus_unregister_driver\n", ibm->name);
5487 acpi_bus_unregister_driver(ibm->acpi->driver);
5488 kfree(ibm->acpi->driver);
5489 ibm->acpi->driver = NULL;
5490 ibm->flags.acpi_driver_registered = 0;
5493 if (ibm->flags.init_called && ibm->exit) {
5495 ibm->flags.init_called = 0;
5498 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5501 static int __init ibm_init(struct ibm_init_struct *iibm)
5504 struct ibm_struct *ibm = iibm->data;
5505 struct proc_dir_entry *entry;
5507 BUG_ON(ibm == NULL);
5509 INIT_LIST_HEAD(&ibm->all_drivers);
5511 if (ibm->flags.experimental && !experimental)
5514 dbg_printk(TPACPI_DBG_INIT,
5515 "probing for %s\n", ibm->name);
5518 ret = iibm->init(iibm);
5520 return 0; /* probe failed */
5524 ibm->flags.init_called = 1;
5528 if (ibm->acpi->hid) {
5529 ret = register_tpacpi_subdriver(ibm);
5534 if (ibm->acpi->notify) {
5535 ret = setup_acpi_notify(ibm);
5536 if (ret == -ENODEV) {
5537 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5547 dbg_printk(TPACPI_DBG_INIT,
5548 "%s installed\n", ibm->name);
5551 entry = create_proc_entry(ibm->name,
5552 S_IFREG | S_IRUGO | S_IWUSR,
5555 printk(TPACPI_ERR "unable to create proc entry %s\n",
5560 entry->owner = THIS_MODULE;
5562 entry->read_proc = &dispatch_procfs_read;
5564 entry->write_proc = &dispatch_procfs_write;
5565 ibm->flags.proc_created = 1;
5568 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5573 dbg_printk(TPACPI_DBG_INIT,
5574 "%s: at error exit path with result %d\n",
5578 return (ret < 0)? ret : 0;
5583 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5585 const struct dmi_device *dev = NULL;
5586 char ec_fw_string[18];
5591 memset(tp, 0, sizeof(*tp));
5593 if (dmi_name_in_vendors("IBM"))
5594 tp->vendor = PCI_VENDOR_ID_IBM;
5595 else if (dmi_name_in_vendors("LENOVO"))
5596 tp->vendor = PCI_VENDOR_ID_LENOVO;
5600 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5602 if (!tp->bios_version_str)
5604 tp->bios_model = tp->bios_version_str[0]
5605 | (tp->bios_version_str[1] << 8);
5608 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5609 * X32 or newer, all Z series; Some models must have an
5610 * up-to-date BIOS or they will not be detected.
5612 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5614 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5615 if (sscanf(dev->name,
5616 "IBM ThinkPad Embedded Controller -[%17c",
5617 ec_fw_string) == 1) {
5618 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5619 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5621 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5622 tp->ec_model = ec_fw_string[0]
5623 | (ec_fw_string[1] << 8);
5628 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5630 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5631 kfree(tp->model_str);
5632 tp->model_str = NULL;
5636 static int __init probe_for_thinkpad(void)
5644 * Non-ancient models have better DMI tagging, but very old models
5647 is_thinkpad = (thinkpad_id.model_str != NULL);
5649 /* ec is required because many other handles are relative to it */
5650 TPACPI_ACPIHANDLE_INIT(ec);
5654 "Not yet supported ThinkPad detected!\n");
5659 * Risks a regression on very old machines, but reduces potential
5660 * false positives a damn great deal
5663 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5665 if (!is_thinkpad && !force_load)
5672 /* Module init, exit, parameters */
5674 static struct ibm_init_struct ibms_init[] __initdata = {
5676 .init = thinkpad_acpi_driver_init,
5677 .data = &thinkpad_acpi_driver_data,
5680 .init = hotkey_init,
5681 .data = &hotkey_driver_data,
5684 .init = bluetooth_init,
5685 .data = &bluetooth_driver_data,
5689 .data = &wan_driver_data,
5693 .data = &video_driver_data,
5697 .data = &light_driver_data,
5699 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5702 .data = &dock_driver_data[0],
5706 .data = &dock_driver_data[1],
5709 #ifdef CONFIG_THINKPAD_ACPI_BAY
5712 .data = &bay_driver_data,
5717 .data = &cmos_driver_data,
5721 .data = &led_driver_data,
5725 .data = &beep_driver_data,
5728 .init = thermal_init,
5729 .data = &thermal_driver_data,
5732 .data = &ecdump_driver_data,
5735 .init = brightness_init,
5736 .data = &brightness_driver_data,
5739 .data = &volume_driver_data,
5743 .data = &fan_driver_data,
5747 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5750 struct ibm_struct *ibm;
5752 if (!kp || !kp->name || !val)
5755 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5756 ibm = ibms_init[i].data;
5757 WARN_ON(ibm == NULL);
5759 if (!ibm || !ibm->name)
5762 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5763 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5765 strcpy(ibms_init[i].param, val);
5766 strcat(ibms_init[i].param, ",");
5774 module_param(experimental, int, 0);
5775 MODULE_PARM_DESC(experimental,
5776 "Enables experimental features when non-zero");
5778 module_param_named(debug, dbg_level, uint, 0);
5779 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5781 module_param(force_load, bool, 0);
5782 MODULE_PARM_DESC(force_load,
5783 "Attempts to load the driver even on a "
5784 "mis-identified ThinkPad when true");
5786 module_param_named(fan_control, fan_control_allowed, bool, 0);
5787 MODULE_PARM_DESC(fan_control,
5788 "Enables setting fan parameters features when true");
5790 module_param_named(brightness_mode, brightness_mode, int, 0);
5791 MODULE_PARM_DESC(brightness_mode,
5792 "Selects brightness control strategy: "
5793 "0=auto, 1=EC, 2=CMOS, 3=both");
5795 module_param(brightness_enable, uint, 0);
5796 MODULE_PARM_DESC(brightness_enable,
5797 "Enables backlight control when 1, disables when 0");
5799 module_param(hotkey_report_mode, uint, 0);
5800 MODULE_PARM_DESC(hotkey_report_mode,
5801 "used for backwards compatibility with userspace, "
5802 "see documentation");
5804 #define TPACPI_PARAM(feature) \
5805 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5806 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5807 "at module load, see documentation")
5809 TPACPI_PARAM(hotkey);
5810 TPACPI_PARAM(bluetooth);
5811 TPACPI_PARAM(video);
5812 TPACPI_PARAM(light);
5813 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5816 #ifdef CONFIG_THINKPAD_ACPI_BAY
5818 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5822 TPACPI_PARAM(ecdump);
5823 TPACPI_PARAM(brightness);
5824 TPACPI_PARAM(volume);
5827 static void thinkpad_acpi_module_exit(void)
5829 struct ibm_struct *ibm, *itmp;
5831 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5833 list_for_each_entry_safe_reverse(ibm, itmp,
5834 &tpacpi_all_drivers,
5839 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5841 if (tpacpi_inputdev) {
5842 if (tp_features.input_device_registered)
5843 input_unregister_device(tpacpi_inputdev);
5845 input_free_device(tpacpi_inputdev);
5849 hwmon_device_unregister(tpacpi_hwmon);
5851 if (tp_features.sensors_pdev_attrs_registered)
5852 device_remove_file(&tpacpi_sensors_pdev->dev,
5853 &dev_attr_thinkpad_acpi_pdev_name);
5854 if (tpacpi_sensors_pdev)
5855 platform_device_unregister(tpacpi_sensors_pdev);
5857 platform_device_unregister(tpacpi_pdev);
5859 if (tp_features.sensors_pdrv_attrs_registered)
5860 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5861 if (tp_features.platform_drv_attrs_registered)
5862 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5864 if (tp_features.sensors_pdrv_registered)
5865 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5867 if (tp_features.platform_drv_registered)
5868 platform_driver_unregister(&tpacpi_pdriver);
5871 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5873 kfree(thinkpad_id.bios_version_str);
5874 kfree(thinkpad_id.ec_version_str);
5875 kfree(thinkpad_id.model_str);
5879 static int __init thinkpad_acpi_module_init(void)
5883 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5885 /* Parameter checking */
5886 if (hotkey_report_mode > 2)
5889 /* Driver-level probe */
5891 get_thinkpad_model_data(&thinkpad_id);
5892 ret = probe_for_thinkpad();
5894 thinkpad_acpi_module_exit();
5898 /* Driver initialization */
5900 TPACPI_ACPIHANDLE_INIT(ecrd);
5901 TPACPI_ACPIHANDLE_INIT(ecwr);
5903 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5906 "unable to create proc dir " TPACPI_PROC_DIR);
5907 thinkpad_acpi_module_exit();
5910 proc_dir->owner = THIS_MODULE;
5912 ret = platform_driver_register(&tpacpi_pdriver);
5915 "unable to register main platform driver\n");
5916 thinkpad_acpi_module_exit();
5919 tp_features.platform_drv_registered = 1;
5921 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5924 "unable to register hwmon platform driver\n");
5925 thinkpad_acpi_module_exit();
5928 tp_features.sensors_pdrv_registered = 1;
5930 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5932 tp_features.platform_drv_attrs_registered = 1;
5933 ret = tpacpi_create_driver_attributes(
5934 &tpacpi_hwmon_pdriver.driver);
5938 "unable to create sysfs driver attributes\n");
5939 thinkpad_acpi_module_exit();
5942 tp_features.sensors_pdrv_attrs_registered = 1;
5945 /* Device initialization */
5946 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
5948 if (IS_ERR(tpacpi_pdev)) {
5949 ret = PTR_ERR(tpacpi_pdev);
5951 printk(TPACPI_ERR "unable to register platform device\n");
5952 thinkpad_acpi_module_exit();
5955 tpacpi_sensors_pdev = platform_device_register_simple(
5956 TPACPI_HWMON_DRVR_NAME,
5958 if (IS_ERR(tpacpi_sensors_pdev)) {
5959 ret = PTR_ERR(tpacpi_sensors_pdev);
5960 tpacpi_sensors_pdev = NULL;
5962 "unable to register hwmon platform device\n");
5963 thinkpad_acpi_module_exit();
5966 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5967 &dev_attr_thinkpad_acpi_pdev_name);
5970 "unable to create sysfs hwmon device attributes\n");
5971 thinkpad_acpi_module_exit();
5974 tp_features.sensors_pdev_attrs_registered = 1;
5975 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5976 if (IS_ERR(tpacpi_hwmon)) {
5977 ret = PTR_ERR(tpacpi_hwmon);
5978 tpacpi_hwmon = NULL;
5979 printk(TPACPI_ERR "unable to register hwmon device\n");
5980 thinkpad_acpi_module_exit();
5983 mutex_init(&tpacpi_inputdev_send_mutex);
5984 tpacpi_inputdev = input_allocate_device();
5985 if (!tpacpi_inputdev) {
5986 printk(TPACPI_ERR "unable to allocate input device\n");
5987 thinkpad_acpi_module_exit();
5990 /* Prepare input device, but don't register */
5991 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5992 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
5993 tpacpi_inputdev->id.bustype = BUS_HOST;
5994 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5995 thinkpad_id.vendor :
5997 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5998 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6000 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6001 ret = ibm_init(&ibms_init[i]);
6002 if (ret >= 0 && *ibms_init[i].param)
6003 ret = ibms_init[i].data->write(ibms_init[i].param);
6005 thinkpad_acpi_module_exit();
6009 ret = input_register_device(tpacpi_inputdev);
6011 printk(TPACPI_ERR "unable to register input device\n");
6012 thinkpad_acpi_module_exit();
6015 tp_features.input_device_registered = 1;
6018 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6022 /* Please remove this in year 2009 */
6023 MODULE_ALIAS("ibm_acpi");
6026 * DMI matching for module autoloading
6028 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6029 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6031 * Only models listed in thinkwiki will be supported, so add yours
6032 * if it is not there yet.
6034 #define IBM_BIOS_MODULE_ALIAS(__type) \
6035 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6037 /* Non-ancient thinkpads */
6038 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6039 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6041 /* Ancient thinkpad BIOSes have to be identified by
6042 * BIOS type or model number, and there are far less
6043 * BIOS types than model numbers... */
6044 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6045 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6046 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6048 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6049 MODULE_DESCRIPTION(TPACPI_DESC);
6050 MODULE_VERSION(TPACPI_VERSION);
6051 MODULE_LICENSE("GPL");
6053 module_init(thinkpad_acpi_module_init);
6054 module_exit(thinkpad_acpi_module_exit);