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);
192 struct list_head all_drivers;
194 struct tp_acpi_drv_struct *acpi;
197 u8 acpi_driver_registered:1;
198 u8 acpi_notify_installed:1;
205 struct ibm_init_struct {
208 int (*init) (struct ibm_init_struct *);
209 struct ibm_struct *data;
213 #ifdef CONFIG_THINKPAD_ACPI_BAY
225 u32 bright_16levels:1;
227 u32 fan_ctrl_status_undef:1;
228 u32 input_device_registered:1;
229 u32 platform_drv_registered:1;
230 u32 platform_drv_attrs_registered:1;
231 u32 sensors_pdrv_registered:1;
232 u32 sensors_pdrv_attrs_registered:1;
233 u32 sensors_pdev_attrs_registered:1;
234 u32 hotkey_poll_active:1;
237 struct thinkpad_id_data {
238 unsigned int vendor; /* ThinkPad vendor:
239 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
241 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
242 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
244 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
249 static struct thinkpad_id_data thinkpad_id;
252 TPACPI_LIFE_INIT = 0,
257 static int experimental;
258 static u32 dbg_level;
260 /****************************************************************************
261 ****************************************************************************
263 * ACPI Helpers and device model
265 ****************************************************************************
266 ****************************************************************************/
268 /*************************************************************************
272 static acpi_handle root_handle;
274 #define TPACPI_HANDLE(object, parent, paths...) \
275 static acpi_handle object##_handle; \
276 static acpi_handle *object##_parent = &parent##_handle; \
277 static char *object##_path; \
278 static char *object##_paths[] = { paths }
280 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
281 "\\_SB.PCI.ISA.EC", /* 570 */
282 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
283 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
284 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
285 "\\_SB.PCI0.ICH3.EC0", /* R31 */
286 "\\_SB.PCI0.LPC.EC", /* all others */
289 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
290 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
292 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
294 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
295 "\\CMS", /* R40, R40e */
298 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
299 "^HKEY", /* R30, R31 */
300 "HKEY", /* all others */
304 /*************************************************************************
308 static int acpi_evalf(acpi_handle handle,
309 void *res, char *method, char *fmt, ...)
312 struct acpi_object_list params;
313 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
314 struct acpi_buffer result, *resultp;
315 union acpi_object out_obj;
323 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
336 params.pointer = &in_objs[0];
343 in_objs[params.count].integer.value = va_arg(ap, int);
344 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346 /* add more types as needed */
348 printk(TPACPI_ERR "acpi_evalf() called "
349 "with invalid format character '%c'\n", c);
355 if (res_type != 'v') {
356 result.length = sizeof(out_obj);
357 result.pointer = &out_obj;
362 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
367 *(int *)res = out_obj.integer.value;
368 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
371 success = status == AE_OK;
373 /* add more types as needed */
375 printk(TPACPI_ERR "acpi_evalf() called "
376 "with invalid format character '%c'\n", res_type);
380 if (!success && !quiet)
381 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
382 method, fmt0, status);
387 static int acpi_ec_read(int i, u8 *p)
392 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
396 if (ec_read(i, p) < 0)
403 static int acpi_ec_write(int i, u8 v)
406 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
409 if (ec_write(i, v) < 0)
416 static int _sta(acpi_handle handle)
420 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
426 static int issue_thinkpad_cmos_command(int cmos_cmd)
431 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
437 /*************************************************************************
441 #define TPACPI_ACPIHANDLE_INIT(object) \
442 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
443 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
445 static void drv_acpi_handle_init(char *name,
446 acpi_handle *handle, acpi_handle parent,
447 char **paths, int num_paths, char **path)
452 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
455 for (i = 0; i < num_paths; i++) {
456 status = acpi_get_handle(parent, paths[i], handle);
457 if (ACPI_SUCCESS(status)) {
459 dbg_printk(TPACPI_DBG_INIT,
460 "Found ACPI handle %s for %s\n",
466 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
471 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
473 struct ibm_struct *ibm = data;
475 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
478 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
481 ibm->acpi->notify(ibm, event);
484 static int __init setup_acpi_notify(struct ibm_struct *ibm)
491 if (!*ibm->acpi->handle)
494 vdbg_printk(TPACPI_DBG_INIT,
495 "setting up ACPI notify for %s\n", ibm->name);
497 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
499 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
504 acpi_driver_data(ibm->acpi->device) = ibm;
505 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
506 TPACPI_ACPI_EVENT_PREFIX,
509 status = acpi_install_notify_handler(*ibm->acpi->handle,
510 ibm->acpi->type, dispatch_acpi_notify, ibm);
511 if (ACPI_FAILURE(status)) {
512 if (status == AE_ALREADY_EXISTS) {
514 "another device driver is already "
515 "handling %s events\n", ibm->name);
518 "acpi_install_notify_handler(%s) failed: %d\n",
523 ibm->flags.acpi_notify_installed = 1;
527 static int __init tpacpi_device_add(struct acpi_device *device)
532 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
536 dbg_printk(TPACPI_DBG_INIT,
537 "registering %s as an ACPI driver\n", ibm->name);
541 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
542 if (!ibm->acpi->driver) {
543 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
547 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
548 ibm->acpi->driver->ids = ibm->acpi->hid;
550 ibm->acpi->driver->ops.add = &tpacpi_device_add;
552 rc = acpi_bus_register_driver(ibm->acpi->driver);
554 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
556 kfree(ibm->acpi->driver);
557 ibm->acpi->driver = NULL;
559 ibm->flags.acpi_driver_registered = 1;
565 /****************************************************************************
566 ****************************************************************************
570 ****************************************************************************
571 ****************************************************************************/
573 static int dispatch_procfs_read(char *page, char **start, off_t off,
574 int count, int *eof, void *data)
576 struct ibm_struct *ibm = data;
579 if (!ibm || !ibm->read)
582 len = ibm->read(page);
586 if (len <= off + count)
598 static int dispatch_procfs_write(struct file *file,
599 const char __user *userbuf,
600 unsigned long count, void *data)
602 struct ibm_struct *ibm = data;
606 if (!ibm || !ibm->write)
609 kernbuf = kmalloc(count + 2, GFP_KERNEL);
613 if (copy_from_user(kernbuf, userbuf, count)) {
619 strcat(kernbuf, ",");
620 ret = ibm->write(kernbuf);
629 static char *next_cmd(char **cmds)
634 while ((end = strchr(start, ',')) && end == start)
646 /****************************************************************************
647 ****************************************************************************
649 * Device model: input, hwmon and platform
651 ****************************************************************************
652 ****************************************************************************/
654 static struct platform_device *tpacpi_pdev;
655 static struct platform_device *tpacpi_sensors_pdev;
656 static struct device *tpacpi_hwmon;
657 static struct input_dev *tpacpi_inputdev;
658 static struct mutex tpacpi_inputdev_send_mutex;
659 static LIST_HEAD(tpacpi_all_drivers);
661 static int tpacpi_resume_handler(struct platform_device *pdev)
663 struct ibm_struct *ibm, *itmp;
665 list_for_each_entry_safe(ibm, itmp,
675 static struct platform_driver tpacpi_pdriver = {
677 .name = TPACPI_DRVR_NAME,
678 .owner = THIS_MODULE,
680 .resume = tpacpi_resume_handler,
683 static struct platform_driver tpacpi_hwmon_pdriver = {
685 .name = TPACPI_HWMON_DRVR_NAME,
686 .owner = THIS_MODULE,
690 /*************************************************************************
691 * sysfs support helpers
694 struct attribute_set {
695 unsigned int members, max_members;
696 struct attribute_group group;
699 struct attribute_set_obj {
700 struct attribute_set s;
702 } __attribute__((packed));
704 static struct attribute_set *create_attr_set(unsigned int max_members,
707 struct attribute_set_obj *sobj;
709 if (max_members == 0)
712 /* Allocates space for implicit NULL at the end too */
713 sobj = kzalloc(sizeof(struct attribute_set_obj) +
714 max_members * sizeof(struct attribute *),
718 sobj->s.max_members = max_members;
719 sobj->s.group.attrs = &sobj->a;
720 sobj->s.group.name = name;
725 #define destroy_attr_set(_set) \
728 /* not multi-threaded safe, use it in a single thread per set */
729 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
734 if (s->members >= s->max_members)
737 s->group.attrs[s->members] = attr;
743 static int add_many_to_attr_set(struct attribute_set *s,
744 struct attribute **attr,
749 for (i = 0; i < count; i++) {
750 res = add_to_attr_set(s, attr[i]);
758 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
760 sysfs_remove_group(kobj, &s->group);
764 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
765 sysfs_create_group(_kobj, &_attr_set->group)
767 static int parse_strtoul(const char *buf,
768 unsigned long max, unsigned long *value)
772 while (*buf && isspace(*buf))
774 *value = simple_strtoul(buf, &endp, 0);
775 while (*endp && isspace(*endp))
777 if (*endp || *value > max)
783 /*************************************************************************
784 * thinkpad-acpi driver attributes
787 /* interface_version --------------------------------------------------- */
788 static ssize_t tpacpi_driver_interface_version_show(
789 struct device_driver *drv,
792 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
795 static DRIVER_ATTR(interface_version, S_IRUGO,
796 tpacpi_driver_interface_version_show, NULL);
798 /* debug_level --------------------------------------------------------- */
799 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
802 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
805 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
806 const char *buf, size_t count)
810 if (parse_strtoul(buf, 0xffff, &t))
818 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
819 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
821 /* version ------------------------------------------------------------- */
822 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
825 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
826 TPACPI_DESC, TPACPI_VERSION);
829 static DRIVER_ATTR(version, S_IRUGO,
830 tpacpi_driver_version_show, NULL);
832 /* --------------------------------------------------------------------- */
834 static struct driver_attribute *tpacpi_driver_attributes[] = {
835 &driver_attr_debug_level, &driver_attr_version,
836 &driver_attr_interface_version,
839 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
845 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
846 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
853 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
857 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
858 driver_remove_file(drv, tpacpi_driver_attributes[i]);
861 /****************************************************************************
862 ****************************************************************************
866 ****************************************************************************
867 ****************************************************************************/
869 /*************************************************************************
870 * thinkpad-acpi init subdriver
873 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
875 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
876 printk(TPACPI_INFO "%s\n", TPACPI_URL);
878 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
879 (thinkpad_id.bios_version_str) ?
880 thinkpad_id.bios_version_str : "unknown",
881 (thinkpad_id.ec_version_str) ?
882 thinkpad_id.ec_version_str : "unknown");
884 if (thinkpad_id.vendor && thinkpad_id.model_str)
885 printk(TPACPI_INFO "%s %s\n",
886 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
887 "IBM" : ((thinkpad_id.vendor ==
888 PCI_VENDOR_ID_LENOVO) ?
889 "Lenovo" : "Unknown vendor"),
890 thinkpad_id.model_str);
895 static int thinkpad_acpi_driver_read(char *p)
899 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
900 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
905 static struct ibm_struct thinkpad_acpi_driver_data = {
907 .read = thinkpad_acpi_driver_read,
910 /*************************************************************************
914 enum { /* hot key scan codes (derived from ACPI DSDT) */
915 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
916 TP_ACPI_HOTKEYSCAN_FNF2,
917 TP_ACPI_HOTKEYSCAN_FNF3,
918 TP_ACPI_HOTKEYSCAN_FNF4,
919 TP_ACPI_HOTKEYSCAN_FNF5,
920 TP_ACPI_HOTKEYSCAN_FNF6,
921 TP_ACPI_HOTKEYSCAN_FNF7,
922 TP_ACPI_HOTKEYSCAN_FNF8,
923 TP_ACPI_HOTKEYSCAN_FNF9,
924 TP_ACPI_HOTKEYSCAN_FNF10,
925 TP_ACPI_HOTKEYSCAN_FNF11,
926 TP_ACPI_HOTKEYSCAN_FNF12,
927 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
928 TP_ACPI_HOTKEYSCAN_FNINSERT,
929 TP_ACPI_HOTKEYSCAN_FNDELETE,
930 TP_ACPI_HOTKEYSCAN_FNHOME,
931 TP_ACPI_HOTKEYSCAN_FNEND,
932 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
933 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
934 TP_ACPI_HOTKEYSCAN_FNSPACE,
935 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
936 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
937 TP_ACPI_HOTKEYSCAN_MUTE,
938 TP_ACPI_HOTKEYSCAN_THINKPAD,
941 enum { /* Keys available through NVRAM polling */
942 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
943 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
946 enum { /* Positions of some of the keys in hotkey masks */
947 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
948 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
949 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
950 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
951 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
952 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
953 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
954 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
957 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
960 enum { /* NVRAM to ACPI HKEY group map */
961 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
962 TP_ACPI_HKEY_ZOOM_MASK |
963 TP_ACPI_HKEY_DISPSWTCH_MASK |
964 TP_ACPI_HKEY_HIBERNATE_MASK,
965 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
966 TP_ACPI_HKEY_BRGHTDWN_MASK,
967 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
968 TP_ACPI_HKEY_VOLDWN_MASK |
969 TP_ACPI_HKEY_MUTE_MASK,
972 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
973 struct tp_nvram_state {
974 u16 thinkpad_toggle:1;
976 u16 display_toggle:1;
977 u16 thinklight_toggle:1;
978 u16 hibernate_toggle:1;
979 u16 displayexp_toggle:1;
981 u16 brightness_toggle:1;
989 static struct task_struct *tpacpi_hotkey_task;
990 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
991 static int hotkey_poll_freq = 10; /* Hz */
992 static struct mutex hotkey_thread_mutex;
993 static struct mutex hotkey_thread_data_mutex;
994 static unsigned int hotkey_config_change;
996 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
998 #define hotkey_source_mask 0U
1000 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1002 static struct mutex hotkey_mutex;
1004 static int hotkey_orig_status;
1005 static u32 hotkey_orig_mask;
1006 static u32 hotkey_all_mask;
1007 static u32 hotkey_reserved_mask;
1008 static u32 hotkey_mask;
1010 static unsigned int hotkey_report_mode;
1012 static u16 *hotkey_keycode_map;
1014 static struct attribute_set *hotkey_dev_attributes;
1016 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1017 #define HOTKEY_CONFIG_CRITICAL_START \
1019 mutex_lock(&hotkey_thread_data_mutex); \
1020 hotkey_config_change++; \
1022 #define HOTKEY_CONFIG_CRITICAL_END \
1023 mutex_unlock(&hotkey_thread_data_mutex);
1025 #define HOTKEY_CONFIG_CRITICAL_START
1026 #define HOTKEY_CONFIG_CRITICAL_END
1027 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1029 static int hotkey_get_wlsw(int *status)
1031 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1037 * Call with hotkey_mutex held
1039 static int hotkey_mask_get(void)
1043 if (tp_features.hotkey_mask) {
1044 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1047 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1053 * Call with hotkey_mutex held
1055 static int hotkey_mask_set(u32 mask)
1060 if (tp_features.hotkey_mask) {
1061 HOTKEY_CONFIG_CRITICAL_START
1062 for (i = 0; i < 32; i++) {
1064 /* enable in firmware mask only keys not in NVRAM
1065 * mode, but enable the key in the cached hotkey_mask
1066 * regardless of mode, or the key will end up
1067 * disabled by hotkey_mask_get() */
1068 if (!acpi_evalf(hkey_handle,
1069 NULL, "MHKM", "vdd", i + 1,
1070 !!((mask & ~hotkey_source_mask) & m))) {
1074 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1077 HOTKEY_CONFIG_CRITICAL_END
1079 /* hotkey_mask_get must be called unconditionally below */
1080 if (!hotkey_mask_get() && !rc &&
1081 (hotkey_mask & ~hotkey_source_mask) !=
1082 (mask & ~hotkey_source_mask)) {
1083 printk(TPACPI_NOTICE
1084 "requested hot key mask 0x%08x, but "
1085 "firmware forced it to 0x%08x\n",
1089 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1090 HOTKEY_CONFIG_CRITICAL_START
1091 hotkey_mask = mask & hotkey_source_mask;
1092 HOTKEY_CONFIG_CRITICAL_END
1094 if (hotkey_mask != mask) {
1095 printk(TPACPI_NOTICE
1096 "requested hot key mask 0x%08x, "
1097 "forced to 0x%08x (NVRAM poll mask is "
1098 "0x%08x): no firmware mask support\n",
1099 mask, hotkey_mask, hotkey_source_mask);
1104 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1110 static int hotkey_status_get(int *status)
1112 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1118 static int hotkey_status_set(int status)
1120 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1126 static void tpacpi_input_send_radiosw(void)
1130 mutex_lock(&tpacpi_inputdev_send_mutex);
1132 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1133 input_report_switch(tpacpi_inputdev,
1135 input_sync(tpacpi_inputdev);
1138 mutex_unlock(&tpacpi_inputdev_send_mutex);
1141 static void tpacpi_input_send_key(unsigned int scancode)
1143 unsigned int keycode;
1145 keycode = hotkey_keycode_map[scancode];
1147 if (keycode != KEY_RESERVED) {
1148 mutex_lock(&tpacpi_inputdev_send_mutex);
1150 input_report_key(tpacpi_inputdev, keycode, 1);
1151 if (keycode == KEY_UNKNOWN)
1152 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1154 input_sync(tpacpi_inputdev);
1156 input_report_key(tpacpi_inputdev, keycode, 0);
1157 if (keycode == KEY_UNKNOWN)
1158 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1160 input_sync(tpacpi_inputdev);
1162 mutex_unlock(&tpacpi_inputdev_send_mutex);
1166 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1167 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1169 static void tpacpi_hotkey_send_key(unsigned int scancode)
1171 tpacpi_input_send_key(scancode);
1172 if (hotkey_report_mode < 2) {
1173 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1174 0x80, 0x1001 + scancode);
1178 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1182 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1183 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1184 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1185 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1186 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1187 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1189 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1190 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1191 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1193 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1194 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1195 n->displayexp_toggle =
1196 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1198 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1199 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1200 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1201 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1202 n->brightness_toggle =
1203 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1205 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1206 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1207 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1208 >> TP_NVRAM_POS_LEVEL_VOLUME;
1209 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1210 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1214 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1216 if ((mask & (1 << __scancode)) && \
1217 oldn->__member != newn->__member) \
1218 tpacpi_hotkey_send_key(__scancode); \
1221 #define TPACPI_MAY_SEND_KEY(__scancode) \
1222 do { if (mask & (1 << __scancode)) \
1223 tpacpi_hotkey_send_key(__scancode); } while (0)
1225 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1226 struct tp_nvram_state *newn,
1229 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1230 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1231 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1232 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1234 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1236 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1239 if (oldn->volume_toggle != newn->volume_toggle) {
1240 if (oldn->mute != newn->mute) {
1241 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1243 if (oldn->volume_level > newn->volume_level) {
1244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1245 } else if (oldn->volume_level < newn->volume_level) {
1246 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1247 } else if (oldn->mute == newn->mute) {
1248 /* repeated key presses that didn't change state */
1250 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1251 } else if (newn->volume_level != 0) {
1252 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1254 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1259 /* handle brightness */
1260 if (oldn->brightness_toggle != newn->brightness_toggle) {
1261 if (oldn->brightness_level < newn->brightness_level) {
1262 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1263 } else if (oldn->brightness_level > newn->brightness_level) {
1264 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1266 /* repeated key presses that didn't change state */
1267 if (newn->brightness_level != 0) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1270 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1276 #undef TPACPI_COMPARE_KEY
1277 #undef TPACPI_MAY_SEND_KEY
1279 static int hotkey_kthread(void *data)
1281 struct tp_nvram_state s[2];
1283 unsigned int si, so;
1285 unsigned int change_detector, must_reset;
1287 mutex_lock(&hotkey_thread_mutex);
1289 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1298 /* Initial state for compares */
1299 mutex_lock(&hotkey_thread_data_mutex);
1300 change_detector = hotkey_config_change;
1301 mask = hotkey_source_mask & hotkey_mask;
1302 mutex_unlock(&hotkey_thread_data_mutex);
1303 hotkey_read_nvram(&s[so], mask);
1305 while (!kthread_should_stop() && hotkey_poll_freq) {
1307 t = 1000/hotkey_poll_freq;
1308 t = msleep_interruptible(t);
1309 if (unlikely(kthread_should_stop()))
1311 must_reset = try_to_freeze();
1312 if (t > 0 && !must_reset)
1315 mutex_lock(&hotkey_thread_data_mutex);
1316 if (must_reset || hotkey_config_change != change_detector) {
1317 /* forget old state on thaw or config change */
1320 change_detector = hotkey_config_change;
1322 mask = hotkey_source_mask & hotkey_mask;
1323 mutex_unlock(&hotkey_thread_data_mutex);
1326 hotkey_read_nvram(&s[si], mask);
1327 if (likely(si != so)) {
1328 hotkey_compare_and_issue_event(&s[so], &s[si],
1338 mutex_unlock(&hotkey_thread_mutex);
1342 static void hotkey_poll_stop_sync(void)
1344 if (tpacpi_hotkey_task) {
1345 if (frozen(tpacpi_hotkey_task) ||
1346 freezing(tpacpi_hotkey_task))
1347 thaw_process(tpacpi_hotkey_task);
1349 kthread_stop(tpacpi_hotkey_task);
1350 tpacpi_hotkey_task = NULL;
1351 mutex_lock(&hotkey_thread_mutex);
1352 /* at this point, the thread did exit */
1353 mutex_unlock(&hotkey_thread_mutex);
1357 /* call with hotkey_mutex held */
1358 static void hotkey_poll_setup(int may_warn)
1360 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1361 hotkey_poll_freq > 0 &&
1362 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1363 if (!tpacpi_hotkey_task) {
1364 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1367 if (IS_ERR(tpacpi_hotkey_task)) {
1368 tpacpi_hotkey_task = NULL;
1370 "could not create kernel thread "
1371 "for hotkey polling\n");
1375 hotkey_poll_stop_sync();
1377 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1378 printk(TPACPI_NOTICE
1379 "hot keys 0x%08x require polling, "
1380 "which is currently disabled\n",
1381 hotkey_source_mask);
1386 static void hotkey_poll_setup_safe(int may_warn)
1388 mutex_lock(&hotkey_mutex);
1389 hotkey_poll_setup(may_warn);
1390 mutex_unlock(&hotkey_mutex);
1393 static int hotkey_inputdev_open(struct input_dev *dev)
1395 switch (tpacpi_lifecycle) {
1396 case TPACPI_LIFE_INIT:
1398 * hotkey_init will call hotkey_poll_setup_safe
1399 * at the appropriate moment
1402 case TPACPI_LIFE_EXITING:
1404 case TPACPI_LIFE_RUNNING:
1405 hotkey_poll_setup_safe(0);
1409 /* Should only happen if tpacpi_lifecycle is corrupt */
1414 static void hotkey_inputdev_close(struct input_dev *dev)
1416 /* disable hotkey polling when possible */
1417 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1418 hotkey_poll_setup_safe(0);
1420 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1422 /* sysfs hotkey enable ------------------------------------------------- */
1423 static ssize_t hotkey_enable_show(struct device *dev,
1424 struct device_attribute *attr,
1429 res = hotkey_status_get(&status);
1433 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1436 static ssize_t hotkey_enable_store(struct device *dev,
1437 struct device_attribute *attr,
1438 const char *buf, size_t count)
1443 if (parse_strtoul(buf, 1, &t))
1446 res = hotkey_status_set(t);
1448 return (res) ? res : count;
1451 static struct device_attribute dev_attr_hotkey_enable =
1452 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1453 hotkey_enable_show, hotkey_enable_store);
1455 /* sysfs hotkey mask --------------------------------------------------- */
1456 static ssize_t hotkey_mask_show(struct device *dev,
1457 struct device_attribute *attr,
1462 if (mutex_lock_interruptible(&hotkey_mutex))
1463 return -ERESTARTSYS;
1464 res = hotkey_mask_get();
1465 mutex_unlock(&hotkey_mutex);
1468 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1471 static ssize_t hotkey_mask_store(struct device *dev,
1472 struct device_attribute *attr,
1473 const char *buf, size_t count)
1478 if (parse_strtoul(buf, 0xffffffffUL, &t))
1481 if (mutex_lock_interruptible(&hotkey_mutex))
1482 return -ERESTARTSYS;
1484 res = hotkey_mask_set(t);
1486 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1487 hotkey_poll_setup(1);
1490 mutex_unlock(&hotkey_mutex);
1492 return (res) ? res : count;
1495 static struct device_attribute dev_attr_hotkey_mask =
1496 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1497 hotkey_mask_show, hotkey_mask_store);
1499 /* sysfs hotkey bios_enabled ------------------------------------------- */
1500 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1501 struct device_attribute *attr,
1504 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1507 static struct device_attribute dev_attr_hotkey_bios_enabled =
1508 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1510 /* sysfs hotkey bios_mask ---------------------------------------------- */
1511 static ssize_t hotkey_bios_mask_show(struct device *dev,
1512 struct device_attribute *attr,
1515 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1518 static struct device_attribute dev_attr_hotkey_bios_mask =
1519 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1521 /* sysfs hotkey all_mask ----------------------------------------------- */
1522 static ssize_t hotkey_all_mask_show(struct device *dev,
1523 struct device_attribute *attr,
1526 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1527 hotkey_all_mask | hotkey_source_mask);
1530 static struct device_attribute dev_attr_hotkey_all_mask =
1531 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1533 /* sysfs hotkey recommended_mask --------------------------------------- */
1534 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1535 struct device_attribute *attr,
1538 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1539 (hotkey_all_mask | hotkey_source_mask)
1540 & ~hotkey_reserved_mask);
1543 static struct device_attribute dev_attr_hotkey_recommended_mask =
1544 __ATTR(hotkey_recommended_mask, S_IRUGO,
1545 hotkey_recommended_mask_show, NULL);
1547 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1549 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1550 static ssize_t hotkey_source_mask_show(struct device *dev,
1551 struct device_attribute *attr,
1554 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1557 static ssize_t hotkey_source_mask_store(struct device *dev,
1558 struct device_attribute *attr,
1559 const char *buf, size_t count)
1563 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1564 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1567 if (mutex_lock_interruptible(&hotkey_mutex))
1568 return -ERESTARTSYS;
1570 HOTKEY_CONFIG_CRITICAL_START
1571 hotkey_source_mask = t;
1572 HOTKEY_CONFIG_CRITICAL_END
1574 hotkey_poll_setup(1);
1576 mutex_unlock(&hotkey_mutex);
1581 static struct device_attribute dev_attr_hotkey_source_mask =
1582 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1583 hotkey_source_mask_show, hotkey_source_mask_store);
1585 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1586 static ssize_t hotkey_poll_freq_show(struct device *dev,
1587 struct device_attribute *attr,
1590 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1593 static ssize_t hotkey_poll_freq_store(struct device *dev,
1594 struct device_attribute *attr,
1595 const char *buf, size_t count)
1599 if (parse_strtoul(buf, 25, &t))
1602 if (mutex_lock_interruptible(&hotkey_mutex))
1603 return -ERESTARTSYS;
1605 hotkey_poll_freq = t;
1607 hotkey_poll_setup(1);
1608 mutex_unlock(&hotkey_mutex);
1613 static struct device_attribute dev_attr_hotkey_poll_freq =
1614 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1615 hotkey_poll_freq_show, hotkey_poll_freq_store);
1617 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1619 /* sysfs hotkey radio_sw ----------------------------------------------- */
1620 static ssize_t hotkey_radio_sw_show(struct device *dev,
1621 struct device_attribute *attr,
1625 res = hotkey_get_wlsw(&s);
1629 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1632 static struct device_attribute dev_attr_hotkey_radio_sw =
1633 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1635 /* sysfs hotkey report_mode -------------------------------------------- */
1636 static ssize_t hotkey_report_mode_show(struct device *dev,
1637 struct device_attribute *attr,
1640 return snprintf(buf, PAGE_SIZE, "%d\n",
1641 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1644 static struct device_attribute dev_attr_hotkey_report_mode =
1645 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1647 /* --------------------------------------------------------------------- */
1649 static struct attribute *hotkey_attributes[] __initdata = {
1650 &dev_attr_hotkey_enable.attr,
1651 &dev_attr_hotkey_bios_enabled.attr,
1652 &dev_attr_hotkey_report_mode.attr,
1653 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1654 &dev_attr_hotkey_mask.attr,
1655 &dev_attr_hotkey_all_mask.attr,
1656 &dev_attr_hotkey_recommended_mask.attr,
1657 &dev_attr_hotkey_source_mask.attr,
1658 &dev_attr_hotkey_poll_freq.attr,
1662 static struct attribute *hotkey_mask_attributes[] __initdata = {
1663 &dev_attr_hotkey_bios_mask.attr,
1664 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1665 &dev_attr_hotkey_mask.attr,
1666 &dev_attr_hotkey_all_mask.attr,
1667 &dev_attr_hotkey_recommended_mask.attr,
1671 static int __init hotkey_init(struct ibm_init_struct *iibm)
1673 /* Requirements for changing the default keymaps:
1675 * 1. Many of the keys are mapped to KEY_RESERVED for very
1676 * good reasons. Do not change them unless you have deep
1677 * knowledge on the IBM and Lenovo ThinkPad firmware for
1678 * the various ThinkPad models. The driver behaves
1679 * differently for KEY_RESERVED: such keys have their
1680 * hot key mask *unset* in mask_recommended, and also
1681 * in the initial hot key mask programmed into the
1682 * firmware at driver load time, which means the firm-
1683 * ware may react very differently if you change them to
1686 * 2. You must be subscribed to the linux-thinkpad and
1687 * ibm-acpi-devel mailing lists, and you should read the
1688 * list archives since 2007 if you want to change the
1689 * keymaps. This requirement exists so that you will
1690 * know the past history of problems with the thinkpad-
1691 * acpi driver keymaps, and also that you will be
1692 * listening to any bug reports;
1694 * 3. Do not send thinkpad-acpi specific patches directly to
1695 * for merging, *ever*. Send them to the linux-acpi
1696 * mailinglist for comments. Merging is to be done only
1697 * through acpi-test and the ACPI maintainer.
1699 * If the above is too much to ask, don't change the keymap.
1700 * Ask the thinkpad-acpi maintainer to do it, instead.
1702 static u16 ibm_keycode_map[] __initdata = {
1703 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1704 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1705 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1706 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1708 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1709 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1710 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1711 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1713 /* brightness: firmware always reacts to them, unless
1714 * X.org did some tricks in the radeon BIOS scratch
1715 * registers of *some* models */
1716 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1717 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1719 /* Thinklight: firmware always react to it */
1720 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1722 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1723 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1725 /* Volume: firmware always react to it and reprograms
1726 * the built-in *extra* mixer. Never map it to control
1727 * another mixer by default. */
1728 KEY_RESERVED, /* 0x14: VOLUME UP */
1729 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1730 KEY_RESERVED, /* 0x16: MUTE */
1732 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1734 /* (assignments unknown, please report if found) */
1735 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1736 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1738 static u16 lenovo_keycode_map[] __initdata = {
1739 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1740 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1741 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1742 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1744 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1745 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1746 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1747 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1749 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1750 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1752 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1754 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1755 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1757 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1758 * react to it and reprograms the built-in *extra* mixer.
1759 * Never map it to control another mixer by default.
1761 * T60?, T61, R60?, R61: firmware and EC tries to send
1762 * these over the regular keyboard, so these are no-ops,
1763 * but there are still weird bugs re. MUTE, so do not
1764 * change unless you get test reports from all Lenovo
1765 * models. May cause the BIOS to interfere with the
1768 KEY_RESERVED, /* 0x14: VOLUME UP */
1769 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1770 KEY_RESERVED, /* 0x16: MUTE */
1772 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1774 /* (assignments unknown, please report if found) */
1775 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1776 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1779 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1780 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1781 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1787 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1789 BUG_ON(!tpacpi_inputdev);
1790 BUG_ON(tpacpi_inputdev->open != NULL ||
1791 tpacpi_inputdev->close != NULL);
1793 TPACPI_ACPIHANDLE_INIT(hkey);
1794 mutex_init(&hotkey_mutex);
1796 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1797 mutex_init(&hotkey_thread_mutex);
1798 mutex_init(&hotkey_thread_data_mutex);
1801 /* hotkey not supported on 570 */
1802 tp_features.hotkey = hkey_handle != NULL;
1804 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1805 str_supported(tp_features.hotkey));
1807 if (tp_features.hotkey) {
1808 hotkey_dev_attributes = create_attr_set(10, NULL);
1809 if (!hotkey_dev_attributes)
1811 res = add_many_to_attr_set(hotkey_dev_attributes,
1813 ARRAY_SIZE(hotkey_attributes));
1817 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1818 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1819 for HKEY interface version 0x100 */
1820 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1821 if ((hkeyv >> 8) != 1) {
1822 printk(TPACPI_ERR "unknown version of the "
1823 "HKEY interface: 0x%x\n", hkeyv);
1824 printk(TPACPI_ERR "please report this to %s\n",
1828 * MHKV 0x100 in A31, R40, R40e,
1829 * T4x, X31, and later
1831 tp_features.hotkey_mask = 1;
1835 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1836 str_supported(tp_features.hotkey_mask));
1838 if (tp_features.hotkey_mask) {
1839 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1842 "missing MHKA handler, "
1843 "please report this to %s\n",
1845 /* FN+F12, FN+F4, FN+F3 */
1846 hotkey_all_mask = 0x080cU;
1850 /* hotkey_source_mask *must* be zero for
1851 * the first hotkey_mask_get */
1852 res = hotkey_status_get(&hotkey_orig_status);
1853 if (!res && tp_features.hotkey_mask) {
1854 res = hotkey_mask_get();
1855 hotkey_orig_mask = hotkey_mask;
1857 res = add_many_to_attr_set(
1858 hotkey_dev_attributes,
1859 hotkey_mask_attributes,
1860 ARRAY_SIZE(hotkey_mask_attributes));
1864 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1865 if (tp_features.hotkey_mask) {
1866 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1869 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1872 vdbg_printk(TPACPI_DBG_INIT,
1873 "hotkey source mask 0x%08x, polling freq %d\n",
1874 hotkey_source_mask, hotkey_poll_freq);
1877 /* Not all thinkpads have a hardware radio switch */
1878 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1879 tp_features.hotkey_wlsw = 1;
1881 "radio switch found; radios are %s\n",
1882 enabled(status, 0));
1883 res = add_to_attr_set(hotkey_dev_attributes,
1884 &dev_attr_hotkey_radio_sw.attr);
1888 res = register_attr_set_with_sysfs(
1889 hotkey_dev_attributes,
1890 &tpacpi_pdev->dev.kobj);
1894 /* Set up key map */
1896 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1898 if (!hotkey_keycode_map) {
1900 "failed to allocate memory for key map\n");
1904 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1905 dbg_printk(TPACPI_DBG_INIT,
1906 "using Lenovo default hot key map\n");
1907 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1908 TPACPI_HOTKEY_MAP_SIZE);
1910 dbg_printk(TPACPI_DBG_INIT,
1911 "using IBM default hot key map\n");
1912 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1913 TPACPI_HOTKEY_MAP_SIZE);
1916 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1917 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1918 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1919 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1920 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1921 tpacpi_inputdev->keycode = hotkey_keycode_map;
1922 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1923 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1924 set_bit(hotkey_keycode_map[i],
1925 tpacpi_inputdev->keybit);
1927 if (i < sizeof(hotkey_reserved_mask)*8)
1928 hotkey_reserved_mask |= 1 << i;
1932 if (tp_features.hotkey_wlsw) {
1933 set_bit(EV_SW, tpacpi_inputdev->evbit);
1934 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1937 dbg_printk(TPACPI_DBG_INIT,
1938 "enabling hot key handling\n");
1939 res = hotkey_status_set(1);
1942 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1943 & ~hotkey_reserved_mask)
1944 | hotkey_orig_mask);
1945 if (res < 0 && res != -ENXIO)
1948 dbg_printk(TPACPI_DBG_INIT,
1949 "legacy hot key reporting over procfs %s\n",
1950 (hotkey_report_mode < 2) ?
1951 "enabled" : "disabled");
1953 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1954 tpacpi_inputdev->open = &hotkey_inputdev_open;
1955 tpacpi_inputdev->close = &hotkey_inputdev_close;
1957 hotkey_poll_setup_safe(1);
1961 return (tp_features.hotkey)? 0 : 1;
1964 static void hotkey_exit(void)
1966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1967 hotkey_poll_stop_sync();
1970 if (tp_features.hotkey) {
1971 dbg_printk(TPACPI_DBG_EXIT,
1972 "restoring original hot key mask\n");
1973 /* no short-circuit boolean operator below! */
1974 if ((hotkey_mask_set(hotkey_orig_mask) |
1975 hotkey_status_set(hotkey_orig_status)) != 0)
1977 "failed to restore hot key mask "
1978 "to BIOS defaults\n");
1981 if (hotkey_dev_attributes) {
1982 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1983 hotkey_dev_attributes = NULL;
1987 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1990 unsigned int scancode;
1994 if (event != 0x80) {
1996 "unknown HKEY notification event %d\n", event);
1997 /* forward it to userspace, maybe it knows how to handle it */
1998 acpi_bus_generate_netlink_event(
1999 ibm->acpi->device->pnp.device_class,
2000 ibm->acpi->device->dev.bus_id,
2006 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2007 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2019 switch (hkey >> 12) {
2021 /* 0x1000-0x1FFF: key presses */
2022 scancode = hkey & 0xfff;
2023 if (scancode > 0 && scancode < 0x21) {
2025 if (!(hotkey_source_mask & (1 << scancode))) {
2026 tpacpi_input_send_key(scancode);
2032 "hotkey 0x%04x out of range "
2033 "for keyboard map\n", hkey);
2038 /* 0x5000-0x5FFF: LID */
2039 /* we don't handle it through this path, just
2040 * eat up known LID events */
2041 if (hkey != 0x5001 && hkey != 0x5002) {
2043 "unknown LID-related HKEY event: "
2051 /* 0x7000-0x7FFF: misc */
2052 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2053 tpacpi_input_send_radiosw();
2056 /* fallthrough to default */
2058 /* case 2: dock-related */
2059 /* 0x2305 - T43 waking up due to bay lever
2060 * eject while aslept */
2061 /* case 3: ultra-bay related. maybe bay in dock? */
2062 /* 0x3003 - T43 after wake up by bay lever
2064 printk(TPACPI_NOTICE
2065 "unhandled HKEY event 0x%04x\n", hkey);
2070 if (!ignore_acpi_ev &&
2071 (send_acpi_ev || hotkey_report_mode < 2)) {
2072 acpi_bus_generate_proc_event(ibm->acpi->device,
2076 /* netlink events */
2077 if (!ignore_acpi_ev && send_acpi_ev) {
2078 acpi_bus_generate_netlink_event(
2079 ibm->acpi->device->pnp.device_class,
2080 ibm->acpi->device->dev.bus_id,
2086 static void hotkey_resume(void)
2088 if (hotkey_mask_get())
2090 "error while trying to read hot key mask "
2092 tpacpi_input_send_radiosw();
2093 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2094 hotkey_poll_setup_safe(0);
2098 /* procfs -------------------------------------------------------------- */
2099 static int hotkey_read(char *p)
2104 if (!tp_features.hotkey) {
2105 len += sprintf(p + len, "status:\t\tnot supported\n");
2109 if (mutex_lock_interruptible(&hotkey_mutex))
2110 return -ERESTARTSYS;
2111 res = hotkey_status_get(&status);
2113 res = hotkey_mask_get();
2114 mutex_unlock(&hotkey_mutex);
2118 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2119 if (tp_features.hotkey_mask) {
2120 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2121 len += sprintf(p + len,
2122 "commands:\tenable, disable, reset, <mask>\n");
2124 len += sprintf(p + len, "mask:\t\tnot supported\n");
2125 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2131 static int hotkey_write(char *buf)
2137 if (!tp_features.hotkey)
2140 if (mutex_lock_interruptible(&hotkey_mutex))
2141 return -ERESTARTSYS;
2147 while ((cmd = next_cmd(&buf))) {
2148 if (strlencmp(cmd, "enable") == 0) {
2150 } else if (strlencmp(cmd, "disable") == 0) {
2152 } else if (strlencmp(cmd, "reset") == 0) {
2153 status = hotkey_orig_status;
2154 mask = hotkey_orig_mask;
2155 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2157 } else if (sscanf(cmd, "%x", &mask) == 1) {
2165 res = hotkey_status_set(status);
2167 if (!res && mask != hotkey_mask)
2168 res = hotkey_mask_set(mask);
2171 mutex_unlock(&hotkey_mutex);
2175 static const struct acpi_device_id ibm_htk_device_ids[] = {
2176 {TPACPI_ACPI_HKEY_HID, 0},
2180 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2181 .hid = ibm_htk_device_ids,
2182 .notify = hotkey_notify,
2183 .handle = &hkey_handle,
2184 .type = ACPI_DEVICE_NOTIFY,
2187 static struct ibm_struct hotkey_driver_data = {
2189 .read = hotkey_read,
2190 .write = hotkey_write,
2191 .exit = hotkey_exit,
2192 .resume = hotkey_resume,
2193 .acpi = &ibm_hotkey_acpidriver,
2196 /*************************************************************************
2197 * Bluetooth subdriver
2201 /* ACPI GBDC/SBDC bits */
2202 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2203 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2204 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2207 static int bluetooth_get_radiosw(void);
2208 static int bluetooth_set_radiosw(int radio_on);
2210 /* sysfs bluetooth enable ---------------------------------------------- */
2211 static ssize_t bluetooth_enable_show(struct device *dev,
2212 struct device_attribute *attr,
2217 status = bluetooth_get_radiosw();
2221 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2224 static ssize_t bluetooth_enable_store(struct device *dev,
2225 struct device_attribute *attr,
2226 const char *buf, size_t count)
2231 if (parse_strtoul(buf, 1, &t))
2234 res = bluetooth_set_radiosw(t);
2236 return (res) ? res : count;
2239 static struct device_attribute dev_attr_bluetooth_enable =
2240 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2241 bluetooth_enable_show, bluetooth_enable_store);
2243 /* --------------------------------------------------------------------- */
2245 static struct attribute *bluetooth_attributes[] = {
2246 &dev_attr_bluetooth_enable.attr,
2250 static const struct attribute_group bluetooth_attr_group = {
2251 .attrs = bluetooth_attributes,
2254 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2259 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2261 TPACPI_ACPIHANDLE_INIT(hkey);
2263 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2264 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2265 tp_features.bluetooth = hkey_handle &&
2266 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2268 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2269 str_supported(tp_features.bluetooth),
2272 if (tp_features.bluetooth) {
2273 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2274 /* no bluetooth hardware present in system */
2275 tp_features.bluetooth = 0;
2276 dbg_printk(TPACPI_DBG_INIT,
2277 "bluetooth hardware not installed\n");
2279 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2280 &bluetooth_attr_group);
2286 return (tp_features.bluetooth)? 0 : 1;
2289 static void bluetooth_exit(void)
2291 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2292 &bluetooth_attr_group);
2295 static int bluetooth_get_radiosw(void)
2299 if (!tp_features.bluetooth)
2302 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2305 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2308 static int bluetooth_set_radiosw(int radio_on)
2312 if (!tp_features.bluetooth)
2315 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2318 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2320 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2321 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2327 /* procfs -------------------------------------------------------------- */
2328 static int bluetooth_read(char *p)
2331 int status = bluetooth_get_radiosw();
2333 if (!tp_features.bluetooth)
2334 len += sprintf(p + len, "status:\t\tnot supported\n");
2336 len += sprintf(p + len, "status:\t\t%s\n",
2337 (status)? "enabled" : "disabled");
2338 len += sprintf(p + len, "commands:\tenable, disable\n");
2344 static int bluetooth_write(char *buf)
2348 if (!tp_features.bluetooth)
2351 while ((cmd = next_cmd(&buf))) {
2352 if (strlencmp(cmd, "enable") == 0) {
2353 bluetooth_set_radiosw(1);
2354 } else if (strlencmp(cmd, "disable") == 0) {
2355 bluetooth_set_radiosw(0);
2363 static struct ibm_struct bluetooth_driver_data = {
2364 .name = "bluetooth",
2365 .read = bluetooth_read,
2366 .write = bluetooth_write,
2367 .exit = bluetooth_exit,
2370 /*************************************************************************
2375 /* ACPI GWAN/SWAN bits */
2376 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2377 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2378 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2381 static int wan_get_radiosw(void);
2382 static int wan_set_radiosw(int radio_on);
2384 /* sysfs wan enable ---------------------------------------------------- */
2385 static ssize_t wan_enable_show(struct device *dev,
2386 struct device_attribute *attr,
2391 status = wan_get_radiosw();
2395 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2398 static ssize_t wan_enable_store(struct device *dev,
2399 struct device_attribute *attr,
2400 const char *buf, size_t count)
2405 if (parse_strtoul(buf, 1, &t))
2408 res = wan_set_radiosw(t);
2410 return (res) ? res : count;
2413 static struct device_attribute dev_attr_wan_enable =
2414 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2415 wan_enable_show, wan_enable_store);
2417 /* --------------------------------------------------------------------- */
2419 static struct attribute *wan_attributes[] = {
2420 &dev_attr_wan_enable.attr,
2424 static const struct attribute_group wan_attr_group = {
2425 .attrs = wan_attributes,
2428 static int __init wan_init(struct ibm_init_struct *iibm)
2433 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2435 TPACPI_ACPIHANDLE_INIT(hkey);
2437 tp_features.wan = hkey_handle &&
2438 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2440 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2441 str_supported(tp_features.wan),
2444 if (tp_features.wan) {
2445 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2446 /* no wan hardware present in system */
2447 tp_features.wan = 0;
2448 dbg_printk(TPACPI_DBG_INIT,
2449 "wan hardware not installed\n");
2451 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2458 return (tp_features.wan)? 0 : 1;
2461 static void wan_exit(void)
2463 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2467 static int wan_get_radiosw(void)
2471 if (!tp_features.wan)
2474 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2477 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2480 static int wan_set_radiosw(int radio_on)
2484 if (!tp_features.wan)
2487 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2490 status |= TP_ACPI_WANCARD_RADIOSSW;
2492 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2493 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2499 /* procfs -------------------------------------------------------------- */
2500 static int wan_read(char *p)
2503 int status = wan_get_radiosw();
2505 if (!tp_features.wan)
2506 len += sprintf(p + len, "status:\t\tnot supported\n");
2508 len += sprintf(p + len, "status:\t\t%s\n",
2509 (status)? "enabled" : "disabled");
2510 len += sprintf(p + len, "commands:\tenable, disable\n");
2516 static int wan_write(char *buf)
2520 if (!tp_features.wan)
2523 while ((cmd = next_cmd(&buf))) {
2524 if (strlencmp(cmd, "enable") == 0) {
2526 } else if (strlencmp(cmd, "disable") == 0) {
2535 static struct ibm_struct wan_driver_data = {
2540 .flags.experimental = 1,
2543 /*************************************************************************
2547 enum video_access_mode {
2548 TPACPI_VIDEO_NONE = 0,
2549 TPACPI_VIDEO_570, /* 570 */
2550 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2551 TPACPI_VIDEO_NEW, /* all others */
2554 enum { /* video status flags, based on VIDEO_570 */
2555 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2556 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2557 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2560 enum { /* TPACPI_VIDEO_570 constants */
2561 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2562 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2563 * video_status_flags */
2564 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2565 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2568 static enum video_access_mode video_supported;
2569 static int video_orig_autosw;
2571 static int video_autosw_get(void);
2572 static int video_autosw_set(int enable);
2574 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2575 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2576 "\\_SB.PCI0.VID0", /* 770e */
2577 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2578 "\\_SB.PCI0.AGP.VID", /* all others */
2581 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2583 static int __init video_init(struct ibm_init_struct *iibm)
2587 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2589 TPACPI_ACPIHANDLE_INIT(vid);
2590 TPACPI_ACPIHANDLE_INIT(vid2);
2592 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2593 /* G41, assume IVGA doesn't change */
2594 vid_handle = vid2_handle;
2597 /* video switching not supported on R30, R31 */
2598 video_supported = TPACPI_VIDEO_NONE;
2599 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2601 video_supported = TPACPI_VIDEO_570;
2602 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2603 /* 600e/x, 770e, 770x */
2604 video_supported = TPACPI_VIDEO_770;
2607 video_supported = TPACPI_VIDEO_NEW;
2609 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2610 str_supported(video_supported != TPACPI_VIDEO_NONE),
2613 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2616 static void video_exit(void)
2618 dbg_printk(TPACPI_DBG_EXIT,
2619 "restoring original video autoswitch mode\n");
2620 if (video_autosw_set(video_orig_autosw))
2621 printk(TPACPI_ERR "error while trying to restore original "
2622 "video autoswitch mode\n");
2625 static int video_outputsw_get(void)
2630 switch (video_supported) {
2631 case TPACPI_VIDEO_570:
2632 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2633 TP_ACPI_VIDEO_570_PHSCMD))
2635 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2637 case TPACPI_VIDEO_770:
2638 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2641 status |= TP_ACPI_VIDEO_S_LCD;
2642 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2645 status |= TP_ACPI_VIDEO_S_CRT;
2647 case TPACPI_VIDEO_NEW:
2648 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2649 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2652 status |= TP_ACPI_VIDEO_S_CRT;
2654 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2655 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2658 status |= TP_ACPI_VIDEO_S_LCD;
2659 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2662 status |= TP_ACPI_VIDEO_S_DVI;
2671 static int video_outputsw_set(int status)
2676 switch (video_supported) {
2677 case TPACPI_VIDEO_570:
2678 res = acpi_evalf(NULL, NULL,
2679 "\\_SB.PHS2", "vdd",
2680 TP_ACPI_VIDEO_570_PHS2CMD,
2681 status | TP_ACPI_VIDEO_570_PHS2SET);
2683 case TPACPI_VIDEO_770:
2684 autosw = video_autosw_get();
2688 res = video_autosw_set(1);
2691 res = acpi_evalf(vid_handle, NULL,
2692 "ASWT", "vdd", status * 0x100, 0);
2693 if (!autosw && video_autosw_set(autosw)) {
2695 "video auto-switch left enabled due to error\n");
2699 case TPACPI_VIDEO_NEW:
2700 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2701 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2707 return (res)? 0 : -EIO;
2710 static int video_autosw_get(void)
2714 switch (video_supported) {
2715 case TPACPI_VIDEO_570:
2716 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2719 case TPACPI_VIDEO_770:
2720 case TPACPI_VIDEO_NEW:
2721 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2731 static int video_autosw_set(int enable)
2733 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2738 static int video_outputsw_cycle(void)
2740 int autosw = video_autosw_get();
2746 switch (video_supported) {
2747 case TPACPI_VIDEO_570:
2748 res = video_autosw_set(1);
2751 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2753 case TPACPI_VIDEO_770:
2754 case TPACPI_VIDEO_NEW:
2755 res = video_autosw_set(1);
2758 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2763 if (!autosw && video_autosw_set(autosw)) {
2765 "video auto-switch left enabled due to error\n");
2769 return (res)? 0 : -EIO;
2772 static int video_expand_toggle(void)
2774 switch (video_supported) {
2775 case TPACPI_VIDEO_570:
2776 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2778 case TPACPI_VIDEO_770:
2779 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2781 case TPACPI_VIDEO_NEW:
2782 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2790 static int video_read(char *p)
2795 if (video_supported == TPACPI_VIDEO_NONE) {
2796 len += sprintf(p + len, "status:\t\tnot supported\n");
2800 status = video_outputsw_get();
2804 autosw = video_autosw_get();
2808 len += sprintf(p + len, "status:\t\tsupported\n");
2809 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2810 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2811 if (video_supported == TPACPI_VIDEO_NEW)
2812 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2813 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2814 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2815 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2816 if (video_supported == TPACPI_VIDEO_NEW)
2817 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2818 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2819 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2824 static int video_write(char *buf)
2827 int enable, disable, status;
2830 if (video_supported == TPACPI_VIDEO_NONE)
2836 while ((cmd = next_cmd(&buf))) {
2837 if (strlencmp(cmd, "lcd_enable") == 0) {
2838 enable |= TP_ACPI_VIDEO_S_LCD;
2839 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2840 disable |= TP_ACPI_VIDEO_S_LCD;
2841 } else if (strlencmp(cmd, "crt_enable") == 0) {
2842 enable |= TP_ACPI_VIDEO_S_CRT;
2843 } else if (strlencmp(cmd, "crt_disable") == 0) {
2844 disable |= TP_ACPI_VIDEO_S_CRT;
2845 } else if (video_supported == TPACPI_VIDEO_NEW &&
2846 strlencmp(cmd, "dvi_enable") == 0) {
2847 enable |= TP_ACPI_VIDEO_S_DVI;
2848 } else if (video_supported == TPACPI_VIDEO_NEW &&
2849 strlencmp(cmd, "dvi_disable") == 0) {
2850 disable |= TP_ACPI_VIDEO_S_DVI;
2851 } else if (strlencmp(cmd, "auto_enable") == 0) {
2852 res = video_autosw_set(1);
2855 } else if (strlencmp(cmd, "auto_disable") == 0) {
2856 res = video_autosw_set(0);
2859 } else if (strlencmp(cmd, "video_switch") == 0) {
2860 res = video_outputsw_cycle();
2863 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2864 res = video_expand_toggle();
2871 if (enable || disable) {
2872 status = video_outputsw_get();
2875 res = video_outputsw_set((status & ~disable) | enable);
2883 static struct ibm_struct video_driver_data = {
2886 .write = video_write,
2890 /*************************************************************************
2891 * Light (thinklight) subdriver
2894 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2895 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
2897 static int __init light_init(struct ibm_init_struct *iibm)
2899 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2901 TPACPI_ACPIHANDLE_INIT(ledb);
2902 TPACPI_ACPIHANDLE_INIT(lght);
2903 TPACPI_ACPIHANDLE_INIT(cmos);
2905 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2906 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2908 if (tp_features.light)
2909 /* light status not supported on
2910 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2911 tp_features.light_status =
2912 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2914 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2915 str_supported(tp_features.light));
2917 return (tp_features.light)? 0 : 1;
2920 static int light_read(char *p)
2925 if (!tp_features.light) {
2926 len += sprintf(p + len, "status:\t\tnot supported\n");
2927 } else if (!tp_features.light_status) {
2928 len += sprintf(p + len, "status:\t\tunknown\n");
2929 len += sprintf(p + len, "commands:\ton, off\n");
2931 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2933 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2934 len += sprintf(p + len, "commands:\ton, off\n");
2940 static int light_write(char *buf)
2942 int cmos_cmd, lght_cmd;
2946 if (!tp_features.light)
2949 while ((cmd = next_cmd(&buf))) {
2950 if (strlencmp(cmd, "on") == 0) {
2953 } else if (strlencmp(cmd, "off") == 0) {
2959 success = cmos_handle ?
2960 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2961 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2969 static struct ibm_struct light_driver_data = {
2972 .write = light_write,
2975 /*************************************************************************
2979 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2981 static void dock_notify(struct ibm_struct *ibm, u32 event);
2982 static int dock_read(char *p);
2983 static int dock_write(char *buf);
2985 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2986 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2987 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2988 "\\_SB.PCI.ISA.SLCE", /* 570 */
2989 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2991 /* don't list other alternatives as we install a notify handler on the 570 */
2992 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2994 static const struct acpi_device_id ibm_pci_device_ids[] = {
2995 {PCI_ROOT_HID_STRING, 0},
2999 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3001 .notify = dock_notify,
3002 .handle = &dock_handle,
3003 .type = ACPI_SYSTEM_NOTIFY,
3006 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3007 * We just use it to get notifications of dock hotplug
3008 * in very old thinkpads */
3009 .hid = ibm_pci_device_ids,
3010 .notify = dock_notify,
3011 .handle = &pci_handle,
3012 .type = ACPI_SYSTEM_NOTIFY,
3016 static struct ibm_struct dock_driver_data[2] = {
3020 .write = dock_write,
3021 .acpi = &ibm_dock_acpidriver[0],
3025 .acpi = &ibm_dock_acpidriver[1],
3029 #define dock_docked() (_sta(dock_handle) & 1)
3031 static int __init dock_init(struct ibm_init_struct *iibm)
3033 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3035 TPACPI_ACPIHANDLE_INIT(dock);
3037 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3038 str_supported(dock_handle != NULL));
3040 return (dock_handle)? 0 : 1;
3043 static int __init dock_init2(struct ibm_init_struct *iibm)
3047 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3049 if (dock_driver_data[0].flags.acpi_driver_registered &&
3050 dock_driver_data[0].flags.acpi_notify_installed) {
3051 TPACPI_ACPIHANDLE_INIT(pci);
3052 dock2_needed = (pci_handle != NULL);
3053 vdbg_printk(TPACPI_DBG_INIT,
3054 "dock PCI handler for the TP 570 is %s\n",
3055 str_supported(dock2_needed));
3057 vdbg_printk(TPACPI_DBG_INIT,
3058 "dock subdriver part 2 not required\n");
3062 return (dock2_needed)? 0 : 1;
3065 static void dock_notify(struct ibm_struct *ibm, u32 event)
3067 int docked = dock_docked();
3068 int pci = ibm->acpi->hid && ibm->acpi->device &&
3069 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3072 if (event == 1 && !pci) /* 570 */
3073 data = 1; /* button */
3074 else if (event == 1 && pci) /* 570 */
3075 data = 3; /* dock */
3076 else if (event == 3 && docked)
3077 data = 1; /* button */
3078 else if (event == 3 && !docked)
3079 data = 2; /* undock */
3080 else if (event == 0 && docked)
3081 data = 3; /* dock */
3083 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3084 event, _sta(dock_handle));
3085 data = 0; /* unknown */
3087 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3088 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3089 ibm->acpi->device->dev.bus_id,
3093 static int dock_read(char *p)
3096 int docked = dock_docked();
3099 len += sprintf(p + len, "status:\t\tnot supported\n");
3101 len += sprintf(p + len, "status:\t\tundocked\n");
3103 len += sprintf(p + len, "status:\t\tdocked\n");
3104 len += sprintf(p + len, "commands:\tdock, undock\n");
3110 static int dock_write(char *buf)
3117 while ((cmd = next_cmd(&buf))) {
3118 if (strlencmp(cmd, "undock") == 0) {
3119 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3120 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3122 } else if (strlencmp(cmd, "dock") == 0) {
3123 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3132 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3134 /*************************************************************************
3138 #ifdef CONFIG_THINKPAD_ACPI_BAY
3140 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3141 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3142 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3143 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3144 ); /* A21e, R30, R31 */
3145 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3146 "_EJ0", /* all others */
3147 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3148 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3149 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3151 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3155 static int __init bay_init(struct ibm_init_struct *iibm)
3157 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3159 TPACPI_ACPIHANDLE_INIT(bay);
3161 TPACPI_ACPIHANDLE_INIT(bay_ej);
3162 TPACPI_ACPIHANDLE_INIT(bay2);
3164 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3166 tp_features.bay_status = bay_handle &&
3167 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3168 tp_features.bay_status2 = bay2_handle &&
3169 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3171 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3172 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3173 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3174 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3176 vdbg_printk(TPACPI_DBG_INIT,
3177 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3178 str_supported(tp_features.bay_status),
3179 str_supported(tp_features.bay_eject),
3180 str_supported(tp_features.bay_status2),
3181 str_supported(tp_features.bay_eject2));
3183 return (tp_features.bay_status || tp_features.bay_eject ||
3184 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3187 static void bay_notify(struct ibm_struct *ibm, u32 event)
3189 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3190 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3191 ibm->acpi->device->dev.bus_id,
3195 #define bay_occupied(b) (_sta(b##_handle) & 1)
3197 static int bay_read(char *p)
3200 int occupied = bay_occupied(bay);
3201 int occupied2 = bay_occupied(bay2);
3204 len += sprintf(p + len, "status:\t\t%s\n",
3205 tp_features.bay_status ?
3206 (occupied ? "occupied" : "unoccupied") :
3208 if (tp_features.bay_status2)
3209 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3210 "occupied" : "unoccupied");
3212 eject = tp_features.bay_eject && occupied;
3213 eject2 = tp_features.bay_eject2 && occupied2;
3215 if (eject && eject2)
3216 len += sprintf(p + len, "commands:\teject, eject2\n");
3218 len += sprintf(p + len, "commands:\teject\n");
3220 len += sprintf(p + len, "commands:\teject2\n");
3225 static int bay_write(char *buf)
3229 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3232 while ((cmd = next_cmd(&buf))) {
3233 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3234 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3236 } else if (tp_features.bay_eject2 &&
3237 strlencmp(cmd, "eject2") == 0) {
3238 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3247 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3248 .notify = bay_notify,
3249 .handle = &bay_handle,
3250 .type = ACPI_SYSTEM_NOTIFY,
3253 static struct ibm_struct bay_driver_data = {
3257 .acpi = &ibm_bay_acpidriver,
3260 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3262 /*************************************************************************
3266 /* sysfs cmos_command -------------------------------------------------- */
3267 static ssize_t cmos_command_store(struct device *dev,
3268 struct device_attribute *attr,
3269 const char *buf, size_t count)
3271 unsigned long cmos_cmd;
3274 if (parse_strtoul(buf, 21, &cmos_cmd))
3277 res = issue_thinkpad_cmos_command(cmos_cmd);
3278 return (res)? res : count;
3281 static struct device_attribute dev_attr_cmos_command =
3282 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3284 /* --------------------------------------------------------------------- */
3286 static int __init cmos_init(struct ibm_init_struct *iibm)
3290 vdbg_printk(TPACPI_DBG_INIT,
3291 "initializing cmos commands subdriver\n");
3293 TPACPI_ACPIHANDLE_INIT(cmos);
3295 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3296 str_supported(cmos_handle != NULL));
3298 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3302 return (cmos_handle)? 0 : 1;
3305 static void cmos_exit(void)
3307 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3310 static int cmos_read(char *p)
3314 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3315 R30, R31, T20-22, X20-21 */
3317 len += sprintf(p + len, "status:\t\tnot supported\n");
3319 len += sprintf(p + len, "status:\t\tsupported\n");
3320 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3326 static int cmos_write(char *buf)
3331 while ((cmd = next_cmd(&buf))) {
3332 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3333 cmos_cmd >= 0 && cmos_cmd <= 21) {
3338 res = issue_thinkpad_cmos_command(cmos_cmd);
3346 static struct ibm_struct cmos_driver_data = {
3349 .write = cmos_write,
3353 /*************************************************************************
3357 enum led_access_mode {
3358 TPACPI_LED_NONE = 0,
3359 TPACPI_LED_570, /* 570 */
3360 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3361 TPACPI_LED_NEW, /* all others */
3364 enum { /* For TPACPI_LED_OLD */
3365 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3366 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3367 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3370 static enum led_access_mode led_supported;
3372 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3373 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3374 /* T20-22, X20-21 */
3375 "LED", /* all others */
3378 static int __init led_init(struct ibm_init_struct *iibm)
3380 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3382 TPACPI_ACPIHANDLE_INIT(led);
3385 /* led not supported on R30, R31 */
3386 led_supported = TPACPI_LED_NONE;
3387 else if (strlencmp(led_path, "SLED") == 0)
3389 led_supported = TPACPI_LED_570;
3390 else if (strlencmp(led_path, "SYSL") == 0)
3391 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3392 led_supported = TPACPI_LED_OLD;
3395 led_supported = TPACPI_LED_NEW;
3397 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3398 str_supported(led_supported), led_supported);
3400 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3403 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3405 static int led_read(char *p)
3409 if (!led_supported) {
3410 len += sprintf(p + len, "status:\t\tnot supported\n");
3413 len += sprintf(p + len, "status:\t\tsupported\n");
3415 if (led_supported == TPACPI_LED_570) {
3418 for (i = 0; i < 8; i++) {
3419 if (!acpi_evalf(ec_handle,
3420 &status, "GLED", "dd", 1 << i))
3422 len += sprintf(p + len, "%d:\t\t%s\n",
3423 i, led_status(status));
3427 len += sprintf(p + len, "commands:\t"
3428 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3433 /* off, on, blink */
3434 static const int led_sled_arg1[] = { 0, 1, 3 };
3435 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3436 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3437 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3439 static int led_write(char *buf)
3447 while ((cmd = next_cmd(&buf))) {
3448 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3451 if (strstr(cmd, "off")) {
3453 } else if (strstr(cmd, "on")) {
3455 } else if (strstr(cmd, "blink")) {
3460 if (led_supported == TPACPI_LED_570) {
3463 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3464 led, led_sled_arg1[ind]))
3466 } else if (led_supported == TPACPI_LED_OLD) {
3467 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3469 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3471 ret = ec_write(TPACPI_LED_EC_HLBL,
3472 led * led_exp_hlbl[ind]);
3474 ret = ec_write(TPACPI_LED_EC_HLCL,
3475 led * led_exp_hlcl[ind]);
3480 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3481 led, led_led_arg1[ind]))
3489 static struct ibm_struct led_driver_data = {
3495 /*************************************************************************
3499 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3501 static int __init beep_init(struct ibm_init_struct *iibm)
3503 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3505 TPACPI_ACPIHANDLE_INIT(beep);
3507 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3508 str_supported(beep_handle != NULL));
3510 return (beep_handle)? 0 : 1;
3513 static int beep_read(char *p)
3518 len += sprintf(p + len, "status:\t\tnot supported\n");
3520 len += sprintf(p + len, "status:\t\tsupported\n");
3521 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3527 static int beep_write(char *buf)
3535 while ((cmd = next_cmd(&buf))) {
3536 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3537 beep_cmd >= 0 && beep_cmd <= 17) {
3541 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3548 static struct ibm_struct beep_driver_data = {
3551 .write = beep_write,
3554 /*************************************************************************
3558 enum thermal_access_mode {
3559 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3560 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3561 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3562 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3563 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3566 enum { /* TPACPI_THERMAL_TPEC_* */
3567 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3568 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3569 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3572 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3573 struct ibm_thermal_sensors_struct {
3574 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3577 static enum thermal_access_mode thermal_read_mode;
3579 /* idx is zero-based */
3580 static int thermal_get_sensor(int idx, s32 *value)
3586 t = TP_EC_THERMAL_TMP0;
3588 switch (thermal_read_mode) {
3589 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3590 case TPACPI_THERMAL_TPEC_16:
3591 if (idx >= 8 && idx <= 15) {
3592 t = TP_EC_THERMAL_TMP8;
3597 case TPACPI_THERMAL_TPEC_8:
3599 if (!acpi_ec_read(t + idx, &tmp))
3601 *value = tmp * 1000;
3606 case TPACPI_THERMAL_ACPI_UPDT:
3608 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3609 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3611 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3613 *value = (t - 2732) * 100;
3618 case TPACPI_THERMAL_ACPI_TMP07:
3620 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3621 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3623 if (t > 127 || t < -127)
3624 t = TP_EC_THERMAL_TMP_NA;
3630 case TPACPI_THERMAL_NONE:
3638 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3649 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3652 for (i = 0 ; i < n; i++) {
3653 res = thermal_get_sensor(i, &s->temp[i]);
3661 /* sysfs temp##_input -------------------------------------------------- */
3663 static ssize_t thermal_temp_input_show(struct device *dev,
3664 struct device_attribute *attr,
3667 struct sensor_device_attribute *sensor_attr =
3668 to_sensor_dev_attr(attr);
3669 int idx = sensor_attr->index;
3673 res = thermal_get_sensor(idx, &value);
3676 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3679 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3682 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3683 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3684 thermal_temp_input_show, NULL, _idxB)
3686 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3687 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3688 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3689 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3690 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3691 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3692 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3693 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3694 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3695 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3696 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3697 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3698 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3699 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3700 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3701 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3702 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3705 #define THERMAL_ATTRS(X) \
3706 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3708 static struct attribute *thermal_temp_input_attr[] = {
3728 static const struct attribute_group thermal_temp_input16_group = {
3729 .attrs = thermal_temp_input_attr
3732 static const struct attribute_group thermal_temp_input8_group = {
3733 .attrs = &thermal_temp_input_attr[8]
3736 #undef THERMAL_SENSOR_ATTR_TEMP
3737 #undef THERMAL_ATTRS
3739 /* --------------------------------------------------------------------- */
3741 static int __init thermal_init(struct ibm_init_struct *iibm)
3748 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3750 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3752 if (thinkpad_id.ec_model) {
3754 * Direct EC access mode: sensors at registers
3755 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3756 * non-implemented, thermal sensors return 0x80 when
3761 for (i = 0; i < 8; i++) {
3762 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3768 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3776 /* This is sheer paranoia, but we handle it anyway */
3779 "ThinkPad ACPI EC access misbehaving, "
3780 "falling back to ACPI TMPx access "
3782 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3785 "ThinkPad ACPI EC access misbehaving, "
3786 "disabling thermal sensors access\n");
3787 thermal_read_mode = TPACPI_THERMAL_NONE;
3792 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3794 } else if (acpi_tmp7) {
3795 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3796 /* 600e/x, 770e, 770x */
3797 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3799 /* Standard ACPI TMPx access, max 8 sensors */
3800 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3803 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3804 thermal_read_mode = TPACPI_THERMAL_NONE;
3807 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3808 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3811 switch (thermal_read_mode) {
3812 case TPACPI_THERMAL_TPEC_16:
3813 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3814 &thermal_temp_input16_group);
3818 case TPACPI_THERMAL_TPEC_8:
3819 case TPACPI_THERMAL_ACPI_TMP07:
3820 case TPACPI_THERMAL_ACPI_UPDT:
3821 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3822 &thermal_temp_input8_group);
3826 case TPACPI_THERMAL_NONE:
3834 static void thermal_exit(void)
3836 switch (thermal_read_mode) {
3837 case TPACPI_THERMAL_TPEC_16:
3838 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3839 &thermal_temp_input16_group);
3841 case TPACPI_THERMAL_TPEC_8:
3842 case TPACPI_THERMAL_ACPI_TMP07:
3843 case TPACPI_THERMAL_ACPI_UPDT:
3844 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3845 &thermal_temp_input16_group);
3847 case TPACPI_THERMAL_NONE:
3853 static int thermal_read(char *p)
3857 struct ibm_thermal_sensors_struct t;
3859 n = thermal_get_sensors(&t);
3860 if (unlikely(n < 0))
3863 len += sprintf(p + len, "temperatures:\t");
3866 for (i = 0; i < (n - 1); i++)
3867 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3868 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3870 len += sprintf(p + len, "not supported\n");
3875 static struct ibm_struct thermal_driver_data = {
3877 .read = thermal_read,
3878 .exit = thermal_exit,
3881 /*************************************************************************
3885 static u8 ecdump_regs[256];
3887 static int ecdump_read(char *p)
3893 len += sprintf(p + len, "EC "
3894 " +00 +01 +02 +03 +04 +05 +06 +07"
3895 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3896 for (i = 0; i < 256; i += 16) {
3897 len += sprintf(p + len, "EC 0x%02x:", i);
3898 for (j = 0; j < 16; j++) {
3899 if (!acpi_ec_read(i + j, &v))
3901 if (v != ecdump_regs[i + j])
3902 len += sprintf(p + len, " *%02x", v);
3904 len += sprintf(p + len, " %02x", v);
3905 ecdump_regs[i + j] = v;
3907 len += sprintf(p + len, "\n");
3912 /* These are way too dangerous to advertise openly... */
3914 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3915 " (<offset> is 00-ff, <value> is 00-ff)\n");
3916 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3917 " (<offset> is 00-ff, <value> is 0-255)\n");
3922 static int ecdump_write(char *buf)
3927 while ((cmd = next_cmd(&buf))) {
3928 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3930 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3934 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3935 if (!acpi_ec_write(i, v))
3944 static struct ibm_struct ecdump_driver_data = {
3946 .read = ecdump_read,
3947 .write = ecdump_write,
3948 .flags.experimental = 1,
3951 /*************************************************************************
3952 * Backlight/brightness subdriver
3955 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3957 static struct backlight_device *ibm_backlight_device;
3958 static int brightness_offset = 0x31;
3959 static int brightness_mode;
3960 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3962 static struct mutex brightness_mutex;
3965 * ThinkPads can read brightness from two places: EC 0x31, or
3966 * CMOS NVRAM byte 0x5E, bits 0-3.
3968 static int brightness_get(struct backlight_device *bd)
3970 u8 lec = 0, lcmos = 0, level = 0;
3972 if (brightness_mode & 1) {
3973 if (!acpi_ec_read(brightness_offset, &lec))
3975 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3978 if (brightness_mode & 2) {
3979 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3980 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3981 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3982 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3986 if (brightness_mode == 3 && lec != lcmos) {
3988 "CMOS NVRAM (%u) and EC (%u) do not agree "
3989 "on display brightness level\n",
3990 (unsigned int) lcmos,
3991 (unsigned int) lec);
3998 /* May return EINTR which can always be mapped to ERESTARTSYS */
3999 static int brightness_set(int value)
4001 int cmos_cmd, inc, i, res;
4004 if (value > ((tp_features.bright_16levels)? 15 : 7))
4007 res = mutex_lock_interruptible(&brightness_mutex);
4011 current_value = brightness_get(NULL);
4012 if (current_value < 0) {
4013 res = current_value;
4017 cmos_cmd = value > current_value ?
4018 TP_CMOS_BRIGHTNESS_UP :
4019 TP_CMOS_BRIGHTNESS_DOWN;
4020 inc = (value > current_value)? 1 : -1;
4023 for (i = current_value; i != value; i += inc) {
4024 if ((brightness_mode & 2) &&
4025 issue_thinkpad_cmos_command(cmos_cmd)) {
4029 if ((brightness_mode & 1) &&
4030 !acpi_ec_write(brightness_offset, i + inc)) {
4037 mutex_unlock(&brightness_mutex);
4041 /* sysfs backlight class ----------------------------------------------- */
4043 static int brightness_update_status(struct backlight_device *bd)
4045 /* it is the backlight class's job (caller) to handle
4046 * EINTR and other errors properly */
4047 return brightness_set(
4048 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4049 bd->props.power == FB_BLANK_UNBLANK) ?
4050 bd->props.brightness : 0);
4053 static struct backlight_ops ibm_backlight_data = {
4054 .get_brightness = brightness_get,
4055 .update_status = brightness_update_status,
4058 /* --------------------------------------------------------------------- */
4060 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4062 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4063 union acpi_object *obj;
4066 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4067 obj = (union acpi_object *)buffer.pointer;
4068 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4069 printk(TPACPI_ERR "Unknown BCLL data, "
4070 "please report this to %s\n", TPACPI_MAIL);
4073 rc = obj->package.count;
4079 kfree(buffer.pointer);
4083 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4084 void *context, void **rv)
4086 char name[ACPI_PATH_SEGMENT_LENGTH];
4087 struct acpi_buffer buffer = { sizeof(name), &name };
4089 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4090 !strncmp("BCLL", name, sizeof(name) - 1)) {
4091 if (tpacpi_query_bcll_levels(handle) == 16) {
4093 return AE_CTRL_TERMINATE;
4102 static int __init brightness_check_levels(void)
4105 void *found_node = NULL;
4108 TPACPI_ACPIHANDLE_INIT(vid);
4113 /* Search for a BCLL package with 16 levels */
4114 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4115 brightness_find_bcll, NULL,
4118 return (ACPI_SUCCESS(status) && found_node != NULL);
4121 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4122 void *context, void **rv)
4124 char name[ACPI_PATH_SEGMENT_LENGTH];
4125 struct acpi_buffer buffer = { sizeof(name), &name };
4127 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4128 !strncmp("_BCL", name, sizeof(name) - 1)) {
4130 return AE_CTRL_TERMINATE;
4136 static int __init brightness_check_std_acpi_support(void)
4139 void *found_node = NULL;
4142 TPACPI_ACPIHANDLE_INIT(vid);
4147 /* Search for a _BCL method, but don't execute it */
4148 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4149 brightness_find_bcl, NULL, &found_node);
4151 return (ACPI_SUCCESS(status) && found_node != NULL);
4154 static int __init brightness_init(struct ibm_init_struct *iibm)
4158 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4160 mutex_init(&brightness_mutex);
4162 if (!brightness_enable) {
4163 dbg_printk(TPACPI_DBG_INIT,
4164 "brightness support disabled by "
4165 "module parameter\n");
4167 } else if (brightness_enable > 1) {
4168 if (brightness_check_std_acpi_support()) {
4169 printk(TPACPI_NOTICE
4170 "standard ACPI backlight interface "
4171 "available, not loading native one...\n");
4176 if (!brightness_mode) {
4177 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4178 brightness_mode = 2;
4180 brightness_mode = 3;
4182 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4186 if (brightness_mode > 3)
4189 tp_features.bright_16levels =
4190 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4191 brightness_check_levels();
4193 b = brightness_get(NULL);
4197 if (tp_features.bright_16levels)
4199 "detected a 16-level brightness capable ThinkPad\n");
4201 ibm_backlight_device = backlight_device_register(
4202 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4203 &ibm_backlight_data);
4204 if (IS_ERR(ibm_backlight_device)) {
4205 printk(TPACPI_ERR "Could not register backlight device\n");
4206 return PTR_ERR(ibm_backlight_device);
4208 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4210 ibm_backlight_device->props.max_brightness =
4211 (tp_features.bright_16levels)? 15 : 7;
4212 ibm_backlight_device->props.brightness = b;
4213 backlight_update_status(ibm_backlight_device);
4218 static void brightness_exit(void)
4220 if (ibm_backlight_device) {
4221 vdbg_printk(TPACPI_DBG_EXIT,
4222 "calling backlight_device_unregister()\n");
4223 backlight_device_unregister(ibm_backlight_device);
4224 ibm_backlight_device = NULL;
4228 static int brightness_read(char *p)
4233 level = brightness_get(NULL);
4235 len += sprintf(p + len, "level:\t\tunreadable\n");
4237 len += sprintf(p + len, "level:\t\t%d\n", level);
4238 len += sprintf(p + len, "commands:\tup, down\n");
4239 len += sprintf(p + len, "commands:\tlevel <level>"
4240 " (<level> is 0-%d)\n",
4241 (tp_features.bright_16levels) ? 15 : 7);
4247 static int brightness_write(char *buf)
4252 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4254 level = brightness_get(NULL);
4258 while ((cmd = next_cmd(&buf))) {
4259 if (strlencmp(cmd, "up") == 0) {
4260 if (level < max_level)
4262 } else if (strlencmp(cmd, "down") == 0) {
4265 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4266 level >= 0 && level <= max_level) {
4273 * Now we know what the final level should be, so we try to set it.
4274 * Doing it this way makes the syscall restartable in case of EINTR
4276 rc = brightness_set(level);
4277 return (rc == -EINTR)? ERESTARTSYS : rc;
4280 static struct ibm_struct brightness_driver_data = {
4281 .name = "brightness",
4282 .read = brightness_read,
4283 .write = brightness_write,
4284 .exit = brightness_exit,
4287 /*************************************************************************
4291 static int volume_offset = 0x30;
4293 static int volume_read(char *p)
4298 if (!acpi_ec_read(volume_offset, &level)) {
4299 len += sprintf(p + len, "level:\t\tunreadable\n");
4301 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4302 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4303 len += sprintf(p + len, "commands:\tup, down, mute\n");
4304 len += sprintf(p + len, "commands:\tlevel <level>"
4305 " (<level> is 0-15)\n");
4311 static int volume_write(char *buf)
4313 int cmos_cmd, inc, i;
4315 int new_level, new_mute;
4318 while ((cmd = next_cmd(&buf))) {
4319 if (!acpi_ec_read(volume_offset, &level))
4321 new_mute = mute = level & 0x40;
4322 new_level = level = level & 0xf;
4324 if (strlencmp(cmd, "up") == 0) {
4328 new_level = level == 15 ? 15 : level + 1;
4329 } else if (strlencmp(cmd, "down") == 0) {
4333 new_level = level == 0 ? 0 : level - 1;
4334 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4335 new_level >= 0 && new_level <= 15) {
4337 } else if (strlencmp(cmd, "mute") == 0) {
4342 if (new_level != level) {
4343 /* mute doesn't change */
4345 cmos_cmd = (new_level > level) ?
4346 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4347 inc = new_level > level ? 1 : -1;
4349 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4350 !acpi_ec_write(volume_offset, level)))
4353 for (i = level; i != new_level; i += inc)
4354 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4355 !acpi_ec_write(volume_offset, i + inc))
4359 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4360 !acpi_ec_write(volume_offset, new_level + mute))) {
4365 if (new_mute != mute) {
4366 /* level doesn't change */
4368 cmos_cmd = (new_mute) ?
4369 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4371 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4372 !acpi_ec_write(volume_offset, level + new_mute))
4380 static struct ibm_struct volume_driver_data = {
4382 .read = volume_read,
4383 .write = volume_write,
4386 /*************************************************************************
4393 * TPACPI_FAN_RD_ACPI_GFAN:
4394 * ACPI GFAN method: returns fan level
4396 * see TPACPI_FAN_WR_ACPI_SFAN
4397 * EC 0x2f (HFSP) not available if GFAN exists
4399 * TPACPI_FAN_WR_ACPI_SFAN:
4400 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4402 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4405 * TPACPI_FAN_WR_TPEC:
4406 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4407 * Supported on almost all ThinkPads
4409 * Fan speed changes of any sort (including those caused by the
4410 * disengaged mode) are usually done slowly by the firmware as the
4411 * maximum ammount of fan duty cycle change per second seems to be
4414 * Reading is not available if GFAN exists.
4415 * Writing is not available if SFAN exists.
4418 * 7 automatic mode engaged;
4419 * (default operation mode of the ThinkPad)
4420 * fan level is ignored in this mode.
4421 * 6 full speed mode (takes precedence over bit 7);
4422 * not available on all thinkpads. May disable
4423 * the tachometer while the fan controller ramps up
4424 * the speed (which can take up to a few *minutes*).
4425 * Speeds up fan to 100% duty-cycle, which is far above
4426 * the standard RPM levels. It is not impossible that
4427 * it could cause hardware damage.
4428 * 5-3 unused in some models. Extra bits for fan level
4429 * in others, but still useless as all values above
4430 * 7 map to the same speed as level 7 in these models.
4431 * 2-0 fan level (0..7 usually)
4433 * 0x07 = max (set when temperatures critical)
4434 * Some ThinkPads may have other levels, see
4435 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4437 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4438 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4439 * does so, its initial value is meaningless (0x07).
4441 * For firmware bugs, refer to:
4442 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4446 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4447 * Main fan tachometer reading (in RPM)
4449 * This register is present on all ThinkPads with a new-style EC, and
4450 * it is known not to be present on the A21m/e, and T22, as there is
4451 * something else in offset 0x84 according to the ACPI DSDT. Other
4452 * ThinkPads from this same time period (and earlier) probably lack the
4453 * tachometer as well.
4455 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4456 * was never fixed by IBM to report the EC firmware version string
4457 * probably support the tachometer (like the early X models), so
4458 * detecting it is quite hard. We need more data to know for sure.
4460 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4463 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4466 * For firmware bugs, refer to:
4467 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4469 * TPACPI_FAN_WR_ACPI_FANS:
4470 * ThinkPad X31, X40, X41. Not available in the X60.
4472 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4473 * high speed. ACPI DSDT seems to map these three speeds to levels
4474 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4475 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4477 * The speeds are stored on handles
4478 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4480 * There are three default speed sets, acessible as handles:
4481 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4483 * ACPI DSDT switches which set is in use depending on various
4486 * TPACPI_FAN_WR_TPEC is also available and should be used to
4487 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4488 * but the ACPI tables just mention level 7.
4491 enum { /* Fan control constants */
4492 fan_status_offset = 0x2f, /* EC register 0x2f */
4493 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4494 * 0x84 must be read before 0x85 */
4496 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4497 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4499 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4502 enum fan_status_access_mode {
4503 TPACPI_FAN_NONE = 0, /* No fan status or control */
4504 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4505 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4508 enum fan_control_access_mode {
4509 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4510 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4511 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4512 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4515 enum fan_control_commands {
4516 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4517 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4518 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4519 * and also watchdog cmd */
4522 static int fan_control_allowed;
4524 static enum fan_status_access_mode fan_status_access_mode;
4525 static enum fan_control_access_mode fan_control_access_mode;
4526 static enum fan_control_commands fan_control_commands;
4528 static u8 fan_control_initial_status;
4529 static u8 fan_control_desired_level;
4530 static int fan_watchdog_maxinterval;
4532 static struct mutex fan_mutex;
4534 static void fan_watchdog_fire(struct work_struct *ignored);
4535 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4537 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4538 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4539 "\\FSPD", /* 600e/x, 770e, 770x */
4541 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4542 "JFNS", /* 770x-JL */
4546 * Call with fan_mutex held
4548 static void fan_update_desired_level(u8 status)
4551 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4553 fan_control_desired_level = 7;
4555 fan_control_desired_level = status;
4559 static int fan_get_status(u8 *status)
4564 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4566 switch (fan_status_access_mode) {
4567 case TPACPI_FAN_RD_ACPI_GFAN:
4568 /* 570, 600e/x, 770e, 770x */
4570 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4578 case TPACPI_FAN_RD_TPEC:
4579 /* all except 570, 600e/x, 770e, 770x */
4580 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4595 static int fan_get_status_safe(u8 *status)
4600 if (mutex_lock_interruptible(&fan_mutex))
4601 return -ERESTARTSYS;
4602 rc = fan_get_status(&s);
4604 fan_update_desired_level(s);
4605 mutex_unlock(&fan_mutex);
4613 static int fan_get_speed(unsigned int *speed)
4617 switch (fan_status_access_mode) {
4618 case TPACPI_FAN_RD_TPEC:
4619 /* all except 570, 600e/x, 770e, 770x */
4620 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4621 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4625 *speed = (hi << 8) | lo;
4636 static int fan_set_level(int level)
4638 if (!fan_control_allowed)
4641 switch (fan_control_access_mode) {
4642 case TPACPI_FAN_WR_ACPI_SFAN:
4643 if (level >= 0 && level <= 7) {
4644 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4650 case TPACPI_FAN_WR_ACPI_FANS:
4651 case TPACPI_FAN_WR_TPEC:
4652 if ((level != TP_EC_FAN_AUTO) &&
4653 (level != TP_EC_FAN_FULLSPEED) &&
4654 ((level < 0) || (level > 7)))
4657 /* safety net should the EC not support AUTO
4658 * or FULLSPEED mode bits and just ignore them */
4659 if (level & TP_EC_FAN_FULLSPEED)
4660 level |= 7; /* safety min speed 7 */
4661 else if (level & TP_EC_FAN_FULLSPEED)
4662 level |= 4; /* safety min speed 4 */
4664 if (!acpi_ec_write(fan_status_offset, level))
4667 tp_features.fan_ctrl_status_undef = 0;
4676 static int fan_set_level_safe(int level)
4680 if (!fan_control_allowed)
4683 if (mutex_lock_interruptible(&fan_mutex))
4684 return -ERESTARTSYS;
4686 if (level == TPACPI_FAN_LAST_LEVEL)
4687 level = fan_control_desired_level;
4689 rc = fan_set_level(level);
4691 fan_update_desired_level(level);
4693 mutex_unlock(&fan_mutex);
4697 static int fan_set_enable(void)
4702 if (!fan_control_allowed)
4705 if (mutex_lock_interruptible(&fan_mutex))
4706 return -ERESTARTSYS;
4708 switch (fan_control_access_mode) {
4709 case TPACPI_FAN_WR_ACPI_FANS:
4710 case TPACPI_FAN_WR_TPEC:
4711 rc = fan_get_status(&s);
4715 /* Don't go out of emergency fan mode */
4718 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4721 if (!acpi_ec_write(fan_status_offset, s))
4724 tp_features.fan_ctrl_status_undef = 0;
4729 case TPACPI_FAN_WR_ACPI_SFAN:
4730 rc = fan_get_status(&s);
4736 /* Set fan to at least level 4 */
4739 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4749 mutex_unlock(&fan_mutex);
4753 static int fan_set_disable(void)
4757 if (!fan_control_allowed)
4760 if (mutex_lock_interruptible(&fan_mutex))
4761 return -ERESTARTSYS;
4764 switch (fan_control_access_mode) {
4765 case TPACPI_FAN_WR_ACPI_FANS:
4766 case TPACPI_FAN_WR_TPEC:
4767 if (!acpi_ec_write(fan_status_offset, 0x00))
4770 fan_control_desired_level = 0;
4771 tp_features.fan_ctrl_status_undef = 0;
4775 case TPACPI_FAN_WR_ACPI_SFAN:
4776 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4779 fan_control_desired_level = 0;
4787 mutex_unlock(&fan_mutex);
4791 static int fan_set_speed(int speed)
4795 if (!fan_control_allowed)
4798 if (mutex_lock_interruptible(&fan_mutex))
4799 return -ERESTARTSYS;
4802 switch (fan_control_access_mode) {
4803 case TPACPI_FAN_WR_ACPI_FANS:
4804 if (speed >= 0 && speed <= 65535) {
4805 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4806 speed, speed, speed))
4816 mutex_unlock(&fan_mutex);
4820 static void fan_watchdog_reset(void)
4822 static int fan_watchdog_active;
4824 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4827 if (fan_watchdog_active)
4828 cancel_delayed_work(&fan_watchdog_task);
4830 if (fan_watchdog_maxinterval > 0 &&
4831 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4832 fan_watchdog_active = 1;
4833 if (!schedule_delayed_work(&fan_watchdog_task,
4834 msecs_to_jiffies(fan_watchdog_maxinterval
4837 "failed to schedule the fan watchdog, "
4838 "watchdog will not trigger\n");
4841 fan_watchdog_active = 0;
4844 static void fan_watchdog_fire(struct work_struct *ignored)
4848 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4851 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4852 rc = fan_set_enable();
4854 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4855 "will try again later...\n", -rc);
4856 /* reschedule for later */
4857 fan_watchdog_reset();
4862 * SYSFS fan layout: hwmon compatible (device)
4865 * 0: "disengaged" mode
4867 * 2: native EC "auto" mode (recommended, hardware default)
4869 * pwm*: set speed in manual mode, ignored otherwise.
4870 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4873 * fan*_input: tachometer reading, RPM
4876 * SYSFS fan layout: extensions
4878 * fan_watchdog (driver):
4879 * fan watchdog interval in seconds, 0 disables (default), max 120
4882 /* sysfs fan pwm1_enable ----------------------------------------------- */
4883 static ssize_t fan_pwm1_enable_show(struct device *dev,
4884 struct device_attribute *attr,
4890 res = fan_get_status_safe(&status);
4894 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4895 if (status != fan_control_initial_status) {
4896 tp_features.fan_ctrl_status_undef = 0;
4898 /* Return most likely status. In fact, it
4899 * might be the only possible status */
4900 status = TP_EC_FAN_AUTO;
4904 if (status & TP_EC_FAN_FULLSPEED) {
4906 } else if (status & TP_EC_FAN_AUTO) {
4911 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4914 static ssize_t fan_pwm1_enable_store(struct device *dev,
4915 struct device_attribute *attr,
4916 const char *buf, size_t count)
4921 if (parse_strtoul(buf, 2, &t))
4926 level = TP_EC_FAN_FULLSPEED;
4929 level = TPACPI_FAN_LAST_LEVEL;
4932 level = TP_EC_FAN_AUTO;
4935 /* reserved for software-controlled auto mode */
4941 res = fan_set_level_safe(level);
4947 fan_watchdog_reset();
4952 static struct device_attribute dev_attr_fan_pwm1_enable =
4953 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4954 fan_pwm1_enable_show, fan_pwm1_enable_store);
4956 /* sysfs fan pwm1 ------------------------------------------------------ */
4957 static ssize_t fan_pwm1_show(struct device *dev,
4958 struct device_attribute *attr,
4964 res = fan_get_status_safe(&status);
4968 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4969 if (status != fan_control_initial_status) {
4970 tp_features.fan_ctrl_status_undef = 0;
4972 status = TP_EC_FAN_AUTO;
4977 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4978 status = fan_control_desired_level;
4983 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4986 static ssize_t fan_pwm1_store(struct device *dev,
4987 struct device_attribute *attr,
4988 const char *buf, size_t count)
4992 u8 status, newlevel;
4994 if (parse_strtoul(buf, 255, &s))
4997 /* scale down from 0-255 to 0-7 */
4998 newlevel = (s >> 5) & 0x07;
5000 if (mutex_lock_interruptible(&fan_mutex))
5001 return -ERESTARTSYS;
5003 rc = fan_get_status(&status);
5004 if (!rc && (status &
5005 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5006 rc = fan_set_level(newlevel);
5010 fan_update_desired_level(newlevel);
5011 fan_watchdog_reset();
5015 mutex_unlock(&fan_mutex);
5016 return (rc)? rc : count;
5019 static struct device_attribute dev_attr_fan_pwm1 =
5020 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5021 fan_pwm1_show, fan_pwm1_store);
5023 /* sysfs fan fan1_input ------------------------------------------------ */
5024 static ssize_t fan_fan1_input_show(struct device *dev,
5025 struct device_attribute *attr,
5031 res = fan_get_speed(&speed);
5035 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5038 static struct device_attribute dev_attr_fan_fan1_input =
5039 __ATTR(fan1_input, S_IRUGO,
5040 fan_fan1_input_show, NULL);
5042 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5043 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5046 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5049 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5050 const char *buf, size_t count)
5054 if (parse_strtoul(buf, 120, &t))
5057 if (!fan_control_allowed)
5060 fan_watchdog_maxinterval = t;
5061 fan_watchdog_reset();
5066 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5067 fan_fan_watchdog_show, fan_fan_watchdog_store);
5069 /* --------------------------------------------------------------------- */
5070 static struct attribute *fan_attributes[] = {
5071 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5072 &dev_attr_fan_fan1_input.attr,
5076 static const struct attribute_group fan_attr_group = {
5077 .attrs = fan_attributes,
5080 static int __init fan_init(struct ibm_init_struct *iibm)
5084 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5086 mutex_init(&fan_mutex);
5087 fan_status_access_mode = TPACPI_FAN_NONE;
5088 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5089 fan_control_commands = 0;
5090 fan_watchdog_maxinterval = 0;
5091 tp_features.fan_ctrl_status_undef = 0;
5092 fan_control_desired_level = 7;
5094 TPACPI_ACPIHANDLE_INIT(fans);
5095 TPACPI_ACPIHANDLE_INIT(gfan);
5096 TPACPI_ACPIHANDLE_INIT(sfan);
5099 /* 570, 600e/x, 770e, 770x */
5100 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5102 /* all other ThinkPads: note that even old-style
5103 * ThinkPad ECs supports the fan control register */
5104 if (likely(acpi_ec_read(fan_status_offset,
5105 &fan_control_initial_status))) {
5106 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5108 /* In some ThinkPads, neither the EC nor the ACPI
5109 * DSDT initialize the fan status, and it ends up
5110 * being set to 0x07 when it *could* be either
5113 * Enable for TP-1Y (T43), TP-78 (R51e),
5114 * TP-76 (R52), TP-70 (T43, R52), which are known
5116 if (fan_control_initial_status == 0x07) {
5117 switch (thinkpad_id.ec_model) {
5118 case 0x5931: /* TP-1Y */
5119 case 0x3837: /* TP-78 */
5120 case 0x3637: /* TP-76 */
5121 case 0x3037: /* TP-70 */
5122 printk(TPACPI_NOTICE
5123 "fan_init: initial fan status "
5124 "is unknown, assuming it is "
5126 tp_features.fan_ctrl_status_undef = 1;
5132 "ThinkPad ACPI EC access misbehaving, "
5133 "fan status and control unavailable\n");
5140 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5141 fan_control_commands |=
5142 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5145 /* gfan without sfan means no fan control */
5146 /* all other models implement TP EC 0x2f control */
5150 fan_control_access_mode =
5151 TPACPI_FAN_WR_ACPI_FANS;
5152 fan_control_commands |=
5153 TPACPI_FAN_CMD_SPEED |
5154 TPACPI_FAN_CMD_LEVEL |
5155 TPACPI_FAN_CMD_ENABLE;
5157 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5158 fan_control_commands |=
5159 TPACPI_FAN_CMD_LEVEL |
5160 TPACPI_FAN_CMD_ENABLE;
5165 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5166 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5167 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5168 fan_status_access_mode, fan_control_access_mode);
5170 /* fan control master switch */
5171 if (!fan_control_allowed) {
5172 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5173 fan_control_commands = 0;
5174 dbg_printk(TPACPI_DBG_INIT,
5175 "fan control features disabled by parameter\n");
5178 /* update fan_control_desired_level */
5179 if (fan_status_access_mode != TPACPI_FAN_NONE)
5180 fan_get_status_safe(NULL);
5182 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5183 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5184 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5187 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5188 &driver_attr_fan_watchdog);
5196 static void fan_exit(void)
5198 vdbg_printk(TPACPI_DBG_EXIT,
5199 "cancelling any pending fan watchdog tasks\n");
5201 /* FIXME: can we really do this unconditionally? */
5202 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5203 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5204 &driver_attr_fan_watchdog);
5206 cancel_delayed_work(&fan_watchdog_task);
5207 flush_scheduled_work();
5210 static int fan_read(char *p)
5215 unsigned int speed = 0;
5217 switch (fan_status_access_mode) {
5218 case TPACPI_FAN_RD_ACPI_GFAN:
5219 /* 570, 600e/x, 770e, 770x */
5220 rc = fan_get_status_safe(&status);
5224 len += sprintf(p + len, "status:\t\t%s\n"
5226 (status != 0) ? "enabled" : "disabled", status);
5229 case TPACPI_FAN_RD_TPEC:
5230 /* all except 570, 600e/x, 770e, 770x */
5231 rc = fan_get_status_safe(&status);
5235 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5236 if (status != fan_control_initial_status)
5237 tp_features.fan_ctrl_status_undef = 0;
5239 /* Return most likely status. In fact, it
5240 * might be the only possible status */
5241 status = TP_EC_FAN_AUTO;
5244 len += sprintf(p + len, "status:\t\t%s\n",
5245 (status != 0) ? "enabled" : "disabled");
5247 rc = fan_get_speed(&speed);
5251 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5253 if (status & TP_EC_FAN_FULLSPEED)
5254 /* Disengaged mode takes precedence */
5255 len += sprintf(p + len, "level:\t\tdisengaged\n");
5256 else if (status & TP_EC_FAN_AUTO)
5257 len += sprintf(p + len, "level:\t\tauto\n");
5259 len += sprintf(p + len, "level:\t\t%d\n", status);
5262 case TPACPI_FAN_NONE:
5264 len += sprintf(p + len, "status:\t\tnot supported\n");
5267 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5268 len += sprintf(p + len, "commands:\tlevel <level>");
5270 switch (fan_control_access_mode) {
5271 case TPACPI_FAN_WR_ACPI_SFAN:
5272 len += sprintf(p + len, " (<level> is 0-7)\n");
5276 len += sprintf(p + len, " (<level> is 0-7, "
5277 "auto, disengaged, full-speed)\n");
5282 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5283 len += sprintf(p + len, "commands:\tenable, disable\n"
5284 "commands:\twatchdog <timeout> (<timeout> "
5285 "is 0 (off), 1-120 (seconds))\n");
5287 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5288 len += sprintf(p + len, "commands:\tspeed <speed>"
5289 " (<speed> is 0-65535)\n");
5294 static int fan_write_cmd_level(const char *cmd, int *rc)
5298 if (strlencmp(cmd, "level auto") == 0)
5299 level = TP_EC_FAN_AUTO;
5300 else if ((strlencmp(cmd, "level disengaged") == 0) |
5301 (strlencmp(cmd, "level full-speed") == 0))
5302 level = TP_EC_FAN_FULLSPEED;
5303 else if (sscanf(cmd, "level %d", &level) != 1)
5306 *rc = fan_set_level_safe(level);
5308 printk(TPACPI_ERR "level command accepted for unsupported "
5309 "access mode %d", fan_control_access_mode);
5314 static int fan_write_cmd_enable(const char *cmd, int *rc)
5316 if (strlencmp(cmd, "enable") != 0)
5319 *rc = fan_set_enable();
5321 printk(TPACPI_ERR "enable command accepted for unsupported "
5322 "access mode %d", fan_control_access_mode);
5327 static int fan_write_cmd_disable(const char *cmd, int *rc)
5329 if (strlencmp(cmd, "disable") != 0)
5332 *rc = fan_set_disable();
5334 printk(TPACPI_ERR "disable command accepted for unsupported "
5335 "access mode %d", fan_control_access_mode);
5340 static int fan_write_cmd_speed(const char *cmd, int *rc)
5345 * Support speed <low> <medium> <high> ? */
5347 if (sscanf(cmd, "speed %d", &speed) != 1)
5350 *rc = fan_set_speed(speed);
5352 printk(TPACPI_ERR "speed command accepted for unsupported "
5353 "access mode %d", fan_control_access_mode);
5358 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5362 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5365 if (interval < 0 || interval > 120)
5368 fan_watchdog_maxinterval = interval;
5373 static int fan_write(char *buf)
5378 while (!rc && (cmd = next_cmd(&buf))) {
5379 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5380 fan_write_cmd_level(cmd, &rc)) &&
5381 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5382 (fan_write_cmd_enable(cmd, &rc) ||
5383 fan_write_cmd_disable(cmd, &rc) ||
5384 fan_write_cmd_watchdog(cmd, &rc))) &&
5385 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5386 fan_write_cmd_speed(cmd, &rc))
5390 fan_watchdog_reset();
5396 static struct ibm_struct fan_driver_data = {
5403 /****************************************************************************
5404 ****************************************************************************
5408 ****************************************************************************
5409 ****************************************************************************/
5411 /* sysfs name ---------------------------------------------------------- */
5412 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5413 struct device_attribute *attr,
5416 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5419 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5420 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5422 /* --------------------------------------------------------------------- */
5425 static struct proc_dir_entry *proc_dir;
5428 * Module and infrastructure proble, init and exit handling
5431 static int force_load;
5433 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5434 static const char * __init str_supported(int is_supported)
5436 static char text_unsupported[] __initdata = "not supported";
5438 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5440 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5442 static void ibm_exit(struct ibm_struct *ibm)
5444 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5446 list_del_init(&ibm->all_drivers);
5448 if (ibm->flags.acpi_notify_installed) {
5449 dbg_printk(TPACPI_DBG_EXIT,
5450 "%s: acpi_remove_notify_handler\n", ibm->name);
5452 acpi_remove_notify_handler(*ibm->acpi->handle,
5454 dispatch_acpi_notify);
5455 ibm->flags.acpi_notify_installed = 0;
5456 ibm->flags.acpi_notify_installed = 0;
5459 if (ibm->flags.proc_created) {
5460 dbg_printk(TPACPI_DBG_EXIT,
5461 "%s: remove_proc_entry\n", ibm->name);
5462 remove_proc_entry(ibm->name, proc_dir);
5463 ibm->flags.proc_created = 0;
5466 if (ibm->flags.acpi_driver_registered) {
5467 dbg_printk(TPACPI_DBG_EXIT,
5468 "%s: acpi_bus_unregister_driver\n", ibm->name);
5470 acpi_bus_unregister_driver(ibm->acpi->driver);
5471 kfree(ibm->acpi->driver);
5472 ibm->acpi->driver = NULL;
5473 ibm->flags.acpi_driver_registered = 0;
5476 if (ibm->flags.init_called && ibm->exit) {
5478 ibm->flags.init_called = 0;
5481 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5484 static int __init ibm_init(struct ibm_init_struct *iibm)
5487 struct ibm_struct *ibm = iibm->data;
5488 struct proc_dir_entry *entry;
5490 BUG_ON(ibm == NULL);
5492 INIT_LIST_HEAD(&ibm->all_drivers);
5494 if (ibm->flags.experimental && !experimental)
5497 dbg_printk(TPACPI_DBG_INIT,
5498 "probing for %s\n", ibm->name);
5501 ret = iibm->init(iibm);
5503 return 0; /* probe failed */
5507 ibm->flags.init_called = 1;
5511 if (ibm->acpi->hid) {
5512 ret = register_tpacpi_subdriver(ibm);
5517 if (ibm->acpi->notify) {
5518 ret = setup_acpi_notify(ibm);
5519 if (ret == -ENODEV) {
5520 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5530 dbg_printk(TPACPI_DBG_INIT,
5531 "%s installed\n", ibm->name);
5534 entry = create_proc_entry(ibm->name,
5535 S_IFREG | S_IRUGO | S_IWUSR,
5538 printk(TPACPI_ERR "unable to create proc entry %s\n",
5543 entry->owner = THIS_MODULE;
5545 entry->read_proc = &dispatch_procfs_read;
5547 entry->write_proc = &dispatch_procfs_write;
5548 ibm->flags.proc_created = 1;
5551 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5556 dbg_printk(TPACPI_DBG_INIT,
5557 "%s: at error exit path with result %d\n",
5561 return (ret < 0)? ret : 0;
5566 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5568 const struct dmi_device *dev = NULL;
5569 char ec_fw_string[18];
5574 memset(tp, 0, sizeof(*tp));
5576 if (dmi_name_in_vendors("IBM"))
5577 tp->vendor = PCI_VENDOR_ID_IBM;
5578 else if (dmi_name_in_vendors("LENOVO"))
5579 tp->vendor = PCI_VENDOR_ID_LENOVO;
5583 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5585 if (!tp->bios_version_str)
5587 tp->bios_model = tp->bios_version_str[0]
5588 | (tp->bios_version_str[1] << 8);
5591 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5592 * X32 or newer, all Z series; Some models must have an
5593 * up-to-date BIOS or they will not be detected.
5595 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5597 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5598 if (sscanf(dev->name,
5599 "IBM ThinkPad Embedded Controller -[%17c",
5600 ec_fw_string) == 1) {
5601 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5602 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5604 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5605 tp->ec_model = ec_fw_string[0]
5606 | (ec_fw_string[1] << 8);
5611 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5613 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5614 kfree(tp->model_str);
5615 tp->model_str = NULL;
5619 static int __init probe_for_thinkpad(void)
5627 * Non-ancient models have better DMI tagging, but very old models
5630 is_thinkpad = (thinkpad_id.model_str != NULL);
5632 /* ec is required because many other handles are relative to it */
5633 TPACPI_ACPIHANDLE_INIT(ec);
5637 "Not yet supported ThinkPad detected!\n");
5642 * Risks a regression on very old machines, but reduces potential
5643 * false positives a damn great deal
5646 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5648 if (!is_thinkpad && !force_load)
5655 /* Module init, exit, parameters */
5657 static struct ibm_init_struct ibms_init[] __initdata = {
5659 .init = thinkpad_acpi_driver_init,
5660 .data = &thinkpad_acpi_driver_data,
5663 .init = hotkey_init,
5664 .data = &hotkey_driver_data,
5667 .init = bluetooth_init,
5668 .data = &bluetooth_driver_data,
5672 .data = &wan_driver_data,
5676 .data = &video_driver_data,
5680 .data = &light_driver_data,
5682 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5685 .data = &dock_driver_data[0],
5689 .data = &dock_driver_data[1],
5692 #ifdef CONFIG_THINKPAD_ACPI_BAY
5695 .data = &bay_driver_data,
5700 .data = &cmos_driver_data,
5704 .data = &led_driver_data,
5708 .data = &beep_driver_data,
5711 .init = thermal_init,
5712 .data = &thermal_driver_data,
5715 .data = &ecdump_driver_data,
5718 .init = brightness_init,
5719 .data = &brightness_driver_data,
5722 .data = &volume_driver_data,
5726 .data = &fan_driver_data,
5730 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5733 struct ibm_struct *ibm;
5735 if (!kp || !kp->name || !val)
5738 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5739 ibm = ibms_init[i].data;
5740 WARN_ON(ibm == NULL);
5742 if (!ibm || !ibm->name)
5745 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5746 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5748 strcpy(ibms_init[i].param, val);
5749 strcat(ibms_init[i].param, ",");
5757 module_param(experimental, int, 0);
5758 MODULE_PARM_DESC(experimental,
5759 "Enables experimental features when non-zero");
5761 module_param_named(debug, dbg_level, uint, 0);
5762 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5764 module_param(force_load, bool, 0);
5765 MODULE_PARM_DESC(force_load,
5766 "Attempts to load the driver even on a "
5767 "mis-identified ThinkPad when true");
5769 module_param_named(fan_control, fan_control_allowed, bool, 0);
5770 MODULE_PARM_DESC(fan_control,
5771 "Enables setting fan parameters features when true");
5773 module_param_named(brightness_mode, brightness_mode, int, 0);
5774 MODULE_PARM_DESC(brightness_mode,
5775 "Selects brightness control strategy: "
5776 "0=auto, 1=EC, 2=CMOS, 3=both");
5778 module_param(brightness_enable, uint, 0);
5779 MODULE_PARM_DESC(brightness_enable,
5780 "Enables backlight control when 1, disables when 0");
5782 module_param(hotkey_report_mode, uint, 0);
5783 MODULE_PARM_DESC(hotkey_report_mode,
5784 "used for backwards compatibility with userspace, "
5785 "see documentation");
5787 #define TPACPI_PARAM(feature) \
5788 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5789 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5790 "at module load, see documentation")
5792 TPACPI_PARAM(hotkey);
5793 TPACPI_PARAM(bluetooth);
5794 TPACPI_PARAM(video);
5795 TPACPI_PARAM(light);
5796 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5799 #ifdef CONFIG_THINKPAD_ACPI_BAY
5801 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5805 TPACPI_PARAM(ecdump);
5806 TPACPI_PARAM(brightness);
5807 TPACPI_PARAM(volume);
5810 static void thinkpad_acpi_module_exit(void)
5812 struct ibm_struct *ibm, *itmp;
5814 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5816 list_for_each_entry_safe_reverse(ibm, itmp,
5817 &tpacpi_all_drivers,
5822 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5824 if (tpacpi_inputdev) {
5825 if (tp_features.input_device_registered)
5826 input_unregister_device(tpacpi_inputdev);
5828 input_free_device(tpacpi_inputdev);
5832 hwmon_device_unregister(tpacpi_hwmon);
5834 if (tp_features.sensors_pdev_attrs_registered)
5835 device_remove_file(&tpacpi_sensors_pdev->dev,
5836 &dev_attr_thinkpad_acpi_pdev_name);
5837 if (tpacpi_sensors_pdev)
5838 platform_device_unregister(tpacpi_sensors_pdev);
5840 platform_device_unregister(tpacpi_pdev);
5842 if (tp_features.sensors_pdrv_attrs_registered)
5843 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5844 if (tp_features.platform_drv_attrs_registered)
5845 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5847 if (tp_features.sensors_pdrv_registered)
5848 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5850 if (tp_features.platform_drv_registered)
5851 platform_driver_unregister(&tpacpi_pdriver);
5854 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5856 kfree(thinkpad_id.bios_version_str);
5857 kfree(thinkpad_id.ec_version_str);
5858 kfree(thinkpad_id.model_str);
5862 static int __init thinkpad_acpi_module_init(void)
5866 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5868 /* Parameter checking */
5869 if (hotkey_report_mode > 2)
5872 /* Driver-level probe */
5874 get_thinkpad_model_data(&thinkpad_id);
5875 ret = probe_for_thinkpad();
5877 thinkpad_acpi_module_exit();
5881 /* Driver initialization */
5883 TPACPI_ACPIHANDLE_INIT(ecrd);
5884 TPACPI_ACPIHANDLE_INIT(ecwr);
5886 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5889 "unable to create proc dir " TPACPI_PROC_DIR);
5890 thinkpad_acpi_module_exit();
5893 proc_dir->owner = THIS_MODULE;
5895 ret = platform_driver_register(&tpacpi_pdriver);
5898 "unable to register main platform driver\n");
5899 thinkpad_acpi_module_exit();
5902 tp_features.platform_drv_registered = 1;
5904 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5907 "unable to register hwmon platform driver\n");
5908 thinkpad_acpi_module_exit();
5911 tp_features.sensors_pdrv_registered = 1;
5913 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5915 tp_features.platform_drv_attrs_registered = 1;
5916 ret = tpacpi_create_driver_attributes(
5917 &tpacpi_hwmon_pdriver.driver);
5921 "unable to create sysfs driver attributes\n");
5922 thinkpad_acpi_module_exit();
5925 tp_features.sensors_pdrv_attrs_registered = 1;
5928 /* Device initialization */
5929 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
5931 if (IS_ERR(tpacpi_pdev)) {
5932 ret = PTR_ERR(tpacpi_pdev);
5934 printk(TPACPI_ERR "unable to register platform device\n");
5935 thinkpad_acpi_module_exit();
5938 tpacpi_sensors_pdev = platform_device_register_simple(
5939 TPACPI_HWMON_DRVR_NAME,
5941 if (IS_ERR(tpacpi_sensors_pdev)) {
5942 ret = PTR_ERR(tpacpi_sensors_pdev);
5943 tpacpi_sensors_pdev = NULL;
5945 "unable to register hwmon platform device\n");
5946 thinkpad_acpi_module_exit();
5949 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5950 &dev_attr_thinkpad_acpi_pdev_name);
5953 "unable to create sysfs hwmon device attributes\n");
5954 thinkpad_acpi_module_exit();
5957 tp_features.sensors_pdev_attrs_registered = 1;
5958 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5959 if (IS_ERR(tpacpi_hwmon)) {
5960 ret = PTR_ERR(tpacpi_hwmon);
5961 tpacpi_hwmon = NULL;
5962 printk(TPACPI_ERR "unable to register hwmon device\n");
5963 thinkpad_acpi_module_exit();
5966 mutex_init(&tpacpi_inputdev_send_mutex);
5967 tpacpi_inputdev = input_allocate_device();
5968 if (!tpacpi_inputdev) {
5969 printk(TPACPI_ERR "unable to allocate input device\n");
5970 thinkpad_acpi_module_exit();
5973 /* Prepare input device, but don't register */
5974 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5975 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
5976 tpacpi_inputdev->id.bustype = BUS_HOST;
5977 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5978 thinkpad_id.vendor :
5980 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5981 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5983 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5984 ret = ibm_init(&ibms_init[i]);
5985 if (ret >= 0 && *ibms_init[i].param)
5986 ret = ibms_init[i].data->write(ibms_init[i].param);
5988 thinkpad_acpi_module_exit();
5992 ret = input_register_device(tpacpi_inputdev);
5994 printk(TPACPI_ERR "unable to register input device\n");
5995 thinkpad_acpi_module_exit();
5998 tp_features.input_device_registered = 1;
6001 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6005 /* Please remove this in year 2009 */
6006 MODULE_ALIAS("ibm_acpi");
6009 * DMI matching for module autoloading
6011 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6012 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6014 * Only models listed in thinkwiki will be supported, so add yours
6015 * if it is not there yet.
6017 #define IBM_BIOS_MODULE_ALIAS(__type) \
6018 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6020 /* Non-ancient thinkpads */
6021 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6022 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6024 /* Ancient thinkpad BIOSes have to be identified by
6025 * BIOS type or model number, and there are far less
6026 * BIOS types than model numbers... */
6027 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6028 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6029 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6031 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6032 MODULE_DESCRIPTION(TPACPI_DESC);
6033 MODULE_VERSION(TPACPI_VERSION);
6034 MODULE_LICENSE("GPL");
6036 module_init(thinkpad_acpi_module_init);
6037 module_exit(thinkpad_acpi_module_exit);