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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.26"
27 #define TPACPI_SYSFS_VERSION 0x030000
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
69 #include <linux/bitops.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <linux/dmi.h>
78 #include <linux/jiffies.h>
79 #include <linux/workqueue.h>
80 #include <linux/acpi.h>
81 #include <linux/pci_ids.h>
82 #include <linux/power_supply.h>
83 #include <linux/thinkpad_acpi.h>
84 #include <sound/core.h>
85 #include <sound/control.h>
86 #include <sound/initval.h>
87 #include <linux/uaccess.h>
88 #include <acpi/battery.h>
89 #include <acpi/video.h>
91 /* ThinkPad CMOS commands */
92 #define TP_CMOS_VOLUME_DOWN 0
93 #define TP_CMOS_VOLUME_UP 1
94 #define TP_CMOS_VOLUME_MUTE 2
95 #define TP_CMOS_BRIGHTNESS_UP 4
96 #define TP_CMOS_BRIGHTNESS_DOWN 5
97 #define TP_CMOS_THINKLIGHT_ON 12
98 #define TP_CMOS_THINKLIGHT_OFF 13
100 /* NVRAM Addresses */
102 TP_NVRAM_ADDR_HK2 = 0x57,
103 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
104 TP_NVRAM_ADDR_VIDEO = 0x59,
105 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
106 TP_NVRAM_ADDR_MIXER = 0x60,
109 /* NVRAM bit masks */
111 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
112 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
113 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
114 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
115 TP_NVRAM_MASK_THINKLIGHT = 0x10,
116 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
117 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
118 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
119 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
120 TP_NVRAM_MASK_MUTE = 0x40,
121 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
122 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
123 TP_NVRAM_POS_LEVEL_VOLUME = 0,
126 /* Misc NVRAM-related */
128 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
132 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
133 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
164 enum tpacpi_hkey_event_t {
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
170 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
171 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
172 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
174 /* Reasons for waking up from S3/S4 */
175 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
176 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
177 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
178 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
179 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
180 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
182 /* Auto-sleep after eject request */
183 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
184 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
186 /* Misc bay events */
187 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
188 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
189 or port replicator */
190 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
191 dock or port replicator */
193 /* User-interface events */
194 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
195 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
196 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
197 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
198 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
199 * enter/leave tablet mode
201 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
202 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
203 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
205 /* Key-related user-interface events */
206 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
207 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
208 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
211 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
212 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
213 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
214 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
215 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
216 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
217 * command completed. Related to
219 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
220 * changed. Related to AML GMTS */
222 /* AC-related events */
223 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
225 /* Further user-interface events */
226 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
227 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
230 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
233 /****************************************************************************
237 #define TPACPI_NAME "thinkpad"
238 #define TPACPI_DESC "ThinkPad ACPI Extras"
239 #define TPACPI_FILE TPACPI_NAME "_acpi"
240 #define TPACPI_URL "http://ibm-acpi.sf.net/"
241 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
243 #define TPACPI_PROC_DIR "ibm"
244 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
245 #define TPACPI_DRVR_NAME TPACPI_FILE
246 #define TPACPI_DRVR_SHORTNAME "tpacpi"
247 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
249 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
250 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
252 #define TPACPI_MAX_ACPI_ARGS 3
254 /* Debugging printk groups */
255 #define TPACPI_DBG_ALL 0xffff
256 #define TPACPI_DBG_DISCLOSETASK 0x8000
257 #define TPACPI_DBG_INIT 0x0001
258 #define TPACPI_DBG_EXIT 0x0002
259 #define TPACPI_DBG_RFKILL 0x0004
260 #define TPACPI_DBG_HKEY 0x0008
261 #define TPACPI_DBG_FAN 0x0010
262 #define TPACPI_DBG_BRGHT 0x0020
263 #define TPACPI_DBG_MIXER 0x0040
265 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
266 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
267 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
270 /****************************************************************************
271 * Driver-wide structs and misc. variables
276 struct tp_acpi_drv_struct {
277 const struct acpi_device_id *hid;
278 struct acpi_driver *driver;
280 void (*notify) (struct ibm_struct *, u32);
283 struct acpi_device *device;
289 int (*read) (struct seq_file *);
290 int (*write) (char *);
292 void (*resume) (void);
293 void (*suspend) (void);
294 void (*shutdown) (void);
296 struct list_head all_drivers;
298 struct tp_acpi_drv_struct *acpi;
301 u8 acpi_driver_registered:1;
302 u8 acpi_notify_installed:1;
309 struct ibm_init_struct {
312 int (*init) (struct ibm_init_struct *);
313 umode_t base_procfs_mode;
314 struct ibm_struct *data;
323 TP_HOTKEY_TABLET_NONE = 0,
324 TP_HOTKEY_TABLET_USES_MHKG,
325 TP_HOTKEY_TABLET_USES_GMMS,
330 u32 bright_acpimode:1;
334 u32 fan_ctrl_status_undef:1;
336 u32 beep_needs_two_args:1;
337 u32 mixer_no_level_control:1;
338 u32 input_device_registered:1;
339 u32 platform_drv_registered:1;
340 u32 platform_drv_attrs_registered:1;
341 u32 sensors_pdrv_registered:1;
342 u32 sensors_pdrv_attrs_registered:1;
343 u32 sensors_pdev_attrs_registered:1;
344 u32 hotkey_poll_active:1;
345 u32 has_adaptive_kbd:1;
350 u16 hotkey_mask_ff:1;
351 u16 volume_ctrl_forbidden:1;
354 struct thinkpad_id_data {
355 unsigned int vendor; /* ThinkPad vendor:
356 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
358 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
359 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
361 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
363 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
366 char *model_str; /* ThinkPad T43 */
367 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
369 static struct thinkpad_id_data thinkpad_id;
372 TPACPI_LIFE_INIT = 0,
377 static int experimental;
378 static u32 dbg_level;
380 static struct workqueue_struct *tpacpi_wq;
388 /* tpacpi LED class */
389 struct tpacpi_led_classdev {
390 struct led_classdev led_classdev;
394 /* brightness level capabilities */
395 static unsigned int bright_maxlvl; /* 0 = unknown */
397 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
398 static int dbg_wlswemul;
399 static bool tpacpi_wlsw_emulstate;
400 static int dbg_bluetoothemul;
401 static bool tpacpi_bluetooth_emulstate;
402 static int dbg_wwanemul;
403 static bool tpacpi_wwan_emulstate;
404 static int dbg_uwbemul;
405 static bool tpacpi_uwb_emulstate;
409 /*************************************************************************
413 #define dbg_printk(a_dbg_level, format, arg...) \
415 if (dbg_level & (a_dbg_level)) \
416 printk(KERN_DEBUG pr_fmt("%s: " format), \
420 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
421 #define vdbg_printk dbg_printk
422 static const char *str_supported(int is_supported);
424 static inline const char *str_supported(int is_supported) { return ""; }
425 #define vdbg_printk(a_dbg_level, format, arg...) \
426 do { if (0) no_printk(format, ##arg); } while (0)
429 static void tpacpi_log_usertask(const char * const what)
431 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
432 what, task_tgid_vnr(current));
435 #define tpacpi_disclose_usertask(what, format, arg...) \
437 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
438 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
439 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
440 what, task_tgid_vnr(current), ## arg); \
445 * Quirk handling helpers
447 * ThinkPad IDs and versions seen in the field so far
448 * are two-characters from the set [0-9A-Z], i.e. base 36.
450 * We use values well outside that range as specials.
453 #define TPACPI_MATCH_ANY 0xffffU
454 #define TPACPI_MATCH_UNKNOWN 0U
456 /* TPID('1', 'Y') == 0x5931 */
457 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
459 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_IBM, \
461 .bios = TPID(__id1, __id2), \
462 .ec = TPACPI_MATCH_ANY, \
463 .quirks = (__quirk) }
465 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
466 { .vendor = PCI_VENDOR_ID_LENOVO, \
467 .bios = TPID(__id1, __id2), \
468 .ec = TPACPI_MATCH_ANY, \
469 .quirks = (__quirk) }
471 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
472 { .vendor = PCI_VENDOR_ID_LENOVO, \
473 .bios = TPACPI_MATCH_ANY, \
474 .ec = TPID(__id1, __id2), \
475 .quirks = (__quirk) }
477 struct tpacpi_quirk {
481 unsigned long quirks;
485 * tpacpi_check_quirks() - search BIOS/EC version on a list
486 * @qlist: array of &struct tpacpi_quirk
487 * @qlist_size: number of elements in @qlist
489 * Iterates over a quirks list until one is found that matches the
490 * ThinkPad's vendor, BIOS and EC model.
492 * Returns 0 if nothing matches, otherwise returns the quirks field of
493 * the matching &struct tpacpi_quirk entry.
495 * The match criteria is: vendor, ec and bios much match.
497 static unsigned long __init tpacpi_check_quirks(
498 const struct tpacpi_quirk *qlist,
499 unsigned int qlist_size)
502 if ((qlist->vendor == thinkpad_id.vendor ||
503 qlist->vendor == TPACPI_MATCH_ANY) &&
504 (qlist->bios == thinkpad_id.bios_model ||
505 qlist->bios == TPACPI_MATCH_ANY) &&
506 (qlist->ec == thinkpad_id.ec_model ||
507 qlist->ec == TPACPI_MATCH_ANY))
508 return qlist->quirks;
516 static inline bool __pure __init tpacpi_is_lenovo(void)
518 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
521 static inline bool __pure __init tpacpi_is_ibm(void)
523 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
526 /****************************************************************************
527 ****************************************************************************
529 * ACPI Helpers and device model
531 ****************************************************************************
532 ****************************************************************************/
534 /*************************************************************************
538 static acpi_handle root_handle;
539 static acpi_handle ec_handle;
541 #define TPACPI_HANDLE(object, parent, paths...) \
542 static acpi_handle object##_handle; \
543 static const acpi_handle * const object##_parent __initconst = \
545 static char *object##_paths[] __initdata = { paths }
547 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
548 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
550 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
552 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
553 "\\CMS", /* R40, R40e */
556 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
557 "^HKEY", /* R30, R31 */
558 "HKEY", /* all others */
561 /*************************************************************************
565 static int acpi_evalf(acpi_handle handle,
566 int *res, char *method, char *fmt, ...)
569 struct acpi_object_list params;
570 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
571 struct acpi_buffer result, *resultp;
572 union acpi_object out_obj;
580 pr_err("acpi_evalf() called with empty format\n");
593 params.pointer = &in_objs[0];
600 in_objs[params.count].integer.value = va_arg(ap, int);
601 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
603 /* add more types as needed */
605 pr_err("acpi_evalf() called with invalid format character '%c'\n",
613 if (res_type != 'v') {
614 result.length = sizeof(out_obj);
615 result.pointer = &out_obj;
620 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
624 success = (status == AE_OK &&
625 out_obj.type == ACPI_TYPE_INTEGER);
627 *res = out_obj.integer.value;
630 success = status == AE_OK;
632 /* add more types as needed */
634 pr_err("acpi_evalf() called with invalid format character '%c'\n",
639 if (!success && !quiet)
640 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
641 method, fmt0, acpi_format_exception(status));
646 static int acpi_ec_read(int i, u8 *p)
651 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
655 if (ec_read(i, p) < 0)
662 static int acpi_ec_write(int i, u8 v)
665 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
668 if (ec_write(i, v) < 0)
675 static int issue_thinkpad_cmos_command(int cmos_cmd)
680 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
686 /*************************************************************************
690 #define TPACPI_ACPIHANDLE_INIT(object) \
691 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
692 object##_paths, ARRAY_SIZE(object##_paths))
694 static void __init drv_acpi_handle_init(const char *name,
695 acpi_handle *handle, const acpi_handle parent,
696 char **paths, const int num_paths)
701 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
704 for (i = 0; i < num_paths; i++) {
705 status = acpi_get_handle(parent, paths[i], handle);
706 if (ACPI_SUCCESS(status)) {
707 dbg_printk(TPACPI_DBG_INIT,
708 "Found ACPI handle %s for %s\n",
714 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
719 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
720 u32 level, void *context, void **return_value)
722 struct acpi_device *dev;
723 if (!strcmp(context, "video")) {
724 if (acpi_bus_get_device(handle, &dev))
726 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
730 *(acpi_handle *)return_value = handle;
732 return AE_CTRL_TERMINATE;
735 static void __init tpacpi_acpi_handle_locate(const char *name,
740 acpi_handle device_found;
742 BUG_ON(!name || !handle);
743 vdbg_printk(TPACPI_DBG_INIT,
744 "trying to locate ACPI handle for %s, using HID %s\n",
745 name, hid ? hid : "NULL");
747 memset(&device_found, 0, sizeof(device_found));
748 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
749 (void *)name, &device_found);
753 if (ACPI_SUCCESS(status)) {
754 *handle = device_found;
755 dbg_printk(TPACPI_DBG_INIT,
756 "Found ACPI handle for %s\n", name);
758 vdbg_printk(TPACPI_DBG_INIT,
759 "Could not locate an ACPI handle for %s: %s\n",
760 name, acpi_format_exception(status));
764 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
766 struct ibm_struct *ibm = data;
768 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
771 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
774 ibm->acpi->notify(ibm, event);
777 static int __init setup_acpi_notify(struct ibm_struct *ibm)
784 if (!*ibm->acpi->handle)
787 vdbg_printk(TPACPI_DBG_INIT,
788 "setting up ACPI notify for %s\n", ibm->name);
790 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
792 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
796 ibm->acpi->device->driver_data = ibm;
797 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
798 TPACPI_ACPI_EVENT_PREFIX,
801 status = acpi_install_notify_handler(*ibm->acpi->handle,
802 ibm->acpi->type, dispatch_acpi_notify, ibm);
803 if (ACPI_FAILURE(status)) {
804 if (status == AE_ALREADY_EXISTS) {
805 pr_notice("another device driver is already handling %s events\n",
808 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
809 ibm->name, acpi_format_exception(status));
813 ibm->flags.acpi_notify_installed = 1;
817 static int __init tpacpi_device_add(struct acpi_device *device)
822 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
826 dbg_printk(TPACPI_DBG_INIT,
827 "registering %s as an ACPI driver\n", ibm->name);
831 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
832 if (!ibm->acpi->driver) {
833 pr_err("failed to allocate memory for ibm->acpi->driver\n");
837 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
838 ibm->acpi->driver->ids = ibm->acpi->hid;
840 ibm->acpi->driver->ops.add = &tpacpi_device_add;
842 rc = acpi_bus_register_driver(ibm->acpi->driver);
844 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
846 kfree(ibm->acpi->driver);
847 ibm->acpi->driver = NULL;
849 ibm->flags.acpi_driver_registered = 1;
855 /****************************************************************************
856 ****************************************************************************
860 ****************************************************************************
861 ****************************************************************************/
863 static int dispatch_proc_show(struct seq_file *m, void *v)
865 struct ibm_struct *ibm = m->private;
867 if (!ibm || !ibm->read)
872 static int dispatch_proc_open(struct inode *inode, struct file *file)
874 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
877 static ssize_t dispatch_proc_write(struct file *file,
878 const char __user *userbuf,
879 size_t count, loff_t *pos)
881 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
885 if (!ibm || !ibm->write)
887 if (count > PAGE_SIZE - 2)
890 kernbuf = kmalloc(count + 2, GFP_KERNEL);
894 if (copy_from_user(kernbuf, userbuf, count)) {
900 strcat(kernbuf, ",");
901 ret = ibm->write(kernbuf);
910 static const struct file_operations dispatch_proc_fops = {
911 .owner = THIS_MODULE,
912 .open = dispatch_proc_open,
915 .release = single_release,
916 .write = dispatch_proc_write,
919 static char *next_cmd(char **cmds)
924 while ((end = strchr(start, ',')) && end == start)
936 /****************************************************************************
937 ****************************************************************************
939 * Device model: input, hwmon and platform
941 ****************************************************************************
942 ****************************************************************************/
944 static struct platform_device *tpacpi_pdev;
945 static struct platform_device *tpacpi_sensors_pdev;
946 static struct device *tpacpi_hwmon;
947 static struct input_dev *tpacpi_inputdev;
948 static struct mutex tpacpi_inputdev_send_mutex;
949 static LIST_HEAD(tpacpi_all_drivers);
951 #ifdef CONFIG_PM_SLEEP
952 static int tpacpi_suspend_handler(struct device *dev)
954 struct ibm_struct *ibm, *itmp;
956 list_for_each_entry_safe(ibm, itmp,
966 static int tpacpi_resume_handler(struct device *dev)
968 struct ibm_struct *ibm, *itmp;
970 list_for_each_entry_safe(ibm, itmp,
981 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
982 tpacpi_suspend_handler, tpacpi_resume_handler);
984 static void tpacpi_shutdown_handler(struct platform_device *pdev)
986 struct ibm_struct *ibm, *itmp;
988 list_for_each_entry_safe(ibm, itmp,
996 static struct platform_driver tpacpi_pdriver = {
998 .name = TPACPI_DRVR_NAME,
1001 .shutdown = tpacpi_shutdown_handler,
1004 static struct platform_driver tpacpi_hwmon_pdriver = {
1006 .name = TPACPI_HWMON_DRVR_NAME,
1010 /*************************************************************************
1011 * sysfs support helpers
1014 struct attribute_set {
1015 unsigned int members, max_members;
1016 struct attribute_group group;
1019 struct attribute_set_obj {
1020 struct attribute_set s;
1021 struct attribute *a;
1022 } __attribute__((packed));
1024 static struct attribute_set *create_attr_set(unsigned int max_members,
1027 struct attribute_set_obj *sobj;
1029 if (max_members == 0)
1032 /* Allocates space for implicit NULL at the end too */
1033 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1034 max_members * sizeof(struct attribute *),
1038 sobj->s.max_members = max_members;
1039 sobj->s.group.attrs = &sobj->a;
1040 sobj->s.group.name = name;
1045 #define destroy_attr_set(_set) \
1048 /* not multi-threaded safe, use it in a single thread per set */
1049 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1054 if (s->members >= s->max_members)
1057 s->group.attrs[s->members] = attr;
1063 static int add_many_to_attr_set(struct attribute_set *s,
1064 struct attribute **attr,
1069 for (i = 0; i < count; i++) {
1070 res = add_to_attr_set(s, attr[i]);
1078 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1080 sysfs_remove_group(kobj, &s->group);
1081 destroy_attr_set(s);
1084 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1085 sysfs_create_group(_kobj, &_attr_set->group)
1087 static int parse_strtoul(const char *buf,
1088 unsigned long max, unsigned long *value)
1092 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1093 endp = skip_spaces(endp);
1094 if (*endp || *value > max)
1100 static void tpacpi_disable_brightness_delay(void)
1102 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1103 pr_notice("ACPI backlight control delay disabled\n");
1106 static void printk_deprecated_attribute(const char * const what,
1107 const char * const details)
1109 tpacpi_log_usertask("deprecated sysfs attribute");
1110 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1114 /*************************************************************************
1115 * rfkill and radio control support helpers
1119 * ThinkPad-ACPI firmware handling model:
1121 * WLSW (master wireless switch) is event-driven, and is common to all
1122 * firmware-controlled radios. It cannot be controlled, just monitored,
1123 * as expected. It overrides all radio state in firmware
1125 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1126 * (TODO: verify how WLSW interacts with the returned radio state).
1128 * The only time there are shadow radio state changes, is when
1129 * masked-off hotkeys are used.
1133 * Internal driver API for radio state:
1135 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1136 * bool: true means radio blocked (off)
1138 enum tpacpi_rfkill_state {
1139 TPACPI_RFK_RADIO_OFF = 0,
1143 /* rfkill switches */
1144 enum tpacpi_rfk_id {
1145 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1146 TPACPI_RFK_WWAN_SW_ID,
1147 TPACPI_RFK_UWB_SW_ID,
1151 static const char *tpacpi_rfkill_names[] = {
1152 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1153 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1154 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1155 [TPACPI_RFK_SW_MAX] = NULL
1158 /* ThinkPad-ACPI rfkill subdriver */
1160 struct rfkill *rfkill;
1161 enum tpacpi_rfk_id id;
1162 const struct tpacpi_rfk_ops *ops;
1165 struct tpacpi_rfk_ops {
1166 /* firmware interface */
1167 int (*get_status)(void);
1168 int (*set_status)(const enum tpacpi_rfkill_state);
1171 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1173 /* Query FW and update rfkill sw state for a given rfkill switch */
1174 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1181 status = (tp_rfk->ops->get_status)();
1185 rfkill_set_sw_state(tp_rfk->rfkill,
1186 (status == TPACPI_RFK_RADIO_OFF));
1191 /* Query FW and update rfkill sw state for all rfkill switches */
1192 static void tpacpi_rfk_update_swstate_all(void)
1196 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1197 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1201 * Sync the HW-blocking state of all rfkill switches,
1202 * do notice it causes the rfkill core to schedule uevents
1204 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1207 struct tpacpi_rfk *tp_rfk;
1209 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1210 tp_rfk = tpacpi_rfkill_switches[i];
1212 if (rfkill_set_hw_state(tp_rfk->rfkill,
1214 /* ignore -- we track sw block */
1220 /* Call to get the WLSW state from the firmware */
1221 static int hotkey_get_wlsw(void);
1223 /* Call to query WLSW state and update all rfkill switches */
1224 static bool tpacpi_rfk_check_hwblock_state(void)
1226 int res = hotkey_get_wlsw();
1229 /* When unknown or unsupported, we have to assume it is unblocked */
1233 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1234 tpacpi_rfk_update_hwblock_state(hw_blocked);
1239 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1241 struct tpacpi_rfk *tp_rfk = data;
1244 dbg_printk(TPACPI_DBG_RFKILL,
1245 "request to change radio state to %s\n",
1246 blocked ? "blocked" : "unblocked");
1248 /* try to set radio state */
1249 res = (tp_rfk->ops->set_status)(blocked ?
1250 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1252 /* and update the rfkill core with whatever the FW really did */
1253 tpacpi_rfk_update_swstate(tp_rfk);
1255 return (res < 0) ? res : 0;
1258 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1259 .set_block = tpacpi_rfk_hook_set_block,
1262 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1263 const struct tpacpi_rfk_ops *tp_rfkops,
1264 const enum rfkill_type rfktype,
1266 const bool set_default)
1268 struct tpacpi_rfk *atp_rfk;
1270 bool sw_state = false;
1274 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1276 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1278 atp_rfk->rfkill = rfkill_alloc(name,
1281 &tpacpi_rfk_rfkill_ops,
1283 if (!atp_rfk || !atp_rfk->rfkill) {
1284 pr_err("failed to allocate memory for rfkill class\n");
1290 atp_rfk->ops = tp_rfkops;
1292 sw_status = (tp_rfkops->get_status)();
1293 if (sw_status < 0) {
1294 pr_err("failed to read initial state for %s, error %d\n",
1297 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1299 /* try to keep the initial state, since we ask the
1300 * firmware to preserve it across S5 in NVRAM */
1301 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1304 hw_state = tpacpi_rfk_check_hwblock_state();
1305 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1307 res = rfkill_register(atp_rfk->rfkill);
1309 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1310 rfkill_destroy(atp_rfk->rfkill);
1315 tpacpi_rfkill_switches[id] = atp_rfk;
1317 pr_info("rfkill switch %s: radio is %sblocked\n",
1318 name, (sw_state || hw_state) ? "" : "un");
1322 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1324 struct tpacpi_rfk *tp_rfk;
1326 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1328 tp_rfk = tpacpi_rfkill_switches[id];
1330 rfkill_unregister(tp_rfk->rfkill);
1331 rfkill_destroy(tp_rfk->rfkill);
1332 tpacpi_rfkill_switches[id] = NULL;
1337 static void printk_deprecated_rfkill_attribute(const char * const what)
1339 printk_deprecated_attribute(what,
1340 "Please switch to generic rfkill before year 2010");
1343 /* sysfs <radio> enable ------------------------------------------------ */
1344 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1345 struct device_attribute *attr,
1350 printk_deprecated_rfkill_attribute(attr->attr.name);
1352 /* This is in the ABI... */
1353 if (tpacpi_rfk_check_hwblock_state()) {
1354 status = TPACPI_RFK_RADIO_OFF;
1356 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1361 return snprintf(buf, PAGE_SIZE, "%d\n",
1362 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1365 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1366 struct device_attribute *attr,
1367 const char *buf, size_t count)
1372 printk_deprecated_rfkill_attribute(attr->attr.name);
1374 if (parse_strtoul(buf, 1, &t))
1377 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1379 /* This is in the ABI... */
1380 if (tpacpi_rfk_check_hwblock_state() && !!t)
1383 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1384 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1385 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1387 return (res < 0) ? res : count;
1390 /* procfs -------------------------------------------------------------- */
1391 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1393 if (id >= TPACPI_RFK_SW_MAX)
1394 seq_printf(m, "status:\t\tnot supported\n");
1398 /* This is in the ABI... */
1399 if (tpacpi_rfk_check_hwblock_state()) {
1400 status = TPACPI_RFK_RADIO_OFF;
1402 status = tpacpi_rfk_update_swstate(
1403 tpacpi_rfkill_switches[id]);
1408 seq_printf(m, "status:\t\t%s\n",
1409 (status == TPACPI_RFK_RADIO_ON) ?
1410 "enabled" : "disabled");
1411 seq_printf(m, "commands:\tenable, disable\n");
1417 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1423 if (id >= TPACPI_RFK_SW_MAX)
1426 while ((cmd = next_cmd(&buf))) {
1427 if (strlencmp(cmd, "enable") == 0)
1428 status = TPACPI_RFK_RADIO_ON;
1429 else if (strlencmp(cmd, "disable") == 0)
1430 status = TPACPI_RFK_RADIO_OFF;
1436 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1437 (status == TPACPI_RFK_RADIO_ON) ?
1438 "enable" : "disable",
1439 tpacpi_rfkill_names[id]);
1440 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1441 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1447 /*************************************************************************
1448 * thinkpad-acpi driver attributes
1451 /* interface_version --------------------------------------------------- */
1452 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1454 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1456 static DRIVER_ATTR_RO(interface_version);
1458 /* debug_level --------------------------------------------------------- */
1459 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1461 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1464 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1469 if (parse_strtoul(buf, 0xffff, &t))
1476 static DRIVER_ATTR_RW(debug_level);
1478 /* version ------------------------------------------------------------- */
1479 static ssize_t version_show(struct device_driver *drv, char *buf)
1481 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1482 TPACPI_DESC, TPACPI_VERSION);
1484 static DRIVER_ATTR_RO(version);
1486 /* --------------------------------------------------------------------- */
1488 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1490 /* wlsw_emulstate ------------------------------------------------------ */
1491 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1496 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1501 if (parse_strtoul(buf, 1, &t))
1504 if (tpacpi_wlsw_emulstate != !!t) {
1505 tpacpi_wlsw_emulstate = !!t;
1506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1511 static DRIVER_ATTR_RW(wlsw_emulstate);
1513 /* bluetooth_emulstate ------------------------------------------------- */
1514 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1516 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1519 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1520 const char *buf, size_t count)
1524 if (parse_strtoul(buf, 1, &t))
1527 tpacpi_bluetooth_emulstate = !!t;
1531 static DRIVER_ATTR_RW(bluetooth_emulstate);
1533 /* wwan_emulstate ------------------------------------------------- */
1534 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1536 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1539 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1544 if (parse_strtoul(buf, 1, &t))
1547 tpacpi_wwan_emulstate = !!t;
1551 static DRIVER_ATTR_RW(wwan_emulstate);
1553 /* uwb_emulstate ------------------------------------------------- */
1554 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1556 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1559 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1564 if (parse_strtoul(buf, 1, &t))
1567 tpacpi_uwb_emulstate = !!t;
1571 static DRIVER_ATTR_RW(uwb_emulstate);
1574 /* --------------------------------------------------------------------- */
1576 static struct driver_attribute *tpacpi_driver_attributes[] = {
1577 &driver_attr_debug_level, &driver_attr_version,
1578 &driver_attr_interface_version,
1581 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1587 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1588 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1592 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1593 if (!res && dbg_wlswemul)
1594 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1595 if (!res && dbg_bluetoothemul)
1596 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1597 if (!res && dbg_wwanemul)
1598 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1599 if (!res && dbg_uwbemul)
1600 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1606 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1610 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1611 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1613 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1614 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1615 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1616 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1617 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1621 /*************************************************************************
1626 * Table of recommended minimum BIOS versions
1628 * Reasons for listing:
1629 * 1. Stable BIOS, listed because the unknown amount of
1630 * bugs and bad ACPI behaviour on older versions
1632 * 2. BIOS or EC fw with known bugs that trigger on Linux
1634 * 3. BIOS with known reduced functionality in older versions
1636 * We recommend the latest BIOS and EC version.
1637 * We only support the latest BIOS and EC fw version as a rule.
1639 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1640 * Information from users in ThinkWiki
1642 * WARNING: we use this table also to detect that the machine is
1643 * a ThinkPad in some cases, so don't remove entries lightly.
1646 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1647 { .vendor = (__v), \
1648 .bios = TPID(__id1, __id2), \
1649 .ec = TPACPI_MATCH_ANY, \
1650 .quirks = TPACPI_MATCH_ANY << 16 \
1651 | (__bv1) << 8 | (__bv2) }
1653 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1654 __eid, __ev1, __ev2) \
1655 { .vendor = (__v), \
1656 .bios = TPID(__bid1, __bid2), \
1658 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1659 | (__bv1) << 8 | (__bv2) }
1661 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1662 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1664 /* Outdated IBM BIOSes often lack the EC id string */
1665 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1666 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1667 __bv1, __bv2, TPID(__id1, __id2), \
1669 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1670 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1673 /* Outdated IBM BIOSes often lack the EC id string */
1674 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1675 __eid1, __eid2, __ev1, __ev2) \
1676 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1677 __bv1, __bv2, TPID(__eid1, __eid2), \
1679 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1680 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1683 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1684 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1686 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1691 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1692 __eid1, __eid2, __ev1, __ev2) \
1693 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1694 __bv1, __bv2, TPID(__eid1, __eid2), \
1697 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1698 /* Numeric models ------------------ */
1699 /* FW MODEL BIOS VERS */
1700 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1701 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1702 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1703 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1704 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1705 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1706 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1707 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1708 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1710 /* A-series ------------------------- */
1711 /* FW MODEL BIOS VERS EC VERS */
1712 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1713 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1714 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1715 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1716 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1717 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1718 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1719 TPV_QI0('1', '3', '2', '0'), /* A22m */
1720 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1721 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1722 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1724 /* G-series ------------------------- */
1725 /* FW MODEL BIOS VERS */
1726 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1727 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1729 /* R-series, T-series --------------- */
1730 /* FW MODEL BIOS VERS EC VERS */
1731 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1732 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1733 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1734 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1735 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1736 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1737 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1738 T40/p, T41/p, T42/p (1) */
1739 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1740 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1741 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1742 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1744 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1745 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1746 TPV_QI0('1', '6', '3', '2'), /* T22 */
1747 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1748 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1749 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1751 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1752 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1753 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1755 /* BIOS FW BIOS VERS EC FW EC VERS */
1756 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1757 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1759 /* X-series ------------------------- */
1760 /* FW MODEL BIOS VERS EC VERS */
1761 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1762 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1763 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1764 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1765 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1766 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1767 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1769 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1770 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1772 /* (0) - older versions lack DMI EC fw string and functionality */
1773 /* (1) - older versions known to lack functionality */
1784 static void __init tpacpi_check_outdated_fw(void)
1786 unsigned long fwvers;
1787 u16 ec_version, bios_version;
1789 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1790 ARRAY_SIZE(tpacpi_bios_version_qtable));
1795 bios_version = fwvers & 0xffffU;
1796 ec_version = (fwvers >> 16) & 0xffffU;
1798 /* note that unknown versions are set to 0x0000 and we use that */
1799 if ((bios_version > thinkpad_id.bios_release) ||
1800 (ec_version > thinkpad_id.ec_release &&
1801 ec_version != TPACPI_MATCH_ANY)) {
1803 * The changelogs would let us track down the exact
1804 * reason, but it is just too much of a pain to track
1805 * it. We only list BIOSes that are either really
1806 * broken, or really stable to begin with, so it is
1807 * best if the user upgrades the firmware anyway.
1809 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1810 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1814 static bool __init tpacpi_is_fw_known(void)
1816 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1817 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1820 /****************************************************************************
1821 ****************************************************************************
1825 ****************************************************************************
1826 ****************************************************************************/
1828 /*************************************************************************
1829 * thinkpad-acpi metadata subdriver
1832 static int thinkpad_acpi_driver_read(struct seq_file *m)
1834 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1835 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1839 static struct ibm_struct thinkpad_acpi_driver_data = {
1841 .read = thinkpad_acpi_driver_read,
1844 /*************************************************************************
1849 * ThinkPad firmware event model
1851 * The ThinkPad firmware has two main event interfaces: normal ACPI
1852 * notifications (which follow the ACPI standard), and a private event
1855 * The private event interface also issues events for the hotkeys. As
1856 * the driver gained features, the event handling code ended up being
1857 * built around the hotkey subdriver. This will need to be refactored
1858 * to a more formal event API eventually.
1860 * Some "hotkeys" are actually supposed to be used as event reports,
1861 * such as "brightness has changed", "volume has changed", depending on
1862 * the ThinkPad model and how the firmware is operating.
1864 * Unlike other classes, hotkey-class events have mask/unmask control on
1865 * non-ancient firmware. However, how it behaves changes a lot with the
1866 * firmware model and version.
1869 enum { /* hot key scan codes (derived from ACPI DSDT) */
1870 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1871 TP_ACPI_HOTKEYSCAN_FNF2,
1872 TP_ACPI_HOTKEYSCAN_FNF3,
1873 TP_ACPI_HOTKEYSCAN_FNF4,
1874 TP_ACPI_HOTKEYSCAN_FNF5,
1875 TP_ACPI_HOTKEYSCAN_FNF6,
1876 TP_ACPI_HOTKEYSCAN_FNF7,
1877 TP_ACPI_HOTKEYSCAN_FNF8,
1878 TP_ACPI_HOTKEYSCAN_FNF9,
1879 TP_ACPI_HOTKEYSCAN_FNF10,
1880 TP_ACPI_HOTKEYSCAN_FNF11,
1881 TP_ACPI_HOTKEYSCAN_FNF12,
1882 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1883 TP_ACPI_HOTKEYSCAN_FNINSERT,
1884 TP_ACPI_HOTKEYSCAN_FNDELETE,
1885 TP_ACPI_HOTKEYSCAN_FNHOME,
1886 TP_ACPI_HOTKEYSCAN_FNEND,
1887 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1888 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1889 TP_ACPI_HOTKEYSCAN_FNSPACE,
1890 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1891 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1892 TP_ACPI_HOTKEYSCAN_MUTE,
1893 TP_ACPI_HOTKEYSCAN_THINKPAD,
1894 TP_ACPI_HOTKEYSCAN_UNK1,
1895 TP_ACPI_HOTKEYSCAN_UNK2,
1896 TP_ACPI_HOTKEYSCAN_UNK3,
1897 TP_ACPI_HOTKEYSCAN_UNK4,
1898 TP_ACPI_HOTKEYSCAN_UNK5,
1899 TP_ACPI_HOTKEYSCAN_UNK6,
1900 TP_ACPI_HOTKEYSCAN_UNK7,
1901 TP_ACPI_HOTKEYSCAN_UNK8,
1903 /* Adaptive keyboard keycodes */
1904 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1905 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1906 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1907 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1908 TP_ACPI_HOTKEYSCAN_CLOUD,
1909 TP_ACPI_HOTKEYSCAN_UNK9,
1910 TP_ACPI_HOTKEYSCAN_VOICE,
1911 TP_ACPI_HOTKEYSCAN_UNK10,
1912 TP_ACPI_HOTKEYSCAN_GESTURES,
1913 TP_ACPI_HOTKEYSCAN_UNK11,
1914 TP_ACPI_HOTKEYSCAN_UNK12,
1915 TP_ACPI_HOTKEYSCAN_UNK13,
1916 TP_ACPI_HOTKEYSCAN_CONFIG,
1917 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1918 TP_ACPI_HOTKEYSCAN_RELOAD,
1919 TP_ACPI_HOTKEYSCAN_BACK,
1920 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1921 TP_ACPI_HOTKEYSCAN_MIC_UP,
1922 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1923 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1924 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1926 /* Lenovo extended keymap, starting at 0x1300 */
1927 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1928 /* first new observed key (star, favorites) is 0x1311 */
1929 TP_ACPI_HOTKEYSCAN_STAR = 69,
1930 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1931 TP_ACPI_HOTKEYSCAN_UNK25,
1932 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1933 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1935 /* Hotkey keymap size */
1936 TPACPI_HOTKEY_MAP_LEN
1939 enum { /* Keys/events available through NVRAM polling */
1940 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1941 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1944 enum { /* Positions of some of the keys in hotkey masks */
1945 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1946 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1947 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1948 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1949 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1950 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1951 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1952 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1953 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1954 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1955 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1958 enum { /* NVRAM to ACPI HKEY group map */
1959 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1960 TP_ACPI_HKEY_ZOOM_MASK |
1961 TP_ACPI_HKEY_DISPSWTCH_MASK |
1962 TP_ACPI_HKEY_HIBERNATE_MASK,
1963 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1964 TP_ACPI_HKEY_BRGHTDWN_MASK,
1965 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1966 TP_ACPI_HKEY_VOLDWN_MASK |
1967 TP_ACPI_HKEY_MUTE_MASK,
1970 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971 struct tp_nvram_state {
1972 u16 thinkpad_toggle:1;
1974 u16 display_toggle:1;
1975 u16 thinklight_toggle:1;
1976 u16 hibernate_toggle:1;
1977 u16 displayexp_toggle:1;
1978 u16 display_state:1;
1979 u16 brightness_toggle:1;
1980 u16 volume_toggle:1;
1983 u8 brightness_level;
1987 /* kthread for the hotkey poller */
1988 static struct task_struct *tpacpi_hotkey_task;
1991 * Acquire mutex to write poller control variables as an
1994 * Increment hotkey_config_change when changing them if you
1995 * want the kthread to forget old state.
1997 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1999 static struct mutex hotkey_thread_data_mutex;
2000 static unsigned int hotkey_config_change;
2003 * hotkey poller control variables
2005 * Must be atomic or readers will also need to acquire mutex
2007 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2008 * should be used only when the changes need to be taken as
2009 * a block, OR when one needs to force the kthread to forget
2012 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2013 static unsigned int hotkey_poll_freq = 10; /* Hz */
2015 #define HOTKEY_CONFIG_CRITICAL_START \
2017 mutex_lock(&hotkey_thread_data_mutex); \
2018 hotkey_config_change++; \
2020 #define HOTKEY_CONFIG_CRITICAL_END \
2021 mutex_unlock(&hotkey_thread_data_mutex);
2023 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2025 #define hotkey_source_mask 0U
2026 #define HOTKEY_CONFIG_CRITICAL_START
2027 #define HOTKEY_CONFIG_CRITICAL_END
2029 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2031 static struct mutex hotkey_mutex;
2033 static enum { /* Reasons for waking up */
2034 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2035 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2036 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2037 } hotkey_wakeup_reason;
2039 static int hotkey_autosleep_ack;
2041 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2042 static u32 hotkey_all_mask; /* all events supported in fw */
2043 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2044 static u32 hotkey_reserved_mask; /* events better left disabled */
2045 static u32 hotkey_driver_mask; /* events needed by the driver */
2046 static u32 hotkey_user_mask; /* events visible to userspace */
2047 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2049 static u16 *hotkey_keycode_map;
2051 static struct attribute_set *hotkey_dev_attributes;
2053 static void tpacpi_driver_event(const unsigned int hkey_event);
2054 static void hotkey_driver_event(const unsigned int scancode);
2055 static void hotkey_poll_setup(const bool may_warn);
2057 /* HKEY.MHKG() return bits */
2058 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2060 TP_ACPI_MULTI_MODE_INVALID = 0,
2061 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2062 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2063 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2064 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2065 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2066 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2067 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2071 /* The following modes are considered tablet mode for the purpose of
2072 * reporting the status to userspace. i.e. in all these modes it makes
2073 * sense to disable the laptop input devices such as touchpad and
2076 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2077 TP_ACPI_MULTI_MODE_STAND |
2078 TP_ACPI_MULTI_MODE_TENT |
2079 TP_ACPI_MULTI_MODE_STAND_TENT,
2082 static int hotkey_get_wlsw(void)
2086 if (!tp_features.hotkey_wlsw)
2089 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2091 return (tpacpi_wlsw_emulstate) ?
2092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2095 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2098 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2101 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2103 int type = (s >> 16) & 0xffff;
2104 int value = s & 0xffff;
2105 int mode = TP_ACPI_MULTI_MODE_INVALID;
2106 int valid_modes = 0;
2108 if (has_tablet_mode)
2109 *has_tablet_mode = 0;
2113 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2114 TP_ACPI_MULTI_MODE_TABLET |
2115 TP_ACPI_MULTI_MODE_STAND_TENT;
2118 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2119 TP_ACPI_MULTI_MODE_FLAT |
2120 TP_ACPI_MULTI_MODE_TABLET |
2121 TP_ACPI_MULTI_MODE_STAND |
2122 TP_ACPI_MULTI_MODE_TENT;
2125 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2126 TP_ACPI_MULTI_MODE_FLAT;
2130 /* In mode 4, FLAT is not specified as a valid mode. However,
2131 * it can be seen at least on the X1 Yoga 2nd Generation.
2133 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2134 TP_ACPI_MULTI_MODE_FLAT |
2135 TP_ACPI_MULTI_MODE_TABLET |
2136 TP_ACPI_MULTI_MODE_STAND |
2137 TP_ACPI_MULTI_MODE_TENT;
2140 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2141 type, value, TPACPI_MAIL);
2145 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2146 *has_tablet_mode = 1;
2150 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2153 mode = TP_ACPI_MULTI_MODE_FLAT;
2156 mode = TP_ACPI_MULTI_MODE_TABLET;
2160 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2162 mode = TP_ACPI_MULTI_MODE_STAND;
2165 mode = TP_ACPI_MULTI_MODE_TENT;
2168 if (type == 5 && value == 0xffff) {
2169 pr_warn("Multi mode status is undetected, assuming laptop\n");
2174 if (!(mode & valid_modes)) {
2175 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2176 value, type, TPACPI_MAIL);
2180 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2183 static int hotkey_get_tablet_mode(int *status)
2187 switch (tp_features.hotkey_tablet) {
2188 case TP_HOTKEY_TABLET_USES_MHKG:
2189 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2192 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2194 case TP_HOTKEY_TABLET_USES_GMMS:
2195 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2198 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2208 * Reads current event mask from firmware, and updates
2209 * hotkey_acpi_mask accordingly. Also resets any bits
2210 * from hotkey_user_mask that are unavailable to be
2211 * delivered (shadow requirement of the userspace ABI).
2213 * Call with hotkey_mutex held
2215 static int hotkey_mask_get(void)
2217 if (tp_features.hotkey_mask) {
2220 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2223 hotkey_acpi_mask = m;
2225 /* no mask support doesn't mean no event support... */
2226 hotkey_acpi_mask = hotkey_all_mask;
2229 /* sync userspace-visible mask */
2230 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2235 static void hotkey_mask_warn_incomplete_mask(void)
2237 /* log only what the user can fix... */
2238 const u32 wantedmask = hotkey_driver_mask &
2239 ~(hotkey_acpi_mask | hotkey_source_mask) &
2240 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2243 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2247 * Set the firmware mask when supported
2249 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2251 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2253 * Call with hotkey_mutex held
2255 static int hotkey_mask_set(u32 mask)
2260 const u32 fwmask = mask & ~hotkey_source_mask;
2262 if (tp_features.hotkey_mask) {
2263 for (i = 0; i < 32; i++) {
2264 if (!acpi_evalf(hkey_handle,
2265 NULL, "MHKM", "vdd", i + 1,
2266 !!(mask & (1 << i)))) {
2274 * We *must* make an inconditional call to hotkey_mask_get to
2275 * refresh hotkey_acpi_mask and update hotkey_user_mask
2277 * Take the opportunity to also log when we cannot _enable_
2280 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2281 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2282 fwmask, hotkey_acpi_mask);
2285 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2286 hotkey_mask_warn_incomplete_mask();
2292 * Sets hotkey_user_mask and tries to set the firmware mask
2294 * Call with hotkey_mutex held
2296 static int hotkey_user_mask_set(const u32 mask)
2300 /* Give people a chance to notice they are doing something that
2301 * is bound to go boom on their users sooner or later */
2302 if (!tp_warned.hotkey_mask_ff &&
2303 (mask == 0xffff || mask == 0xffffff ||
2304 mask == 0xffffffff)) {
2305 tp_warned.hotkey_mask_ff = 1;
2306 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2308 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2311 /* Try to enable what the user asked for, plus whatever we need.
2312 * this syncs everything but won't enable bits in hotkey_user_mask */
2313 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2315 /* Enable the available bits in hotkey_user_mask */
2316 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2322 * Sets the driver hotkey mask.
2324 * Can be called even if the hotkey subdriver is inactive
2326 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2330 /* Do the right thing if hotkey_init has not been called yet */
2331 if (!tp_features.hotkey) {
2332 hotkey_driver_mask = mask;
2336 mutex_lock(&hotkey_mutex);
2338 HOTKEY_CONFIG_CRITICAL_START
2339 hotkey_driver_mask = mask;
2340 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2341 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2343 HOTKEY_CONFIG_CRITICAL_END
2345 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2346 ~hotkey_source_mask);
2347 hotkey_poll_setup(true);
2349 mutex_unlock(&hotkey_mutex);
2354 static int hotkey_status_get(int *status)
2356 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2362 static int hotkey_status_set(bool enable)
2364 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2370 static void tpacpi_input_send_tabletsw(void)
2374 if (tp_features.hotkey_tablet &&
2375 !hotkey_get_tablet_mode(&state)) {
2376 mutex_lock(&tpacpi_inputdev_send_mutex);
2378 input_report_switch(tpacpi_inputdev,
2379 SW_TABLET_MODE, !!state);
2380 input_sync(tpacpi_inputdev);
2382 mutex_unlock(&tpacpi_inputdev_send_mutex);
2386 /* Do NOT call without validating scancode first */
2387 static void tpacpi_input_send_key(const unsigned int scancode)
2389 const unsigned int keycode = hotkey_keycode_map[scancode];
2391 if (keycode != KEY_RESERVED) {
2392 mutex_lock(&tpacpi_inputdev_send_mutex);
2394 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2395 input_report_key(tpacpi_inputdev, keycode, 1);
2396 input_sync(tpacpi_inputdev);
2398 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2399 input_report_key(tpacpi_inputdev, keycode, 0);
2400 input_sync(tpacpi_inputdev);
2402 mutex_unlock(&tpacpi_inputdev_send_mutex);
2406 /* Do NOT call without validating scancode first */
2407 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2409 hotkey_driver_event(scancode);
2410 if (hotkey_user_mask & (1 << scancode))
2411 tpacpi_input_send_key(scancode);
2414 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2415 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2417 /* Do NOT call without validating scancode first */
2418 static void tpacpi_hotkey_send_key(unsigned int scancode)
2420 tpacpi_input_send_key_masked(scancode);
2423 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2427 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2428 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2429 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2430 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2431 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2432 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2434 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2435 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2436 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2438 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2439 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2440 n->displayexp_toggle =
2441 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2443 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2444 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2445 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2446 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2447 n->brightness_toggle =
2448 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2450 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2451 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2452 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2453 >> TP_NVRAM_POS_LEVEL_VOLUME;
2454 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2455 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2459 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2461 if ((event_mask & (1 << __scancode)) && \
2462 oldn->__member != newn->__member) \
2463 tpacpi_hotkey_send_key(__scancode); \
2466 #define TPACPI_MAY_SEND_KEY(__scancode) \
2468 if (event_mask & (1 << __scancode)) \
2469 tpacpi_hotkey_send_key(__scancode); \
2472 static void issue_volchange(const unsigned int oldvol,
2473 const unsigned int newvol,
2474 const u32 event_mask)
2476 unsigned int i = oldvol;
2478 while (i > newvol) {
2479 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2482 while (i < newvol) {
2483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2488 static void issue_brightnesschange(const unsigned int oldbrt,
2489 const unsigned int newbrt,
2490 const u32 event_mask)
2492 unsigned int i = oldbrt;
2494 while (i > newbrt) {
2495 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2498 while (i < newbrt) {
2499 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2504 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2505 struct tp_nvram_state *newn,
2506 const u32 event_mask)
2509 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2510 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2511 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2512 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2514 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2516 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2521 * This code is supposed to duplicate the IBM firmware behaviour:
2522 * - Pressing MUTE issues mute hotkey message, even when already mute
2523 * - Pressing Volume up/down issues volume up/down hotkey messages,
2524 * even when already at maximum or minimum volume
2525 * - The act of unmuting issues volume up/down notification,
2526 * depending which key was used to unmute
2528 * We are constrained to what the NVRAM can tell us, which is not much
2529 * and certainly not enough if more than one volume hotkey was pressed
2530 * since the last poll cycle.
2532 * Just to make our life interesting, some newer Lenovo ThinkPads have
2533 * bugs in the BIOS and may fail to update volume_toggle properly.
2538 oldn->volume_toggle != newn->volume_toggle ||
2539 oldn->volume_level != newn->volume_level) {
2540 /* recently muted, or repeated mute keypress, or
2541 * multiple presses ending in mute */
2542 issue_volchange(oldn->volume_level, newn->volume_level,
2544 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2549 /* recently unmuted, issue 'unmute' keypress */
2550 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2552 if (oldn->volume_level != newn->volume_level) {
2553 issue_volchange(oldn->volume_level, newn->volume_level,
2555 } else if (oldn->volume_toggle != newn->volume_toggle) {
2556 /* repeated vol up/down keypress at end of scale ? */
2557 if (newn->volume_level == 0)
2558 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2559 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2560 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2564 /* handle brightness */
2565 if (oldn->brightness_level != newn->brightness_level) {
2566 issue_brightnesschange(oldn->brightness_level,
2567 newn->brightness_level, event_mask);
2568 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2569 /* repeated key presses that didn't change state */
2570 if (newn->brightness_level == 0)
2571 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2572 else if (newn->brightness_level >= bright_maxlvl
2573 && !tp_features.bright_unkfw)
2574 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2577 #undef TPACPI_COMPARE_KEY
2578 #undef TPACPI_MAY_SEND_KEY
2584 * We track all events in hotkey_source_mask all the time, since
2585 * most of them are edge-based. We only issue those requested by
2586 * hotkey_user_mask or hotkey_driver_mask, though.
2588 static int hotkey_kthread(void *data)
2590 struct tp_nvram_state s[2];
2591 u32 poll_mask, event_mask;
2592 unsigned int si, so;
2594 unsigned int change_detector;
2595 unsigned int poll_freq;
2598 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2607 /* Initial state for compares */
2608 mutex_lock(&hotkey_thread_data_mutex);
2609 change_detector = hotkey_config_change;
2610 poll_mask = hotkey_source_mask;
2611 event_mask = hotkey_source_mask &
2612 (hotkey_driver_mask | hotkey_user_mask);
2613 poll_freq = hotkey_poll_freq;
2614 mutex_unlock(&hotkey_thread_data_mutex);
2615 hotkey_read_nvram(&s[so], poll_mask);
2617 while (!kthread_should_stop()) {
2619 if (likely(poll_freq))
2622 t = 100; /* should never happen... */
2624 t = msleep_interruptible(t);
2625 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2628 if (t > 0 && !was_frozen)
2631 mutex_lock(&hotkey_thread_data_mutex);
2632 if (was_frozen || hotkey_config_change != change_detector) {
2633 /* forget old state on thaw or config change */
2636 change_detector = hotkey_config_change;
2638 poll_mask = hotkey_source_mask;
2639 event_mask = hotkey_source_mask &
2640 (hotkey_driver_mask | hotkey_user_mask);
2641 poll_freq = hotkey_poll_freq;
2642 mutex_unlock(&hotkey_thread_data_mutex);
2644 if (likely(poll_mask)) {
2645 hotkey_read_nvram(&s[si], poll_mask);
2646 if (likely(si != so)) {
2647 hotkey_compare_and_issue_event(&s[so], &s[si],
2660 /* call with hotkey_mutex held */
2661 static void hotkey_poll_stop_sync(void)
2663 if (tpacpi_hotkey_task) {
2664 kthread_stop(tpacpi_hotkey_task);
2665 tpacpi_hotkey_task = NULL;
2669 /* call with hotkey_mutex held */
2670 static void hotkey_poll_setup(const bool may_warn)
2672 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2673 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2675 if (hotkey_poll_freq > 0 &&
2676 (poll_driver_mask ||
2677 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2678 if (!tpacpi_hotkey_task) {
2679 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2680 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2681 if (IS_ERR(tpacpi_hotkey_task)) {
2682 tpacpi_hotkey_task = NULL;
2683 pr_err("could not create kernel thread for hotkey polling\n");
2687 hotkey_poll_stop_sync();
2688 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2689 hotkey_poll_freq == 0) {
2690 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2691 poll_user_mask, poll_driver_mask);
2696 static void hotkey_poll_setup_safe(const bool may_warn)
2698 mutex_lock(&hotkey_mutex);
2699 hotkey_poll_setup(may_warn);
2700 mutex_unlock(&hotkey_mutex);
2703 /* call with hotkey_mutex held */
2704 static void hotkey_poll_set_freq(unsigned int freq)
2707 hotkey_poll_stop_sync();
2709 hotkey_poll_freq = freq;
2712 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2714 static void hotkey_poll_setup(const bool __unused)
2718 static void hotkey_poll_setup_safe(const bool __unused)
2722 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2724 static int hotkey_inputdev_open(struct input_dev *dev)
2726 switch (tpacpi_lifecycle) {
2727 case TPACPI_LIFE_INIT:
2728 case TPACPI_LIFE_RUNNING:
2729 hotkey_poll_setup_safe(false);
2731 case TPACPI_LIFE_EXITING:
2735 /* Should only happen if tpacpi_lifecycle is corrupt */
2740 static void hotkey_inputdev_close(struct input_dev *dev)
2742 /* disable hotkey polling when possible */
2743 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2744 !(hotkey_source_mask & hotkey_driver_mask))
2745 hotkey_poll_setup_safe(false);
2748 /* sysfs hotkey enable ------------------------------------------------- */
2749 static ssize_t hotkey_enable_show(struct device *dev,
2750 struct device_attribute *attr,
2755 printk_deprecated_attribute("hotkey_enable",
2756 "Hotkey reporting is always enabled");
2758 res = hotkey_status_get(&status);
2762 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2765 static ssize_t hotkey_enable_store(struct device *dev,
2766 struct device_attribute *attr,
2767 const char *buf, size_t count)
2771 printk_deprecated_attribute("hotkey_enable",
2772 "Hotkeys can be disabled through hotkey_mask");
2774 if (parse_strtoul(buf, 1, &t))
2783 static DEVICE_ATTR_RW(hotkey_enable);
2785 /* sysfs hotkey mask --------------------------------------------------- */
2786 static ssize_t hotkey_mask_show(struct device *dev,
2787 struct device_attribute *attr,
2790 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2793 static ssize_t hotkey_mask_store(struct device *dev,
2794 struct device_attribute *attr,
2795 const char *buf, size_t count)
2800 if (parse_strtoul(buf, 0xffffffffUL, &t))
2803 if (mutex_lock_killable(&hotkey_mutex))
2804 return -ERESTARTSYS;
2806 res = hotkey_user_mask_set(t);
2808 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2809 hotkey_poll_setup(true);
2812 mutex_unlock(&hotkey_mutex);
2814 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2816 return (res) ? res : count;
2819 static DEVICE_ATTR_RW(hotkey_mask);
2821 /* sysfs hotkey bios_enabled ------------------------------------------- */
2822 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2823 struct device_attribute *attr,
2826 return sprintf(buf, "0\n");
2829 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2831 /* sysfs hotkey bios_mask ---------------------------------------------- */
2832 static ssize_t hotkey_bios_mask_show(struct device *dev,
2833 struct device_attribute *attr,
2836 printk_deprecated_attribute("hotkey_bios_mask",
2837 "This attribute is useless.");
2838 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2841 static DEVICE_ATTR_RO(hotkey_bios_mask);
2843 /* sysfs hotkey all_mask ----------------------------------------------- */
2844 static ssize_t hotkey_all_mask_show(struct device *dev,
2845 struct device_attribute *attr,
2848 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2849 hotkey_all_mask | hotkey_source_mask);
2852 static DEVICE_ATTR_RO(hotkey_all_mask);
2854 /* sysfs hotkey all_mask ----------------------------------------------- */
2855 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2856 struct device_attribute *attr,
2859 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2860 hotkey_adaptive_all_mask | hotkey_source_mask);
2863 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2865 /* sysfs hotkey recommended_mask --------------------------------------- */
2866 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2867 struct device_attribute *attr,
2870 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2871 (hotkey_all_mask | hotkey_source_mask)
2872 & ~hotkey_reserved_mask);
2875 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2877 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2879 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2880 static ssize_t hotkey_source_mask_show(struct device *dev,
2881 struct device_attribute *attr,
2884 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2887 static ssize_t hotkey_source_mask_store(struct device *dev,
2888 struct device_attribute *attr,
2889 const char *buf, size_t count)
2895 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2896 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2899 if (mutex_lock_killable(&hotkey_mutex))
2900 return -ERESTARTSYS;
2902 HOTKEY_CONFIG_CRITICAL_START
2903 hotkey_source_mask = t;
2904 HOTKEY_CONFIG_CRITICAL_END
2906 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2907 ~hotkey_source_mask);
2908 hotkey_poll_setup(true);
2910 /* check if events needed by the driver got disabled */
2911 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2912 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2914 mutex_unlock(&hotkey_mutex);
2917 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2920 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2923 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2925 return (rc < 0) ? rc : count;
2928 static DEVICE_ATTR_RW(hotkey_source_mask);
2930 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2931 static ssize_t hotkey_poll_freq_show(struct device *dev,
2932 struct device_attribute *attr,
2935 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2938 static ssize_t hotkey_poll_freq_store(struct device *dev,
2939 struct device_attribute *attr,
2940 const char *buf, size_t count)
2944 if (parse_strtoul(buf, 25, &t))
2947 if (mutex_lock_killable(&hotkey_mutex))
2948 return -ERESTARTSYS;
2950 hotkey_poll_set_freq(t);
2951 hotkey_poll_setup(true);
2953 mutex_unlock(&hotkey_mutex);
2955 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2960 static DEVICE_ATTR_RW(hotkey_poll_freq);
2962 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2964 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2965 static ssize_t hotkey_radio_sw_show(struct device *dev,
2966 struct device_attribute *attr,
2970 res = hotkey_get_wlsw();
2974 /* Opportunistic update */
2975 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2977 return snprintf(buf, PAGE_SIZE, "%d\n",
2978 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2981 static DEVICE_ATTR_RO(hotkey_radio_sw);
2983 static void hotkey_radio_sw_notify_change(void)
2985 if (tp_features.hotkey_wlsw)
2986 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2990 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2991 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2992 struct device_attribute *attr,
2996 res = hotkey_get_tablet_mode(&s);
3000 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3003 static DEVICE_ATTR_RO(hotkey_tablet_mode);
3005 static void hotkey_tablet_mode_notify_change(void)
3007 if (tp_features.hotkey_tablet)
3008 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3009 "hotkey_tablet_mode");
3012 /* sysfs wakeup reason (pollable) -------------------------------------- */
3013 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3014 struct device_attribute *attr,
3017 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3020 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3022 static void hotkey_wakeup_reason_notify_change(void)
3024 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3028 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3029 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3030 struct device_attribute *attr,
3033 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3036 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3037 hotkey_wakeup_hotunplug_complete_show, NULL);
3039 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3041 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3042 "wakeup_hotunplug_complete");
3045 /* sysfs adaptive kbd mode --------------------------------------------- */
3047 static int adaptive_keyboard_get_mode(void);
3048 static int adaptive_keyboard_set_mode(int new_mode);
3050 enum ADAPTIVE_KEY_MODE {
3053 WEB_CONFERENCE_MODE,
3058 static ssize_t adaptive_kbd_mode_show(struct device *dev,
3059 struct device_attribute *attr,
3064 current_mode = adaptive_keyboard_get_mode();
3065 if (current_mode < 0)
3066 return current_mode;
3068 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3071 static ssize_t adaptive_kbd_mode_store(struct device *dev,
3072 struct device_attribute *attr,
3073 const char *buf, size_t count)
3078 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3081 res = adaptive_keyboard_set_mode(t);
3082 return (res < 0) ? res : count;
3085 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3087 static struct attribute *adaptive_kbd_attributes[] = {
3088 &dev_attr_adaptive_kbd_mode.attr,
3092 static const struct attribute_group adaptive_kbd_attr_group = {
3093 .attrs = adaptive_kbd_attributes,
3096 /* --------------------------------------------------------------------- */
3098 static struct attribute *hotkey_attributes[] __initdata = {
3099 &dev_attr_hotkey_enable.attr,
3100 &dev_attr_hotkey_bios_enabled.attr,
3101 &dev_attr_hotkey_bios_mask.attr,
3102 &dev_attr_wakeup_reason.attr,
3103 &dev_attr_wakeup_hotunplug_complete.attr,
3104 &dev_attr_hotkey_mask.attr,
3105 &dev_attr_hotkey_all_mask.attr,
3106 &dev_attr_hotkey_adaptive_all_mask.attr,
3107 &dev_attr_hotkey_recommended_mask.attr,
3108 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3109 &dev_attr_hotkey_source_mask.attr,
3110 &dev_attr_hotkey_poll_freq.attr,
3115 * Sync both the hw and sw blocking state of all switches
3117 static void tpacpi_send_radiosw_update(void)
3122 * We must sync all rfkill controllers *before* issuing any
3123 * rfkill input events, or we will race the rfkill core input
3126 * tpacpi_inputdev_send_mutex works as a synchronization point
3129 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3132 wlsw = hotkey_get_wlsw();
3134 /* Sync hw blocking state first if it is hw-blocked */
3135 if (wlsw == TPACPI_RFK_RADIO_OFF)
3136 tpacpi_rfk_update_hwblock_state(true);
3138 /* Sync sw blocking state */
3139 tpacpi_rfk_update_swstate_all();
3141 /* Sync hw blocking state last if it is hw-unblocked */
3142 if (wlsw == TPACPI_RFK_RADIO_ON)
3143 tpacpi_rfk_update_hwblock_state(false);
3145 /* Issue rfkill input event for WLSW switch */
3147 mutex_lock(&tpacpi_inputdev_send_mutex);
3149 input_report_switch(tpacpi_inputdev,
3150 SW_RFKILL_ALL, (wlsw > 0));
3151 input_sync(tpacpi_inputdev);
3153 mutex_unlock(&tpacpi_inputdev_send_mutex);
3157 * this can be unconditional, as we will poll state again
3158 * if userspace uses the notify to read data
3160 hotkey_radio_sw_notify_change();
3163 static void hotkey_exit(void)
3165 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3166 mutex_lock(&hotkey_mutex);
3167 hotkey_poll_stop_sync();
3168 mutex_unlock(&hotkey_mutex);
3171 if (hotkey_dev_attributes)
3172 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3174 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3175 "restoring original HKEY status and mask\n");
3176 /* yes, there is a bitwise or below, we want the
3177 * functions to be called even if one of them fail */
3178 if (((tp_features.hotkey_mask &&
3179 hotkey_mask_set(hotkey_orig_mask)) |
3180 hotkey_status_set(false)) != 0)
3181 pr_err("failed to restore hot key mask to BIOS defaults\n");
3184 static void __init hotkey_unmap(const unsigned int&n