2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 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.23"
25 #define TPACPI_SYSFS_VERSION 0x020300
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/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
170 #define TPACPI_LOG TPACPI_FILE ": "
171 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174 #define TPACPI_ERR KERN_ERR TPACPI_LOG
175 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
176 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177 #define TPACPI_INFO KERN_INFO TPACPI_LOG
178 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
180 /* Debugging printk groups */
181 #define TPACPI_DBG_ALL 0xffff
182 #define TPACPI_DBG_DISCLOSETASK 0x8000
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define TPACPI_DBG_RFKILL 0x0004
186 #define TPACPI_DBG_HKEY 0x0008
187 #define TPACPI_DBG_FAN 0x0010
188 #define TPACPI_DBG_BRGHT 0x0020
190 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
195 /****************************************************************************
196 * Driver-wide structs and misc. variables
201 struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
205 void (*notify) (struct ibm_struct *, u32);
208 struct acpi_device *device;
214 int (*read) (char *);
215 int (*write) (char *);
217 void (*resume) (void);
218 void (*suspend) (pm_message_t state);
219 void (*shutdown) (void);
221 struct list_head all_drivers;
223 struct tp_acpi_drv_struct *acpi;
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
234 struct ibm_init_struct {
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
242 #ifdef CONFIG_THINKPAD_ACPI_BAY
255 u32 bright_16levels:1;
256 u32 bright_acpimode:1;
259 u32 fan_ctrl_status_undef:1;
260 u32 input_device_registered:1;
261 u32 platform_drv_registered:1;
262 u32 platform_drv_attrs_registered:1;
263 u32 sensors_pdrv_registered:1;
264 u32 sensors_pdrv_attrs_registered:1;
265 u32 sensors_pdev_attrs_registered:1;
266 u32 hotkey_poll_active:1;
270 u16 hotkey_mask_ff:1;
273 struct thinkpad_id_data {
274 unsigned int vendor; /* ThinkPad vendor:
275 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
277 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
278 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
280 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
283 char *model_str; /* ThinkPad T43 */
284 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
286 static struct thinkpad_id_data thinkpad_id;
289 TPACPI_LIFE_INIT = 0,
294 static int experimental;
295 static u32 dbg_level;
297 static struct workqueue_struct *tpacpi_wq;
305 /* Special LED class that can defer work */
306 struct tpacpi_led_classdev {
307 struct led_classdev led_classdev;
308 struct work_struct work;
309 enum led_status_t new_state;
313 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
314 static int dbg_wlswemul;
315 static int tpacpi_wlsw_emulstate;
316 static int dbg_bluetoothemul;
317 static int tpacpi_bluetooth_emulstate;
318 static int dbg_wwanemul;
319 static int tpacpi_wwan_emulstate;
320 static int dbg_uwbemul;
321 static int tpacpi_uwb_emulstate;
325 /*************************************************************************
329 #define dbg_printk(a_dbg_level, format, arg...) \
330 do { if (dbg_level & (a_dbg_level)) \
331 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
334 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
335 #define vdbg_printk dbg_printk
336 static const char *str_supported(int is_supported);
338 #define vdbg_printk(a_dbg_level, format, arg...) \
342 static void tpacpi_log_usertask(const char * const what)
344 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
345 what, task_tgid_vnr(current));
348 #define tpacpi_disclose_usertask(what, format, arg...) \
351 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
352 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
353 printk(TPACPI_DEBUG "%s: PID %d: " format, \
354 what, task_tgid_vnr(current), ## arg); \
358 /****************************************************************************
359 ****************************************************************************
361 * ACPI Helpers and device model
363 ****************************************************************************
364 ****************************************************************************/
366 /*************************************************************************
370 static acpi_handle root_handle;
372 #define TPACPI_HANDLE(object, parent, paths...) \
373 static acpi_handle object##_handle; \
374 static acpi_handle *object##_parent = &parent##_handle; \
375 static char *object##_path; \
376 static char *object##_paths[] = { paths }
378 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
379 "\\_SB.PCI.ISA.EC", /* 570 */
380 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
381 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
382 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
383 "\\_SB.PCI0.ICH3.EC0", /* R31 */
384 "\\_SB.PCI0.LPC.EC", /* all others */
387 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
388 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
390 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
392 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
393 "\\CMS", /* R40, R40e */
396 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
397 "^HKEY", /* R30, R31 */
398 "HKEY", /* all others */
401 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
402 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
403 "\\_SB.PCI0.VID0", /* 770e */
404 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
405 "\\_SB.PCI0.AGP.VID", /* all others */
409 /*************************************************************************
413 static int acpi_evalf(acpi_handle handle,
414 void *res, char *method, char *fmt, ...)
417 struct acpi_object_list params;
418 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
419 struct acpi_buffer result, *resultp;
420 union acpi_object out_obj;
428 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
441 params.pointer = &in_objs[0];
448 in_objs[params.count].integer.value = va_arg(ap, int);
449 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
451 /* add more types as needed */
453 printk(TPACPI_ERR "acpi_evalf() called "
454 "with invalid format character '%c'\n", c);
460 if (res_type != 'v') {
461 result.length = sizeof(out_obj);
462 result.pointer = &out_obj;
467 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
472 *(int *)res = out_obj.integer.value;
473 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
476 success = status == AE_OK;
478 /* add more types as needed */
480 printk(TPACPI_ERR "acpi_evalf() called "
481 "with invalid format character '%c'\n", res_type);
485 if (!success && !quiet)
486 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
487 method, fmt0, status);
492 static int acpi_ec_read(int i, u8 *p)
497 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
501 if (ec_read(i, p) < 0)
508 static int acpi_ec_write(int i, u8 v)
511 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
514 if (ec_write(i, v) < 0)
521 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
522 static int _sta(acpi_handle handle)
526 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
533 static int issue_thinkpad_cmos_command(int cmos_cmd)
538 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
544 /*************************************************************************
548 #define TPACPI_ACPIHANDLE_INIT(object) \
549 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
550 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
552 static void drv_acpi_handle_init(char *name,
553 acpi_handle *handle, acpi_handle parent,
554 char **paths, int num_paths, char **path)
559 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
562 for (i = 0; i < num_paths; i++) {
563 status = acpi_get_handle(parent, paths[i], handle);
564 if (ACPI_SUCCESS(status)) {
566 dbg_printk(TPACPI_DBG_INIT,
567 "Found ACPI handle %s for %s\n",
573 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
578 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
580 struct ibm_struct *ibm = data;
582 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
585 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
588 ibm->acpi->notify(ibm, event);
591 static int __init setup_acpi_notify(struct ibm_struct *ibm)
598 if (!*ibm->acpi->handle)
601 vdbg_printk(TPACPI_DBG_INIT,
602 "setting up ACPI notify for %s\n", ibm->name);
604 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
606 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
611 ibm->acpi->device->driver_data = ibm;
612 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
613 TPACPI_ACPI_EVENT_PREFIX,
616 status = acpi_install_notify_handler(*ibm->acpi->handle,
617 ibm->acpi->type, dispatch_acpi_notify, ibm);
618 if (ACPI_FAILURE(status)) {
619 if (status == AE_ALREADY_EXISTS) {
621 "another device driver is already "
622 "handling %s events\n", ibm->name);
625 "acpi_install_notify_handler(%s) failed: %d\n",
630 ibm->flags.acpi_notify_installed = 1;
634 static int __init tpacpi_device_add(struct acpi_device *device)
639 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
643 dbg_printk(TPACPI_DBG_INIT,
644 "registering %s as an ACPI driver\n", ibm->name);
648 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
649 if (!ibm->acpi->driver) {
651 "failed to allocate memory for ibm->acpi->driver\n");
655 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
656 ibm->acpi->driver->ids = ibm->acpi->hid;
658 ibm->acpi->driver->ops.add = &tpacpi_device_add;
660 rc = acpi_bus_register_driver(ibm->acpi->driver);
662 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
664 kfree(ibm->acpi->driver);
665 ibm->acpi->driver = NULL;
667 ibm->flags.acpi_driver_registered = 1;
673 /****************************************************************************
674 ****************************************************************************
678 ****************************************************************************
679 ****************************************************************************/
681 static int dispatch_procfs_read(char *page, char **start, off_t off,
682 int count, int *eof, void *data)
684 struct ibm_struct *ibm = data;
687 if (!ibm || !ibm->read)
690 len = ibm->read(page);
694 if (len <= off + count)
706 static int dispatch_procfs_write(struct file *file,
707 const char __user *userbuf,
708 unsigned long count, void *data)
710 struct ibm_struct *ibm = data;
714 if (!ibm || !ibm->write)
717 kernbuf = kmalloc(count + 2, GFP_KERNEL);
721 if (copy_from_user(kernbuf, userbuf, count)) {
727 strcat(kernbuf, ",");
728 ret = ibm->write(kernbuf);
737 static char *next_cmd(char **cmds)
742 while ((end = strchr(start, ',')) && end == start)
754 /****************************************************************************
755 ****************************************************************************
757 * Device model: input, hwmon and platform
759 ****************************************************************************
760 ****************************************************************************/
762 static struct platform_device *tpacpi_pdev;
763 static struct platform_device *tpacpi_sensors_pdev;
764 static struct device *tpacpi_hwmon;
765 static struct input_dev *tpacpi_inputdev;
766 static struct mutex tpacpi_inputdev_send_mutex;
767 static LIST_HEAD(tpacpi_all_drivers);
769 static int tpacpi_suspend_handler(struct platform_device *pdev,
772 struct ibm_struct *ibm, *itmp;
774 list_for_each_entry_safe(ibm, itmp,
778 (ibm->suspend)(state);
784 static int tpacpi_resume_handler(struct platform_device *pdev)
786 struct ibm_struct *ibm, *itmp;
788 list_for_each_entry_safe(ibm, itmp,
798 static void tpacpi_shutdown_handler(struct platform_device *pdev)
800 struct ibm_struct *ibm, *itmp;
802 list_for_each_entry_safe(ibm, itmp,
810 static struct platform_driver tpacpi_pdriver = {
812 .name = TPACPI_DRVR_NAME,
813 .owner = THIS_MODULE,
815 .suspend = tpacpi_suspend_handler,
816 .resume = tpacpi_resume_handler,
817 .shutdown = tpacpi_shutdown_handler,
820 static struct platform_driver tpacpi_hwmon_pdriver = {
822 .name = TPACPI_HWMON_DRVR_NAME,
823 .owner = THIS_MODULE,
827 /*************************************************************************
828 * sysfs support helpers
831 struct attribute_set {
832 unsigned int members, max_members;
833 struct attribute_group group;
836 struct attribute_set_obj {
837 struct attribute_set s;
839 } __attribute__((packed));
841 static struct attribute_set *create_attr_set(unsigned int max_members,
844 struct attribute_set_obj *sobj;
846 if (max_members == 0)
849 /* Allocates space for implicit NULL at the end too */
850 sobj = kzalloc(sizeof(struct attribute_set_obj) +
851 max_members * sizeof(struct attribute *),
855 sobj->s.max_members = max_members;
856 sobj->s.group.attrs = &sobj->a;
857 sobj->s.group.name = name;
862 #define destroy_attr_set(_set) \
865 /* not multi-threaded safe, use it in a single thread per set */
866 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
871 if (s->members >= s->max_members)
874 s->group.attrs[s->members] = attr;
880 static int add_many_to_attr_set(struct attribute_set *s,
881 struct attribute **attr,
886 for (i = 0; i < count; i++) {
887 res = add_to_attr_set(s, attr[i]);
895 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
897 sysfs_remove_group(kobj, &s->group);
901 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
902 sysfs_create_group(_kobj, &_attr_set->group)
904 static int parse_strtoul(const char *buf,
905 unsigned long max, unsigned long *value)
909 while (*buf && isspace(*buf))
911 *value = simple_strtoul(buf, &endp, 0);
912 while (*endp && isspace(*endp))
914 if (*endp || *value > max)
920 static void tpacpi_disable_brightness_delay(void)
922 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
924 "ACPI backlight control delay disabled\n");
927 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
929 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
930 union acpi_object *obj;
933 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
934 obj = (union acpi_object *)buffer.pointer;
935 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
936 printk(TPACPI_ERR "Unknown _BCL data, "
937 "please report this to %s\n", TPACPI_MAIL);
940 rc = obj->package.count;
946 kfree(buffer.pointer);
950 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
951 u32 lvl, void *context, void **rv)
953 char name[ACPI_PATH_SEGMENT_LENGTH];
954 struct acpi_buffer buffer = { sizeof(name), &name };
956 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
957 !strncmp("_BCL", name, sizeof(name) - 1)) {
959 **(int **)rv = tpacpi_query_bcl_levels(handle);
960 return AE_CTRL_TERMINATE;
967 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
969 static int __init tpacpi_check_std_acpi_brightness_support(void)
973 void *bcl_ptr = &bcl_levels;
976 TPACPI_ACPIHANDLE_INIT(vid);
982 * Search for a _BCL method, and execute it. This is safe on all
983 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
984 * BIOS in ACPI backlight control mode. We do NOT have to care
985 * about calling the _BCL method in an enabled video device, any
986 * will do for our purposes.
989 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
990 tpacpi_acpi_walk_find_bcl, NULL,
993 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
994 tp_features.bright_acpimode = 1;
995 return (bcl_levels - 2);
1001 static void printk_deprecated_attribute(const char * const what,
1002 const char * const details)
1004 tpacpi_log_usertask("deprecated sysfs attribute");
1005 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1006 "will be removed. %s\n",
1010 /*************************************************************************
1011 * rfkill and radio control support helpers
1015 * ThinkPad-ACPI firmware handling model:
1017 * WLSW (master wireless switch) is event-driven, and is common to all
1018 * firmware-controlled radios. It cannot be controlled, just monitored,
1019 * as expected. It overrides all radio state in firmware
1021 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1022 * (TODO: verify how WLSW interacts with the returned radio state).
1024 * The only time there are shadow radio state changes, is when
1025 * masked-off hotkeys are used.
1029 * Internal driver API for radio state:
1031 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1032 * bool: true means radio blocked (off)
1034 enum tpacpi_rfkill_state {
1035 TPACPI_RFK_RADIO_OFF = 0,
1039 /* rfkill switches */
1040 enum tpacpi_rfk_id {
1041 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1042 TPACPI_RFK_WWAN_SW_ID,
1043 TPACPI_RFK_UWB_SW_ID,
1047 static const char *tpacpi_rfkill_names[] = {
1048 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1049 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1050 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1051 [TPACPI_RFK_SW_MAX] = NULL
1054 /* ThinkPad-ACPI rfkill subdriver */
1056 struct rfkill *rfkill;
1057 enum tpacpi_rfk_id id;
1058 const struct tpacpi_rfk_ops *ops;
1061 struct tpacpi_rfk_ops {
1062 /* firmware interface */
1063 int (*get_status)(void);
1064 int (*set_status)(const enum tpacpi_rfkill_state);
1067 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1069 /* Query FW and update rfkill sw state for a given rfkill switch */
1070 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1077 status = (tp_rfk->ops->get_status)();
1081 rfkill_set_sw_state(tp_rfk->rfkill,
1082 (status == TPACPI_RFK_RADIO_OFF));
1087 /* Query FW and update rfkill sw state for all rfkill switches */
1088 static void tpacpi_rfk_update_swstate_all(void)
1092 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1093 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1097 * Sync the HW-blocking state of all rfkill switches,
1098 * do notice it causes the rfkill core to schedule uevents
1100 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1103 struct tpacpi_rfk *tp_rfk;
1105 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1106 tp_rfk = tpacpi_rfkill_switches[i];
1108 if (rfkill_set_hw_state(tp_rfk->rfkill,
1110 /* ignore -- we track sw block */
1116 /* Call to get the WLSW state from the firmware */
1117 static int hotkey_get_wlsw(void);
1119 /* Call to query WLSW state and update all rfkill switches */
1120 static bool tpacpi_rfk_check_hwblock_state(void)
1122 int res = hotkey_get_wlsw();
1125 /* When unknown or unsupported, we have to assume it is unblocked */
1129 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1130 tpacpi_rfk_update_hwblock_state(hw_blocked);
1135 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1137 struct tpacpi_rfk *tp_rfk = data;
1140 dbg_printk(TPACPI_DBG_RFKILL,
1141 "request to change radio state to %s\n",
1142 blocked ? "blocked" : "unblocked");
1144 /* try to set radio state */
1145 res = (tp_rfk->ops->set_status)(blocked ?
1146 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1148 /* and update the rfkill core with whatever the FW really did */
1149 tpacpi_rfk_update_swstate(tp_rfk);
1151 return (res < 0) ? res : 0;
1154 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1155 .set_block = tpacpi_rfk_hook_set_block,
1158 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1159 const struct tpacpi_rfk_ops *tp_rfkops,
1160 const enum rfkill_type rfktype,
1162 const bool set_default)
1164 struct tpacpi_rfk *atp_rfk;
1166 bool sw_state = false;
1169 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1171 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1173 atp_rfk->rfkill = rfkill_alloc(name,
1176 &tpacpi_rfk_rfkill_ops,
1178 if (!atp_rfk || !atp_rfk->rfkill) {
1180 "failed to allocate memory for rfkill class\n");
1186 atp_rfk->ops = tp_rfkops;
1188 sw_status = (tp_rfkops->get_status)();
1189 if (sw_status < 0) {
1191 "failed to read initial state for %s, error %d\n",
1194 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1196 /* try to keep the initial state, since we ask the
1197 * firmware to preserve it across S5 in NVRAM */
1198 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1201 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
1203 res = rfkill_register(atp_rfk->rfkill);
1206 "failed to register %s rfkill switch: %d\n",
1208 rfkill_destroy(atp_rfk->rfkill);
1213 tpacpi_rfkill_switches[id] = atp_rfk;
1217 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1219 struct tpacpi_rfk *tp_rfk;
1221 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1223 tp_rfk = tpacpi_rfkill_switches[id];
1225 rfkill_unregister(tp_rfk->rfkill);
1226 tpacpi_rfkill_switches[id] = NULL;
1231 static void printk_deprecated_rfkill_attribute(const char * const what)
1233 printk_deprecated_attribute(what,
1234 "Please switch to generic rfkill before year 2010");
1237 /* sysfs <radio> enable ------------------------------------------------ */
1238 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1239 struct device_attribute *attr,
1244 printk_deprecated_rfkill_attribute(attr->attr.name);
1246 /* This is in the ABI... */
1247 if (tpacpi_rfk_check_hwblock_state()) {
1248 status = TPACPI_RFK_RADIO_OFF;
1250 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1255 return snprintf(buf, PAGE_SIZE, "%d\n",
1256 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1259 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1266 printk_deprecated_rfkill_attribute(attr->attr.name);
1268 if (parse_strtoul(buf, 1, &t))
1271 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1273 /* This is in the ABI... */
1274 if (tpacpi_rfk_check_hwblock_state() && !!t)
1277 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1278 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1279 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1281 return (res < 0) ? res : count;
1284 /* procfs -------------------------------------------------------------- */
1285 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1289 if (id >= TPACPI_RFK_SW_MAX)
1290 len += sprintf(p + len, "status:\t\tnot supported\n");
1294 /* This is in the ABI... */
1295 if (tpacpi_rfk_check_hwblock_state()) {
1296 status = TPACPI_RFK_RADIO_OFF;
1298 status = tpacpi_rfk_update_swstate(
1299 tpacpi_rfkill_switches[id]);
1304 len += sprintf(p + len, "status:\t\t%s\n",
1305 (status == TPACPI_RFK_RADIO_ON) ?
1306 "enabled" : "disabled");
1307 len += sprintf(p + len, "commands:\tenable, disable\n");
1313 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1319 if (id >= TPACPI_RFK_SW_MAX)
1322 while ((cmd = next_cmd(&buf))) {
1323 if (strlencmp(cmd, "enable") == 0)
1324 status = TPACPI_RFK_RADIO_ON;
1325 else if (strlencmp(cmd, "disable") == 0)
1326 status = TPACPI_RFK_RADIO_OFF;
1332 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1333 (status == TPACPI_RFK_RADIO_ON) ?
1334 "enable" : "disable",
1335 tpacpi_rfkill_names[id]);
1336 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1337 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1343 /*************************************************************************
1344 * thinkpad-acpi driver attributes
1347 /* interface_version --------------------------------------------------- */
1348 static ssize_t tpacpi_driver_interface_version_show(
1349 struct device_driver *drv,
1352 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1355 static DRIVER_ATTR(interface_version, S_IRUGO,
1356 tpacpi_driver_interface_version_show, NULL);
1358 /* debug_level --------------------------------------------------------- */
1359 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1362 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1365 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1366 const char *buf, size_t count)
1370 if (parse_strtoul(buf, 0xffff, &t))
1378 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1379 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1381 /* version ------------------------------------------------------------- */
1382 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1385 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1386 TPACPI_DESC, TPACPI_VERSION);
1389 static DRIVER_ATTR(version, S_IRUGO,
1390 tpacpi_driver_version_show, NULL);
1392 /* --------------------------------------------------------------------- */
1394 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1396 /* wlsw_emulstate ------------------------------------------------------ */
1397 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1400 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1403 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1404 const char *buf, size_t count)
1408 if (parse_strtoul(buf, 1, &t))
1411 if (tpacpi_wlsw_emulstate != !!t) {
1412 tpacpi_wlsw_emulstate = !!t;
1413 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1419 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1420 tpacpi_driver_wlsw_emulstate_show,
1421 tpacpi_driver_wlsw_emulstate_store);
1423 /* bluetooth_emulstate ------------------------------------------------- */
1424 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1425 struct device_driver *drv,
1428 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1431 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1432 struct device_driver *drv,
1433 const char *buf, size_t count)
1437 if (parse_strtoul(buf, 1, &t))
1440 tpacpi_bluetooth_emulstate = !!t;
1445 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1446 tpacpi_driver_bluetooth_emulstate_show,
1447 tpacpi_driver_bluetooth_emulstate_store);
1449 /* wwan_emulstate ------------------------------------------------- */
1450 static ssize_t tpacpi_driver_wwan_emulstate_show(
1451 struct device_driver *drv,
1454 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1457 static ssize_t tpacpi_driver_wwan_emulstate_store(
1458 struct device_driver *drv,
1459 const char *buf, size_t count)
1463 if (parse_strtoul(buf, 1, &t))
1466 tpacpi_wwan_emulstate = !!t;
1471 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_wwan_emulstate_show,
1473 tpacpi_driver_wwan_emulstate_store);
1475 /* uwb_emulstate ------------------------------------------------- */
1476 static ssize_t tpacpi_driver_uwb_emulstate_show(
1477 struct device_driver *drv,
1480 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1483 static ssize_t tpacpi_driver_uwb_emulstate_store(
1484 struct device_driver *drv,
1485 const char *buf, size_t count)
1489 if (parse_strtoul(buf, 1, &t))
1492 tpacpi_uwb_emulstate = !!t;
1497 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1498 tpacpi_driver_uwb_emulstate_show,
1499 tpacpi_driver_uwb_emulstate_store);
1502 /* --------------------------------------------------------------------- */
1504 static struct driver_attribute *tpacpi_driver_attributes[] = {
1505 &driver_attr_debug_level, &driver_attr_version,
1506 &driver_attr_interface_version,
1509 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1515 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1516 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1520 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1521 if (!res && dbg_wlswemul)
1522 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1523 if (!res && dbg_bluetoothemul)
1524 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1525 if (!res && dbg_wwanemul)
1526 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1527 if (!res && dbg_uwbemul)
1528 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1534 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1538 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1539 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1541 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1542 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1543 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1544 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1545 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1549 /****************************************************************************
1550 ****************************************************************************
1554 ****************************************************************************
1555 ****************************************************************************/
1557 /*************************************************************************
1558 * thinkpad-acpi init subdriver
1561 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1563 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1564 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1566 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1567 (thinkpad_id.bios_version_str) ?
1568 thinkpad_id.bios_version_str : "unknown",
1569 (thinkpad_id.ec_version_str) ?
1570 thinkpad_id.ec_version_str : "unknown");
1572 if (thinkpad_id.vendor && thinkpad_id.model_str)
1573 printk(TPACPI_INFO "%s %s, model %s\n",
1574 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1575 "IBM" : ((thinkpad_id.vendor ==
1576 PCI_VENDOR_ID_LENOVO) ?
1577 "Lenovo" : "Unknown vendor"),
1578 thinkpad_id.model_str,
1579 (thinkpad_id.nummodel_str) ?
1580 thinkpad_id.nummodel_str : "unknown");
1585 static int thinkpad_acpi_driver_read(char *p)
1589 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1590 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1595 static struct ibm_struct thinkpad_acpi_driver_data = {
1597 .read = thinkpad_acpi_driver_read,
1600 /*************************************************************************
1604 enum { /* hot key scan codes (derived from ACPI DSDT) */
1605 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1606 TP_ACPI_HOTKEYSCAN_FNF2,
1607 TP_ACPI_HOTKEYSCAN_FNF3,
1608 TP_ACPI_HOTKEYSCAN_FNF4,
1609 TP_ACPI_HOTKEYSCAN_FNF5,
1610 TP_ACPI_HOTKEYSCAN_FNF6,
1611 TP_ACPI_HOTKEYSCAN_FNF7,
1612 TP_ACPI_HOTKEYSCAN_FNF8,
1613 TP_ACPI_HOTKEYSCAN_FNF9,
1614 TP_ACPI_HOTKEYSCAN_FNF10,
1615 TP_ACPI_HOTKEYSCAN_FNF11,
1616 TP_ACPI_HOTKEYSCAN_FNF12,
1617 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1618 TP_ACPI_HOTKEYSCAN_FNINSERT,
1619 TP_ACPI_HOTKEYSCAN_FNDELETE,
1620 TP_ACPI_HOTKEYSCAN_FNHOME,
1621 TP_ACPI_HOTKEYSCAN_FNEND,
1622 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1623 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1624 TP_ACPI_HOTKEYSCAN_FNSPACE,
1625 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1626 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1627 TP_ACPI_HOTKEYSCAN_MUTE,
1628 TP_ACPI_HOTKEYSCAN_THINKPAD,
1631 enum { /* Keys available through NVRAM polling */
1632 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1633 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1636 enum { /* Positions of some of the keys in hotkey masks */
1637 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1638 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1639 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1640 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1641 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1642 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1643 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1644 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1645 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1646 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1647 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1650 enum { /* NVRAM to ACPI HKEY group map */
1651 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1652 TP_ACPI_HKEY_ZOOM_MASK |
1653 TP_ACPI_HKEY_DISPSWTCH_MASK |
1654 TP_ACPI_HKEY_HIBERNATE_MASK,
1655 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1656 TP_ACPI_HKEY_BRGHTDWN_MASK,
1657 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1658 TP_ACPI_HKEY_VOLDWN_MASK |
1659 TP_ACPI_HKEY_MUTE_MASK,
1662 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1663 struct tp_nvram_state {
1664 u16 thinkpad_toggle:1;
1666 u16 display_toggle:1;
1667 u16 thinklight_toggle:1;
1668 u16 hibernate_toggle:1;
1669 u16 displayexp_toggle:1;
1670 u16 display_state:1;
1671 u16 brightness_toggle:1;
1672 u16 volume_toggle:1;
1675 u8 brightness_level;
1679 static struct task_struct *tpacpi_hotkey_task;
1680 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1681 static int hotkey_poll_freq = 10; /* Hz */
1682 static struct mutex hotkey_thread_mutex;
1683 static struct mutex hotkey_thread_data_mutex;
1684 static unsigned int hotkey_config_change;
1686 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1688 #define hotkey_source_mask 0U
1690 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1692 static struct mutex hotkey_mutex;
1694 static enum { /* Reasons for waking up */
1695 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1696 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1697 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1698 } hotkey_wakeup_reason;
1700 static int hotkey_autosleep_ack;
1702 static u32 hotkey_orig_mask;
1703 static u32 hotkey_all_mask;
1704 static u32 hotkey_reserved_mask;
1705 static u32 hotkey_mask;
1707 static unsigned int hotkey_report_mode;
1709 static u16 *hotkey_keycode_map;
1711 static struct attribute_set *hotkey_dev_attributes;
1713 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1714 #define HOTKEY_CONFIG_CRITICAL_START \
1716 mutex_lock(&hotkey_thread_data_mutex); \
1717 hotkey_config_change++; \
1719 #define HOTKEY_CONFIG_CRITICAL_END \
1720 mutex_unlock(&hotkey_thread_data_mutex);
1722 #define HOTKEY_CONFIG_CRITICAL_START
1723 #define HOTKEY_CONFIG_CRITICAL_END
1724 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1726 /* HKEY.MHKG() return bits */
1727 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1729 static int hotkey_get_wlsw(void)
1733 if (!tp_features.hotkey_wlsw)
1736 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1738 return (tpacpi_wlsw_emulstate) ?
1739 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1742 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1745 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1748 static int hotkey_get_tablet_mode(int *status)
1752 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1755 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1760 * Call with hotkey_mutex held
1762 static int hotkey_mask_get(void)
1766 if (tp_features.hotkey_mask) {
1767 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1770 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1776 * Call with hotkey_mutex held
1778 static int hotkey_mask_set(u32 mask)
1783 if (tp_features.hotkey_mask) {
1784 if (!tp_warned.hotkey_mask_ff &&
1785 (mask == 0xffff || mask == 0xffffff ||
1786 mask == 0xffffffff)) {
1787 tp_warned.hotkey_mask_ff = 1;
1788 printk(TPACPI_NOTICE
1789 "setting the hotkey mask to 0x%08x is likely "
1790 "not the best way to go about it\n", mask);
1791 printk(TPACPI_NOTICE
1792 "please consider using the driver defaults, "
1793 "and refer to up-to-date thinkpad-acpi "
1797 HOTKEY_CONFIG_CRITICAL_START
1798 for (i = 0; i < 32; i++) {
1800 /* enable in firmware mask only keys not in NVRAM
1801 * mode, but enable the key in the cached hotkey_mask
1802 * regardless of mode, or the key will end up
1803 * disabled by hotkey_mask_get() */
1804 if (!acpi_evalf(hkey_handle,
1805 NULL, "MHKM", "vdd", i + 1,
1806 !!((mask & ~hotkey_source_mask) & m))) {
1810 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1813 HOTKEY_CONFIG_CRITICAL_END
1815 /* hotkey_mask_get must be called unconditionally below */
1816 if (!hotkey_mask_get() && !rc &&
1817 (hotkey_mask & ~hotkey_source_mask) !=
1818 (mask & ~hotkey_source_mask)) {
1819 printk(TPACPI_NOTICE
1820 "requested hot key mask 0x%08x, but "
1821 "firmware forced it to 0x%08x\n",
1825 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1826 HOTKEY_CONFIG_CRITICAL_START
1827 hotkey_mask = mask & hotkey_source_mask;
1828 HOTKEY_CONFIG_CRITICAL_END
1830 if (hotkey_mask != mask) {
1831 printk(TPACPI_NOTICE
1832 "requested hot key mask 0x%08x, "
1833 "forced to 0x%08x (NVRAM poll mask is "
1834 "0x%08x): no firmware mask support\n",
1835 mask, hotkey_mask, hotkey_source_mask);
1840 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1846 static int hotkey_status_get(int *status)
1848 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1854 static int hotkey_status_set(bool enable)
1856 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1862 static void tpacpi_input_send_tabletsw(void)
1866 if (tp_features.hotkey_tablet &&
1867 !hotkey_get_tablet_mode(&state)) {
1868 mutex_lock(&tpacpi_inputdev_send_mutex);
1870 input_report_switch(tpacpi_inputdev,
1871 SW_TABLET_MODE, !!state);
1872 input_sync(tpacpi_inputdev);
1874 mutex_unlock(&tpacpi_inputdev_send_mutex);
1878 static void tpacpi_input_send_key(unsigned int scancode)
1880 unsigned int keycode;
1882 keycode = hotkey_keycode_map[scancode];
1884 if (keycode != KEY_RESERVED) {
1885 mutex_lock(&tpacpi_inputdev_send_mutex);
1887 input_report_key(tpacpi_inputdev, keycode, 1);
1888 if (keycode == KEY_UNKNOWN)
1889 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1891 input_sync(tpacpi_inputdev);
1893 input_report_key(tpacpi_inputdev, keycode, 0);
1894 if (keycode == KEY_UNKNOWN)
1895 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1897 input_sync(tpacpi_inputdev);
1899 mutex_unlock(&tpacpi_inputdev_send_mutex);
1903 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1904 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1906 static void tpacpi_hotkey_send_key(unsigned int scancode)
1908 tpacpi_input_send_key(scancode);
1909 if (hotkey_report_mode < 2) {
1910 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1911 0x80, 0x1001 + scancode);
1915 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1919 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1920 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1921 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1922 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1923 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1924 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1926 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1927 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1928 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1930 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1931 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1932 n->displayexp_toggle =
1933 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1935 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1936 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1937 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1938 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1939 n->brightness_toggle =
1940 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1942 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1943 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1944 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1945 >> TP_NVRAM_POS_LEVEL_VOLUME;
1946 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1947 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1951 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1953 if ((mask & (1 << __scancode)) && \
1954 oldn->__member != newn->__member) \
1955 tpacpi_hotkey_send_key(__scancode); \
1958 #define TPACPI_MAY_SEND_KEY(__scancode) \
1959 do { if (mask & (1 << __scancode)) \
1960 tpacpi_hotkey_send_key(__scancode); } while (0)
1962 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1963 struct tp_nvram_state *newn,
1966 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1967 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1968 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1969 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1971 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1973 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1976 if (oldn->volume_toggle != newn->volume_toggle) {
1977 if (oldn->mute != newn->mute) {
1978 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1980 if (oldn->volume_level > newn->volume_level) {
1981 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1982 } else if (oldn->volume_level < newn->volume_level) {
1983 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1984 } else if (oldn->mute == newn->mute) {
1985 /* repeated key presses that didn't change state */
1987 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1988 } else if (newn->volume_level != 0) {
1989 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1991 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1996 /* handle brightness */
1997 if (oldn->brightness_toggle != newn->brightness_toggle) {
1998 if (oldn->brightness_level < newn->brightness_level) {
1999 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2000 } else if (oldn->brightness_level > newn->brightness_level) {
2001 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2003 /* repeated key presses that didn't change state */
2004 if (newn->brightness_level != 0) {
2005 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2007 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2013 #undef TPACPI_COMPARE_KEY
2014 #undef TPACPI_MAY_SEND_KEY
2016 static int hotkey_kthread(void *data)
2018 struct tp_nvram_state s[2];
2020 unsigned int si, so;
2022 unsigned int change_detector, must_reset;
2024 mutex_lock(&hotkey_thread_mutex);
2026 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2035 /* Initial state for compares */
2036 mutex_lock(&hotkey_thread_data_mutex);
2037 change_detector = hotkey_config_change;
2038 mask = hotkey_source_mask & hotkey_mask;
2039 mutex_unlock(&hotkey_thread_data_mutex);
2040 hotkey_read_nvram(&s[so], mask);
2042 while (!kthread_should_stop() && hotkey_poll_freq) {
2044 t = 1000/hotkey_poll_freq;
2045 t = msleep_interruptible(t);
2046 if (unlikely(kthread_should_stop()))
2048 must_reset = try_to_freeze();
2049 if (t > 0 && !must_reset)
2052 mutex_lock(&hotkey_thread_data_mutex);
2053 if (must_reset || hotkey_config_change != change_detector) {
2054 /* forget old state on thaw or config change */
2057 change_detector = hotkey_config_change;
2059 mask = hotkey_source_mask & hotkey_mask;
2060 mutex_unlock(&hotkey_thread_data_mutex);
2063 hotkey_read_nvram(&s[si], mask);
2064 if (likely(si != so)) {
2065 hotkey_compare_and_issue_event(&s[so], &s[si],
2075 mutex_unlock(&hotkey_thread_mutex);
2079 static void hotkey_poll_stop_sync(void)
2081 if (tpacpi_hotkey_task) {
2082 if (frozen(tpacpi_hotkey_task) ||
2083 freezing(tpacpi_hotkey_task))
2084 thaw_process(tpacpi_hotkey_task);
2086 kthread_stop(tpacpi_hotkey_task);
2087 tpacpi_hotkey_task = NULL;
2088 mutex_lock(&hotkey_thread_mutex);
2089 /* at this point, the thread did exit */
2090 mutex_unlock(&hotkey_thread_mutex);
2094 /* call with hotkey_mutex held */
2095 static void hotkey_poll_setup(int may_warn)
2097 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2098 hotkey_poll_freq > 0 &&
2099 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2100 if (!tpacpi_hotkey_task) {
2101 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2102 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2103 if (IS_ERR(tpacpi_hotkey_task)) {
2104 tpacpi_hotkey_task = NULL;
2106 "could not create kernel thread "
2107 "for hotkey polling\n");
2111 hotkey_poll_stop_sync();
2113 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
2114 printk(TPACPI_NOTICE
2115 "hot keys 0x%08x require polling, "
2116 "which is currently disabled\n",
2117 hotkey_source_mask);
2122 static void hotkey_poll_setup_safe(int may_warn)
2124 mutex_lock(&hotkey_mutex);
2125 hotkey_poll_setup(may_warn);
2126 mutex_unlock(&hotkey_mutex);
2129 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2131 static void hotkey_poll_setup_safe(int __unused)
2135 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2137 static int hotkey_inputdev_open(struct input_dev *dev)
2139 switch (tpacpi_lifecycle) {
2140 case TPACPI_LIFE_INIT:
2142 * hotkey_init will call hotkey_poll_setup_safe
2143 * at the appropriate moment
2146 case TPACPI_LIFE_EXITING:
2148 case TPACPI_LIFE_RUNNING:
2149 hotkey_poll_setup_safe(0);
2153 /* Should only happen if tpacpi_lifecycle is corrupt */
2158 static void hotkey_inputdev_close(struct input_dev *dev)
2160 /* disable hotkey polling when possible */
2161 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2162 hotkey_poll_setup_safe(0);
2165 /* sysfs hotkey enable ------------------------------------------------- */
2166 static ssize_t hotkey_enable_show(struct device *dev,
2167 struct device_attribute *attr,
2172 printk_deprecated_attribute("hotkey_enable",
2173 "Hotkey reporting is always enabled");
2175 res = hotkey_status_get(&status);
2179 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2182 static ssize_t hotkey_enable_store(struct device *dev,
2183 struct device_attribute *attr,
2184 const char *buf, size_t count)
2188 printk_deprecated_attribute("hotkey_enable",
2189 "Hotkeys can be disabled through hotkey_mask");
2191 if (parse_strtoul(buf, 1, &t))
2200 static struct device_attribute dev_attr_hotkey_enable =
2201 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2202 hotkey_enable_show, hotkey_enable_store);
2204 /* sysfs hotkey mask --------------------------------------------------- */
2205 static ssize_t hotkey_mask_show(struct device *dev,
2206 struct device_attribute *attr,
2211 if (mutex_lock_killable(&hotkey_mutex))
2212 return -ERESTARTSYS;
2213 res = hotkey_mask_get();
2214 mutex_unlock(&hotkey_mutex);
2217 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2220 static ssize_t hotkey_mask_store(struct device *dev,
2221 struct device_attribute *attr,
2222 const char *buf, size_t count)
2227 if (parse_strtoul(buf, 0xffffffffUL, &t))
2230 if (mutex_lock_killable(&hotkey_mutex))
2231 return -ERESTARTSYS;
2233 res = hotkey_mask_set(t);
2235 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2236 hotkey_poll_setup(1);
2239 mutex_unlock(&hotkey_mutex);
2241 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2243 return (res) ? res : count;
2246 static struct device_attribute dev_attr_hotkey_mask =
2247 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2248 hotkey_mask_show, hotkey_mask_store);
2250 /* sysfs hotkey bios_enabled ------------------------------------------- */
2251 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2252 struct device_attribute *attr,
2255 return sprintf(buf, "0\n");
2258 static struct device_attribute dev_attr_hotkey_bios_enabled =
2259 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2261 /* sysfs hotkey bios_mask ---------------------------------------------- */
2262 static ssize_t hotkey_bios_mask_show(struct device *dev,
2263 struct device_attribute *attr,
2266 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2269 static struct device_attribute dev_attr_hotkey_bios_mask =
2270 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2272 /* sysfs hotkey all_mask ----------------------------------------------- */
2273 static ssize_t hotkey_all_mask_show(struct device *dev,
2274 struct device_attribute *attr,
2277 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2278 hotkey_all_mask | hotkey_source_mask);
2281 static struct device_attribute dev_attr_hotkey_all_mask =
2282 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2284 /* sysfs hotkey recommended_mask --------------------------------------- */
2285 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2286 struct device_attribute *attr,
2289 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2290 (hotkey_all_mask | hotkey_source_mask)
2291 & ~hotkey_reserved_mask);
2294 static struct device_attribute dev_attr_hotkey_recommended_mask =
2295 __ATTR(hotkey_recommended_mask, S_IRUGO,
2296 hotkey_recommended_mask_show, NULL);
2298 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2300 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2301 static ssize_t hotkey_source_mask_show(struct device *dev,
2302 struct device_attribute *attr,
2305 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2308 static ssize_t hotkey_source_mask_store(struct device *dev,
2309 struct device_attribute *attr,
2310 const char *buf, size_t count)
2314 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2315 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2318 if (mutex_lock_killable(&hotkey_mutex))
2319 return -ERESTARTSYS;
2321 HOTKEY_CONFIG_CRITICAL_START
2322 hotkey_source_mask = t;
2323 HOTKEY_CONFIG_CRITICAL_END
2325 hotkey_poll_setup(1);
2327 mutex_unlock(&hotkey_mutex);
2329 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2334 static struct device_attribute dev_attr_hotkey_source_mask =
2335 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2336 hotkey_source_mask_show, hotkey_source_mask_store);
2338 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2339 static ssize_t hotkey_poll_freq_show(struct device *dev,
2340 struct device_attribute *attr,
2343 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2346 static ssize_t hotkey_poll_freq_store(struct device *dev,
2347 struct device_attribute *attr,
2348 const char *buf, size_t count)
2352 if (parse_strtoul(buf, 25, &t))
2355 if (mutex_lock_killable(&hotkey_mutex))
2356 return -ERESTARTSYS;
2358 hotkey_poll_freq = t;
2360 hotkey_poll_setup(1);
2361 mutex_unlock(&hotkey_mutex);
2363 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2368 static struct device_attribute dev_attr_hotkey_poll_freq =
2369 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2370 hotkey_poll_freq_show, hotkey_poll_freq_store);
2372 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2374 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2375 static ssize_t hotkey_radio_sw_show(struct device *dev,
2376 struct device_attribute *attr,
2380 res = hotkey_get_wlsw();
2384 /* Opportunistic update */
2385 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2387 return snprintf(buf, PAGE_SIZE, "%d\n",
2388 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2391 static struct device_attribute dev_attr_hotkey_radio_sw =
2392 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2394 static void hotkey_radio_sw_notify_change(void)
2396 if (tp_features.hotkey_wlsw)
2397 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2401 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2402 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2403 struct device_attribute *attr,
2407 res = hotkey_get_tablet_mode(&s);
2411 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2414 static struct device_attribute dev_attr_hotkey_tablet_mode =
2415 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2417 static void hotkey_tablet_mode_notify_change(void)
2419 if (tp_features.hotkey_tablet)
2420 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2421 "hotkey_tablet_mode");
2424 /* sysfs hotkey report_mode -------------------------------------------- */
2425 static ssize_t hotkey_report_mode_show(struct device *dev,
2426 struct device_attribute *attr,
2429 return snprintf(buf, PAGE_SIZE, "%d\n",
2430 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2433 static struct device_attribute dev_attr_hotkey_report_mode =
2434 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2436 /* sysfs wakeup reason (pollable) -------------------------------------- */
2437 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2438 struct device_attribute *attr,
2441 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2444 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2445 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2447 static void hotkey_wakeup_reason_notify_change(void)
2449 if (tp_features.hotkey_mask)
2450 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2454 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2455 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2456 struct device_attribute *attr,
2459 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2462 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2463 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2464 hotkey_wakeup_hotunplug_complete_show, NULL);
2466 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2468 if (tp_features.hotkey_mask)
2469 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2470 "wakeup_hotunplug_complete");
2473 /* --------------------------------------------------------------------- */
2475 static struct attribute *hotkey_attributes[] __initdata = {
2476 &dev_attr_hotkey_enable.attr,
2477 &dev_attr_hotkey_bios_enabled.attr,
2478 &dev_attr_hotkey_report_mode.attr,
2479 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2480 &dev_attr_hotkey_mask.attr,
2481 &dev_attr_hotkey_all_mask.attr,
2482 &dev_attr_hotkey_recommended_mask.attr,
2483 &dev_attr_hotkey_source_mask.attr,
2484 &dev_attr_hotkey_poll_freq.attr,
2488 static struct attribute *hotkey_mask_attributes[] __initdata = {
2489 &dev_attr_hotkey_bios_mask.attr,
2490 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2491 &dev_attr_hotkey_mask.attr,
2492 &dev_attr_hotkey_all_mask.attr,
2493 &dev_attr_hotkey_recommended_mask.attr,
2495 &dev_attr_hotkey_wakeup_reason.attr,
2496 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2500 * Sync both the hw and sw blocking state of all switches
2502 static void tpacpi_send_radiosw_update(void)
2507 * We must sync all rfkill controllers *before* issuing any
2508 * rfkill input events, or we will race the rfkill core input
2511 * tpacpi_inputdev_send_mutex works as a syncronization point
2514 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2517 wlsw = hotkey_get_wlsw();
2519 /* Sync hw blocking state first if it is hw-blocked */
2520 if (wlsw == TPACPI_RFK_RADIO_OFF)
2521 tpacpi_rfk_update_hwblock_state(true);
2523 /* Sync sw blocking state */
2524 tpacpi_rfk_update_swstate_all();
2526 /* Sync hw blocking state last if it is hw-unblocked */
2527 if (wlsw == TPACPI_RFK_RADIO_ON)
2528 tpacpi_rfk_update_hwblock_state(false);
2530 /* Issue rfkill input event for WLSW switch */
2532 mutex_lock(&tpacpi_inputdev_send_mutex);
2534 input_report_switch(tpacpi_inputdev,
2535 SW_RFKILL_ALL, (wlsw > 0));
2536 input_sync(tpacpi_inputdev);
2538 mutex_unlock(&tpacpi_inputdev_send_mutex);
2542 * this can be unconditional, as we will poll state again
2543 * if userspace uses the notify to read data
2545 hotkey_radio_sw_notify_change();
2548 static void hotkey_exit(void)
2550 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2551 hotkey_poll_stop_sync();
2554 if (hotkey_dev_attributes)
2555 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2557 kfree(hotkey_keycode_map);
2559 if (tp_features.hotkey) {
2560 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2561 "restoring original hot key mask\n");
2562 /* no short-circuit boolean operator below! */
2563 if ((hotkey_mask_set(hotkey_orig_mask) |
2564 hotkey_status_set(false)) != 0)
2566 "failed to restore hot key mask "
2567 "to BIOS defaults\n");
2571 static int __init hotkey_init(struct ibm_init_struct *iibm)
2573 /* Requirements for changing the default keymaps:
2575 * 1. Many of the keys are mapped to KEY_RESERVED for very
2576 * good reasons. Do not change them unless you have deep
2577 * knowledge on the IBM and Lenovo ThinkPad firmware for
2578 * the various ThinkPad models. The driver behaves
2579 * differently for KEY_RESERVED: such keys have their
2580 * hot key mask *unset* in mask_recommended, and also
2581 * in the initial hot key mask programmed into the
2582 * firmware at driver load time, which means the firm-
2583 * ware may react very differently if you change them to
2586 * 2. You must be subscribed to the linux-thinkpad and
2587 * ibm-acpi-devel mailing lists, and you should read the
2588 * list archives since 2007 if you want to change the
2589 * keymaps. This requirement exists so that you will
2590 * know the past history of problems with the thinkpad-
2591 * acpi driver keymaps, and also that you will be
2592 * listening to any bug reports;
2594 * 3. Do not send thinkpad-acpi specific patches directly to
2595 * for merging, *ever*. Send them to the linux-acpi
2596 * mailinglist for comments. Merging is to be done only
2597 * through acpi-test and the ACPI maintainer.
2599 * If the above is too much to ask, don't change the keymap.
2600 * Ask the thinkpad-acpi maintainer to do it, instead.
2602 static u16 ibm_keycode_map[] __initdata = {
2603 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2604 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2605 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2606 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2608 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2609 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2610 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2611 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2613 /* brightness: firmware always reacts to them, unless
2614 * X.org did some tricks in the radeon BIOS scratch
2615 * registers of *some* models */
2616 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2617 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2619 /* Thinklight: firmware always react to it */
2620 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2622 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2623 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2625 /* Volume: firmware always react to it and reprograms
2626 * the built-in *extra* mixer. Never map it to control
2627 * another mixer by default. */
2628 KEY_RESERVED, /* 0x14: VOLUME UP */
2629 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2630 KEY_RESERVED, /* 0x16: MUTE */
2632 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2634 /* (assignments unknown, please report if found) */
2635 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2636 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2638 static u16 lenovo_keycode_map[] __initdata = {
2639 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2640 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2641 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2642 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2644 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2645 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2646 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2647 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2649 /* These either have to go through ACPI video, or
2650 * act like in the IBM ThinkPads, so don't ever
2651 * enable them by default */
2652 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2653 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2655 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2657 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2658 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2660 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2661 * react to it and reprograms the built-in *extra* mixer.
2662 * Never map it to control another mixer by default.
2664 * T60?, T61, R60?, R61: firmware and EC tries to send
2665 * these over the regular keyboard, so these are no-ops,
2666 * but there are still weird bugs re. MUTE, so do not
2667 * change unless you get test reports from all Lenovo
2668 * models. May cause the BIOS to interfere with the
2671 KEY_RESERVED, /* 0x14: VOLUME UP */
2672 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2673 KEY_RESERVED, /* 0x16: MUTE */
2675 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2677 /* (assignments unknown, please report if found) */
2678 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2679 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2682 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2683 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2684 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2690 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2691 "initializing hotkey subdriver\n");
2693 BUG_ON(!tpacpi_inputdev);
2694 BUG_ON(tpacpi_inputdev->open != NULL ||
2695 tpacpi_inputdev->close != NULL);
2697 TPACPI_ACPIHANDLE_INIT(hkey);
2698 mutex_init(&hotkey_mutex);
2700 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2701 mutex_init(&hotkey_thread_mutex);
2702 mutex_init(&hotkey_thread_data_mutex);
2705 /* hotkey not supported on 570 */
2706 tp_features.hotkey = hkey_handle != NULL;
2708 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2710 str_supported(tp_features.hotkey));
2712 if (!tp_features.hotkey)
2715 tpacpi_disable_brightness_delay();
2717 hotkey_dev_attributes = create_attr_set(13, NULL);
2718 if (!hotkey_dev_attributes)
2720 res = add_many_to_attr_set(hotkey_dev_attributes,
2722 ARRAY_SIZE(hotkey_attributes));
2726 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2727 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2728 for HKEY interface version 0x100 */
2729 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2730 if ((hkeyv >> 8) != 1) {
2731 printk(TPACPI_ERR "unknown version of the "
2732 "HKEY interface: 0x%x\n", hkeyv);
2733 printk(TPACPI_ERR "please report this to %s\n",
2737 * MHKV 0x100 in A31, R40, R40e,
2738 * T4x, X31, and later
2740 tp_features.hotkey_mask = 1;
2741 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2742 "firmware HKEY interface version: 0x%x\n",
2747 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2748 "hotkey masks are %s\n",
2749 str_supported(tp_features.hotkey_mask));
2751 if (tp_features.hotkey_mask) {
2752 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2755 "missing MHKA handler, "
2756 "please report this to %s\n",
2758 /* FN+F12, FN+F4, FN+F3 */
2759 hotkey_all_mask = 0x080cU;
2763 /* hotkey_source_mask *must* be zero for
2764 * the first hotkey_mask_get */
2765 if (tp_features.hotkey_mask) {
2766 res = hotkey_mask_get();
2770 hotkey_orig_mask = hotkey_mask;
2771 res = add_many_to_attr_set(
2772 hotkey_dev_attributes,
2773 hotkey_mask_attributes,
2774 ARRAY_SIZE(hotkey_mask_attributes));
2779 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2780 if (tp_features.hotkey_mask) {
2781 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2784 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2787 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2788 "hotkey source mask 0x%08x, polling freq %d\n",
2789 hotkey_source_mask, hotkey_poll_freq);
2792 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2794 tp_features.hotkey_wlsw = 1;
2796 "radio switch emulation enabled\n");
2799 /* Not all thinkpads have a hardware radio switch */
2800 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2801 tp_features.hotkey_wlsw = 1;
2803 "radio switch found; radios are %s\n",
2804 enabled(status, 0));
2806 if (tp_features.hotkey_wlsw)
2807 res = add_to_attr_set(hotkey_dev_attributes,
2808 &dev_attr_hotkey_radio_sw.attr);
2810 /* For X41t, X60t, X61t Tablets... */
2811 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2812 tp_features.hotkey_tablet = 1;
2814 "possible tablet mode switch found; "
2815 "ThinkPad in %s mode\n",
2816 (status & TP_HOTKEY_TABLET_MASK)?
2817 "tablet" : "laptop");
2818 res = add_to_attr_set(hotkey_dev_attributes,
2819 &dev_attr_hotkey_tablet_mode.attr);
2823 res = register_attr_set_with_sysfs(
2824 hotkey_dev_attributes,
2825 &tpacpi_pdev->dev.kobj);
2829 /* Set up key map */
2831 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2833 if (!hotkey_keycode_map) {
2835 "failed to allocate memory for key map\n");
2840 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2841 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2842 "using Lenovo default hot key map\n");
2843 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2844 TPACPI_HOTKEY_MAP_SIZE);
2846 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2847 "using IBM default hot key map\n");
2848 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2849 TPACPI_HOTKEY_MAP_SIZE);
2852 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2853 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2854 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2855 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2856 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2857 tpacpi_inputdev->keycode = hotkey_keycode_map;
2858 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2859 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2860 set_bit(hotkey_keycode_map[i],
2861 tpacpi_inputdev->keybit);
2863 if (i < sizeof(hotkey_reserved_mask)*8)
2864 hotkey_reserved_mask |= 1 << i;
2868 if (tp_features.hotkey_wlsw) {
2869 set_bit(EV_SW, tpacpi_inputdev->evbit);
2870 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2872 if (tp_features.hotkey_tablet) {
2873 set_bit(EV_SW, tpacpi_inputdev->evbit);
2874 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2877 /* Do not issue duplicate brightness change events to
2879 if (!tp_features.bright_acpimode)
2880 /* update bright_acpimode... */
2881 tpacpi_check_std_acpi_brightness_support();
2883 if (tp_features.bright_acpimode) {
2885 "This ThinkPad has standard ACPI backlight "
2886 "brightness control, supported by the ACPI "
2888 printk(TPACPI_NOTICE
2889 "Disabling thinkpad-acpi brightness events "
2892 /* The hotkey_reserved_mask change below is not
2893 * necessary while the keys are at KEY_RESERVED in the
2894 * default map, but better safe than sorry, leave it
2895 * here as a marker of what we have to do, especially
2896 * when we finally become able to set this at runtime
2897 * on response to X.org requests */
2898 hotkey_reserved_mask |=
2899 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2900 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2903 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2904 "enabling firmware HKEY event interface...\n");
2905 res = hotkey_status_set(true);
2910 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2911 & ~hotkey_reserved_mask)
2912 | hotkey_orig_mask);
2913 if (res < 0 && res != -ENXIO) {
2918 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2919 "legacy ibm/hotkey event reporting over procfs %s\n",
2920 (hotkey_report_mode < 2) ?
2921 "enabled" : "disabled");
2923 tpacpi_inputdev->open = &hotkey_inputdev_open;
2924 tpacpi_inputdev->close = &hotkey_inputdev_close;
2926 hotkey_poll_setup_safe(1);
2927 tpacpi_send_radiosw_update();
2928 tpacpi_input_send_tabletsw();
2933 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2934 hotkey_dev_attributes = NULL;
2936 return (res < 0)? res : 1;
2939 static bool hotkey_notify_hotkey(const u32 hkey,
2941 bool *ignore_acpi_ev)
2943 /* 0x1000-0x1FFF: key presses */
2944 unsigned int scancode = hkey & 0xfff;
2945 *send_acpi_ev = true;
2946 *ignore_acpi_ev = false;
2948 if (scancode > 0 && scancode < 0x21) {
2950 if (!(hotkey_source_mask & (1 << scancode))) {
2951 tpacpi_input_send_key(scancode);
2952 *send_acpi_ev = false;
2954 *ignore_acpi_ev = true;
2961 static bool hotkey_notify_wakeup(const u32 hkey,
2963 bool *ignore_acpi_ev)
2965 /* 0x2000-0x2FFF: Wakeup reason */
2966 *send_acpi_ev = true;
2967 *ignore_acpi_ev = false;
2970 case 0x2304: /* suspend, undock */
2971 case 0x2404: /* hibernation, undock */
2972 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2973 *ignore_acpi_ev = true;
2976 case 0x2305: /* suspend, bay eject */
2977 case 0x2405: /* hibernation, bay eject */
2978 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2979 *ignore_acpi_ev = true;
2982 case 0x2313: /* Battery on critical low level (S3) */
2983 case 0x2413: /* Battery on critical low level (S4) */
2985 "EMERGENCY WAKEUP: battery almost empty\n");
2986 /* how to auto-heal: */
2987 /* 2313: woke up from S3, go to S4/S5 */
2988 /* 2413: woke up from S4, go to S5 */
2995 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2997 "woke up due to a hot-unplug "
2999 hotkey_wakeup_reason_notify_change();
3004 static bool hotkey_notify_usrevent(const u32 hkey,
3006 bool *ignore_acpi_ev)
3008 /* 0x5000-0x5FFF: human interface helpers */
3009 *send_acpi_ev = true;
3010 *ignore_acpi_ev = false;
3013 case 0x5010: /* Lenovo new BIOS: brightness changed */
3014 case 0x500b: /* X61t: tablet pen inserted into bay */
3015 case 0x500c: /* X61t: tablet pen removed from bay */
3018 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3019 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3020 tpacpi_input_send_tabletsw();
3021 hotkey_tablet_mode_notify_change();
3022 *send_acpi_ev = false;
3027 /* LID switch events. Do not propagate */
3028 *ignore_acpi_ev = true;
3036 static bool hotkey_notify_thermal(const u32 hkey,
3038 bool *ignore_acpi_ev)
3040 /* 0x6000-0x6FFF: thermal alarms */
3041 *send_acpi_ev = true;
3042 *ignore_acpi_ev = false;
3047 "THERMAL ALARM: battery is too hot!\n");
3048 /* recommended action: warn user through gui */
3052 "THERMAL EMERGENCY: battery is extremely hot!\n");
3053 /* recommended action: immediate sleep/hibernate */
3058 "a sensor reports something is too hot!\n");
3059 /* recommended action: warn user through gui, that */
3060 /* some internal component is too hot */
3064 "THERMAL EMERGENCY: "
3065 "a sensor reports something is extremely hot!\n");
3066 /* recommended action: immediate sleep/hibernate */
3070 "EC reports that Thermal Table has changed\n");
3071 /* recommended action: do nothing, we don't have
3072 * Lenovo ATM information */
3076 "THERMAL ALERT: unknown thermal alarm received\n");
3081 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3085 bool ignore_acpi_ev;
3088 if (event != 0x80) {
3090 "unknown HKEY notification event %d\n", event);
3091 /* forward it to userspace, maybe it knows how to handle it */
3092 acpi_bus_generate_netlink_event(
3093 ibm->acpi->device->pnp.device_class,
3094 dev_name(&ibm->acpi->device->dev),
3100 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3101 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3110 send_acpi_ev = true;
3111 ignore_acpi_ev = false;
3113 switch (hkey >> 12) {
3115 /* 0x1000-0x1FFF: key presses */
3116 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3120 /* 0x2000-0x2FFF: Wakeup reason */
3121 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3125 /* 0x3000-0x3FFF: bay-related wakeups */
3126 if (hkey == 0x3003) {
3127 hotkey_autosleep_ack = 1;
3130 hotkey_wakeup_hotunplug_complete_notify_change();
3137 /* 0x4000-0x4FFF: dock-related wakeups */
3138 if (hkey == 0x4003) {
3139 hotkey_autosleep_ack = 1;
3142 hotkey_wakeup_hotunplug_complete_notify_change();
3149 /* 0x5000-0x5FFF: human interface helpers */
3150 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3154 /* 0x6000-0x6FFF: thermal alarms */
3155 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3159 /* 0x7000-0x7FFF: misc */
3160 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3161 tpacpi_send_radiosw_update();
3166 /* fallthrough to default */
3171 printk(TPACPI_NOTICE
3172 "unhandled HKEY event 0x%04x\n", hkey);
3173 printk(TPACPI_NOTICE
3174 "please report the conditions when this "
3175 "event happened to %s\n", TPACPI_MAIL);
3179 if (!ignore_acpi_ev &&
3180 (send_acpi_ev || hotkey_report_mode < 2)) {
3181 acpi_bus_generate_proc_event(ibm->acpi->device,
3185 /* netlink events */
3186 if (!ignore_acpi_ev && send_acpi_ev) {
3187 acpi_bus_generate_netlink_event(
3188 ibm->acpi->device->pnp.device_class,
3189 dev_name(&ibm->acpi->device->dev),
3195 static void hotkey_suspend(pm_message_t state)
3197 /* Do these on suspend, we get the events on early resume! */
3198 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3199 hotkey_autosleep_ack = 0;
3202 static void hotkey_resume(void)
3204 tpacpi_disable_brightness_delay();
3206 if (hotkey_mask_get())
3208 "error while trying to read hot key mask "
3210 tpacpi_send_radiosw_update();
3211 hotkey_tablet_mode_notify_change();
3212 hotkey_wakeup_reason_notify_change();
3213 hotkey_wakeup_hotunplug_complete_notify_change();
3214 hotkey_poll_setup_safe(0);
3217 /* procfs -------------------------------------------------------------- */
3218 static int hotkey_read(char *p)
3223 if (!tp_features.hotkey) {
3224 len += sprintf(p + len, "status:\t\tnot supported\n");
3228 if (mutex_lock_killable(&hotkey_mutex))
3229 return -ERESTARTSYS;
3230 res = hotkey_status_get(&status);
3232 res = hotkey_mask_get();
3233 mutex_unlock(&hotkey_mutex);
3237 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3238 if (tp_features.hotkey_mask) {
3239 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3240 len += sprintf(p + len,
3241 "commands:\tenable, disable, reset, <mask>\n");
3243 len += sprintf(p + len, "mask:\t\tnot supported\n");
3244 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3250 static void hotkey_enabledisable_warn(bool enable)
3252 tpacpi_log_usertask("procfs hotkey enable/disable");
3253 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3255 "hotkey enable/disable functionality has been "
3256 "removed from the driver. Hotkeys are always "
3259 "Please remove the hotkey=enable module "
3260 "parameter, it is deprecated. Hotkeys are always "
3264 static int hotkey_write(char *buf)
3270 if (!tp_features.hotkey)
3273 if (mutex_lock_killable(&hotkey_mutex))
3274 return -ERESTARTSYS;
3279 while ((cmd = next_cmd(&buf))) {
3280 if (strlencmp(cmd, "enable") == 0) {
3281 hotkey_enabledisable_warn(1);
3282 } else if (strlencmp(cmd, "disable") == 0) {
3283 hotkey_enabledisable_warn(0);
3285 } else if (strlencmp(cmd, "reset") == 0) {
3286 mask = hotkey_orig_mask;
3287 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3289 } else if (sscanf(cmd, "%x", &mask) == 1) {
3298 tpacpi_disclose_usertask("procfs hotkey",
3299 "set mask to 0x%08x\n", mask);
3301 if (!res && mask != hotkey_mask)
3302 res = hotkey_mask_set(mask);
3305 mutex_unlock(&hotkey_mutex);
3309 static const struct acpi_device_id ibm_htk_device_ids[] = {
3310 {TPACPI_ACPI_HKEY_HID, 0},
3314 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3315 .hid = ibm_htk_device_ids,
3316 .notify = hotkey_notify,
3317 .handle = &hkey_handle,
3318 .type = ACPI_DEVICE_NOTIFY,
3321 static struct ibm_struct hotkey_driver_data = {
3323 .read = hotkey_read,
3324 .write = hotkey_write,
3325 .exit = hotkey_exit,
3326 .resume = hotkey_resume,
3327 .suspend = hotkey_suspend,
3328 .acpi = &ibm_hotkey_acpidriver,
3331 /*************************************************************************
3332 * Bluetooth subdriver
3336 /* ACPI GBDC/SBDC bits */
3337 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3338 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3339 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3344 /* ACPI \BLTH commands */
3345 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3346 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3347 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3348 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3349 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3352 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3354 static void bluetooth_suspend(pm_message_t state)
3356 /* Try to make sure radio will resume powered off */
3357 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3358 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3359 vdbg_printk(TPACPI_DBG_RFKILL,
3360 "bluetooth power down on resume request failed\n");
3363 static int bluetooth_get_status(void)
3367 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3368 if (dbg_bluetoothemul)
3369 return (tpacpi_bluetooth_emulstate) ?
3370 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3373 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3376 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3377 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3380 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3384 vdbg_printk(TPACPI_DBG_RFKILL,
3385 "will attempt to %s bluetooth\n",
3386 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3388 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3389 if (dbg_bluetoothemul) {
3390 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3395 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3396 if (state == TPACPI_RFK_RADIO_ON)
3397 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3401 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3407 /* sysfs bluetooth enable ---------------------------------------------- */
3408 static ssize_t bluetooth_enable_show(struct device *dev,
3409 struct device_attribute *attr,
3412 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3416 static ssize_t bluetooth_enable_store(struct device *dev,
3417 struct device_attribute *attr,
3418 const char *buf, size_t count)
3420 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3424 static struct device_attribute dev_attr_bluetooth_enable =
3425 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3426 bluetooth_enable_show, bluetooth_enable_store);
3428 /* --------------------------------------------------------------------- */
3430 static struct attribute *bluetooth_attributes[] = {
3431 &dev_attr_bluetooth_enable.attr,
3435 static const struct attribute_group bluetooth_attr_group = {
3436 .attrs = bluetooth_attributes,
3439 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3440 .get_status = bluetooth_get_status,
3441 .set_status = bluetooth_set_status,
3444 static void bluetooth_shutdown(void)
3446 /* Order firmware to save current state to NVRAM */
3447 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3448 TP_ACPI_BLTH_SAVE_STATE))
3449 printk(TPACPI_NOTICE
3450 "failed to save bluetooth state to NVRAM\n");
3452 vdbg_printk(TPACPI_DBG_RFKILL,
3453 "bluestooth state saved to NVRAM\n");
3456 static void bluetooth_exit(void)
3458 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3459 &bluetooth_attr_group);
3461 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3463 bluetooth_shutdown();
3466 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3471 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3472 "initializing bluetooth subdriver\n");
3474 TPACPI_ACPIHANDLE_INIT(hkey);
3476 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3477 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3478 tp_features.bluetooth = hkey_handle &&
3479 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3481 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3482 "bluetooth is %s, status 0x%02x\n",
3483 str_supported(tp_features.bluetooth),
3486 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3487 if (dbg_bluetoothemul) {
3488 tp_features.bluetooth = 1;
3490 "bluetooth switch emulation enabled\n");
3493 if (tp_features.bluetooth &&
3494 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3495 /* no bluetooth hardware present in system */
3496 tp_features.bluetooth = 0;
3497 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3498 "bluetooth hardware not installed\n");
3501 if (!tp_features.bluetooth)
3504 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3505 &bluetooth_tprfk_ops,
3506 RFKILL_TYPE_BLUETOOTH,
3507 TPACPI_RFK_BLUETOOTH_SW_NAME,
3512 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3513 &bluetooth_attr_group);
3515 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3522 /* procfs -------------------------------------------------------------- */
3523 static int bluetooth_read(char *p)
3525 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
3528 static int bluetooth_write(char *buf)
3530 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
3533 static struct ibm_struct bluetooth_driver_data = {
3534 .name = "bluetooth",
3535 .read = bluetooth_read,
3536 .write = bluetooth_write,
3537 .exit = bluetooth_exit,
3538 .suspend = bluetooth_suspend,
3539 .shutdown = bluetooth_shutdown,
3542 /*************************************************************************
3547 /* ACPI GWAN/SWAN bits */
3548 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3549 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3550 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3554 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3556 static void wan_suspend(pm_message_t state)
3558 /* Try to make sure radio will resume powered off */
3559 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3560 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3561 vdbg_printk(TPACPI_DBG_RFKILL,
3562 "WWAN power down on resume request failed\n");
3565 static int wan_get_status(void)
3569 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3571 return (tpacpi_wwan_emulstate) ?
3572 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3575 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3578 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3579 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3582 static int wan_set_status(enum tpacpi_rfkill_state state)
3586 vdbg_printk(TPACPI_DBG_RFKILL,
3587 "will attempt to %s wwan\n",
3588 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3590 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3592 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
3597 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3598 if (state == TPACPI_RFK_RADIO_ON)
3599 status = TP_ACPI_WANCARD_RADIOSSW;
3603 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3609 /* sysfs wan enable ---------------------------------------------------- */
3610 static ssize_t wan_enable_show(struct device *dev,
3611 struct device_attribute *attr,
3614 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3618 static ssize_t wan_enable_store(struct device *dev,
3619 struct device_attribute *attr,
3620 const char *buf, size_t count)
3622 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3626 static struct device_attribute dev_attr_wan_enable =
3627 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3628 wan_enable_show, wan_enable_store);
3630 /* --------------------------------------------------------------------- */
3632 static struct attribute *wan_attributes[] = {
3633 &dev_attr_wan_enable.attr,
3637 static const struct attribute_group wan_attr_group = {
3638 .attrs = wan_attributes,
3641 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3642 .get_status = wan_get_status,
3643 .set_status = wan_set_status,
3646 static void wan_shutdown(void)
3648 /* Order firmware to save current state to NVRAM */
3649 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3650 TP_ACPI_WGSV_SAVE_STATE))
3651 printk(TPACPI_NOTICE
3652 "failed to save WWAN state to NVRAM\n");
3654 vdbg_printk(TPACPI_DBG_RFKILL,
3655 "WWAN state saved to NVRAM\n");
3658 static void wan_exit(void)
3660 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3663 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3668 static int __init wan_init(struct ibm_init_struct *iibm)
3673 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3674 "initializing wan subdriver\n");
3676 TPACPI_ACPIHANDLE_INIT(hkey);
3678 tp_features.wan = hkey_handle &&
3679 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3681 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3682 "wan is %s, status 0x%02x\n",
3683 str_supported(tp_features.wan),
3686 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3688 tp_features.wan = 1;
3690 "wwan switch emulation enabled\n");
3693 if (tp_features.wan &&
3694 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3695 /* no wan hardware present in system */
3696 tp_features.wan = 0;
3697 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3698 "wan hardware not installed\n");
3701 if (!tp_features.wan)
3704 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3707 TPACPI_RFK_WWAN_SW_NAME,
3712 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3716 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3723 /* procfs -------------------------------------------------------------- */
3724 static int wan_read(char *p)
3726 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
3729 static int wan_write(char *buf)
3731 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
3734 static struct ibm_struct wan_driver_data = {
3739 .suspend = wan_suspend,
3740 .shutdown = wan_shutdown,
3743 /*************************************************************************
3748 /* ACPI GUWB/SUWB bits */
3749 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3750 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3753 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3755 static int uwb_get_status(void)
3759 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3761 return (tpacpi_uwb_emulstate) ?
3762 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3765 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3768 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3769 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3772 static int uwb_set_status(enum tpacpi_rfkill_state state)
3776 vdbg_printk(TPACPI_DBG_RFKILL,
3777 "will attempt to %s UWB\n",
3778 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3780 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3782 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
3787 if (state == TPACPI_RFK_RADIO_ON)
3788 status = TP_ACPI_UWB_RADIOSSW;
3792 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3798 /* --------------------------------------------------------------------- */
3800 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
3801 .get_status = uwb_get_status,
3802 .set_status = uwb_set_status,
3805 static void uwb_exit(void)
3807 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
3810 static int __init uwb_init(struct ibm_init_struct *iibm)
3815 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3816 "initializing uwb subdriver\n");
3818 TPACPI_ACPIHANDLE_INIT(hkey);
3820 tp_features.uwb = hkey_handle &&
3821 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3823 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3824 "uwb is %s, status 0x%02x\n",
3825 str_supported(tp_features.uwb),
3828 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3830 tp_features.uwb = 1;
3832 "uwb switch emulation enabled\n");
3835 if (tp_features.uwb &&
3836 !(status & TP_ACPI_UWB_HWPRESENT)) {
3837 /* no uwb hardware present in system */
3838 tp_features.uwb = 0;
3839 dbg_printk(TPACPI_DBG_INIT,
3840 "uwb hardware not installed\n");
3843 if (!tp_features.uwb)
3846 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3849 TPACPI_RFK_UWB_SW_NAME,
3854 static struct ibm_struct uwb_driver_data = {
3857 .flags.experimental = 1,
3860 /*************************************************************************
3864 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3866 enum video_access_mode {
3867 TPACPI_VIDEO_NONE = 0,
3868 TPACPI_VIDEO_570, /* 570 */
3869 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3870 TPACPI_VIDEO_NEW, /* all others */
3873 enum { /* video status flags, based on VIDEO_570 */
3874 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3875 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3876 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3879 enum { /* TPACPI_VIDEO_570 constants */
3880 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3881 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3882 * video_status_flags */
3883 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3884 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3887 static enum video_access_mode video_supported;
3888 static int video_orig_autosw;
3890 static int video_autosw_get(void);
3891 static int video_autosw_set(int enable);
3893 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3895 static int __init video_init(struct ibm_init_struct *iibm)
3899 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3901 TPACPI_ACPIHANDLE_INIT(vid);
3902 TPACPI_ACPIHANDLE_INIT(vid2);
3904 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3905 /* G41, assume IVGA doesn't change */
3906 vid_handle = vid2_handle;
3909 /* video switching not supported on R30, R31 */
3910 video_supported = TPACPI_VIDEO_NONE;
3911 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3913 video_supported = TPACPI_VIDEO_570;
3914 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3915 /* 600e/x, 770e, 770x */
3916 video_supported = TPACPI_VIDEO_770;
3919 video_supported = TPACPI_VIDEO_NEW;
3921 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3922 str_supported(video_supported != TPACPI_VIDEO_NONE),
3925 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3928 static void video_exit(void)
3930 dbg_printk(TPACPI_DBG_EXIT,
3931 "restoring original video autoswitch mode\n");
3932 if (video_autosw_set(video_orig_autosw))
3933 printk(TPACPI_ERR "error while trying to restore original "
3934 "video autoswitch mode\n");
3937 static int video_outputsw_get(void)
3942 switch (video_supported) {
3943 case TPACPI_VIDEO_570:
3944 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3945 TP_ACPI_VIDEO_570_PHSCMD))
3947 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3949 case TPACPI_VIDEO_770:
3950 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3953 status |= TP_ACPI_VIDEO_S_LCD;
3954 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3957 status |= TP_ACPI_VIDEO_S_CRT;
3959 case TPACPI_VIDEO_NEW:
3960 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3961 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3964 status |= TP_ACPI_VIDEO_S_CRT;
3966 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3967 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3970 status |= TP_ACPI_VIDEO_S_LCD;
3971 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3974 status |= TP_ACPI_VIDEO_S_DVI;
3983 static int video_outputsw_set(int status)
3988 switch (video_supported) {
3989 case TPACPI_VIDEO_570:
3990 res = acpi_evalf(NULL, NULL,
3991 "\\_SB.PHS2", "vdd",
3992 TP_ACPI_VIDEO_570_PHS2CMD,
3993 status | TP_ACPI_VIDEO_570_PHS2SET);
3995 case TPACPI_VIDEO_770:
3996 autosw = video_autosw_get();
4000 res = video_autosw_set(1);
4003 res = acpi_evalf(vid_handle, NULL,
4004 "ASWT", "vdd", status * 0x100, 0);
4005 if (!autosw && video_autosw_set(autosw)) {
4007 "video auto-switch left enabled due to error\n");
4011 case TPACPI_VIDEO_NEW:
4012 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4013 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4019 return (res)? 0 : -EIO;
4022 static int video_autosw_get(void)
4026 switch (video_supported) {
4027 case TPACPI_VIDEO_570:
4028 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4031 case TPACPI_VIDEO_770:
4032 case TPACPI_VIDEO_NEW:
4033 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4043 static int video_autosw_set(int enable)
4045 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4050 static int video_outputsw_cycle(void)
4052 int autosw = video_autosw_get();
4058 switch (video_supported) {
4059 case TPACPI_VIDEO_570:
4060 res = video_autosw_set(1);
4063 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4065 case TPACPI_VIDEO_770:
4066 case TPACPI_VIDEO_NEW:
4067 res = video_autosw_set(1);
4070 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4075 if (!autosw && video_autosw_set(autosw)) {
4077 "video auto-switch left enabled due to error\n");
4081 return (res)? 0 : -EIO;
4084 static int video_expand_toggle(void)
4086 switch (video_supported) {
4087 case TPACPI_VIDEO_570:
4088 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4090 case TPACPI_VIDEO_770:
4091 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4093 case TPACPI_VIDEO_NEW:
4094 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4102 static int video_read(char *p)
4107 if (video_supported == TPACPI_VIDEO_NONE) {
4108 len += sprintf(p + len, "status:\t\tnot supported\n");
4112 status = video_outputsw_get();
4116 autosw = video_autosw_get();
4120 len += sprintf(p + len, "status:\t\tsupported\n");
4121 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4122 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4123 if (video_supported == TPACPI_VIDEO_NEW)
4124 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4125 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4126 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4127 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4128 if (video_supported == TPACPI_VIDEO_NEW)
4129 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4130 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4131 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4136 static int video_write(char *buf)
4139 int enable, disable, status;
4142 if (video_supported == TPACPI_VIDEO_NONE)
4148 while ((cmd = next_cmd(&buf))) {
4149 if (strlencmp(cmd, "lcd_enable") == 0) {
4150 enable |= TP_ACPI_VIDEO_S_LCD;
4151 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4152 disable |= TP_ACPI_VIDEO_S_LCD;
4153 } else if (strlencmp(cmd, "crt_enable") == 0) {
4154 enable |= TP_ACPI_VIDEO_S_CRT;
4155 } else if (strlencmp(cmd, "crt_disable") == 0) {
4156 disable |= TP_ACPI_VIDEO_S_CRT;
4157 } else if (video_supported == TPACPI_VIDEO_NEW &&
4158 strlencmp(cmd, "dvi_enable") == 0) {
4159 enable |= TP_ACPI_VIDEO_S_DVI;
4160 } else if (video_supported == TPACPI_VIDEO_NEW &&
4161 strlencmp(cmd, "dvi_disable") == 0) {
4162 disable |= TP_ACPI_VIDEO_S_DVI;
4163 } else if (strlencmp(cmd, "auto_enable") == 0) {
4164 res = video_autosw_set(1);
4167 } else if (strlencmp(cmd, "auto_disable") == 0) {
4168 res = video_autosw_set(0);
4171 } else if (strlencmp(cmd, "video_switch") == 0) {
4172 res = video_outputsw_cycle();
4175 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4176 res = video_expand_toggle();
4183 if (enable || disable) {
4184 status = video_outputsw_get();
4187 res = video_outputsw_set((status & ~disable) | enable);
4195 static struct ibm_struct video_driver_data = {
4198 .write = video_write,
4202 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4204 /*************************************************************************
4205 * Light (thinklight) subdriver
4208 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4209 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4211 static int light_get_status(void)
4215 if (tp_features.light_status) {
4216 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4224 static int light_set_status(int status)
4228 if (tp_features.light) {
4230 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4232 TP_CMOS_THINKLIGHT_ON :
4233 TP_CMOS_THINKLIGHT_OFF);
4235 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4238 return (rc)? 0 : -EIO;
4244 static void light_set_status_worker(struct work_struct *work)
4246 struct tpacpi_led_classdev *data =
4247 container_of(work, struct tpacpi_led_classdev, work);
4249 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4250 light_set_status((data->new_state != TPACPI_LED_OFF));
4253 static void light_sysfs_set(struct led_classdev *led_cdev,
4254 enum led_brightness brightness)
4256 struct tpacpi_led_classdev *data =
4257 container_of(led_cdev,
4258 struct tpacpi_led_classdev,
4260 data->new_state = (brightness != LED_OFF) ?
4261 TPACPI_LED_ON : TPACPI_LED_OFF;
4262 queue_work(tpacpi_wq, &data->work);
4265 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4267 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4270 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4272 .name = "tpacpi::thinklight",
4273 .brightness_set = &light_sysfs_set,
4274 .brightness_get = &light_sysfs_get,
4278 static int __init light_init(struct ibm_init_struct *iibm)
4282 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4284 TPACPI_ACPIHANDLE_INIT(ledb);
4285 TPACPI_ACPIHANDLE_INIT(lght);
4286 TPACPI_ACPIHANDLE_INIT(cmos);
4287 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4289 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4290 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4292 if (tp_features.light)
4293 /* light status not supported on
4294 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4295 tp_features.light_status =
4296 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4298 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4299 str_supported(tp_features.light),
4300 str_supported(tp_features.light_status));
4302 if (!tp_features.light)
4305 rc = led_classdev_register(&tpacpi_pdev->dev,
4306 &tpacpi_led_thinklight.led_classdev);
4309 tp_features.light = 0;
4310 tp_features.light_status = 0;
4318 static void light_exit(void)
4320 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4321 if (work_pending(&tpacpi_led_thinklight.work))
4322 flush_workqueue(tpacpi_wq);
4325 static int light_read(char *p)
4330 if (!tp_features.light) {
4331 len += sprintf(p + len, "status:\t\tnot supported\n");
4332 } else if (!tp_features.light_status) {
4333 len += sprintf(p + len, "status:\t\tunknown\n");
4334 len += sprintf(p + len, "commands:\ton, off\n");
4336 status = light_get_status();
4339 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4340 len += sprintf(p + len, "commands:\ton, off\n");
4346 static int light_write(char *buf)
4351 if (!tp_features.light)
4354 while ((cmd = next_cmd(&buf))) {
4355 if (strlencmp(cmd, "on") == 0) {
4357 } else if (strlencmp(cmd, "off") == 0) {
4363 return light_set_status(newstatus);
4366 static struct ibm_struct light_driver_data = {
4369 .write = light_write,
4373 /*************************************************************************
4377 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4379 static void dock_notify(struct ibm_struct *ibm, u32 event);
4380 static int dock_read(char *p);
4381 static int dock_write(char *buf);
4383 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4384 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4385 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4386 "\\_SB.PCI.ISA.SLCE", /* 570 */
4387 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4389 /* don't list other alternatives as we install a notify handler on the 570 */
4390 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4392 static const struct acpi_device_id ibm_pci_device_ids[] = {
4393 {PCI_ROOT_HID_STRING, 0},
4397 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4399 .notify = dock_notify,
4400 .handle = &dock_handle,
4401 .type = ACPI_SYSTEM_NOTIFY,
4404 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4405 * We just use it to get notifications of dock hotplug
4406 * in very old thinkpads */
4407 .hid = ibm_pci_device_ids,
4408 .notify = dock_notify,
4409 .handle = &pci_handle,
4410 .type = ACPI_SYSTEM_NOTIFY,
4414 static struct ibm_struct dock_driver_data[2] = {
4418 .write = dock_write,
4419 .acpi = &ibm_dock_acpidriver[0],
4423 .acpi = &ibm_dock_acpidriver[1],
4427 #define dock_docked() (_sta(dock_handle) & 1)
4429 static int __init dock_init(struct ibm_init_struct *iibm)
4431 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4433 TPACPI_ACPIHANDLE_INIT(dock);
4435 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4436 str_supported(dock_handle != NULL));
4438 return (dock_handle)? 0 : 1;
4441 static int __init dock_init2(struct ibm_init_struct *iibm)
4445 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4447 if (dock_driver_data[0].flags.acpi_driver_registered &&
4448 dock_driver_data[0].flags.acpi_notify_installed) {
4449 TPACPI_ACPIHANDLE_INIT(pci);
4450 dock2_needed = (pci_handle != NULL);
4451 vdbg_printk(TPACPI_DBG_INIT,
4452 "dock PCI handler for the TP 570 is %s\n",
4453 str_supported(dock2_needed));
4455 vdbg_printk(TPACPI_DBG_INIT,
4456 "dock subdriver part 2 not required\n");
4460 return (dock2_needed)? 0 : 1;
4463 static void dock_notify(struct ibm_struct *ibm, u32 event)
4465 int docked = dock_docked();
4466 int pci = ibm->acpi->hid && ibm->acpi->device &&
4467 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4470 if (event == 1 && !pci) /* 570 */
4471 data = 1; /* button */
4472 else if (event == 1 && pci) /* 570 */
4473 data = 3; /* dock */
4474 else if (event == 3 && docked)
4475 data = 1; /* button */
4476 else if (event == 3 && !docked)
4477 data = 2; /* undock */
4478 else if (event == 0 && docked)
4479 data = 3; /* dock */
4481 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4482 event, _sta(dock_handle));
4483 data = 0; /* unknown */
4485 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4486 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4487 dev_name(&ibm->acpi->device->dev),
4491 static int dock_read(char *p)
4494 int docked = dock_docked();
4497 len += sprintf(p + len, "status:\t\tnot supported\n");
4499 len += sprintf(p + len, "status:\t\tundocked\n");
4501 len += sprintf(p + len, "status:\t\tdocked\n");
4502 len += sprintf(p + len, "commands:\tdock, undock\n");
4508 static int dock_write(char *buf)
4515 while ((cmd = next_cmd(&buf))) {
4516 if (strlencmp(cmd, "undock") == 0) {
4517 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4518 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4520 } else if (strlencmp(cmd, "dock") == 0) {
4521 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4530 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4532 /*************************************************************************
4536 #ifdef CONFIG_THINKPAD_ACPI_BAY
4538 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4539 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4540 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4541 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4542 ); /* A21e, R30, R31 */
4543 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4544 "_EJ0", /* all others */
4545 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4546 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4547 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4549 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4553 static int __init bay_init(struct ibm_init_struct *iibm)
4555 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4557 TPACPI_ACPIHANDLE_INIT(bay);
4559 TPACPI_ACPIHANDLE_INIT(bay_ej);
4560 TPACPI_ACPIHANDLE_INIT(bay2);
4562 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4564 tp_features.bay_status = bay_handle &&
4565 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4566 tp_features.bay_status2 = bay2_handle &&
4567 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4569 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4570 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4571 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4572 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4574 vdbg_printk(TPACPI_DBG_INIT,
4575 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4576 str_supported(tp_features.bay_status),
4577 str_supported(tp_features.bay_eject),
4578 str_supported(tp_features.bay_status2),
4579 str_supported(tp_features.bay_eject2));
4581 return (tp_features.bay_status || tp_features.bay_eject ||
4582 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4585 static void bay_notify(struct ibm_struct *ibm, u32 event)
4587 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4588 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4589 dev_name(&ibm->acpi->device->dev),
4593 #define bay_occupied(b) (_sta(b##_handle) & 1)
4595 static int bay_read(char *p)
4598 int occupied = bay_occupied(bay);
4599 int occupied2 = bay_occupied(bay2);
4602 len += sprintf(p + len, "status:\t\t%s\n",
4603 tp_features.bay_status ?
4604 (occupied ? "occupied" : "unoccupied") :
4606 if (tp_features.bay_status2)
4607 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4608 "occupied" : "unoccupied");
4610 eject = tp_features.bay_eject && occupied;
4611 eject2 = tp_features.bay_eject2 && occupied2;
4613 if (eject && eject2)
4614 len += sprintf(p + len, "commands:\teject, eject2\n");
4616 len += sprintf(p + len, "commands:\teject\n");
4618 len += sprintf(p + len, "commands:\teject2\n");
4623 static int bay_write(char *buf)
4627 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4630 while ((cmd = next_cmd(&buf))) {
4631 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4632 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4634 } else if (tp_features.bay_eject2 &&
4635 strlencmp(cmd, "eject2") == 0) {
4636 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4645 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4646 .notify = bay_notify,
4647 .handle = &bay_handle,
4648 .type = ACPI_SYSTEM_NOTIFY,
4651 static struct ibm_struct bay_driver_data = {
4655 .acpi = &ibm_bay_acpidriver,
4658 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4660 /*************************************************************************
4664 /* sysfs cmos_command -------------------------------------------------- */
4665 static ssize_t cmos_command_store(struct device *dev,
4666 struct device_attribute *attr,
4667 const char *buf, size_t count)
4669 unsigned long cmos_cmd;
4672 if (parse_strtoul(buf, 21, &cmos_cmd))
4675 res = issue_thinkpad_cmos_command(cmos_cmd);
4676 return (res)? res : count;
4679 static struct device_attribute dev_attr_cmos_command =
4680 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4682 /* --------------------------------------------------------------------- */
4684 static int __init cmos_init(struct ibm_init_struct *iibm)
4688 vdbg_printk(TPACPI_DBG_INIT,
4689 "initializing cmos commands subdriver\n");
4691 TPACPI_ACPIHANDLE_INIT(cmos);
4693 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4694 str_supported(cmos_handle != NULL));
4696 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4700 return (cmos_handle)? 0 : 1;
4703 static void cmos_exit(void)
4705 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4708 static int cmos_read(char *p)
4712 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4713 R30, R31, T20-22, X20-21 */
4715 len += sprintf(p + len, "status:\t\tnot supported\n");
4717 len += sprintf(p + len, "status:\t\tsupported\n");
4718 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4724 static int cmos_write(char *buf)
4729 while ((cmd = next_cmd(&buf))) {
4730 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4731 cmos_cmd >= 0 && cmos_cmd <= 21) {
4736 res = issue_thinkpad_cmos_command(cmos_cmd);
4744 static struct ibm_struct cmos_driver_data = {
4747 .write = cmos_write,
4751 /*************************************************************************
4755 enum led_access_mode {
4756 TPACPI_LED_NONE = 0,
4757 TPACPI_LED_570, /* 570 */
4758 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4759 TPACPI_LED_NEW, /* all others */
4762 enum { /* For TPACPI_LED_OLD */
4763 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4764 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4765 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4768 static enum led_access_mode led_supported;
4770 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4771 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4772 /* T20-22, X20-21 */
4773 "LED", /* all others */
4776 #define TPACPI_LED_NUMLEDS 8
4777 static struct tpacpi_led_classdev *tpacpi_leds;
4778 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4779 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4780 /* there's a limit of 19 chars + NULL before 2.6.26 */
4782 "tpacpi:orange:batt",
4783 "tpacpi:green:batt",
4784 "tpacpi::dock_active",
4785 "tpacpi::bay_active",
4786 "tpacpi::dock_batt",
4787 "tpacpi::unknown_led",
4790 #define TPACPI_SAFE_LEDS 0x0081U
4792 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4794 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4797 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4801 static int led_get_status(const unsigned int led)
4804 enum led_status_t led_s;
4806 switch (led_supported) {
4807 case TPACPI_LED_570:
4808 if (!acpi_evalf(ec_handle,
4809 &status, "GLED", "dd", 1 << led))
4811 led_s = (status == 0)?
4816 tpacpi_led_state_cache[led] = led_s;
4825 static int led_set_status(const unsigned int led,
4826 const enum led_status_t ledstatus)
4828 /* off, on, blink. Index is led_status_t */
4829 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4830 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4834 switch (led_supported) {
4835 case TPACPI_LED_570:
4837 if (unlikely(led > 7))
4839 if (unlikely(tpacpi_is_led_restricted(led)))
4841 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4842 (1 << led), led_sled_arg1[ledstatus]))
4845 case TPACPI_LED_OLD:
4846 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4847 if (unlikely(led > 7))
4849 if (unlikely(tpacpi_is_led_restricted(led)))
4851 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4853 rc = ec_write(TPACPI_LED_EC_HLBL,
4854 (ledstatus == TPACPI_LED_BLINK) << led);
4856 rc = ec_write(TPACPI_LED_EC_HLCL,
4857 (ledstatus != TPACPI_LED_OFF) << led);
4859 case TPACPI_LED_NEW:
4861 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4863 if (unlikely(tpacpi_is_led_restricted(led)))
4865 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4866 led, led_led_arg1[ledstatus]))
4874 tpacpi_led_state_cache[led] = ledstatus;
4879 static void led_set_status_worker(struct work_struct *work)
4881 struct tpacpi_led_classdev *data =
4882 container_of(work, struct tpacpi_led_classdev, work);
4884 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4885 led_set_status(data->led, data->new_state);
4888 static void led_sysfs_set(struct led_classdev *led_cdev,
4889 enum led_brightness brightness)
4891 struct tpacpi_led_classdev *data = container_of(led_cdev,
4892 struct tpacpi_led_classdev, led_classdev);
4894 if (brightness == LED_OFF)
4895 data->new_state = TPACPI_LED_OFF;
4896 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4897 data->new_state = TPACPI_LED_ON;
4899 data->new_state = TPACPI_LED_BLINK;
4901 queue_work(tpacpi_wq, &data->work);
4904 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4905 unsigned long *delay_on, unsigned long *delay_off)
4907 struct tpacpi_led_classdev *data = container_of(led_cdev,
4908 struct tpacpi_led_classdev, led_classdev);
4910 /* Can we choose the flash rate? */
4911 if (*delay_on == 0 && *delay_off == 0) {
4912 /* yes. set them to the hardware blink rate (1 Hz) */
4913 *delay_on = 500; /* ms */
4914 *delay_off = 500; /* ms */
4915 } else if ((*delay_on != 500) || (*delay_off != 500))
4918 data->new_state = TPACPI_LED_BLINK;
4919 queue_work(tpacpi_wq, &data->work);
4924 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4928 struct tpacpi_led_classdev *data = container_of(led_cdev,
4929 struct tpacpi_led_classdev, led_classdev);
4931 rc = led_get_status(data->led);
4933 if (rc == TPACPI_LED_OFF || rc < 0)
4934 rc = LED_OFF; /* no error handling in led class :( */
4941 static void led_exit(void)
4945 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4946 if (tpacpi_leds[i].led_classdev.name)
4947 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4953 static int __init tpacpi_init_led(unsigned int led)
4957 tpacpi_leds[led].led = led;
4959 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4960 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4961 if (led_supported == TPACPI_LED_570)
4962 tpacpi_leds[led].led_classdev.brightness_get =
4965 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4967 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4969 rc = led_classdev_register(&tpacpi_pdev->dev,
4970 &tpacpi_leds[led].led_classdev);
4972 tpacpi_leds[led].led_classdev.name = NULL;
4977 static int __init led_init(struct ibm_init_struct *iibm)
4982 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4984 TPACPI_ACPIHANDLE_INIT(led);
4987 /* led not supported on R30, R31 */
4988 led_supported = TPACPI_LED_NONE;
4989 else if (strlencmp(led_path, "SLED") == 0)
4991 led_supported = TPACPI_LED_570;
4992 else if (strlencmp(led_path, "SYSL") == 0)
4993 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4994 led_supported = TPACPI_LED_OLD;
4997 led_supported = TPACPI_LED_NEW;
4999 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5000 str_supported(led_supported), led_supported);
5002 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5005 printk(TPACPI_ERR "Out of memory for LED data\n");
5009 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5010 if (!tpacpi_is_led_restricted(i)) {
5011 rc = tpacpi_init_led(i);
5019 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5020 if (led_supported != TPACPI_LED_NONE)
5021 printk(TPACPI_NOTICE
5022 "warning: userspace override of important "
5023 "firmware LEDs is enabled\n");
5025 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
5028 #define str_led_status(s) \
5029 ((s) == TPACPI_LED_OFF ? "off" : \
5030 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5032 static int led_read(char *p)
5036 if (!led_supported) {
5037 len += sprintf(p + len, "status:\t\tnot supported\n");
5040 len += sprintf(p + len, "status:\t\tsupported\n");
5042 if (led_supported == TPACPI_LED_570) {
5045 for (i = 0; i < 8; i++) {
5046 status = led_get_status(i);
5049 len += sprintf(p + len, "%d:\t\t%s\n",
5050 i, str_led_status(status));
5054 len += sprintf(p + len, "commands:\t"
5055 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5060 static int led_write(char *buf)
5064 enum led_status_t s;
5069 while ((cmd = next_cmd(&buf))) {
5070 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5073 if (strstr(cmd, "off")) {
5075 } else if (strstr(cmd, "on")) {
5077 } else if (strstr(cmd, "blink")) {
5078 s = TPACPI_LED_BLINK;
5083 rc = led_set_status(led, s);
5091 static struct ibm_struct led_driver_data = {
5098 /*************************************************************************
5102 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5104 static int __init beep_init(struct ibm_init_struct *iibm)
5106 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5108 TPACPI_ACPIHANDLE_INIT(beep);
5110 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5111 str_supported(beep_handle != NULL));
5113 return (beep_handle)? 0 : 1;
5116 static int beep_read(char *p)
5121 len += sprintf(p + len, "status:\t\tnot supported\n");
5123 len += sprintf(p + len, "status:\t\tsupported\n");
5124 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5130 static int beep_write(char *buf)
5138 while ((cmd = next_cmd(&buf))) {
5139 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5140 beep_cmd >= 0 && beep_cmd <= 17) {
5144 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5151 static struct ibm_struct beep_driver_data = {
5154 .write = beep_write,
5157 /*************************************************************************
5161 enum thermal_access_mode {
5162 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5163 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5164 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5165 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5166 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5169 enum { /* TPACPI_THERMAL_TPEC_* */
5170 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5171 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5172 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5175 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5176 struct ibm_thermal_sensors_struct {
5177 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5180 static enum thermal_access_mode thermal_read_mode;
5182 /* idx is zero-based */
5183 static int thermal_get_sensor(int idx, s32 *value)
5189 t = TP_EC_THERMAL_TMP0;
5191 switch (thermal_read_mode) {
5192 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5193 case TPACPI_THERMAL_TPEC_16:
5194 if (idx >= 8 && idx <= 15) {
5195 t = TP_EC_THERMAL_TMP8;
5200 case TPACPI_THERMAL_TPEC_8:
5202 if (!acpi_ec_read(t + idx, &tmp))
5204 *value = tmp * 1000;
5209 case TPACPI_THERMAL_ACPI_UPDT:
5211 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5212 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5214 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5216 *value = (t - 2732) * 100;
5221 case TPACPI_THERMAL_ACPI_TMP07:
5223 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5224 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5226 if (t > 127 || t < -127)
5227 t = TP_EC_THERMAL_TMP_NA;
5233 case TPACPI_THERMAL_NONE:
5241 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5252 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5255 for (i = 0 ; i < n; i++) {
5256 res = thermal_get_sensor(i, &s->temp[i]);
5264 /* sysfs temp##_input -------------------------------------------------- */
5266 static ssize_t thermal_temp_input_show(struct device *dev,
5267 struct device_attribute *attr,
5270 struct sensor_device_attribute *sensor_attr =
5271 to_sensor_dev_attr(attr);
5272 int idx = sensor_attr->index;
5276 res = thermal_get_sensor(idx, &value);
5279 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5282 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5285 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5286 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5287 thermal_temp_input_show, NULL, _idxB)
5289 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5290 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5291 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5292 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5293 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5294 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5295 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5296 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5297 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5298 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5299 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5300 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5301 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5302 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5303 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5304 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5305 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5308 #define THERMAL_ATTRS(X) \
5309 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5311 static struct attribute *thermal_temp_input_attr[] = {
5331 static const struct attribute_group thermal_temp_input16_group = {
5332 .attrs = thermal_temp_input_attr
5335 static const struct attribute_group thermal_temp_input8_group = {
5336 .attrs = &thermal_temp_input_attr[8]
5339 #undef THERMAL_SENSOR_ATTR_TEMP
5340 #undef THERMAL_ATTRS
5342 /* --------------------------------------------------------------------- */
5344 static int __init thermal_init(struct ibm_init_struct *iibm)
5351 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5353 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5355 if (thinkpad_id.ec_model) {
5357 * Direct EC access mode: sensors at registers
5358 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5359 * non-implemented, thermal sensors return 0x80 when
5364 for (i = 0; i < 8; i++) {
5365 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5371 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5379 /* This is sheer paranoia, but we handle it anyway */
5382 "ThinkPad ACPI EC access misbehaving, "
5383 "falling back to ACPI TMPx access "
5385 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5388 "ThinkPad ACPI EC access misbehaving, "
5389 "disabling thermal sensors access\n");
5390 thermal_read_mode = TPACPI_THERMAL_NONE;
5395 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5397 } else if (acpi_tmp7) {
5398 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5399 /* 600e/x, 770e, 770x */
5400 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5402 /* Standard ACPI TMPx access, max 8 sensors */
5403 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5406 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5407 thermal_read_mode = TPACPI_THERMAL_NONE;
5410 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5411 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5414 switch (thermal_read_mode) {
5415 case TPACPI_THERMAL_TPEC_16:
5416 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5417 &thermal_temp_input16_group);
5421 case TPACPI_THERMAL_TPEC_8:
5422 case TPACPI_THERMAL_ACPI_TMP07:
5423 case TPACPI_THERMAL_ACPI_UPDT:
5424 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5425 &thermal_temp_input8_group);
5429 case TPACPI_THERMAL_NONE:
5437 static void thermal_exit(void)
5439 switch (thermal_read_mode) {
5440 case TPACPI_THERMAL_TPEC_16:
5441 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5442 &thermal_temp_input16_group);
5444 case TPACPI_THERMAL_TPEC_8:
5445 case TPACPI_THERMAL_ACPI_TMP07:
5446 case TPACPI_THERMAL_ACPI_UPDT:
5447 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5448 &thermal_temp_input16_group);
5450 case TPACPI_THERMAL_NONE:
5456 static int thermal_read(char *p)
5460 struct ibm_thermal_sensors_struct t;
5462 n = thermal_get_sensors(&t);
5463 if (unlikely(n < 0))
5466 len += sprintf(p + len, "temperatures:\t");
5469 for (i = 0; i < (n - 1); i++)
5470 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5471 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5473 len += sprintf(p + len, "not supported\n");
5478 static struct ibm_struct thermal_driver_data = {
5480 .read = thermal_read,
5481 .exit = thermal_exit,
5484 /*************************************************************************
5488 static u8 ecdump_regs[256];
5490 static int ecdump_read(char *p)
5496 len += sprintf(p + len, "EC "
5497 " +00 +01 +02 +03 +04 +05 +06 +07"
5498 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5499 for (i = 0; i < 256; i += 16) {
5500 len += sprintf(p + len, "EC 0x%02x:", i);
5501 for (j = 0; j < 16; j++) {
5502 if (!acpi_ec_read(i + j, &v))
5504 if (v != ecdump_regs[i + j])
5505 len += sprintf(p + len, " *%02x", v);
5507 len += sprintf(p + len, " %02x", v);
5508 ecdump_regs[i + j] = v;
5510 len += sprintf(p + len, "\n");
5515 /* These are way too dangerous to advertise openly... */
5517 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5518 " (<offset> is 00-ff, <value> is 00-ff)\n");
5519 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5520 " (<offset> is 00-ff, <value> is 0-255)\n");
5525 static int ecdump_write(char *buf)
5530 while ((cmd = next_cmd(&buf))) {
5531 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5533 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5537 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5538 if (!acpi_ec_write(i, v))
5547 static struct ibm_struct ecdump_driver_data = {
5549 .read = ecdump_read,
5550 .write = ecdump_write,
5551 .flags.experimental = 1,
5554 /*************************************************************************
5555 * Backlight/brightness subdriver
5558 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5561 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5562 * CMOS NVRAM byte 0x5E, bits 0-3.
5564 * EC HBRV (0x31) has the following layout
5565 * Bit 7: unknown function
5566 * Bit 6: unknown function
5567 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5568 * Bit 4: must be set to zero to avoid problems
5569 * Bit 3-0: backlight brightness level
5571 * brightness_get_raw returns status data in the HBRV layout
5575 TP_EC_BACKLIGHT = 0x31,
5577 /* TP_EC_BACKLIGHT bitmasks */
5578 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5579 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5580 TP_EC_BACKLIGHT_MAPSW = 0x20,
5583 enum tpacpi_brightness_access_mode {
5584 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5585 TPACPI_BRGHT_MODE_EC, /* EC control */
5586 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5587 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5588 TPACPI_BRGHT_MODE_MAX
5591 static struct backlight_device *ibm_backlight_device;
5593 static enum tpacpi_brightness_access_mode brightness_mode =
5594 TPACPI_BRGHT_MODE_MAX;
5596 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5598 static struct mutex brightness_mutex;
5600 /* NVRAM brightness access,
5601 * call with brightness_mutex held! */
5602 static unsigned int tpacpi_brightness_nvram_get(void)
5606 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5607 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5608 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5609 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5614 static void tpacpi_brightness_checkpoint_nvram(void)
5619 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5622 vdbg_printk(TPACPI_DBG_BRGHT,
5623 "trying to checkpoint backlight level to NVRAM...\n");
5625 if (mutex_lock_killable(&brightness_mutex) < 0)
5628 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5630 lec &= TP_EC_BACKLIGHT_LVLMSK;
5631 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5633 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5634 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5635 /* NVRAM needs update */
5636 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5637 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5639 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5640 dbg_printk(TPACPI_DBG_BRGHT,
5641 "updated NVRAM backlight level to %u (0x%02x)\n",
5642 (unsigned int) lec, (unsigned int) b_nvram);
5644 vdbg_printk(TPACPI_DBG_BRGHT,
5645 "NVRAM backlight level already is %u (0x%02x)\n",
5646 (unsigned int) lec, (unsigned int) b_nvram);
5649 mutex_unlock(&brightness_mutex);
5653 /* call with brightness_mutex held! */
5654 static int tpacpi_brightness_get_raw(int *status)
5658 switch (brightness_mode) {
5659 case TPACPI_BRGHT_MODE_UCMS_STEP:
5660 *status = tpacpi_brightness_nvram_get();
5662 case TPACPI_BRGHT_MODE_EC:
5663 case TPACPI_BRGHT_MODE_ECNVRAM:
5664 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5673 /* call with brightness_mutex held! */
5674 /* do NOT call with illegal backlight level value */
5675 static int tpacpi_brightness_set_ec(unsigned int value)
5679 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5682 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5683 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5684 (value & TP_EC_BACKLIGHT_LVLMSK))))
5690 /* call with brightness_mutex held! */
5691 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5694 unsigned int current_value, i;
5696 current_value = tpacpi_brightness_nvram_get();
5698 if (value == current_value)
5701 cmos_cmd = (value > current_value) ?
5702 TP_CMOS_BRIGHTNESS_UP :
5703 TP_CMOS_BRIGHTNESS_DOWN;
5704 inc = (value > current_value) ? 1 : -1;
5706 for (i = current_value; i != value; i += inc)
5707 if (issue_thinkpad_cmos_command(cmos_cmd))
5713 /* May return EINTR which can always be mapped to ERESTARTSYS */
5714 static int brightness_set(unsigned int value)
5718 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5722 vdbg_printk(TPACPI_DBG_BRGHT,
5723 "set backlight level to %d\n", value);
5725 res = mutex_lock_killable(&brightness_mutex);
5729 switch (brightness_mode) {
5730 case TPACPI_BRGHT_MODE_EC:
5731 case TPACPI_BRGHT_MODE_ECNVRAM:
5732 res = tpacpi_brightness_set_ec(value);
5734 case TPACPI_BRGHT_MODE_UCMS_STEP:
5735 res = tpacpi_brightness_set_ucmsstep(value);
5741 mutex_unlock(&brightness_mutex);
5745 /* sysfs backlight class ----------------------------------------------- */
5747 static int brightness_update_status(struct backlight_device *bd)
5749 unsigned int level =
5750 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5751 bd->props.power == FB_BLANK_UNBLANK) ?
5752 bd->props.brightness : 0;
5754 dbg_printk(TPACPI_DBG_BRGHT,
5755 "backlight: attempt to set level to %d\n",
5758 /* it is the backlight class's job (caller) to handle
5759 * EINTR and other errors properly */
5760 return brightness_set(level);
5763 static int brightness_get(struct backlight_device *bd)
5767 res = mutex_lock_killable(&brightness_mutex);
5771 res = tpacpi_brightness_get_raw(&status);
5773 mutex_unlock(&brightness_mutex);
5778 return status & TP_EC_BACKLIGHT_LVLMSK;
5781 static struct backlight_ops ibm_backlight_data = {
5782 .get_brightness = brightness_get,
5783 .update_status = brightness_update_status,
5786 /* --------------------------------------------------------------------- */
5788 static int __init brightness_init(struct ibm_init_struct *iibm)
5792 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5794 mutex_init(&brightness_mutex);
5797 * We always attempt to detect acpi support, so as to switch
5798 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5799 * going to publish a backlight interface
5801 b = tpacpi_check_std_acpi_brightness_support();
5804 if (acpi_video_backlight_support()) {
5805 if (brightness_enable > 1) {
5806 printk(TPACPI_NOTICE
5807 "Standard ACPI backlight interface "
5808 "available, not loading native one.\n");
5810 } else if (brightness_enable == 1) {
5811 printk(TPACPI_NOTICE
5812 "Backlight control force enabled, even if standard "
5813 "ACPI backlight interface is available\n");
5816 if (brightness_enable > 1) {
5817 printk(TPACPI_NOTICE
5818 "Standard ACPI backlight interface not "
5819 "available, thinkpad_acpi native "
5820 "brightness control enabled\n");
5825 if (!brightness_enable) {
5826 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5827 "brightness support disabled by "
5828 "module parameter\n");
5834 "Unsupported brightness interface, "
5835 "please contact %s\n", TPACPI_MAIL);
5839 tp_features.bright_16levels = 1;
5842 * Check for module parameter bogosity, note that we
5843 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5844 * able to detect "unspecified"
5846 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5849 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5850 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5851 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5852 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5854 * IBM models that define HBRV probably have
5855 * EC-based backlight level control
5857 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5858 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5859 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5861 /* all other IBM ThinkPads */
5862 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5864 /* All Lenovo ThinkPads */
5865 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5867 dbg_printk(TPACPI_DBG_BRGHT,
5868 "selected brightness_mode=%d\n",
5872 if (tpacpi_brightness_get_raw(&b) < 0)
5875 if (tp_features.bright_16levels)
5877 "detected a 16-level brightness capable ThinkPad\n");
5879 ibm_backlight_device = backlight_device_register(
5880 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5881 &ibm_backlight_data);
5882 if (IS_ERR(ibm_backlight_device)) {
5883 printk(TPACPI_ERR "Could not register backlight device\n");
5884 return PTR_ERR(ibm_backlight_device);
5886 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5887 "brightness is supported\n");
5889 ibm_backlight_device->props.max_brightness =
5890 (tp_features.bright_16levels)? 15 : 7;
5891 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5892 backlight_update_status(ibm_backlight_device);
5897 static void brightness_suspend(pm_message_t state)
5899 tpacpi_brightness_checkpoint_nvram();
5902 static void brightness_shutdown(void)
5904 tpacpi_brightness_checkpoint_nvram();
5907 static void brightness_exit(void)
5909 if (ibm_backlight_device) {
5910 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5911 "calling backlight_device_unregister()\n");
5912 backlight_device_unregister(ibm_backlight_device);
5915 tpacpi_brightness_checkpoint_nvram();
5918 static int brightness_read(char *p)
5923 level = brightness_get(NULL);
5925 len += sprintf(p + len, "level:\t\tunreadable\n");
5927 len += sprintf(p + len, "level:\t\t%d\n", level);
5928 len += sprintf(p + len, "commands:\tup, down\n");
5929 len += sprintf(p + len, "commands:\tlevel <level>"
5930 " (<level> is 0-%d)\n",
5931 (tp_features.bright_16levels) ? 15 : 7);
5937 static int brightness_write(char *buf)
5942 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5944 level = brightness_get(NULL);
5948 while ((cmd = next_cmd(&buf))) {
5949 if (strlencmp(cmd, "up") == 0) {
5950 if (level < max_level)
5952 } else if (strlencmp(cmd, "down") == 0) {
5955 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5956 level >= 0 && level <= max_level) {
5962 tpacpi_disclose_usertask("procfs brightness",
5963 "set level to %d\n", level);
5966 * Now we know what the final level should be, so we try to set it.
5967 * Doing it this way makes the syscall restartable in case of EINTR
5969 rc = brightness_set(level);
5970 return (rc == -EINTR)? ERESTARTSYS : rc;
5973 static struct ibm_struct brightness_driver_data = {
5974 .name = "brightness",
5975 .read = brightness_read,
5976 .write = brightness_write,
5977 .exit = brightness_exit,
5978 .suspend = brightness_suspend,
5979 .shutdown = brightness_shutdown,
5982 /*************************************************************************
5986 static int volume_offset = 0x30;
5988 static int volume_read(char *p)
5993 if (!acpi_ec_read(volume_offset, &level)) {
5994 len += sprintf(p + len, "level:\t\tunreadable\n");
5996 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5997 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5998 len += sprintf(p + len, "commands:\tup, down, mute\n");
5999 len += sprintf(p + len, "commands:\tlevel <level>"
6000 " (<level> is 0-15)\n");
6006 static int volume_write(char *buf)
6008 int cmos_cmd, inc, i;
6010 int new_level, new_mute;
6013 while ((cmd = next_cmd(&buf))) {
6014 if (!acpi_ec_read(volume_offset, &level))
6016 new_mute = mute = level & 0x40;
6017 new_level = level = level & 0xf;
6019 if (strlencmp(cmd, "up") == 0) {
6023 new_level = level == 15 ? 15 : level + 1;
6024 } else if (strlencmp(cmd, "down") == 0) {
6028 new_level = level == 0 ? 0 : level - 1;
6029 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6030 new_level >= 0 && new_level <= 15) {
6032 } else if (strlencmp(cmd, "mute") == 0) {
6037 if (new_level != level) {
6038 /* mute doesn't change */
6040 cmos_cmd = (new_level > level) ?
6041 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6042 inc = new_level > level ? 1 : -1;
6044 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6045 !acpi_ec_write(volume_offset, level)))
6048 for (i = level; i != new_level; i += inc)
6049 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6050 !acpi_ec_write(volume_offset, i + inc))
6054 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6055 !acpi_ec_write(volume_offset, new_level + mute))) {
6060 if (new_mute != mute) {
6061 /* level doesn't change */
6063 cmos_cmd = (new_mute) ?
6064 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6066 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6067 !acpi_ec_write(volume_offset, level + new_mute))
6075 static struct ibm_struct volume_driver_data = {
6077 .read = volume_read,
6078 .write = volume_write,
6081 /*************************************************************************
6088 * TPACPI_FAN_RD_ACPI_GFAN:
6089 * ACPI GFAN method: returns fan level
6091 * see TPACPI_FAN_WR_ACPI_SFAN
6092 * EC 0x2f (HFSP) not available if GFAN exists
6094 * TPACPI_FAN_WR_ACPI_SFAN:
6095 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6097 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6100 * TPACPI_FAN_WR_TPEC:
6101 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6102 * Supported on almost all ThinkPads
6104 * Fan speed changes of any sort (including those caused by the
6105 * disengaged mode) are usually done slowly by the firmware as the
6106 * maximum ammount of fan duty cycle change per second seems to be
6109 * Reading is not available if GFAN exists.
6110 * Writing is not available if SFAN exists.
6113 * 7 automatic mode engaged;
6114 * (default operation mode of the ThinkPad)
6115 * fan level is ignored in this mode.
6116 * 6 full speed mode (takes precedence over bit 7);
6117 * not available on all thinkpads. May disable
6118 * the tachometer while the fan controller ramps up
6119 * the speed (which can take up to a few *minutes*).
6120 * Speeds up fan to 100% duty-cycle, which is far above
6121 * the standard RPM levels. It is not impossible that
6122 * it could cause hardware damage.
6123 * 5-3 unused in some models. Extra bits for fan level
6124 * in others, but still useless as all values above
6125 * 7 map to the same speed as level 7 in these models.
6126 * 2-0 fan level (0..7 usually)
6128 * 0x07 = max (set when temperatures critical)
6129 * Some ThinkPads may have other levels, see
6130 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6132 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6133 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6134 * does so, its initial value is meaningless (0x07).
6136 * For firmware bugs, refer to:
6137 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6141 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6142 * Main fan tachometer reading (in RPM)
6144 * This register is present on all ThinkPads with a new-style EC, and
6145 * it is known not to be present on the A21m/e, and T22, as there is
6146 * something else in offset 0x84 according to the ACPI DSDT. Other
6147 * ThinkPads from this same time period (and earlier) probably lack the
6148 * tachometer as well.
6150 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6151 * was never fixed by IBM to report the EC firmware version string
6152 * probably support the tachometer (like the early X models), so
6153 * detecting it is quite hard. We need more data to know for sure.
6155 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6158 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6161 * For firmware bugs, refer to:
6162 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6164 * TPACPI_FAN_WR_ACPI_FANS:
6165 * ThinkPad X31, X40, X41. Not available in the X60.
6167 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6168 * high speed. ACPI DSDT seems to map these three speeds to levels
6169 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6170 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6172 * The speeds are stored on handles
6173 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6175 * There are three default speed sets, acessible as handles:
6176 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6178 * ACPI DSDT switches which set is in use depending on various
6181 * TPACPI_FAN_WR_TPEC is also available and should be used to
6182 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6183 * but the ACPI tables just mention level 7.
6186 enum { /* Fan control constants */
6187 fan_status_offset = 0x2f, /* EC register 0x2f */
6188 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6189 * 0x84 must be read before 0x85 */
6191 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6192 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6194 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6197 enum fan_status_access_mode {
6198 TPACPI_FAN_NONE = 0, /* No fan status or control */
6199 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6200 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6203 enum fan_control_access_mode {
6204 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6205 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6206 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6207 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6210 enum fan_control_commands {
6211 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6212 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6213 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6214 * and also watchdog cmd */
6217 static int fan_control_allowed;
6219 static enum fan_status_access_mode fan_status_access_mode;
6220 static enum fan_control_access_mode fan_control_access_mode;
6221 static enum fan_control_commands fan_control_commands;
6223 static u8 fan_control_initial_status;
6224 static u8 fan_control_desired_level;
6225 static u8 fan_control_resume_level;
6226 static int fan_watchdog_maxinterval;
6228 static struct mutex fan_mutex;
6230 static void fan_watchdog_fire(struct work_struct *ignored);
6231 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6233 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6234 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6235 "\\FSPD", /* 600e/x, 770e, 770x */
6237 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6238 "JFNS", /* 770x-JL */
6242 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6243 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6244 * be in auto mode (0x80).
6246 * This is corrected by any write to HFSP either by the driver, or
6249 * We assume 0x07 really means auto mode while this quirk is active,
6250 * as this is far more likely than the ThinkPad being in level 7,
6251 * which is only used by the firmware during thermal emergencies.
6254 static void fan_quirk1_detect(void)
6256 /* In some ThinkPads, neither the EC nor the ACPI
6257 * DSDT initialize the HFSP register, and it ends up
6258 * being initially set to 0x07 when it *could* be
6259 * either 0x07 or 0x80.
6261 * Enable for TP-1Y (T43), TP-78 (R51e),
6262 * TP-76 (R52), TP-70 (T43, R52), which are known
6264 if (fan_control_initial_status == 0x07) {
6265 switch (thinkpad_id.ec_model) {
6266 case 0x5931: /* TP-1Y */
6267 case 0x3837: /* TP-78 */
6268 case 0x3637: /* TP-76 */
6269 case 0x3037: /* TP-70 */
6270 printk(TPACPI_NOTICE
6271 "fan_init: initial fan status is unknown, "
6272 "assuming it is in auto mode\n");
6273 tp_features.fan_ctrl_status_undef = 1;
6279 static void fan_quirk1_handle(u8 *fan_status)
6281 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6282 if (*fan_status != fan_control_initial_status) {
6283 /* something changed the HFSP regisnter since
6284 * driver init time, so it is not undefined
6286 tp_features.fan_ctrl_status_undef = 0;
6288 /* Return most likely status. In fact, it
6289 * might be the only possible status */
6290 *fan_status = TP_EC_FAN_AUTO;
6296 * Call with fan_mutex held
6298 static void fan_update_desired_level(u8 status)
6301 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6303 fan_control_desired_level = 7;
6305 fan_control_desired_level = status;
6309 static int fan_get_status(u8 *status)
6314 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6316 switch (fan_status_access_mode) {
6317 case TPACPI_FAN_RD_ACPI_GFAN:
6318 /* 570, 600e/x, 770e, 770x */
6320 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6328 case TPACPI_FAN_RD_TPEC:
6329 /* all except 570, 600e/x, 770e, 770x */
6330 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6333 if (likely(status)) {
6335 fan_quirk1_handle(status);
6347 static int fan_get_status_safe(u8 *status)
6352 if (mutex_lock_killable(&fan_mutex))
6353 return -ERESTARTSYS;
6354 rc = fan_get_status(&s);
6356 fan_update_desired_level(s);
6357 mutex_unlock(&fan_mutex);
6365 static int fan_get_speed(unsigned int *speed)
6369 switch (fan_status_access_mode) {
6370 case TPACPI_FAN_RD_TPEC:
6371 /* all except 570, 600e/x, 770e, 770x */
6372 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6373 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6377 *speed = (hi << 8) | lo;
6388 static int fan_set_level(int level)
6390 if (!fan_control_allowed)
6393 switch (fan_control_access_mode) {
6394 case TPACPI_FAN_WR_ACPI_SFAN:
6395 if (level >= 0 && level <= 7) {
6396 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6402 case TPACPI_FAN_WR_ACPI_FANS:
6403 case TPACPI_FAN_WR_TPEC:
6404 if (!(level & TP_EC_FAN_AUTO) &&
6405 !(level & TP_EC_FAN_FULLSPEED) &&
6406 ((level < 0) || (level > 7)))
6409 /* safety net should the EC not support AUTO
6410 * or FULLSPEED mode bits and just ignore them */
6411 if (level & TP_EC_FAN_FULLSPEED)
6412 level |= 7; /* safety min speed 7 */
6413 else if (level & TP_EC_FAN_AUTO)
6414 level |= 4; /* safety min speed 4 */
6416 if (!acpi_ec_write(fan_status_offset, level))
6419 tp_features.fan_ctrl_status_undef = 0;
6426 vdbg_printk(TPACPI_DBG_FAN,
6427 "fan control: set fan control register to 0x%02x\n", level);
6431 static int fan_set_level_safe(int level)
6435 if (!fan_control_allowed)
6438 if (mutex_lock_killable(&fan_mutex))
6439 return -ERESTARTSYS;
6441 if (level == TPACPI_FAN_LAST_LEVEL)
6442 level = fan_control_desired_level;
6444 rc = fan_set_level(level);
6446 fan_update_desired_level(level);
6448 mutex_unlock(&fan_mutex);
6452 static int fan_set_enable(void)
6457 if (!fan_control_allowed)
6460 if (mutex_lock_killable(&fan_mutex))
6461 return -ERESTARTSYS;
6463 switch (fan_control_access_mode) {
6464 case TPACPI_FAN_WR_ACPI_FANS:
6465 case TPACPI_FAN_WR_TPEC:
6466 rc = fan_get_status(&s);
6470 /* Don't go out of emergency fan mode */
6473 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6476 if (!acpi_ec_write(fan_status_offset, s))
6479 tp_features.fan_ctrl_status_undef = 0;
6484 case TPACPI_FAN_WR_ACPI_SFAN:
6485 rc = fan_get_status(&s);
6491 /* Set fan to at least level 4 */
6494 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6504 mutex_unlock(&fan_mutex);
6507 vdbg_printk(TPACPI_DBG_FAN,
6508 "fan control: set fan control register to 0x%02x\n",
6513 static int fan_set_disable(void)
6517 if (!fan_control_allowed)
6520 if (mutex_lock_killable(&fan_mutex))
6521 return -ERESTARTSYS;
6524 switch (fan_control_access_mode) {
6525 case TPACPI_FAN_WR_ACPI_FANS:
6526 case TPACPI_FAN_WR_TPEC:
6527 if (!acpi_ec_write(fan_status_offset, 0x00))
6530 fan_control_desired_level = 0;
6531 tp_features.fan_ctrl_status_undef = 0;
6535 case TPACPI_FAN_WR_ACPI_SFAN:
6536 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6539 fan_control_desired_level = 0;
6547 vdbg_printk(TPACPI_DBG_FAN,
6548 "fan control: set fan control register to 0\n");
6550 mutex_unlock(&fan_mutex);
6554 static int fan_set_speed(int speed)
6558 if (!fan_control_allowed)
6561 if (mutex_lock_killable(&fan_mutex))
6562 return -ERESTARTSYS;
6565 switch (fan_control_access_mode) {
6566 case TPACPI_FAN_WR_ACPI_FANS:
6567 if (speed >= 0 && speed <= 65535) {
6568 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6569 speed, speed, speed))
6579 mutex_unlock(&fan_mutex);
6583 static void fan_watchdog_reset(void)
6585 static int fan_watchdog_active;
6587 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6590 if (fan_watchdog_active)
6591 cancel_delayed_work(&fan_watchdog_task);
6593 if (fan_watchdog_maxinterval > 0 &&
6594 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6595 fan_watchdog_active = 1;
6596 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6597 msecs_to_jiffies(fan_watchdog_maxinterval
6600 "failed to queue the fan watchdog, "
6601 "watchdog will not trigger\n");
6604 fan_watchdog_active = 0;
6607 static void fan_watchdog_fire(struct work_struct *ignored)
6611 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6614 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6615 rc = fan_set_enable();
6617 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6618 "will try again later...\n", -rc);
6619 /* reschedule for later */
6620 fan_watchdog_reset();
6625 * SYSFS fan layout: hwmon compatible (device)
6628 * 0: "disengaged" mode
6630 * 2: native EC "auto" mode (recommended, hardware default)
6632 * pwm*: set speed in manual mode, ignored otherwise.
6633 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6636 * fan*_input: tachometer reading, RPM
6639 * SYSFS fan layout: extensions
6641 * fan_watchdog (driver):
6642 * fan watchdog interval in seconds, 0 disables (default), max 120
6645 /* sysfs fan pwm1_enable ----------------------------------------------- */
6646 static ssize_t fan_pwm1_enable_show(struct device *dev,
6647 struct device_attribute *attr,
6653 res = fan_get_status_safe(&status);
6657 if (status & TP_EC_FAN_FULLSPEED) {
6659 } else if (status & TP_EC_FAN_AUTO) {
6664 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6667 static ssize_t fan_pwm1_enable_store(struct device *dev,
6668 struct device_attribute *attr,
6669 const char *buf, size_t count)
6674 if (parse_strtoul(buf, 2, &t))
6677 tpacpi_disclose_usertask("hwmon pwm1_enable",
6678 "set fan mode to %lu\n", t);
6682 level = TP_EC_FAN_FULLSPEED;
6685 level = TPACPI_FAN_LAST_LEVEL;
6688 level = TP_EC_FAN_AUTO;
6691 /* reserved for software-controlled auto mode */
6697 res = fan_set_level_safe(level);
6703 fan_watchdog_reset();
6708 static struct device_attribute dev_attr_fan_pwm1_enable =
6709 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6710 fan_pwm1_enable_show, fan_pwm1_enable_store);
6712 /* sysfs fan pwm1 ------------------------------------------------------ */
6713 static ssize_t fan_pwm1_show(struct device *dev,
6714 struct device_attribute *attr,
6720 res = fan_get_status_safe(&status);
6725 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6726 status = fan_control_desired_level;
6731 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6734 static ssize_t fan_pwm1_store(struct device *dev,
6735 struct device_attribute *attr,
6736 const char *buf, size_t count)
6740 u8 status, newlevel;
6742 if (parse_strtoul(buf, 255, &s))
6745 tpacpi_disclose_usertask("hwmon pwm1",
6746 "set fan speed to %lu\n", s);
6748 /* scale down from 0-255 to 0-7 */
6749 newlevel = (s >> 5) & 0x07;
6751 if (mutex_lock_killable(&fan_mutex))
6752 return -ERESTARTSYS;
6754 rc = fan_get_status(&status);
6755 if (!rc && (status &
6756 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6757 rc = fan_set_level(newlevel);
6761 fan_update_desired_level(newlevel);
6762 fan_watchdog_reset();
6766 mutex_unlock(&fan_mutex);
6767 return (rc)? rc : count;
6770 static struct device_attribute dev_attr_fan_pwm1 =
6771 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6772 fan_pwm1_show, fan_pwm1_store);
6774 /* sysfs fan fan1_input ------------------------------------------------ */
6775 static ssize_t fan_fan1_input_show(struct device *dev,
6776 struct device_attribute *attr,
6782 res = fan_get_speed(&speed);
6786 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6789 static struct device_attribute dev_attr_fan_fan1_input =
6790 __ATTR(fan1_input, S_IRUGO,
6791 fan_fan1_input_show, NULL);
6793 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6794 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6797 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6800 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6801 const char *buf, size_t count)
6805 if (parse_strtoul(buf, 120, &t))
6808 if (!fan_control_allowed)
6811 fan_watchdog_maxinterval = t;
6812 fan_watchdog_reset();
6814 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6819 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6820 fan_fan_watchdog_show, fan_fan_watchdog_store);
6822 /* --------------------------------------------------------------------- */
6823 static struct attribute *fan_attributes[] = {
6824 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6825 &dev_attr_fan_fan1_input.attr,
6829 static const struct attribute_group fan_attr_group = {
6830 .attrs = fan_attributes,
6833 static int __init fan_init(struct ibm_init_struct *iibm)
6837 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6838 "initializing fan subdriver\n");
6840 mutex_init(&fan_mutex);
6841 fan_status_access_mode = TPACPI_FAN_NONE;
6842 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6843 fan_control_commands = 0;
6844 fan_watchdog_maxinterval = 0;
6845 tp_features.fan_ctrl_status_undef = 0;
6846 fan_control_desired_level = 7;
6848 TPACPI_ACPIHANDLE_INIT(fans);
6849 TPACPI_ACPIHANDLE_INIT(gfan);
6850 TPACPI_ACPIHANDLE_INIT(sfan);
6853 /* 570, 600e/x, 770e, 770x */
6854 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6856 /* all other ThinkPads: note that even old-style
6857 * ThinkPad ECs supports the fan control register */
6858 if (likely(acpi_ec_read(fan_status_offset,
6859 &fan_control_initial_status))) {
6860 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6861 fan_quirk1_detect();
6864 "ThinkPad ACPI EC access misbehaving, "
6865 "fan status and control unavailable\n");
6872 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6873 fan_control_commands |=
6874 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6877 /* gfan without sfan means no fan control */
6878 /* all other models implement TP EC 0x2f control */
6882 fan_control_access_mode =
6883 TPACPI_FAN_WR_ACPI_FANS;
6884 fan_control_commands |=
6885 TPACPI_FAN_CMD_SPEED |
6886 TPACPI_FAN_CMD_LEVEL |
6887 TPACPI_FAN_CMD_ENABLE;
6889 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6890 fan_control_commands |=
6891 TPACPI_FAN_CMD_LEVEL |
6892 TPACPI_FAN_CMD_ENABLE;
6897 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6898 "fan is %s, modes %d, %d\n",
6899 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6900 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6901 fan_status_access_mode, fan_control_access_mode);
6903 /* fan control master switch */
6904 if (!fan_control_allowed) {
6905 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6906 fan_control_commands = 0;
6907 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6908 "fan control features disabled by parameter\n");
6911 /* update fan_control_desired_level */
6912 if (fan_status_access_mode != TPACPI_FAN_NONE)
6913 fan_get_status_safe(NULL);
6915 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6916 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6917 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6922 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6923 &driver_attr_fan_watchdog);
6925 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6934 static void fan_exit(void)
6936 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
6937 "cancelling any pending fan watchdog tasks\n");
6939 /* FIXME: can we really do this unconditionally? */
6940 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6941 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6942 &driver_attr_fan_watchdog);
6944 cancel_delayed_work(&fan_watchdog_task);
6945 flush_workqueue(tpacpi_wq);
6948 static void fan_suspend(pm_message_t state)
6952 if (!fan_control_allowed)
6955 /* Store fan status in cache */
6956 fan_control_resume_level = 0;
6957 rc = fan_get_status_safe(&fan_control_resume_level);
6959 printk(TPACPI_NOTICE
6960 "failed to read fan level for later "
6961 "restore during resume: %d\n", rc);
6963 /* if it is undefined, don't attempt to restore it.
6965 if (tp_features.fan_ctrl_status_undef)
6966 fan_control_resume_level = 0;
6969 static void fan_resume(void)
6971 u8 current_level = 7;
6972 bool do_set = false;
6975 /* DSDT *always* updates status on resume */
6976 tp_features.fan_ctrl_status_undef = 0;
6978 if (!fan_control_allowed ||
6979 !fan_control_resume_level ||
6980 (fan_get_status_safe(¤t_level) < 0))
6983 switch (fan_control_access_mode) {
6984 case TPACPI_FAN_WR_ACPI_SFAN:
6985 /* never decrease fan level */
6986 do_set = (fan_control_resume_level > current_level);
6988 case TPACPI_FAN_WR_ACPI_FANS:
6989 case TPACPI_FAN_WR_TPEC:
6990 /* never decrease fan level, scale is:
6991 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6993 * We expect the firmware to set either 7 or AUTO, but we
6994 * handle FULLSPEED out of paranoia.
6996 * So, we can safely only restore FULLSPEED or 7, anything
6997 * else could slow the fan. Restoring AUTO is useless, at
6998 * best that's exactly what the DSDT already set (it is the
7001 * Always keep in mind that the DSDT *will* have set the
7002 * fans to what the vendor supposes is the best level. We
7003 * muck with it only to speed the fan up.
7005 if (fan_control_resume_level != 7 &&
7006 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7009 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7010 (current_level != fan_control_resume_level);
7016 printk(TPACPI_NOTICE
7017 "restoring fan level to 0x%02x\n",
7018 fan_control_resume_level);
7019 rc = fan_set_level_safe(fan_control_resume_level);
7021 printk(TPACPI_NOTICE
7022 "failed to restore fan level: %d\n", rc);
7026 static int fan_read(char *p)
7031 unsigned int speed = 0;
7033 switch (fan_status_access_mode) {
7034 case TPACPI_FAN_RD_ACPI_GFAN:
7035 /* 570, 600e/x, 770e, 770x */
7036 rc = fan_get_status_safe(&status);
7040 len += sprintf(p + len, "status:\t\t%s\n"
7042 (status != 0) ? "enabled" : "disabled", status);
7045 case TPACPI_FAN_RD_TPEC:
7046 /* all except 570, 600e/x, 770e, 770x */
7047 rc = fan_get_status_safe(&status);
7051 len += sprintf(p + len, "status:\t\t%s\n",
7052 (status != 0) ? "enabled" : "disabled");
7054 rc = fan_get_speed(&speed);
7058 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7060 if (status & TP_EC_FAN_FULLSPEED)
7061 /* Disengaged mode takes precedence */
7062 len += sprintf(p + len, "level:\t\tdisengaged\n");
7063 else if (status & TP_EC_FAN_AUTO)
7064 len += sprintf(p + len, "level:\t\tauto\n");
7066 len += sprintf(p + len, "level:\t\t%d\n", status);
7069 case TPACPI_FAN_NONE:
7071 len += sprintf(p + len, "status:\t\tnot supported\n");
7074 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7075 len += sprintf(p + len, "commands:\tlevel <level>");
7077 switch (fan_control_access_mode) {
7078 case TPACPI_FAN_WR_ACPI_SFAN:
7079 len += sprintf(p + len, " (<level> is 0-7)\n");
7083 len += sprintf(p + len, " (<level> is 0-7, "
7084 "auto, disengaged, full-speed)\n");
7089 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7090 len += sprintf(p + len, "commands:\tenable, disable\n"
7091 "commands:\twatchdog <timeout> (<timeout> "
7092 "is 0 (off), 1-120 (seconds))\n");
7094 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7095 len += sprintf(p + len, "commands:\tspeed <speed>"
7096 " (<speed> is 0-65535)\n");
7101 static int fan_write_cmd_level(const char *cmd, int *rc)
7105 if (strlencmp(cmd, "level auto") == 0)
7106 level = TP_EC_FAN_AUTO;
7107 else if ((strlencmp(cmd, "level disengaged") == 0) |
7108 (strlencmp(cmd, "level full-speed") == 0))
7109 level = TP_EC_FAN_FULLSPEED;
7110 else if (sscanf(cmd, "level %d", &level) != 1)
7113 *rc = fan_set_level_safe(level);
7115 printk(TPACPI_ERR "level command accepted for unsupported "
7116 "access mode %d", fan_control_access_mode);
7118 tpacpi_disclose_usertask("procfs fan",
7119 "set level to %d\n", level);
7124 static int fan_write_cmd_enable(const char *cmd, int *rc)
7126 if (strlencmp(cmd, "enable") != 0)
7129 *rc = fan_set_enable();
7131 printk(TPACPI_ERR "enable command accepted for unsupported "
7132 "access mode %d", fan_control_access_mode);
7134 tpacpi_disclose_usertask("procfs fan", "enable\n");
7139 static int fan_write_cmd_disable(const char *cmd, int *rc)
7141 if (strlencmp(cmd, "disable") != 0)
7144 *rc = fan_set_disable();
7146 printk(TPACPI_ERR "disable command accepted for unsupported "
7147 "access mode %d", fan_control_access_mode);
7149 tpacpi_disclose_usertask("procfs fan", "disable\n");
7154 static int fan_write_cmd_speed(const char *cmd, int *rc)
7159 * Support speed <low> <medium> <high> ? */
7161 if (sscanf(cmd, "speed %d", &speed) != 1)
7164 *rc = fan_set_speed(speed);
7166 printk(TPACPI_ERR "speed command accepted for unsupported "
7167 "access mode %d", fan_control_access_mode);
7169 tpacpi_disclose_usertask("procfs fan",
7170 "set speed to %d\n", speed);
7175 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7179 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7182 if (interval < 0 || interval > 120)
7185 fan_watchdog_maxinterval = interval;
7186 tpacpi_disclose_usertask("procfs fan",
7187 "set watchdog timer to %d\n",
7194 static int fan_write(char *buf)
7199 while (!rc && (cmd = next_cmd(&buf))) {
7200 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7201 fan_write_cmd_level(cmd, &rc)) &&
7202 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7203 (fan_write_cmd_enable(cmd, &rc) ||
7204 fan_write_cmd_disable(cmd, &rc) ||
7205 fan_write_cmd_watchdog(cmd, &rc))) &&
7206 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7207 fan_write_cmd_speed(cmd, &rc))
7211 fan_watchdog_reset();
7217 static struct ibm_struct fan_driver_data = {
7222 .suspend = fan_suspend,
7223 .resume = fan_resume,
7226 /****************************************************************************
7227 ****************************************************************************
7231 ****************************************************************************
7232 ****************************************************************************/
7234 /* sysfs name ---------------------------------------------------------- */
7235 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7236 struct device_attribute *attr,
7239 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7242 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7243 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7245 /* --------------------------------------------------------------------- */
7248 static struct proc_dir_entry *proc_dir;
7251 * Module and infrastructure proble, init and exit handling
7254 static int force_load;
7256 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7257 static const char * __init str_supported(int is_supported)
7259 static char text_unsupported[] __initdata = "not supported";
7261 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7263 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7265 static void ibm_exit(struct ibm_struct *ibm)
7267 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7269 list_del_init(&ibm->all_drivers);
7271 if (ibm->flags.acpi_notify_installed) {
7272 dbg_printk(TPACPI_DBG_EXIT,
7273 "%s: acpi_remove_notify_handler\n", ibm->name);
7275 acpi_remove_notify_handler(*ibm->acpi->handle,
7277 dispatch_acpi_notify);
7278 ibm->flags.acpi_notify_installed = 0;
7279 ibm->flags.acpi_notify_installed = 0;
7282 if (ibm->flags.proc_created) {
7283 dbg_printk(TPACPI_DBG_EXIT,
7284 "%s: remove_proc_entry\n", ibm->name);
7285 remove_proc_entry(ibm->name, proc_dir);
7286 ibm->flags.proc_created = 0;
7289 if (ibm->flags.acpi_driver_registered) {
7290 dbg_printk(TPACPI_DBG_EXIT,
7291 "%s: acpi_bus_unregister_driver\n", ibm->name);
7293 acpi_bus_unregister_driver(ibm->acpi->driver);
7294 kfree(ibm->acpi->driver);
7295 ibm->acpi->driver = NULL;
7296 ibm->flags.acpi_driver_registered = 0;
7299 if (ibm->flags.init_called && ibm->exit) {
7301 ibm->flags.init_called = 0;
7304 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7307 static int __init ibm_init(struct ibm_init_struct *iibm)
7310 struct ibm_struct *ibm = iibm->data;
7311 struct proc_dir_entry *entry;
7313 BUG_ON(ibm == NULL);
7315 INIT_LIST_HEAD(&ibm->all_drivers);
7317 if (ibm->flags.experimental && !experimental)
7320 dbg_printk(TPACPI_DBG_INIT,
7321 "probing for %s\n", ibm->name);
7324 ret = iibm->init(iibm);
7326 return 0; /* probe failed */
7330 ibm->flags.init_called = 1;
7334 if (ibm->acpi->hid) {
7335 ret = register_tpacpi_subdriver(ibm);
7340 if (ibm->acpi->notify) {
7341 ret = setup_acpi_notify(ibm);
7342 if (ret == -ENODEV) {
7343 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7353 dbg_printk(TPACPI_DBG_INIT,
7354 "%s installed\n", ibm->name);
7357 entry = create_proc_entry(ibm->name,
7358 S_IFREG | S_IRUGO | S_IWUSR,
7361 printk(TPACPI_ERR "unable to create proc entry %s\n",
7367 entry->read_proc = &dispatch_procfs_read;
7369 entry->write_proc = &dispatch_procfs_write;
7370 ibm->flags.proc_created = 1;
7373 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7378 dbg_printk(TPACPI_DBG_INIT,
7379 "%s: at error exit path with result %d\n",
7383 return (ret < 0)? ret : 0;
7388 /* returns 0 - probe ok, or < 0 - probe error.
7389 * Probe ok doesn't mean thinkpad found.
7390 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7391 static int __must_check __init get_thinkpad_model_data(
7392 struct thinkpad_id_data *tp)
7394 const struct dmi_device *dev = NULL;
7395 char ec_fw_string[18];
7401 memset(tp, 0, sizeof(*tp));
7403 if (dmi_name_in_vendors("IBM"))
7404 tp->vendor = PCI_VENDOR_ID_IBM;
7405 else if (dmi_name_in_vendors("LENOVO"))
7406 tp->vendor = PCI_VENDOR_ID_LENOVO;
7410 s = dmi_get_system_info(DMI_BIOS_VERSION);
7411 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7412 if (s && !tp->bios_version_str)
7414 if (!tp->bios_version_str)
7416 tp->bios_model = tp->bios_version_str[0]
7417 | (tp->bios_version_str[1] << 8);
7420 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7421 * X32 or newer, all Z series; Some models must have an
7422 * up-to-date BIOS or they will not be detected.
7424 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7426 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7427 if (sscanf(dev->name,
7428 "IBM ThinkPad Embedded Controller -[%17c",
7429 ec_fw_string) == 1) {
7430 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7431 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7433 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7434 if (!tp->ec_version_str)
7436 tp->ec_model = ec_fw_string[0]
7437 | (ec_fw_string[1] << 8);
7442 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7443 if (s && !strnicmp(s, "ThinkPad", 8)) {
7444 tp->model_str = kstrdup(s, GFP_KERNEL);
7449 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7450 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7451 if (s && !tp->nummodel_str)
7457 static int __init probe_for_thinkpad(void)
7465 * Non-ancient models have better DMI tagging, but very old models
7468 is_thinkpad = (thinkpad_id.model_str != NULL);
7470 /* ec is required because many other handles are relative to it */
7471 TPACPI_ACPIHANDLE_INIT(ec);
7475 "Not yet supported ThinkPad detected!\n");
7480 * Risks a regression on very old machines, but reduces potential
7481 * false positives a damn great deal
7484 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7486 if (!is_thinkpad && !force_load)
7493 /* Module init, exit, parameters */
7495 static struct ibm_init_struct ibms_init[] __initdata = {
7497 .init = thinkpad_acpi_driver_init,
7498 .data = &thinkpad_acpi_driver_data,
7501 .init = hotkey_init,
7502 .data = &hotkey_driver_data,
7505 .init = bluetooth_init,
7506 .data = &bluetooth_driver_data,
7510 .data = &wan_driver_data,
7514 .data = &uwb_driver_data,
7516 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7519 .data = &video_driver_data,
7524 .data = &light_driver_data,
7526 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7529 .data = &dock_driver_data[0],
7533 .data = &dock_driver_data[1],
7536 #ifdef CONFIG_THINKPAD_ACPI_BAY
7539 .data = &bay_driver_data,
7544 .data = &cmos_driver_data,
7548 .data = &led_driver_data,
7552 .data = &beep_driver_data,
7555 .init = thermal_init,
7556 .data = &thermal_driver_data,
7559 .data = &ecdump_driver_data,
7562 .init = brightness_init,
7563 .data = &brightness_driver_data,
7566 .data = &volume_driver_data,
7570 .data = &fan_driver_data,
7574 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7577 struct ibm_struct *ibm;
7579 if (!kp || !kp->name || !val)
7582 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7583 ibm = ibms_init[i].data;
7584 WARN_ON(ibm == NULL);
7586 if (!ibm || !ibm->name)
7589 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7590 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7592 strcpy(ibms_init[i].param, val);
7593 strcat(ibms_init[i].param, ",");
7601 module_param(experimental, int, 0);
7602 MODULE_PARM_DESC(experimental,
7603 "Enables experimental features when non-zero");
7605 module_param_named(debug, dbg_level, uint, 0);
7606 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7608 module_param(force_load, bool, 0);
7609 MODULE_PARM_DESC(force_load,
7610 "Attempts to load the driver even on a "
7611 "mis-identified ThinkPad when true");
7613 module_param_named(fan_control, fan_control_allowed, bool, 0);
7614 MODULE_PARM_DESC(fan_control,
7615 "Enables setting fan parameters features when true");
7617 module_param_named(brightness_mode, brightness_mode, uint, 0);
7618 MODULE_PARM_DESC(brightness_mode,
7619 "Selects brightness control strategy: "
7620 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7622 module_param(brightness_enable, uint, 0);
7623 MODULE_PARM_DESC(brightness_enable,
7624 "Enables backlight control when 1, disables when 0");
7626 module_param(hotkey_report_mode, uint, 0);
7627 MODULE_PARM_DESC(hotkey_report_mode,
7628 "used for backwards compatibility with userspace, "
7629 "see documentation");
7631 #define TPACPI_PARAM(feature) \
7632 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7633 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7634 "at module load, see documentation")
7636 TPACPI_PARAM(hotkey);
7637 TPACPI_PARAM(bluetooth);
7638 TPACPI_PARAM(video);
7639 TPACPI_PARAM(light);
7640 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7643 #ifdef CONFIG_THINKPAD_ACPI_BAY
7645 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7649 TPACPI_PARAM(ecdump);
7650 TPACPI_PARAM(brightness);
7651 TPACPI_PARAM(volume);
7654 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7655 module_param(dbg_wlswemul, uint, 0);
7656 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7657 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7658 MODULE_PARM_DESC(wlsw_state,
7659 "Initial state of the emulated WLSW switch");
7661 module_param(dbg_bluetoothemul, uint, 0);
7662 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7663 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7664 MODULE_PARM_DESC(bluetooth_state,
7665 "Initial state of the emulated bluetooth switch");
7667 module_param(dbg_wwanemul, uint, 0);
7668 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7669 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7670 MODULE_PARM_DESC(wwan_state,
7671 "Initial state of the emulated WWAN switch");
7673 module_param(dbg_uwbemul, uint, 0);
7674 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7675 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7676 MODULE_PARM_DESC(uwb_state,
7677 "Initial state of the emulated UWB switch");
7680 static void thinkpad_acpi_module_exit(void)
7682 struct ibm_struct *ibm, *itmp;
7684 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7686 list_for_each_entry_safe_reverse(ibm, itmp,
7687 &tpacpi_all_drivers,
7692 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7694 if (tpacpi_inputdev) {
7695 if (tp_features.input_device_registered)
7696 input_unregister_device(tpacpi_inputdev);
7698 input_free_device(tpacpi_inputdev);
7702 hwmon_device_unregister(tpacpi_hwmon);
7704 if (tp_features.sensors_pdev_attrs_registered)
7705 device_remove_file(&tpacpi_sensors_pdev->dev,
7706 &dev_attr_thinkpad_acpi_pdev_name);
7707 if (tpacpi_sensors_pdev)
7708 platform_device_unregister(tpacpi_sensors_pdev);
7710 platform_device_unregister(tpacpi_pdev);
7712 if (tp_features.sensors_pdrv_attrs_registered)
7713 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7714 if (tp_features.platform_drv_attrs_registered)
7715 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7717 if (tp_features.sensors_pdrv_registered)
7718 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7720 if (tp_features.platform_drv_registered)
7721 platform_driver_unregister(&tpacpi_pdriver);
7724 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7727 destroy_workqueue(tpacpi_wq);
7729 kfree(thinkpad_id.bios_version_str);
7730 kfree(thinkpad_id.ec_version_str);
7731 kfree(thinkpad_id.model_str);
7735 static int __init thinkpad_acpi_module_init(void)
7739 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7741 /* Parameter checking */
7742 if (hotkey_report_mode > 2)
7745 /* Driver-level probe */
7747 ret = get_thinkpad_model_data(&thinkpad_id);
7750 "unable to get DMI data: %d\n", ret);
7751 thinkpad_acpi_module_exit();
7754 ret = probe_for_thinkpad();
7756 thinkpad_acpi_module_exit();
7760 /* Driver initialization */
7762 TPACPI_ACPIHANDLE_INIT(ecrd);
7763 TPACPI_ACPIHANDLE_INIT(ecwr);
7765 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7767 thinkpad_acpi_module_exit();
7771 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7774 "unable to create proc dir " TPACPI_PROC_DIR);
7775 thinkpad_acpi_module_exit();
7779 ret = platform_driver_register(&tpacpi_pdriver);
7782 "unable to register main platform driver\n");
7783 thinkpad_acpi_module_exit();
7786 tp_features.platform_drv_registered = 1;
7788 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7791 "unable to register hwmon platform driver\n");
7792 thinkpad_acpi_module_exit();
7795 tp_features.sensors_pdrv_registered = 1;
7797 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7799 tp_features.platform_drv_attrs_registered = 1;
7800 ret = tpacpi_create_driver_attributes(
7801 &tpacpi_hwmon_pdriver.driver);
7805 "unable to create sysfs driver attributes\n");
7806 thinkpad_acpi_module_exit();
7809 tp_features.sensors_pdrv_attrs_registered = 1;
7812 /* Device initialization */
7813 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7815 if (IS_ERR(tpacpi_pdev)) {
7816 ret = PTR_ERR(tpacpi_pdev);
7818 printk(TPACPI_ERR "unable to register platform device\n");
7819 thinkpad_acpi_module_exit();
7822 tpacpi_sensors_pdev = platform_device_register_simple(
7823 TPACPI_HWMON_DRVR_NAME,
7825 if (IS_ERR(tpacpi_sensors_pdev)) {
7826 ret = PTR_ERR(tpacpi_sensors_pdev);
7827 tpacpi_sensors_pdev = NULL;
7829 "unable to register hwmon platform device\n");
7830 thinkpad_acpi_module_exit();
7833 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7834 &dev_attr_thinkpad_acpi_pdev_name);
7837 "unable to create sysfs hwmon device attributes\n");
7838 thinkpad_acpi_module_exit();
7841 tp_features.sensors_pdev_attrs_registered = 1;
7842 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7843 if (IS_ERR(tpacpi_hwmon)) {
7844 ret = PTR_ERR(tpacpi_hwmon);
7845 tpacpi_hwmon = NULL;
7846 printk(TPACPI_ERR "unable to register hwmon device\n");
7847 thinkpad_acpi_module_exit();
7850 mutex_init(&tpacpi_inputdev_send_mutex);
7851 tpacpi_inputdev = input_allocate_device();
7852 if (!tpacpi_inputdev) {
7853 printk(TPACPI_ERR "unable to allocate input device\n");
7854 thinkpad_acpi_module_exit();
7857 /* Prepare input device, but don't register */
7858 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7859 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7860 tpacpi_inputdev->id.bustype = BUS_HOST;
7861 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7862 thinkpad_id.vendor :
7864 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7865 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7867 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7868 ret = ibm_init(&ibms_init[i]);
7869 if (ret >= 0 && *ibms_init[i].param)
7870 ret = ibms_init[i].data->write(ibms_init[i].param);
7872 thinkpad_acpi_module_exit();
7876 ret = input_register_device(tpacpi_inputdev);
7878 printk(TPACPI_ERR "unable to register input device\n");
7879 thinkpad_acpi_module_exit();
7882 tp_features.input_device_registered = 1;
7885 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7889 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7892 * This will autoload the driver in almost every ThinkPad
7893 * in widespread use.
7895 * Only _VERY_ old models, like the 240, 240x and 570 lack
7896 * the HKEY event interface.
7898 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7901 * DMI matching for module autoloading
7903 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7904 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7906 * Only models listed in thinkwiki will be supported, so add yours
7907 * if it is not there yet.
7909 #define IBM_BIOS_MODULE_ALIAS(__type) \
7910 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7912 /* Ancient thinkpad BIOSes have to be identified by
7913 * BIOS type or model number, and there are far less
7914 * BIOS types than model numbers... */
7915 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
7917 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7918 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
7919 MODULE_DESCRIPTION(TPACPI_DESC);
7920 MODULE_VERSION(TPACPI_VERSION);
7921 MODULE_LICENSE("GPL");
7923 module_init(thinkpad_acpi_module_init);
7924 module_exit(thinkpad_acpi_module_exit);