1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * 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>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define TPACPI_VERSION "0.26"
12 #define TPACPI_SYSFS_VERSION 0x030000
16 * 2007-10-20 changelog trimmed down
18 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
21 * 2006-11-22 0.13 new maintainer
22 * changelog now lives in git commit history, and will
23 * not be updated further in-file.
25 * 2005-03-17 0.11 support for 600e, 770x
26 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
28 * 2005-01-16 0.9 use MODULE_VERSION
29 * thanks to Henrik Brix Andersen <brix@gentoo.org>
30 * fix parameter passing on module loading
31 * thanks to Rusty Russell <rusty@rustcorp.com.au>
32 * thanks to Jim Radford <radford@blackbean.org>
33 * 2004-11-08 0.8 fix init error case, don't return from a macro
34 * thanks to Chris Wright <chrisw@osdl.org>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/string.h>
42 #include <linux/list.h>
43 #include <linux/mutex.h>
44 #include <linux/sched.h>
45 #include <linux/sched/signal.h>
46 #include <linux/kthread.h>
47 #include <linux/freezer.h>
48 #include <linux/delay.h>
49 #include <linux/slab.h>
50 #include <linux/nvram.h>
51 #include <linux/proc_fs.h>
52 #include <linux/seq_file.h>
53 #include <linux/sysfs.h>
54 #include <linux/backlight.h>
55 #include <linux/bitops.h>
57 #include <linux/platform_device.h>
58 #include <linux/hwmon.h>
59 #include <linux/hwmon-sysfs.h>
60 #include <linux/input.h>
61 #include <linux/leds.h>
62 #include <linux/rfkill.h>
63 #include <linux/dmi.h>
64 #include <linux/jiffies.h>
65 #include <linux/workqueue.h>
66 #include <linux/acpi.h>
67 #include <linux/pci.h>
68 #include <linux/power_supply.h>
69 #include <linux/platform_profile.h>
70 #include <sound/core.h>
71 #include <sound/control.h>
72 #include <sound/initval.h>
73 #include <linux/uaccess.h>
74 #include <acpi/battery.h>
75 #include <acpi/video.h>
77 /* ThinkPad CMOS commands */
78 #define TP_CMOS_VOLUME_DOWN 0
79 #define TP_CMOS_VOLUME_UP 1
80 #define TP_CMOS_VOLUME_MUTE 2
81 #define TP_CMOS_BRIGHTNESS_UP 4
82 #define TP_CMOS_BRIGHTNESS_DOWN 5
83 #define TP_CMOS_THINKLIGHT_ON 12
84 #define TP_CMOS_THINKLIGHT_OFF 13
88 TP_NVRAM_ADDR_HK2 = 0x57,
89 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
90 TP_NVRAM_ADDR_VIDEO = 0x59,
91 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
92 TP_NVRAM_ADDR_MIXER = 0x60,
97 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
98 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
99 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
100 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
101 TP_NVRAM_MASK_THINKLIGHT = 0x10,
102 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
103 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
104 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
105 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
106 TP_NVRAM_MASK_MUTE = 0x40,
107 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
108 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
109 TP_NVRAM_POS_LEVEL_VOLUME = 0,
112 /* Misc NVRAM-related */
114 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
118 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
119 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
120 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
121 #define TPACPI_ACPI_EC_HID "PNP0C09"
124 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
125 #define TPACPI_HKEY_INPUT_VERSION 0x4101
127 /* ACPI \WGSV commands */
129 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
130 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
131 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
132 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
135 /* TP_ACPI_WGSV_GET_STATE bits */
137 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
138 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
139 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
140 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
141 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
142 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
143 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
144 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
145 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
146 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
150 enum tpacpi_hkey_event_t {
152 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
153 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
154 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
155 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
156 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
157 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
158 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
160 /* Reasons for waking up from S3/S4 */
161 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
162 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
163 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
164 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
165 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
166 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
168 /* Auto-sleep after eject request */
169 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
170 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
172 /* Misc bay events */
173 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
174 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
175 or port replicator */
176 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
177 dock or port replicator */
179 * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
180 * when keyboard cover is attached, detached or folded onto the back
182 TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
183 TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
191 * enter/leave tablet mode
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
197 /* Key-related user-interface events */
198 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
199 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
200 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
203 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
204 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
205 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
206 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
207 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
208 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
209 * command completed. Related to
211 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
212 * changed. Related to AML GMTS */
214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
217 /* Further user-interface events */
218 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
219 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
222 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
225 /****************************************************************************
229 #define TPACPI_NAME "thinkpad"
230 #define TPACPI_DESC "ThinkPad ACPI Extras"
231 #define TPACPI_FILE TPACPI_NAME "_acpi"
232 #define TPACPI_URL "http://ibm-acpi.sf.net/"
233 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
235 #define TPACPI_PROC_DIR "ibm"
236 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
237 #define TPACPI_DRVR_NAME TPACPI_FILE
238 #define TPACPI_DRVR_SHORTNAME "tpacpi"
239 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
241 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
242 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
244 #define TPACPI_MAX_ACPI_ARGS 3
246 /* Debugging printk groups */
247 #define TPACPI_DBG_ALL 0xffff
248 #define TPACPI_DBG_DISCLOSETASK 0x8000
249 #define TPACPI_DBG_INIT 0x0001
250 #define TPACPI_DBG_EXIT 0x0002
251 #define TPACPI_DBG_RFKILL 0x0004
252 #define TPACPI_DBG_HKEY 0x0008
253 #define TPACPI_DBG_FAN 0x0010
254 #define TPACPI_DBG_BRGHT 0x0020
255 #define TPACPI_DBG_MIXER 0x0040
257 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
258 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
259 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
262 /****************************************************************************
263 * Driver-wide structs and misc. variables
268 struct tp_acpi_drv_struct {
269 const struct acpi_device_id *hid;
270 struct acpi_driver *driver;
272 void (*notify) (struct ibm_struct *, u32);
275 struct acpi_device *device;
281 int (*read) (struct seq_file *);
282 int (*write) (char *);
284 void (*resume) (void);
285 void (*suspend) (void);
286 void (*shutdown) (void);
288 struct list_head all_drivers;
290 struct tp_acpi_drv_struct *acpi;
293 u8 acpi_driver_registered:1;
294 u8 acpi_notify_installed:1;
301 struct ibm_init_struct {
304 int (*init) (struct ibm_init_struct *);
305 umode_t base_procfs_mode;
306 struct ibm_struct *data;
315 TP_HOTKEY_TABLET_NONE = 0,
316 TP_HOTKEY_TABLET_USES_MHKG,
317 TP_HOTKEY_TABLET_USES_GMMS,
322 u32 bright_acpimode:1;
326 u32 fan_ctrl_status_undef:1;
328 u32 second_fan_ctl:1;
329 u32 beep_needs_two_args:1;
330 u32 mixer_no_level_control:1;
331 u32 battery_force_primary:1;
332 u32 input_device_registered:1;
333 u32 platform_drv_registered:1;
334 u32 platform_drv_attrs_registered:1;
335 u32 sensors_pdrv_registered:1;
336 u32 sensors_pdrv_attrs_registered:1;
337 u32 sensors_pdev_attrs_registered:1;
338 u32 hotkey_poll_active:1;
339 u32 has_adaptive_kbd:1;
343 u16 hotkey_mask_ff:1;
344 u16 volume_ctrl_forbidden:1;
347 struct thinkpad_id_data {
348 unsigned int vendor; /* ThinkPad vendor:
349 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
351 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
352 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
354 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
356 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
359 char *model_str; /* ThinkPad T43 */
360 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
362 static struct thinkpad_id_data thinkpad_id;
365 TPACPI_LIFE_INIT = 0,
370 static int experimental;
371 static u32 dbg_level;
373 static struct workqueue_struct *tpacpi_wq;
381 /* tpacpi LED class */
382 struct tpacpi_led_classdev {
383 struct led_classdev led_classdev;
387 /* brightness level capabilities */
388 static unsigned int bright_maxlvl; /* 0 = unknown */
390 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
391 static int dbg_wlswemul;
392 static bool tpacpi_wlsw_emulstate;
393 static int dbg_bluetoothemul;
394 static bool tpacpi_bluetooth_emulstate;
395 static int dbg_wwanemul;
396 static bool tpacpi_wwan_emulstate;
397 static int dbg_uwbemul;
398 static bool tpacpi_uwb_emulstate;
402 /*************************************************************************
406 #define dbg_printk(a_dbg_level, format, arg...) \
408 if (dbg_level & (a_dbg_level)) \
409 printk(KERN_DEBUG pr_fmt("%s: " format), \
413 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
414 #define vdbg_printk dbg_printk
415 static const char *str_supported(int is_supported);
417 static inline const char *str_supported(int is_supported) { return ""; }
418 #define vdbg_printk(a_dbg_level, format, arg...) \
419 do { if (0) no_printk(format, ##arg); } while (0)
422 static void tpacpi_log_usertask(const char * const what)
424 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
425 what, task_tgid_vnr(current));
428 #define tpacpi_disclose_usertask(what, format, arg...) \
430 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
431 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
432 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
433 what, task_tgid_vnr(current), ## arg); \
438 * Quirk handling helpers
440 * ThinkPad IDs and versions seen in the field so far are
441 * two or three characters from the set [0-9A-Z], i.e. base 36.
443 * We use values well outside that range as specials.
446 #define TPACPI_MATCH_ANY 0xffffffffU
447 #define TPACPI_MATCH_ANY_VERSION 0xffffU
448 #define TPACPI_MATCH_UNKNOWN 0U
450 /* TPID('1', 'Y') == 0x3159 */
451 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
452 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
455 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
456 { .vendor = PCI_VENDOR_ID_IBM, \
457 .bios = TPID(__id1, __id2), \
458 .ec = TPACPI_MATCH_ANY, \
459 .quirks = (__quirk) }
461 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
462 { .vendor = PCI_VENDOR_ID_LENOVO, \
463 .bios = TPID(__id1, __id2), \
464 .ec = TPACPI_MATCH_ANY, \
465 .quirks = (__quirk) }
467 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
468 { .vendor = PCI_VENDOR_ID_LENOVO, \
469 .bios = TPID3(__id1, __id2, __id3), \
470 .ec = TPACPI_MATCH_ANY, \
471 .quirks = (__quirk) }
473 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
474 { .vendor = PCI_VENDOR_ID_IBM, \
475 .bios = TPACPI_MATCH_ANY, \
476 .ec = TPID(__id1, __id2), \
477 .quirks = (__quirk) }
479 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
480 { .vendor = PCI_VENDOR_ID_LENOVO, \
481 .bios = TPACPI_MATCH_ANY, \
482 .ec = TPID(__id1, __id2), \
483 .quirks = (__quirk) }
485 struct tpacpi_quirk {
489 unsigned long quirks;
493 * tpacpi_check_quirks() - search BIOS/EC version on a list
494 * @qlist: array of &struct tpacpi_quirk
495 * @qlist_size: number of elements in @qlist
497 * Iterates over a quirks list until one is found that matches the
498 * ThinkPad's vendor, BIOS and EC model.
500 * Returns 0 if nothing matches, otherwise returns the quirks field of
501 * the matching &struct tpacpi_quirk entry.
503 * The match criteria is: vendor, ec and bios much match.
505 static unsigned long __init tpacpi_check_quirks(
506 const struct tpacpi_quirk *qlist,
507 unsigned int qlist_size)
510 if ((qlist->vendor == thinkpad_id.vendor ||
511 qlist->vendor == TPACPI_MATCH_ANY) &&
512 (qlist->bios == thinkpad_id.bios_model ||
513 qlist->bios == TPACPI_MATCH_ANY) &&
514 (qlist->ec == thinkpad_id.ec_model ||
515 qlist->ec == TPACPI_MATCH_ANY))
516 return qlist->quirks;
524 static inline bool __pure __init tpacpi_is_lenovo(void)
526 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
529 static inline bool __pure __init tpacpi_is_ibm(void)
531 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
534 /****************************************************************************
535 ****************************************************************************
537 * ACPI Helpers and device model
539 ****************************************************************************
540 ****************************************************************************/
542 /*************************************************************************
546 static acpi_handle root_handle;
547 static acpi_handle ec_handle;
549 #define TPACPI_HANDLE(object, parent, paths...) \
550 static acpi_handle object##_handle; \
551 static const acpi_handle * const object##_parent __initconst = \
553 static char *object##_paths[] __initdata = { paths }
555 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
556 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
558 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
560 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
561 "\\CMS", /* R40, R40e */
564 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
565 "^HKEY", /* R30, R31 */
566 "HKEY", /* all others */
569 /*************************************************************************
573 static int acpi_evalf(acpi_handle handle,
574 int *res, char *method, char *fmt, ...)
577 struct acpi_object_list params;
578 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
579 struct acpi_buffer result, *resultp;
580 union acpi_object out_obj;
588 pr_err("acpi_evalf() called with empty format\n");
601 params.pointer = &in_objs[0];
608 in_objs[params.count].integer.value = va_arg(ap, int);
609 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
611 /* add more types as needed */
613 pr_err("acpi_evalf() called with invalid format character '%c'\n",
621 if (res_type != 'v') {
622 result.length = sizeof(out_obj);
623 result.pointer = &out_obj;
628 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
632 success = (status == AE_OK &&
633 out_obj.type == ACPI_TYPE_INTEGER);
635 *res = out_obj.integer.value;
638 success = status == AE_OK;
640 /* add more types as needed */
642 pr_err("acpi_evalf() called with invalid format character '%c'\n",
647 if (!success && !quiet)
648 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
649 method, fmt0, acpi_format_exception(status));
654 static int acpi_ec_read(int i, u8 *p)
659 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
663 if (ec_read(i, p) < 0)
670 static int acpi_ec_write(int i, u8 v)
673 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
676 if (ec_write(i, v) < 0)
683 static int issue_thinkpad_cmos_command(int cmos_cmd)
688 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
694 /*************************************************************************
698 #define TPACPI_ACPIHANDLE_INIT(object) \
699 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
700 object##_paths, ARRAY_SIZE(object##_paths))
702 static void __init drv_acpi_handle_init(const char *name,
703 acpi_handle *handle, const acpi_handle parent,
704 char **paths, const int num_paths)
709 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
712 for (i = 0; i < num_paths; i++) {
713 status = acpi_get_handle(parent, paths[i], handle);
714 if (ACPI_SUCCESS(status)) {
715 dbg_printk(TPACPI_DBG_INIT,
716 "Found ACPI handle %s for %s\n",
722 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
727 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
728 u32 level, void *context, void **return_value)
730 struct acpi_device *dev;
731 if (!strcmp(context, "video")) {
732 if (acpi_bus_get_device(handle, &dev))
734 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
738 *(acpi_handle *)return_value = handle;
740 return AE_CTRL_TERMINATE;
743 static void __init tpacpi_acpi_handle_locate(const char *name,
748 acpi_handle device_found;
750 BUG_ON(!name || !handle);
751 vdbg_printk(TPACPI_DBG_INIT,
752 "trying to locate ACPI handle for %s, using HID %s\n",
753 name, hid ? hid : "NULL");
755 memset(&device_found, 0, sizeof(device_found));
756 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
757 (void *)name, &device_found);
761 if (ACPI_SUCCESS(status)) {
762 *handle = device_found;
763 dbg_printk(TPACPI_DBG_INIT,
764 "Found ACPI handle for %s\n", name);
766 vdbg_printk(TPACPI_DBG_INIT,
767 "Could not locate an ACPI handle for %s: %s\n",
768 name, acpi_format_exception(status));
772 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
774 struct ibm_struct *ibm = data;
776 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
779 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
782 ibm->acpi->notify(ibm, event);
785 static int __init setup_acpi_notify(struct ibm_struct *ibm)
792 if (!*ibm->acpi->handle)
795 vdbg_printk(TPACPI_DBG_INIT,
796 "setting up ACPI notify for %s\n", ibm->name);
798 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
800 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
804 ibm->acpi->device->driver_data = ibm;
805 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
806 TPACPI_ACPI_EVENT_PREFIX,
809 status = acpi_install_notify_handler(*ibm->acpi->handle,
810 ibm->acpi->type, dispatch_acpi_notify, ibm);
811 if (ACPI_FAILURE(status)) {
812 if (status == AE_ALREADY_EXISTS) {
813 pr_notice("another device driver is already handling %s events\n",
816 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
817 ibm->name, acpi_format_exception(status));
821 ibm->flags.acpi_notify_installed = 1;
825 static int __init tpacpi_device_add(struct acpi_device *device)
830 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
834 dbg_printk(TPACPI_DBG_INIT,
835 "registering %s as an ACPI driver\n", ibm->name);
839 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
840 if (!ibm->acpi->driver) {
841 pr_err("failed to allocate memory for ibm->acpi->driver\n");
845 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
846 ibm->acpi->driver->ids = ibm->acpi->hid;
848 ibm->acpi->driver->ops.add = &tpacpi_device_add;
850 rc = acpi_bus_register_driver(ibm->acpi->driver);
852 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
854 kfree(ibm->acpi->driver);
855 ibm->acpi->driver = NULL;
857 ibm->flags.acpi_driver_registered = 1;
863 /****************************************************************************
864 ****************************************************************************
868 ****************************************************************************
869 ****************************************************************************/
871 static int dispatch_proc_show(struct seq_file *m, void *v)
873 struct ibm_struct *ibm = m->private;
875 if (!ibm || !ibm->read)
880 static int dispatch_proc_open(struct inode *inode, struct file *file)
882 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
885 static ssize_t dispatch_proc_write(struct file *file,
886 const char __user *userbuf,
887 size_t count, loff_t *pos)
889 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
893 if (!ibm || !ibm->write)
895 if (count > PAGE_SIZE - 1)
898 kernbuf = kmalloc(count + 1, GFP_KERNEL);
902 if (copy_from_user(kernbuf, userbuf, count)) {
908 ret = ibm->write(kernbuf);
917 static const struct proc_ops dispatch_proc_ops = {
918 .proc_open = dispatch_proc_open,
919 .proc_read = seq_read,
920 .proc_lseek = seq_lseek,
921 .proc_release = single_release,
922 .proc_write = dispatch_proc_write,
925 /****************************************************************************
926 ****************************************************************************
928 * Device model: input, hwmon and platform
930 ****************************************************************************
931 ****************************************************************************/
933 static struct platform_device *tpacpi_pdev;
934 static struct platform_device *tpacpi_sensors_pdev;
935 static struct device *tpacpi_hwmon;
936 static struct input_dev *tpacpi_inputdev;
937 static struct mutex tpacpi_inputdev_send_mutex;
938 static LIST_HEAD(tpacpi_all_drivers);
940 #ifdef CONFIG_PM_SLEEP
941 static int tpacpi_suspend_handler(struct device *dev)
943 struct ibm_struct *ibm, *itmp;
945 list_for_each_entry_safe(ibm, itmp,
955 static int tpacpi_resume_handler(struct device *dev)
957 struct ibm_struct *ibm, *itmp;
959 list_for_each_entry_safe(ibm, itmp,
970 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
971 tpacpi_suspend_handler, tpacpi_resume_handler);
973 static void tpacpi_shutdown_handler(struct platform_device *pdev)
975 struct ibm_struct *ibm, *itmp;
977 list_for_each_entry_safe(ibm, itmp,
985 static struct platform_driver tpacpi_pdriver = {
987 .name = TPACPI_DRVR_NAME,
990 .shutdown = tpacpi_shutdown_handler,
993 static struct platform_driver tpacpi_hwmon_pdriver = {
995 .name = TPACPI_HWMON_DRVR_NAME,
999 /*************************************************************************
1000 * sysfs support helpers
1003 struct attribute_set {
1004 unsigned int members, max_members;
1005 struct attribute_group group;
1008 struct attribute_set_obj {
1009 struct attribute_set s;
1010 struct attribute *a;
1011 } __attribute__((packed));
1013 static struct attribute_set *create_attr_set(unsigned int max_members,
1016 struct attribute_set_obj *sobj;
1018 if (max_members == 0)
1021 /* Allocates space for implicit NULL at the end too */
1022 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1023 max_members * sizeof(struct attribute *),
1027 sobj->s.max_members = max_members;
1028 sobj->s.group.attrs = &sobj->a;
1029 sobj->s.group.name = name;
1034 #define destroy_attr_set(_set) \
1037 /* not multi-threaded safe, use it in a single thread per set */
1038 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1043 if (s->members >= s->max_members)
1046 s->group.attrs[s->members] = attr;
1052 static int add_many_to_attr_set(struct attribute_set *s,
1053 struct attribute **attr,
1058 for (i = 0; i < count; i++) {
1059 res = add_to_attr_set(s, attr[i]);
1067 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1069 sysfs_remove_group(kobj, &s->group);
1070 destroy_attr_set(s);
1073 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1074 sysfs_create_group(_kobj, &_attr_set->group)
1076 static int parse_strtoul(const char *buf,
1077 unsigned long max, unsigned long *value)
1081 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1082 endp = skip_spaces(endp);
1083 if (*endp || *value > max)
1089 static void tpacpi_disable_brightness_delay(void)
1091 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1092 pr_notice("ACPI backlight control delay disabled\n");
1095 static void printk_deprecated_attribute(const char * const what,
1096 const char * const details)
1098 tpacpi_log_usertask("deprecated sysfs attribute");
1099 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1103 /*************************************************************************
1104 * rfkill and radio control support helpers
1108 * ThinkPad-ACPI firmware handling model:
1110 * WLSW (master wireless switch) is event-driven, and is common to all
1111 * firmware-controlled radios. It cannot be controlled, just monitored,
1112 * as expected. It overrides all radio state in firmware
1114 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1115 * (TODO: verify how WLSW interacts with the returned radio state).
1117 * The only time there are shadow radio state changes, is when
1118 * masked-off hotkeys are used.
1122 * Internal driver API for radio state:
1124 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1125 * bool: true means radio blocked (off)
1127 enum tpacpi_rfkill_state {
1128 TPACPI_RFK_RADIO_OFF = 0,
1132 /* rfkill switches */
1133 enum tpacpi_rfk_id {
1134 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1135 TPACPI_RFK_WWAN_SW_ID,
1136 TPACPI_RFK_UWB_SW_ID,
1140 static const char *tpacpi_rfkill_names[] = {
1141 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1142 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1143 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1144 [TPACPI_RFK_SW_MAX] = NULL
1147 /* ThinkPad-ACPI rfkill subdriver */
1149 struct rfkill *rfkill;
1150 enum tpacpi_rfk_id id;
1151 const struct tpacpi_rfk_ops *ops;
1154 struct tpacpi_rfk_ops {
1155 /* firmware interface */
1156 int (*get_status)(void);
1157 int (*set_status)(const enum tpacpi_rfkill_state);
1160 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1162 /* Query FW and update rfkill sw state for a given rfkill switch */
1163 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1170 status = (tp_rfk->ops->get_status)();
1174 rfkill_set_sw_state(tp_rfk->rfkill,
1175 (status == TPACPI_RFK_RADIO_OFF));
1180 /* Query FW and update rfkill sw state for all rfkill switches */
1181 static void tpacpi_rfk_update_swstate_all(void)
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1186 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1190 * Sync the HW-blocking state of all rfkill switches,
1191 * do notice it causes the rfkill core to schedule uevents
1193 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1196 struct tpacpi_rfk *tp_rfk;
1198 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1199 tp_rfk = tpacpi_rfkill_switches[i];
1201 if (rfkill_set_hw_state(tp_rfk->rfkill,
1203 /* ignore -- we track sw block */
1209 /* Call to get the WLSW state from the firmware */
1210 static int hotkey_get_wlsw(void);
1212 /* Call to query WLSW state and update all rfkill switches */
1213 static bool tpacpi_rfk_check_hwblock_state(void)
1215 int res = hotkey_get_wlsw();
1218 /* When unknown or unsupported, we have to assume it is unblocked */
1222 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1223 tpacpi_rfk_update_hwblock_state(hw_blocked);
1228 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1230 struct tpacpi_rfk *tp_rfk = data;
1233 dbg_printk(TPACPI_DBG_RFKILL,
1234 "request to change radio state to %s\n",
1235 blocked ? "blocked" : "unblocked");
1237 /* try to set radio state */
1238 res = (tp_rfk->ops->set_status)(blocked ?
1239 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1241 /* and update the rfkill core with whatever the FW really did */
1242 tpacpi_rfk_update_swstate(tp_rfk);
1244 return (res < 0) ? res : 0;
1247 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1248 .set_block = tpacpi_rfk_hook_set_block,
1251 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1252 const struct tpacpi_rfk_ops *tp_rfkops,
1253 const enum rfkill_type rfktype,
1255 const bool set_default)
1257 struct tpacpi_rfk *atp_rfk;
1259 bool sw_state = false;
1263 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1265 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1267 atp_rfk->rfkill = rfkill_alloc(name,
1270 &tpacpi_rfk_rfkill_ops,
1272 if (!atp_rfk || !atp_rfk->rfkill) {
1273 pr_err("failed to allocate memory for rfkill class\n");
1279 atp_rfk->ops = tp_rfkops;
1281 sw_status = (tp_rfkops->get_status)();
1282 if (sw_status < 0) {
1283 pr_err("failed to read initial state for %s, error %d\n",
1286 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1288 /* try to keep the initial state, since we ask the
1289 * firmware to preserve it across S5 in NVRAM */
1290 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1293 hw_state = tpacpi_rfk_check_hwblock_state();
1294 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1296 res = rfkill_register(atp_rfk->rfkill);
1298 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1299 rfkill_destroy(atp_rfk->rfkill);
1304 tpacpi_rfkill_switches[id] = atp_rfk;
1306 pr_info("rfkill switch %s: radio is %sblocked\n",
1307 name, (sw_state || hw_state) ? "" : "un");
1311 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1313 struct tpacpi_rfk *tp_rfk;
1315 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1317 tp_rfk = tpacpi_rfkill_switches[id];
1319 rfkill_unregister(tp_rfk->rfkill);
1320 rfkill_destroy(tp_rfk->rfkill);
1321 tpacpi_rfkill_switches[id] = NULL;
1326 static void printk_deprecated_rfkill_attribute(const char * const what)
1328 printk_deprecated_attribute(what,
1329 "Please switch to generic rfkill before year 2010");
1332 /* sysfs <radio> enable ------------------------------------------------ */
1333 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1334 struct device_attribute *attr,
1339 printk_deprecated_rfkill_attribute(attr->attr.name);
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state()) {
1343 status = TPACPI_RFK_RADIO_OFF;
1345 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1350 return snprintf(buf, PAGE_SIZE, "%d\n",
1351 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1354 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 const char *buf, size_t count)
1361 printk_deprecated_rfkill_attribute(attr->attr.name);
1363 if (parse_strtoul(buf, 1, &t))
1366 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state() && !!t)
1372 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1373 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1374 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1376 return (res < 0) ? res : count;
1379 /* procfs -------------------------------------------------------------- */
1380 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1382 if (id >= TPACPI_RFK_SW_MAX)
1383 seq_printf(m, "status:\t\tnot supported\n");
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1397 seq_printf(m, "status:\t\t%s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
1400 seq_printf(m, "commands:\tenable, disable\n");
1406 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1412 if (id >= TPACPI_RFK_SW_MAX)
1415 while ((cmd = strsep(&buf, ","))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1436 /*************************************************************************
1437 * thinkpad-acpi driver attributes
1440 /* interface_version --------------------------------------------------- */
1441 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1443 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1445 static DRIVER_ATTR_RO(interface_version);
1447 /* debug_level --------------------------------------------------------- */
1448 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1450 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1453 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1458 if (parse_strtoul(buf, 0xffff, &t))
1465 static DRIVER_ATTR_RW(debug_level);
1467 /* version ------------------------------------------------------------- */
1468 static ssize_t version_show(struct device_driver *drv, char *buf)
1470 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1471 TPACPI_DESC, TPACPI_VERSION);
1473 static DRIVER_ATTR_RO(version);
1475 /* --------------------------------------------------------------------- */
1477 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1479 /* wlsw_emulstate ------------------------------------------------------ */
1480 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1482 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1485 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1490 if (parse_strtoul(buf, 1, &t))
1493 if (tpacpi_wlsw_emulstate != !!t) {
1494 tpacpi_wlsw_emulstate = !!t;
1495 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1500 static DRIVER_ATTR_RW(wlsw_emulstate);
1502 /* bluetooth_emulstate ------------------------------------------------- */
1503 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1505 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1508 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1509 const char *buf, size_t count)
1513 if (parse_strtoul(buf, 1, &t))
1516 tpacpi_bluetooth_emulstate = !!t;
1520 static DRIVER_ATTR_RW(bluetooth_emulstate);
1522 /* wwan_emulstate ------------------------------------------------- */
1523 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1525 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1528 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1533 if (parse_strtoul(buf, 1, &t))
1536 tpacpi_wwan_emulstate = !!t;
1540 static DRIVER_ATTR_RW(wwan_emulstate);
1542 /* uwb_emulstate ------------------------------------------------- */
1543 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1548 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1553 if (parse_strtoul(buf, 1, &t))
1556 tpacpi_uwb_emulstate = !!t;
1560 static DRIVER_ATTR_RW(uwb_emulstate);
1563 /* --------------------------------------------------------------------- */
1565 static struct driver_attribute *tpacpi_driver_attributes[] = {
1566 &driver_attr_debug_level, &driver_attr_version,
1567 &driver_attr_interface_version,
1570 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1576 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1577 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1581 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1582 if (!res && dbg_wlswemul)
1583 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1584 if (!res && dbg_bluetoothemul)
1585 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1586 if (!res && dbg_wwanemul)
1587 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1588 if (!res && dbg_uwbemul)
1589 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1595 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1599 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1600 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1602 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1603 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1604 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1605 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1606 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1610 /*************************************************************************
1615 * Table of recommended minimum BIOS versions
1617 * Reasons for listing:
1618 * 1. Stable BIOS, listed because the unknown amount of
1619 * bugs and bad ACPI behaviour on older versions
1621 * 2. BIOS or EC fw with known bugs that trigger on Linux
1623 * 3. BIOS with known reduced functionality in older versions
1625 * We recommend the latest BIOS and EC version.
1626 * We only support the latest BIOS and EC fw version as a rule.
1628 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1629 * Information from users in ThinkWiki
1631 * WARNING: we use this table also to detect that the machine is
1632 * a ThinkPad in some cases, so don't remove entries lightly.
1635 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1636 { .vendor = (__v), \
1637 .bios = TPID(__id1, __id2), \
1638 .ec = TPACPI_MATCH_ANY, \
1639 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1640 | TPVER(__bv1, __bv2) }
1642 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1643 __eid, __ev1, __ev2) \
1644 { .vendor = (__v), \
1645 .bios = TPID(__bid1, __bid2), \
1647 .quirks = TPVER(__ev1, __ev2) << 16 \
1648 | TPVER(__bv1, __bv2) }
1650 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1651 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1653 /* Outdated IBM BIOSes often lack the EC id string */
1654 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1655 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1656 __bv1, __bv2, TPID(__id1, __id2), \
1658 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1659 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1662 /* Outdated IBM BIOSes often lack the EC id string */
1663 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1664 __eid1, __eid2, __ev1, __ev2) \
1665 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1666 __bv1, __bv2, TPID(__eid1, __eid2), \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1669 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1672 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1673 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1675 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1676 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1677 __bv1, __bv2, TPID(__id1, __id2), \
1680 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1681 __eid1, __eid2, __ev1, __ev2) \
1682 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1683 __bv1, __bv2, TPID(__eid1, __eid2), \
1686 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1687 /* Numeric models ------------------ */
1688 /* FW MODEL BIOS VERS */
1689 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1690 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1691 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1692 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1693 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1694 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1695 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1696 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1697 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1699 /* A-series ------------------------- */
1700 /* FW MODEL BIOS VERS EC VERS */
1701 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1702 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1703 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1704 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1705 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1706 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1707 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1708 TPV_QI0('1', '3', '2', '0'), /* A22m */
1709 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1710 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1711 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1713 /* G-series ------------------------- */
1714 /* FW MODEL BIOS VERS */
1715 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1716 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1718 /* R-series, T-series --------------- */
1719 /* FW MODEL BIOS VERS EC VERS */
1720 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1721 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1722 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1723 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1724 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1725 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1726 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1727 T40/p, T41/p, T42/p (1) */
1728 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1729 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1730 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1731 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1733 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1734 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1735 TPV_QI0('1', '6', '3', '2'), /* T22 */
1736 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1737 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1738 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1740 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1741 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1742 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1744 /* BIOS FW BIOS VERS EC FW EC VERS */
1745 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1746 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1748 /* X-series ------------------------- */
1749 /* FW MODEL BIOS VERS EC VERS */
1750 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1751 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1752 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1753 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1754 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1755 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1756 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1758 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1759 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1761 /* (0) - older versions lack DMI EC fw string and functionality */
1762 /* (1) - older versions known to lack functionality */
1773 static void __init tpacpi_check_outdated_fw(void)
1775 unsigned long fwvers;
1776 u16 ec_version, bios_version;
1778 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1779 ARRAY_SIZE(tpacpi_bios_version_qtable));
1784 bios_version = fwvers & 0xffffU;
1785 ec_version = (fwvers >> 16) & 0xffffU;
1787 /* note that unknown versions are set to 0x0000 and we use that */
1788 if ((bios_version > thinkpad_id.bios_release) ||
1789 (ec_version > thinkpad_id.ec_release &&
1790 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1792 * The changelogs would let us track down the exact
1793 * reason, but it is just too much of a pain to track
1794 * it. We only list BIOSes that are either really
1795 * broken, or really stable to begin with, so it is
1796 * best if the user upgrades the firmware anyway.
1798 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1799 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1803 static bool __init tpacpi_is_fw_known(void)
1805 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1806 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1809 /****************************************************************************
1810 ****************************************************************************
1814 ****************************************************************************
1815 ****************************************************************************/
1817 /*************************************************************************
1818 * thinkpad-acpi metadata subdriver
1821 static int thinkpad_acpi_driver_read(struct seq_file *m)
1823 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1824 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1828 static struct ibm_struct thinkpad_acpi_driver_data = {
1830 .read = thinkpad_acpi_driver_read,
1833 /*************************************************************************
1838 * ThinkPad firmware event model
1840 * The ThinkPad firmware has two main event interfaces: normal ACPI
1841 * notifications (which follow the ACPI standard), and a private event
1844 * The private event interface also issues events for the hotkeys. As
1845 * the driver gained features, the event handling code ended up being
1846 * built around the hotkey subdriver. This will need to be refactored
1847 * to a more formal event API eventually.
1849 * Some "hotkeys" are actually supposed to be used as event reports,
1850 * such as "brightness has changed", "volume has changed", depending on
1851 * the ThinkPad model and how the firmware is operating.
1853 * Unlike other classes, hotkey-class events have mask/unmask control on
1854 * non-ancient firmware. However, how it behaves changes a lot with the
1855 * firmware model and version.
1858 enum { /* hot key scan codes (derived from ACPI DSDT) */
1859 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1860 TP_ACPI_HOTKEYSCAN_FNF2,
1861 TP_ACPI_HOTKEYSCAN_FNF3,
1862 TP_ACPI_HOTKEYSCAN_FNF4,
1863 TP_ACPI_HOTKEYSCAN_FNF5,
1864 TP_ACPI_HOTKEYSCAN_FNF6,
1865 TP_ACPI_HOTKEYSCAN_FNF7,
1866 TP_ACPI_HOTKEYSCAN_FNF8,
1867 TP_ACPI_HOTKEYSCAN_FNF9,
1868 TP_ACPI_HOTKEYSCAN_FNF10,
1869 TP_ACPI_HOTKEYSCAN_FNF11,
1870 TP_ACPI_HOTKEYSCAN_FNF12,
1871 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1872 TP_ACPI_HOTKEYSCAN_FNINSERT,
1873 TP_ACPI_HOTKEYSCAN_FNDELETE,
1874 TP_ACPI_HOTKEYSCAN_FNHOME,
1875 TP_ACPI_HOTKEYSCAN_FNEND,
1876 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1877 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1878 TP_ACPI_HOTKEYSCAN_FNSPACE,
1879 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1880 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1881 TP_ACPI_HOTKEYSCAN_MUTE,
1882 TP_ACPI_HOTKEYSCAN_THINKPAD,
1883 TP_ACPI_HOTKEYSCAN_UNK1,
1884 TP_ACPI_HOTKEYSCAN_UNK2,
1885 TP_ACPI_HOTKEYSCAN_UNK3,
1886 TP_ACPI_HOTKEYSCAN_UNK4,
1887 TP_ACPI_HOTKEYSCAN_UNK5,
1888 TP_ACPI_HOTKEYSCAN_UNK6,
1889 TP_ACPI_HOTKEYSCAN_UNK7,
1890 TP_ACPI_HOTKEYSCAN_UNK8,
1892 /* Adaptive keyboard keycodes */
1893 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1894 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1895 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1896 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1897 TP_ACPI_HOTKEYSCAN_CLOUD,
1898 TP_ACPI_HOTKEYSCAN_UNK9,
1899 TP_ACPI_HOTKEYSCAN_VOICE,
1900 TP_ACPI_HOTKEYSCAN_UNK10,
1901 TP_ACPI_HOTKEYSCAN_GESTURES,
1902 TP_ACPI_HOTKEYSCAN_UNK11,
1903 TP_ACPI_HOTKEYSCAN_UNK12,
1904 TP_ACPI_HOTKEYSCAN_UNK13,
1905 TP_ACPI_HOTKEYSCAN_CONFIG,
1906 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1907 TP_ACPI_HOTKEYSCAN_RELOAD,
1908 TP_ACPI_HOTKEYSCAN_BACK,
1909 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1910 TP_ACPI_HOTKEYSCAN_MIC_UP,
1911 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1912 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1913 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1915 /* Lenovo extended keymap, starting at 0x1300 */
1916 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1917 /* first new observed key (star, favorites) is 0x1311 */
1918 TP_ACPI_HOTKEYSCAN_STAR = 69,
1919 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1920 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1921 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1922 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1923 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1924 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1925 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1926 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1928 /* Hotkey keymap size */
1929 TPACPI_HOTKEY_MAP_LEN
1932 enum { /* Keys/events available through NVRAM polling */
1933 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1934 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1937 enum { /* Positions of some of the keys in hotkey masks */
1938 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1939 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1940 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1941 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1942 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1943 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1944 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1945 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1946 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1947 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1948 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1951 enum { /* NVRAM to ACPI HKEY group map */
1952 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1953 TP_ACPI_HKEY_ZOOM_MASK |
1954 TP_ACPI_HKEY_DISPSWTCH_MASK |
1955 TP_ACPI_HKEY_HIBERNATE_MASK,
1956 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1957 TP_ACPI_HKEY_BRGHTDWN_MASK,
1958 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1959 TP_ACPI_HKEY_VOLDWN_MASK |
1960 TP_ACPI_HKEY_MUTE_MASK,
1963 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1964 struct tp_nvram_state {
1965 u16 thinkpad_toggle:1;
1967 u16 display_toggle:1;
1968 u16 thinklight_toggle:1;
1969 u16 hibernate_toggle:1;
1970 u16 displayexp_toggle:1;
1971 u16 display_state:1;
1972 u16 brightness_toggle:1;
1973 u16 volume_toggle:1;
1976 u8 brightness_level;
1980 /* kthread for the hotkey poller */
1981 static struct task_struct *tpacpi_hotkey_task;
1984 * Acquire mutex to write poller control variables as an
1987 * Increment hotkey_config_change when changing them if you
1988 * want the kthread to forget old state.
1990 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1992 static struct mutex hotkey_thread_data_mutex;
1993 static unsigned int hotkey_config_change;
1996 * hotkey poller control variables
1998 * Must be atomic or readers will also need to acquire mutex
2000 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2001 * should be used only when the changes need to be taken as
2002 * a block, OR when one needs to force the kthread to forget
2005 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2006 static unsigned int hotkey_poll_freq = 10; /* Hz */
2008 #define HOTKEY_CONFIG_CRITICAL_START \
2010 mutex_lock(&hotkey_thread_data_mutex); \
2011 hotkey_config_change++; \
2013 #define HOTKEY_CONFIG_CRITICAL_END \
2014 mutex_unlock(&hotkey_thread_data_mutex);
2016 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2018 #define hotkey_source_mask 0U
2019 #define HOTKEY_CONFIG_CRITICAL_START
2020 #define HOTKEY_CONFIG_CRITICAL_END
2022 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2024 static struct mutex hotkey_mutex;
2026 static enum { /* Reasons for waking up */
2027 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2028 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2029 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2030 } hotkey_wakeup_reason;
2032 static int hotkey_autosleep_ack;
2034 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2035 static u32 hotkey_all_mask; /* all events supported in fw */
2036 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2037 static u32 hotkey_reserved_mask; /* events better left disabled */
2038 static u32 hotkey_driver_mask; /* events needed by the driver */
2039 static u32 hotkey_user_mask; /* events visible to userspace */
2040 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2042 static u16 *hotkey_keycode_map;
2044 static struct attribute_set *hotkey_dev_attributes;
2046 static void tpacpi_driver_event(const unsigned int hkey_event);
2047 static void hotkey_driver_event(const unsigned int scancode);
2048 static void hotkey_poll_setup(const bool may_warn);
2050 /* HKEY.MHKG() return bits */
2051 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2053 TP_ACPI_MULTI_MODE_INVALID = 0,
2054 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2055 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2056 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2057 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2058 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2059 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2060 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2064 /* The following modes are considered tablet mode for the purpose of
2065 * reporting the status to userspace. i.e. in all these modes it makes
2066 * sense to disable the laptop input devices such as touchpad and
2069 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2070 TP_ACPI_MULTI_MODE_STAND |
2071 TP_ACPI_MULTI_MODE_TENT |
2072 TP_ACPI_MULTI_MODE_STAND_TENT,
2075 static int hotkey_get_wlsw(void)
2079 if (!tp_features.hotkey_wlsw)
2082 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2084 return (tpacpi_wlsw_emulstate) ?
2085 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2088 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2091 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2094 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2096 int type = (s >> 16) & 0xffff;
2097 int value = s & 0xffff;
2098 int mode = TP_ACPI_MULTI_MODE_INVALID;
2099 int valid_modes = 0;
2101 if (has_tablet_mode)
2102 *has_tablet_mode = 0;
2106 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2107 TP_ACPI_MULTI_MODE_TABLET |
2108 TP_ACPI_MULTI_MODE_STAND_TENT;
2111 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2112 TP_ACPI_MULTI_MODE_FLAT |
2113 TP_ACPI_MULTI_MODE_TABLET |
2114 TP_ACPI_MULTI_MODE_STAND |
2115 TP_ACPI_MULTI_MODE_TENT;
2118 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2119 TP_ACPI_MULTI_MODE_FLAT;
2123 /* In mode 4, FLAT is not specified as a valid mode. However,
2124 * it can be seen at least on the X1 Yoga 2nd Generation.
2126 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2127 TP_ACPI_MULTI_MODE_FLAT |
2128 TP_ACPI_MULTI_MODE_TABLET |
2129 TP_ACPI_MULTI_MODE_STAND |
2130 TP_ACPI_MULTI_MODE_TENT;
2133 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2134 type, value, TPACPI_MAIL);
2138 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2139 *has_tablet_mode = 1;
2143 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2146 mode = TP_ACPI_MULTI_MODE_FLAT;
2149 mode = TP_ACPI_MULTI_MODE_TABLET;
2153 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2155 mode = TP_ACPI_MULTI_MODE_STAND;
2158 mode = TP_ACPI_MULTI_MODE_TENT;
2161 if (type == 5 && value == 0xffff) {
2162 pr_warn("Multi mode status is undetected, assuming laptop\n");
2167 if (!(mode & valid_modes)) {
2168 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2169 value, type, TPACPI_MAIL);
2173 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2176 static int hotkey_get_tablet_mode(int *status)
2180 switch (tp_features.hotkey_tablet) {
2181 case TP_HOTKEY_TABLET_USES_MHKG:
2182 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2185 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2187 case TP_HOTKEY_TABLET_USES_GMMS:
2188 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2191 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2201 * Reads current event mask from firmware, and updates
2202 * hotkey_acpi_mask accordingly. Also resets any bits
2203 * from hotkey_user_mask that are unavailable to be
2204 * delivered (shadow requirement of the userspace ABI).
2206 * Call with hotkey_mutex held
2208 static int hotkey_mask_get(void)
2210 if (tp_features.hotkey_mask) {
2213 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2216 hotkey_acpi_mask = m;
2218 /* no mask support doesn't mean no event support... */
2219 hotkey_acpi_mask = hotkey_all_mask;
2222 /* sync userspace-visible mask */
2223 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2228 static void hotkey_mask_warn_incomplete_mask(void)
2230 /* log only what the user can fix... */
2231 const u32 wantedmask = hotkey_driver_mask &
2232 ~(hotkey_acpi_mask | hotkey_source_mask) &
2233 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2236 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2240 * Set the firmware mask when supported
2242 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2244 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2246 * Call with hotkey_mutex held
2248 static int hotkey_mask_set(u32 mask)
2253 const u32 fwmask = mask & ~hotkey_source_mask;
2255 if (tp_features.hotkey_mask) {
2256 for (i = 0; i < 32; i++) {
2257 if (!acpi_evalf(hkey_handle,
2258 NULL, "MHKM", "vdd", i + 1,
2259 !!(mask & (1 << i)))) {
2267 * We *must* make an inconditional call to hotkey_mask_get to
2268 * refresh hotkey_acpi_mask and update hotkey_user_mask
2270 * Take the opportunity to also log when we cannot _enable_
2273 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2274 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2275 fwmask, hotkey_acpi_mask);
2278 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2279 hotkey_mask_warn_incomplete_mask();
2285 * Sets hotkey_user_mask and tries to set the firmware mask
2287 * Call with hotkey_mutex held
2289 static int hotkey_user_mask_set(const u32 mask)
2293 /* Give people a chance to notice they are doing something that
2294 * is bound to go boom on their users sooner or later */
2295 if (!tp_warned.hotkey_mask_ff &&
2296 (mask == 0xffff || mask == 0xffffff ||
2297 mask == 0xffffffff)) {
2298 tp_warned.hotkey_mask_ff = 1;
2299 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2301 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2304 /* Try to enable what the user asked for, plus whatever we need.
2305 * this syncs everything but won't enable bits in hotkey_user_mask */
2306 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2308 /* Enable the available bits in hotkey_user_mask */
2309 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2315 * Sets the driver hotkey mask.
2317 * Can be called even if the hotkey subdriver is inactive
2319 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2323 /* Do the right thing if hotkey_init has not been called yet */
2324 if (!tp_features.hotkey) {
2325 hotkey_driver_mask = mask;
2329 mutex_lock(&hotkey_mutex);
2331 HOTKEY_CONFIG_CRITICAL_START
2332 hotkey_driver_mask = mask;
2333 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2334 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2336 HOTKEY_CONFIG_CRITICAL_END
2338 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2339 ~hotkey_source_mask);
2340 hotkey_poll_setup(true);
2342 mutex_unlock(&hotkey_mutex);
2347 static int hotkey_status_get(int *status)
2349 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2355 static int hotkey_status_set(bool enable)
2357 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2363 static void tpacpi_input_send_tabletsw(void)
2367 if (tp_features.hotkey_tablet &&
2368 !hotkey_get_tablet_mode(&state)) {
2369 mutex_lock(&tpacpi_inputdev_send_mutex);
2371 input_report_switch(tpacpi_inputdev,
2372 SW_TABLET_MODE, !!state);
2373 input_sync(tpacpi_inputdev);
2375 mutex_unlock(&tpacpi_inputdev_send_mutex);
2379 /* Do NOT call without validating scancode first */
2380 static void tpacpi_input_send_key(const unsigned int scancode)
2382 const unsigned int keycode = hotkey_keycode_map[scancode];
2384 if (keycode != KEY_RESERVED) {
2385 mutex_lock(&tpacpi_inputdev_send_mutex);
2387 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2388 input_report_key(tpacpi_inputdev, keycode, 1);
2389 input_sync(tpacpi_inputdev);
2391 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2392 input_report_key(tpacpi_inputdev, keycode, 0);
2393 input_sync(tpacpi_inputdev);
2395 mutex_unlock(&tpacpi_inputdev_send_mutex);
2399 /* Do NOT call without validating scancode first */
2400 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2402 hotkey_driver_event(scancode);
2403 if (hotkey_user_mask & (1 << scancode))
2404 tpacpi_input_send_key(scancode);
2407 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2408 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2410 /* Do NOT call without validating scancode first */
2411 static void tpacpi_hotkey_send_key(unsigned int scancode)
2413 tpacpi_input_send_key_masked(scancode);
2416 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2420 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2421 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2422 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2423 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2424 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2425 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2427 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2428 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2429 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2431 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2432 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2433 n->displayexp_toggle =
2434 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2436 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2437 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2438 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2439 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2440 n->brightness_toggle =
2441 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2443 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2444 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2445 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2446 >> TP_NVRAM_POS_LEVEL_VOLUME;
2447 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2448 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2452 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2454 if ((event_mask & (1 << __scancode)) && \
2455 oldn->__member != newn->__member) \
2456 tpacpi_hotkey_send_key(__scancode); \
2459 #define TPACPI_MAY_SEND_KEY(__scancode) \
2461 if (event_mask & (1 << __scancode)) \
2462 tpacpi_hotkey_send_key(__scancode); \
2465 static void issue_volchange(const unsigned int oldvol,
2466 const unsigned int newvol,
2467 const u32 event_mask)
2469 unsigned int i = oldvol;
2471 while (i > newvol) {
2472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2475 while (i < newvol) {
2476 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2481 static void issue_brightnesschange(const unsigned int oldbrt,
2482 const unsigned int newbrt,
2483 const u32 event_mask)
2485 unsigned int i = oldbrt;
2487 while (i > newbrt) {
2488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2491 while (i < newbrt) {
2492 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2497 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2498 struct tp_nvram_state *newn,
2499 const u32 event_mask)
2502 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2503 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2504 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2505 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2507 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2509 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2514 * This code is supposed to duplicate the IBM firmware behaviour:
2515 * - Pressing MUTE issues mute hotkey message, even when already mute
2516 * - Pressing Volume up/down issues volume up/down hotkey messages,
2517 * even when already at maximum or minimum volume
2518 * - The act of unmuting issues volume up/down notification,
2519 * depending which key was used to unmute
2521 * We are constrained to what the NVRAM can tell us, which is not much
2522 * and certainly not enough if more than one volume hotkey was pressed
2523 * since the last poll cycle.
2525 * Just to make our life interesting, some newer Lenovo ThinkPads have
2526 * bugs in the BIOS and may fail to update volume_toggle properly.
2531 oldn->volume_toggle != newn->volume_toggle ||
2532 oldn->volume_level != newn->volume_level) {
2533 /* recently muted, or repeated mute keypress, or
2534 * multiple presses ending in mute */
2535 issue_volchange(oldn->volume_level, newn->volume_level,
2537 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2542 /* recently unmuted, issue 'unmute' keypress */
2543 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2545 if (oldn->volume_level != newn->volume_level) {
2546 issue_volchange(oldn->volume_level, newn->volume_level,
2548 } else if (oldn->volume_toggle != newn->volume_toggle) {
2549 /* repeated vol up/down keypress at end of scale ? */
2550 if (newn->volume_level == 0)
2551 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2552 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2553 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2557 /* handle brightness */
2558 if (oldn->brightness_level != newn->brightness_level) {
2559 issue_brightnesschange(oldn->brightness_level,
2560 newn->brightness_level, event_mask);
2561 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2562 /* repeated key presses that didn't change state */
2563 if (newn->brightness_level == 0)
2564 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2565 else if (newn->brightness_level >= bright_maxlvl
2566 && !tp_features.bright_unkfw)
2567 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2570 #undef TPACPI_COMPARE_KEY
2571 #undef TPACPI_MAY_SEND_KEY
2577 * We track all events in hotkey_source_mask all the time, since
2578 * most of them are edge-based. We only issue those requested by
2579 * hotkey_user_mask or hotkey_driver_mask, though.
2581 static int hotkey_kthread(void *data)
2583 struct tp_nvram_state s[2] = { 0 };
2584 u32 poll_mask, event_mask;
2585 unsigned int si, so;
2587 unsigned int change_detector;
2588 unsigned int poll_freq;
2591 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2600 /* Initial state for compares */
2601 mutex_lock(&hotkey_thread_data_mutex);
2602 change_detector = hotkey_config_change;
2603 poll_mask = hotkey_source_mask;
2604 event_mask = hotkey_source_mask &
2605 (hotkey_driver_mask | hotkey_user_mask);
2606 poll_freq = hotkey_poll_freq;
2607 mutex_unlock(&hotkey_thread_data_mutex);
2608 hotkey_read_nvram(&s[so], poll_mask);
2610 while (!kthread_should_stop()) {
2612 if (likely(poll_freq))
2615 t = 100; /* should never happen... */
2617 t = msleep_interruptible(t);
2618 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2621 if (t > 0 && !was_frozen)
2624 mutex_lock(&hotkey_thread_data_mutex);
2625 if (was_frozen || hotkey_config_change != change_detector) {
2626 /* forget old state on thaw or config change */
2629 change_detector = hotkey_config_change;
2631 poll_mask = hotkey_source_mask;
2632 event_mask = hotkey_source_mask &
2633 (hotkey_driver_mask | hotkey_user_mask);
2634 poll_freq = hotkey_poll_freq;
2635 mutex_unlock(&hotkey_thread_data_mutex);
2637 if (likely(poll_mask)) {
2638 hotkey_read_nvram(&s[si], poll_mask);
2639 if (likely(si != so)) {
2640 hotkey_compare_and_issue_event(&s[so], &s[si],
2653 /* call with hotkey_mutex held */
2654 static void hotkey_poll_stop_sync(void)
2656 if (tpacpi_hotkey_task) {
2657 kthread_stop(tpacpi_hotkey_task);
2658 tpacpi_hotkey_task = NULL;
2662 /* call with hotkey_mutex held */
2663 static void hotkey_poll_setup(const bool may_warn)
2665 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2666 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2668 if (hotkey_poll_freq > 0 &&
2669 (poll_driver_mask ||
2670 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2671 if (!tpacpi_hotkey_task) {
2672 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2673 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2674 if (IS_ERR(tpacpi_hotkey_task)) {
2675 tpacpi_hotkey_task = NULL;
2676 pr_err("could not create kernel thread for hotkey polling\n");
2680 hotkey_poll_stop_sync();
2681 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2682 hotkey_poll_freq == 0) {
2683 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2684 poll_user_mask, poll_driver_mask);
2689 static void hotkey_poll_setup_safe(const bool may_warn)
2691 mutex_lock(&hotkey_mutex);
2692 hotkey_poll_setup(may_warn);
2693 mutex_unlock(&hotkey_mutex);
2696 /* call with hotkey_mutex held */
2697 static void hotkey_poll_set_freq(unsigned int freq)
2700 hotkey_poll_stop_sync();
2702 hotkey_poll_freq = freq;
2705 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2707 static void hotkey_poll_setup(const bool __unused)
2711 static void hotkey_poll_setup_safe(const bool __unused)
2715 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2717 static int hotkey_inputdev_open(struct input_dev *dev)
2719 switch (tpacpi_lifecycle) {
2720 case TPACPI_LIFE_INIT:
2721 case TPACPI_LIFE_RUNNING:
2722 hotkey_poll_setup_safe(false);
2724 case TPACPI_LIFE_EXITING:
2728 /* Should only happen if tpacpi_lifecycle is corrupt */
2733 static void hotkey_inputdev_close(struct input_dev *dev)
2735 /* disable hotkey polling when possible */
2736 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2737 !(hotkey_source_mask & hotkey_driver_mask))
2738 hotkey_poll_setup_safe(false);
2741 /* sysfs hotkey enable ------------------------------------------------- */
2742 static ssize_t hotkey_enable_show(struct device *dev,
2743 struct device_attribute *attr,
2748 printk_deprecated_attribute("hotkey_enable",
2749 "Hotkey reporting is always enabled");
2751 res = hotkey_status_get(&status);
2755 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2758 static ssize_t hotkey_enable_store(struct device *dev,
2759 struct device_attribute *attr,
2760 const char *buf, size_t count)
2764 printk_deprecated_attribute("hotkey_enable",
2765 "Hotkeys can be disabled through hotkey_mask");
2767 if (parse_strtoul(buf, 1, &t))
2776 static DEVICE_ATTR_RW(hotkey_enable);
2778 /* sysfs hotkey mask --------------------------------------------------- */
2779 static ssize_t hotkey_mask_show(struct device *dev,
2780 struct device_attribute *attr,
2783 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2786 static ssize_t hotkey_mask_store(struct device *dev,
2787 struct device_attribute *attr,
2788 const char *buf, size_t count)
2793 if (parse_strtoul(buf, 0xffffffffUL, &t))
2796 if (mutex_lock_killable(&hotkey_mutex))
2797 return -ERESTARTSYS;
2799 res = hotkey_user_mask_set(t);
2801 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2802 hotkey_poll_setup(true);
2805 mutex_unlock(&hotkey_mutex);
2807 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2809 return (res) ? res : count;
2812 static DEVICE_ATTR_RW(hotkey_mask);
2814 /* sysfs hotkey bios_enabled ------------------------------------------- */
2815 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2816 struct device_attribute *attr,
2819 return sprintf(buf, "0\n");
2822 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2824 /* sysfs hotkey bios_mask ---------------------------------------------- */
2825 static ssize_t hotkey_bios_mask_show(struct device *dev,
2826 struct device_attribute *attr,
2829 printk_deprecated_attribute("hotkey_bios_mask",
2830 "This attribute is useless.");
2831 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2834 static DEVICE_ATTR_RO(hotkey_bios_mask);
2836 /* sysfs hotkey all_mask ----------------------------------------------- */
2837 static ssize_t hotkey_all_mask_show(struct device *dev,
2838 struct device_attribute *attr,
2841 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2842 hotkey_all_mask | hotkey_source_mask);
2845 static DEVICE_ATTR_RO(hotkey_all_mask);
2847 /* sysfs hotkey all_mask ----------------------------------------------- */
2848 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2849 struct device_attribute *attr,
2852 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2853 hotkey_adaptive_all_mask | hotkey_source_mask);
2856 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2858 /* sysfs hotkey recommended_mask --------------------------------------- */
2859 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2860 struct device_attribute *attr,
2863 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2864 (hotkey_all_mask | hotkey_source_mask)
2865 & ~hotkey_reserved_mask);
2868 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2870 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2872 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2873 static ssize_t hotkey_source_mask_show(struct device *dev,
2874 struct device_attribute *attr,
2877 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2880 static ssize_t hotkey_source_mask_store(struct device *dev,
2881 struct device_attribute *attr,
2882 const char *buf, size_t count)
2888 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2889 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2892 if (mutex_lock_killable(&hotkey_mutex))
2893 return -ERESTARTSYS;
2895 HOTKEY_CONFIG_CRITICAL_START
2896 hotkey_source_mask = t;
2897 HOTKEY_CONFIG_CRITICAL_END
2899 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2900 ~hotkey_source_mask);
2901 hotkey_poll_setup(true);
2903 /* check if events needed by the driver got disabled */
2904 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2905 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2907 mutex_unlock(&hotkey_mutex);
2910 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2913 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2916 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2918 return (rc < 0) ? rc : count;
2921 static DEVICE_ATTR_RW(hotkey_source_mask);
2923 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2924 static ssize_t hotkey_poll_freq_show(struct device *dev,
2925 struct device_attribute *attr,
2928 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2931 static ssize_t hotkey_poll_freq_store(struct device *dev,
2932 struct device_attribute *attr,
2933 const char *buf, size_t count)
2937 if (parse_strtoul(buf, 25, &t))
2940 if (mutex_lock_killable(&hotkey_mutex))
2941 return -ERESTARTSYS;
2943 hotkey_poll_set_freq(t);
2944 hotkey_poll_setup(true);
2946 mutex_unlock(&hotkey_mutex);
2948 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2953 static DEVICE_ATTR_RW(hotkey_poll_freq);
2955 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2957 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2958 static ssize_t hotkey_radio_sw_show(struct device *dev,
2959 struct device_attribute *attr,
2963 res = hotkey_get_wlsw();
2967 /* Opportunistic update */
2968 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2970 return snprintf(buf, PAGE_SIZE, "%d\n",
2971 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2974 static DEVICE_ATTR_RO(hotkey_radio_sw);
2976 static void hotkey_radio_sw_notify_change(void)
2978 if (tp_features.hotkey_wlsw)
2979 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2983 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2984 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2985 struct device_attribute *attr,
2989 res = hotkey_get_tablet_mode(&s);
2993 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2996 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2998 static void hotkey_tablet_mode_notify_change(void)
3000 if (tp_features.hotkey_tablet)
3001 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3002 "hotkey_tablet_mode");
3005 /* sysfs wakeup reason (pollable) -------------------------------------- */
3006 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3007 struct device_attribute *attr,
3010 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3013 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3015 static void hotkey_wakeup_reason_notify_change(void)
3017 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3021 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3022 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3023 struct device_attribute *attr,
3026 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3029 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3030 hotkey_wakeup_hotunplug_complete_show, NULL);
3032 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3034 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3035 "wakeup_hotunplug_complete");
3038 /* sysfs adaptive kbd mode --------------------------------------------- */
3040 static int adaptive_keyboard_get_mode(void);
3041 static int adaptive_keyboard_set_mode(int new_mode);
3043 enum ADAPTIVE_KEY_MODE {
3046 WEB_CONFERENCE_MODE,
3051 static ssize_t adaptive_kbd_mode_show(struct device *dev,
3052 struct device_attribute *attr,
3057 current_mode = adaptive_keyboard_get_mode();
3058 if (current_mode < 0)
3059 return current_mode;
3061 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3064 static ssize_t adaptive_kbd_mode_store(struct device *dev,
3065 struct device_attribute *attr,
3066 const char *buf, size_t count)
3071 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3074 res = adaptive_keyboard_set_mode(t);
3075 return (res < 0) ? res : count;
3078 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3080 static struct attribute *adaptive_kbd_attributes[] = {
3081 &dev_attr_adaptive_kbd_mode.attr,
3085 static const struct attribute_group adaptive_kbd_attr_group = {
3086 .attrs = adaptive_kbd_attributes,
3089 /* --------------------------------------------------------------------- */
3091 static struct attribute *hotkey_attributes[] __initdata = {
3092 &dev_attr_hotkey_enable.attr,
3093 &dev_attr_hotkey_bios_enabled.attr,
3094 &dev_attr_hotkey_bios_mask.attr,
3095 &dev_attr_wakeup_reason.attr,
3096 &dev_attr_wakeup_hotunplug_complete.attr,
3097 &dev_attr_hotkey_mask.attr,
3098 &dev_attr_hotkey_all_mask.attr,
3099 &dev_attr_hotkey_adaptive_all_mask.attr,
3100 &dev_attr_hotkey_recommended_mask.attr,
3101 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3102 &dev_attr_hotkey_source_mask.attr,
3103 &dev_attr_hotkey_poll_freq.attr,
3108 * Sync both the hw and sw blocking state of all switches
3110 static void tpacpi_send_radiosw_update(void)
3115 * We must sync all rfkill controllers *before* issuing any
3116 * rfkill input events, or we will race the rfkill core input
3119 * tpacpi_inputdev_send_mutex works as a synchronization point
3122 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3125 wlsw = hotkey_get_wlsw();
3127 /* Sync hw blocking state first if it is hw-blocked */
3128 if (wlsw == TPACPI_RFK_RADIO_OFF)
3129 tpacpi_rfk_update_hwblock_state(true);
3131 /* Sync sw blocking state */
3132 tpacpi_rfk_update_swstate_all();
3134 /* Sync hw blocking state last if it is hw-unblocked */
3135 if (wlsw == TPACPI_RFK_RADIO_ON)
3136 tpacpi_rfk_update_hwblock_state(false);
3138 /* Issue rfkill input event for WLSW switch */
3140 mutex_lock(&tpacpi_inputdev_send_mutex);
3142 input_report_switch(tpacpi_inputdev,
3143 SW_RFKILL_ALL, (wlsw > 0));
3144 input_sync(tpacpi_inputdev);
3146 mutex_unlock(&tpacpi_inputdev_send_mutex);
3150 * this can be unconditional, as we will poll state again
3151 * if userspace uses the notify to read data
3153 hotkey_radio_sw_notify_change();
3156 static void hotkey_exit(void)
3158 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3159 mutex_lock(&hotkey_mutex);
3160 hotkey_poll_stop_sync();
3161 mutex_unlock(&hotkey_mutex);
3164 if (hotkey_dev_attributes)
3165 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3167 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3168 "restoring original HKEY status and mask\n");
3169 /* yes, there is a bitwise or below, we want the
3170 * functions to be called even if one of them fail */
3171 if (((tp_features.hotkey_mask &&
3172 hotkey_mask_set(hotkey_orig_mask)) |
3173 hotkey_status_set(false)) != 0)
3174 pr_err("failed to restore hot key mask to BIOS defaults\n");
3177 static void __init hotkey_unmap(const unsigned int scancode)
3179 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3180 clear_bit(hotkey_keycode_map[scancode],
3181 tpacpi_inputdev->keybit);
3182 hotkey_keycode_map[scancode] = KEY_RESERVED;
3188 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3191 #define TPACPI_HK_Q_INIMASK 0x0001
3193 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3194 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3195 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3196 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3197 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3198 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3199 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3200 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3201 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3202 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3203 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3204 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3205 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3206 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3207 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3208 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3209 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3210 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3211 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3212 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3215 typedef u16 tpacpi_keymap_entry_t;
3216 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3218 static int hotkey_init_tablet_mode(void)
3220 int in_tablet_mode = 0, res;
3223 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3224 int has_tablet_mode;
3226 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3229 * The Yoga 11e series has 2 accelerometers described by a
3230 * BOSC0200 ACPI node. This setup relies on a Windows service
3231 * which calls special ACPI methods on this node to report
3232 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3233 * does not support this, so skip the hotkey on these models.
3235 if (has_tablet_mode && !acpi_dev_present("BOSC0200", "1", -1))
3236 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3238 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3239 /* For X41t, X60t, X61t Tablets... */
3240 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3241 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3245 if (!tp_features.hotkey_tablet)
3248 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3249 type, in_tablet_mode ? "tablet" : "laptop");
3251 res = add_to_attr_set(hotkey_dev_attributes,
3252 &dev_attr_hotkey_tablet_mode.attr);
3256 return in_tablet_mode;
3259 static int __init hotkey_init(struct ibm_init_struct *iibm)
3261 /* Requirements for changing the default keymaps:
3263 * 1. Many of the keys are mapped to KEY_RESERVED for very
3264 * good reasons. Do not change them unless you have deep
3265 * knowledge on the IBM and Lenovo ThinkPad firmware for
3266 * the various ThinkPad models. The driver behaves
3267 * differently for KEY_RESERVED: such keys have their
3268 * hot key mask *unset* in mask_recommended, and also
3269 * in the initial hot key mask programmed into the
3270 * firmware at driver load time, which means the firm-
3271 * ware may react very differently if you change them to
3274 * 2. You must be subscribed to the linux-thinkpad and
3275 * ibm-acpi-devel mailing lists, and you should read the
3276 * list archives since 2007 if you want to change the
3277 * keymaps. This requirement exists so that you will
3278 * know the past history of problems with the thinkpad-
3279 * acpi driver keymaps, and also that you will be
3280 * listening to any bug reports;
3282 * 3. Do not send thinkpad-acpi specific patches directly to
3283 * for merging, *ever*. Send them to the linux-acpi
3284 * mailinglist for comments. Merging is to be done only
3285 * through acpi-test and the ACPI maintainer.
3287 * If the above is too much to ask, don't change the keymap.
3288 * Ask the thinkpad-acpi maintainer to do it, instead.
3292 TPACPI_KEYMAP_IBM_GENERIC = 0,
3293 TPACPI_KEYMAP_LENOVO_GENERIC,
3296 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3297 /* Generic keymap for IBM ThinkPads */
3298 [TPACPI_KEYMAP_IBM_GENERIC] = {
3299 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3300 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3301 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3302 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3304 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3305 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3306 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3307 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3309 /* brightness: firmware always reacts to them */
3310 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3311 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3313 /* Thinklight: firmware always react to it */
3314 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3316 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3317 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3319 /* Volume: firmware always react to it and reprograms
3320 * the built-in *extra* mixer. Never map it to control
3321 * another mixer by default. */
3322 KEY_RESERVED, /* 0x14: VOLUME UP */
3323 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3324 KEY_RESERVED, /* 0x16: MUTE */
3326 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3328 /* (assignments unknown, please report if found) */
3329 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3330 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3332 /* No assignments, only used for Adaptive keyboards. */
3333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3335 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3336 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3337 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3339 /* No assignment, used for newer Lenovo models */
3340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3341 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3342 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3345 KEY_UNKNOWN, KEY_UNKNOWN
3349 /* Generic keymap for Lenovo ThinkPads */
3350 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3351 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3352 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3353 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3354 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3356 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3357 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3358 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3359 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3361 /* These should be enabled --only-- when ACPI video
3362 * is disabled (i.e. in "vendor" mode), and are handled
3363 * in a special way by the init code */
3364 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3365 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3367 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3369 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3370 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3372 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3373 * react to it and reprograms the built-in *extra* mixer.
3374 * Never map it to control another mixer by default.
3376 * T60?, T61, R60?, R61: firmware and EC tries to send
3377 * these over the regular keyboard, so these are no-ops,
3378 * but there are still weird bugs re. MUTE, so do not
3379 * change unless you get test reports from all Lenovo
3380 * models. May cause the BIOS to interfere with the
3383 KEY_RESERVED, /* 0x14: VOLUME UP */
3384 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3385 KEY_RESERVED, /* 0x16: MUTE */
3387 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3389 /* (assignments unknown, please report if found) */
3390 KEY_UNKNOWN, KEY_UNKNOWN,
3393 * The mic mute button only sends 0x1a. It does not
3394 * automatically mute the mic or change the mute light.
3396 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3398 /* (assignments unknown, please report if found) */
3401 /* Extra keys in use since the X240 / T440 / T540 */
3402 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3405 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3406 * The first item in this list is the Mute button which is
3407 * emitted with 0x103 through
3408 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3410 * We'll need to offset those by 0x20.
3412 KEY_RESERVED, /* Mute held, 0x103 */
3413 KEY_BRIGHTNESS_MIN, /* Backlight off */
3414 KEY_RESERVED, /* Clipping tool */
3415 KEY_RESERVED, /* Cloud */
3417 KEY_VOICECOMMAND, /* Voice */
3419 KEY_RESERVED, /* Gestures */
3423 KEY_CONFIG, /* Settings */
3424 KEY_RESERVED, /* New tab */
3425 KEY_REFRESH, /* Reload */
3426 KEY_BACK, /* Back */
3427 KEY_RESERVED, /* Microphone down */
3428 KEY_RESERVED, /* Microphone up */
3429 KEY_RESERVED, /* Microphone cancellation */
3430 KEY_RESERVED, /* Camera mode */
3431 KEY_RESERVED, /* Rotate display, 0x116 */
3434 * These are found in 2017 models (e.g. T470s, X270).
3435 * The lowest known value is 0x311, which according to
3436 * the manual should launch a user defined favorite
3439 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3440 * corresponding to 0x34.
3443 /* (assignments unknown, please report if found) */
3444 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3445 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3446 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3447 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3450 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3451 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
3452 KEY_CALC, /* Calculator (above numpad, P52) */
3453 KEY_BLUETOOTH, /* Bluetooth */
3454 KEY_KEYBOARD, /* Keyboard, 0x315 */
3455 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3456 KEY_NOTIFICATION_CENTER, /* Notification Center */
3457 KEY_PICKUP_PHONE, /* Answer incoming call */
3458 KEY_HANGUP_PHONE, /* Decline incoming call */
3462 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3463 /* Generic maps (fallback) */
3465 .vendor = PCI_VENDOR_ID_IBM,
3466 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3467 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3470 .vendor = PCI_VENDOR_ID_LENOVO,
3471 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3472 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3476 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3477 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3482 bool radiosw_state = false;
3483 bool tabletsw_state = false;
3485 unsigned long quirks;
3486 unsigned long keymap_id;
3488 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3489 "initializing hotkey subdriver\n");
3491 BUG_ON(!tpacpi_inputdev);
3492 BUG_ON(tpacpi_inputdev->open != NULL ||
3493 tpacpi_inputdev->close != NULL);
3495 TPACPI_ACPIHANDLE_INIT(hkey);
3496 mutex_init(&hotkey_mutex);
3498 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3499 mutex_init(&hotkey_thread_data_mutex);
3502 /* hotkey not supported on 570 */
3503 tp_features.hotkey = hkey_handle != NULL;
3505 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3507 str_supported(tp_features.hotkey));
3509 if (!tp_features.hotkey)
3512 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3513 ARRAY_SIZE(tpacpi_hotkey_qtable));
3515 tpacpi_disable_brightness_delay();
3517 /* MUST have enough space for all attributes to be added to
3518 * hotkey_dev_attributes */
3519 hotkey_dev_attributes = create_attr_set(
3520 ARRAY_SIZE(hotkey_attributes) + 2,
3522 if (!hotkey_dev_attributes)
3524 res = add_many_to_attr_set(hotkey_dev_attributes,
3526 ARRAY_SIZE(hotkey_attributes));
3530 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3531 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3532 for HKEY interface version 0x100 */
3533 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3534 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3535 "firmware HKEY interface version: 0x%x\n",
3538 switch (hkeyv >> 8) {
3541 * MHKV 0x100 in A31, R40, R40e,
3542 * T4x, X31, and later
3545 /* Paranoia check AND init hotkey_all_mask */
3546 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3548 pr_err("missing MHKA handler, please report this to %s\n",
3550 /* Fallback: pre-init for FN+F3,F4,F12 */
3551 hotkey_all_mask = 0x080cU;
3553 tp_features.hotkey_mask = 1;
3559 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3562 /* Paranoia check AND init hotkey_all_mask */
3563 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3565 pr_err("missing MHKA handler, please report this to %s\n",
3567 /* Fallback: pre-init for FN+F3,F4,F12 */
3568 hotkey_all_mask = 0x080cU;
3570 tp_features.hotkey_mask = 1;
3574 * Check if we have an adaptive keyboard, like on the
3575 * Lenovo Carbon X1 2014 (2nd Gen).
3577 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3579 if (hotkey_adaptive_all_mask != 0) {
3580 tp_features.has_adaptive_kbd = true;
3581 res = sysfs_create_group(
3582 &tpacpi_pdev->dev.kobj,
3583 &adaptive_kbd_attr_group);
3588 tp_features.has_adaptive_kbd = false;
3589 hotkey_adaptive_all_mask = 0x0U;
3594 pr_err("unknown version of the HKEY interface: 0x%x\n",
3596 pr_err("please report this to %s\n", TPACPI_MAIL);
3601 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3602 "hotkey masks are %s\n",
3603 str_supported(tp_features.hotkey_mask));
3605 /* Init hotkey_all_mask if not initialized yet */
3606 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3607 (quirks & TPACPI_HK_Q_INIMASK))
3608 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3610 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3611 if (tp_features.hotkey_mask) {
3612 /* hotkey_source_mask *must* be zero for
3613 * the first hotkey_mask_get to return hotkey_orig_mask */
3614 res = hotkey_mask_get();
3618 hotkey_orig_mask = hotkey_acpi_mask;
3620 hotkey_orig_mask = hotkey_all_mask;
3621 hotkey_acpi_mask = hotkey_all_mask;
3624 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3626 tp_features.hotkey_wlsw = 1;
3627 radiosw_state = !!tpacpi_wlsw_emulstate;
3628 pr_info("radio switch emulation enabled\n");
3631 /* Not all thinkpads have a hardware radio switch */
3632 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3633 tp_features.hotkey_wlsw = 1;
3634 radiosw_state = !!status;
3635 pr_info("radio switch found; radios are %s\n",
3636 enabled(status, 0));
3638 if (tp_features.hotkey_wlsw)
3639 res = add_to_attr_set(hotkey_dev_attributes,
3640 &dev_attr_hotkey_radio_sw.attr);
3642 res = hotkey_init_tablet_mode();
3646 tabletsw_state = res;
3648 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3649 &tpacpi_pdev->dev.kobj);
3653 /* Set up key map */
3654 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3655 ARRAY_SIZE(tpacpi_keymap_qtable));
3656 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3657 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3658 "using keymap number %lu\n", keymap_id);
3660 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3661 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3662 if (!hotkey_keycode_map) {
3663 pr_err("failed to allocate memory for key map\n");
3668 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3669 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3670 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3671 tpacpi_inputdev->keycode = hotkey_keycode_map;
3672 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3673 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3674 input_set_capability(tpacpi_inputdev, EV_KEY,
3675 hotkey_keycode_map[i]);
3677 if (i < sizeof(hotkey_reserved_mask)*8)
3678 hotkey_reserved_mask |= 1 << i;
3682 if (tp_features.hotkey_wlsw) {
3683 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3684 input_report_switch(tpacpi_inputdev,
3685 SW_RFKILL_ALL, radiosw_state);
3687 if (tp_features.hotkey_tablet) {
3688 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3689 input_report_switch(tpacpi_inputdev,
3690 SW_TABLET_MODE, tabletsw_state);
3693 /* Do not issue duplicate brightness change events to
3694 * userspace. tpacpi_detect_brightness_capabilities() must have
3695 * been called before this point */
3696 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3697 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3698 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3700 /* Disable brightness up/down on Lenovo thinkpads when
3701 * ACPI is handling them, otherwise it is plain impossible
3702 * for userspace to do something even remotely sane */
3703 hotkey_reserved_mask |=
3704 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3705 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3706 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3707 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3710 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3711 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3713 & ~hotkey_reserved_mask;
3715 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3716 "hotkey source mask 0x%08x, polling freq %u\n",
3717 hotkey_source_mask, hotkey_poll_freq);
3720 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3721 "enabling firmware HKEY event interface...\n");
3722 res = hotkey_status_set(true);
3727 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3728 | hotkey_driver_mask)
3729 & ~hotkey_source_mask);
3730 if (res < 0 && res != -ENXIO) {
3734 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3735 & ~hotkey_reserved_mask;
3736 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3737 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3738 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3740 tpacpi_inputdev->open = &hotkey_inputdev_open;
3741 tpacpi_inputdev->close = &hotkey_inputdev_close;
3743 hotkey_poll_setup_safe(true);
3748 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3749 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3750 &adaptive_kbd_attr_group);
3752 hotkey_dev_attributes = NULL;
3754 return (res < 0) ? res : 1;
3757 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3758 * mode, Web conference mode, Function mode and Lay-flat mode.
3759 * We support Home mode and Function mode currently.
3761 * Will consider support rest of modes in future.
3764 static const int adaptive_keyboard_modes[] = {
3766 /* WEB_BROWSER_MODE = 2,
3767 WEB_CONFERENCE_MODE = 3, */
3771 #define DFR_CHANGE_ROW 0x101
3772 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3773 #define FIRST_ADAPTIVE_KEY 0x103
3775 /* press Fn key a while second, it will switch to Function Mode. Then
3776 * release Fn key, previous mode be restored.
3778 static bool adaptive_keyboard_mode_is_saved;
3779 static int adaptive_keyboard_prev_mode;
3781 static int adaptive_keyboard_get_mode(void)
3785 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3786 pr_err("Cannot read adaptive keyboard mode\n");
3793 static int adaptive_keyboard_set_mode(int new_mode)
3796 new_mode > LAYFLAT_MODE)
3799 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3800 pr_err("Cannot set adaptive keyboard mode\n");
3807 static int adaptive_keyboard_get_next_mode(int mode)
3810 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3812 for (i = 0; i <= max_mode; i++) {
3813 if (adaptive_keyboard_modes[i] == mode)
3822 return adaptive_keyboard_modes[i];
3825 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3827 int current_mode = 0;
3832 case DFR_CHANGE_ROW:
3833 if (adaptive_keyboard_mode_is_saved) {
3834 new_mode = adaptive_keyboard_prev_mode;
3835 adaptive_keyboard_mode_is_saved = false;
3837 current_mode = adaptive_keyboard_get_mode();
3838 if (current_mode < 0)
3840 new_mode = adaptive_keyboard_get_next_mode(
3844 if (adaptive_keyboard_set_mode(new_mode) < 0)
3849 case DFR_SHOW_QUICKVIEW_ROW:
3850 current_mode = adaptive_keyboard_get_mode();
3851 if (current_mode < 0)
3854 adaptive_keyboard_prev_mode = current_mode;
3855 adaptive_keyboard_mode_is_saved = true;
3857 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3862 if (scancode < FIRST_ADAPTIVE_KEY ||
3863 scancode >= FIRST_ADAPTIVE_KEY +
3864 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3865 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3866 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3870 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3871 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3872 if (keycode != KEY_RESERVED) {
3873 mutex_lock(&tpacpi_inputdev_send_mutex);
3875 input_report_key(tpacpi_inputdev, keycode, 1);
3876 input_sync(tpacpi_inputdev);
3878 input_report_key(tpacpi_inputdev, keycode, 0);
3879 input_sync(tpacpi_inputdev);
3881 mutex_unlock(&tpacpi_inputdev_send_mutex);
3887 static bool hotkey_notify_hotkey(const u32 hkey,
3889 bool *ignore_acpi_ev)
3891 /* 0x1000-0x1FFF: key presses */
3892 unsigned int scancode = hkey & 0xfff;
3893 *send_acpi_ev = true;
3894 *ignore_acpi_ev = false;
3897 * Original events are in the 0x10XX range, the adaptive keyboard
3898 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3899 * models, additional keys are emitted through 0x13XX.
3901 switch ((hkey >> 8) & 0xf) {
3904 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3905 /* HKEY event 0x1001 is scancode 0x00 */
3907 if (!(hotkey_source_mask & (1 << scancode))) {
3908 tpacpi_input_send_key_masked(scancode);
3909 *send_acpi_ev = false;
3911 *ignore_acpi_ev = true;
3918 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3921 /* Extended keycodes start at 0x300 and our offset into the map
3922 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3923 * will be positive, but might not be in the correct range.
3925 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3926 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3927 scancode < TPACPI_HOTKEY_MAP_LEN) {
3928 tpacpi_input_send_key(scancode);
3937 static bool hotkey_notify_wakeup(const u32 hkey,
3939 bool *ignore_acpi_ev)
3941 /* 0x2000-0x2FFF: Wakeup reason */
3942 *send_acpi_ev = true;
3943 *ignore_acpi_ev = false;
3946 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3947 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3948 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3949 *ignore_acpi_ev = true;
3952 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3953 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3954 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3955 *ignore_acpi_ev = true;
3958 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3959 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3960 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3961 /* how to auto-heal: */
3962 /* 2313: woke up from S3, go to S4/S5 */
3963 /* 2413: woke up from S4, go to S5 */
3970 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3971 pr_info("woke up due to a hot-unplug request...\n");
3972 hotkey_wakeup_reason_notify_change();
3977 static bool hotkey_notify_dockevent(const u32 hkey,
3979 bool *ignore_acpi_ev)
3981 /* 0x4000-0x4FFF: dock-related events */
3982 *send_acpi_ev = true;
3983 *ignore_acpi_ev = false;
3986 case TP_HKEY_EV_UNDOCK_ACK:
3987 /* ACPI undock operation completed after wakeup */
3988 hotkey_autosleep_ack = 1;
3989 pr_info("undocked\n");
3990 hotkey_wakeup_hotunplug_complete_notify_change();
3993 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3994 pr_info("docked into hotplug port replicator\n");
3996 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3997 pr_info("undocked from hotplug port replicator\n");
4001 * Deliberately ignore attaching and detaching the keybord cover to avoid
4002 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
4005 * Please refer to the following thread for more information and a preliminary
4006 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
4007 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
4008 * the Pico cartridge dock module:
4009 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
4011 case TP_HKEY_EV_KBD_COVER_ATTACH:
4012 case TP_HKEY_EV_KBD_COVER_DETACH:
4013 *send_acpi_ev = false;
4014 *ignore_acpi_ev = true;
4022 static bool hotkey_notify_usrevent(const u32 hkey,
4024 bool *ignore_acpi_ev)
4026 /* 0x5000-0x5FFF: human interface helpers */
4027 *send_acpi_ev = true;
4028 *ignore_acpi_ev = false;
4031 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4032 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
4035 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4036 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4037 tpacpi_input_send_tabletsw();
4038 hotkey_tablet_mode_notify_change();
4039 *send_acpi_ev = false;
4042 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4043 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4044 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
4045 /* do not propagate these events */
4046 *ignore_acpi_ev = true;
4054 static void thermal_dump_all_sensors(void);
4055 static void palmsensor_refresh(void);
4057 static bool hotkey_notify_6xxx(const u32 hkey,
4059 bool *ignore_acpi_ev)
4061 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4062 *send_acpi_ev = true;
4063 *ignore_acpi_ev = false;
4066 case TP_HKEY_EV_THM_TABLE_CHANGED:
4067 pr_debug("EC reports: Thermal Table has changed\n");
4068 /* recommended action: do nothing, we don't have
4069 * Lenovo ATM information */
4071 case TP_HKEY_EV_THM_CSM_COMPLETED:
4072 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4073 /* Thermal event - pass on to event handler */
4074 tpacpi_driver_event(hkey);
4076 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4077 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
4078 /* recommended action: do nothing, we don't have
4079 * Lenovo ATM information */
4081 case TP_HKEY_EV_ALARM_BAT_HOT:
4082 pr_crit("THERMAL ALARM: battery is too hot!\n");
4083 /* recommended action: warn user through gui */
4085 case TP_HKEY_EV_ALARM_BAT_XHOT:
4086 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4087 /* recommended action: immediate sleep/hibernate */
4089 case TP_HKEY_EV_ALARM_SENSOR_HOT:
4090 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4091 /* recommended action: warn user through gui, that */
4092 /* some internal component is too hot */
4094 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
4095 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4096 /* recommended action: immediate sleep/hibernate */
4098 case TP_HKEY_EV_AC_CHANGED:
4099 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4100 * AC status changed; can be triggered by plugging or
4101 * unplugging AC adapter, docking or undocking. */
4105 case TP_HKEY_EV_KEY_NUMLOCK:
4106 case TP_HKEY_EV_KEY_FN:
4107 /* key press events, we just ignore them as long as the EC
4108 * is still reporting them in the normal keyboard stream */
4109 *send_acpi_ev = false;
4110 *ignore_acpi_ev = true;
4113 case TP_HKEY_EV_KEY_FN_ESC:
4114 /* Get the media key status to force the status LED to update */
4115 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
4116 *send_acpi_ev = false;
4117 *ignore_acpi_ev = true;
4120 case TP_HKEY_EV_TABLET_CHANGED:
4121 tpacpi_input_send_tabletsw();
4122 hotkey_tablet_mode_notify_change();
4123 *send_acpi_ev = false;
4126 case TP_HKEY_EV_PALM_DETECTED:
4127 case TP_HKEY_EV_PALM_UNDETECTED:
4128 /* palm detected - pass on to event handler */
4129 palmsensor_refresh();
4133 /* report simply as unknown, no sensor dump */
4137 thermal_dump_all_sensors();
4141 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4145 bool ignore_acpi_ev;
4148 if (event != 0x80) {
4149 pr_err("unknown HKEY notification event %d\n", event);
4150 /* forward it to userspace, maybe it knows how to handle it */
4151 acpi_bus_generate_netlink_event(
4152 ibm->acpi->device->pnp.device_class,
4153 dev_name(&ibm->acpi->device->dev),
4159 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4160 pr_err("failed to retrieve HKEY event\n");
4169 send_acpi_ev = true;
4170 ignore_acpi_ev = false;
4172 switch (hkey >> 12) {
4174 /* 0x1000-0x1FFF: key presses */
4175 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4179 /* 0x2000-0x2FFF: Wakeup reason */
4180 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4184 /* 0x3000-0x3FFF: bay-related wakeups */
4186 case TP_HKEY_EV_BAYEJ_ACK:
4187 hotkey_autosleep_ack = 1;
4188 pr_info("bay ejected\n");
4189 hotkey_wakeup_hotunplug_complete_notify_change();
4192 case TP_HKEY_EV_OPTDRV_EJ:
4193 /* FIXME: kick libata if SATA link offline */
4201 /* 0x4000-0x4FFF: dock-related events */
4202 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4206 /* 0x5000-0x5FFF: human interface helpers */
4207 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4211 /* 0x6000-0x6FFF: thermal alarms/notices and
4212 * keyboard events */
4213 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4217 /* 0x7000-0x7FFF: misc */
4218 if (tp_features.hotkey_wlsw &&
4219 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4220 tpacpi_send_radiosw_update();
4225 fallthrough; /* to default */
4230 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4231 pr_notice("please report the conditions when this event happened to %s\n",
4235 /* netlink events */
4236 if (!ignore_acpi_ev && send_acpi_ev) {
4237 acpi_bus_generate_netlink_event(
4238 ibm->acpi->device->pnp.device_class,
4239 dev_name(&ibm->acpi->device->dev),
4245 static void hotkey_suspend(void)
4247 /* Do these on suspend, we get the events on early resume! */
4248 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4249 hotkey_autosleep_ack = 0;
4251 /* save previous mode of adaptive keyboard of X1 Carbon */
4252 if (tp_features.has_adaptive_kbd) {
4253 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4255 pr_err("Cannot read adaptive keyboard mode.\n");
4260 static void hotkey_resume(void)
4262 tpacpi_disable_brightness_delay();
4264 if (hotkey_status_set(true) < 0 ||
4265 hotkey_mask_set(hotkey_acpi_mask) < 0)
4266 pr_err("error while attempting to reset the event firmware interface\n");
4268 tpacpi_send_radiosw_update();
4269 tpacpi_input_send_tabletsw();
4270 hotkey_tablet_mode_notify_change();
4271 hotkey_wakeup_reason_notify_change();
4272 hotkey_wakeup_hotunplug_complete_notify_change();
4273 hotkey_poll_setup_safe(false);
4275 /* restore previous mode of adapive keyboard of X1 Carbon */
4276 if (tp_features.has_adaptive_kbd) {
4277 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4278 adaptive_keyboard_prev_mode)) {
4279 pr_err("Cannot set adaptive keyboard mode.\n");
4284 /* procfs -------------------------------------------------------------- */
4285 static int hotkey_read(struct seq_file *m)
4289 if (!tp_features.hotkey) {
4290 seq_printf(m, "status:\t\tnot supported\n");
4294 if (mutex_lock_killable(&hotkey_mutex))
4295 return -ERESTARTSYS;
4296 res = hotkey_status_get(&status);
4298 res = hotkey_mask_get();
4299 mutex_unlock(&hotkey_mutex);
4303 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4304 if (hotkey_all_mask) {
4305 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4306 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4308 seq_printf(m, "mask:\t\tnot supported\n");
4309 seq_printf(m, "commands:\tenable, disable, reset\n");
4315 static void hotkey_enabledisable_warn(bool enable)
4317 tpacpi_log_usertask("procfs hotkey enable/disable");
4318 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4319 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4320 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4323 static int hotkey_write(char *buf)
4329 if (!tp_features.hotkey)
4332 if (mutex_lock_killable(&hotkey_mutex))
4333 return -ERESTARTSYS;
4335 mask = hotkey_user_mask;
4338 while ((cmd = strsep(&buf, ","))) {
4339 if (strlencmp(cmd, "enable") == 0) {
4340 hotkey_enabledisable_warn(1);
4341 } else if (strlencmp(cmd, "disable") == 0) {
4342 hotkey_enabledisable_warn(0);
4344 } else if (strlencmp(cmd, "reset") == 0) {
4345 mask = (hotkey_all_mask | hotkey_source_mask)
4346 & ~hotkey_reserved_mask;
4347 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4349 } else if (sscanf(cmd, "%x", &mask) == 1) {
4358 tpacpi_disclose_usertask("procfs hotkey",
4359 "set mask to 0x%08x\n", mask);
4360 res = hotkey_user_mask_set(mask);
4364 mutex_unlock(&hotkey_mutex);
4368 static const struct acpi_device_id ibm_htk_device_ids[] = {
4369 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4370 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4371 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4375 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4376 .hid = ibm_htk_device_ids,
4377 .notify = hotkey_notify,
4378 .handle = &hkey_handle,
4379 .type = ACPI_DEVICE_NOTIFY,
4382 static struct ibm_struct hotkey_driver_data = {
4384 .read = hotkey_read,
4385 .write = hotkey_write,
4386 .exit = hotkey_exit,
4387 .resume = hotkey_resume,
4388 .suspend = hotkey_suspend,
4389 .acpi = &ibm_hotkey_acpidriver,
4392 /*************************************************************************
4393 * Bluetooth subdriver
4397 /* ACPI GBDC/SBDC bits */
4398 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4399 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4400 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4401 0 = disable, 1 = enable */
4405 /* ACPI \BLTH commands */
4406 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4407 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4408 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4409 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4410 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4413 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4415 static int bluetooth_get_status(void)
4419 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4420 if (dbg_bluetoothemul)
4421 return (tpacpi_bluetooth_emulstate) ?
4422 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4425 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4428 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4429 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4432 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4436 vdbg_printk(TPACPI_DBG_RFKILL,
4437 "will attempt to %s bluetooth\n",
4438 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4440 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4441 if (dbg_bluetoothemul) {
4442 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4447 if (state == TPACPI_RFK_RADIO_ON)
4448 status = TP_ACPI_BLUETOOTH_RADIOSSW
4449 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4453 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4459 /* sysfs bluetooth enable ---------------------------------------------- */
4460 static ssize_t bluetooth_enable_show(struct device *dev,
4461 struct device_attribute *attr,
4464 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4468 static ssize_t bluetooth_enable_store(struct device *dev,
4469 struct device_attribute *attr,
4470 const char *buf, size_t count)
4472 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4476 static DEVICE_ATTR_RW(bluetooth_enable);
4478 /* --------------------------------------------------------------------- */
4480 static struct attribute *bluetooth_attributes[] = {
4481 &dev_attr_bluetooth_enable.attr,
4485 static const struct attribute_group bluetooth_attr_group = {
4486 .attrs = bluetooth_attributes,
4489 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4490 .get_status = bluetooth_get_status,
4491 .set_status = bluetooth_set_status,
4494 static void bluetooth_shutdown(void)
4496 /* Order firmware to save current state to NVRAM */
4497 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4498 TP_ACPI_BLTH_SAVE_STATE))
4499 pr_notice("failed to save bluetooth state to NVRAM\n");
4501 vdbg_printk(TPACPI_DBG_RFKILL,
4502 "bluetooth state saved to NVRAM\n");
4505 static void bluetooth_exit(void)
4507 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4508 &bluetooth_attr_group);
4510 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4512 bluetooth_shutdown();
4515 static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4517 .ident = "ThinkPad E485",
4519 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4520 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4524 .ident = "ThinkPad E585",
4526 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4527 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4531 .ident = "ThinkPad A285 - 20MW",
4533 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4534 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4538 .ident = "ThinkPad A285 - 20MX",
4540 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4541 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4545 .ident = "ThinkPad A485 - 20MU",
4547 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4548 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4552 .ident = "ThinkPad A485 - 20MV",
4554 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4555 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4561 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4562 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4563 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4564 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4569 static int __init have_bt_fwbug(void)
4572 * Some AMD based ThinkPads have a firmware bug that calling
4573 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4575 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4576 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4577 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4583 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4588 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4589 "initializing bluetooth subdriver\n");
4591 TPACPI_ACPIHANDLE_INIT(hkey);
4593 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4594 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4595 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4596 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4598 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4599 "bluetooth is %s, status 0x%02x\n",
4600 str_supported(tp_features.bluetooth),
4603 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4604 if (dbg_bluetoothemul) {
4605 tp_features.bluetooth = 1;
4606 pr_info("bluetooth switch emulation enabled\n");
4609 if (tp_features.bluetooth &&
4610 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4611 /* no bluetooth hardware present in system */
4612 tp_features.bluetooth = 0;
4613 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4614 "bluetooth hardware not installed\n");
4617 if (!tp_features.bluetooth)
4620 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4621 &bluetooth_tprfk_ops,
4622 RFKILL_TYPE_BLUETOOTH,
4623 TPACPI_RFK_BLUETOOTH_SW_NAME,
4628 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4629 &bluetooth_attr_group);
4631 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4638 /* procfs -------------------------------------------------------------- */
4639 static int bluetooth_read(struct seq_file *m)
4641 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4644 static int bluetooth_write(char *buf)
4646 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4649 static struct ibm_struct bluetooth_driver_data = {
4650 .name = "bluetooth",
4651 .read = bluetooth_read,
4652 .write = bluetooth_write,
4653 .exit = bluetooth_exit,
4654 .shutdown = bluetooth_shutdown,
4657 /*************************************************************************
4662 /* ACPI GWAN/SWAN bits */
4663 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4664 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4665 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4666 0 = disable, 1 = enable */
4669 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4671 static int wan_get_status(void)
4675 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4677 return (tpacpi_wwan_emulstate) ?
4678 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4681 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4684 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4685 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4688 static int wan_set_status(enum tpacpi_rfkill_state state)
4692 vdbg_printk(TPACPI_DBG_RFKILL,
4693 "will attempt to %s wwan\n",
4694 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4696 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4698 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4703 if (state == TPACPI_RFK_RADIO_ON)
4704 status = TP_ACPI_WANCARD_RADIOSSW
4705 | TP_ACPI_WANCARD_RESUMECTRL;
4709 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4715 /* sysfs wan enable ---------------------------------------------------- */
4716 static ssize_t wan_enable_show(struct device *dev,
4717 struct device_attribute *attr,
4720 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4724 static ssize_t wan_enable_store(struct device *dev,
4725 struct device_attribute *attr,
4726 const char *buf, size_t count)
4728 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4732 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4733 wan_enable_show, wan_enable_store);
4735 /* --------------------------------------------------------------------- */
4737 static struct attribute *wan_attributes[] = {
4738 &dev_attr_wwan_enable.attr,
4742 static const struct attribute_group wan_attr_group = {
4743 .attrs = wan_attributes,
4746 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4747 .get_status = wan_get_status,
4748 .set_status = wan_set_status,
4751 static void wan_shutdown(void)
4753 /* Order firmware to save current state to NVRAM */
4754 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4755 TP_ACPI_WGSV_SAVE_STATE))
4756 pr_notice("failed to save WWAN state to NVRAM\n");
4758 vdbg_printk(TPACPI_DBG_RFKILL,
4759 "WWAN state saved to NVRAM\n");
4762 static void wan_exit(void)
4764 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4767 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4772 static int __init wan_init(struct ibm_init_struct *iibm)
4777 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4778 "initializing wan subdriver\n");
4780 TPACPI_ACPIHANDLE_INIT(hkey);
4782 tp_features.wan = hkey_handle &&
4783 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4785 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4786 "wan is %s, status 0x%02x\n",
4787 str_supported(tp_features.wan),
4790 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4792 tp_features.wan = 1;
4793 pr_info("wwan switch emulation enabled\n");
4796 if (tp_features.wan &&
4797 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4798 /* no wan hardware present in system */
4799 tp_features.wan = 0;
4800 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4801 "wan hardware not installed\n");
4804 if (!tp_features.wan)
4807 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4810 TPACPI_RFK_WWAN_SW_NAME,
4815 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4819 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4826 /* procfs -------------------------------------------------------------- */
4827 static int wan_read(struct seq_file *m)
4829 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4832 static int wan_write(char *buf)
4834 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4837 static struct ibm_struct wan_driver_data = {
4842 .shutdown = wan_shutdown,
4845 /*************************************************************************
4850 /* ACPI GUWB/SUWB bits */
4851 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4852 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4855 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4857 static int uwb_get_status(void)
4861 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4863 return (tpacpi_uwb_emulstate) ?
4864 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4867 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4870 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4871 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4874 static int uwb_set_status(enum tpacpi_rfkill_state state)
4878 vdbg_printk(TPACPI_DBG_RFKILL,
4879 "will attempt to %s UWB\n",
4880 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4882 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4884 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4889 if (state == TPACPI_RFK_RADIO_ON)
4890 status = TP_ACPI_UWB_RADIOSSW;
4894 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4900 /* --------------------------------------------------------------------- */
4902 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4903 .get_status = uwb_get_status,
4904 .set_status = uwb_set_status,
4907 static void uwb_exit(void)
4909 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4912 static int __init uwb_init(struct ibm_init_struct *iibm)
4917 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4918 "initializing uwb subdriver\n");
4920 TPACPI_ACPIHANDLE_INIT(hkey);
4922 tp_features.uwb = hkey_handle &&
4923 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4925 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4926 "uwb is %s, status 0x%02x\n",
4927 str_supported(tp_features.uwb),
4930 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4932 tp_features.uwb = 1;
4933 pr_info("uwb switch emulation enabled\n");
4936 if (tp_features.uwb &&
4937 !(status & TP_ACPI_UWB_HWPRESENT)) {
4938 /* no uwb hardware present in system */
4939 tp_features.uwb = 0;
4940 dbg_printk(TPACPI_DBG_INIT,
4941 "uwb hardware not installed\n");
4944 if (!tp_features.uwb)
4947 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4950 TPACPI_RFK_UWB_SW_NAME,
4955 static struct ibm_struct uwb_driver_data = {
4958 .flags.experimental = 1,
4961 /*************************************************************************
4965 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4967 enum video_access_mode {
4968 TPACPI_VIDEO_NONE = 0,
4969 TPACPI_VIDEO_570, /* 570 */
4970 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4971 TPACPI_VIDEO_NEW, /* all others */
4974 enum { /* video status flags, based on VIDEO_570 */
4975 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4976 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4977 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4980 enum { /* TPACPI_VIDEO_570 constants */
4981 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4982 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4983 * video_status_flags */
4984 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4985 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4988 static enum video_access_mode video_supported;
4989 static int video_orig_autosw;
4991 static int video_autosw_get(void);
4992 static int video_autosw_set(int enable);
4994 TPACPI_HANDLE(vid, root,
4995 "\\_SB.PCI.AGP.VGA", /* 570 */
4996 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4997 "\\_SB.PCI0.VID0", /* 770e */
4998 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4999 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
5000 "\\_SB.PCI0.AGP.VID", /* all others */
5003 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
5005 static int __init video_init(struct ibm_init_struct *iibm)
5009 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
5011 TPACPI_ACPIHANDLE_INIT(vid);
5012 if (tpacpi_is_ibm())
5013 TPACPI_ACPIHANDLE_INIT(vid2);
5015 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
5016 /* G41, assume IVGA doesn't change */
5017 vid_handle = vid2_handle;
5020 /* video switching not supported on R30, R31 */
5021 video_supported = TPACPI_VIDEO_NONE;
5022 else if (tpacpi_is_ibm() &&
5023 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
5025 video_supported = TPACPI_VIDEO_570;
5026 else if (tpacpi_is_ibm() &&
5027 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
5028 /* 600e/x, 770e, 770x */
5029 video_supported = TPACPI_VIDEO_770;
5032 video_supported = TPACPI_VIDEO_NEW;
5034 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
5035 str_supported(video_supported != TPACPI_VIDEO_NONE),
5038 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
5041 static void video_exit(void)
5043 dbg_printk(TPACPI_DBG_EXIT,
5044 "restoring original video autoswitch mode\n");
5045 if (video_autosw_set(video_orig_autosw))
5046 pr_err("error while trying to restore original video autoswitch mode\n");
5049 static int video_outputsw_get(void)
5054 switch (video_supported) {
5055 case TPACPI_VIDEO_570:
5056 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5057 TP_ACPI_VIDEO_570_PHSCMD))
5059 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5061 case TPACPI_VIDEO_770:
5062 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5065 status |= TP_ACPI_VIDEO_S_LCD;
5066 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5069 status |= TP_ACPI_VIDEO_S_CRT;
5071 case TPACPI_VIDEO_NEW:
5072 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5073 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5076 status |= TP_ACPI_VIDEO_S_CRT;
5078 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5079 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5082 status |= TP_ACPI_VIDEO_S_LCD;
5083 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5086 status |= TP_ACPI_VIDEO_S_DVI;
5095 static int video_outputsw_set(int status)
5100 switch (video_supported) {
5101 case TPACPI_VIDEO_570:
5102 res = acpi_evalf(NULL, NULL,
5103 "\\_SB.PHS2", "vdd",
5104 TP_ACPI_VIDEO_570_PHS2CMD,
5105 status | TP_ACPI_VIDEO_570_PHS2SET);
5107 case TPACPI_VIDEO_770:
5108 autosw = video_autosw_get();
5112 res = video_autosw_set(1);
5115 res = acpi_evalf(vid_handle, NULL,
5116 "ASWT", "vdd", status * 0x100, 0);
5117 if (!autosw && video_autosw_set(autosw)) {
5118 pr_err("video auto-switch left enabled due to error\n");
5122 case TPACPI_VIDEO_NEW:
5123 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5124 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5130 return (res) ? 0 : -EIO;
5133 static int video_autosw_get(void)
5137 switch (video_supported) {
5138 case TPACPI_VIDEO_570:
5139 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5142 case TPACPI_VIDEO_770:
5143 case TPACPI_VIDEO_NEW:
5144 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5154 static int video_autosw_set(int enable)
5156 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5161 static int video_outputsw_cycle(void)
5163 int autosw = video_autosw_get();
5169 switch (video_supported) {
5170 case TPACPI_VIDEO_570:
5171 res = video_autosw_set(1);
5174 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5176 case TPACPI_VIDEO_770:
5177 case TPACPI_VIDEO_NEW:
5178 res = video_autosw_set(1);
5181 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5186 if (!autosw && video_autosw_set(autosw)) {
5187 pr_err("video auto-switch left enabled due to error\n");
5191 return (res) ? 0 : -EIO;
5194 static int video_expand_toggle(void)
5196 switch (video_supported) {
5197 case TPACPI_VIDEO_570:
5198 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5200 case TPACPI_VIDEO_770:
5201 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5203 case TPACPI_VIDEO_NEW:
5204 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5212 static int video_read(struct seq_file *m)
5216 if (video_supported == TPACPI_VIDEO_NONE) {
5217 seq_printf(m, "status:\t\tnot supported\n");
5221 /* Even reads can crash X.org, so... */
5222 if (!capable(CAP_SYS_ADMIN))
5225 status = video_outputsw_get();
5229 autosw = video_autosw_get();
5233 seq_printf(m, "status:\t\tsupported\n");
5234 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5235 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5236 if (video_supported == TPACPI_VIDEO_NEW)
5237 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5238 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5239 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5240 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5241 if (video_supported == TPACPI_VIDEO_NEW)
5242 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5243 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5244 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5249 static int video_write(char *buf)
5252 int enable, disable, status;
5255 if (video_supported == TPACPI_VIDEO_NONE)
5258 /* Even reads can crash X.org, let alone writes... */
5259 if (!capable(CAP_SYS_ADMIN))
5265 while ((cmd = strsep(&buf, ","))) {
5266 if (strlencmp(cmd, "lcd_enable") == 0) {
5267 enable |= TP_ACPI_VIDEO_S_LCD;
5268 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5269 disable |= TP_ACPI_VIDEO_S_LCD;
5270 } else if (strlencmp(cmd, "crt_enable") == 0) {
5271 enable |= TP_ACPI_VIDEO_S_CRT;
5272 } else if (strlencmp(cmd, "crt_disable") == 0) {
5273 disable |= TP_ACPI_VIDEO_S_CRT;
5274 } else if (video_supported == TPACPI_VIDEO_NEW &&
5275 strlencmp(cmd, "dvi_enable") == 0) {
5276 enable |= TP_ACPI_VIDEO_S_DVI;
5277 } else if (video_supported == TPACPI_VIDEO_NEW &&
5278 strlencmp(cmd, "dvi_disable") == 0) {
5279 disable |= TP_ACPI_VIDEO_S_DVI;
5280 } else if (strlencmp(cmd, "auto_enable") == 0) {
5281 res = video_autosw_set(1);
5284 } else if (strlencmp(cmd, "auto_disable") == 0) {
5285 res = video_autosw_set(0);
5288 } else if (strlencmp(cmd, "video_switch") == 0) {
5289 res = video_outputsw_cycle();
5292 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5293 res = video_expand_toggle();
5300 if (enable || disable) {
5301 status = video_outputsw_get();
5304 res = video_outputsw_set((status & ~disable) | enable);
5312 static struct ibm_struct video_driver_data = {
5315 .write = video_write,
5319 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5321 /*************************************************************************
5322 * Keyboard backlight subdriver
5325 static enum led_brightness kbdlight_brightness;
5326 static DEFINE_MUTEX(kbdlight_mutex);
5328 static int kbdlight_set_level(int level)
5335 mutex_lock(&kbdlight_mutex);
5337 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5340 kbdlight_brightness = level;
5342 mutex_unlock(&kbdlight_mutex);
5347 static int kbdlight_get_level(void)
5354 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5360 return status & 0x3;
5363 static bool kbdlight_is_supported(void)
5370 if (!acpi_has_method(hkey_handle, "MLCG")) {
5371 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5375 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5376 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5381 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5385 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5387 * Guessed test for keyboard backlight:
5389 * Machines with backlight keyboard return:
5390 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5391 * b110100010010000000XX - ThinkPad x230
5392 * b010100000010000000XX - ThinkPad x240
5393 * b010100000010000000XX - ThinkPad W541
5394 * (XX is current backlight level)
5396 * Machines without backlight keyboard return:
5397 * b10100001000000000000 - ThinkPad x230
5398 * b10110001000000000000 - ThinkPad E430
5399 * b00000000000000000000 - ThinkPad E450
5401 * Candidate BITs for detection test (XOR):
5402 * b01000000001000000000
5405 return status & BIT(9);
5408 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5409 enum led_brightness brightness)
5411 return kbdlight_set_level(brightness);
5414 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5418 level = kbdlight_get_level();
5425 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5427 .name = "tpacpi::kbd_backlight",
5428 .max_brightness = 2,
5429 .flags = LED_BRIGHT_HW_CHANGED,
5430 .brightness_set_blocking = &kbdlight_sysfs_set,
5431 .brightness_get = &kbdlight_sysfs_get,
5435 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5439 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5441 TPACPI_ACPIHANDLE_INIT(hkey);
5443 if (!kbdlight_is_supported()) {
5444 tp_features.kbdlight = 0;
5445 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5449 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5450 tp_features.kbdlight = 1;
5452 rc = led_classdev_register(&tpacpi_pdev->dev,
5453 &tpacpi_led_kbdlight.led_classdev);
5455 tp_features.kbdlight = 0;
5459 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5460 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5464 static void kbdlight_exit(void)
5466 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5469 static int kbdlight_set_level_and_update(int level)
5472 struct led_classdev *led_cdev;
5474 ret = kbdlight_set_level(level);
5475 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5477 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5478 led_cdev->brightness = level;
5483 static int kbdlight_read(struct seq_file *m)
5487 if (!tp_features.kbdlight) {
5488 seq_printf(m, "status:\t\tnot supported\n");
5490 level = kbdlight_get_level();
5492 seq_printf(m, "status:\t\terror %d\n", level);
5494 seq_printf(m, "status:\t\t%d\n", level);
5495 seq_printf(m, "commands:\t0, 1, 2\n");
5501 static int kbdlight_write(char *buf)
5504 int res, level = -EINVAL;
5506 if (!tp_features.kbdlight)
5509 while ((cmd = strsep(&buf, ","))) {
5510 res = kstrtoint(cmd, 10, &level);
5515 if (level >= 3 || level < 0)
5518 return kbdlight_set_level_and_update(level);
5521 static void kbdlight_suspend(void)
5523 struct led_classdev *led_cdev;
5525 if (!tp_features.kbdlight)
5528 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5529 led_update_brightness(led_cdev);
5530 led_classdev_suspend(led_cdev);
5533 static void kbdlight_resume(void)
5535 if (!tp_features.kbdlight)
5538 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5541 static struct ibm_struct kbdlight_driver_data = {
5543 .read = kbdlight_read,
5544 .write = kbdlight_write,
5545 .suspend = kbdlight_suspend,
5546 .resume = kbdlight_resume,
5547 .exit = kbdlight_exit,
5550 /*************************************************************************
5551 * Light (thinklight) subdriver
5554 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5555 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5557 static int light_get_status(void)
5561 if (tp_features.light_status) {
5562 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5570 static int light_set_status(int status)
5574 if (tp_features.light) {
5576 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5578 TP_CMOS_THINKLIGHT_ON :
5579 TP_CMOS_THINKLIGHT_OFF);
5581 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5584 return (rc) ? 0 : -EIO;
5590 static int light_sysfs_set(struct led_classdev *led_cdev,
5591 enum led_brightness brightness)
5593 return light_set_status((brightness != LED_OFF) ?
5594 TPACPI_LED_ON : TPACPI_LED_OFF);
5597 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5599 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5602 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5604 .name = "tpacpi::thinklight",
5605 .brightness_set_blocking = &light_sysfs_set,
5606 .brightness_get = &light_sysfs_get,
5610 static int __init light_init(struct ibm_init_struct *iibm)
5614 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5616 if (tpacpi_is_ibm()) {
5617 TPACPI_ACPIHANDLE_INIT(ledb);
5618 TPACPI_ACPIHANDLE_INIT(lght);
5620 TPACPI_ACPIHANDLE_INIT(cmos);
5622 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5623 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5625 if (tp_features.light)
5626 /* light status not supported on
5627 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5628 tp_features.light_status =
5629 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5631 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5632 str_supported(tp_features.light),
5633 str_supported(tp_features.light_status));
5635 if (!tp_features.light)
5638 rc = led_classdev_register(&tpacpi_pdev->dev,
5639 &tpacpi_led_thinklight.led_classdev);
5642 tp_features.light = 0;
5643 tp_features.light_status = 0;
5651 static void light_exit(void)
5653 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5656 static int light_read(struct seq_file *m)
5660 if (!tp_features.light) {
5661 seq_printf(m, "status:\t\tnot supported\n");
5662 } else if (!tp_features.light_status) {
5663 seq_printf(m, "status:\t\tunknown\n");
5664 seq_printf(m, "commands:\ton, off\n");
5666 status = light_get_status();
5669 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5670 seq_printf(m, "commands:\ton, off\n");
5676 static int light_write(char *buf)
5681 if (!tp_features.light)
5684 while ((cmd = strsep(&buf, ","))) {
5685 if (strlencmp(cmd, "on") == 0) {
5687 } else if (strlencmp(cmd, "off") == 0) {
5693 return light_set_status(newstatus);
5696 static struct ibm_struct light_driver_data = {
5699 .write = light_write,
5703 /*************************************************************************
5707 /* sysfs cmos_command -------------------------------------------------- */
5708 static ssize_t cmos_command_store(struct device *dev,
5709 struct device_attribute *attr,
5710 const char *buf, size_t count)
5712 unsigned long cmos_cmd;
5715 if (parse_strtoul(buf, 21, &cmos_cmd))
5718 res = issue_thinkpad_cmos_command(cmos_cmd);
5719 return (res) ? res : count;
5722 static DEVICE_ATTR_WO(cmos_command);
5724 /* --------------------------------------------------------------------- */
5726 static int __init cmos_init(struct ibm_init_struct *iibm)
5730 vdbg_printk(TPACPI_DBG_INIT,
5731 "initializing cmos commands subdriver\n");
5733 TPACPI_ACPIHANDLE_INIT(cmos);
5735 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5736 str_supported(cmos_handle != NULL));
5738 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5742 return (cmos_handle) ? 0 : 1;
5745 static void cmos_exit(void)
5747 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5750 static int cmos_read(struct seq_file *m)
5752 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5753 R30, R31, T20-22, X20-21 */
5755 seq_printf(m, "status:\t\tnot supported\n");
5757 seq_printf(m, "status:\t\tsupported\n");
5758 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5764 static int cmos_write(char *buf)
5769 while ((cmd = strsep(&buf, ","))) {
5770 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5771 cmos_cmd >= 0 && cmos_cmd <= 21) {
5776 res = issue_thinkpad_cmos_command(cmos_cmd);
5784 static struct ibm_struct cmos_driver_data = {
5787 .write = cmos_write,
5791 /*************************************************************************
5795 enum led_access_mode {
5796 TPACPI_LED_NONE = 0,
5797 TPACPI_LED_570, /* 570 */
5798 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5799 TPACPI_LED_NEW, /* all others */
5802 enum { /* For TPACPI_LED_OLD */
5803 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5804 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5805 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5808 static enum led_access_mode led_supported;
5810 static acpi_handle led_handle;
5812 #define TPACPI_LED_NUMLEDS 16
5813 static struct tpacpi_led_classdev *tpacpi_leds;
5814 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5815 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5816 /* there's a limit of 19 chars + NULL before 2.6.26 */
5818 "tpacpi:orange:batt",
5819 "tpacpi:green:batt",
5820 "tpacpi::dock_active",
5821 "tpacpi::bay_active",
5822 "tpacpi::dock_batt",
5823 "tpacpi::unknown_led",
5825 "tpacpi::dock_status1",
5826 "tpacpi::dock_status2",
5827 "tpacpi::unknown_led2",
5828 "tpacpi::unknown_led3",
5829 "tpacpi::thinkvantage",
5831 #define TPACPI_SAFE_LEDS 0x1081U
5833 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5835 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5838 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5842 static int led_get_status(const unsigned int led)
5845 enum led_status_t led_s;
5847 switch (led_supported) {
5848 case TPACPI_LED_570:
5849 if (!acpi_evalf(ec_handle,
5850 &status, "GLED", "dd", 1 << led))
5852 led_s = (status == 0) ?
5857 tpacpi_led_state_cache[led] = led_s;
5866 static int led_set_status(const unsigned int led,
5867 const enum led_status_t ledstatus)
5869 /* off, on, blink. Index is led_status_t */
5870 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5871 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5875 switch (led_supported) {
5876 case TPACPI_LED_570:
5878 if (unlikely(led > 7))
5880 if (unlikely(tpacpi_is_led_restricted(led)))
5882 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5883 (1 << led), led_sled_arg1[ledstatus]))
5886 case TPACPI_LED_OLD:
5887 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5888 if (unlikely(led > 7))
5890 if (unlikely(tpacpi_is_led_restricted(led)))
5892 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5894 rc = ec_write(TPACPI_LED_EC_HLBL,
5895 (ledstatus == TPACPI_LED_BLINK) << led);
5897 rc = ec_write(TPACPI_LED_EC_HLCL,
5898 (ledstatus != TPACPI_LED_OFF) << led);
5900 case TPACPI_LED_NEW:
5902 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5904 if (unlikely(tpacpi_is_led_restricted(led)))
5906 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5907 led, led_led_arg1[ledstatus]))
5915 tpacpi_led_state_cache[led] = ledstatus;
5920 static int led_sysfs_set(struct led_classdev *led_cdev,
5921 enum led_brightness brightness)
5923 struct tpacpi_led_classdev *data = container_of(led_cdev,
5924 struct tpacpi_led_classdev, led_classdev);
5925 enum led_status_t new_state;
5927 if (brightness == LED_OFF)
5928 new_state = TPACPI_LED_OFF;
5929 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5930 new_state = TPACPI_LED_ON;
5932 new_state = TPACPI_LED_BLINK;
5934 return led_set_status(data->led, new_state);
5937 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5938 unsigned long *delay_on, unsigned long *delay_off)
5940 struct tpacpi_led_classdev *data = container_of(led_cdev,
5941 struct tpacpi_led_classdev, led_classdev);
5943 /* Can we choose the flash rate? */
5944 if (*delay_on == 0 && *delay_off == 0) {
5945 /* yes. set them to the hardware blink rate (1 Hz) */
5946 *delay_on = 500; /* ms */
5947 *delay_off = 500; /* ms */
5948 } else if ((*delay_on != 500) || (*delay_off != 500))
5951 return led_set_status(data->led, TPACPI_LED_BLINK);
5954 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5958 struct tpacpi_led_classdev *data = container_of(led_cdev,
5959 struct tpacpi_led_classdev, led_classdev);
5961 rc = led_get_status(data->led);
5963 if (rc == TPACPI_LED_OFF || rc < 0)
5964 rc = LED_OFF; /* no error handling in led class :( */
5971 static void led_exit(void)
5975 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5976 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5981 static int __init tpacpi_init_led(unsigned int led)
5983 /* LEDs with no name don't get registered */
5984 if (!tpacpi_led_names[led])
5987 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5988 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5989 if (led_supported == TPACPI_LED_570)
5990 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5992 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5993 tpacpi_leds[led].led = led;
5995 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5998 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5999 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
6000 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
6001 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
6003 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
6004 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
6005 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
6006 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
6007 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
6008 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
6009 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
6010 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
6012 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
6013 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
6014 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
6015 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
6016 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
6018 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
6019 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
6020 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
6021 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
6023 /* (1) - may have excess leds enabled on MSB */
6025 /* Defaults (order matters, keep last, don't reorder!) */
6027 .vendor = PCI_VENDOR_ID_LENOVO,
6028 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6031 { /* IBM ThinkPads with no EC version string */
6032 .vendor = PCI_VENDOR_ID_IBM,
6033 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6036 { /* IBM ThinkPads with EC version string */
6037 .vendor = PCI_VENDOR_ID_IBM,
6038 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6043 static enum led_access_mode __init led_init_detect_mode(void)
6047 if (tpacpi_is_ibm()) {
6049 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6050 if (ACPI_SUCCESS(status))
6051 return TPACPI_LED_570;
6053 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6054 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6055 if (ACPI_SUCCESS(status))
6056 return TPACPI_LED_OLD;
6060 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6061 if (ACPI_SUCCESS(status))
6062 return TPACPI_LED_NEW;
6064 /* R30, R31, and unknown firmwares */
6066 return TPACPI_LED_NONE;
6069 static int __init led_init(struct ibm_init_struct *iibm)
6073 unsigned long useful_leds;
6075 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6077 led_supported = led_init_detect_mode();
6079 if (led_supported != TPACPI_LED_NONE) {
6080 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6081 ARRAY_SIZE(led_useful_qtable));
6085 led_supported = TPACPI_LED_NONE;
6089 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6090 str_supported(led_supported), led_supported);
6092 if (led_supported == TPACPI_LED_NONE)
6095 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6098 pr_err("Out of memory for LED data\n");
6102 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6103 tpacpi_leds[i].led = -1;
6105 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
6106 rc = tpacpi_init_led(i);
6114 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6115 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6120 #define str_led_status(s) \
6121 ((s) == TPACPI_LED_OFF ? "off" : \
6122 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6124 static int led_read(struct seq_file *m)
6126 if (!led_supported) {
6127 seq_printf(m, "status:\t\tnot supported\n");
6130 seq_printf(m, "status:\t\tsupported\n");
6132 if (led_supported == TPACPI_LED_570) {
6135 for (i = 0; i < 8; i++) {
6136 status = led_get_status(i);
6139 seq_printf(m, "%d:\t\t%s\n",
6140 i, str_led_status(status));
6144 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6149 static int led_write(char *buf)
6153 enum led_status_t s;
6158 while ((cmd = strsep(&buf, ","))) {
6159 if (sscanf(cmd, "%d", &led) != 1)
6162 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6165 if (tpacpi_leds[led].led < 0)
6168 if (strstr(cmd, "off")) {
6170 } else if (strstr(cmd, "on")) {
6172 } else if (strstr(cmd, "blink")) {
6173 s = TPACPI_LED_BLINK;
6178 rc = led_set_status(led, s);
6186 static struct ibm_struct led_driver_data = {
6193 /*************************************************************************
6197 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6199 #define TPACPI_BEEP_Q1 0x0001
6201 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6202 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6203 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6206 static int __init beep_init(struct ibm_init_struct *iibm)
6208 unsigned long quirks;
6210 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6212 TPACPI_ACPIHANDLE_INIT(beep);
6214 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6215 str_supported(beep_handle != NULL));
6217 quirks = tpacpi_check_quirks(beep_quirk_table,
6218 ARRAY_SIZE(beep_quirk_table));
6220 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6222 return (beep_handle) ? 0 : 1;
6225 static int beep_read(struct seq_file *m)
6228 seq_printf(m, "status:\t\tnot supported\n");
6230 seq_printf(m, "status:\t\tsupported\n");
6231 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6237 static int beep_write(char *buf)
6245 while ((cmd = strsep(&buf, ","))) {
6246 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6247 beep_cmd >= 0 && beep_cmd <= 17) {
6251 if (tp_features.beep_needs_two_args) {
6252 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6256 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6265 static struct ibm_struct beep_driver_data = {
6268 .write = beep_write,
6271 /*************************************************************************
6275 enum thermal_access_mode {
6276 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6277 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6278 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6279 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6280 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6283 enum { /* TPACPI_THERMAL_TPEC_* */
6284 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6285 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6286 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6287 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6289 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6293 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6294 struct ibm_thermal_sensors_struct {
6295 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6298 static enum thermal_access_mode thermal_read_mode;
6299 static const struct attribute_group *thermal_attr_group;
6300 static bool thermal_use_labels;
6302 /* idx is zero-based */
6303 static int thermal_get_sensor(int idx, s32 *value)
6309 t = TP_EC_THERMAL_TMP0;
6311 switch (thermal_read_mode) {
6312 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6313 case TPACPI_THERMAL_TPEC_16:
6314 if (idx >= 8 && idx <= 15) {
6315 t = TP_EC_THERMAL_TMP8;
6320 case TPACPI_THERMAL_TPEC_8:
6322 if (!acpi_ec_read(t + idx, &tmp))
6324 *value = tmp * 1000;
6329 case TPACPI_THERMAL_ACPI_UPDT:
6331 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6332 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6334 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6336 *value = (t - 2732) * 100;
6341 case TPACPI_THERMAL_ACPI_TMP07:
6343 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6344 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6346 if (t > 127 || t < -127)
6347 t = TP_EC_THERMAL_TMP_NA;
6353 case TPACPI_THERMAL_NONE:
6361 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6372 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6375 for (i = 0 ; i < n; i++) {
6376 res = thermal_get_sensor(i, &s->temp[i]);
6384 static void thermal_dump_all_sensors(void)
6387 struct ibm_thermal_sensors_struct t;
6389 n = thermal_get_sensors(&t);
6393 pr_notice("temperatures (Celsius):");
6395 for (i = 0; i < n; i++) {
6396 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6397 pr_cont(" %d", (int)(t.temp[i] / 1000));
6405 /* sysfs temp##_input -------------------------------------------------- */
6407 static ssize_t thermal_temp_input_show(struct device *dev,
6408 struct device_attribute *attr,
6411 struct sensor_device_attribute *sensor_attr =
6412 to_sensor_dev_attr(attr);
6413 int idx = sensor_attr->index;
6417 res = thermal_get_sensor(idx, &value);
6420 if (value == TPACPI_THERMAL_SENSOR_NA)
6423 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6426 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6427 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6428 thermal_temp_input_show, NULL, _idxB)
6430 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6431 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6432 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6433 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6434 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6435 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6436 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6437 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6438 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6439 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6440 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6441 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6442 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6443 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6444 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6445 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6446 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6449 #define THERMAL_ATTRS(X) \
6450 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6452 static struct attribute *thermal_temp_input_attr[] = {
6472 static const struct attribute_group thermal_temp_input16_group = {
6473 .attrs = thermal_temp_input_attr
6476 static const struct attribute_group thermal_temp_input8_group = {
6477 .attrs = &thermal_temp_input_attr[8]
6480 #undef THERMAL_SENSOR_ATTR_TEMP
6481 #undef THERMAL_ATTRS
6483 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6485 return sysfs_emit(buf, "CPU\n");
6487 static DEVICE_ATTR_RO(temp1_label);
6489 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6491 return sysfs_emit(buf, "GPU\n");
6493 static DEVICE_ATTR_RO(temp2_label);
6495 static struct attribute *temp_label_attributes[] = {
6496 &dev_attr_temp1_label.attr,
6497 &dev_attr_temp2_label.attr,
6501 static const struct attribute_group temp_label_attr_group = {
6502 .attrs = temp_label_attributes,
6505 /* --------------------------------------------------------------------- */
6507 static int __init thermal_init(struct ibm_init_struct *iibm)
6509 u8 t, ta1, ta2, ver = 0;
6514 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6516 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6518 if (thinkpad_id.ec_model) {
6520 * Direct EC access mode: sensors at registers
6521 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6522 * non-implemented, thermal sensors return 0x80 when
6524 * The above rule is unfortunately flawed. This has been seen with
6525 * 0xC2 (power supply ID) causing thermal control problems.
6526 * The EC version can be determined by offset 0xEF and at least for
6527 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6528 * are not thermal registers.
6530 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6531 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6534 for (i = 0; i < 8; i++) {
6535 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6542 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6551 /* This is sheer paranoia, but we handle it anyway */
6553 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6554 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6556 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6557 thermal_read_mode = TPACPI_THERMAL_NONE;
6561 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6562 thermal_use_labels = true;
6566 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6569 } else if (acpi_tmp7) {
6570 if (tpacpi_is_ibm() &&
6571 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6572 /* 600e/x, 770e, 770x */
6573 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6575 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6576 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6579 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6580 thermal_read_mode = TPACPI_THERMAL_NONE;
6583 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6584 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6587 switch (thermal_read_mode) {
6588 case TPACPI_THERMAL_TPEC_16:
6589 thermal_attr_group = &thermal_temp_input16_group;
6591 case TPACPI_THERMAL_TPEC_8:
6592 case TPACPI_THERMAL_ACPI_TMP07:
6593 case TPACPI_THERMAL_ACPI_UPDT:
6594 thermal_attr_group = &thermal_temp_input8_group;
6596 case TPACPI_THERMAL_NONE:
6601 res = sysfs_create_group(&tpacpi_hwmon->kobj, thermal_attr_group);
6605 if (thermal_use_labels) {
6606 res = sysfs_create_group(&tpacpi_hwmon->kobj, &temp_label_attr_group);
6608 sysfs_remove_group(&tpacpi_hwmon->kobj, thermal_attr_group);
6616 static void thermal_exit(void)
6618 if (thermal_attr_group)
6619 sysfs_remove_group(&tpacpi_hwmon->kobj, thermal_attr_group);
6621 if (thermal_use_labels)
6622 sysfs_remove_group(&tpacpi_hwmon->kobj, &temp_label_attr_group);
6625 static int thermal_read(struct seq_file *m)
6628 struct ibm_thermal_sensors_struct t;
6630 n = thermal_get_sensors(&t);
6631 if (unlikely(n < 0))
6634 seq_printf(m, "temperatures:\t");
6637 for (i = 0; i < (n - 1); i++)
6638 seq_printf(m, "%d ", t.temp[i] / 1000);
6639 seq_printf(m, "%d\n", t.temp[i] / 1000);
6641 seq_printf(m, "not supported\n");
6646 static struct ibm_struct thermal_driver_data = {
6648 .read = thermal_read,
6649 .exit = thermal_exit,
6652 /*************************************************************************
6653 * Backlight/brightness subdriver
6656 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6659 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6660 * CMOS NVRAM byte 0x5E, bits 0-3.
6662 * EC HBRV (0x31) has the following layout
6663 * Bit 7: unknown function
6664 * Bit 6: unknown function
6665 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6666 * Bit 4: must be set to zero to avoid problems
6667 * Bit 3-0: backlight brightness level
6669 * brightness_get_raw returns status data in the HBRV layout
6671 * WARNING: The X61 has been verified to use HBRV for something else, so
6672 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6673 * testing on the very early *60 Lenovo models...
6677 TP_EC_BACKLIGHT = 0x31,
6679 /* TP_EC_BACKLIGHT bitmasks */
6680 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6681 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6682 TP_EC_BACKLIGHT_MAPSW = 0x20,
6685 enum tpacpi_brightness_access_mode {
6686 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6687 TPACPI_BRGHT_MODE_EC, /* EC control */
6688 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6689 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6690 TPACPI_BRGHT_MODE_MAX
6693 static struct backlight_device *ibm_backlight_device;
6695 static enum tpacpi_brightness_access_mode brightness_mode =
6696 TPACPI_BRGHT_MODE_MAX;
6698 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6700 static struct mutex brightness_mutex;
6702 /* NVRAM brightness access,
6703 * call with brightness_mutex held! */
6704 static unsigned int tpacpi_brightness_nvram_get(void)
6708 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6709 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6710 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6711 lnvram &= bright_maxlvl;
6716 static void tpacpi_brightness_checkpoint_nvram(void)
6721 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6724 vdbg_printk(TPACPI_DBG_BRGHT,
6725 "trying to checkpoint backlight level to NVRAM...\n");
6727 if (mutex_lock_killable(&brightness_mutex) < 0)
6730 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6732 lec &= TP_EC_BACKLIGHT_LVLMSK;
6733 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6735 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6736 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6737 /* NVRAM needs update */
6738 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6739 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6741 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6742 dbg_printk(TPACPI_DBG_BRGHT,
6743 "updated NVRAM backlight level to %u (0x%02x)\n",
6744 (unsigned int) lec, (unsigned int) b_nvram);
6746 vdbg_printk(TPACPI_DBG_BRGHT,
6747 "NVRAM backlight level already is %u (0x%02x)\n",
6748 (unsigned int) lec, (unsigned int) b_nvram);
6751 mutex_unlock(&brightness_mutex);
6755 /* call with brightness_mutex held! */
6756 static int tpacpi_brightness_get_raw(int *status)
6760 switch (brightness_mode) {
6761 case TPACPI_BRGHT_MODE_UCMS_STEP:
6762 *status = tpacpi_brightness_nvram_get();
6764 case TPACPI_BRGHT_MODE_EC:
6765 case TPACPI_BRGHT_MODE_ECNVRAM:
6766 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6775 /* call with brightness_mutex held! */
6776 /* do NOT call with illegal backlight level value */
6777 static int tpacpi_brightness_set_ec(unsigned int value)
6781 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6784 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6785 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6786 (value & TP_EC_BACKLIGHT_LVLMSK))))
6792 /* call with brightness_mutex held! */
6793 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6796 unsigned int current_value, i;
6798 current_value = tpacpi_brightness_nvram_get();
6800 if (value == current_value)
6803 cmos_cmd = (value > current_value) ?
6804 TP_CMOS_BRIGHTNESS_UP :
6805 TP_CMOS_BRIGHTNESS_DOWN;
6806 inc = (value > current_value) ? 1 : -1;
6808 for (i = current_value; i != value; i += inc)
6809 if (issue_thinkpad_cmos_command(cmos_cmd))
6815 /* May return EINTR which can always be mapped to ERESTARTSYS */
6816 static int brightness_set(unsigned int value)
6820 if (value > bright_maxlvl)
6823 vdbg_printk(TPACPI_DBG_BRGHT,
6824 "set backlight level to %d\n", value);
6826 res = mutex_lock_killable(&brightness_mutex);
6830 switch (brightness_mode) {
6831 case TPACPI_BRGHT_MODE_EC:
6832 case TPACPI_BRGHT_MODE_ECNVRAM:
6833 res = tpacpi_brightness_set_ec(value);
6835 case TPACPI_BRGHT_MODE_UCMS_STEP:
6836 res = tpacpi_brightness_set_ucmsstep(value);
6842 mutex_unlock(&brightness_mutex);
6846 /* sysfs backlight class ----------------------------------------------- */
6848 static int brightness_update_status(struct backlight_device *bd)
6850 unsigned int level =
6851 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6852 bd->props.power == FB_BLANK_UNBLANK) ?
6853 bd->props.brightness : 0;
6855 dbg_printk(TPACPI_DBG_BRGHT,
6856 "backlight: attempt to set level to %d\n",
6859 /* it is the backlight class's job (caller) to handle
6860 * EINTR and other errors properly */
6861 return brightness_set(level);
6864 static int brightness_get(struct backlight_device *bd)
6868 res = mutex_lock_killable(&brightness_mutex);
6872 res = tpacpi_brightness_get_raw(&status);
6874 mutex_unlock(&brightness_mutex);
6879 return status & TP_EC_BACKLIGHT_LVLMSK;
6882 static void tpacpi_brightness_notify_change(void)
6884 backlight_force_update(ibm_backlight_device,
6885 BACKLIGHT_UPDATE_HOTKEY);
6888 static const struct backlight_ops ibm_backlight_data = {
6889 .get_brightness = brightness_get,
6890 .update_status = brightness_update_status,
6893 /* --------------------------------------------------------------------- */
6896 * Call _BCL method of video device. On some ThinkPads this will
6897 * switch the firmware to the ACPI brightness control mode.
6900 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6902 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6903 union acpi_object *obj;
6904 struct acpi_device *device, *child;
6907 if (acpi_bus_get_device(handle, &device))
6911 list_for_each_entry(child, &device->children, node) {
6912 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6914 if (ACPI_FAILURE(status)) {
6915 buffer.length = ACPI_ALLOCATE_BUFFER;
6919 obj = (union acpi_object *)buffer.pointer;
6920 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6921 pr_err("Unknown _BCL data, please report this to %s\n",
6925 rc = obj->package.count;
6930 kfree(buffer.pointer);
6936 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6938 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6940 acpi_handle video_device;
6943 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6945 bcl_levels = tpacpi_query_bcl_levels(video_device);
6947 tp_features.bright_acpimode = (bcl_levels > 0);
6949 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6953 * These are only useful for models that have only one possibility
6954 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6957 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6958 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6959 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6961 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6962 /* Models with ATI GPUs known to require ECNVRAM mode */
6963 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6965 /* Models with ATI GPUs that can use ECNVRAM */
6966 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6967 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6968 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6969 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6971 /* Models with Intel Extreme Graphics 2 */
6972 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6973 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6974 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6976 /* Models with Intel GMA900 */
6977 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6978 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6979 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6983 * Returns < 0 for error, otherwise sets tp_features.bright_*
6984 * and bright_maxlvl.
6986 static void __init tpacpi_detect_brightness_capabilities(void)
6990 vdbg_printk(TPACPI_DBG_INIT,
6991 "detecting firmware brightness interface capabilities\n");
6993 /* we could run a quirks check here (same table used by
6994 * brightness_init) if needed */
6997 * We always attempt to detect acpi support, so as to switch
6998 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6999 * going to publish a backlight interface
7001 b = tpacpi_check_std_acpi_brightness_support();
7011 tp_features.bright_unkfw = 1;
7012 bright_maxlvl = b - 1;
7014 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
7017 static int __init brightness_init(struct ibm_init_struct *iibm)
7019 struct backlight_properties props;
7021 unsigned long quirks;
7023 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
7025 mutex_init(&brightness_mutex);
7027 quirks = tpacpi_check_quirks(brightness_quirk_table,
7028 ARRAY_SIZE(brightness_quirk_table));
7030 /* tpacpi_detect_brightness_capabilities() must have run already */
7032 /* if it is unknown, we don't handle it: it wouldn't be safe */
7033 if (tp_features.bright_unkfw)
7036 if (!brightness_enable) {
7037 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7038 "brightness support disabled by module parameter\n");
7042 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
7043 if (brightness_enable > 1) {
7044 pr_info("Standard ACPI backlight interface available, not loading native one\n");
7046 } else if (brightness_enable == 1) {
7047 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
7050 } else if (!tp_features.bright_acpimode) {
7051 pr_notice("ACPI backlight interface not available\n");
7055 pr_notice("ACPI native brightness control enabled\n");
7058 * Check for module parameter bogosity, note that we
7059 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7060 * able to detect "unspecified"
7062 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7065 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7066 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7067 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7068 if (quirks & TPACPI_BRGHT_Q_EC)
7069 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7071 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7073 dbg_printk(TPACPI_DBG_BRGHT,
7074 "driver auto-selected brightness_mode=%d\n",
7079 if (!tpacpi_is_ibm() &&
7080 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7081 brightness_mode == TPACPI_BRGHT_MODE_EC))
7084 if (tpacpi_brightness_get_raw(&b) < 0)
7087 memset(&props, 0, sizeof(struct backlight_properties));
7088 props.type = BACKLIGHT_PLATFORM;
7089 props.max_brightness = bright_maxlvl;
7090 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7091 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7093 &ibm_backlight_data,
7095 if (IS_ERR(ibm_backlight_device)) {
7096 int rc = PTR_ERR(ibm_backlight_device);
7097 ibm_backlight_device = NULL;
7098 pr_err("Could not register backlight device\n");
7101 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7102 "brightness is supported\n");
7104 if (quirks & TPACPI_BRGHT_Q_ASK) {
7105 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7107 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7111 /* Added by mistake in early 2007. Probably useless, but it could
7112 * be working around some unknown firmware problem where the value
7113 * read at startup doesn't match the real hardware state... so leave
7114 * it in place just in case */
7115 backlight_update_status(ibm_backlight_device);
7117 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7118 "brightness: registering brightness hotkeys as change notification\n");
7119 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7120 | TP_ACPI_HKEY_BRGHTUP_MASK
7121 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7125 static void brightness_suspend(void)
7127 tpacpi_brightness_checkpoint_nvram();
7130 static void brightness_shutdown(void)
7132 tpacpi_brightness_checkpoint_nvram();
7135 static void brightness_exit(void)
7137 if (ibm_backlight_device) {
7138 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7139 "calling backlight_device_unregister()\n");
7140 backlight_device_unregister(ibm_backlight_device);
7143 tpacpi_brightness_checkpoint_nvram();
7146 static int brightness_read(struct seq_file *m)
7150 level = brightness_get(NULL);
7152 seq_printf(m, "level:\t\tunreadable\n");
7154 seq_printf(m, "level:\t\t%d\n", level);
7155 seq_printf(m, "commands:\tup, down\n");
7156 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7163 static int brightness_write(char *buf)
7169 level = brightness_get(NULL);
7173 while ((cmd = strsep(&buf, ","))) {
7174 if (strlencmp(cmd, "up") == 0) {
7175 if (level < bright_maxlvl)
7177 } else if (strlencmp(cmd, "down") == 0) {
7180 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7181 level >= 0 && level <= bright_maxlvl) {
7187 tpacpi_disclose_usertask("procfs brightness",
7188 "set level to %d\n", level);
7191 * Now we know what the final level should be, so we try to set it.
7192 * Doing it this way makes the syscall restartable in case of EINTR
7194 rc = brightness_set(level);
7195 if (!rc && ibm_backlight_device)
7196 backlight_force_update(ibm_backlight_device,
7197 BACKLIGHT_UPDATE_SYSFS);
7198 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7201 static struct ibm_struct brightness_driver_data = {
7202 .name = "brightness",
7203 .read = brightness_read,
7204 .write = brightness_write,
7205 .exit = brightness_exit,
7206 .suspend = brightness_suspend,
7207 .shutdown = brightness_shutdown,
7210 /*************************************************************************
7215 * IBM ThinkPads have a simple volume controller with MUTE gating.
7216 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7218 * Since the *61 series (and probably also the later *60 series), Lenovo
7219 * ThinkPads only implement the MUTE gate.
7222 * Bit 6: MUTE (1 mutes sound)
7224 * Other bits should be zero as far as we know.
7226 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7227 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7228 * such as bit 7 which is used to detect repeated presses of MUTE,
7229 * and we leave them unchanged.
7231 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7232 * in response to user input. Unfortunately, this rarely works well.
7233 * The laptop changes the state of its internal MUTE gate and, on some
7234 * models, sends KEY_MUTE, causing any user code that responds to the
7235 * mute button to get confused. The hardware MUTE gate is also
7236 * unnecessary, since user code can handle the mute button without
7237 * kernel or EC help.
7239 * To avoid confusing userspace, we simply disable all EC-based mute
7240 * and volume controls when possible.
7243 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7245 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7246 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7247 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7249 #if SNDRV_CARDS <= 32
7250 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7252 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7254 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7255 static char *alsa_id = "ThinkPadEC";
7256 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7258 struct tpacpi_alsa_data {
7259 struct snd_card *card;
7260 struct snd_ctl_elem_id *ctl_mute_id;
7261 struct snd_ctl_elem_id *ctl_vol_id;
7264 static struct snd_card *alsa_card;
7269 /* TP_EC_AUDIO bits */
7270 TP_EC_AUDIO_MUTESW = 6,
7272 /* TP_EC_AUDIO bitmasks */
7273 TP_EC_AUDIO_LVL_MSK = 0x0F,
7274 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7276 /* Maximum volume */
7277 TP_EC_VOLUME_MAX = 14,
7280 enum tpacpi_volume_access_mode {
7281 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7282 TPACPI_VOL_MODE_EC, /* Pure EC control */
7283 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7284 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7288 enum tpacpi_volume_capabilities {
7289 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7290 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7291 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7295 enum tpacpi_mute_btn_mode {
7296 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7297 /* We don't know what mode 1 is. */
7298 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7299 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7302 static enum tpacpi_volume_access_mode volume_mode =
7303 TPACPI_VOL_MODE_MAX;
7305 static enum tpacpi_volume_capabilities volume_capabilities;
7306 static bool volume_control_allowed;
7307 static bool software_mute_requested = true;
7308 static bool software_mute_active;
7309 static int software_mute_orig_mode;
7312 * Used to syncronize writers to TP_EC_AUDIO and
7313 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7315 static struct mutex volume_mutex;
7317 static void tpacpi_volume_checkpoint_nvram(void)
7323 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7325 if (!volume_control_allowed)
7327 if (software_mute_active)
7330 vdbg_printk(TPACPI_DBG_MIXER,
7331 "trying to checkpoint mixer state to NVRAM...\n");
7333 if (tp_features.mixer_no_level_control)
7334 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7336 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7338 if (mutex_lock_killable(&volume_mutex) < 0)
7341 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7344 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7346 if (lec != (b_nvram & ec_mask)) {
7347 /* NVRAM needs update */
7348 b_nvram &= ~ec_mask;
7350 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7351 dbg_printk(TPACPI_DBG_MIXER,
7352 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7353 (unsigned int) lec, (unsigned int) b_nvram);
7355 vdbg_printk(TPACPI_DBG_MIXER,
7356 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7357 (unsigned int) lec, (unsigned int) b_nvram);
7361 mutex_unlock(&volume_mutex);
7364 static int volume_get_status_ec(u8 *status)
7368 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7373 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7378 static int volume_get_status(u8 *status)
7380 return volume_get_status_ec(status);
7383 static int volume_set_status_ec(const u8 status)
7385 if (!acpi_ec_write(TP_EC_AUDIO, status))
7388 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7391 * On X200s, and possibly on others, it can take a while for
7392 * reads to become correct.
7399 static int volume_set_status(const u8 status)
7401 return volume_set_status_ec(status);
7404 /* returns < 0 on error, 0 on no change, 1 on change */
7405 static int __volume_set_mute_ec(const bool mute)
7410 if (mutex_lock_killable(&volume_mutex) < 0)
7413 rc = volume_get_status_ec(&s);
7417 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7418 s & ~TP_EC_AUDIO_MUTESW_MSK;
7421 rc = volume_set_status_ec(n);
7427 mutex_unlock(&volume_mutex);
7431 static int volume_alsa_set_mute(const bool mute)
7433 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7434 (mute) ? "" : "un");
7435 return __volume_set_mute_ec(mute);
7438 static int volume_set_mute(const bool mute)
7442 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7443 (mute) ? "" : "un");
7445 rc = __volume_set_mute_ec(mute);
7446 return (rc < 0) ? rc : 0;
7449 /* returns < 0 on error, 0 on no change, 1 on change */
7450 static int __volume_set_volume_ec(const u8 vol)
7455 if (vol > TP_EC_VOLUME_MAX)
7458 if (mutex_lock_killable(&volume_mutex) < 0)
7461 rc = volume_get_status_ec(&s);
7465 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7468 rc = volume_set_status_ec(n);
7474 mutex_unlock(&volume_mutex);
7478 static int volume_set_software_mute(bool startup)
7482 if (!tpacpi_is_lenovo())
7486 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7490 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7491 "Initial HAUM setting was %d\n",
7492 software_mute_orig_mode);
7495 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7496 (int)TP_EC_MUTE_BTN_NONE))
7499 if (result != TP_EC_MUTE_BTN_NONE)
7500 pr_warn("Unexpected SAUM result %d\n",
7504 * In software mute mode, the standard codec controls take
7505 * precendence, so we unmute the ThinkPad HW switch at
7506 * startup. Just on case there are SAUM-capable ThinkPads
7507 * with level controls, set max HW volume as well.
7509 if (tp_features.mixer_no_level_control)
7510 result = volume_set_mute(false);
7512 result = volume_set_status(TP_EC_VOLUME_MAX);
7515 pr_warn("Failed to unmute the HW mute switch\n");
7520 static void volume_exit_software_mute(void)
7524 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7525 || r != software_mute_orig_mode)
7526 pr_warn("Failed to restore mute mode\n");
7529 static int volume_alsa_set_volume(const u8 vol)
7531 dbg_printk(TPACPI_DBG_MIXER,
7532 "ALSA: trying to set volume level to %hu\n", vol);
7533 return __volume_set_volume_ec(vol);
7536 static void volume_alsa_notify_change(void)
7538 struct tpacpi_alsa_data *d;
7540 if (alsa_card && alsa_card->private_data) {
7541 d = alsa_card->private_data;
7543 snd_ctl_notify(alsa_card,
7544 SNDRV_CTL_EVENT_MASK_VALUE,
7547 snd_ctl_notify(alsa_card,
7548 SNDRV_CTL_EVENT_MASK_VALUE,
7553 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7554 struct snd_ctl_elem_info *uinfo)
7556 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7558 uinfo->value.integer.min = 0;
7559 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7563 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7564 struct snd_ctl_elem_value *ucontrol)
7569 rc = volume_get_status(&s);
7573 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7577 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7578 struct snd_ctl_elem_value *ucontrol)
7580 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7581 ucontrol->value.integer.value[0]);
7582 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7585 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7587 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7588 struct snd_ctl_elem_value *ucontrol)
7593 rc = volume_get_status(&s);
7597 ucontrol->value.integer.value[0] =
7598 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7602 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7603 struct snd_ctl_elem_value *ucontrol)
7605 tpacpi_disclose_usertask("ALSA", "%smute\n",
7606 ucontrol->value.integer.value[0] ?
7608 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7611 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7613 .name = "Console Playback Volume",
7615 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7616 .info = volume_alsa_vol_info,
7617 .get = volume_alsa_vol_get,
7620 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7621 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7622 .name = "Console Playback Switch",
7624 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7625 .info = volume_alsa_mute_info,
7626 .get = volume_alsa_mute_get,
7629 static void volume_suspend(void)
7631 tpacpi_volume_checkpoint_nvram();
7634 static void volume_resume(void)
7636 if (software_mute_active) {
7637 if (volume_set_software_mute(false) < 0)
7638 pr_warn("Failed to restore software mute\n");
7640 volume_alsa_notify_change();
7644 static void volume_shutdown(void)
7646 tpacpi_volume_checkpoint_nvram();
7649 static void volume_exit(void)
7652 snd_card_free(alsa_card);
7656 tpacpi_volume_checkpoint_nvram();
7658 if (software_mute_active)
7659 volume_exit_software_mute();
7662 static int __init volume_create_alsa_mixer(void)
7664 struct snd_card *card;
7665 struct tpacpi_alsa_data *data;
7666 struct snd_kcontrol *ctl_vol;
7667 struct snd_kcontrol *ctl_mute;
7670 rc = snd_card_new(&tpacpi_pdev->dev,
7671 alsa_index, alsa_id, THIS_MODULE,
7672 sizeof(struct tpacpi_alsa_data), &card);
7673 if (rc < 0 || !card) {
7674 pr_err("Failed to create ALSA card structures: %d\n", rc);
7678 BUG_ON(!card->private_data);
7679 data = card->private_data;
7682 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7683 sizeof(card->driver));
7684 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7685 sizeof(card->shortname));
7686 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7687 (thinkpad_id.ec_version_str) ?
7688 thinkpad_id.ec_version_str : "(unknown)");
7689 snprintf(card->longname, sizeof(card->longname),
7690 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7691 (thinkpad_id.ec_version_str) ?
7692 thinkpad_id.ec_version_str : "unknown");
7694 if (volume_control_allowed) {
7695 volume_alsa_control_vol.put = volume_alsa_vol_put;
7696 volume_alsa_control_vol.access =
7697 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7699 volume_alsa_control_mute.put = volume_alsa_mute_put;
7700 volume_alsa_control_mute.access =
7701 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7704 if (!tp_features.mixer_no_level_control) {
7705 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7706 rc = snd_ctl_add(card, ctl_vol);
7708 pr_err("Failed to create ALSA volume control: %d\n",
7712 data->ctl_vol_id = &ctl_vol->id;
7715 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7716 rc = snd_ctl_add(card, ctl_mute);
7718 pr_err("Failed to create ALSA mute control: %d\n", rc);
7721 data->ctl_mute_id = &ctl_mute->id;
7723 rc = snd_card_register(card);
7725 pr_err("Failed to register ALSA card: %d\n", rc);
7733 snd_card_free(card);
7737 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7738 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7740 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7741 /* Whitelist volume level on all IBM by default */
7742 { .vendor = PCI_VENDOR_ID_IBM,
7743 .bios = TPACPI_MATCH_ANY,
7744 .ec = TPACPI_MATCH_ANY,
7745 .quirks = TPACPI_VOL_Q_LEVEL },
7747 /* Lenovo models with volume control (needs confirmation) */
7748 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7749 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7750 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7751 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7752 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7753 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7754 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7756 /* Whitelist mute-only on all Lenovo by default */
7757 { .vendor = PCI_VENDOR_ID_LENOVO,
7758 .bios = TPACPI_MATCH_ANY,
7759 .ec = TPACPI_MATCH_ANY,
7760 .quirks = TPACPI_VOL_Q_MUTEONLY }
7763 static int __init volume_init(struct ibm_init_struct *iibm)
7765 unsigned long quirks;
7768 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7770 mutex_init(&volume_mutex);
7773 * Check for module parameter bogosity, note that we
7774 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7775 * able to detect "unspecified"
7777 if (volume_mode > TPACPI_VOL_MODE_MAX)
7780 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7781 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7786 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7790 * The ALSA mixer is our primary interface.
7791 * When disabled, don't install the subdriver at all
7794 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7795 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7799 quirks = tpacpi_check_quirks(volume_quirk_table,
7800 ARRAY_SIZE(volume_quirk_table));
7802 switch (volume_capabilities) {
7803 case TPACPI_VOL_CAP_AUTO:
7804 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7805 tp_features.mixer_no_level_control = 1;
7806 else if (quirks & TPACPI_VOL_Q_LEVEL)
7807 tp_features.mixer_no_level_control = 0;
7809 return 1; /* no mixer */
7811 case TPACPI_VOL_CAP_VOLMUTE:
7812 tp_features.mixer_no_level_control = 0;
7814 case TPACPI_VOL_CAP_MUTEONLY:
7815 tp_features.mixer_no_level_control = 1;
7821 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7822 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7823 "using user-supplied volume_capabilities=%d\n",
7824 volume_capabilities);
7826 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7827 volume_mode == TPACPI_VOL_MODE_MAX) {
7828 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7830 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7831 "driver auto-selected volume_mode=%d\n",
7834 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7835 "using user-supplied volume_mode=%d\n",
7839 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7840 "mute is supported, volume control is %s\n",
7841 str_supported(!tp_features.mixer_no_level_control));
7843 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7844 software_mute_active = true;
7846 rc = volume_create_alsa_mixer();
7848 pr_err("Could not create the ALSA mixer interface\n");
7852 pr_info("Console audio control enabled, mode: %s\n",
7853 (volume_control_allowed) ?
7854 "override (read/write)" :
7855 "monitor (read only)");
7858 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7859 "registering volume hotkeys as change notification\n");
7860 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7861 | TP_ACPI_HKEY_VOLUP_MASK
7862 | TP_ACPI_HKEY_VOLDWN_MASK
7863 | TP_ACPI_HKEY_MUTE_MASK);
7868 static int volume_read(struct seq_file *m)
7872 if (volume_get_status(&status) < 0) {
7873 seq_printf(m, "level:\t\tunreadable\n");
7875 if (tp_features.mixer_no_level_control)
7876 seq_printf(m, "level:\t\tunsupported\n");
7878 seq_printf(m, "level:\t\t%d\n",
7879 status & TP_EC_AUDIO_LVL_MSK);
7881 seq_printf(m, "mute:\t\t%s\n",
7882 onoff(status, TP_EC_AUDIO_MUTESW));
7884 if (volume_control_allowed) {
7885 seq_printf(m, "commands:\tunmute, mute\n");
7886 if (!tp_features.mixer_no_level_control) {
7887 seq_printf(m, "commands:\tup, down\n");
7888 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7897 static int volume_write(char *buf)
7900 u8 new_level, new_mute;
7906 * We do allow volume control at driver startup, so that the
7907 * user can set initial state through the volume=... parameter hack.
7909 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7910 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7911 tp_warned.volume_ctrl_forbidden = 1;
7912 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7913 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7918 rc = volume_get_status(&s);
7922 new_level = s & TP_EC_AUDIO_LVL_MSK;
7923 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7925 while ((cmd = strsep(&buf, ","))) {
7926 if (!tp_features.mixer_no_level_control) {
7927 if (strlencmp(cmd, "up") == 0) {
7930 else if (new_level < TP_EC_VOLUME_MAX)
7933 } else if (strlencmp(cmd, "down") == 0) {
7936 else if (new_level > 0)
7939 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7940 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7945 if (strlencmp(cmd, "mute") == 0)
7946 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7947 else if (strlencmp(cmd, "unmute") == 0)
7953 if (tp_features.mixer_no_level_control) {
7954 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7955 new_mute ? "" : "un");
7956 rc = volume_set_mute(!!new_mute);
7958 tpacpi_disclose_usertask("procfs volume",
7959 "%smute and set level to %d\n",
7960 new_mute ? "" : "un", new_level);
7961 rc = volume_set_status(new_mute | new_level);
7963 volume_alsa_notify_change();
7965 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7968 static struct ibm_struct volume_driver_data = {
7970 .read = volume_read,
7971 .write = volume_write,
7972 .exit = volume_exit,
7973 .suspend = volume_suspend,
7974 .resume = volume_resume,
7975 .shutdown = volume_shutdown,
7978 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7980 #define alsa_card NULL
7982 static inline void volume_alsa_notify_change(void)
7986 static int __init volume_init(struct ibm_init_struct *iibm)
7988 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7993 static struct ibm_struct volume_driver_data = {
7997 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7999 /*************************************************************************
8006 * TPACPI_FAN_RD_ACPI_GFAN:
8007 * ACPI GFAN method: returns fan level
8009 * see TPACPI_FAN_WR_ACPI_SFAN
8010 * EC 0x2f (HFSP) not available if GFAN exists
8012 * TPACPI_FAN_WR_ACPI_SFAN:
8013 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
8015 * EC 0x2f (HFSP) might be available *for reading*, but do not use
8018 * TPACPI_FAN_WR_TPEC:
8019 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
8020 * Supported on almost all ThinkPads
8022 * Fan speed changes of any sort (including those caused by the
8023 * disengaged mode) are usually done slowly by the firmware as the
8024 * maximum amount of fan duty cycle change per second seems to be
8027 * Reading is not available if GFAN exists.
8028 * Writing is not available if SFAN exists.
8031 * 7 automatic mode engaged;
8032 * (default operation mode of the ThinkPad)
8033 * fan level is ignored in this mode.
8034 * 6 full speed mode (takes precedence over bit 7);
8035 * not available on all thinkpads. May disable
8036 * the tachometer while the fan controller ramps up
8037 * the speed (which can take up to a few *minutes*).
8038 * Speeds up fan to 100% duty-cycle, which is far above
8039 * the standard RPM levels. It is not impossible that
8040 * it could cause hardware damage.
8041 * 5-3 unused in some models. Extra bits for fan level
8042 * in others, but still useless as all values above
8043 * 7 map to the same speed as level 7 in these models.
8044 * 2-0 fan level (0..7 usually)
8046 * 0x07 = max (set when temperatures critical)
8047 * Some ThinkPads may have other levels, see
8048 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
8050 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
8051 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
8052 * does so, its initial value is meaningless (0x07).
8054 * For firmware bugs, refer to:
8055 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8059 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8060 * Main fan tachometer reading (in RPM)
8062 * This register is present on all ThinkPads with a new-style EC, and
8063 * it is known not to be present on the A21m/e, and T22, as there is
8064 * something else in offset 0x84 according to the ACPI DSDT. Other
8065 * ThinkPads from this same time period (and earlier) probably lack the
8066 * tachometer as well.
8068 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8069 * was never fixed by IBM to report the EC firmware version string
8070 * probably support the tachometer (like the early X models), so
8071 * detecting it is quite hard. We need more data to know for sure.
8073 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8076 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8079 * For firmware bugs, refer to:
8080 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8084 * ThinkPad EC register 0x31 bit 0 (only on select models)
8086 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8087 * show the speed of the main fan. When bit 0 of EC register 0x31
8088 * is one, the tachometer registers show the speed of the auxiliary
8091 * Fan control seems to affect both fans, regardless of the state
8094 * So far, only the firmware for the X60/X61 non-tablet versions
8095 * seem to support this (firmware TP-7M).
8097 * TPACPI_FAN_WR_ACPI_FANS:
8098 * ThinkPad X31, X40, X41. Not available in the X60.
8100 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8101 * high speed. ACPI DSDT seems to map these three speeds to levels
8102 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8103 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8105 * The speeds are stored on handles
8106 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8108 * There are three default speed sets, accessible as handles:
8109 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8111 * ACPI DSDT switches which set is in use depending on various
8114 * TPACPI_FAN_WR_TPEC is also available and should be used to
8115 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8116 * but the ACPI tables just mention level 7.
8119 enum { /* Fan control constants */
8120 fan_status_offset = 0x2f, /* EC register 0x2f */
8121 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8122 * 0x84 must be read before 0x85 */
8123 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8124 bit 0 selects which fan is active */
8126 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8127 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8129 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8132 enum fan_status_access_mode {
8133 TPACPI_FAN_NONE = 0, /* No fan status or control */
8134 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8135 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8138 enum fan_control_access_mode {
8139 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8140 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8141 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8142 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8145 enum fan_control_commands {
8146 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8147 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8148 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8149 * and also watchdog cmd */
8152 static bool fan_control_allowed;
8154 static enum fan_status_access_mode fan_status_access_mode;
8155 static enum fan_control_access_mode fan_control_access_mode;
8156 static enum fan_control_commands fan_control_commands;
8158 static u8 fan_control_initial_status;
8159 static u8 fan_control_desired_level;
8160 static u8 fan_control_resume_level;
8161 static int fan_watchdog_maxinterval;
8163 static struct mutex fan_mutex;
8165 static void fan_watchdog_fire(struct work_struct *ignored);
8166 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8168 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8169 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8170 "\\FSPD", /* 600e/x, 770e, 770x */
8172 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8173 "JFNS", /* 770x-JL */
8177 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8178 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8179 * be in auto mode (0x80).
8181 * This is corrected by any write to HFSP either by the driver, or
8184 * We assume 0x07 really means auto mode while this quirk is active,
8185 * as this is far more likely than the ThinkPad being in level 7,
8186 * which is only used by the firmware during thermal emergencies.
8188 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8189 * TP-70 (T43, R52), which are known to be buggy.
8192 static void fan_quirk1_setup(void)
8194 if (fan_control_initial_status == 0x07) {
8195 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8196 tp_features.fan_ctrl_status_undef = 1;
8200 static void fan_quirk1_handle(u8 *fan_status)
8202 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8203 if (*fan_status != fan_control_initial_status) {
8204 /* something changed the HFSP regisnter since
8205 * driver init time, so it is not undefined
8207 tp_features.fan_ctrl_status_undef = 0;
8209 /* Return most likely status. In fact, it
8210 * might be the only possible status */
8211 *fan_status = TP_EC_FAN_AUTO;
8216 /* Select main fan on X60/X61, NOOP on others */
8217 static bool fan_select_fan1(void)
8219 if (tp_features.second_fan) {
8222 if (ec_read(fan_select_offset, &val) < 0)
8225 if (ec_write(fan_select_offset, val) < 0)
8231 /* Select secondary fan on X60/X61 */
8232 static bool fan_select_fan2(void)
8236 if (!tp_features.second_fan)
8239 if (ec_read(fan_select_offset, &val) < 0)
8242 if (ec_write(fan_select_offset, val) < 0)
8249 * Call with fan_mutex held
8251 static void fan_update_desired_level(u8 status)
8254 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8256 fan_control_desired_level = 7;
8258 fan_control_desired_level = status;
8262 static int fan_get_status(u8 *status)
8267 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8269 switch (fan_status_access_mode) {
8270 case TPACPI_FAN_RD_ACPI_GFAN: {
8271 /* 570, 600e/x, 770e, 770x */
8274 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8278 *status = res & 0x07;
8282 case TPACPI_FAN_RD_TPEC:
8283 /* all except 570, 600e/x, 770e, 770x */
8284 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8287 if (likely(status)) {
8289 fan_quirk1_handle(status);
8301 static int fan_get_status_safe(u8 *status)
8306 if (mutex_lock_killable(&fan_mutex))
8307 return -ERESTARTSYS;
8308 rc = fan_get_status(&s);
8310 fan_update_desired_level(s);
8311 mutex_unlock(&fan_mutex);
8321 static int fan_get_speed(unsigned int *speed)
8325 switch (fan_status_access_mode) {
8326 case TPACPI_FAN_RD_TPEC:
8327 /* all except 570, 600e/x, 770e, 770x */
8328 if (unlikely(!fan_select_fan1()))
8330 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8331 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8335 *speed = (hi << 8) | lo;
8346 static int fan2_get_speed(unsigned int *speed)
8351 switch (fan_status_access_mode) {
8352 case TPACPI_FAN_RD_TPEC:
8353 /* all except 570, 600e/x, 770e, 770x */
8354 if (unlikely(!fan_select_fan2()))
8356 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8357 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8358 fan_select_fan1(); /* play it safe */
8363 *speed = (hi << 8) | lo;
8374 static int fan_set_level(int level)
8376 if (!fan_control_allowed)
8379 switch (fan_control_access_mode) {
8380 case TPACPI_FAN_WR_ACPI_SFAN:
8381 if ((level < 0) || (level > 7))
8384 if (tp_features.second_fan_ctl) {
8385 if (!fan_select_fan2() ||
8386 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8387 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8388 tp_features.second_fan_ctl = 0;
8392 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8396 case TPACPI_FAN_WR_ACPI_FANS:
8397 case TPACPI_FAN_WR_TPEC:
8398 if (!(level & TP_EC_FAN_AUTO) &&
8399 !(level & TP_EC_FAN_FULLSPEED) &&
8400 ((level < 0) || (level > 7)))
8403 /* safety net should the EC not support AUTO
8404 * or FULLSPEED mode bits and just ignore them */
8405 if (level & TP_EC_FAN_FULLSPEED)
8406 level |= 7; /* safety min speed 7 */
8407 else if (level & TP_EC_FAN_AUTO)
8408 level |= 4; /* safety min speed 4 */
8410 if (tp_features.second_fan_ctl) {
8411 if (!fan_select_fan2() ||
8412 !acpi_ec_write(fan_status_offset, level)) {
8413 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8414 tp_features.second_fan_ctl = 0;
8419 if (!acpi_ec_write(fan_status_offset, level))
8422 tp_features.fan_ctrl_status_undef = 0;
8429 vdbg_printk(TPACPI_DBG_FAN,
8430 "fan control: set fan control register to 0x%02x\n", level);
8434 static int fan_set_level_safe(int level)
8438 if (!fan_control_allowed)
8441 if (mutex_lock_killable(&fan_mutex))
8442 return -ERESTARTSYS;
8444 if (level == TPACPI_FAN_LAST_LEVEL)
8445 level = fan_control_desired_level;
8447 rc = fan_set_level(level);
8449 fan_update_desired_level(level);
8451 mutex_unlock(&fan_mutex);
8455 static int fan_set_enable(void)
8460 if (!fan_control_allowed)
8463 if (mutex_lock_killable(&fan_mutex))
8464 return -ERESTARTSYS;
8466 switch (fan_control_access_mode) {
8467 case TPACPI_FAN_WR_ACPI_FANS:
8468 case TPACPI_FAN_WR_TPEC:
8469 rc = fan_get_status(&s);
8473 /* Don't go out of emergency fan mode */
8476 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8479 if (!acpi_ec_write(fan_status_offset, s))
8482 tp_features.fan_ctrl_status_undef = 0;
8487 case TPACPI_FAN_WR_ACPI_SFAN:
8488 rc = fan_get_status(&s);
8494 /* Set fan to at least level 4 */
8497 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8507 mutex_unlock(&fan_mutex);
8510 vdbg_printk(TPACPI_DBG_FAN,
8511 "fan control: set fan control register to 0x%02x\n",
8516 static int fan_set_disable(void)
8520 if (!fan_control_allowed)
8523 if (mutex_lock_killable(&fan_mutex))
8524 return -ERESTARTSYS;
8527 switch (fan_control_access_mode) {
8528 case TPACPI_FAN_WR_ACPI_FANS:
8529 case TPACPI_FAN_WR_TPEC:
8530 if (!acpi_ec_write(fan_status_offset, 0x00))
8533 fan_control_desired_level = 0;
8534 tp_features.fan_ctrl_status_undef = 0;
8538 case TPACPI_FAN_WR_ACPI_SFAN:
8539 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8542 fan_control_desired_level = 0;
8550 vdbg_printk(TPACPI_DBG_FAN,
8551 "fan control: set fan control register to 0\n");
8553 mutex_unlock(&fan_mutex);
8557 static int fan_set_speed(int speed)
8561 if (!fan_control_allowed)
8564 if (mutex_lock_killable(&fan_mutex))
8565 return -ERESTARTSYS;
8568 switch (fan_control_access_mode) {
8569 case TPACPI_FAN_WR_ACPI_FANS:
8570 if (speed >= 0 && speed <= 65535) {
8571 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8572 speed, speed, speed))
8582 mutex_unlock(&fan_mutex);
8586 static void fan_watchdog_reset(void)
8588 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8591 if (fan_watchdog_maxinterval > 0 &&
8592 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8593 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8594 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8596 cancel_delayed_work(&fan_watchdog_task);
8599 static void fan_watchdog_fire(struct work_struct *ignored)
8603 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8606 pr_notice("fan watchdog: enabling fan\n");
8607 rc = fan_set_enable();
8609 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8611 /* reschedule for later */
8612 fan_watchdog_reset();
8617 * SYSFS fan layout: hwmon compatible (device)
8620 * 0: "disengaged" mode
8622 * 2: native EC "auto" mode (recommended, hardware default)
8624 * pwm*: set speed in manual mode, ignored otherwise.
8625 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8628 * fan*_input: tachometer reading, RPM
8631 * SYSFS fan layout: extensions
8633 * fan_watchdog (driver):
8634 * fan watchdog interval in seconds, 0 disables (default), max 120
8637 /* sysfs fan pwm1_enable ----------------------------------------------- */
8638 static ssize_t fan_pwm1_enable_show(struct device *dev,
8639 struct device_attribute *attr,
8645 res = fan_get_status_safe(&status);
8649 if (status & TP_EC_FAN_FULLSPEED) {
8651 } else if (status & TP_EC_FAN_AUTO) {
8656 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8659 static ssize_t fan_pwm1_enable_store(struct device *dev,
8660 struct device_attribute *attr,
8661 const char *buf, size_t count)
8666 if (parse_strtoul(buf, 2, &t))
8669 tpacpi_disclose_usertask("hwmon pwm1_enable",
8670 "set fan mode to %lu\n", t);
8674 level = TP_EC_FAN_FULLSPEED;
8677 level = TPACPI_FAN_LAST_LEVEL;
8680 level = TP_EC_FAN_AUTO;
8683 /* reserved for software-controlled auto mode */
8689 res = fan_set_level_safe(level);
8695 fan_watchdog_reset();
8700 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8701 fan_pwm1_enable_show, fan_pwm1_enable_store);
8703 /* sysfs fan pwm1 ------------------------------------------------------ */
8704 static ssize_t fan_pwm1_show(struct device *dev,
8705 struct device_attribute *attr,
8711 res = fan_get_status_safe(&status);
8716 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8717 status = fan_control_desired_level;
8722 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8725 static ssize_t fan_pwm1_store(struct device *dev,
8726 struct device_attribute *attr,
8727 const char *buf, size_t count)
8731 u8 status, newlevel;
8733 if (parse_strtoul(buf, 255, &s))
8736 tpacpi_disclose_usertask("hwmon pwm1",
8737 "set fan speed to %lu\n", s);
8739 /* scale down from 0-255 to 0-7 */
8740 newlevel = (s >> 5) & 0x07;
8742 if (mutex_lock_killable(&fan_mutex))
8743 return -ERESTARTSYS;
8745 rc = fan_get_status(&status);
8746 if (!rc && (status &
8747 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8748 rc = fan_set_level(newlevel);
8752 fan_update_desired_level(newlevel);
8753 fan_watchdog_reset();
8757 mutex_unlock(&fan_mutex);
8758 return (rc) ? rc : count;
8761 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8763 /* sysfs fan fan1_input ------------------------------------------------ */
8764 static ssize_t fan_fan1_input_show(struct device *dev,
8765 struct device_attribute *attr,
8771 res = fan_get_speed(&speed);
8775 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8778 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8780 /* sysfs fan fan2_input ------------------------------------------------ */
8781 static ssize_t fan_fan2_input_show(struct device *dev,
8782 struct device_attribute *attr,
8788 res = fan2_get_speed(&speed);
8792 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8795 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8797 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8798 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8800 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8803 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8808 if (parse_strtoul(buf, 120, &t))
8811 if (!fan_control_allowed)
8814 fan_watchdog_maxinterval = t;
8815 fan_watchdog_reset();
8817 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8821 static DRIVER_ATTR_RW(fan_watchdog);
8823 /* --------------------------------------------------------------------- */
8824 static struct attribute *fan_attributes[] = {
8825 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8826 &dev_attr_fan1_input.attr,
8827 NULL, /* for fan2_input */
8831 static const struct attribute_group fan_attr_group = {
8832 .attrs = fan_attributes,
8835 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8836 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8837 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8839 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8840 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8841 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8842 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8843 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8844 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8845 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8846 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8847 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8848 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8849 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8850 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8851 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8852 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8853 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8854 TPACPI_Q_LNV3('N', '2', 'V', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (3nd gen) */
8855 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8856 TPACPI_Q_LNV3('N', '3', '2', TPACPI_FAN_2CTL), /* X1 Carbon (9th gen) */
8859 static int __init fan_init(struct ibm_init_struct *iibm)
8862 unsigned long quirks;
8864 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8865 "initializing fan subdriver\n");
8867 mutex_init(&fan_mutex);
8868 fan_status_access_mode = TPACPI_FAN_NONE;
8869 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8870 fan_control_commands = 0;
8871 fan_watchdog_maxinterval = 0;
8872 tp_features.fan_ctrl_status_undef = 0;
8873 tp_features.second_fan = 0;
8874 tp_features.second_fan_ctl = 0;
8875 fan_control_desired_level = 7;
8877 if (tpacpi_is_ibm()) {
8878 TPACPI_ACPIHANDLE_INIT(fans);
8879 TPACPI_ACPIHANDLE_INIT(gfan);
8880 TPACPI_ACPIHANDLE_INIT(sfan);
8883 quirks = tpacpi_check_quirks(fan_quirk_table,
8884 ARRAY_SIZE(fan_quirk_table));
8887 /* 570, 600e/x, 770e, 770x */
8888 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8890 /* all other ThinkPads: note that even old-style
8891 * ThinkPad ECs supports the fan control register */
8892 if (likely(acpi_ec_read(fan_status_offset,
8893 &fan_control_initial_status))) {
8894 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8895 if (quirks & TPACPI_FAN_Q1)
8897 if (quirks & TPACPI_FAN_2FAN) {
8898 tp_features.second_fan = 1;
8899 pr_info("secondary fan support enabled\n");
8901 if (quirks & TPACPI_FAN_2CTL) {
8902 tp_features.second_fan = 1;
8903 tp_features.second_fan_ctl = 1;
8904 pr_info("secondary fan control enabled\n");
8907 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8914 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8915 fan_control_commands |=
8916 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8919 /* gfan without sfan means no fan control */
8920 /* all other models implement TP EC 0x2f control */
8924 fan_control_access_mode =
8925 TPACPI_FAN_WR_ACPI_FANS;
8926 fan_control_commands |=
8927 TPACPI_FAN_CMD_SPEED |
8928 TPACPI_FAN_CMD_LEVEL |
8929 TPACPI_FAN_CMD_ENABLE;
8931 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8932 fan_control_commands |=
8933 TPACPI_FAN_CMD_LEVEL |
8934 TPACPI_FAN_CMD_ENABLE;
8939 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8940 "fan is %s, modes %d, %d\n",
8941 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8942 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8943 fan_status_access_mode, fan_control_access_mode);
8945 /* fan control master switch */
8946 if (!fan_control_allowed) {
8947 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8948 fan_control_commands = 0;
8949 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8950 "fan control features disabled by parameter\n");
8953 /* update fan_control_desired_level */
8954 if (fan_status_access_mode != TPACPI_FAN_NONE)
8955 fan_get_status_safe(NULL);
8957 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8958 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8959 if (tp_features.second_fan) {
8960 /* attach second fan tachometer */
8961 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8962 &dev_attr_fan2_input.attr;
8964 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8969 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8970 &driver_attr_fan_watchdog);
8972 sysfs_remove_group(&tpacpi_hwmon->kobj,
8981 static void fan_exit(void)
8983 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8984 "cancelling any pending fan watchdog tasks\n");
8986 /* FIXME: can we really do this unconditionally? */
8987 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8988 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8989 &driver_attr_fan_watchdog);
8991 cancel_delayed_work(&fan_watchdog_task);
8992 flush_workqueue(tpacpi_wq);
8995 static void fan_suspend(void)
8999 if (!fan_control_allowed)
9002 /* Store fan status in cache */
9003 fan_control_resume_level = 0;
9004 rc = fan_get_status_safe(&fan_control_resume_level);
9006 pr_notice("failed to read fan level for later restore during resume: %d\n",
9009 /* if it is undefined, don't attempt to restore it.
9011 if (tp_features.fan_ctrl_status_undef)
9012 fan_control_resume_level = 0;
9015 static void fan_resume(void)
9017 u8 current_level = 7;
9018 bool do_set = false;
9021 /* DSDT *always* updates status on resume */
9022 tp_features.fan_ctrl_status_undef = 0;
9024 if (!fan_control_allowed ||
9025 !fan_control_resume_level ||
9026 (fan_get_status_safe(¤t_level) < 0))
9029 switch (fan_control_access_mode) {
9030 case TPACPI_FAN_WR_ACPI_SFAN:
9031 /* never decrease fan level */
9032 do_set = (fan_control_resume_level > current_level);
9034 case TPACPI_FAN_WR_ACPI_FANS:
9035 case TPACPI_FAN_WR_TPEC:
9036 /* never decrease fan level, scale is:
9037 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
9039 * We expect the firmware to set either 7 or AUTO, but we
9040 * handle FULLSPEED out of paranoia.
9042 * So, we can safely only restore FULLSPEED or 7, anything
9043 * else could slow the fan. Restoring AUTO is useless, at
9044 * best that's exactly what the DSDT already set (it is the
9047 * Always keep in mind that the DSDT *will* have set the
9048 * fans to what the vendor supposes is the best level. We
9049 * muck with it only to speed the fan up.
9051 if (fan_control_resume_level != 7 &&
9052 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
9055 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
9056 (current_level != fan_control_resume_level);
9062 pr_notice("restoring fan level to 0x%02x\n",
9063 fan_control_resume_level);
9064 rc = fan_set_level_safe(fan_control_resume_level);
9066 pr_notice("failed to restore fan level: %d\n", rc);
9070 static int fan_read(struct seq_file *m)
9074 unsigned int speed = 0;
9076 switch (fan_status_access_mode) {
9077 case TPACPI_FAN_RD_ACPI_GFAN:
9078 /* 570, 600e/x, 770e, 770x */
9079 rc = fan_get_status_safe(&status);
9083 seq_printf(m, "status:\t\t%s\n"
9085 (status != 0) ? "enabled" : "disabled", status);
9088 case TPACPI_FAN_RD_TPEC:
9089 /* all except 570, 600e/x, 770e, 770x */
9090 rc = fan_get_status_safe(&status);
9094 seq_printf(m, "status:\t\t%s\n",
9095 (status != 0) ? "enabled" : "disabled");
9097 rc = fan_get_speed(&speed);
9101 seq_printf(m, "speed:\t\t%d\n", speed);
9103 if (status & TP_EC_FAN_FULLSPEED)
9104 /* Disengaged mode takes precedence */
9105 seq_printf(m, "level:\t\tdisengaged\n");
9106 else if (status & TP_EC_FAN_AUTO)
9107 seq_printf(m, "level:\t\tauto\n");
9109 seq_printf(m, "level:\t\t%d\n", status);
9112 case TPACPI_FAN_NONE:
9114 seq_printf(m, "status:\t\tnot supported\n");
9117 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9118 seq_printf(m, "commands:\tlevel <level>");
9120 switch (fan_control_access_mode) {
9121 case TPACPI_FAN_WR_ACPI_SFAN:
9122 seq_printf(m, " (<level> is 0-7)\n");
9126 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9131 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9132 seq_printf(m, "commands:\tenable, disable\n"
9133 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9135 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9136 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9141 static int fan_write_cmd_level(const char *cmd, int *rc)
9145 if (strlencmp(cmd, "level auto") == 0)
9146 level = TP_EC_FAN_AUTO;
9147 else if ((strlencmp(cmd, "level disengaged") == 0) |
9148 (strlencmp(cmd, "level full-speed") == 0))
9149 level = TP_EC_FAN_FULLSPEED;
9150 else if (sscanf(cmd, "level %d", &level) != 1)
9153 *rc = fan_set_level_safe(level);
9155 pr_err("level command accepted for unsupported access mode %d\n",
9156 fan_control_access_mode);
9158 tpacpi_disclose_usertask("procfs fan",
9159 "set level to %d\n", level);
9164 static int fan_write_cmd_enable(const char *cmd, int *rc)
9166 if (strlencmp(cmd, "enable") != 0)
9169 *rc = fan_set_enable();
9171 pr_err("enable command accepted for unsupported access mode %d\n",
9172 fan_control_access_mode);
9174 tpacpi_disclose_usertask("procfs fan", "enable\n");
9179 static int fan_write_cmd_disable(const char *cmd, int *rc)
9181 if (strlencmp(cmd, "disable") != 0)
9184 *rc = fan_set_disable();
9186 pr_err("disable command accepted for unsupported access mode %d\n",
9187 fan_control_access_mode);
9189 tpacpi_disclose_usertask("procfs fan", "disable\n");
9194 static int fan_write_cmd_speed(const char *cmd, int *rc)
9199 * Support speed <low> <medium> <high> ? */
9201 if (sscanf(cmd, "speed %d", &speed) != 1)
9204 *rc = fan_set_speed(speed);
9206 pr_err("speed command accepted for unsupported access mode %d\n",
9207 fan_control_access_mode);
9209 tpacpi_disclose_usertask("procfs fan",
9210 "set speed to %d\n", speed);
9215 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9219 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9222 if (interval < 0 || interval > 120)
9225 fan_watchdog_maxinterval = interval;
9226 tpacpi_disclose_usertask("procfs fan",
9227 "set watchdog timer to %d\n",
9234 static int fan_write(char *buf)
9239 while (!rc && (cmd = strsep(&buf, ","))) {
9240 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9241 fan_write_cmd_level(cmd, &rc)) &&
9242 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9243 (fan_write_cmd_enable(cmd, &rc) ||
9244 fan_write_cmd_disable(cmd, &rc) ||
9245 fan_write_cmd_watchdog(cmd, &rc))) &&
9246 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9247 fan_write_cmd_speed(cmd, &rc))
9251 fan_watchdog_reset();
9257 static struct ibm_struct fan_driver_data = {
9262 .suspend = fan_suspend,
9263 .resume = fan_resume,
9266 /*************************************************************************
9267 * Mute LED subdriver
9270 #define TPACPI_LED_MAX 2
9272 struct tp_led_table {
9279 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9280 [LED_AUDIO_MUTE] = {
9285 [LED_AUDIO_MICMUTE] = {
9292 static int mute_led_on_off(struct tp_led_table *t, bool state)
9297 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9298 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9302 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9303 state ? t->on_value : t->off_value))
9310 static int tpacpi_led_set(int whichled, bool on)
9312 struct tp_led_table *t;
9314 t = &led_tables[whichled];
9315 if (t->state < 0 || t->state == on)
9317 return mute_led_on_off(t, on);
9320 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9321 enum led_brightness brightness)
9323 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9326 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9327 enum led_brightness brightness)
9329 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9332 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9333 [LED_AUDIO_MUTE] = {
9334 .name = "platform::mute",
9335 .max_brightness = 1,
9336 .brightness_set_blocking = tpacpi_led_mute_set,
9337 .default_trigger = "audio-mute",
9339 [LED_AUDIO_MICMUTE] = {
9340 .name = "platform::micmute",
9341 .max_brightness = 1,
9342 .brightness_set_blocking = tpacpi_led_micmute_set,
9343 .default_trigger = "audio-micmute",
9347 static int mute_led_init(struct ibm_init_struct *iibm)
9352 for (i = 0; i < TPACPI_LED_MAX; i++) {
9353 struct tp_led_table *t = &led_tables[i];
9354 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9359 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9360 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9363 led_classdev_unregister(&mute_led_cdev[i]);
9370 static void mute_led_exit(void)
9374 for (i = 0; i < TPACPI_LED_MAX; i++) {
9375 led_classdev_unregister(&mute_led_cdev[i]);
9376 tpacpi_led_set(i, false);
9380 static void mute_led_resume(void)
9384 for (i = 0; i < TPACPI_LED_MAX; i++) {
9385 struct tp_led_table *t = &led_tables[i];
9387 mute_led_on_off(t, t->state);
9391 static struct ibm_struct mute_led_driver_data = {
9393 .exit = mute_led_exit,
9394 .resume = mute_led_resume,
9398 * Battery Wear Control Driver
9399 * Contact: Ognjen Galic <smclt30p@gmail.com>
9404 #define GET_START "BCTG"
9405 #define SET_START "BCCS"
9406 #define GET_STOP "BCSG"
9407 #define SET_STOP "BCSS"
9416 /* Error condition bit */
9417 METHOD_ERR = BIT(31),
9421 /* This is used in the get/set helpers */
9426 struct tpacpi_battery_data {
9433 struct tpacpi_battery_driver_data {
9434 struct tpacpi_battery_data batteries[3];
9435 int individual_addressing;
9438 static struct tpacpi_battery_driver_data battery_info;
9440 /* ACPI helpers/functions/probes */
9443 * This evaluates a ACPI method call specific to the battery
9444 * ACPI extension. The specifics are that an error is marked
9445 * in the 32rd bit of the response, so we just check that here.
9447 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9451 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9452 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9455 if (response & METHOD_ERR) {
9456 acpi_handle_err(hkey_handle,
9457 "%s evaluated but flagged as error", method);
9464 static int tpacpi_battery_get(int what, int battery, int *ret)
9467 case THRESHOLD_START:
9468 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9471 /* The value is in the low 8 bits of the response */
9474 case THRESHOLD_STOP:
9475 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9477 /* Value is in lower 8 bits */
9480 * On the stop value, if we return 0 that
9481 * does not make any sense. 0 means Default, which
9482 * means that charging stops at 100%, so we return
9489 pr_crit("wrong parameter: %d", what);
9494 static int tpacpi_battery_set(int what, int battery, int value)
9497 /* The first 8 bits are the value of the threshold */
9499 /* The battery ID is in bits 8-9, 2 bits */
9500 param |= battery << 8;
9503 case THRESHOLD_START:
9504 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9505 pr_err("failed to set charge threshold on battery %d",
9510 case THRESHOLD_STOP:
9511 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9512 pr_err("failed to set stop threshold: %d", battery);
9517 pr_crit("wrong parameter: %d", what);
9522 static int tpacpi_battery_probe(int battery)
9526 memset(&battery_info.batteries[battery], 0,
9527 sizeof(battery_info.batteries[battery]));
9530 * 1) Get the current start threshold
9531 * 2) Check for support
9532 * 3) Get the current stop threshold
9533 * 4) Check for support
9535 if (acpi_has_method(hkey_handle, GET_START)) {
9536 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9537 pr_err("Error probing battery %d\n", battery);
9540 /* Individual addressing is in bit 9 */
9542 battery_info.individual_addressing = true;
9543 /* Support is marked in bit 8 */
9545 battery_info.batteries[battery].start_support = 1;
9548 if (tpacpi_battery_get(THRESHOLD_START, battery,
9549 &battery_info.batteries[battery].charge_start)) {
9550 pr_err("Error probing battery %d\n", battery);
9554 if (acpi_has_method(hkey_handle, GET_STOP)) {
9555 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9556 pr_err("Error probing battery stop; %d\n", battery);
9559 /* Support is marked in bit 8 */
9561 battery_info.batteries[battery].stop_support = 1;
9564 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9565 &battery_info.batteries[battery].charge_stop)) {
9566 pr_err("Error probing battery stop: %d\n", battery);
9570 pr_info("battery %d registered (start %d, stop %d)",
9572 battery_info.batteries[battery].charge_start,
9573 battery_info.batteries[battery].charge_stop);
9578 /* General helper functions */
9580 static int tpacpi_battery_get_id(const char *battery_name)
9583 if (strcmp(battery_name, "BAT0") == 0 ||
9584 tp_features.battery_force_primary)
9586 if (strcmp(battery_name, "BAT1") == 0)
9587 return BAT_SECONDARY;
9589 * If for some reason the battery is not BAT0 nor is it
9590 * BAT1, we will assume it's the default, first battery,
9593 pr_warn("unknown battery %s, assuming primary", battery_name);
9597 /* sysfs interface */
9599 static ssize_t tpacpi_battery_store(int what,
9601 const char *buf, size_t count)
9603 struct power_supply *supply = to_power_supply(dev);
9604 unsigned long value;
9607 * Some systems have support for more than
9608 * one battery. If that is the case,
9609 * tpacpi_battery_probe marked that addressing
9610 * them individually is supported, so we do that
9611 * based on the device struct.
9613 * On systems that are not supported, we assume
9614 * the primary as most of the ACPI calls fail
9615 * with "Any Battery" as the parameter.
9617 if (battery_info.individual_addressing)
9618 /* BAT_PRIMARY or BAT_SECONDARY */
9619 battery = tpacpi_battery_get_id(supply->desc->name);
9621 battery = BAT_PRIMARY;
9623 rval = kstrtoul(buf, 10, &value);
9628 case THRESHOLD_START:
9629 if (!battery_info.batteries[battery].start_support)
9631 /* valid values are [0, 99] */
9634 if (value > battery_info.batteries[battery].charge_stop)
9636 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9638 battery_info.batteries[battery].charge_start = value;
9641 case THRESHOLD_STOP:
9642 if (!battery_info.batteries[battery].stop_support)
9644 /* valid values are [1, 100] */
9645 if (value < 1 || value > 100)
9647 if (value < battery_info.batteries[battery].charge_start)
9649 battery_info.batteries[battery].charge_stop = value;
9651 * When 100 is passed to stop, we need to flip
9652 * it to 0 as that the EC understands that as
9653 * "Default", which will charge to 100%
9657 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9661 pr_crit("Wrong parameter: %d", what);
9667 static ssize_t tpacpi_battery_show(int what,
9671 struct power_supply *supply = to_power_supply(dev);
9674 * Some systems have support for more than
9675 * one battery. If that is the case,
9676 * tpacpi_battery_probe marked that addressing
9677 * them individually is supported, so we;
9678 * based on the device struct.
9680 * On systems that are not supported, we assume
9681 * the primary as most of the ACPI calls fail
9682 * with "Any Battery" as the parameter.
9684 if (battery_info.individual_addressing)
9685 /* BAT_PRIMARY or BAT_SECONDARY */
9686 battery = tpacpi_battery_get_id(supply->desc->name);
9688 battery = BAT_PRIMARY;
9689 if (tpacpi_battery_get(what, battery, &ret))
9691 return sprintf(buf, "%d\n", ret);
9694 static ssize_t charge_control_start_threshold_show(struct device *device,
9695 struct device_attribute *attr,
9698 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9701 static ssize_t charge_control_end_threshold_show(struct device *device,
9702 struct device_attribute *attr,
9705 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9708 static ssize_t charge_control_start_threshold_store(struct device *dev,
9709 struct device_attribute *attr,
9710 const char *buf, size_t count)
9712 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9715 static ssize_t charge_control_end_threshold_store(struct device *dev,
9716 struct device_attribute *attr,
9717 const char *buf, size_t count)
9719 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9722 static DEVICE_ATTR_RW(charge_control_start_threshold);
9723 static DEVICE_ATTR_RW(charge_control_end_threshold);
9724 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9725 charge_start_threshold,
9727 charge_control_start_threshold_show,
9728 charge_control_start_threshold_store
9730 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9731 charge_stop_threshold,
9733 charge_control_end_threshold_show,
9734 charge_control_end_threshold_store
9737 static struct attribute *tpacpi_battery_attrs[] = {
9738 &dev_attr_charge_control_start_threshold.attr,
9739 &dev_attr_charge_control_end_threshold.attr,
9740 &dev_attr_charge_start_threshold.attr,
9741 &dev_attr_charge_stop_threshold.attr,
9745 ATTRIBUTE_GROUPS(tpacpi_battery);
9747 /* ACPI battery hooking */
9749 static int tpacpi_battery_add(struct power_supply *battery)
9751 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9753 if (tpacpi_battery_probe(batteryid))
9755 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9760 static int tpacpi_battery_remove(struct power_supply *battery)
9762 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9766 static struct acpi_battery_hook battery_hook = {
9767 .add_battery = tpacpi_battery_add,
9768 .remove_battery = tpacpi_battery_remove,
9769 .name = "ThinkPad Battery Extension",
9772 /* Subdriver init/exit */
9774 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9776 * Individual addressing is broken on models that expose the
9777 * primary battery as BAT1.
9779 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9780 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9781 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9782 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9783 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9784 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9787 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9789 memset(&battery_info, 0, sizeof(battery_info));
9791 tp_features.battery_force_primary = tpacpi_check_quirks(
9792 battery_quirk_table,
9793 ARRAY_SIZE(battery_quirk_table));
9795 battery_hook_register(&battery_hook);
9799 static void tpacpi_battery_exit(void)
9801 battery_hook_unregister(&battery_hook);
9804 static struct ibm_struct battery_driver_data = {
9806 .exit = tpacpi_battery_exit,
9809 /*************************************************************************
9810 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9813 static int lcdshadow_state;
9815 static int lcdshadow_on_off(bool state)
9817 acpi_handle set_shadow_handle;
9820 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSSS", &set_shadow_handle))) {
9821 pr_warn("Thinkpad ACPI has no %s interface.\n", "SSSS");
9825 if (!acpi_evalf(set_shadow_handle, &output, NULL, "dd", (int)state))
9828 lcdshadow_state = state;
9832 static int lcdshadow_set(bool on)
9834 if (lcdshadow_state < 0)
9835 return lcdshadow_state;
9836 if (lcdshadow_state == on)
9838 return lcdshadow_on_off(on);
9841 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9843 acpi_handle get_shadow_handle;
9846 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSSS", &get_shadow_handle))) {
9847 lcdshadow_state = -ENODEV;
9851 if (!acpi_evalf(get_shadow_handle, &output, NULL, "dd", 0)) {
9852 lcdshadow_state = -EIO;
9855 if (!(output & 0x10000)) {
9856 lcdshadow_state = -ENODEV;
9859 lcdshadow_state = output & 0x1;
9864 static void lcdshadow_resume(void)
9866 if (lcdshadow_state >= 0)
9867 lcdshadow_on_off(lcdshadow_state);
9870 static int lcdshadow_read(struct seq_file *m)
9872 if (lcdshadow_state < 0) {
9873 seq_puts(m, "status:\t\tnot supported\n");
9875 seq_printf(m, "status:\t\t%d\n", lcdshadow_state);
9876 seq_puts(m, "commands:\t0, 1\n");
9882 static int lcdshadow_write(char *buf)
9885 int res, state = -EINVAL;
9887 if (lcdshadow_state < 0)
9890 while ((cmd = strsep(&buf, ","))) {
9891 res = kstrtoint(cmd, 10, &state);
9896 if (state >= 2 || state < 0)
9899 return lcdshadow_set(state);
9902 static struct ibm_struct lcdshadow_driver_data = {
9903 .name = "lcdshadow",
9904 .resume = lcdshadow_resume,
9905 .read = lcdshadow_read,
9906 .write = lcdshadow_write,
9909 /*************************************************************************
9910 * Thinkpad sensor interfaces
9913 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
9914 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
9915 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
9916 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
9918 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
9919 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
9921 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
9922 #define PALMSENSOR_ON_BIT 1 /* psensor status */
9924 static bool has_palmsensor;
9925 static bool has_lapsensor;
9926 static bool palm_state;
9927 static bool lap_state;
9928 static int dytc_version;
9930 static int dytc_command(int command, int *output)
9932 acpi_handle dytc_handle;
9934 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
9935 /* Platform doesn't support DYTC */
9938 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
9943 static int dytc_get_version(void)
9947 /* Check if we've been called before - and just return cached value */
9949 return dytc_version;
9951 /* Otherwise query DYTC and extract version information */
9952 err = dytc_command(DYTC_CMD_QUERY, &output);
9954 * If support isn't available (ENODEV) then don't return an error
9955 * and don't create the sysfs group
9959 /* For all other errors we can flag the failure */
9963 /* Check DYTC is enabled and supports mode setting */
9964 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
9965 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
9970 static int lapsensor_get(bool *present, bool *state)
9975 err = dytc_command(DYTC_CMD_GET, &output);
9979 *present = true; /*If we get his far, we have lapmode support*/
9980 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
9984 static int palmsensor_get(bool *present, bool *state)
9986 acpi_handle psensor_handle;
9990 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
9992 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
9995 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
9996 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10000 static void lapsensor_refresh(void)
10005 if (has_lapsensor) {
10006 err = lapsensor_get(&has_lapsensor, &state);
10009 if (lap_state != state) {
10011 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10016 static void palmsensor_refresh(void)
10021 if (has_palmsensor) {
10022 err = palmsensor_get(&has_palmsensor, &state);
10025 if (palm_state != state) {
10026 palm_state = state;
10027 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10032 static ssize_t dytc_lapmode_show(struct device *dev,
10033 struct device_attribute *attr,
10037 return sysfs_emit(buf, "%d\n", lap_state);
10038 return sysfs_emit(buf, "\n");
10040 static DEVICE_ATTR_RO(dytc_lapmode);
10042 static ssize_t palmsensor_show(struct device *dev,
10043 struct device_attribute *attr,
10046 if (has_palmsensor)
10047 return sysfs_emit(buf, "%d\n", palm_state);
10048 return sysfs_emit(buf, "\n");
10050 static DEVICE_ATTR_RO(palmsensor);
10052 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10054 int palm_err, lap_err, err;
10056 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10057 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10059 * If support isn't available (ENODEV) for both devices then quit, but
10060 * don't return an error.
10062 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10064 /* Otherwise, if there was an error return it */
10065 if (palm_err && (palm_err != -ENODEV))
10067 if (lap_err && (lap_err != -ENODEV))
10070 if (has_palmsensor) {
10071 err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_palmsensor.attr);
10076 /* Check if we know the DYTC version, if we don't then get it */
10077 if (!dytc_version) {
10078 err = dytc_get_version();
10083 * Platforms before DYTC version 5 claim to have a lap sensor, but it doesn't work, so we
10086 if (has_lapsensor && (dytc_version >= 5)) {
10087 err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_dytc_lapmode.attr);
10094 static void proxsensor_exit(void)
10097 sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_dytc_lapmode.attr);
10098 if (has_palmsensor)
10099 sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_palmsensor.attr);
10102 static struct ibm_struct proxsensor_driver_data = {
10103 .name = "proximity-sensor",
10104 .exit = proxsensor_exit,
10107 /*************************************************************************
10108 * DYTC Platform Profile interface
10111 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10112 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10113 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10115 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10116 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10118 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10119 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10120 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10122 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10123 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10124 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
10126 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
10127 #define DYTC_MODE_LOWPOWER 3 /* Low power mode */
10128 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
10129 #define DYTC_MODE_MMC_BALANCE 0 /* Default mode from MMC_GET, aka balanced */
10131 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10132 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10134 #define DYTC_SET_COMMAND(function, mode, on) \
10135 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10136 (mode) << DYTC_SET_MODE_BIT | \
10137 (on) << DYTC_SET_VALID_BIT)
10139 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
10141 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
10143 static bool dytc_profile_available;
10144 static enum platform_profile_option dytc_current_profile;
10145 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10146 static DEFINE_MUTEX(dytc_mutex);
10147 static bool dytc_mmc_get_available;
10149 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
10151 switch (dytcmode) {
10152 case DYTC_MODE_LOWPOWER:
10153 *profile = PLATFORM_PROFILE_LOW_POWER;
10155 case DYTC_MODE_BALANCE:
10156 case DYTC_MODE_MMC_BALANCE:
10157 *profile = PLATFORM_PROFILE_BALANCED;
10159 case DYTC_MODE_PERFORM:
10160 *profile = PLATFORM_PROFILE_PERFORMANCE;
10162 default: /* Unknown mode */
10168 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10171 case PLATFORM_PROFILE_LOW_POWER:
10172 *perfmode = DYTC_MODE_LOWPOWER;
10174 case PLATFORM_PROFILE_BALANCED:
10175 *perfmode = DYTC_MODE_BALANCE;
10177 case PLATFORM_PROFILE_PERFORMANCE:
10178 *perfmode = DYTC_MODE_PERFORM;
10180 default: /* Unknown profile */
10181 return -EOPNOTSUPP;
10187 * dytc_profile_get: Function to register with platform_profile
10188 * handler. Returns current platform profile.
10190 static int dytc_profile_get(struct platform_profile_handler *pprof,
10191 enum platform_profile_option *profile)
10193 *profile = dytc_current_profile;
10198 * Helper function - check if we are in CQL mode and if we are
10200 * - run the command
10202 * If not in CQL mode, just run the command
10204 static int dytc_cql_command(int command, int *output)
10206 int err, cmd_err, dummy;
10209 /* Determine if we are in CQL mode. This alters the commands we do */
10210 err = dytc_command(DYTC_CMD_GET, output);
10214 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10215 /* Check if we're OK to return immediately */
10216 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10219 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10220 atomic_inc(&dytc_ignore_event);
10221 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10226 cmd_err = dytc_command(command, output);
10227 /* Check return condition after we've restored CQL state */
10229 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10230 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10238 * dytc_profile_set: Function to register with platform_profile
10239 * handler. Sets current platform profile.
10241 static int dytc_profile_set(struct platform_profile_handler *pprof,
10242 enum platform_profile_option profile)
10247 if (!dytc_profile_available)
10250 err = mutex_lock_interruptible(&dytc_mutex);
10254 if (profile == PLATFORM_PROFILE_BALANCED) {
10256 * To get back to balanced mode we need to issue a reset command.
10257 * Note we still need to disable CQL mode before hand and re-enable
10258 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10259 * stuck at 0 for aprox. 30 minutes.
10261 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10267 err = convert_profile_to_dytc(profile, &perfmode);
10271 /* Determine if we are in CQL mode. This alters the commands we do */
10272 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1), &output);
10276 /* Success - update current profile */
10277 dytc_current_profile = profile;
10279 mutex_unlock(&dytc_mutex);
10283 static void dytc_profile_refresh(void)
10285 enum platform_profile_option profile;
10289 mutex_lock(&dytc_mutex);
10290 if (dytc_mmc_get_available)
10291 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10293 err = dytc_cql_command(DYTC_CMD_GET, &output);
10294 mutex_unlock(&dytc_mutex);
10298 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10299 convert_dytc_to_profile(perfmode, &profile);
10300 if (profile != dytc_current_profile) {
10301 dytc_current_profile = profile;
10302 platform_profile_notify();
10306 static struct platform_profile_handler dytc_profile = {
10307 .profile_get = dytc_profile_get,
10308 .profile_set = dytc_profile_set,
10311 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10315 /* Setup supported modes */
10316 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10317 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10318 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10320 dytc_profile_available = false;
10321 err = dytc_command(DYTC_CMD_QUERY, &output);
10323 * If support isn't available (ENODEV) then don't return an error
10324 * and don't create the sysfs group
10326 if (err == -ENODEV)
10328 /* For all other errors we can flag the failure */
10332 /* Check if we know the DYTC version, if we don't then get it */
10333 if (!dytc_version) {
10334 err = dytc_get_version();
10338 /* Check DYTC is enabled and supports mode setting */
10339 if (dytc_version >= 5) {
10340 dbg_printk(TPACPI_DBG_INIT,
10341 "DYTC version %d: thermal mode available\n", dytc_version);
10343 * Check if MMC_GET functionality available
10344 * Version > 6 and return success from MMC_GET command
10346 dytc_mmc_get_available = false;
10347 if (dytc_version >= 6) {
10348 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10349 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10350 dytc_mmc_get_available = true;
10352 /* Create platform_profile structure and register */
10353 err = platform_profile_register(&dytc_profile);
10355 * If for some reason platform_profiles aren't enabled
10356 * don't quit terminally.
10361 dytc_profile_available = true;
10362 /* Ensure initial values are correct */
10363 dytc_profile_refresh();
10368 static void dytc_profile_exit(void)
10370 if (dytc_profile_available) {
10371 dytc_profile_available = false;
10372 platform_profile_remove();
10376 static struct ibm_struct dytc_profile_driver_data = {
10377 .name = "dytc-profile",
10378 .exit = dytc_profile_exit,
10381 /*************************************************************************
10382 * Keyboard language interface
10385 struct keyboard_lang_data {
10386 const char *lang_str;
10390 static const struct keyboard_lang_data keyboard_lang_data[] = {
10412 static int set_keyboard_lang_command(int command)
10414 acpi_handle sskl_handle;
10417 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10418 /* Platform doesn't support SSKL */
10422 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10428 static int get_keyboard_lang(int *output)
10430 acpi_handle gskl_handle;
10433 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10434 /* Platform doesn't support GSKL */
10438 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10442 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10443 * '(' and ')') keys which use layout dependent key-press emulation.
10445 if (kbd_lang & METHOD_ERR)
10448 *output = kbd_lang;
10453 /* sysfs keyboard language entry */
10454 static ssize_t keyboard_lang_show(struct device *dev,
10455 struct device_attribute *attr,
10458 int output, err, i, len = 0;
10460 err = get_keyboard_lang(&output);
10464 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10466 len += sysfs_emit_at(buf, len, "%s", " ");
10468 if (output == keyboard_lang_data[i].lang_code) {
10469 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10471 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10474 len += sysfs_emit_at(buf, len, "\n");
10479 static ssize_t keyboard_lang_store(struct device *dev,
10480 struct device_attribute *attr,
10481 const char *buf, size_t count)
10484 bool lang_found = false;
10487 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10488 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10489 lang_code = keyboard_lang_data[i].lang_code;
10496 lang_code = lang_code | 1 << 24;
10498 /* Set language code */
10499 err = set_keyboard_lang_command(lang_code);
10503 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10507 tpacpi_disclose_usertask(attr->attr.name,
10508 "keyboard language is set to %s\n", buf);
10510 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10514 static DEVICE_ATTR_RW(keyboard_lang);
10516 static struct attribute *kbdlang_attributes[] = {
10517 &dev_attr_keyboard_lang.attr,
10521 static const struct attribute_group kbdlang_attr_group = {
10522 .attrs = kbdlang_attributes,
10525 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10529 err = get_keyboard_lang(&output);
10531 * If support isn't available (ENODEV) then don't return an error
10532 * just don't create the sysfs group.
10534 if (err == -ENODEV)
10540 /* Platform supports this feature - create the sysfs file */
10541 return sysfs_create_group(&tpacpi_pdev->dev.kobj, &kbdlang_attr_group);
10544 static void kbdlang_exit(void)
10546 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &kbdlang_attr_group);
10549 static struct ibm_struct kbdlang_driver_data = {
10551 .exit = kbdlang_exit,
10554 /*************************************************************************
10555 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10556 * and WLAN feature.
10558 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10559 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10560 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10561 static bool has_antennatype;
10562 static int wwan_antennatype;
10564 static int dprc_command(int command, int *output)
10566 acpi_handle dprc_handle;
10568 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10569 /* Platform doesn't support DPRC */
10573 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10577 * METHOD_ERR gets returned on devices where few commands are not supported
10578 * for example command to get WWAN Antenna type command is not supported on
10581 if (*output & METHOD_ERR)
10587 static int get_wwan_antenna(int *wwan_antennatype)
10591 /* Get current Antenna type */
10592 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10596 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10597 *wwan_antennatype = 1;
10598 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10599 *wwan_antennatype = 2;
10606 /* sysfs wwan antenna type entry */
10607 static ssize_t wwan_antenna_type_show(struct device *dev,
10608 struct device_attribute *attr,
10611 switch (wwan_antennatype) {
10613 return sysfs_emit(buf, "type a\n");
10615 return sysfs_emit(buf, "type b\n");
10620 static DEVICE_ATTR_RO(wwan_antenna_type);
10622 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10624 int wwanantenna_err, err;
10626 wwanantenna_err = get_wwan_antenna(&wwan_antennatype);
10628 * If support isn't available (ENODEV) then quit, but don't
10631 if (wwanantenna_err == -ENODEV)
10634 /* if there was an error return it */
10635 if (wwanantenna_err && (wwanantenna_err != -ENODEV))
10636 return wwanantenna_err;
10637 else if (!wwanantenna_err)
10638 has_antennatype = true;
10640 if (has_antennatype) {
10641 err = sysfs_create_file(&tpacpi_pdev->dev.kobj, &dev_attr_wwan_antenna_type.attr);
10648 static void dprc_exit(void)
10650 if (has_antennatype)
10651 sysfs_remove_file(&tpacpi_pdev->dev.kobj, &dev_attr_wwan_antenna_type.attr);
10654 static struct ibm_struct dprc_driver_data = {
10659 /****************************************************************************
10660 ****************************************************************************
10664 ****************************************************************************
10665 ****************************************************************************/
10668 * HKEY event callout for other subdrivers go here
10669 * (yes, it is ugly, but it is quick, safe, and gets the job done
10671 static void tpacpi_driver_event(const unsigned int hkey_event)
10673 if (ibm_backlight_device) {
10674 switch (hkey_event) {
10675 case TP_HKEY_EV_BRGHT_UP:
10676 case TP_HKEY_EV_BRGHT_DOWN:
10677 tpacpi_brightness_notify_change();
10681 switch (hkey_event) {
10682 case TP_HKEY_EV_VOL_UP:
10683 case TP_HKEY_EV_VOL_DOWN:
10684 case TP_HKEY_EV_VOL_MUTE:
10685 volume_alsa_notify_change();
10688 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
10689 enum led_brightness brightness;
10691 mutex_lock(&kbdlight_mutex);
10694 * Check the brightness actually changed, setting the brightness
10695 * through kbdlight_set_level() also triggers this event.
10697 brightness = kbdlight_sysfs_get(NULL);
10698 if (kbdlight_brightness != brightness) {
10699 kbdlight_brightness = brightness;
10700 led_classdev_notify_brightness_hw_changed(
10701 &tpacpi_led_kbdlight.led_classdev, brightness);
10704 mutex_unlock(&kbdlight_mutex);
10707 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
10708 lapsensor_refresh();
10709 /* If we are already accessing DYTC then skip dytc update */
10710 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
10711 dytc_profile_refresh();
10715 static void hotkey_driver_event(const unsigned int scancode)
10717 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
10720 /* --------------------------------------------------------------------- */
10722 /* /proc support */
10723 static struct proc_dir_entry *proc_dir;
10726 * Module and infrastructure proble, init and exit handling
10729 static bool force_load;
10731 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
10732 static const char * __init str_supported(int is_supported)
10734 static char text_unsupported[] __initdata = "not supported";
10736 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
10738 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
10740 static void ibm_exit(struct ibm_struct *ibm)
10742 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
10744 list_del_init(&ibm->all_drivers);
10746 if (ibm->flags.acpi_notify_installed) {
10747 dbg_printk(TPACPI_DBG_EXIT,
10748 "%s: acpi_remove_notify_handler\n", ibm->name);
10749 BUG_ON(!ibm->acpi);
10750 acpi_remove_notify_handler(*ibm->acpi->handle,
10752 dispatch_acpi_notify);
10753 ibm->flags.acpi_notify_installed = 0;
10756 if (ibm->flags.proc_created) {
10757 dbg_printk(TPACPI_DBG_EXIT,
10758 "%s: remove_proc_entry\n", ibm->name);
10759 remove_proc_entry(ibm->name, proc_dir);
10760 ibm->flags.proc_created = 0;
10763 if (ibm->flags.acpi_driver_registered) {
10764 dbg_printk(TPACPI_DBG_EXIT,
10765 "%s: acpi_bus_unregister_driver\n", ibm->name);
10766 BUG_ON(!ibm->acpi);
10767 acpi_bus_unregister_driver(ibm->acpi->driver);
10768 kfree(ibm->acpi->driver);
10769 ibm->acpi->driver = NULL;
10770 ibm->flags.acpi_driver_registered = 0;
10773 if (ibm->flags.init_called && ibm->exit) {
10775 ibm->flags.init_called = 0;
10778 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
10781 static int __init ibm_init(struct ibm_init_struct *iibm)
10784 struct ibm_struct *ibm = iibm->data;
10785 struct proc_dir_entry *entry;
10787 BUG_ON(ibm == NULL);
10789 INIT_LIST_HEAD(&ibm->all_drivers);
10791 if (ibm->flags.experimental && !experimental)
10794 dbg_printk(TPACPI_DBG_INIT,
10795 "probing for %s\n", ibm->name);
10798 ret = iibm->init(iibm);
10800 return 0; /* probe failed */
10804 ibm->flags.init_called = 1;
10808 if (ibm->acpi->hid) {
10809 ret = register_tpacpi_subdriver(ibm);
10814 if (ibm->acpi->notify) {
10815 ret = setup_acpi_notify(ibm);
10816 if (ret == -ENODEV) {
10817 pr_notice("disabling subdriver %s\n",
10827 dbg_printk(TPACPI_DBG_INIT,
10828 "%s installed\n", ibm->name);
10831 umode_t mode = iibm->base_procfs_mode;
10837 entry = proc_create_data(ibm->name, mode, proc_dir,
10838 &dispatch_proc_ops, ibm);
10840 pr_err("unable to create proc entry %s\n", ibm->name);
10844 ibm->flags.proc_created = 1;
10847 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10852 dbg_printk(TPACPI_DBG_INIT,
10853 "%s: at error exit path with result %d\n",
10857 return (ret < 0) ? ret : 0;
10862 static char __init tpacpi_parse_fw_id(const char * const s,
10863 u32 *model, u16 *release)
10867 if (!s || strlen(s) < 8)
10870 for (i = 0; i < 8; i++)
10871 if (!((s[i] >= '0' && s[i] <= '9') ||
10872 (s[i] >= 'A' && s[i] <= 'Z')))
10876 * Most models: xxyTkkWW (#.##c)
10877 * Ancient 570/600 and -SL lacks (#.##c)
10879 if (s[3] == 'T' || s[3] == 'N') {
10880 *model = TPID(s[0], s[1]);
10881 *release = TPVER(s[4], s[5]);
10884 /* New models: xxxyTkkW (#.##c); T550 and some others */
10885 } else if (s[4] == 'T' || s[4] == 'N') {
10886 *model = TPID3(s[0], s[1], s[2]);
10887 *release = TPVER(s[5], s[6]);
10895 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10897 char *ec_fw_string = (char *) private;
10898 const char *dmi_data = (const char *)dm;
10900 * ThinkPad Embedded Controller Program Table on newer models
10902 * Offset | Name | Width | Description
10903 * ----------------------------------------------------
10904 * 0x00 | Type | BYTE | 0x8C
10905 * 0x01 | Length | BYTE |
10906 * 0x02 | Handle | WORD | Varies
10907 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
10908 * 0x0A | OEM struct offset | BYTE | 0x0B
10909 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
10910 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
10911 * 0x0D | ECP version ID | STR ID |
10912 * 0x0E | ECP release date | STR ID |
10915 /* Return if data structure not match */
10916 if (dm->type != 140 || dm->length < 0x0F ||
10917 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10918 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
10919 dmi_data[0x0C] != 0x01)
10922 /* fwstr is the first 8byte string */
10923 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
10926 /* returns 0 - probe ok, or < 0 - probe error.
10927 * Probe ok doesn't mean thinkpad found.
10928 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
10929 static int __must_check __init get_thinkpad_model_data(
10930 struct thinkpad_id_data *tp)
10932 const struct dmi_device *dev = NULL;
10933 char ec_fw_string[18] = {0};
10940 memset(tp, 0, sizeof(*tp));
10942 if (dmi_name_in_vendors("IBM"))
10943 tp->vendor = PCI_VENDOR_ID_IBM;
10944 else if (dmi_name_in_vendors("LENOVO"))
10945 tp->vendor = PCI_VENDOR_ID_LENOVO;
10949 s = dmi_get_system_info(DMI_BIOS_VERSION);
10950 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
10951 if (s && !tp->bios_version_str)
10954 /* Really ancient ThinkPad 240X will fail this, which is fine */
10955 t = tpacpi_parse_fw_id(tp->bios_version_str,
10956 &tp->bios_model, &tp->bios_release);
10957 if (t != 'E' && t != 'C')
10961 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
10962 * X32 or newer, all Z series; Some models must have an
10963 * up-to-date BIOS or they will not be detected.
10965 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
10967 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
10968 if (sscanf(dev->name,
10969 "IBM ThinkPad Embedded Controller -[%17c",
10970 ec_fw_string) == 1) {
10971 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10972 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
10977 /* Newer ThinkPads have different EC program info table */
10978 if (!ec_fw_string[0])
10979 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
10981 if (ec_fw_string[0]) {
10982 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
10983 if (!tp->ec_version_str)
10986 t = tpacpi_parse_fw_id(ec_fw_string,
10987 &tp->ec_model, &tp->ec_release);
10989 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
10991 pr_notice("please report this to %s\n", TPACPI_MAIL);
10995 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
10996 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
10997 tp->model_str = kstrdup(s, GFP_KERNEL);
10998 if (!tp->model_str)
11001 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11002 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11003 tp->model_str = kstrdup(s, GFP_KERNEL);
11004 if (!tp->model_str)
11009 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11010 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11011 if (s && !tp->nummodel_str)
11017 static int __init probe_for_thinkpad(void)
11024 /* It would be dangerous to run the driver in this case */
11025 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11029 * Non-ancient models have better DMI tagging, but very old models
11030 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11032 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11033 (thinkpad_id.ec_model != 0) ||
11034 tpacpi_is_fw_known();
11036 /* The EC handler is required */
11037 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11040 pr_err("Not yet supported ThinkPad detected!\n");
11044 if (!is_thinkpad && !force_load)
11050 static void __init thinkpad_acpi_init_banner(void)
11052 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11053 pr_info("%s\n", TPACPI_URL);
11055 pr_info("ThinkPad BIOS %s, EC %s\n",
11056 (thinkpad_id.bios_version_str) ?
11057 thinkpad_id.bios_version_str : "unknown",
11058 (thinkpad_id.ec_version_str) ?
11059 thinkpad_id.ec_version_str : "unknown");
11061 BUG_ON(!thinkpad_id.vendor);
11063 if (thinkpad_id.model_str)
11064 pr_info("%s %s, model %s\n",
11065 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11066 "IBM" : ((thinkpad_id.vendor ==
11067 PCI_VENDOR_ID_LENOVO) ?
11068 "Lenovo" : "Unknown vendor"),
11069 thinkpad_id.model_str,
11070 (thinkpad_id.nummodel_str) ?
11071 thinkpad_id.nummodel_str : "unknown");
11074 /* Module init, exit, parameters */
11076 static struct ibm_init_struct ibms_init[] __initdata = {
11078 .data = &thinkpad_acpi_driver_data,
11081 .init = hotkey_init,
11082 .data = &hotkey_driver_data,
11085 .init = bluetooth_init,
11086 .data = &bluetooth_driver_data,
11090 .data = &wan_driver_data,
11094 .data = &uwb_driver_data,
11096 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
11098 .init = video_init,
11099 .base_procfs_mode = S_IRUSR,
11100 .data = &video_driver_data,
11104 .init = kbdlight_init,
11105 .data = &kbdlight_driver_data,
11108 .init = light_init,
11109 .data = &light_driver_data,
11113 .data = &cmos_driver_data,
11117 .data = &led_driver_data,
11121 .data = &beep_driver_data,
11124 .init = thermal_init,
11125 .data = &thermal_driver_data,
11128 .init = brightness_init,
11129 .data = &brightness_driver_data,
11132 .init = volume_init,
11133 .data = &volume_driver_data,
11137 .data = &fan_driver_data,
11140 .init = mute_led_init,
11141 .data = &mute_led_driver_data,
11144 .init = tpacpi_battery_init,
11145 .data = &battery_driver_data,
11148 .init = tpacpi_lcdshadow_init,
11149 .data = &lcdshadow_driver_data,
11152 .init = tpacpi_proxsensor_init,
11153 .data = &proxsensor_driver_data,
11156 .init = tpacpi_dytc_profile_init,
11157 .data = &dytc_profile_driver_data,
11160 .init = tpacpi_kbdlang_init,
11161 .data = &kbdlang_driver_data,
11164 .init = tpacpi_dprc_init,
11165 .data = &dprc_driver_data,
11169 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
11172 struct ibm_struct *ibm;
11174 if (!kp || !kp->name || !val)
11177 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11178 ibm = ibms_init[i].data;
11179 WARN_ON(ibm == NULL);
11181 if (!ibm || !ibm->name)
11184 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
11185 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
11187 strcpy(ibms_init[i].param, val);
11195 module_param(experimental, int, 0444);
11196 MODULE_PARM_DESC(experimental,
11197 "Enables experimental features when non-zero");
11199 module_param_named(debug, dbg_level, uint, 0);
11200 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
11202 module_param(force_load, bool, 0444);
11203 MODULE_PARM_DESC(force_load,
11204 "Attempts to load the driver even on a mis-identified ThinkPad when true");
11206 module_param_named(fan_control, fan_control_allowed, bool, 0444);
11207 MODULE_PARM_DESC(fan_control,
11208 "Enables setting fan parameters features when true");
11210 module_param_named(brightness_mode, brightness_mode, uint, 0444);
11211 MODULE_PARM_DESC(brightness_mode,
11212 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
11214 module_param(brightness_enable, uint, 0444);
11215 MODULE_PARM_DESC(brightness_enable,
11216 "Enables backlight control when 1, disables when 0");
11218 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
11219 module_param_named(volume_mode, volume_mode, uint, 0444);
11220 MODULE_PARM_DESC(volume_mode,
11221 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
11223 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11224 MODULE_PARM_DESC(volume_capabilities,
11225 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
11227 module_param_named(volume_control, volume_control_allowed, bool, 0444);
11228 MODULE_PARM_DESC(volume_control,
11229 "Enables software override for the console audio control when true");
11231 module_param_named(software_mute, software_mute_requested, bool, 0444);
11232 MODULE_PARM_DESC(software_mute,
11233 "Request full software mute control");
11235 /* ALSA module API parameters */
11236 module_param_named(index, alsa_index, int, 0444);
11237 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11238 module_param_named(id, alsa_id, charp, 0444);
11239 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11240 module_param_named(enable, alsa_enable, bool, 0444);
11241 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
11242 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
11244 /* The module parameter can't be read back, that's why 0 is used here */
11245 #define TPACPI_PARAM(feature) \
11246 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
11247 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
11249 TPACPI_PARAM(hotkey);
11250 TPACPI_PARAM(bluetooth);
11251 TPACPI_PARAM(video);
11252 TPACPI_PARAM(light);
11253 TPACPI_PARAM(cmos);
11255 TPACPI_PARAM(beep);
11256 TPACPI_PARAM(brightness);
11257 TPACPI_PARAM(volume);
11260 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11261 module_param(dbg_wlswemul, uint, 0444);
11262 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11263 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11264 MODULE_PARM_DESC(wlsw_state,
11265 "Initial state of the emulated WLSW switch");
11267 module_param(dbg_bluetoothemul, uint, 0444);
11268 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11269 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11270 MODULE_PARM_DESC(bluetooth_state,
11271 "Initial state of the emulated bluetooth switch");
11273 module_param(dbg_wwanemul, uint, 0444);
11274 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11275 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11276 MODULE_PARM_DESC(wwan_state,
11277 "Initial state of the emulated WWAN switch");
11279 module_param(dbg_uwbemul, uint, 0444);
11280 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11281 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11282 MODULE_PARM_DESC(uwb_state,
11283 "Initial state of the emulated UWB switch");
11286 static void thinkpad_acpi_module_exit(void)
11288 struct ibm_struct *ibm, *itmp;
11290 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11292 list_for_each_entry_safe_reverse(ibm, itmp,
11293 &tpacpi_all_drivers,
11298 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11300 if (tpacpi_inputdev) {
11301 if (tp_features.input_device_registered)
11302 input_unregister_device(tpacpi_inputdev);
11304 input_free_device(tpacpi_inputdev);
11305 kfree(hotkey_keycode_map);
11309 hwmon_device_unregister(tpacpi_hwmon);
11311 if (tpacpi_sensors_pdev)
11312 platform_device_unregister(tpacpi_sensors_pdev);
11314 platform_device_unregister(tpacpi_pdev);
11316 if (tp_features.sensors_pdrv_attrs_registered)
11317 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
11318 if (tp_features.platform_drv_attrs_registered)
11319 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
11321 if (tp_features.sensors_pdrv_registered)
11322 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11324 if (tp_features.platform_drv_registered)
11325 platform_driver_unregister(&tpacpi_pdriver);
11328 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
11331 destroy_workqueue(tpacpi_wq);
11333 kfree(thinkpad_id.bios_version_str);
11334 kfree(thinkpad_id.ec_version_str);
11335 kfree(thinkpad_id.model_str);
11336 kfree(thinkpad_id.nummodel_str);
11340 static int __init thinkpad_acpi_module_init(void)
11344 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11346 /* Driver-level probe */
11348 ret = get_thinkpad_model_data(&thinkpad_id);
11350 pr_err("unable to get DMI data: %d\n", ret);
11351 thinkpad_acpi_module_exit();
11354 ret = probe_for_thinkpad();
11356 thinkpad_acpi_module_exit();
11360 /* Driver initialization */
11362 thinkpad_acpi_init_banner();
11363 tpacpi_check_outdated_fw();
11365 TPACPI_ACPIHANDLE_INIT(ecrd);
11366 TPACPI_ACPIHANDLE_INIT(ecwr);
11368 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11370 thinkpad_acpi_module_exit();
11374 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
11376 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
11377 thinkpad_acpi_module_exit();
11381 ret = platform_driver_register(&tpacpi_pdriver);
11383 pr_err("unable to register main platform driver\n");
11384 thinkpad_acpi_module_exit();
11387 tp_features.platform_drv_registered = 1;
11389 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11391 pr_err("unable to register hwmon platform driver\n");
11392 thinkpad_acpi_module_exit();
11395 tp_features.sensors_pdrv_registered = 1;
11397 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
11399 tp_features.platform_drv_attrs_registered = 1;
11400 ret = tpacpi_create_driver_attributes(
11401 &tpacpi_hwmon_pdriver.driver);
11404 pr_err("unable to create sysfs driver attributes\n");
11405 thinkpad_acpi_module_exit();
11408 tp_features.sensors_pdrv_attrs_registered = 1;
11411 /* Device initialization */
11412 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
11414 if (IS_ERR(tpacpi_pdev)) {
11415 ret = PTR_ERR(tpacpi_pdev);
11416 tpacpi_pdev = NULL;
11417 pr_err("unable to register platform device\n");
11418 thinkpad_acpi_module_exit();
11421 tpacpi_sensors_pdev = platform_device_register_simple(
11422 TPACPI_HWMON_DRVR_NAME,
11424 if (IS_ERR(tpacpi_sensors_pdev)) {
11425 ret = PTR_ERR(tpacpi_sensors_pdev);
11426 tpacpi_sensors_pdev = NULL;
11427 pr_err("unable to register hwmon platform device\n");
11428 thinkpad_acpi_module_exit();
11431 tp_features.sensors_pdev_attrs_registered = 1;
11432 tpacpi_hwmon = hwmon_device_register_with_groups(
11433 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
11435 if (IS_ERR(tpacpi_hwmon)) {
11436 ret = PTR_ERR(tpacpi_hwmon);
11437 tpacpi_hwmon = NULL;
11438 pr_err("unable to register hwmon device\n");
11439 thinkpad_acpi_module_exit();
11442 mutex_init(&tpacpi_inputdev_send_mutex);
11443 tpacpi_inputdev = input_allocate_device();
11444 if (!tpacpi_inputdev) {
11445 thinkpad_acpi_module_exit();
11448 /* Prepare input device, but don't register */
11449 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
11450 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
11451 tpacpi_inputdev->id.bustype = BUS_HOST;
11452 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
11453 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11454 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
11455 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
11458 /* Init subdriver dependencies */
11459 tpacpi_detect_brightness_capabilities();
11461 /* Init subdrivers */
11462 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11463 ret = ibm_init(&ibms_init[i]);
11464 if (ret >= 0 && *ibms_init[i].param)
11465 ret = ibms_init[i].data->write(ibms_init[i].param);
11467 thinkpad_acpi_module_exit();
11472 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11474 ret = input_register_device(tpacpi_inputdev);
11476 pr_err("unable to register input device\n");
11477 thinkpad_acpi_module_exit();
11480 tp_features.input_device_registered = 1;
11486 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11489 * This will autoload the driver in almost every ThinkPad
11490 * in widespread use.
11492 * Only _VERY_ old models, like the 240, 240x and 570 lack
11493 * the HKEY event interface.
11495 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11498 * DMI matching for module autoloading
11500 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11501 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
11503 * Only models listed in thinkwiki will be supported, so add yours
11504 * if it is not there yet.
11506 #define IBM_BIOS_MODULE_ALIAS(__type) \
11507 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
11509 /* Ancient thinkpad BIOSes have to be identified by
11510 * BIOS type or model number, and there are far less
11511 * BIOS types than model numbers... */
11512 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
11514 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
11515 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
11516 MODULE_DESCRIPTION(TPACPI_DESC);
11517 MODULE_VERSION(TPACPI_VERSION);
11518 MODULE_LICENSE("GPL");
11520 module_init(thinkpad_acpi_module_init);
11521 module_exit(thinkpad_acpi_module_exit);