2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define TPACPI_VERSION "0.21"
25 #define TPACPI_SYSFS_VERSION 0x020200
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
80 #include <linux/pci_ids.h>
83 /* ThinkPad CMOS commands */
84 #define TP_CMOS_VOLUME_DOWN 0
85 #define TP_CMOS_VOLUME_UP 1
86 #define TP_CMOS_VOLUME_MUTE 2
87 #define TP_CMOS_BRIGHTNESS_UP 4
88 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 #define TP_CMOS_THINKLIGHT_ON 12
90 #define TP_CMOS_THINKLIGHT_OFF 13
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
101 /* NVRAM bit masks */
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123 #define TPACPI_HKEY_INPUT_VERSION 0x4101
125 /* ACPI \WGSV commands */
127 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
128 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
129 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
130 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
133 /* TP_ACPI_WGSV_GET_STATE bits */
135 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
136 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
137 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
138 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
139 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
140 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
141 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
142 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
143 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
144 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
147 /****************************************************************************
151 #define TPACPI_NAME "thinkpad"
152 #define TPACPI_DESC "ThinkPad ACPI Extras"
153 #define TPACPI_FILE TPACPI_NAME "_acpi"
154 #define TPACPI_URL "http://ibm-acpi.sf.net/"
155 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
157 #define TPACPI_PROC_DIR "ibm"
158 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
159 #define TPACPI_DRVR_NAME TPACPI_FILE
160 #define TPACPI_DRVR_SHORTNAME "tpacpi"
161 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
163 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
164 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
166 #define TPACPI_MAX_ACPI_ARGS 3
168 /* rfkill switches */
170 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
171 TPACPI_RFK_WWAN_SW_ID,
172 TPACPI_RFK_UWB_SW_ID,
176 #define TPACPI_LOG TPACPI_FILE ": "
177 #define TPACPI_ERR KERN_ERR TPACPI_LOG
178 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
179 #define TPACPI_INFO KERN_INFO TPACPI_LOG
180 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
182 #define TPACPI_DBG_ALL 0xffff
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define dbg_printk(a_dbg_level, format, arg...) \
186 do { if (dbg_level & a_dbg_level) \
187 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
189 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
190 #define vdbg_printk(a_dbg_level, format, arg...) \
191 dbg_printk(a_dbg_level, format, ## arg)
192 static const char *str_supported(int is_supported);
194 #define vdbg_printk(a_dbg_level, format, arg...)
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
215 struct acpi_device *device;
221 int (*read) (char *);
222 int (*write) (char *);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
226 void (*shutdown) (void);
228 struct list_head all_drivers;
230 struct tp_acpi_drv_struct *acpi;
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
241 struct ibm_init_struct {
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
249 #ifdef CONFIG_THINKPAD_ACPI_BAY
262 u32 bright_16levels:1;
263 u32 bright_acpimode:1;
266 u32 fan_ctrl_status_undef:1;
267 u32 input_device_registered:1;
268 u32 platform_drv_registered:1;
269 u32 platform_drv_attrs_registered:1;
270 u32 sensors_pdrv_registered:1;
271 u32 sensors_pdrv_attrs_registered:1;
272 u32 sensors_pdev_attrs_registered:1;
273 u32 hotkey_poll_active:1;
277 u16 hotkey_mask_ff:1;
278 u16 bright_cmos_ec_unsync:1;
281 struct thinkpad_id_data {
282 unsigned int vendor; /* ThinkPad vendor:
283 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
285 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
286 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
288 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
291 char *model_str; /* ThinkPad T43 */
292 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
294 static struct thinkpad_id_data thinkpad_id;
297 TPACPI_LIFE_INIT = 0,
302 static int experimental;
303 static u32 dbg_level;
305 static struct workqueue_struct *tpacpi_wq;
307 /* Special LED class that can defer work */
308 struct tpacpi_led_classdev {
309 struct led_classdev led_classdev;
310 struct work_struct work;
311 enum led_brightness new_brightness;
315 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
316 static int dbg_wlswemul;
317 static int tpacpi_wlsw_emulstate;
318 static int dbg_bluetoothemul;
319 static int tpacpi_bluetooth_emulstate;
320 static int dbg_wwanemul;
321 static int tpacpi_wwan_emulstate;
322 static int dbg_uwbemul;
323 static int tpacpi_uwb_emulstate;
327 /****************************************************************************
328 ****************************************************************************
330 * ACPI Helpers and device model
332 ****************************************************************************
333 ****************************************************************************/
335 /*************************************************************************
339 static acpi_handle root_handle;
341 #define TPACPI_HANDLE(object, parent, paths...) \
342 static acpi_handle object##_handle; \
343 static acpi_handle *object##_parent = &parent##_handle; \
344 static char *object##_path; \
345 static char *object##_paths[] = { paths }
347 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
348 "\\_SB.PCI.ISA.EC", /* 570 */
349 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
350 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
351 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
352 "\\_SB.PCI0.ICH3.EC0", /* R31 */
353 "\\_SB.PCI0.LPC.EC", /* all others */
356 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
357 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
359 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
361 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
362 "\\CMS", /* R40, R40e */
365 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
366 "^HKEY", /* R30, R31 */
367 "HKEY", /* all others */
370 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
371 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
372 "\\_SB.PCI0.VID0", /* 770e */
373 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
374 "\\_SB.PCI0.AGP.VID", /* all others */
378 /*************************************************************************
382 static int acpi_evalf(acpi_handle handle,
383 void *res, char *method, char *fmt, ...)
386 struct acpi_object_list params;
387 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
388 struct acpi_buffer result, *resultp;
389 union acpi_object out_obj;
397 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
410 params.pointer = &in_objs[0];
417 in_objs[params.count].integer.value = va_arg(ap, int);
418 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
420 /* add more types as needed */
422 printk(TPACPI_ERR "acpi_evalf() called "
423 "with invalid format character '%c'\n", c);
429 if (res_type != 'v') {
430 result.length = sizeof(out_obj);
431 result.pointer = &out_obj;
436 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
441 *(int *)res = out_obj.integer.value;
442 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
445 success = status == AE_OK;
447 /* add more types as needed */
449 printk(TPACPI_ERR "acpi_evalf() called "
450 "with invalid format character '%c'\n", res_type);
454 if (!success && !quiet)
455 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
456 method, fmt0, status);
461 static int acpi_ec_read(int i, u8 *p)
466 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
470 if (ec_read(i, p) < 0)
477 static int acpi_ec_write(int i, u8 v)
480 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
483 if (ec_write(i, v) < 0)
490 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
491 static int _sta(acpi_handle handle)
495 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
502 static int issue_thinkpad_cmos_command(int cmos_cmd)
507 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
513 /*************************************************************************
517 #define TPACPI_ACPIHANDLE_INIT(object) \
518 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
519 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
521 static void drv_acpi_handle_init(char *name,
522 acpi_handle *handle, acpi_handle parent,
523 char **paths, int num_paths, char **path)
528 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
531 for (i = 0; i < num_paths; i++) {
532 status = acpi_get_handle(parent, paths[i], handle);
533 if (ACPI_SUCCESS(status)) {
535 dbg_printk(TPACPI_DBG_INIT,
536 "Found ACPI handle %s for %s\n",
542 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
547 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
549 struct ibm_struct *ibm = data;
551 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
554 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
557 ibm->acpi->notify(ibm, event);
560 static int __init setup_acpi_notify(struct ibm_struct *ibm)
567 if (!*ibm->acpi->handle)
570 vdbg_printk(TPACPI_DBG_INIT,
571 "setting up ACPI notify for %s\n", ibm->name);
573 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
575 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
580 ibm->acpi->device->driver_data = ibm;
581 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
582 TPACPI_ACPI_EVENT_PREFIX,
585 status = acpi_install_notify_handler(*ibm->acpi->handle,
586 ibm->acpi->type, dispatch_acpi_notify, ibm);
587 if (ACPI_FAILURE(status)) {
588 if (status == AE_ALREADY_EXISTS) {
590 "another device driver is already "
591 "handling %s events\n", ibm->name);
594 "acpi_install_notify_handler(%s) failed: %d\n",
599 ibm->flags.acpi_notify_installed = 1;
603 static int __init tpacpi_device_add(struct acpi_device *device)
608 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
612 dbg_printk(TPACPI_DBG_INIT,
613 "registering %s as an ACPI driver\n", ibm->name);
617 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
618 if (!ibm->acpi->driver) {
620 "failed to allocate memory for ibm->acpi->driver\n");
624 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
625 ibm->acpi->driver->ids = ibm->acpi->hid;
627 ibm->acpi->driver->ops.add = &tpacpi_device_add;
629 rc = acpi_bus_register_driver(ibm->acpi->driver);
631 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
633 kfree(ibm->acpi->driver);
634 ibm->acpi->driver = NULL;
636 ibm->flags.acpi_driver_registered = 1;
642 /****************************************************************************
643 ****************************************************************************
647 ****************************************************************************
648 ****************************************************************************/
650 static int dispatch_procfs_read(char *page, char **start, off_t off,
651 int count, int *eof, void *data)
653 struct ibm_struct *ibm = data;
656 if (!ibm || !ibm->read)
659 len = ibm->read(page);
663 if (len <= off + count)
675 static int dispatch_procfs_write(struct file *file,
676 const char __user *userbuf,
677 unsigned long count, void *data)
679 struct ibm_struct *ibm = data;
683 if (!ibm || !ibm->write)
686 kernbuf = kmalloc(count + 2, GFP_KERNEL);
690 if (copy_from_user(kernbuf, userbuf, count)) {
696 strcat(kernbuf, ",");
697 ret = ibm->write(kernbuf);
706 static char *next_cmd(char **cmds)
711 while ((end = strchr(start, ',')) && end == start)
723 /****************************************************************************
724 ****************************************************************************
726 * Device model: input, hwmon and platform
728 ****************************************************************************
729 ****************************************************************************/
731 static struct platform_device *tpacpi_pdev;
732 static struct platform_device *tpacpi_sensors_pdev;
733 static struct device *tpacpi_hwmon;
734 static struct input_dev *tpacpi_inputdev;
735 static struct mutex tpacpi_inputdev_send_mutex;
736 static LIST_HEAD(tpacpi_all_drivers);
738 static int tpacpi_suspend_handler(struct platform_device *pdev,
741 struct ibm_struct *ibm, *itmp;
743 list_for_each_entry_safe(ibm, itmp,
747 (ibm->suspend)(state);
753 static int tpacpi_resume_handler(struct platform_device *pdev)
755 struct ibm_struct *ibm, *itmp;
757 list_for_each_entry_safe(ibm, itmp,
767 static void tpacpi_shutdown_handler(struct platform_device *pdev)
769 struct ibm_struct *ibm, *itmp;
771 list_for_each_entry_safe(ibm, itmp,
779 static struct platform_driver tpacpi_pdriver = {
781 .name = TPACPI_DRVR_NAME,
782 .owner = THIS_MODULE,
784 .suspend = tpacpi_suspend_handler,
785 .resume = tpacpi_resume_handler,
786 .shutdown = tpacpi_shutdown_handler,
789 static struct platform_driver tpacpi_hwmon_pdriver = {
791 .name = TPACPI_HWMON_DRVR_NAME,
792 .owner = THIS_MODULE,
796 /*************************************************************************
797 * sysfs support helpers
800 struct attribute_set {
801 unsigned int members, max_members;
802 struct attribute_group group;
805 struct attribute_set_obj {
806 struct attribute_set s;
808 } __attribute__((packed));
810 static struct attribute_set *create_attr_set(unsigned int max_members,
813 struct attribute_set_obj *sobj;
815 if (max_members == 0)
818 /* Allocates space for implicit NULL at the end too */
819 sobj = kzalloc(sizeof(struct attribute_set_obj) +
820 max_members * sizeof(struct attribute *),
824 sobj->s.max_members = max_members;
825 sobj->s.group.attrs = &sobj->a;
826 sobj->s.group.name = name;
831 #define destroy_attr_set(_set) \
834 /* not multi-threaded safe, use it in a single thread per set */
835 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
840 if (s->members >= s->max_members)
843 s->group.attrs[s->members] = attr;
849 static int add_many_to_attr_set(struct attribute_set *s,
850 struct attribute **attr,
855 for (i = 0; i < count; i++) {
856 res = add_to_attr_set(s, attr[i]);
864 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
866 sysfs_remove_group(kobj, &s->group);
870 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
871 sysfs_create_group(_kobj, &_attr_set->group)
873 static int parse_strtoul(const char *buf,
874 unsigned long max, unsigned long *value)
878 while (*buf && isspace(*buf))
880 *value = simple_strtoul(buf, &endp, 0);
881 while (*endp && isspace(*endp))
883 if (*endp || *value > max)
889 static void tpacpi_disable_brightness_delay(void)
891 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
893 "ACPI backlight control delay disabled\n");
896 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
898 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
899 union acpi_object *obj;
902 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
903 obj = (union acpi_object *)buffer.pointer;
904 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
905 printk(TPACPI_ERR "Unknown _BCL data, "
906 "please report this to %s\n", TPACPI_MAIL);
909 rc = obj->package.count;
915 kfree(buffer.pointer);
919 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
920 u32 lvl, void *context, void **rv)
922 char name[ACPI_PATH_SEGMENT_LENGTH];
923 struct acpi_buffer buffer = { sizeof(name), &name };
925 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
926 !strncmp("_BCL", name, sizeof(name) - 1)) {
928 **(int **)rv = tpacpi_query_bcl_levels(handle);
929 return AE_CTRL_TERMINATE;
936 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
938 static int __init tpacpi_check_std_acpi_brightness_support(void)
942 void *bcl_ptr = &bcl_levels;
945 TPACPI_ACPIHANDLE_INIT(vid);
951 * Search for a _BCL method, and execute it. This is safe on all
952 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
953 * BIOS in ACPI backlight control mode. We do NOT have to care
954 * about calling the _BCL method in an enabled video device, any
955 * will do for our purposes.
958 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
959 tpacpi_acpi_walk_find_bcl, NULL,
962 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
963 tp_features.bright_acpimode = 1;
964 return (bcl_levels - 2);
970 static int __init tpacpi_new_rfkill(const unsigned int id,
972 const enum rfkill_type rfktype,
974 const bool set_default,
975 int (*toggle_radio)(void *, enum rfkill_state),
976 int (*get_state)(void *, enum rfkill_state *))
979 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
981 res = get_state(NULL, &initial_state);
984 "failed to read initial state for %s, error %d; "
985 "will turn radio off\n", name, res);
986 } else if (set_default) {
987 /* try to set the initial state as the default for the rfkill
988 * type, since we ask the firmware to preserve it across S5 in
990 rfkill_set_default(rfktype,
991 (initial_state == RFKILL_STATE_UNBLOCKED) ?
992 RFKILL_STATE_UNBLOCKED :
993 RFKILL_STATE_SOFT_BLOCKED);
996 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
999 "failed to allocate memory for rfkill class\n");
1003 (*rfk)->name = name;
1004 (*rfk)->get_state = get_state;
1005 (*rfk)->toggle_radio = toggle_radio;
1006 (*rfk)->state = initial_state;
1008 res = rfkill_register(*rfk);
1011 "failed to register %s rfkill switch: %d\n",
1021 /*************************************************************************
1022 * thinkpad-acpi driver attributes
1025 /* interface_version --------------------------------------------------- */
1026 static ssize_t tpacpi_driver_interface_version_show(
1027 struct device_driver *drv,
1030 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1033 static DRIVER_ATTR(interface_version, S_IRUGO,
1034 tpacpi_driver_interface_version_show, NULL);
1036 /* debug_level --------------------------------------------------------- */
1037 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1040 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1043 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1044 const char *buf, size_t count)
1048 if (parse_strtoul(buf, 0xffff, &t))
1056 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1057 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1059 /* version ------------------------------------------------------------- */
1060 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1063 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1064 TPACPI_DESC, TPACPI_VERSION);
1067 static DRIVER_ATTR(version, S_IRUGO,
1068 tpacpi_driver_version_show, NULL);
1070 /* --------------------------------------------------------------------- */
1072 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1074 static void tpacpi_send_radiosw_update(void);
1076 /* wlsw_emulstate ------------------------------------------------------ */
1077 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1080 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1083 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1084 const char *buf, size_t count)
1088 if (parse_strtoul(buf, 1, &t))
1091 if (tpacpi_wlsw_emulstate != t) {
1092 tpacpi_wlsw_emulstate = !!t;
1093 tpacpi_send_radiosw_update();
1095 tpacpi_wlsw_emulstate = !!t;
1100 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1101 tpacpi_driver_wlsw_emulstate_show,
1102 tpacpi_driver_wlsw_emulstate_store);
1104 /* bluetooth_emulstate ------------------------------------------------- */
1105 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1106 struct device_driver *drv,
1109 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1112 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1113 struct device_driver *drv,
1114 const char *buf, size_t count)
1118 if (parse_strtoul(buf, 1, &t))
1121 tpacpi_bluetooth_emulstate = !!t;
1126 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1127 tpacpi_driver_bluetooth_emulstate_show,
1128 tpacpi_driver_bluetooth_emulstate_store);
1130 /* wwan_emulstate ------------------------------------------------- */
1131 static ssize_t tpacpi_driver_wwan_emulstate_show(
1132 struct device_driver *drv,
1135 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1138 static ssize_t tpacpi_driver_wwan_emulstate_store(
1139 struct device_driver *drv,
1140 const char *buf, size_t count)
1144 if (parse_strtoul(buf, 1, &t))
1147 tpacpi_wwan_emulstate = !!t;
1152 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1153 tpacpi_driver_wwan_emulstate_show,
1154 tpacpi_driver_wwan_emulstate_store);
1156 /* uwb_emulstate ------------------------------------------------- */
1157 static ssize_t tpacpi_driver_uwb_emulstate_show(
1158 struct device_driver *drv,
1161 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1164 static ssize_t tpacpi_driver_uwb_emulstate_store(
1165 struct device_driver *drv,
1166 const char *buf, size_t count)
1170 if (parse_strtoul(buf, 1, &t))
1173 tpacpi_uwb_emulstate = !!t;
1178 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1179 tpacpi_driver_uwb_emulstate_show,
1180 tpacpi_driver_uwb_emulstate_store);
1183 /* --------------------------------------------------------------------- */
1185 static struct driver_attribute *tpacpi_driver_attributes[] = {
1186 &driver_attr_debug_level, &driver_attr_version,
1187 &driver_attr_interface_version,
1190 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1196 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1197 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1201 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1202 if (!res && dbg_wlswemul)
1203 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1204 if (!res && dbg_bluetoothemul)
1205 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1206 if (!res && dbg_wwanemul)
1207 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1208 if (!res && dbg_uwbemul)
1209 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1215 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1219 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1220 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1222 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1223 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1224 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1225 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1226 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1230 /****************************************************************************
1231 ****************************************************************************
1235 ****************************************************************************
1236 ****************************************************************************/
1238 /*************************************************************************
1239 * thinkpad-acpi init subdriver
1242 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1244 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1245 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1247 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1248 (thinkpad_id.bios_version_str) ?
1249 thinkpad_id.bios_version_str : "unknown",
1250 (thinkpad_id.ec_version_str) ?
1251 thinkpad_id.ec_version_str : "unknown");
1253 if (thinkpad_id.vendor && thinkpad_id.model_str)
1254 printk(TPACPI_INFO "%s %s, model %s\n",
1255 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1256 "IBM" : ((thinkpad_id.vendor ==
1257 PCI_VENDOR_ID_LENOVO) ?
1258 "Lenovo" : "Unknown vendor"),
1259 thinkpad_id.model_str,
1260 (thinkpad_id.nummodel_str) ?
1261 thinkpad_id.nummodel_str : "unknown");
1266 static int thinkpad_acpi_driver_read(char *p)
1270 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1271 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1276 static struct ibm_struct thinkpad_acpi_driver_data = {
1278 .read = thinkpad_acpi_driver_read,
1281 /*************************************************************************
1285 enum { /* hot key scan codes (derived from ACPI DSDT) */
1286 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1287 TP_ACPI_HOTKEYSCAN_FNF2,
1288 TP_ACPI_HOTKEYSCAN_FNF3,
1289 TP_ACPI_HOTKEYSCAN_FNF4,
1290 TP_ACPI_HOTKEYSCAN_FNF5,
1291 TP_ACPI_HOTKEYSCAN_FNF6,
1292 TP_ACPI_HOTKEYSCAN_FNF7,
1293 TP_ACPI_HOTKEYSCAN_FNF8,
1294 TP_ACPI_HOTKEYSCAN_FNF9,
1295 TP_ACPI_HOTKEYSCAN_FNF10,
1296 TP_ACPI_HOTKEYSCAN_FNF11,
1297 TP_ACPI_HOTKEYSCAN_FNF12,
1298 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1299 TP_ACPI_HOTKEYSCAN_FNINSERT,
1300 TP_ACPI_HOTKEYSCAN_FNDELETE,
1301 TP_ACPI_HOTKEYSCAN_FNHOME,
1302 TP_ACPI_HOTKEYSCAN_FNEND,
1303 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1304 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1305 TP_ACPI_HOTKEYSCAN_FNSPACE,
1306 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1307 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1308 TP_ACPI_HOTKEYSCAN_MUTE,
1309 TP_ACPI_HOTKEYSCAN_THINKPAD,
1312 enum { /* Keys available through NVRAM polling */
1313 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1314 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1317 enum { /* Positions of some of the keys in hotkey masks */
1318 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1319 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1320 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1321 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1322 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1323 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1324 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1325 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1326 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1327 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1328 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1331 enum { /* NVRAM to ACPI HKEY group map */
1332 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1333 TP_ACPI_HKEY_ZOOM_MASK |
1334 TP_ACPI_HKEY_DISPSWTCH_MASK |
1335 TP_ACPI_HKEY_HIBERNATE_MASK,
1336 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1337 TP_ACPI_HKEY_BRGHTDWN_MASK,
1338 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1339 TP_ACPI_HKEY_VOLDWN_MASK |
1340 TP_ACPI_HKEY_MUTE_MASK,
1343 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1344 struct tp_nvram_state {
1345 u16 thinkpad_toggle:1;
1347 u16 display_toggle:1;
1348 u16 thinklight_toggle:1;
1349 u16 hibernate_toggle:1;
1350 u16 displayexp_toggle:1;
1351 u16 display_state:1;
1352 u16 brightness_toggle:1;
1353 u16 volume_toggle:1;
1356 u8 brightness_level;
1360 static struct task_struct *tpacpi_hotkey_task;
1361 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1362 static int hotkey_poll_freq = 10; /* Hz */
1363 static struct mutex hotkey_thread_mutex;
1364 static struct mutex hotkey_thread_data_mutex;
1365 static unsigned int hotkey_config_change;
1367 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1369 #define hotkey_source_mask 0U
1371 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1373 static struct mutex hotkey_mutex;
1375 static enum { /* Reasons for waking up */
1376 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1377 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1378 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1379 } hotkey_wakeup_reason;
1381 static int hotkey_autosleep_ack;
1383 static int hotkey_orig_status;
1384 static u32 hotkey_orig_mask;
1385 static u32 hotkey_all_mask;
1386 static u32 hotkey_reserved_mask;
1387 static u32 hotkey_mask;
1389 static unsigned int hotkey_report_mode;
1391 static u16 *hotkey_keycode_map;
1393 static struct attribute_set *hotkey_dev_attributes;
1395 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1396 #define HOTKEY_CONFIG_CRITICAL_START \
1398 mutex_lock(&hotkey_thread_data_mutex); \
1399 hotkey_config_change++; \
1401 #define HOTKEY_CONFIG_CRITICAL_END \
1402 mutex_unlock(&hotkey_thread_data_mutex);
1404 #define HOTKEY_CONFIG_CRITICAL_START
1405 #define HOTKEY_CONFIG_CRITICAL_END
1406 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1408 /* HKEY.MHKG() return bits */
1409 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1411 static int hotkey_get_wlsw(int *status)
1413 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1415 *status = !!tpacpi_wlsw_emulstate;
1419 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1424 static int hotkey_get_tablet_mode(int *status)
1428 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1431 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1436 * Call with hotkey_mutex held
1438 static int hotkey_mask_get(void)
1442 if (tp_features.hotkey_mask) {
1443 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1446 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1452 * Call with hotkey_mutex held
1454 static int hotkey_mask_set(u32 mask)
1459 if (tp_features.hotkey_mask) {
1460 if (!tp_warned.hotkey_mask_ff &&
1461 (mask == 0xffff || mask == 0xffffff ||
1462 mask == 0xffffffff)) {
1463 tp_warned.hotkey_mask_ff = 1;
1464 printk(TPACPI_NOTICE
1465 "setting the hotkey mask to 0x%08x is likely "
1466 "not the best way to go about it\n", mask);
1467 printk(TPACPI_NOTICE
1468 "please consider using the driver defaults, "
1469 "and refer to up-to-date thinkpad-acpi "
1473 HOTKEY_CONFIG_CRITICAL_START
1474 for (i = 0; i < 32; i++) {
1476 /* enable in firmware mask only keys not in NVRAM
1477 * mode, but enable the key in the cached hotkey_mask
1478 * regardless of mode, or the key will end up
1479 * disabled by hotkey_mask_get() */
1480 if (!acpi_evalf(hkey_handle,
1481 NULL, "MHKM", "vdd", i + 1,
1482 !!((mask & ~hotkey_source_mask) & m))) {
1486 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1489 HOTKEY_CONFIG_CRITICAL_END
1491 /* hotkey_mask_get must be called unconditionally below */
1492 if (!hotkey_mask_get() && !rc &&
1493 (hotkey_mask & ~hotkey_source_mask) !=
1494 (mask & ~hotkey_source_mask)) {
1495 printk(TPACPI_NOTICE
1496 "requested hot key mask 0x%08x, but "
1497 "firmware forced it to 0x%08x\n",
1501 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1502 HOTKEY_CONFIG_CRITICAL_START
1503 hotkey_mask = mask & hotkey_source_mask;
1504 HOTKEY_CONFIG_CRITICAL_END
1506 if (hotkey_mask != mask) {
1507 printk(TPACPI_NOTICE
1508 "requested hot key mask 0x%08x, "
1509 "forced to 0x%08x (NVRAM poll mask is "
1510 "0x%08x): no firmware mask support\n",
1511 mask, hotkey_mask, hotkey_source_mask);
1516 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1522 static int hotkey_status_get(int *status)
1524 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1530 static int hotkey_status_set(int status)
1532 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1538 static void tpacpi_input_send_tabletsw(void)
1542 if (tp_features.hotkey_tablet &&
1543 !hotkey_get_tablet_mode(&state)) {
1544 mutex_lock(&tpacpi_inputdev_send_mutex);
1546 input_report_switch(tpacpi_inputdev,
1547 SW_TABLET_MODE, !!state);
1548 input_sync(tpacpi_inputdev);
1550 mutex_unlock(&tpacpi_inputdev_send_mutex);
1554 static void tpacpi_input_send_key(unsigned int scancode)
1556 unsigned int keycode;
1558 keycode = hotkey_keycode_map[scancode];
1560 if (keycode != KEY_RESERVED) {
1561 mutex_lock(&tpacpi_inputdev_send_mutex);
1563 input_report_key(tpacpi_inputdev, keycode, 1);
1564 if (keycode == KEY_UNKNOWN)
1565 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1567 input_sync(tpacpi_inputdev);
1569 input_report_key(tpacpi_inputdev, keycode, 0);
1570 if (keycode == KEY_UNKNOWN)
1571 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1573 input_sync(tpacpi_inputdev);
1575 mutex_unlock(&tpacpi_inputdev_send_mutex);
1579 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1580 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1582 static void tpacpi_hotkey_send_key(unsigned int scancode)
1584 tpacpi_input_send_key(scancode);
1585 if (hotkey_report_mode < 2) {
1586 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1587 0x80, 0x1001 + scancode);
1591 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1595 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1596 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1597 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1598 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1599 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1600 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1602 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1603 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1604 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1606 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1607 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1608 n->displayexp_toggle =
1609 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1611 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1612 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1613 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1614 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1615 n->brightness_toggle =
1616 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1618 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1619 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1620 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1621 >> TP_NVRAM_POS_LEVEL_VOLUME;
1622 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1623 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1627 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1629 if ((mask & (1 << __scancode)) && \
1630 oldn->__member != newn->__member) \
1631 tpacpi_hotkey_send_key(__scancode); \
1634 #define TPACPI_MAY_SEND_KEY(__scancode) \
1635 do { if (mask & (1 << __scancode)) \
1636 tpacpi_hotkey_send_key(__scancode); } while (0)
1638 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1639 struct tp_nvram_state *newn,
1642 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1643 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1644 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1645 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1647 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1649 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1652 if (oldn->volume_toggle != newn->volume_toggle) {
1653 if (oldn->mute != newn->mute) {
1654 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1656 if (oldn->volume_level > newn->volume_level) {
1657 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1658 } else if (oldn->volume_level < newn->volume_level) {
1659 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1660 } else if (oldn->mute == newn->mute) {
1661 /* repeated key presses that didn't change state */
1663 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1664 } else if (newn->volume_level != 0) {
1665 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1667 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1672 /* handle brightness */
1673 if (oldn->brightness_toggle != newn->brightness_toggle) {
1674 if (oldn->brightness_level < newn->brightness_level) {
1675 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1676 } else if (oldn->brightness_level > newn->brightness_level) {
1677 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1679 /* repeated key presses that didn't change state */
1680 if (newn->brightness_level != 0) {
1681 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1683 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1689 #undef TPACPI_COMPARE_KEY
1690 #undef TPACPI_MAY_SEND_KEY
1692 static int hotkey_kthread(void *data)
1694 struct tp_nvram_state s[2];
1696 unsigned int si, so;
1698 unsigned int change_detector, must_reset;
1700 mutex_lock(&hotkey_thread_mutex);
1702 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1711 /* Initial state for compares */
1712 mutex_lock(&hotkey_thread_data_mutex);
1713 change_detector = hotkey_config_change;
1714 mask = hotkey_source_mask & hotkey_mask;
1715 mutex_unlock(&hotkey_thread_data_mutex);
1716 hotkey_read_nvram(&s[so], mask);
1718 while (!kthread_should_stop() && hotkey_poll_freq) {
1720 t = 1000/hotkey_poll_freq;
1721 t = msleep_interruptible(t);
1722 if (unlikely(kthread_should_stop()))
1724 must_reset = try_to_freeze();
1725 if (t > 0 && !must_reset)
1728 mutex_lock(&hotkey_thread_data_mutex);
1729 if (must_reset || hotkey_config_change != change_detector) {
1730 /* forget old state on thaw or config change */
1733 change_detector = hotkey_config_change;
1735 mask = hotkey_source_mask & hotkey_mask;
1736 mutex_unlock(&hotkey_thread_data_mutex);
1739 hotkey_read_nvram(&s[si], mask);
1740 if (likely(si != so)) {
1741 hotkey_compare_and_issue_event(&s[so], &s[si],
1751 mutex_unlock(&hotkey_thread_mutex);
1755 static void hotkey_poll_stop_sync(void)
1757 if (tpacpi_hotkey_task) {
1758 if (frozen(tpacpi_hotkey_task) ||
1759 freezing(tpacpi_hotkey_task))
1760 thaw_process(tpacpi_hotkey_task);
1762 kthread_stop(tpacpi_hotkey_task);
1763 tpacpi_hotkey_task = NULL;
1764 mutex_lock(&hotkey_thread_mutex);
1765 /* at this point, the thread did exit */
1766 mutex_unlock(&hotkey_thread_mutex);
1770 /* call with hotkey_mutex held */
1771 static void hotkey_poll_setup(int may_warn)
1773 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1774 hotkey_poll_freq > 0 &&
1775 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1776 if (!tpacpi_hotkey_task) {
1777 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1778 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1779 if (IS_ERR(tpacpi_hotkey_task)) {
1780 tpacpi_hotkey_task = NULL;
1782 "could not create kernel thread "
1783 "for hotkey polling\n");
1787 hotkey_poll_stop_sync();
1789 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1790 printk(TPACPI_NOTICE
1791 "hot keys 0x%08x require polling, "
1792 "which is currently disabled\n",
1793 hotkey_source_mask);
1798 static void hotkey_poll_setup_safe(int may_warn)
1800 mutex_lock(&hotkey_mutex);
1801 hotkey_poll_setup(may_warn);
1802 mutex_unlock(&hotkey_mutex);
1805 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1807 static void hotkey_poll_setup_safe(int __unused)
1811 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1813 static int hotkey_inputdev_open(struct input_dev *dev)
1815 switch (tpacpi_lifecycle) {
1816 case TPACPI_LIFE_INIT:
1818 * hotkey_init will call hotkey_poll_setup_safe
1819 * at the appropriate moment
1822 case TPACPI_LIFE_EXITING:
1824 case TPACPI_LIFE_RUNNING:
1825 hotkey_poll_setup_safe(0);
1829 /* Should only happen if tpacpi_lifecycle is corrupt */
1834 static void hotkey_inputdev_close(struct input_dev *dev)
1836 /* disable hotkey polling when possible */
1837 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1838 hotkey_poll_setup_safe(0);
1841 /* sysfs hotkey enable ------------------------------------------------- */
1842 static ssize_t hotkey_enable_show(struct device *dev,
1843 struct device_attribute *attr,
1848 res = hotkey_status_get(&status);
1852 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1855 static ssize_t hotkey_enable_store(struct device *dev,
1856 struct device_attribute *attr,
1857 const char *buf, size_t count)
1862 if (parse_strtoul(buf, 1, &t))
1865 res = hotkey_status_set(t);
1867 return (res) ? res : count;
1870 static struct device_attribute dev_attr_hotkey_enable =
1871 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1872 hotkey_enable_show, hotkey_enable_store);
1874 /* sysfs hotkey mask --------------------------------------------------- */
1875 static ssize_t hotkey_mask_show(struct device *dev,
1876 struct device_attribute *attr,
1881 if (mutex_lock_interruptible(&hotkey_mutex))
1882 return -ERESTARTSYS;
1883 res = hotkey_mask_get();
1884 mutex_unlock(&hotkey_mutex);
1887 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1890 static ssize_t hotkey_mask_store(struct device *dev,
1891 struct device_attribute *attr,
1892 const char *buf, size_t count)
1897 if (parse_strtoul(buf, 0xffffffffUL, &t))
1900 if (mutex_lock_interruptible(&hotkey_mutex))
1901 return -ERESTARTSYS;
1903 res = hotkey_mask_set(t);
1905 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1906 hotkey_poll_setup(1);
1909 mutex_unlock(&hotkey_mutex);
1911 return (res) ? res : count;
1914 static struct device_attribute dev_attr_hotkey_mask =
1915 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1916 hotkey_mask_show, hotkey_mask_store);
1918 /* sysfs hotkey bios_enabled ------------------------------------------- */
1919 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1920 struct device_attribute *attr,
1923 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1926 static struct device_attribute dev_attr_hotkey_bios_enabled =
1927 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1929 /* sysfs hotkey bios_mask ---------------------------------------------- */
1930 static ssize_t hotkey_bios_mask_show(struct device *dev,
1931 struct device_attribute *attr,
1934 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1937 static struct device_attribute dev_attr_hotkey_bios_mask =
1938 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1940 /* sysfs hotkey all_mask ----------------------------------------------- */
1941 static ssize_t hotkey_all_mask_show(struct device *dev,
1942 struct device_attribute *attr,
1945 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1946 hotkey_all_mask | hotkey_source_mask);
1949 static struct device_attribute dev_attr_hotkey_all_mask =
1950 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1952 /* sysfs hotkey recommended_mask --------------------------------------- */
1953 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1954 struct device_attribute *attr,
1957 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1958 (hotkey_all_mask | hotkey_source_mask)
1959 & ~hotkey_reserved_mask);
1962 static struct device_attribute dev_attr_hotkey_recommended_mask =
1963 __ATTR(hotkey_recommended_mask, S_IRUGO,
1964 hotkey_recommended_mask_show, NULL);
1966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1968 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1969 static ssize_t hotkey_source_mask_show(struct device *dev,
1970 struct device_attribute *attr,
1973 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1976 static ssize_t hotkey_source_mask_store(struct device *dev,
1977 struct device_attribute *attr,
1978 const char *buf, size_t count)
1982 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1983 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1986 if (mutex_lock_interruptible(&hotkey_mutex))
1987 return -ERESTARTSYS;
1989 HOTKEY_CONFIG_CRITICAL_START
1990 hotkey_source_mask = t;
1991 HOTKEY_CONFIG_CRITICAL_END
1993 hotkey_poll_setup(1);
1995 mutex_unlock(&hotkey_mutex);
2000 static struct device_attribute dev_attr_hotkey_source_mask =
2001 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2002 hotkey_source_mask_show, hotkey_source_mask_store);
2004 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2005 static ssize_t hotkey_poll_freq_show(struct device *dev,
2006 struct device_attribute *attr,
2009 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2012 static ssize_t hotkey_poll_freq_store(struct device *dev,
2013 struct device_attribute *attr,
2014 const char *buf, size_t count)
2018 if (parse_strtoul(buf, 25, &t))
2021 if (mutex_lock_interruptible(&hotkey_mutex))
2022 return -ERESTARTSYS;
2024 hotkey_poll_freq = t;
2026 hotkey_poll_setup(1);
2027 mutex_unlock(&hotkey_mutex);
2032 static struct device_attribute dev_attr_hotkey_poll_freq =
2033 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2034 hotkey_poll_freq_show, hotkey_poll_freq_store);
2036 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2038 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2039 static ssize_t hotkey_radio_sw_show(struct device *dev,
2040 struct device_attribute *attr,
2044 res = hotkey_get_wlsw(&s);
2048 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2051 static struct device_attribute dev_attr_hotkey_radio_sw =
2052 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2054 static void hotkey_radio_sw_notify_change(void)
2056 if (tp_features.hotkey_wlsw)
2057 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2061 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2062 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2063 struct device_attribute *attr,
2067 res = hotkey_get_tablet_mode(&s);
2071 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2074 static struct device_attribute dev_attr_hotkey_tablet_mode =
2075 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2077 static void hotkey_tablet_mode_notify_change(void)
2079 if (tp_features.hotkey_tablet)
2080 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2081 "hotkey_tablet_mode");
2084 /* sysfs hotkey report_mode -------------------------------------------- */
2085 static ssize_t hotkey_report_mode_show(struct device *dev,
2086 struct device_attribute *attr,
2089 return snprintf(buf, PAGE_SIZE, "%d\n",
2090 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2093 static struct device_attribute dev_attr_hotkey_report_mode =
2094 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2096 /* sysfs wakeup reason (pollable) -------------------------------------- */
2097 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2098 struct device_attribute *attr,
2101 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2104 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2105 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2107 static void hotkey_wakeup_reason_notify_change(void)
2109 if (tp_features.hotkey_mask)
2110 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2114 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2115 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2116 struct device_attribute *attr,
2119 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2122 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2123 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2124 hotkey_wakeup_hotunplug_complete_show, NULL);
2126 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2128 if (tp_features.hotkey_mask)
2129 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2130 "wakeup_hotunplug_complete");
2133 /* --------------------------------------------------------------------- */
2135 static struct attribute *hotkey_attributes[] __initdata = {
2136 &dev_attr_hotkey_enable.attr,
2137 &dev_attr_hotkey_bios_enabled.attr,
2138 &dev_attr_hotkey_report_mode.attr,
2139 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2140 &dev_attr_hotkey_mask.attr,
2141 &dev_attr_hotkey_all_mask.attr,
2142 &dev_attr_hotkey_recommended_mask.attr,
2143 &dev_attr_hotkey_source_mask.attr,
2144 &dev_attr_hotkey_poll_freq.attr,
2148 static struct attribute *hotkey_mask_attributes[] __initdata = {
2149 &dev_attr_hotkey_bios_mask.attr,
2150 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2151 &dev_attr_hotkey_mask.attr,
2152 &dev_attr_hotkey_all_mask.attr,
2153 &dev_attr_hotkey_recommended_mask.attr,
2155 &dev_attr_hotkey_wakeup_reason.attr,
2156 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2159 static void bluetooth_update_rfk(void);
2160 static void wan_update_rfk(void);
2161 static void uwb_update_rfk(void);
2162 static void tpacpi_send_radiosw_update(void)
2166 /* Sync these BEFORE sending any rfkill events */
2167 if (tp_features.bluetooth)
2168 bluetooth_update_rfk();
2169 if (tp_features.wan)
2171 if (tp_features.uwb)
2174 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2175 mutex_lock(&tpacpi_inputdev_send_mutex);
2177 input_report_switch(tpacpi_inputdev,
2178 SW_RFKILL_ALL, !!wlsw);
2179 input_sync(tpacpi_inputdev);
2181 mutex_unlock(&tpacpi_inputdev_send_mutex);
2183 hotkey_radio_sw_notify_change();
2186 static void hotkey_exit(void)
2188 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2189 hotkey_poll_stop_sync();
2192 if (hotkey_dev_attributes)
2193 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2195 kfree(hotkey_keycode_map);
2197 if (tp_features.hotkey) {
2198 dbg_printk(TPACPI_DBG_EXIT,
2199 "restoring original hot key mask\n");
2200 /* no short-circuit boolean operator below! */
2201 if ((hotkey_mask_set(hotkey_orig_mask) |
2202 hotkey_status_set(hotkey_orig_status)) != 0)
2204 "failed to restore hot key mask "
2205 "to BIOS defaults\n");
2209 static int __init hotkey_init(struct ibm_init_struct *iibm)
2211 /* Requirements for changing the default keymaps:
2213 * 1. Many of the keys are mapped to KEY_RESERVED for very
2214 * good reasons. Do not change them unless you have deep
2215 * knowledge on the IBM and Lenovo ThinkPad firmware for
2216 * the various ThinkPad models. The driver behaves
2217 * differently for KEY_RESERVED: such keys have their
2218 * hot key mask *unset* in mask_recommended, and also
2219 * in the initial hot key mask programmed into the
2220 * firmware at driver load time, which means the firm-
2221 * ware may react very differently if you change them to
2224 * 2. You must be subscribed to the linux-thinkpad and
2225 * ibm-acpi-devel mailing lists, and you should read the
2226 * list archives since 2007 if you want to change the
2227 * keymaps. This requirement exists so that you will
2228 * know the past history of problems with the thinkpad-
2229 * acpi driver keymaps, and also that you will be
2230 * listening to any bug reports;
2232 * 3. Do not send thinkpad-acpi specific patches directly to
2233 * for merging, *ever*. Send them to the linux-acpi
2234 * mailinglist for comments. Merging is to be done only
2235 * through acpi-test and the ACPI maintainer.
2237 * If the above is too much to ask, don't change the keymap.
2238 * Ask the thinkpad-acpi maintainer to do it, instead.
2240 static u16 ibm_keycode_map[] __initdata = {
2241 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2242 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2243 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2244 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2246 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2247 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2248 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2249 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2251 /* brightness: firmware always reacts to them, unless
2252 * X.org did some tricks in the radeon BIOS scratch
2253 * registers of *some* models */
2254 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2255 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2257 /* Thinklight: firmware always react to it */
2258 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2260 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2261 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2263 /* Volume: firmware always react to it and reprograms
2264 * the built-in *extra* mixer. Never map it to control
2265 * another mixer by default. */
2266 KEY_RESERVED, /* 0x14: VOLUME UP */
2267 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2268 KEY_RESERVED, /* 0x16: MUTE */
2270 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2272 /* (assignments unknown, please report if found) */
2273 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2274 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2276 static u16 lenovo_keycode_map[] __initdata = {
2277 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2278 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2279 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2280 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2282 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2283 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2284 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2285 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2287 /* These either have to go through ACPI video, or
2288 * act like in the IBM ThinkPads, so don't ever
2289 * enable them by default */
2290 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2291 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2293 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2295 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2296 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2298 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2299 * react to it and reprograms the built-in *extra* mixer.
2300 * Never map it to control another mixer by default.
2302 * T60?, T61, R60?, R61: firmware and EC tries to send
2303 * these over the regular keyboard, so these are no-ops,
2304 * but there are still weird bugs re. MUTE, so do not
2305 * change unless you get test reports from all Lenovo
2306 * models. May cause the BIOS to interfere with the
2309 KEY_RESERVED, /* 0x14: VOLUME UP */
2310 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2311 KEY_RESERVED, /* 0x16: MUTE */
2313 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2315 /* (assignments unknown, please report if found) */
2316 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2320 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2321 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2322 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2328 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2330 BUG_ON(!tpacpi_inputdev);
2331 BUG_ON(tpacpi_inputdev->open != NULL ||
2332 tpacpi_inputdev->close != NULL);
2334 TPACPI_ACPIHANDLE_INIT(hkey);
2335 mutex_init(&hotkey_mutex);
2337 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2338 mutex_init(&hotkey_thread_mutex);
2339 mutex_init(&hotkey_thread_data_mutex);
2342 /* hotkey not supported on 570 */
2343 tp_features.hotkey = hkey_handle != NULL;
2345 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2346 str_supported(tp_features.hotkey));
2348 if (!tp_features.hotkey)
2351 tpacpi_disable_brightness_delay();
2353 hotkey_dev_attributes = create_attr_set(13, NULL);
2354 if (!hotkey_dev_attributes)
2356 res = add_many_to_attr_set(hotkey_dev_attributes,
2358 ARRAY_SIZE(hotkey_attributes));
2362 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2363 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2364 for HKEY interface version 0x100 */
2365 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2366 if ((hkeyv >> 8) != 1) {
2367 printk(TPACPI_ERR "unknown version of the "
2368 "HKEY interface: 0x%x\n", hkeyv);
2369 printk(TPACPI_ERR "please report this to %s\n",
2373 * MHKV 0x100 in A31, R40, R40e,
2374 * T4x, X31, and later
2376 tp_features.hotkey_mask = 1;
2380 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2381 str_supported(tp_features.hotkey_mask));
2383 if (tp_features.hotkey_mask) {
2384 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2387 "missing MHKA handler, "
2388 "please report this to %s\n",
2390 /* FN+F12, FN+F4, FN+F3 */
2391 hotkey_all_mask = 0x080cU;
2395 /* hotkey_source_mask *must* be zero for
2396 * the first hotkey_mask_get */
2397 res = hotkey_status_get(&hotkey_orig_status);
2401 if (tp_features.hotkey_mask) {
2402 res = hotkey_mask_get();
2406 hotkey_orig_mask = hotkey_mask;
2407 res = add_many_to_attr_set(
2408 hotkey_dev_attributes,
2409 hotkey_mask_attributes,
2410 ARRAY_SIZE(hotkey_mask_attributes));
2415 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2416 if (tp_features.hotkey_mask) {
2417 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2420 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2423 vdbg_printk(TPACPI_DBG_INIT,
2424 "hotkey source mask 0x%08x, polling freq %d\n",
2425 hotkey_source_mask, hotkey_poll_freq);
2428 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2430 tp_features.hotkey_wlsw = 1;
2432 "radio switch emulation enabled\n");
2435 /* Not all thinkpads have a hardware radio switch */
2436 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2437 tp_features.hotkey_wlsw = 1;
2439 "radio switch found; radios are %s\n",
2440 enabled(status, 0));
2442 if (tp_features.hotkey_wlsw)
2443 res = add_to_attr_set(hotkey_dev_attributes,
2444 &dev_attr_hotkey_radio_sw.attr);
2446 /* For X41t, X60t, X61t Tablets... */
2447 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2448 tp_features.hotkey_tablet = 1;
2450 "possible tablet mode switch found; "
2451 "ThinkPad in %s mode\n",
2452 (status & TP_HOTKEY_TABLET_MASK)?
2453 "tablet" : "laptop");
2454 res = add_to_attr_set(hotkey_dev_attributes,
2455 &dev_attr_hotkey_tablet_mode.attr);
2459 res = register_attr_set_with_sysfs(
2460 hotkey_dev_attributes,
2461 &tpacpi_pdev->dev.kobj);
2465 /* Set up key map */
2467 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2469 if (!hotkey_keycode_map) {
2471 "failed to allocate memory for key map\n");
2476 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2477 dbg_printk(TPACPI_DBG_INIT,
2478 "using Lenovo default hot key map\n");
2479 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2480 TPACPI_HOTKEY_MAP_SIZE);
2482 dbg_printk(TPACPI_DBG_INIT,
2483 "using IBM default hot key map\n");
2484 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2485 TPACPI_HOTKEY_MAP_SIZE);
2488 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2489 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2490 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2491 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2492 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2493 tpacpi_inputdev->keycode = hotkey_keycode_map;
2494 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2495 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2496 set_bit(hotkey_keycode_map[i],
2497 tpacpi_inputdev->keybit);
2499 if (i < sizeof(hotkey_reserved_mask)*8)
2500 hotkey_reserved_mask |= 1 << i;
2504 if (tp_features.hotkey_wlsw) {
2505 set_bit(EV_SW, tpacpi_inputdev->evbit);
2506 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2508 if (tp_features.hotkey_tablet) {
2509 set_bit(EV_SW, tpacpi_inputdev->evbit);
2510 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2513 /* Do not issue duplicate brightness change events to
2515 if (!tp_features.bright_acpimode)
2516 /* update bright_acpimode... */
2517 tpacpi_check_std_acpi_brightness_support();
2519 if (tp_features.bright_acpimode) {
2521 "This ThinkPad has standard ACPI backlight "
2522 "brightness control, supported by the ACPI "
2524 printk(TPACPI_NOTICE
2525 "Disabling thinkpad-acpi brightness events "
2528 /* The hotkey_reserved_mask change below is not
2529 * necessary while the keys are at KEY_RESERVED in the
2530 * default map, but better safe than sorry, leave it
2531 * here as a marker of what we have to do, especially
2532 * when we finally become able to set this at runtime
2533 * on response to X.org requests */
2534 hotkey_reserved_mask |=
2535 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2536 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2539 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2540 res = hotkey_status_set(1);
2545 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2546 & ~hotkey_reserved_mask)
2547 | hotkey_orig_mask);
2548 if (res < 0 && res != -ENXIO) {
2553 dbg_printk(TPACPI_DBG_INIT,
2554 "legacy hot key reporting over procfs %s\n",
2555 (hotkey_report_mode < 2) ?
2556 "enabled" : "disabled");
2558 tpacpi_inputdev->open = &hotkey_inputdev_open;
2559 tpacpi_inputdev->close = &hotkey_inputdev_close;
2561 hotkey_poll_setup_safe(1);
2562 tpacpi_send_radiosw_update();
2563 tpacpi_input_send_tabletsw();
2568 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2569 hotkey_dev_attributes = NULL;
2571 return (res < 0)? res : 1;
2574 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2577 unsigned int scancode;
2582 if (event != 0x80) {
2584 "unknown HKEY notification event %d\n", event);
2585 /* forward it to userspace, maybe it knows how to handle it */
2586 acpi_bus_generate_netlink_event(
2587 ibm->acpi->device->pnp.device_class,
2588 dev_name(&ibm->acpi->device->dev),
2594 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2595 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2608 switch (hkey >> 12) {
2610 /* 0x1000-0x1FFF: key presses */
2611 scancode = hkey & 0xfff;
2612 if (scancode > 0 && scancode < 0x21) {
2614 if (!(hotkey_source_mask & (1 << scancode))) {
2615 tpacpi_input_send_key(scancode);
2627 case 0x2304: /* suspend, undock */
2628 case 0x2404: /* hibernation, undock */
2629 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2632 case 0x2305: /* suspend, bay eject */
2633 case 0x2405: /* hibernation, bay eject */
2634 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2640 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2642 "woke up due to a hot-unplug "
2644 hotkey_wakeup_reason_notify_change();
2648 /* bay-related wakeups */
2649 if (hkey == 0x3003) {
2650 hotkey_autosleep_ack = 1;
2653 hotkey_wakeup_hotunplug_complete_notify_change();
2659 /* dock-related wakeups */
2660 if (hkey == 0x4003) {
2661 hotkey_autosleep_ack = 1;
2664 hotkey_wakeup_hotunplug_complete_notify_change();
2670 /* 0x5000-0x5FFF: human interface helpers */
2672 case 0x5010: /* Lenovo new BIOS: brightness changed */
2673 case 0x500b: /* X61t: tablet pen inserted into bay */
2674 case 0x500c: /* X61t: tablet pen removed from bay */
2676 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2677 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2678 tpacpi_input_send_tabletsw();
2679 hotkey_tablet_mode_notify_change();
2684 /* LID switch events. Do not propagate */
2692 /* 0x7000-0x7FFF: misc */
2693 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2694 tpacpi_send_radiosw_update();
2698 /* fallthrough to default */
2703 printk(TPACPI_NOTICE
2704 "unhandled HKEY event 0x%04x\n", hkey);
2708 if (!ignore_acpi_ev &&
2709 (send_acpi_ev || hotkey_report_mode < 2)) {
2710 acpi_bus_generate_proc_event(ibm->acpi->device,
2714 /* netlink events */
2715 if (!ignore_acpi_ev && send_acpi_ev) {
2716 acpi_bus_generate_netlink_event(
2717 ibm->acpi->device->pnp.device_class,
2718 dev_name(&ibm->acpi->device->dev),
2724 static void hotkey_suspend(pm_message_t state)
2726 /* Do these on suspend, we get the events on early resume! */
2727 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2728 hotkey_autosleep_ack = 0;
2731 static void hotkey_resume(void)
2733 tpacpi_disable_brightness_delay();
2735 if (hotkey_mask_get())
2737 "error while trying to read hot key mask "
2739 tpacpi_send_radiosw_update();
2740 hotkey_tablet_mode_notify_change();
2741 hotkey_wakeup_reason_notify_change();
2742 hotkey_wakeup_hotunplug_complete_notify_change();
2743 hotkey_poll_setup_safe(0);
2746 /* procfs -------------------------------------------------------------- */
2747 static int hotkey_read(char *p)
2752 if (!tp_features.hotkey) {
2753 len += sprintf(p + len, "status:\t\tnot supported\n");
2757 if (mutex_lock_interruptible(&hotkey_mutex))
2758 return -ERESTARTSYS;
2759 res = hotkey_status_get(&status);
2761 res = hotkey_mask_get();
2762 mutex_unlock(&hotkey_mutex);
2766 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2767 if (tp_features.hotkey_mask) {
2768 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2769 len += sprintf(p + len,
2770 "commands:\tenable, disable, reset, <mask>\n");
2772 len += sprintf(p + len, "mask:\t\tnot supported\n");
2773 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2779 static int hotkey_write(char *buf)
2785 if (!tp_features.hotkey)
2788 if (mutex_lock_interruptible(&hotkey_mutex))
2789 return -ERESTARTSYS;
2795 while ((cmd = next_cmd(&buf))) {
2796 if (strlencmp(cmd, "enable") == 0) {
2798 } else if (strlencmp(cmd, "disable") == 0) {
2800 } else if (strlencmp(cmd, "reset") == 0) {
2801 status = hotkey_orig_status;
2802 mask = hotkey_orig_mask;
2803 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2805 } else if (sscanf(cmd, "%x", &mask) == 1) {
2813 res = hotkey_status_set(status);
2815 if (!res && mask != hotkey_mask)
2816 res = hotkey_mask_set(mask);
2819 mutex_unlock(&hotkey_mutex);
2823 static const struct acpi_device_id ibm_htk_device_ids[] = {
2824 {TPACPI_ACPI_HKEY_HID, 0},
2828 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2829 .hid = ibm_htk_device_ids,
2830 .notify = hotkey_notify,
2831 .handle = &hkey_handle,
2832 .type = ACPI_DEVICE_NOTIFY,
2835 static struct ibm_struct hotkey_driver_data = {
2837 .read = hotkey_read,
2838 .write = hotkey_write,
2839 .exit = hotkey_exit,
2840 .resume = hotkey_resume,
2841 .suspend = hotkey_suspend,
2842 .acpi = &ibm_hotkey_acpidriver,
2845 /*************************************************************************
2846 * Bluetooth subdriver
2850 /* ACPI GBDC/SBDC bits */
2851 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2852 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2853 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2858 /* ACPI \BLTH commands */
2859 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2860 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2861 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2862 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2863 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2866 static struct rfkill *tpacpi_bluetooth_rfkill;
2868 static void bluetooth_suspend(pm_message_t state)
2870 /* Try to make sure radio will resume powered off */
2871 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2872 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2875 static int bluetooth_get_radiosw(void)
2879 if (!tp_features.bluetooth)
2882 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2883 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2884 return RFKILL_STATE_HARD_BLOCKED;
2886 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2887 if (dbg_bluetoothemul)
2888 return (tpacpi_bluetooth_emulstate) ?
2889 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2892 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2895 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2896 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2899 static void bluetooth_update_rfk(void)
2903 if (!tpacpi_bluetooth_rfkill)
2906 status = bluetooth_get_radiosw();
2909 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2912 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2916 if (!tp_features.bluetooth)
2919 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2920 * reason to risk weird behaviour. */
2921 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2925 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2926 if (dbg_bluetoothemul) {
2927 tpacpi_bluetooth_emulstate = !!radio_on;
2929 bluetooth_update_rfk();
2934 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
2936 status = TP_ACPI_BLUETOOTH_RADIOSSW;
2939 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2943 bluetooth_update_rfk();
2948 /* sysfs bluetooth enable ---------------------------------------------- */
2949 static ssize_t bluetooth_enable_show(struct device *dev,
2950 struct device_attribute *attr,
2955 status = bluetooth_get_radiosw();
2959 return snprintf(buf, PAGE_SIZE, "%d\n",
2960 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2963 static ssize_t bluetooth_enable_store(struct device *dev,
2964 struct device_attribute *attr,
2965 const char *buf, size_t count)
2970 if (parse_strtoul(buf, 1, &t))
2973 res = bluetooth_set_radiosw(t, 1);
2975 return (res) ? res : count;
2978 static struct device_attribute dev_attr_bluetooth_enable =
2979 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2980 bluetooth_enable_show, bluetooth_enable_store);
2982 /* --------------------------------------------------------------------- */
2984 static struct attribute *bluetooth_attributes[] = {
2985 &dev_attr_bluetooth_enable.attr,
2989 static const struct attribute_group bluetooth_attr_group = {
2990 .attrs = bluetooth_attributes,
2993 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2995 int bts = bluetooth_get_radiosw();
3004 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3006 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3009 static void bluetooth_shutdown(void)
3011 /* Order firmware to save current state to NVRAM */
3012 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3013 TP_ACPI_BLTH_SAVE_STATE))
3014 printk(TPACPI_NOTICE
3015 "failed to save bluetooth state to NVRAM\n");
3018 static void bluetooth_exit(void)
3020 bluetooth_shutdown();
3022 if (tpacpi_bluetooth_rfkill)
3023 rfkill_unregister(tpacpi_bluetooth_rfkill);
3025 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3026 &bluetooth_attr_group);
3029 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3034 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3036 TPACPI_ACPIHANDLE_INIT(hkey);
3038 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3039 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3040 tp_features.bluetooth = hkey_handle &&
3041 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3043 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3044 str_supported(tp_features.bluetooth),
3047 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3048 if (dbg_bluetoothemul) {
3049 tp_features.bluetooth = 1;
3051 "bluetooth switch emulation enabled\n");
3054 if (tp_features.bluetooth &&
3055 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3056 /* no bluetooth hardware present in system */
3057 tp_features.bluetooth = 0;
3058 dbg_printk(TPACPI_DBG_INIT,
3059 "bluetooth hardware not installed\n");
3062 if (!tp_features.bluetooth)
3065 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3066 &bluetooth_attr_group);
3070 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3071 &tpacpi_bluetooth_rfkill,
3072 RFKILL_TYPE_BLUETOOTH,
3073 "tpacpi_bluetooth_sw",
3075 tpacpi_bluetooth_rfk_set,
3076 tpacpi_bluetooth_rfk_get);
3085 /* procfs -------------------------------------------------------------- */
3086 static int bluetooth_read(char *p)
3089 int status = bluetooth_get_radiosw();
3091 if (!tp_features.bluetooth)
3092 len += sprintf(p + len, "status:\t\tnot supported\n");
3094 len += sprintf(p + len, "status:\t\t%s\n",
3095 (status == RFKILL_STATE_UNBLOCKED) ?
3096 "enabled" : "disabled");
3097 len += sprintf(p + len, "commands:\tenable, disable\n");
3103 static int bluetooth_write(char *buf)
3107 if (!tp_features.bluetooth)
3110 while ((cmd = next_cmd(&buf))) {
3111 if (strlencmp(cmd, "enable") == 0) {
3112 bluetooth_set_radiosw(1, 1);
3113 } else if (strlencmp(cmd, "disable") == 0) {
3114 bluetooth_set_radiosw(0, 1);
3122 static struct ibm_struct bluetooth_driver_data = {
3123 .name = "bluetooth",
3124 .read = bluetooth_read,
3125 .write = bluetooth_write,
3126 .exit = bluetooth_exit,
3127 .suspend = bluetooth_suspend,
3128 .shutdown = bluetooth_shutdown,
3131 /*************************************************************************
3136 /* ACPI GWAN/SWAN bits */
3137 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3138 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3139 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3143 static struct rfkill *tpacpi_wan_rfkill;
3145 static void wan_suspend(pm_message_t state)
3147 /* Try to make sure radio will resume powered off */
3148 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3149 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3152 static int wan_get_radiosw(void)
3156 if (!tp_features.wan)
3159 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3160 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3161 return RFKILL_STATE_HARD_BLOCKED;
3163 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3165 return (tpacpi_wwan_emulstate) ?
3166 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3169 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3172 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3173 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3176 static void wan_update_rfk(void)
3180 if (!tpacpi_wan_rfkill)
3183 status = wan_get_radiosw();
3186 rfkill_force_state(tpacpi_wan_rfkill, status);
3189 static int wan_set_radiosw(int radio_on, int update_rfk)
3193 if (!tp_features.wan)
3196 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3197 * reason to risk weird behaviour. */
3198 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3202 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3204 tpacpi_wwan_emulstate = !!radio_on;
3211 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3213 status = TP_ACPI_WANCARD_RADIOSSW;
3216 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3225 /* sysfs wan enable ---------------------------------------------------- */
3226 static ssize_t wan_enable_show(struct device *dev,
3227 struct device_attribute *attr,
3232 status = wan_get_radiosw();
3236 return snprintf(buf, PAGE_SIZE, "%d\n",
3237 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3240 static ssize_t wan_enable_store(struct device *dev,
3241 struct device_attribute *attr,
3242 const char *buf, size_t count)
3247 if (parse_strtoul(buf, 1, &t))
3250 res = wan_set_radiosw(t, 1);
3252 return (res) ? res : count;
3255 static struct device_attribute dev_attr_wan_enable =
3256 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3257 wan_enable_show, wan_enable_store);
3259 /* --------------------------------------------------------------------- */
3261 static struct attribute *wan_attributes[] = {
3262 &dev_attr_wan_enable.attr,
3266 static const struct attribute_group wan_attr_group = {
3267 .attrs = wan_attributes,
3270 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3272 int wans = wan_get_radiosw();
3281 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3283 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3286 static void wan_shutdown(void)
3288 /* Order firmware to save current state to NVRAM */
3289 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3290 TP_ACPI_WGSV_SAVE_STATE))
3291 printk(TPACPI_NOTICE
3292 "failed to save WWAN state to NVRAM\n");
3295 static void wan_exit(void)
3299 if (tpacpi_wan_rfkill)
3300 rfkill_unregister(tpacpi_wan_rfkill);
3302 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3306 static int __init wan_init(struct ibm_init_struct *iibm)
3311 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3313 TPACPI_ACPIHANDLE_INIT(hkey);
3315 tp_features.wan = hkey_handle &&
3316 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3318 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3319 str_supported(tp_features.wan),
3322 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3324 tp_features.wan = 1;
3326 "wwan switch emulation enabled\n");
3329 if (tp_features.wan &&
3330 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3331 /* no wan hardware present in system */
3332 tp_features.wan = 0;
3333 dbg_printk(TPACPI_DBG_INIT,
3334 "wan hardware not installed\n");
3337 if (!tp_features.wan)
3340 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3345 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3351 tpacpi_wan_rfk_get);
3360 /* procfs -------------------------------------------------------------- */
3361 static int wan_read(char *p)
3364 int status = wan_get_radiosw();
3366 if (!tp_features.wan)
3367 len += sprintf(p + len, "status:\t\tnot supported\n");
3369 len += sprintf(p + len, "status:\t\t%s\n",
3370 (status == RFKILL_STATE_UNBLOCKED) ?
3371 "enabled" : "disabled");
3372 len += sprintf(p + len, "commands:\tenable, disable\n");
3378 static int wan_write(char *buf)
3382 if (!tp_features.wan)
3385 while ((cmd = next_cmd(&buf))) {
3386 if (strlencmp(cmd, "enable") == 0) {
3387 wan_set_radiosw(1, 1);
3388 } else if (strlencmp(cmd, "disable") == 0) {
3389 wan_set_radiosw(0, 1);
3397 static struct ibm_struct wan_driver_data = {
3402 .suspend = wan_suspend,
3403 .shutdown = wan_shutdown,
3406 /*************************************************************************
3411 /* ACPI GUWB/SUWB bits */
3412 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3413 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3416 static struct rfkill *tpacpi_uwb_rfkill;
3418 static int uwb_get_radiosw(void)
3422 if (!tp_features.uwb)
3425 /* WLSW overrides UWB in firmware/hardware, reflect that */
3426 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3427 return RFKILL_STATE_HARD_BLOCKED;
3429 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3431 return (tpacpi_uwb_emulstate) ?
3432 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3435 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3438 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3439 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3442 static void uwb_update_rfk(void)
3446 if (!tpacpi_uwb_rfkill)
3449 status = uwb_get_radiosw();
3452 rfkill_force_state(tpacpi_uwb_rfkill, status);
3455 static int uwb_set_radiosw(int radio_on, int update_rfk)
3459 if (!tp_features.uwb)
3462 /* WLSW overrides UWB in firmware/hardware, but there is no
3463 * reason to risk weird behaviour. */
3464 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3468 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3470 tpacpi_uwb_emulstate = !!radio_on;
3477 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3478 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3487 /* --------------------------------------------------------------------- */
3489 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3491 int uwbs = uwb_get_radiosw();
3500 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3502 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3505 static void uwb_exit(void)
3507 if (tpacpi_uwb_rfkill)
3508 rfkill_unregister(tpacpi_uwb_rfkill);
3511 static int __init uwb_init(struct ibm_init_struct *iibm)
3516 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3518 TPACPI_ACPIHANDLE_INIT(hkey);
3520 tp_features.uwb = hkey_handle &&
3521 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3523 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3524 str_supported(tp_features.uwb),
3527 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3529 tp_features.uwb = 1;
3531 "uwb switch emulation enabled\n");
3534 if (tp_features.uwb &&
3535 !(status & TP_ACPI_UWB_HWPRESENT)) {
3536 /* no uwb hardware present in system */
3537 tp_features.uwb = 0;
3538 dbg_printk(TPACPI_DBG_INIT,
3539 "uwb hardware not installed\n");
3542 if (!tp_features.uwb)
3545 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3551 tpacpi_uwb_rfk_get);
3556 static struct ibm_struct uwb_driver_data = {
3559 .flags.experimental = 1,
3562 /*************************************************************************
3566 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3568 enum video_access_mode {
3569 TPACPI_VIDEO_NONE = 0,
3570 TPACPI_VIDEO_570, /* 570 */
3571 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3572 TPACPI_VIDEO_NEW, /* all others */
3575 enum { /* video status flags, based on VIDEO_570 */
3576 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3577 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3578 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3581 enum { /* TPACPI_VIDEO_570 constants */
3582 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3583 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3584 * video_status_flags */
3585 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3586 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3589 static enum video_access_mode video_supported;
3590 static int video_orig_autosw;
3592 static int video_autosw_get(void);
3593 static int video_autosw_set(int enable);
3595 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3597 static int __init video_init(struct ibm_init_struct *iibm)
3601 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3603 TPACPI_ACPIHANDLE_INIT(vid);
3604 TPACPI_ACPIHANDLE_INIT(vid2);
3606 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3607 /* G41, assume IVGA doesn't change */
3608 vid_handle = vid2_handle;
3611 /* video switching not supported on R30, R31 */
3612 video_supported = TPACPI_VIDEO_NONE;
3613 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3615 video_supported = TPACPI_VIDEO_570;
3616 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3617 /* 600e/x, 770e, 770x */
3618 video_supported = TPACPI_VIDEO_770;
3621 video_supported = TPACPI_VIDEO_NEW;
3623 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3624 str_supported(video_supported != TPACPI_VIDEO_NONE),
3627 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3630 static void video_exit(void)
3632 dbg_printk(TPACPI_DBG_EXIT,
3633 "restoring original video autoswitch mode\n");
3634 if (video_autosw_set(video_orig_autosw))
3635 printk(TPACPI_ERR "error while trying to restore original "
3636 "video autoswitch mode\n");
3639 static int video_outputsw_get(void)
3644 switch (video_supported) {
3645 case TPACPI_VIDEO_570:
3646 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3647 TP_ACPI_VIDEO_570_PHSCMD))
3649 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3651 case TPACPI_VIDEO_770:
3652 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3655 status |= TP_ACPI_VIDEO_S_LCD;
3656 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3659 status |= TP_ACPI_VIDEO_S_CRT;
3661 case TPACPI_VIDEO_NEW:
3662 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3663 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3666 status |= TP_ACPI_VIDEO_S_CRT;
3668 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3669 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3672 status |= TP_ACPI_VIDEO_S_LCD;
3673 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3676 status |= TP_ACPI_VIDEO_S_DVI;
3685 static int video_outputsw_set(int status)
3690 switch (video_supported) {
3691 case TPACPI_VIDEO_570:
3692 res = acpi_evalf(NULL, NULL,
3693 "\\_SB.PHS2", "vdd",
3694 TP_ACPI_VIDEO_570_PHS2CMD,
3695 status | TP_ACPI_VIDEO_570_PHS2SET);
3697 case TPACPI_VIDEO_770:
3698 autosw = video_autosw_get();
3702 res = video_autosw_set(1);
3705 res = acpi_evalf(vid_handle, NULL,
3706 "ASWT", "vdd", status * 0x100, 0);
3707 if (!autosw && video_autosw_set(autosw)) {
3709 "video auto-switch left enabled due to error\n");
3713 case TPACPI_VIDEO_NEW:
3714 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3715 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3721 return (res)? 0 : -EIO;
3724 static int video_autosw_get(void)
3728 switch (video_supported) {
3729 case TPACPI_VIDEO_570:
3730 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3733 case TPACPI_VIDEO_770:
3734 case TPACPI_VIDEO_NEW:
3735 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3745 static int video_autosw_set(int enable)
3747 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3752 static int video_outputsw_cycle(void)
3754 int autosw = video_autosw_get();
3760 switch (video_supported) {
3761 case TPACPI_VIDEO_570:
3762 res = video_autosw_set(1);
3765 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3767 case TPACPI_VIDEO_770:
3768 case TPACPI_VIDEO_NEW:
3769 res = video_autosw_set(1);
3772 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3777 if (!autosw && video_autosw_set(autosw)) {
3779 "video auto-switch left enabled due to error\n");
3783 return (res)? 0 : -EIO;
3786 static int video_expand_toggle(void)
3788 switch (video_supported) {
3789 case TPACPI_VIDEO_570:
3790 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3792 case TPACPI_VIDEO_770:
3793 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3795 case TPACPI_VIDEO_NEW:
3796 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3804 static int video_read(char *p)
3809 if (video_supported == TPACPI_VIDEO_NONE) {
3810 len += sprintf(p + len, "status:\t\tnot supported\n");
3814 status = video_outputsw_get();
3818 autosw = video_autosw_get();
3822 len += sprintf(p + len, "status:\t\tsupported\n");
3823 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3824 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3825 if (video_supported == TPACPI_VIDEO_NEW)
3826 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3827 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3828 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3829 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3830 if (video_supported == TPACPI_VIDEO_NEW)
3831 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3832 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3833 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3838 static int video_write(char *buf)
3841 int enable, disable, status;
3844 if (video_supported == TPACPI_VIDEO_NONE)
3850 while ((cmd = next_cmd(&buf))) {
3851 if (strlencmp(cmd, "lcd_enable") == 0) {
3852 enable |= TP_ACPI_VIDEO_S_LCD;
3853 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3854 disable |= TP_ACPI_VIDEO_S_LCD;
3855 } else if (strlencmp(cmd, "crt_enable") == 0) {
3856 enable |= TP_ACPI_VIDEO_S_CRT;
3857 } else if (strlencmp(cmd, "crt_disable") == 0) {
3858 disable |= TP_ACPI_VIDEO_S_CRT;
3859 } else if (video_supported == TPACPI_VIDEO_NEW &&
3860 strlencmp(cmd, "dvi_enable") == 0) {
3861 enable |= TP_ACPI_VIDEO_S_DVI;
3862 } else if (video_supported == TPACPI_VIDEO_NEW &&
3863 strlencmp(cmd, "dvi_disable") == 0) {
3864 disable |= TP_ACPI_VIDEO_S_DVI;
3865 } else if (strlencmp(cmd, "auto_enable") == 0) {
3866 res = video_autosw_set(1);
3869 } else if (strlencmp(cmd, "auto_disable") == 0) {
3870 res = video_autosw_set(0);
3873 } else if (strlencmp(cmd, "video_switch") == 0) {
3874 res = video_outputsw_cycle();
3877 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3878 res = video_expand_toggle();
3885 if (enable || disable) {
3886 status = video_outputsw_get();
3889 res = video_outputsw_set((status & ~disable) | enable);
3897 static struct ibm_struct video_driver_data = {
3900 .write = video_write,
3904 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3906 /*************************************************************************
3907 * Light (thinklight) subdriver
3910 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3911 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3913 static int light_get_status(void)
3917 if (tp_features.light_status) {
3918 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3926 static int light_set_status(int status)
3930 if (tp_features.light) {
3932 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3934 TP_CMOS_THINKLIGHT_ON :
3935 TP_CMOS_THINKLIGHT_OFF);
3937 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3940 return (rc)? 0 : -EIO;
3946 static void light_set_status_worker(struct work_struct *work)
3948 struct tpacpi_led_classdev *data =
3949 container_of(work, struct tpacpi_led_classdev, work);
3951 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3952 light_set_status((data->new_brightness != LED_OFF));
3955 static void light_sysfs_set(struct led_classdev *led_cdev,
3956 enum led_brightness brightness)
3958 struct tpacpi_led_classdev *data =
3959 container_of(led_cdev,
3960 struct tpacpi_led_classdev,
3962 data->new_brightness = brightness;
3963 queue_work(tpacpi_wq, &data->work);
3966 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3968 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3971 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3973 .name = "tpacpi::thinklight",
3974 .brightness_set = &light_sysfs_set,
3975 .brightness_get = &light_sysfs_get,
3979 static int __init light_init(struct ibm_init_struct *iibm)
3983 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3985 TPACPI_ACPIHANDLE_INIT(ledb);
3986 TPACPI_ACPIHANDLE_INIT(lght);
3987 TPACPI_ACPIHANDLE_INIT(cmos);
3988 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3990 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3991 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3993 if (tp_features.light)
3994 /* light status not supported on
3995 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3996 tp_features.light_status =
3997 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3999 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4000 str_supported(tp_features.light),
4001 str_supported(tp_features.light_status));
4003 if (!tp_features.light)
4006 rc = led_classdev_register(&tpacpi_pdev->dev,
4007 &tpacpi_led_thinklight.led_classdev);
4010 tp_features.light = 0;
4011 tp_features.light_status = 0;
4019 static void light_exit(void)
4021 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4022 if (work_pending(&tpacpi_led_thinklight.work))
4023 flush_workqueue(tpacpi_wq);
4026 static int light_read(char *p)
4031 if (!tp_features.light) {
4032 len += sprintf(p + len, "status:\t\tnot supported\n");
4033 } else if (!tp_features.light_status) {
4034 len += sprintf(p + len, "status:\t\tunknown\n");
4035 len += sprintf(p + len, "commands:\ton, off\n");
4037 status = light_get_status();
4040 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4041 len += sprintf(p + len, "commands:\ton, off\n");
4047 static int light_write(char *buf)
4052 if (!tp_features.light)
4055 while ((cmd = next_cmd(&buf))) {
4056 if (strlencmp(cmd, "on") == 0) {
4058 } else if (strlencmp(cmd, "off") == 0) {
4064 return light_set_status(newstatus);
4067 static struct ibm_struct light_driver_data = {
4070 .write = light_write,
4074 /*************************************************************************
4078 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4080 static void dock_notify(struct ibm_struct *ibm, u32 event);
4081 static int dock_read(char *p);
4082 static int dock_write(char *buf);
4084 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4085 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4086 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4087 "\\_SB.PCI.ISA.SLCE", /* 570 */
4088 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4090 /* don't list other alternatives as we install a notify handler on the 570 */
4091 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4093 static const struct acpi_device_id ibm_pci_device_ids[] = {
4094 {PCI_ROOT_HID_STRING, 0},
4098 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4100 .notify = dock_notify,
4101 .handle = &dock_handle,
4102 .type = ACPI_SYSTEM_NOTIFY,
4105 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4106 * We just use it to get notifications of dock hotplug
4107 * in very old thinkpads */
4108 .hid = ibm_pci_device_ids,
4109 .notify = dock_notify,
4110 .handle = &pci_handle,
4111 .type = ACPI_SYSTEM_NOTIFY,
4115 static struct ibm_struct dock_driver_data[2] = {
4119 .write = dock_write,
4120 .acpi = &ibm_dock_acpidriver[0],
4124 .acpi = &ibm_dock_acpidriver[1],
4128 #define dock_docked() (_sta(dock_handle) & 1)
4130 static int __init dock_init(struct ibm_init_struct *iibm)
4132 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4134 TPACPI_ACPIHANDLE_INIT(dock);
4136 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4137 str_supported(dock_handle != NULL));
4139 return (dock_handle)? 0 : 1;
4142 static int __init dock_init2(struct ibm_init_struct *iibm)
4146 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4148 if (dock_driver_data[0].flags.acpi_driver_registered &&
4149 dock_driver_data[0].flags.acpi_notify_installed) {
4150 TPACPI_ACPIHANDLE_INIT(pci);
4151 dock2_needed = (pci_handle != NULL);
4152 vdbg_printk(TPACPI_DBG_INIT,
4153 "dock PCI handler for the TP 570 is %s\n",
4154 str_supported(dock2_needed));
4156 vdbg_printk(TPACPI_DBG_INIT,
4157 "dock subdriver part 2 not required\n");
4161 return (dock2_needed)? 0 : 1;
4164 static void dock_notify(struct ibm_struct *ibm, u32 event)
4166 int docked = dock_docked();
4167 int pci = ibm->acpi->hid && ibm->acpi->device &&
4168 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4171 if (event == 1 && !pci) /* 570 */
4172 data = 1; /* button */
4173 else if (event == 1 && pci) /* 570 */
4174 data = 3; /* dock */
4175 else if (event == 3 && docked)
4176 data = 1; /* button */
4177 else if (event == 3 && !docked)
4178 data = 2; /* undock */
4179 else if (event == 0 && docked)
4180 data = 3; /* dock */
4182 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4183 event, _sta(dock_handle));
4184 data = 0; /* unknown */
4186 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4187 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4188 dev_name(&ibm->acpi->device->dev),
4192 static int dock_read(char *p)
4195 int docked = dock_docked();
4198 len += sprintf(p + len, "status:\t\tnot supported\n");
4200 len += sprintf(p + len, "status:\t\tundocked\n");
4202 len += sprintf(p + len, "status:\t\tdocked\n");
4203 len += sprintf(p + len, "commands:\tdock, undock\n");
4209 static int dock_write(char *buf)
4216 while ((cmd = next_cmd(&buf))) {
4217 if (strlencmp(cmd, "undock") == 0) {
4218 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4219 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4221 } else if (strlencmp(cmd, "dock") == 0) {
4222 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4231 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4233 /*************************************************************************
4237 #ifdef CONFIG_THINKPAD_ACPI_BAY
4239 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4240 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4241 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4242 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4243 ); /* A21e, R30, R31 */
4244 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4245 "_EJ0", /* all others */
4246 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4247 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4248 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4250 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4254 static int __init bay_init(struct ibm_init_struct *iibm)
4256 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4258 TPACPI_ACPIHANDLE_INIT(bay);
4260 TPACPI_ACPIHANDLE_INIT(bay_ej);
4261 TPACPI_ACPIHANDLE_INIT(bay2);
4263 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4265 tp_features.bay_status = bay_handle &&
4266 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4267 tp_features.bay_status2 = bay2_handle &&
4268 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4270 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4271 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4272 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4273 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4275 vdbg_printk(TPACPI_DBG_INIT,
4276 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4277 str_supported(tp_features.bay_status),
4278 str_supported(tp_features.bay_eject),
4279 str_supported(tp_features.bay_status2),
4280 str_supported(tp_features.bay_eject2));
4282 return (tp_features.bay_status || tp_features.bay_eject ||
4283 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4286 static void bay_notify(struct ibm_struct *ibm, u32 event)
4288 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4289 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4290 dev_name(&ibm->acpi->device->dev),
4294 #define bay_occupied(b) (_sta(b##_handle) & 1)
4296 static int bay_read(char *p)
4299 int occupied = bay_occupied(bay);
4300 int occupied2 = bay_occupied(bay2);
4303 len += sprintf(p + len, "status:\t\t%s\n",
4304 tp_features.bay_status ?
4305 (occupied ? "occupied" : "unoccupied") :
4307 if (tp_features.bay_status2)
4308 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4309 "occupied" : "unoccupied");
4311 eject = tp_features.bay_eject && occupied;
4312 eject2 = tp_features.bay_eject2 && occupied2;
4314 if (eject && eject2)
4315 len += sprintf(p + len, "commands:\teject, eject2\n");
4317 len += sprintf(p + len, "commands:\teject\n");
4319 len += sprintf(p + len, "commands:\teject2\n");
4324 static int bay_write(char *buf)
4328 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4331 while ((cmd = next_cmd(&buf))) {
4332 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4333 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4335 } else if (tp_features.bay_eject2 &&
4336 strlencmp(cmd, "eject2") == 0) {
4337 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4346 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4347 .notify = bay_notify,
4348 .handle = &bay_handle,
4349 .type = ACPI_SYSTEM_NOTIFY,
4352 static struct ibm_struct bay_driver_data = {
4356 .acpi = &ibm_bay_acpidriver,
4359 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4361 /*************************************************************************
4365 /* sysfs cmos_command -------------------------------------------------- */
4366 static ssize_t cmos_command_store(struct device *dev,
4367 struct device_attribute *attr,
4368 const char *buf, size_t count)
4370 unsigned long cmos_cmd;
4373 if (parse_strtoul(buf, 21, &cmos_cmd))
4376 res = issue_thinkpad_cmos_command(cmos_cmd);
4377 return (res)? res : count;
4380 static struct device_attribute dev_attr_cmos_command =
4381 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4383 /* --------------------------------------------------------------------- */
4385 static int __init cmos_init(struct ibm_init_struct *iibm)
4389 vdbg_printk(TPACPI_DBG_INIT,
4390 "initializing cmos commands subdriver\n");
4392 TPACPI_ACPIHANDLE_INIT(cmos);
4394 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4395 str_supported(cmos_handle != NULL));
4397 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4401 return (cmos_handle)? 0 : 1;
4404 static void cmos_exit(void)
4406 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4409 static int cmos_read(char *p)
4413 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4414 R30, R31, T20-22, X20-21 */
4416 len += sprintf(p + len, "status:\t\tnot supported\n");
4418 len += sprintf(p + len, "status:\t\tsupported\n");
4419 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4425 static int cmos_write(char *buf)
4430 while ((cmd = next_cmd(&buf))) {
4431 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4432 cmos_cmd >= 0 && cmos_cmd <= 21) {
4437 res = issue_thinkpad_cmos_command(cmos_cmd);
4445 static struct ibm_struct cmos_driver_data = {
4448 .write = cmos_write,
4452 /*************************************************************************
4456 enum led_access_mode {
4457 TPACPI_LED_NONE = 0,
4458 TPACPI_LED_570, /* 570 */
4459 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4460 TPACPI_LED_NEW, /* all others */
4463 enum { /* For TPACPI_LED_OLD */
4464 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4465 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4466 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4475 static enum led_access_mode led_supported;
4477 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4478 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4479 /* T20-22, X20-21 */
4480 "LED", /* all others */
4483 #define TPACPI_LED_NUMLEDS 8
4484 static struct tpacpi_led_classdev *tpacpi_leds;
4485 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4486 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4487 /* there's a limit of 19 chars + NULL before 2.6.26 */
4489 "tpacpi:orange:batt",
4490 "tpacpi:green:batt",
4491 "tpacpi::dock_active",
4492 "tpacpi::bay_active",
4493 "tpacpi::dock_batt",
4494 "tpacpi::unknown_led",
4498 static int led_get_status(const unsigned int led)
4501 enum led_status_t led_s;
4503 switch (led_supported) {
4504 case TPACPI_LED_570:
4505 if (!acpi_evalf(ec_handle,
4506 &status, "GLED", "dd", 1 << led))
4508 led_s = (status == 0)?
4513 tpacpi_led_state_cache[led] = led_s;
4522 static int led_set_status(const unsigned int led,
4523 const enum led_status_t ledstatus)
4525 /* off, on, blink. Index is led_status_t */
4526 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4527 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4531 switch (led_supported) {
4532 case TPACPI_LED_570:
4536 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4537 (1 << led), led_sled_arg1[ledstatus]))
4540 case TPACPI_LED_OLD:
4541 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4544 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4546 rc = ec_write(TPACPI_LED_EC_HLBL,
4547 (ledstatus == TPACPI_LED_BLINK) << led);
4549 rc = ec_write(TPACPI_LED_EC_HLCL,
4550 (ledstatus != TPACPI_LED_OFF) << led);
4552 case TPACPI_LED_NEW:
4554 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4555 led, led_led_arg1[ledstatus]))
4563 tpacpi_led_state_cache[led] = ledstatus;
4568 static void led_sysfs_set_status(unsigned int led,
4569 enum led_brightness brightness)
4572 (brightness == LED_OFF) ?
4574 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4575 TPACPI_LED_BLINK : TPACPI_LED_ON);
4578 static void led_set_status_worker(struct work_struct *work)
4580 struct tpacpi_led_classdev *data =
4581 container_of(work, struct tpacpi_led_classdev, work);
4583 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4584 led_sysfs_set_status(data->led, data->new_brightness);
4587 static void led_sysfs_set(struct led_classdev *led_cdev,
4588 enum led_brightness brightness)
4590 struct tpacpi_led_classdev *data = container_of(led_cdev,
4591 struct tpacpi_led_classdev, led_classdev);
4593 data->new_brightness = brightness;
4594 queue_work(tpacpi_wq, &data->work);
4597 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4598 unsigned long *delay_on, unsigned long *delay_off)
4600 struct tpacpi_led_classdev *data = container_of(led_cdev,
4601 struct tpacpi_led_classdev, led_classdev);
4603 /* Can we choose the flash rate? */
4604 if (*delay_on == 0 && *delay_off == 0) {
4605 /* yes. set them to the hardware blink rate (1 Hz) */
4606 *delay_on = 500; /* ms */
4607 *delay_off = 500; /* ms */
4608 } else if ((*delay_on != 500) || (*delay_off != 500))
4611 data->new_brightness = TPACPI_LED_BLINK;
4612 queue_work(tpacpi_wq, &data->work);
4617 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4621 struct tpacpi_led_classdev *data = container_of(led_cdev,
4622 struct tpacpi_led_classdev, led_classdev);
4624 rc = led_get_status(data->led);
4626 if (rc == TPACPI_LED_OFF || rc < 0)
4627 rc = LED_OFF; /* no error handling in led class :( */
4634 static void led_exit(void)
4638 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4639 if (tpacpi_leds[i].led_classdev.name)
4640 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4646 static int __init led_init(struct ibm_init_struct *iibm)
4651 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4653 TPACPI_ACPIHANDLE_INIT(led);
4656 /* led not supported on R30, R31 */
4657 led_supported = TPACPI_LED_NONE;
4658 else if (strlencmp(led_path, "SLED") == 0)
4660 led_supported = TPACPI_LED_570;
4661 else if (strlencmp(led_path, "SYSL") == 0)
4662 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4663 led_supported = TPACPI_LED_OLD;
4666 led_supported = TPACPI_LED_NEW;
4668 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4669 str_supported(led_supported), led_supported);
4671 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4674 printk(TPACPI_ERR "Out of memory for LED data\n");
4678 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4679 tpacpi_leds[i].led = i;
4681 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4682 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4683 if (led_supported == TPACPI_LED_570)
4684 tpacpi_leds[i].led_classdev.brightness_get =
4687 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4689 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4691 rc = led_classdev_register(&tpacpi_pdev->dev,
4692 &tpacpi_leds[i].led_classdev);
4694 tpacpi_leds[i].led_classdev.name = NULL;
4700 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4703 #define str_led_status(s) \
4704 ((s) == TPACPI_LED_OFF ? "off" : \
4705 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4707 static int led_read(char *p)
4711 if (!led_supported) {
4712 len += sprintf(p + len, "status:\t\tnot supported\n");
4715 len += sprintf(p + len, "status:\t\tsupported\n");
4717 if (led_supported == TPACPI_LED_570) {
4720 for (i = 0; i < 8; i++) {
4721 status = led_get_status(i);
4724 len += sprintf(p + len, "%d:\t\t%s\n",
4725 i, str_led_status(status));
4729 len += sprintf(p + len, "commands:\t"
4730 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4735 static int led_write(char *buf)
4739 enum led_status_t s;
4744 while ((cmd = next_cmd(&buf))) {
4745 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4748 if (strstr(cmd, "off")) {
4750 } else if (strstr(cmd, "on")) {
4752 } else if (strstr(cmd, "blink")) {
4753 s = TPACPI_LED_BLINK;
4758 rc = led_set_status(led, s);
4766 static struct ibm_struct led_driver_data = {
4773 /*************************************************************************
4777 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4779 static int __init beep_init(struct ibm_init_struct *iibm)
4781 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4783 TPACPI_ACPIHANDLE_INIT(beep);
4785 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4786 str_supported(beep_handle != NULL));
4788 return (beep_handle)? 0 : 1;
4791 static int beep_read(char *p)
4796 len += sprintf(p + len, "status:\t\tnot supported\n");
4798 len += sprintf(p + len, "status:\t\tsupported\n");
4799 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4805 static int beep_write(char *buf)
4813 while ((cmd = next_cmd(&buf))) {
4814 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4815 beep_cmd >= 0 && beep_cmd <= 17) {
4819 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4826 static struct ibm_struct beep_driver_data = {
4829 .write = beep_write,
4832 /*************************************************************************
4836 enum thermal_access_mode {
4837 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4838 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4839 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4840 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4841 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4844 enum { /* TPACPI_THERMAL_TPEC_* */
4845 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4846 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4847 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4850 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4851 struct ibm_thermal_sensors_struct {
4852 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4855 static enum thermal_access_mode thermal_read_mode;
4857 /* idx is zero-based */
4858 static int thermal_get_sensor(int idx, s32 *value)
4864 t = TP_EC_THERMAL_TMP0;
4866 switch (thermal_read_mode) {
4867 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4868 case TPACPI_THERMAL_TPEC_16:
4869 if (idx >= 8 && idx <= 15) {
4870 t = TP_EC_THERMAL_TMP8;
4875 case TPACPI_THERMAL_TPEC_8:
4877 if (!acpi_ec_read(t + idx, &tmp))
4879 *value = tmp * 1000;
4884 case TPACPI_THERMAL_ACPI_UPDT:
4886 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4887 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4889 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4891 *value = (t - 2732) * 100;
4896 case TPACPI_THERMAL_ACPI_TMP07:
4898 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4899 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4901 if (t > 127 || t < -127)
4902 t = TP_EC_THERMAL_TMP_NA;
4908 case TPACPI_THERMAL_NONE:
4916 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4927 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4930 for (i = 0 ; i < n; i++) {
4931 res = thermal_get_sensor(i, &s->temp[i]);
4939 /* sysfs temp##_input -------------------------------------------------- */
4941 static ssize_t thermal_temp_input_show(struct device *dev,
4942 struct device_attribute *attr,
4945 struct sensor_device_attribute *sensor_attr =
4946 to_sensor_dev_attr(attr);
4947 int idx = sensor_attr->index;
4951 res = thermal_get_sensor(idx, &value);
4954 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4957 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4960 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4961 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4962 thermal_temp_input_show, NULL, _idxB)
4964 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4965 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4966 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4967 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4968 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4969 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4970 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4971 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4972 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4973 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4974 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4975 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4976 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4977 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4978 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4979 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4980 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4983 #define THERMAL_ATTRS(X) \
4984 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4986 static struct attribute *thermal_temp_input_attr[] = {
5006 static const struct attribute_group thermal_temp_input16_group = {
5007 .attrs = thermal_temp_input_attr
5010 static const struct attribute_group thermal_temp_input8_group = {
5011 .attrs = &thermal_temp_input_attr[8]
5014 #undef THERMAL_SENSOR_ATTR_TEMP
5015 #undef THERMAL_ATTRS
5017 /* --------------------------------------------------------------------- */
5019 static int __init thermal_init(struct ibm_init_struct *iibm)
5026 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5028 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5030 if (thinkpad_id.ec_model) {
5032 * Direct EC access mode: sensors at registers
5033 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5034 * non-implemented, thermal sensors return 0x80 when
5039 for (i = 0; i < 8; i++) {
5040 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5046 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5054 /* This is sheer paranoia, but we handle it anyway */
5057 "ThinkPad ACPI EC access misbehaving, "
5058 "falling back to ACPI TMPx access "
5060 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5063 "ThinkPad ACPI EC access misbehaving, "
5064 "disabling thermal sensors access\n");
5065 thermal_read_mode = TPACPI_THERMAL_NONE;
5070 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5072 } else if (acpi_tmp7) {
5073 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5074 /* 600e/x, 770e, 770x */
5075 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5077 /* Standard ACPI TMPx access, max 8 sensors */
5078 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5081 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5082 thermal_read_mode = TPACPI_THERMAL_NONE;
5085 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5086 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5089 switch (thermal_read_mode) {
5090 case TPACPI_THERMAL_TPEC_16:
5091 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5092 &thermal_temp_input16_group);
5096 case TPACPI_THERMAL_TPEC_8:
5097 case TPACPI_THERMAL_ACPI_TMP07:
5098 case TPACPI_THERMAL_ACPI_UPDT:
5099 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5100 &thermal_temp_input8_group);
5104 case TPACPI_THERMAL_NONE:
5112 static void thermal_exit(void)
5114 switch (thermal_read_mode) {
5115 case TPACPI_THERMAL_TPEC_16:
5116 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5117 &thermal_temp_input16_group);
5119 case TPACPI_THERMAL_TPEC_8:
5120 case TPACPI_THERMAL_ACPI_TMP07:
5121 case TPACPI_THERMAL_ACPI_UPDT:
5122 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5123 &thermal_temp_input16_group);
5125 case TPACPI_THERMAL_NONE:
5131 static int thermal_read(char *p)
5135 struct ibm_thermal_sensors_struct t;
5137 n = thermal_get_sensors(&t);
5138 if (unlikely(n < 0))
5141 len += sprintf(p + len, "temperatures:\t");
5144 for (i = 0; i < (n - 1); i++)
5145 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5146 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5148 len += sprintf(p + len, "not supported\n");
5153 static struct ibm_struct thermal_driver_data = {
5155 .read = thermal_read,
5156 .exit = thermal_exit,
5159 /*************************************************************************
5163 static u8 ecdump_regs[256];
5165 static int ecdump_read(char *p)
5171 len += sprintf(p + len, "EC "
5172 " +00 +01 +02 +03 +04 +05 +06 +07"
5173 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5174 for (i = 0; i < 256; i += 16) {
5175 len += sprintf(p + len, "EC 0x%02x:", i);
5176 for (j = 0; j < 16; j++) {
5177 if (!acpi_ec_read(i + j, &v))
5179 if (v != ecdump_regs[i + j])
5180 len += sprintf(p + len, " *%02x", v);
5182 len += sprintf(p + len, " %02x", v);
5183 ecdump_regs[i + j] = v;
5185 len += sprintf(p + len, "\n");
5190 /* These are way too dangerous to advertise openly... */
5192 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5193 " (<offset> is 00-ff, <value> is 00-ff)\n");
5194 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5195 " (<offset> is 00-ff, <value> is 0-255)\n");
5200 static int ecdump_write(char *buf)
5205 while ((cmd = next_cmd(&buf))) {
5206 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5208 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5212 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5213 if (!acpi_ec_write(i, v))
5222 static struct ibm_struct ecdump_driver_data = {
5224 .read = ecdump_read,
5225 .write = ecdump_write,
5226 .flags.experimental = 1,
5229 /*************************************************************************
5230 * Backlight/brightness subdriver
5233 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5236 TP_EC_BACKLIGHT = 0x31,
5238 /* TP_EC_BACKLIGHT bitmasks */
5239 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5240 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5241 TP_EC_BACKLIGHT_MAPSW = 0x20,
5244 static struct backlight_device *ibm_backlight_device;
5245 static int brightness_mode;
5246 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5248 static struct mutex brightness_mutex;
5251 * ThinkPads can read brightness from two places: EC 0x31, or
5252 * CMOS NVRAM byte 0x5E, bits 0-3.
5254 * EC 0x31 has the following layout
5255 * Bit 7: unknown function
5256 * Bit 6: unknown function
5257 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5258 * Bit 4: must be set to zero to avoid problems
5259 * Bit 3-0: backlight brightness level
5261 * brightness_get_raw returns status data in the EC 0x31 layout
5263 static int brightness_get_raw(int *status)
5265 u8 lec = 0, lcmos = 0, level = 0;
5267 if (brightness_mode & 1) {
5268 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5270 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5272 if (brightness_mode & 2) {
5273 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5274 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5275 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5276 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5280 if (brightness_mode == 3) {
5281 *status = lec; /* Prefer EC, CMOS is just a backing store */
5282 lec &= TP_EC_BACKLIGHT_LVLMSK;
5284 tp_warned.bright_cmos_ec_unsync = 0;
5286 if (!tp_warned.bright_cmos_ec_unsync) {
5288 "CMOS NVRAM (%u) and EC (%u) do not "
5289 "agree on display brightness level\n",
5290 (unsigned int) lcmos,
5291 (unsigned int) lec);
5292 tp_warned.bright_cmos_ec_unsync = 1;
5303 /* May return EINTR which can always be mapped to ERESTARTSYS */
5304 static int brightness_set(int value)
5306 int cmos_cmd, inc, i, res;
5310 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5314 res = mutex_lock_interruptible(&brightness_mutex);
5318 res = brightness_get_raw(¤t_value);
5322 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5323 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5325 cmos_cmd = value > current_value ?
5326 TP_CMOS_BRIGHTNESS_UP :
5327 TP_CMOS_BRIGHTNESS_DOWN;
5328 inc = (value > current_value)? 1 : -1;
5331 for (i = current_value; i != value; i += inc) {
5332 if ((brightness_mode & 2) &&
5333 issue_thinkpad_cmos_command(cmos_cmd)) {
5337 if ((brightness_mode & 1) &&
5338 !acpi_ec_write(TP_EC_BACKLIGHT,
5339 (i + inc) | command_bits)) {
5346 mutex_unlock(&brightness_mutex);
5350 /* sysfs backlight class ----------------------------------------------- */
5352 static int brightness_update_status(struct backlight_device *bd)
5354 /* it is the backlight class's job (caller) to handle
5355 * EINTR and other errors properly */
5356 return brightness_set(
5357 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5358 bd->props.power == FB_BLANK_UNBLANK) ?
5359 bd->props.brightness : 0);
5362 static int brightness_get(struct backlight_device *bd)
5366 res = brightness_get_raw(&status);
5368 return 0; /* FIXME: teach backlight about error handling */
5370 return status & TP_EC_BACKLIGHT_LVLMSK;
5373 static struct backlight_ops ibm_backlight_data = {
5374 .get_brightness = brightness_get,
5375 .update_status = brightness_update_status,
5378 /* --------------------------------------------------------------------- */
5380 static int __init brightness_init(struct ibm_init_struct *iibm)
5384 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5386 mutex_init(&brightness_mutex);
5389 * We always attempt to detect acpi support, so as to switch
5390 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5391 * going to publish a backlight interface
5393 b = tpacpi_check_std_acpi_brightness_support();
5396 if (acpi_video_backlight_support()) {
5397 if (brightness_enable > 1) {
5398 printk(TPACPI_NOTICE
5399 "Standard ACPI backlight interface "
5400 "available, not loading native one.\n");
5402 } else if (brightness_enable == 1) {
5403 printk(TPACPI_NOTICE
5404 "Backlight control force enabled, even if standard "
5405 "ACPI backlight interface is available\n");
5408 if (brightness_enable > 1) {
5409 printk(TPACPI_NOTICE
5410 "Standard ACPI backlight interface not "
5411 "available, thinkpad_acpi native "
5412 "brightness control enabled\n");
5417 if (!brightness_enable) {
5418 dbg_printk(TPACPI_DBG_INIT,
5419 "brightness support disabled by "
5420 "module parameter\n");
5426 "Unsupported brightness interface, "
5427 "please contact %s\n", TPACPI_MAIL);
5431 tp_features.bright_16levels = 1;
5433 if (!brightness_mode) {
5434 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5435 brightness_mode = 2;
5437 brightness_mode = 3;
5439 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5443 if (brightness_mode > 3)
5446 if (brightness_get_raw(&b) < 0)
5449 if (tp_features.bright_16levels)
5451 "detected a 16-level brightness capable ThinkPad\n");
5453 ibm_backlight_device = backlight_device_register(
5454 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5455 &ibm_backlight_data);
5456 if (IS_ERR(ibm_backlight_device)) {
5457 printk(TPACPI_ERR "Could not register backlight device\n");
5458 return PTR_ERR(ibm_backlight_device);
5460 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5462 ibm_backlight_device->props.max_brightness =
5463 (tp_features.bright_16levels)? 15 : 7;
5464 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5465 backlight_update_status(ibm_backlight_device);
5470 static void brightness_exit(void)
5472 if (ibm_backlight_device) {
5473 vdbg_printk(TPACPI_DBG_EXIT,
5474 "calling backlight_device_unregister()\n");
5475 backlight_device_unregister(ibm_backlight_device);
5479 static int brightness_read(char *p)
5484 level = brightness_get(NULL);
5486 len += sprintf(p + len, "level:\t\tunreadable\n");
5488 len += sprintf(p + len, "level:\t\t%d\n", level);
5489 len += sprintf(p + len, "commands:\tup, down\n");
5490 len += sprintf(p + len, "commands:\tlevel <level>"
5491 " (<level> is 0-%d)\n",
5492 (tp_features.bright_16levels) ? 15 : 7);
5498 static int brightness_write(char *buf)
5503 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5505 level = brightness_get(NULL);
5509 while ((cmd = next_cmd(&buf))) {
5510 if (strlencmp(cmd, "up") == 0) {
5511 if (level < max_level)
5513 } else if (strlencmp(cmd, "down") == 0) {
5516 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5517 level >= 0 && level <= max_level) {
5524 * Now we know what the final level should be, so we try to set it.
5525 * Doing it this way makes the syscall restartable in case of EINTR
5527 rc = brightness_set(level);
5528 return (rc == -EINTR)? ERESTARTSYS : rc;
5531 static struct ibm_struct brightness_driver_data = {
5532 .name = "brightness",
5533 .read = brightness_read,
5534 .write = brightness_write,
5535 .exit = brightness_exit,
5538 /*************************************************************************
5542 static int volume_offset = 0x30;
5544 static int volume_read(char *p)
5549 if (!acpi_ec_read(volume_offset, &level)) {
5550 len += sprintf(p + len, "level:\t\tunreadable\n");
5552 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5553 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5554 len += sprintf(p + len, "commands:\tup, down, mute\n");
5555 len += sprintf(p + len, "commands:\tlevel <level>"
5556 " (<level> is 0-15)\n");
5562 static int volume_write(char *buf)
5564 int cmos_cmd, inc, i;
5566 int new_level, new_mute;
5569 while ((cmd = next_cmd(&buf))) {
5570 if (!acpi_ec_read(volume_offset, &level))
5572 new_mute = mute = level & 0x40;
5573 new_level = level = level & 0xf;
5575 if (strlencmp(cmd, "up") == 0) {
5579 new_level = level == 15 ? 15 : level + 1;
5580 } else if (strlencmp(cmd, "down") == 0) {
5584 new_level = level == 0 ? 0 : level - 1;
5585 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5586 new_level >= 0 && new_level <= 15) {
5588 } else if (strlencmp(cmd, "mute") == 0) {
5593 if (new_level != level) {
5594 /* mute doesn't change */
5596 cmos_cmd = (new_level > level) ?
5597 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5598 inc = new_level > level ? 1 : -1;
5600 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5601 !acpi_ec_write(volume_offset, level)))
5604 for (i = level; i != new_level; i += inc)
5605 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5606 !acpi_ec_write(volume_offset, i + inc))
5610 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5611 !acpi_ec_write(volume_offset, new_level + mute))) {
5616 if (new_mute != mute) {
5617 /* level doesn't change */
5619 cmos_cmd = (new_mute) ?
5620 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5622 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5623 !acpi_ec_write(volume_offset, level + new_mute))
5631 static struct ibm_struct volume_driver_data = {
5633 .read = volume_read,
5634 .write = volume_write,
5637 /*************************************************************************
5644 * TPACPI_FAN_RD_ACPI_GFAN:
5645 * ACPI GFAN method: returns fan level
5647 * see TPACPI_FAN_WR_ACPI_SFAN
5648 * EC 0x2f (HFSP) not available if GFAN exists
5650 * TPACPI_FAN_WR_ACPI_SFAN:
5651 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5653 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5656 * TPACPI_FAN_WR_TPEC:
5657 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5658 * Supported on almost all ThinkPads
5660 * Fan speed changes of any sort (including those caused by the
5661 * disengaged mode) are usually done slowly by the firmware as the
5662 * maximum ammount of fan duty cycle change per second seems to be
5665 * Reading is not available if GFAN exists.
5666 * Writing is not available if SFAN exists.
5669 * 7 automatic mode engaged;
5670 * (default operation mode of the ThinkPad)
5671 * fan level is ignored in this mode.
5672 * 6 full speed mode (takes precedence over bit 7);
5673 * not available on all thinkpads. May disable
5674 * the tachometer while the fan controller ramps up
5675 * the speed (which can take up to a few *minutes*).
5676 * Speeds up fan to 100% duty-cycle, which is far above
5677 * the standard RPM levels. It is not impossible that
5678 * it could cause hardware damage.
5679 * 5-3 unused in some models. Extra bits for fan level
5680 * in others, but still useless as all values above
5681 * 7 map to the same speed as level 7 in these models.
5682 * 2-0 fan level (0..7 usually)
5684 * 0x07 = max (set when temperatures critical)
5685 * Some ThinkPads may have other levels, see
5686 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5688 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5689 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5690 * does so, its initial value is meaningless (0x07).
5692 * For firmware bugs, refer to:
5693 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5697 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5698 * Main fan tachometer reading (in RPM)
5700 * This register is present on all ThinkPads with a new-style EC, and
5701 * it is known not to be present on the A21m/e, and T22, as there is
5702 * something else in offset 0x84 according to the ACPI DSDT. Other
5703 * ThinkPads from this same time period (and earlier) probably lack the
5704 * tachometer as well.
5706 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5707 * was never fixed by IBM to report the EC firmware version string
5708 * probably support the tachometer (like the early X models), so
5709 * detecting it is quite hard. We need more data to know for sure.
5711 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5714 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5717 * For firmware bugs, refer to:
5718 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5720 * TPACPI_FAN_WR_ACPI_FANS:
5721 * ThinkPad X31, X40, X41. Not available in the X60.
5723 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5724 * high speed. ACPI DSDT seems to map these three speeds to levels
5725 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5726 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5728 * The speeds are stored on handles
5729 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5731 * There are three default speed sets, acessible as handles:
5732 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5734 * ACPI DSDT switches which set is in use depending on various
5737 * TPACPI_FAN_WR_TPEC is also available and should be used to
5738 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5739 * but the ACPI tables just mention level 7.
5742 enum { /* Fan control constants */
5743 fan_status_offset = 0x2f, /* EC register 0x2f */
5744 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5745 * 0x84 must be read before 0x85 */
5747 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5748 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5750 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5753 enum fan_status_access_mode {
5754 TPACPI_FAN_NONE = 0, /* No fan status or control */
5755 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5756 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5759 enum fan_control_access_mode {
5760 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5761 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5762 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5763 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5766 enum fan_control_commands {
5767 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5768 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5769 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5770 * and also watchdog cmd */
5773 static int fan_control_allowed;
5775 static enum fan_status_access_mode fan_status_access_mode;
5776 static enum fan_control_access_mode fan_control_access_mode;
5777 static enum fan_control_commands fan_control_commands;
5779 static u8 fan_control_initial_status;
5780 static u8 fan_control_desired_level;
5781 static u8 fan_control_resume_level;
5782 static int fan_watchdog_maxinterval;
5784 static struct mutex fan_mutex;
5786 static void fan_watchdog_fire(struct work_struct *ignored);
5787 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5789 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5790 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5791 "\\FSPD", /* 600e/x, 770e, 770x */
5793 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5794 "JFNS", /* 770x-JL */
5798 * Call with fan_mutex held
5800 static void fan_update_desired_level(u8 status)
5803 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5805 fan_control_desired_level = 7;
5807 fan_control_desired_level = status;
5811 static int fan_get_status(u8 *status)
5816 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5818 switch (fan_status_access_mode) {
5819 case TPACPI_FAN_RD_ACPI_GFAN:
5820 /* 570, 600e/x, 770e, 770x */
5822 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5830 case TPACPI_FAN_RD_TPEC:
5831 /* all except 570, 600e/x, 770e, 770x */
5832 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5847 static int fan_get_status_safe(u8 *status)
5852 if (mutex_lock_interruptible(&fan_mutex))
5853 return -ERESTARTSYS;
5854 rc = fan_get_status(&s);
5856 fan_update_desired_level(s);
5857 mutex_unlock(&fan_mutex);
5865 static int fan_get_speed(unsigned int *speed)
5869 switch (fan_status_access_mode) {
5870 case TPACPI_FAN_RD_TPEC:
5871 /* all except 570, 600e/x, 770e, 770x */
5872 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5873 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5877 *speed = (hi << 8) | lo;
5888 static int fan_set_level(int level)
5890 if (!fan_control_allowed)
5893 switch (fan_control_access_mode) {
5894 case TPACPI_FAN_WR_ACPI_SFAN:
5895 if (level >= 0 && level <= 7) {
5896 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5902 case TPACPI_FAN_WR_ACPI_FANS:
5903 case TPACPI_FAN_WR_TPEC:
5904 if (!(level & TP_EC_FAN_AUTO) &&
5905 !(level & TP_EC_FAN_FULLSPEED) &&
5906 ((level < 0) || (level > 7)))
5909 /* safety net should the EC not support AUTO
5910 * or FULLSPEED mode bits and just ignore them */
5911 if (level & TP_EC_FAN_FULLSPEED)
5912 level |= 7; /* safety min speed 7 */
5913 else if (level & TP_EC_FAN_AUTO)
5914 level |= 4; /* safety min speed 4 */
5916 if (!acpi_ec_write(fan_status_offset, level))
5919 tp_features.fan_ctrl_status_undef = 0;
5928 static int fan_set_level_safe(int level)
5932 if (!fan_control_allowed)
5935 if (mutex_lock_interruptible(&fan_mutex))
5936 return -ERESTARTSYS;
5938 if (level == TPACPI_FAN_LAST_LEVEL)
5939 level = fan_control_desired_level;
5941 rc = fan_set_level(level);
5943 fan_update_desired_level(level);
5945 mutex_unlock(&fan_mutex);
5949 static int fan_set_enable(void)
5954 if (!fan_control_allowed)
5957 if (mutex_lock_interruptible(&fan_mutex))
5958 return -ERESTARTSYS;
5960 switch (fan_control_access_mode) {
5961 case TPACPI_FAN_WR_ACPI_FANS:
5962 case TPACPI_FAN_WR_TPEC:
5963 rc = fan_get_status(&s);
5967 /* Don't go out of emergency fan mode */
5970 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5973 if (!acpi_ec_write(fan_status_offset, s))
5976 tp_features.fan_ctrl_status_undef = 0;
5981 case TPACPI_FAN_WR_ACPI_SFAN:
5982 rc = fan_get_status(&s);
5988 /* Set fan to at least level 4 */
5991 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6001 mutex_unlock(&fan_mutex);
6005 static int fan_set_disable(void)
6009 if (!fan_control_allowed)
6012 if (mutex_lock_interruptible(&fan_mutex))
6013 return -ERESTARTSYS;
6016 switch (fan_control_access_mode) {
6017 case TPACPI_FAN_WR_ACPI_FANS:
6018 case TPACPI_FAN_WR_TPEC:
6019 if (!acpi_ec_write(fan_status_offset, 0x00))
6022 fan_control_desired_level = 0;
6023 tp_features.fan_ctrl_status_undef = 0;
6027 case TPACPI_FAN_WR_ACPI_SFAN:
6028 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6031 fan_control_desired_level = 0;
6039 mutex_unlock(&fan_mutex);
6043 static int fan_set_speed(int speed)
6047 if (!fan_control_allowed)
6050 if (mutex_lock_interruptible(&fan_mutex))
6051 return -ERESTARTSYS;
6054 switch (fan_control_access_mode) {
6055 case TPACPI_FAN_WR_ACPI_FANS:
6056 if (speed >= 0 && speed <= 65535) {
6057 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6058 speed, speed, speed))
6068 mutex_unlock(&fan_mutex);
6072 static void fan_watchdog_reset(void)
6074 static int fan_watchdog_active;
6076 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6079 if (fan_watchdog_active)
6080 cancel_delayed_work(&fan_watchdog_task);
6082 if (fan_watchdog_maxinterval > 0 &&
6083 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6084 fan_watchdog_active = 1;
6085 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6086 msecs_to_jiffies(fan_watchdog_maxinterval
6089 "failed to queue the fan watchdog, "
6090 "watchdog will not trigger\n");
6093 fan_watchdog_active = 0;
6096 static void fan_watchdog_fire(struct work_struct *ignored)
6100 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6103 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6104 rc = fan_set_enable();
6106 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6107 "will try again later...\n", -rc);
6108 /* reschedule for later */
6109 fan_watchdog_reset();
6114 * SYSFS fan layout: hwmon compatible (device)
6117 * 0: "disengaged" mode
6119 * 2: native EC "auto" mode (recommended, hardware default)
6121 * pwm*: set speed in manual mode, ignored otherwise.
6122 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6125 * fan*_input: tachometer reading, RPM
6128 * SYSFS fan layout: extensions
6130 * fan_watchdog (driver):
6131 * fan watchdog interval in seconds, 0 disables (default), max 120
6134 /* sysfs fan pwm1_enable ----------------------------------------------- */
6135 static ssize_t fan_pwm1_enable_show(struct device *dev,
6136 struct device_attribute *attr,
6142 res = fan_get_status_safe(&status);
6146 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6147 if (status != fan_control_initial_status) {
6148 tp_features.fan_ctrl_status_undef = 0;
6150 /* Return most likely status. In fact, it
6151 * might be the only possible status */
6152 status = TP_EC_FAN_AUTO;
6156 if (status & TP_EC_FAN_FULLSPEED) {
6158 } else if (status & TP_EC_FAN_AUTO) {
6163 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6166 static ssize_t fan_pwm1_enable_store(struct device *dev,
6167 struct device_attribute *attr,
6168 const char *buf, size_t count)
6173 if (parse_strtoul(buf, 2, &t))
6178 level = TP_EC_FAN_FULLSPEED;
6181 level = TPACPI_FAN_LAST_LEVEL;
6184 level = TP_EC_FAN_AUTO;
6187 /* reserved for software-controlled auto mode */
6193 res = fan_set_level_safe(level);
6199 fan_watchdog_reset();
6204 static struct device_attribute dev_attr_fan_pwm1_enable =
6205 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6206 fan_pwm1_enable_show, fan_pwm1_enable_store);
6208 /* sysfs fan pwm1 ------------------------------------------------------ */
6209 static ssize_t fan_pwm1_show(struct device *dev,
6210 struct device_attribute *attr,
6216 res = fan_get_status_safe(&status);
6220 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6221 if (status != fan_control_initial_status) {
6222 tp_features.fan_ctrl_status_undef = 0;
6224 status = TP_EC_FAN_AUTO;
6229 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6230 status = fan_control_desired_level;
6235 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6238 static ssize_t fan_pwm1_store(struct device *dev,
6239 struct device_attribute *attr,
6240 const char *buf, size_t count)
6244 u8 status, newlevel;
6246 if (parse_strtoul(buf, 255, &s))
6249 /* scale down from 0-255 to 0-7 */
6250 newlevel = (s >> 5) & 0x07;
6252 if (mutex_lock_interruptible(&fan_mutex))
6253 return -ERESTARTSYS;
6255 rc = fan_get_status(&status);
6256 if (!rc && (status &
6257 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6258 rc = fan_set_level(newlevel);
6262 fan_update_desired_level(newlevel);
6263 fan_watchdog_reset();
6267 mutex_unlock(&fan_mutex);
6268 return (rc)? rc : count;
6271 static struct device_attribute dev_attr_fan_pwm1 =
6272 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6273 fan_pwm1_show, fan_pwm1_store);
6275 /* sysfs fan fan1_input ------------------------------------------------ */
6276 static ssize_t fan_fan1_input_show(struct device *dev,
6277 struct device_attribute *attr,
6283 res = fan_get_speed(&speed);
6287 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6290 static struct device_attribute dev_attr_fan_fan1_input =
6291 __ATTR(fan1_input, S_IRUGO,
6292 fan_fan1_input_show, NULL);
6294 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6295 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6298 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6301 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6302 const char *buf, size_t count)
6306 if (parse_strtoul(buf, 120, &t))
6309 if (!fan_control_allowed)
6312 fan_watchdog_maxinterval = t;
6313 fan_watchdog_reset();
6318 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6319 fan_fan_watchdog_show, fan_fan_watchdog_store);
6321 /* --------------------------------------------------------------------- */
6322 static struct attribute *fan_attributes[] = {
6323 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6324 &dev_attr_fan_fan1_input.attr,
6328 static const struct attribute_group fan_attr_group = {
6329 .attrs = fan_attributes,
6332 static int __init fan_init(struct ibm_init_struct *iibm)
6336 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6338 mutex_init(&fan_mutex);
6339 fan_status_access_mode = TPACPI_FAN_NONE;
6340 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6341 fan_control_commands = 0;
6342 fan_watchdog_maxinterval = 0;
6343 tp_features.fan_ctrl_status_undef = 0;
6344 fan_control_desired_level = 7;
6346 TPACPI_ACPIHANDLE_INIT(fans);
6347 TPACPI_ACPIHANDLE_INIT(gfan);
6348 TPACPI_ACPIHANDLE_INIT(sfan);
6351 /* 570, 600e/x, 770e, 770x */
6352 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6354 /* all other ThinkPads: note that even old-style
6355 * ThinkPad ECs supports the fan control register */
6356 if (likely(acpi_ec_read(fan_status_offset,
6357 &fan_control_initial_status))) {
6358 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6360 /* In some ThinkPads, neither the EC nor the ACPI
6361 * DSDT initialize the fan status, and it ends up
6362 * being set to 0x07 when it *could* be either
6365 * Enable for TP-1Y (T43), TP-78 (R51e),
6366 * TP-76 (R52), TP-70 (T43, R52), which are known
6368 if (fan_control_initial_status == 0x07) {
6369 switch (thinkpad_id.ec_model) {
6370 case 0x5931: /* TP-1Y */
6371 case 0x3837: /* TP-78 */
6372 case 0x3637: /* TP-76 */
6373 case 0x3037: /* TP-70 */
6374 printk(TPACPI_NOTICE
6375 "fan_init: initial fan status "
6376 "is unknown, assuming it is "
6378 tp_features.fan_ctrl_status_undef = 1;
6384 "ThinkPad ACPI EC access misbehaving, "
6385 "fan status and control unavailable\n");
6392 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6393 fan_control_commands |=
6394 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6397 /* gfan without sfan means no fan control */
6398 /* all other models implement TP EC 0x2f control */
6402 fan_control_access_mode =
6403 TPACPI_FAN_WR_ACPI_FANS;
6404 fan_control_commands |=
6405 TPACPI_FAN_CMD_SPEED |
6406 TPACPI_FAN_CMD_LEVEL |
6407 TPACPI_FAN_CMD_ENABLE;
6409 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6410 fan_control_commands |=
6411 TPACPI_FAN_CMD_LEVEL |
6412 TPACPI_FAN_CMD_ENABLE;
6417 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6418 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6419 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6420 fan_status_access_mode, fan_control_access_mode);
6422 /* fan control master switch */
6423 if (!fan_control_allowed) {
6424 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6425 fan_control_commands = 0;
6426 dbg_printk(TPACPI_DBG_INIT,
6427 "fan control features disabled by parameter\n");
6430 /* update fan_control_desired_level */
6431 if (fan_status_access_mode != TPACPI_FAN_NONE)
6432 fan_get_status_safe(NULL);
6434 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6435 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6436 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6441 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6442 &driver_attr_fan_watchdog);
6444 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6453 static void fan_exit(void)
6455 vdbg_printk(TPACPI_DBG_EXIT,
6456 "cancelling any pending fan watchdog tasks\n");
6458 /* FIXME: can we really do this unconditionally? */
6459 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6460 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6461 &driver_attr_fan_watchdog);
6463 cancel_delayed_work(&fan_watchdog_task);
6464 flush_workqueue(tpacpi_wq);
6467 static void fan_suspend(pm_message_t state)
6471 if (!fan_control_allowed)
6474 /* Store fan status in cache */
6475 fan_control_resume_level = 0;
6476 rc = fan_get_status_safe(&fan_control_resume_level);
6478 printk(TPACPI_NOTICE
6479 "failed to read fan level for later "
6480 "restore during resume: %d\n", rc);
6482 /* if it is undefined, don't attempt to restore it.
6484 if (tp_features.fan_ctrl_status_undef)
6485 fan_control_resume_level = 0;
6488 static void fan_resume(void)
6490 u8 current_level = 7;
6491 bool do_set = false;
6494 /* DSDT *always* updates status on resume */
6495 tp_features.fan_ctrl_status_undef = 0;
6497 if (!fan_control_allowed ||
6498 !fan_control_resume_level ||
6499 (fan_get_status_safe(¤t_level) < 0))
6502 switch (fan_control_access_mode) {
6503 case TPACPI_FAN_WR_ACPI_SFAN:
6504 /* never decrease fan level */
6505 do_set = (fan_control_resume_level > current_level);
6507 case TPACPI_FAN_WR_ACPI_FANS:
6508 case TPACPI_FAN_WR_TPEC:
6509 /* never decrease fan level, scale is:
6510 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6512 * We expect the firmware to set either 7 or AUTO, but we
6513 * handle FULLSPEED out of paranoia.
6515 * So, we can safely only restore FULLSPEED or 7, anything
6516 * else could slow the fan. Restoring AUTO is useless, at
6517 * best that's exactly what the DSDT already set (it is the
6520 * Always keep in mind that the DSDT *will* have set the
6521 * fans to what the vendor supposes is the best level. We
6522 * muck with it only to speed the fan up.
6524 if (fan_control_resume_level != 7 &&
6525 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6528 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6529 (current_level != fan_control_resume_level);
6535 printk(TPACPI_NOTICE
6536 "restoring fan level to 0x%02x\n",
6537 fan_control_resume_level);
6538 rc = fan_set_level_safe(fan_control_resume_level);
6540 printk(TPACPI_NOTICE
6541 "failed to restore fan level: %d\n", rc);
6545 static int fan_read(char *p)
6550 unsigned int speed = 0;
6552 switch (fan_status_access_mode) {
6553 case TPACPI_FAN_RD_ACPI_GFAN:
6554 /* 570, 600e/x, 770e, 770x */
6555 rc = fan_get_status_safe(&status);
6559 len += sprintf(p + len, "status:\t\t%s\n"
6561 (status != 0) ? "enabled" : "disabled", status);
6564 case TPACPI_FAN_RD_TPEC:
6565 /* all except 570, 600e/x, 770e, 770x */
6566 rc = fan_get_status_safe(&status);
6570 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6571 if (status != fan_control_initial_status)
6572 tp_features.fan_ctrl_status_undef = 0;
6574 /* Return most likely status. In fact, it
6575 * might be the only possible status */
6576 status = TP_EC_FAN_AUTO;
6579 len += sprintf(p + len, "status:\t\t%s\n",
6580 (status != 0) ? "enabled" : "disabled");
6582 rc = fan_get_speed(&speed);
6586 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6588 if (status & TP_EC_FAN_FULLSPEED)
6589 /* Disengaged mode takes precedence */
6590 len += sprintf(p + len, "level:\t\tdisengaged\n");
6591 else if (status & TP_EC_FAN_AUTO)
6592 len += sprintf(p + len, "level:\t\tauto\n");
6594 len += sprintf(p + len, "level:\t\t%d\n", status);
6597 case TPACPI_FAN_NONE:
6599 len += sprintf(p + len, "status:\t\tnot supported\n");
6602 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6603 len += sprintf(p + len, "commands:\tlevel <level>");
6605 switch (fan_control_access_mode) {
6606 case TPACPI_FAN_WR_ACPI_SFAN:
6607 len += sprintf(p + len, " (<level> is 0-7)\n");
6611 len += sprintf(p + len, " (<level> is 0-7, "
6612 "auto, disengaged, full-speed)\n");
6617 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6618 len += sprintf(p + len, "commands:\tenable, disable\n"
6619 "commands:\twatchdog <timeout> (<timeout> "
6620 "is 0 (off), 1-120 (seconds))\n");
6622 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6623 len += sprintf(p + len, "commands:\tspeed <speed>"
6624 " (<speed> is 0-65535)\n");
6629 static int fan_write_cmd_level(const char *cmd, int *rc)
6633 if (strlencmp(cmd, "level auto") == 0)
6634 level = TP_EC_FAN_AUTO;
6635 else if ((strlencmp(cmd, "level disengaged") == 0) |
6636 (strlencmp(cmd, "level full-speed") == 0))
6637 level = TP_EC_FAN_FULLSPEED;
6638 else if (sscanf(cmd, "level %d", &level) != 1)
6641 *rc = fan_set_level_safe(level);
6643 printk(TPACPI_ERR "level command accepted for unsupported "
6644 "access mode %d", fan_control_access_mode);
6649 static int fan_write_cmd_enable(const char *cmd, int *rc)
6651 if (strlencmp(cmd, "enable") != 0)
6654 *rc = fan_set_enable();
6656 printk(TPACPI_ERR "enable command accepted for unsupported "
6657 "access mode %d", fan_control_access_mode);
6662 static int fan_write_cmd_disable(const char *cmd, int *rc)
6664 if (strlencmp(cmd, "disable") != 0)
6667 *rc = fan_set_disable();
6669 printk(TPACPI_ERR "disable command accepted for unsupported "
6670 "access mode %d", fan_control_access_mode);
6675 static int fan_write_cmd_speed(const char *cmd, int *rc)
6680 * Support speed <low> <medium> <high> ? */
6682 if (sscanf(cmd, "speed %d", &speed) != 1)
6685 *rc = fan_set_speed(speed);
6687 printk(TPACPI_ERR "speed command accepted for unsupported "
6688 "access mode %d", fan_control_access_mode);
6693 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6697 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6700 if (interval < 0 || interval > 120)
6703 fan_watchdog_maxinterval = interval;
6708 static int fan_write(char *buf)
6713 while (!rc && (cmd = next_cmd(&buf))) {
6714 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6715 fan_write_cmd_level(cmd, &rc)) &&
6716 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6717 (fan_write_cmd_enable(cmd, &rc) ||
6718 fan_write_cmd_disable(cmd, &rc) ||
6719 fan_write_cmd_watchdog(cmd, &rc))) &&
6720 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6721 fan_write_cmd_speed(cmd, &rc))
6725 fan_watchdog_reset();
6731 static struct ibm_struct fan_driver_data = {
6736 .suspend = fan_suspend,
6737 .resume = fan_resume,
6740 /****************************************************************************
6741 ****************************************************************************
6745 ****************************************************************************
6746 ****************************************************************************/
6748 /* sysfs name ---------------------------------------------------------- */
6749 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6750 struct device_attribute *attr,
6753 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6756 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6757 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6759 /* --------------------------------------------------------------------- */
6762 static struct proc_dir_entry *proc_dir;
6765 * Module and infrastructure proble, init and exit handling
6768 static int force_load;
6770 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6771 static const char * __init str_supported(int is_supported)
6773 static char text_unsupported[] __initdata = "not supported";
6775 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6777 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6779 static void ibm_exit(struct ibm_struct *ibm)
6781 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6783 list_del_init(&ibm->all_drivers);
6785 if (ibm->flags.acpi_notify_installed) {
6786 dbg_printk(TPACPI_DBG_EXIT,
6787 "%s: acpi_remove_notify_handler\n", ibm->name);
6789 acpi_remove_notify_handler(*ibm->acpi->handle,
6791 dispatch_acpi_notify);
6792 ibm->flags.acpi_notify_installed = 0;
6793 ibm->flags.acpi_notify_installed = 0;
6796 if (ibm->flags.proc_created) {
6797 dbg_printk(TPACPI_DBG_EXIT,
6798 "%s: remove_proc_entry\n", ibm->name);
6799 remove_proc_entry(ibm->name, proc_dir);
6800 ibm->flags.proc_created = 0;
6803 if (ibm->flags.acpi_driver_registered) {
6804 dbg_printk(TPACPI_DBG_EXIT,
6805 "%s: acpi_bus_unregister_driver\n", ibm->name);
6807 acpi_bus_unregister_driver(ibm->acpi->driver);
6808 kfree(ibm->acpi->driver);
6809 ibm->acpi->driver = NULL;
6810 ibm->flags.acpi_driver_registered = 0;
6813 if (ibm->flags.init_called && ibm->exit) {
6815 ibm->flags.init_called = 0;
6818 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6821 static int __init ibm_init(struct ibm_init_struct *iibm)
6824 struct ibm_struct *ibm = iibm->data;
6825 struct proc_dir_entry *entry;
6827 BUG_ON(ibm == NULL);
6829 INIT_LIST_HEAD(&ibm->all_drivers);
6831 if (ibm->flags.experimental && !experimental)
6834 dbg_printk(TPACPI_DBG_INIT,
6835 "probing for %s\n", ibm->name);
6838 ret = iibm->init(iibm);
6840 return 0; /* probe failed */
6844 ibm->flags.init_called = 1;
6848 if (ibm->acpi->hid) {
6849 ret = register_tpacpi_subdriver(ibm);
6854 if (ibm->acpi->notify) {
6855 ret = setup_acpi_notify(ibm);
6856 if (ret == -ENODEV) {
6857 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6867 dbg_printk(TPACPI_DBG_INIT,
6868 "%s installed\n", ibm->name);
6871 entry = create_proc_entry(ibm->name,
6872 S_IFREG | S_IRUGO | S_IWUSR,
6875 printk(TPACPI_ERR "unable to create proc entry %s\n",
6880 entry->owner = THIS_MODULE;
6882 entry->read_proc = &dispatch_procfs_read;
6884 entry->write_proc = &dispatch_procfs_write;
6885 ibm->flags.proc_created = 1;
6888 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6893 dbg_printk(TPACPI_DBG_INIT,
6894 "%s: at error exit path with result %d\n",
6898 return (ret < 0)? ret : 0;
6903 /* returns 0 - probe ok, or < 0 - probe error.
6904 * Probe ok doesn't mean thinkpad found.
6905 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6906 static int __must_check __init get_thinkpad_model_data(
6907 struct thinkpad_id_data *tp)
6909 const struct dmi_device *dev = NULL;
6910 char ec_fw_string[18];
6916 memset(tp, 0, sizeof(*tp));
6918 if (dmi_name_in_vendors("IBM"))
6919 tp->vendor = PCI_VENDOR_ID_IBM;
6920 else if (dmi_name_in_vendors("LENOVO"))
6921 tp->vendor = PCI_VENDOR_ID_LENOVO;
6925 s = dmi_get_system_info(DMI_BIOS_VERSION);
6926 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6927 if (s && !tp->bios_version_str)
6929 if (!tp->bios_version_str)
6931 tp->bios_model = tp->bios_version_str[0]
6932 | (tp->bios_version_str[1] << 8);
6935 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6936 * X32 or newer, all Z series; Some models must have an
6937 * up-to-date BIOS or they will not be detected.
6939 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6941 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6942 if (sscanf(dev->name,
6943 "IBM ThinkPad Embedded Controller -[%17c",
6944 ec_fw_string) == 1) {
6945 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6946 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6948 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6949 if (!tp->ec_version_str)
6951 tp->ec_model = ec_fw_string[0]
6952 | (ec_fw_string[1] << 8);
6957 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6958 if (s && !strnicmp(s, "ThinkPad", 8)) {
6959 tp->model_str = kstrdup(s, GFP_KERNEL);
6964 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6965 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6966 if (s && !tp->nummodel_str)
6972 static int __init probe_for_thinkpad(void)
6980 * Non-ancient models have better DMI tagging, but very old models
6983 is_thinkpad = (thinkpad_id.model_str != NULL);
6985 /* ec is required because many other handles are relative to it */
6986 TPACPI_ACPIHANDLE_INIT(ec);
6990 "Not yet supported ThinkPad detected!\n");
6995 * Risks a regression on very old machines, but reduces potential
6996 * false positives a damn great deal
6999 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7001 if (!is_thinkpad && !force_load)
7008 /* Module init, exit, parameters */
7010 static struct ibm_init_struct ibms_init[] __initdata = {
7012 .init = thinkpad_acpi_driver_init,
7013 .data = &thinkpad_acpi_driver_data,
7016 .init = hotkey_init,
7017 .data = &hotkey_driver_data,
7020 .init = bluetooth_init,
7021 .data = &bluetooth_driver_data,
7025 .data = &wan_driver_data,
7029 .data = &uwb_driver_data,
7031 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7034 .data = &video_driver_data,
7039 .data = &light_driver_data,
7041 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7044 .data = &dock_driver_data[0],
7048 .data = &dock_driver_data[1],
7051 #ifdef CONFIG_THINKPAD_ACPI_BAY
7054 .data = &bay_driver_data,
7059 .data = &cmos_driver_data,
7063 .data = &led_driver_data,
7067 .data = &beep_driver_data,
7070 .init = thermal_init,
7071 .data = &thermal_driver_data,
7074 .data = &ecdump_driver_data,
7077 .init = brightness_init,
7078 .data = &brightness_driver_data,
7081 .data = &volume_driver_data,
7085 .data = &fan_driver_data,
7089 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7092 struct ibm_struct *ibm;
7094 if (!kp || !kp->name || !val)
7097 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7098 ibm = ibms_init[i].data;
7099 WARN_ON(ibm == NULL);
7101 if (!ibm || !ibm->name)
7104 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7105 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7107 strcpy(ibms_init[i].param, val);
7108 strcat(ibms_init[i].param, ",");
7116 module_param(experimental, int, 0);
7117 MODULE_PARM_DESC(experimental,
7118 "Enables experimental features when non-zero");
7120 module_param_named(debug, dbg_level, uint, 0);
7121 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7123 module_param(force_load, bool, 0);
7124 MODULE_PARM_DESC(force_load,
7125 "Attempts to load the driver even on a "
7126 "mis-identified ThinkPad when true");
7128 module_param_named(fan_control, fan_control_allowed, bool, 0);
7129 MODULE_PARM_DESC(fan_control,
7130 "Enables setting fan parameters features when true");
7132 module_param_named(brightness_mode, brightness_mode, int, 0);
7133 MODULE_PARM_DESC(brightness_mode,
7134 "Selects brightness control strategy: "
7135 "0=auto, 1=EC, 2=CMOS, 3=both");
7137 module_param(brightness_enable, uint, 0);
7138 MODULE_PARM_DESC(brightness_enable,
7139 "Enables backlight control when 1, disables when 0");
7141 module_param(hotkey_report_mode, uint, 0);
7142 MODULE_PARM_DESC(hotkey_report_mode,
7143 "used for backwards compatibility with userspace, "
7144 "see documentation");
7146 #define TPACPI_PARAM(feature) \
7147 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7148 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7149 "at module load, see documentation")
7151 TPACPI_PARAM(hotkey);
7152 TPACPI_PARAM(bluetooth);
7153 TPACPI_PARAM(video);
7154 TPACPI_PARAM(light);
7155 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7158 #ifdef CONFIG_THINKPAD_ACPI_BAY
7160 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7164 TPACPI_PARAM(ecdump);
7165 TPACPI_PARAM(brightness);
7166 TPACPI_PARAM(volume);
7169 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7170 module_param(dbg_wlswemul, uint, 0);
7171 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7172 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7173 MODULE_PARM_DESC(wlsw_state,
7174 "Initial state of the emulated WLSW switch");
7176 module_param(dbg_bluetoothemul, uint, 0);
7177 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7178 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7179 MODULE_PARM_DESC(bluetooth_state,
7180 "Initial state of the emulated bluetooth switch");
7182 module_param(dbg_wwanemul, uint, 0);
7183 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7184 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7185 MODULE_PARM_DESC(wwan_state,
7186 "Initial state of the emulated WWAN switch");
7188 module_param(dbg_uwbemul, uint, 0);
7189 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7190 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7191 MODULE_PARM_DESC(uwb_state,
7192 "Initial state of the emulated UWB switch");
7195 static void thinkpad_acpi_module_exit(void)
7197 struct ibm_struct *ibm, *itmp;
7199 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7201 list_for_each_entry_safe_reverse(ibm, itmp,
7202 &tpacpi_all_drivers,
7207 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7209 if (tpacpi_inputdev) {
7210 if (tp_features.input_device_registered)
7211 input_unregister_device(tpacpi_inputdev);
7213 input_free_device(tpacpi_inputdev);
7217 hwmon_device_unregister(tpacpi_hwmon);
7219 if (tp_features.sensors_pdev_attrs_registered)
7220 device_remove_file(&tpacpi_sensors_pdev->dev,
7221 &dev_attr_thinkpad_acpi_pdev_name);
7222 if (tpacpi_sensors_pdev)
7223 platform_device_unregister(tpacpi_sensors_pdev);
7225 platform_device_unregister(tpacpi_pdev);
7227 if (tp_features.sensors_pdrv_attrs_registered)
7228 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7229 if (tp_features.platform_drv_attrs_registered)
7230 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7232 if (tp_features.sensors_pdrv_registered)
7233 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7235 if (tp_features.platform_drv_registered)
7236 platform_driver_unregister(&tpacpi_pdriver);
7239 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7242 destroy_workqueue(tpacpi_wq);
7244 kfree(thinkpad_id.bios_version_str);
7245 kfree(thinkpad_id.ec_version_str);
7246 kfree(thinkpad_id.model_str);
7250 static int __init thinkpad_acpi_module_init(void)
7254 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7256 /* Parameter checking */
7257 if (hotkey_report_mode > 2)
7260 /* Driver-level probe */
7262 ret = get_thinkpad_model_data(&thinkpad_id);
7265 "unable to get DMI data: %d\n", ret);
7266 thinkpad_acpi_module_exit();
7269 ret = probe_for_thinkpad();
7271 thinkpad_acpi_module_exit();
7275 /* Driver initialization */
7277 TPACPI_ACPIHANDLE_INIT(ecrd);
7278 TPACPI_ACPIHANDLE_INIT(ecwr);
7280 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7282 thinkpad_acpi_module_exit();
7286 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7289 "unable to create proc dir " TPACPI_PROC_DIR);
7290 thinkpad_acpi_module_exit();
7293 proc_dir->owner = THIS_MODULE;
7295 ret = platform_driver_register(&tpacpi_pdriver);
7298 "unable to register main platform driver\n");
7299 thinkpad_acpi_module_exit();
7302 tp_features.platform_drv_registered = 1;
7304 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7307 "unable to register hwmon platform driver\n");
7308 thinkpad_acpi_module_exit();
7311 tp_features.sensors_pdrv_registered = 1;
7313 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7315 tp_features.platform_drv_attrs_registered = 1;
7316 ret = tpacpi_create_driver_attributes(
7317 &tpacpi_hwmon_pdriver.driver);
7321 "unable to create sysfs driver attributes\n");
7322 thinkpad_acpi_module_exit();
7325 tp_features.sensors_pdrv_attrs_registered = 1;
7328 /* Device initialization */
7329 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7331 if (IS_ERR(tpacpi_pdev)) {
7332 ret = PTR_ERR(tpacpi_pdev);
7334 printk(TPACPI_ERR "unable to register platform device\n");
7335 thinkpad_acpi_module_exit();
7338 tpacpi_sensors_pdev = platform_device_register_simple(
7339 TPACPI_HWMON_DRVR_NAME,
7341 if (IS_ERR(tpacpi_sensors_pdev)) {
7342 ret = PTR_ERR(tpacpi_sensors_pdev);
7343 tpacpi_sensors_pdev = NULL;
7345 "unable to register hwmon platform device\n");
7346 thinkpad_acpi_module_exit();
7349 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7350 &dev_attr_thinkpad_acpi_pdev_name);
7353 "unable to create sysfs hwmon device attributes\n");
7354 thinkpad_acpi_module_exit();
7357 tp_features.sensors_pdev_attrs_registered = 1;
7358 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7359 if (IS_ERR(tpacpi_hwmon)) {
7360 ret = PTR_ERR(tpacpi_hwmon);
7361 tpacpi_hwmon = NULL;
7362 printk(TPACPI_ERR "unable to register hwmon device\n");
7363 thinkpad_acpi_module_exit();
7366 mutex_init(&tpacpi_inputdev_send_mutex);
7367 tpacpi_inputdev = input_allocate_device();
7368 if (!tpacpi_inputdev) {
7369 printk(TPACPI_ERR "unable to allocate input device\n");
7370 thinkpad_acpi_module_exit();
7373 /* Prepare input device, but don't register */
7374 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7375 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7376 tpacpi_inputdev->id.bustype = BUS_HOST;
7377 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7378 thinkpad_id.vendor :
7380 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7381 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7383 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7384 ret = ibm_init(&ibms_init[i]);
7385 if (ret >= 0 && *ibms_init[i].param)
7386 ret = ibms_init[i].data->write(ibms_init[i].param);
7388 thinkpad_acpi_module_exit();
7392 ret = input_register_device(tpacpi_inputdev);
7394 printk(TPACPI_ERR "unable to register input device\n");
7395 thinkpad_acpi_module_exit();
7398 tp_features.input_device_registered = 1;
7401 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7405 /* Please remove this in year 2009 */
7406 MODULE_ALIAS("ibm_acpi");
7408 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7411 * DMI matching for module autoloading
7413 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7414 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7416 * Only models listed in thinkwiki will be supported, so add yours
7417 * if it is not there yet.
7419 #define IBM_BIOS_MODULE_ALIAS(__type) \
7420 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7422 /* Non-ancient thinkpads */
7423 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7424 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7426 /* Ancient thinkpad BIOSes have to be identified by
7427 * BIOS type or model number, and there are far less
7428 * BIOS types than model numbers... */
7429 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7430 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7431 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7433 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7434 MODULE_DESCRIPTION(TPACPI_DESC);
7435 MODULE_VERSION(TPACPI_VERSION);
7436 MODULE_LICENSE("GPL");
7438 module_init(thinkpad_acpi_module_init);
7439 module_exit(thinkpad_acpi_module_exit);