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>
76 #include <drm/drm_privacy_screen_driver.h>
77 #include "dual_accel_detect.h"
79 /* ThinkPad CMOS commands */
80 #define TP_CMOS_VOLUME_DOWN 0
81 #define TP_CMOS_VOLUME_UP 1
82 #define TP_CMOS_VOLUME_MUTE 2
83 #define TP_CMOS_BRIGHTNESS_UP 4
84 #define TP_CMOS_BRIGHTNESS_DOWN 5
85 #define TP_CMOS_THINKLIGHT_ON 12
86 #define TP_CMOS_THINKLIGHT_OFF 13
90 TP_NVRAM_ADDR_HK2 = 0x57,
91 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
92 TP_NVRAM_ADDR_VIDEO = 0x59,
93 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
94 TP_NVRAM_ADDR_MIXER = 0x60,
99 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
100 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
101 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
102 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
103 TP_NVRAM_MASK_THINKLIGHT = 0x10,
104 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
105 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
106 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
107 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
108 TP_NVRAM_MASK_MUTE = 0x40,
109 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
110 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
111 TP_NVRAM_POS_LEVEL_VOLUME = 0,
114 /* Misc NVRAM-related */
116 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
120 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
121 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
122 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
123 #define TPACPI_ACPI_EC_HID "PNP0C09"
126 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
127 #define TPACPI_HKEY_INPUT_VERSION 0x4101
129 /* ACPI \WGSV commands */
131 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
132 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
133 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
134 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
137 /* TP_ACPI_WGSV_GET_STATE bits */
139 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
140 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
141 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
142 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
143 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
144 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
145 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
146 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
147 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
148 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
152 enum tpacpi_hkey_event_t {
154 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
155 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
156 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
157 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
158 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
159 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
160 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
161 TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
163 /* Reasons for waking up from S3/S4 */
164 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
165 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
166 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
167 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
168 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
169 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
171 /* Auto-sleep after eject request */
172 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
173 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
175 /* Misc bay events */
176 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
177 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
178 or port replicator */
179 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
180 dock or port replicator */
182 * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
183 * when keyboard cover is attached, detached or folded onto the back
185 TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
186 TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
194 * enter/leave tablet mode
196 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
197 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
198 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
200 /* Key-related user-interface events */
201 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
202 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
203 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
206 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
207 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
208 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
209 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
210 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
211 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
212 * command completed. Related to
214 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
215 * changed. Related to AML GMTS */
217 /* AC-related events */
218 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
220 /* Further user-interface events */
221 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
222 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
225 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
228 /****************************************************************************
232 #define TPACPI_NAME "thinkpad"
233 #define TPACPI_DESC "ThinkPad ACPI Extras"
234 #define TPACPI_FILE TPACPI_NAME "_acpi"
235 #define TPACPI_URL "http://ibm-acpi.sf.net/"
236 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
238 #define TPACPI_PROC_DIR "ibm"
239 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
240 #define TPACPI_DRVR_NAME TPACPI_FILE
241 #define TPACPI_DRVR_SHORTNAME "tpacpi"
242 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
244 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
245 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
247 #define TPACPI_MAX_ACPI_ARGS 3
249 /* Debugging printk groups */
250 #define TPACPI_DBG_ALL 0xffff
251 #define TPACPI_DBG_DISCLOSETASK 0x8000
252 #define TPACPI_DBG_INIT 0x0001
253 #define TPACPI_DBG_EXIT 0x0002
254 #define TPACPI_DBG_RFKILL 0x0004
255 #define TPACPI_DBG_HKEY 0x0008
256 #define TPACPI_DBG_FAN 0x0010
257 #define TPACPI_DBG_BRGHT 0x0020
258 #define TPACPI_DBG_MIXER 0x0040
260 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
261 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
262 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
265 /****************************************************************************
266 * Driver-wide structs and misc. variables
271 struct tp_acpi_drv_struct {
272 const struct acpi_device_id *hid;
273 struct acpi_driver *driver;
275 void (*notify) (struct ibm_struct *, u32);
278 struct acpi_device *device;
284 int (*read) (struct seq_file *);
285 int (*write) (char *);
287 void (*resume) (void);
288 void (*suspend) (void);
289 void (*shutdown) (void);
291 struct list_head all_drivers;
293 struct tp_acpi_drv_struct *acpi;
296 u8 acpi_driver_registered:1;
297 u8 acpi_notify_installed:1;
304 struct ibm_init_struct {
307 int (*init) (struct ibm_init_struct *);
308 umode_t base_procfs_mode;
309 struct ibm_struct *data;
318 TP_HOTKEY_TABLET_NONE = 0,
319 TP_HOTKEY_TABLET_USES_MHKG,
320 TP_HOTKEY_TABLET_USES_GMMS,
325 u32 bright_acpimode:1;
329 u32 fan_ctrl_status_undef:1;
331 u32 second_fan_ctl:1;
332 u32 beep_needs_two_args:1;
333 u32 mixer_no_level_control:1;
334 u32 battery_force_primary:1;
335 u32 input_device_registered:1;
336 u32 platform_drv_registered:1;
337 u32 sensors_pdrv_registered:1;
338 u32 hotkey_poll_active:1;
339 u32 has_adaptive_kbd:1;
344 u16 hotkey_mask_ff:1;
345 u16 volume_ctrl_forbidden:1;
348 struct thinkpad_id_data {
349 unsigned int vendor; /* ThinkPad vendor:
350 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
352 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
353 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
355 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
357 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
360 char *model_str; /* ThinkPad T43 */
361 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
363 static struct thinkpad_id_data thinkpad_id;
366 TPACPI_LIFE_INIT = 0,
371 static int experimental;
372 static u32 dbg_level;
374 static struct workqueue_struct *tpacpi_wq;
382 /* tpacpi LED class */
383 struct tpacpi_led_classdev {
384 struct led_classdev led_classdev;
388 /* brightness level capabilities */
389 static unsigned int bright_maxlvl; /* 0 = unknown */
391 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
392 static int dbg_wlswemul;
393 static bool tpacpi_wlsw_emulstate;
394 static int dbg_bluetoothemul;
395 static bool tpacpi_bluetooth_emulstate;
396 static int dbg_wwanemul;
397 static bool tpacpi_wwan_emulstate;
398 static int dbg_uwbemul;
399 static bool tpacpi_uwb_emulstate;
403 /*************************************************************************
407 #define dbg_printk(a_dbg_level, format, arg...) \
409 if (dbg_level & (a_dbg_level)) \
410 printk(KERN_DEBUG pr_fmt("%s: " format), \
414 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
415 #define vdbg_printk dbg_printk
416 static const char *str_supported(int is_supported);
418 static inline const char *str_supported(int is_supported) { return ""; }
419 #define vdbg_printk(a_dbg_level, format, arg...) \
420 do { if (0) no_printk(format, ##arg); } while (0)
423 static void tpacpi_log_usertask(const char * const what)
425 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
426 what, task_tgid_vnr(current));
429 #define tpacpi_disclose_usertask(what, format, arg...) \
431 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
432 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
433 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
434 what, task_tgid_vnr(current), ## arg); \
439 * Quirk handling helpers
441 * ThinkPad IDs and versions seen in the field so far are
442 * two or three characters from the set [0-9A-Z], i.e. base 36.
444 * We use values well outside that range as specials.
447 #define TPACPI_MATCH_ANY 0xffffffffU
448 #define TPACPI_MATCH_ANY_VERSION 0xffffU
449 #define TPACPI_MATCH_UNKNOWN 0U
451 /* TPID('1', 'Y') == 0x3159 */
452 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
453 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
456 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
457 { .vendor = PCI_VENDOR_ID_IBM, \
458 .bios = TPID(__id1, __id2), \
459 .ec = TPACPI_MATCH_ANY, \
460 .quirks = (__quirk) }
462 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_LENOVO, \
464 .bios = TPID(__id1, __id2), \
465 .ec = TPACPI_MATCH_ANY, \
466 .quirks = (__quirk) }
468 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
469 { .vendor = PCI_VENDOR_ID_LENOVO, \
470 .bios = TPID3(__id1, __id2, __id3), \
471 .ec = TPACPI_MATCH_ANY, \
472 .quirks = (__quirk) }
474 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
475 { .vendor = PCI_VENDOR_ID_IBM, \
476 .bios = TPACPI_MATCH_ANY, \
477 .ec = TPID(__id1, __id2), \
478 .quirks = (__quirk) }
480 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
481 { .vendor = PCI_VENDOR_ID_LENOVO, \
482 .bios = TPACPI_MATCH_ANY, \
483 .ec = TPID(__id1, __id2), \
484 .quirks = (__quirk) }
486 struct tpacpi_quirk {
490 unsigned long quirks;
494 * tpacpi_check_quirks() - search BIOS/EC version on a list
495 * @qlist: array of &struct tpacpi_quirk
496 * @qlist_size: number of elements in @qlist
498 * Iterates over a quirks list until one is found that matches the
499 * ThinkPad's vendor, BIOS and EC model.
501 * Returns 0 if nothing matches, otherwise returns the quirks field of
502 * the matching &struct tpacpi_quirk entry.
504 * The match criteria is: vendor, ec and bios much match.
506 static unsigned long __init tpacpi_check_quirks(
507 const struct tpacpi_quirk *qlist,
508 unsigned int qlist_size)
511 if ((qlist->vendor == thinkpad_id.vendor ||
512 qlist->vendor == TPACPI_MATCH_ANY) &&
513 (qlist->bios == thinkpad_id.bios_model ||
514 qlist->bios == TPACPI_MATCH_ANY) &&
515 (qlist->ec == thinkpad_id.ec_model ||
516 qlist->ec == TPACPI_MATCH_ANY))
517 return qlist->quirks;
525 static inline bool __pure __init tpacpi_is_lenovo(void)
527 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
530 static inline bool __pure __init tpacpi_is_ibm(void)
532 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
535 /****************************************************************************
536 ****************************************************************************
538 * ACPI Helpers and device model
540 ****************************************************************************
541 ****************************************************************************/
543 /*************************************************************************
547 static acpi_handle root_handle;
548 static acpi_handle ec_handle;
550 #define TPACPI_HANDLE(object, parent, paths...) \
551 static acpi_handle object##_handle; \
552 static const acpi_handle * const object##_parent __initconst = \
554 static char *object##_paths[] __initdata = { paths }
556 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
557 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
559 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
561 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
562 "\\CMS", /* R40, R40e */
565 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
566 "^HKEY", /* R30, R31 */
567 "HKEY", /* all others */
570 /*************************************************************************
574 static int acpi_evalf(acpi_handle handle,
575 int *res, char *method, char *fmt, ...)
578 struct acpi_object_list params;
579 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
580 struct acpi_buffer result, *resultp;
581 union acpi_object out_obj;
589 pr_err("acpi_evalf() called with empty format\n");
602 params.pointer = &in_objs[0];
609 in_objs[params.count].integer.value = va_arg(ap, int);
610 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
612 /* add more types as needed */
614 pr_err("acpi_evalf() called with invalid format character '%c'\n",
622 if (res_type != 'v') {
623 result.length = sizeof(out_obj);
624 result.pointer = &out_obj;
629 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
633 success = (status == AE_OK &&
634 out_obj.type == ACPI_TYPE_INTEGER);
636 *res = out_obj.integer.value;
639 success = status == AE_OK;
641 /* add more types as needed */
643 pr_err("acpi_evalf() called with invalid format character '%c'\n",
648 if (!success && !quiet)
649 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
650 method, fmt0, acpi_format_exception(status));
655 static int acpi_ec_read(int i, u8 *p)
660 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
664 if (ec_read(i, p) < 0)
671 static int acpi_ec_write(int i, u8 v)
674 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
677 if (ec_write(i, v) < 0)
684 static int issue_thinkpad_cmos_command(int cmos_cmd)
689 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
695 /*************************************************************************
699 #define TPACPI_ACPIHANDLE_INIT(object) \
700 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
701 object##_paths, ARRAY_SIZE(object##_paths))
703 static void __init drv_acpi_handle_init(const char *name,
704 acpi_handle *handle, const acpi_handle parent,
705 char **paths, const int num_paths)
710 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
713 for (i = 0; i < num_paths; i++) {
714 status = acpi_get_handle(parent, paths[i], handle);
715 if (ACPI_SUCCESS(status)) {
716 dbg_printk(TPACPI_DBG_INIT,
717 "Found ACPI handle %s for %s\n",
723 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
728 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
729 u32 level, void *context, void **return_value)
731 if (!strcmp(context, "video")) {
732 struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
734 if (!dev || 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)
791 if (!*ibm->acpi->handle)
794 vdbg_printk(TPACPI_DBG_INIT,
795 "setting up ACPI notify for %s\n", ibm->name);
797 ibm->acpi->device = acpi_fetch_acpi_dev(*ibm->acpi->handle);
798 if (!ibm->acpi->device) {
799 pr_err("acpi_fetch_acpi_dev(%s) failed\n", ibm->name);
803 ibm->acpi->device->driver_data = ibm;
804 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
805 TPACPI_ACPI_EVENT_PREFIX,
808 status = acpi_install_notify_handler(*ibm->acpi->handle,
809 ibm->acpi->type, dispatch_acpi_notify, ibm);
810 if (ACPI_FAILURE(status)) {
811 if (status == AE_ALREADY_EXISTS) {
812 pr_notice("another device driver is already handling %s events\n",
815 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
816 ibm->name, acpi_format_exception(status));
820 ibm->flags.acpi_notify_installed = 1;
824 static int __init tpacpi_device_add(struct acpi_device *device)
829 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
833 dbg_printk(TPACPI_DBG_INIT,
834 "registering %s as an ACPI driver\n", ibm->name);
838 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
839 if (!ibm->acpi->driver) {
840 pr_err("failed to allocate memory for ibm->acpi->driver\n");
844 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
845 ibm->acpi->driver->ids = ibm->acpi->hid;
847 ibm->acpi->driver->ops.add = &tpacpi_device_add;
849 rc = acpi_bus_register_driver(ibm->acpi->driver);
851 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
853 kfree(ibm->acpi->driver);
854 ibm->acpi->driver = NULL;
856 ibm->flags.acpi_driver_registered = 1;
862 /****************************************************************************
863 ****************************************************************************
867 ****************************************************************************
868 ****************************************************************************/
870 static int dispatch_proc_show(struct seq_file *m, void *v)
872 struct ibm_struct *ibm = m->private;
874 if (!ibm || !ibm->read)
879 static int dispatch_proc_open(struct inode *inode, struct file *file)
881 return single_open(file, dispatch_proc_show, pde_data(inode));
884 static ssize_t dispatch_proc_write(struct file *file,
885 const char __user *userbuf,
886 size_t count, loff_t *pos)
888 struct ibm_struct *ibm = pde_data(file_inode(file));
892 if (!ibm || !ibm->write)
894 if (count > PAGE_SIZE - 1)
897 kernbuf = kmalloc(count + 1, GFP_KERNEL);
901 if (copy_from_user(kernbuf, userbuf, count)) {
907 ret = ibm->write(kernbuf);
916 static const struct proc_ops dispatch_proc_ops = {
917 .proc_open = dispatch_proc_open,
918 .proc_read = seq_read,
919 .proc_lseek = seq_lseek,
920 .proc_release = single_release,
921 .proc_write = dispatch_proc_write,
924 /****************************************************************************
925 ****************************************************************************
927 * Device model: input, hwmon and platform
929 ****************************************************************************
930 ****************************************************************************/
932 static struct platform_device *tpacpi_pdev;
933 static struct platform_device *tpacpi_sensors_pdev;
934 static struct device *tpacpi_hwmon;
935 static struct input_dev *tpacpi_inputdev;
936 static struct mutex tpacpi_inputdev_send_mutex;
937 static LIST_HEAD(tpacpi_all_drivers);
939 #ifdef CONFIG_PM_SLEEP
940 static int tpacpi_suspend_handler(struct device *dev)
942 struct ibm_struct *ibm, *itmp;
944 list_for_each_entry_safe(ibm, itmp,
954 static int tpacpi_resume_handler(struct device *dev)
956 struct ibm_struct *ibm, *itmp;
958 list_for_each_entry_safe(ibm, itmp,
969 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
970 tpacpi_suspend_handler, tpacpi_resume_handler);
972 static void tpacpi_shutdown_handler(struct platform_device *pdev)
974 struct ibm_struct *ibm, *itmp;
976 list_for_each_entry_safe(ibm, itmp,
984 /*************************************************************************
985 * sysfs support helpers
988 static int parse_strtoul(const char *buf,
989 unsigned long max, unsigned long *value)
993 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
994 endp = skip_spaces(endp);
995 if (*endp || *value > max)
1001 static void tpacpi_disable_brightness_delay(void)
1003 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1004 pr_notice("ACPI backlight control delay disabled\n");
1007 static void printk_deprecated_attribute(const char * const what,
1008 const char * const details)
1010 tpacpi_log_usertask("deprecated sysfs attribute");
1011 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1015 /*************************************************************************
1016 * rfkill and radio control support helpers
1020 * ThinkPad-ACPI firmware handling model:
1022 * WLSW (master wireless switch) is event-driven, and is common to all
1023 * firmware-controlled radios. It cannot be controlled, just monitored,
1024 * as expected. It overrides all radio state in firmware
1026 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1027 * (TODO: verify how WLSW interacts with the returned radio state).
1029 * The only time there are shadow radio state changes, is when
1030 * masked-off hotkeys are used.
1034 * Internal driver API for radio state:
1036 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1037 * bool: true means radio blocked (off)
1039 enum tpacpi_rfkill_state {
1040 TPACPI_RFK_RADIO_OFF = 0,
1044 /* rfkill switches */
1045 enum tpacpi_rfk_id {
1046 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1047 TPACPI_RFK_WWAN_SW_ID,
1048 TPACPI_RFK_UWB_SW_ID,
1052 static const char *tpacpi_rfkill_names[] = {
1053 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1054 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1055 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1056 [TPACPI_RFK_SW_MAX] = NULL
1059 /* ThinkPad-ACPI rfkill subdriver */
1061 struct rfkill *rfkill;
1062 enum tpacpi_rfk_id id;
1063 const struct tpacpi_rfk_ops *ops;
1066 struct tpacpi_rfk_ops {
1067 /* firmware interface */
1068 int (*get_status)(void);
1069 int (*set_status)(const enum tpacpi_rfkill_state);
1072 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1074 /* Query FW and update rfkill sw state for a given rfkill switch */
1075 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1082 status = (tp_rfk->ops->get_status)();
1086 rfkill_set_sw_state(tp_rfk->rfkill,
1087 (status == TPACPI_RFK_RADIO_OFF));
1093 * Sync the HW-blocking state of all rfkill switches,
1094 * do notice it causes the rfkill core to schedule uevents
1096 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1099 struct tpacpi_rfk *tp_rfk;
1101 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1102 tp_rfk = tpacpi_rfkill_switches[i];
1104 if (rfkill_set_hw_state(tp_rfk->rfkill,
1106 /* ignore -- we track sw block */
1112 /* Call to get the WLSW state from the firmware */
1113 static int hotkey_get_wlsw(void);
1115 /* Call to query WLSW state and update all rfkill switches */
1116 static bool tpacpi_rfk_check_hwblock_state(void)
1118 int res = hotkey_get_wlsw();
1121 /* When unknown or unsupported, we have to assume it is unblocked */
1125 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1126 tpacpi_rfk_update_hwblock_state(hw_blocked);
1131 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1133 struct tpacpi_rfk *tp_rfk = data;
1136 dbg_printk(TPACPI_DBG_RFKILL,
1137 "request to change radio state to %s\n",
1138 blocked ? "blocked" : "unblocked");
1140 /* try to set radio state */
1141 res = (tp_rfk->ops->set_status)(blocked ?
1142 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1144 /* and update the rfkill core with whatever the FW really did */
1145 tpacpi_rfk_update_swstate(tp_rfk);
1147 return (res < 0) ? res : 0;
1150 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1151 .set_block = tpacpi_rfk_hook_set_block,
1154 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1155 const struct tpacpi_rfk_ops *tp_rfkops,
1156 const enum rfkill_type rfktype,
1158 const bool set_default)
1160 struct tpacpi_rfk *atp_rfk;
1162 bool sw_state = false;
1166 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1168 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1170 atp_rfk->rfkill = rfkill_alloc(name,
1173 &tpacpi_rfk_rfkill_ops,
1175 if (!atp_rfk || !atp_rfk->rfkill) {
1176 pr_err("failed to allocate memory for rfkill class\n");
1182 atp_rfk->ops = tp_rfkops;
1184 sw_status = (tp_rfkops->get_status)();
1185 if (sw_status < 0) {
1186 pr_err("failed to read initial state for %s, error %d\n",
1189 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1191 /* try to keep the initial state, since we ask the
1192 * firmware to preserve it across S5 in NVRAM */
1193 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1196 hw_state = tpacpi_rfk_check_hwblock_state();
1197 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1199 res = rfkill_register(atp_rfk->rfkill);
1201 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1202 rfkill_destroy(atp_rfk->rfkill);
1207 tpacpi_rfkill_switches[id] = atp_rfk;
1209 pr_info("rfkill switch %s: radio is %sblocked\n",
1210 name, (sw_state || hw_state) ? "" : "un");
1214 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1216 struct tpacpi_rfk *tp_rfk;
1218 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1220 tp_rfk = tpacpi_rfkill_switches[id];
1222 rfkill_unregister(tp_rfk->rfkill);
1223 rfkill_destroy(tp_rfk->rfkill);
1224 tpacpi_rfkill_switches[id] = NULL;
1229 static void printk_deprecated_rfkill_attribute(const char * const what)
1231 printk_deprecated_attribute(what,
1232 "Please switch to generic rfkill before year 2010");
1235 /* sysfs <radio> enable ------------------------------------------------ */
1236 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1237 struct device_attribute *attr,
1242 printk_deprecated_rfkill_attribute(attr->attr.name);
1244 /* This is in the ABI... */
1245 if (tpacpi_rfk_check_hwblock_state()) {
1246 status = TPACPI_RFK_RADIO_OFF;
1248 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1253 return sysfs_emit(buf, "%d\n",
1254 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1257 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1258 struct device_attribute *attr,
1259 const char *buf, size_t count)
1264 printk_deprecated_rfkill_attribute(attr->attr.name);
1266 if (parse_strtoul(buf, 1, &t))
1269 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1271 /* This is in the ABI... */
1272 if (tpacpi_rfk_check_hwblock_state() && !!t)
1275 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1276 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1277 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1279 return (res < 0) ? res : count;
1282 /* procfs -------------------------------------------------------------- */
1283 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1285 if (id >= TPACPI_RFK_SW_MAX)
1286 seq_printf(m, "status:\t\tnot supported\n");
1290 /* This is in the ABI... */
1291 if (tpacpi_rfk_check_hwblock_state()) {
1292 status = TPACPI_RFK_RADIO_OFF;
1294 status = tpacpi_rfk_update_swstate(
1295 tpacpi_rfkill_switches[id]);
1300 seq_printf(m, "status:\t\t%s\n",
1301 (status == TPACPI_RFK_RADIO_ON) ?
1302 "enabled" : "disabled");
1303 seq_printf(m, "commands:\tenable, disable\n");
1309 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1315 if (id >= TPACPI_RFK_SW_MAX)
1318 while ((cmd = strsep(&buf, ","))) {
1319 if (strlencmp(cmd, "enable") == 0)
1320 status = TPACPI_RFK_RADIO_ON;
1321 else if (strlencmp(cmd, "disable") == 0)
1322 status = TPACPI_RFK_RADIO_OFF;
1328 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1329 (status == TPACPI_RFK_RADIO_ON) ?
1330 "enable" : "disable",
1331 tpacpi_rfkill_names[id]);
1332 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1333 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1339 /*************************************************************************
1340 * thinkpad-acpi driver attributes
1343 /* interface_version --------------------------------------------------- */
1344 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1346 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
1348 static DRIVER_ATTR_RO(interface_version);
1350 /* debug_level --------------------------------------------------------- */
1351 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1353 return sysfs_emit(buf, "0x%04x\n", dbg_level);
1356 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1361 if (parse_strtoul(buf, 0xffff, &t))
1368 static DRIVER_ATTR_RW(debug_level);
1370 /* version ------------------------------------------------------------- */
1371 static ssize_t version_show(struct device_driver *drv, char *buf)
1373 return sysfs_emit(buf, "%s v%s\n",
1374 TPACPI_DESC, TPACPI_VERSION);
1376 static DRIVER_ATTR_RO(version);
1378 /* --------------------------------------------------------------------- */
1380 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1382 /* wlsw_emulstate ------------------------------------------------------ */
1383 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1385 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
1388 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1393 if (parse_strtoul(buf, 1, &t))
1396 if (tpacpi_wlsw_emulstate != !!t) {
1397 tpacpi_wlsw_emulstate = !!t;
1398 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1403 static DRIVER_ATTR_RW(wlsw_emulstate);
1405 /* bluetooth_emulstate ------------------------------------------------- */
1406 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1408 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
1411 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1412 const char *buf, size_t count)
1416 if (parse_strtoul(buf, 1, &t))
1419 tpacpi_bluetooth_emulstate = !!t;
1423 static DRIVER_ATTR_RW(bluetooth_emulstate);
1425 /* wwan_emulstate ------------------------------------------------- */
1426 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1428 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
1431 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1436 if (parse_strtoul(buf, 1, &t))
1439 tpacpi_wwan_emulstate = !!t;
1443 static DRIVER_ATTR_RW(wwan_emulstate);
1445 /* uwb_emulstate ------------------------------------------------- */
1446 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1448 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
1451 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1456 if (parse_strtoul(buf, 1, &t))
1459 tpacpi_uwb_emulstate = !!t;
1463 static DRIVER_ATTR_RW(uwb_emulstate);
1466 /*************************************************************************
1471 * Table of recommended minimum BIOS versions
1473 * Reasons for listing:
1474 * 1. Stable BIOS, listed because the unknown amount of
1475 * bugs and bad ACPI behaviour on older versions
1477 * 2. BIOS or EC fw with known bugs that trigger on Linux
1479 * 3. BIOS with known reduced functionality in older versions
1481 * We recommend the latest BIOS and EC version.
1482 * We only support the latest BIOS and EC fw version as a rule.
1484 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1485 * Information from users in ThinkWiki
1487 * WARNING: we use this table also to detect that the machine is
1488 * a ThinkPad in some cases, so don't remove entries lightly.
1491 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1492 { .vendor = (__v), \
1493 .bios = TPID(__id1, __id2), \
1494 .ec = TPACPI_MATCH_ANY, \
1495 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1496 | TPVER(__bv1, __bv2) }
1498 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1499 __eid, __ev1, __ev2) \
1500 { .vendor = (__v), \
1501 .bios = TPID(__bid1, __bid2), \
1503 .quirks = TPVER(__ev1, __ev2) << 16 \
1504 | TPVER(__bv1, __bv2) }
1506 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1507 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1509 /* Outdated IBM BIOSes often lack the EC id string */
1510 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1511 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1512 __bv1, __bv2, TPID(__id1, __id2), \
1514 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1515 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1518 /* Outdated IBM BIOSes often lack the EC id string */
1519 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1520 __eid1, __eid2, __ev1, __ev2) \
1521 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1522 __bv1, __bv2, TPID(__eid1, __eid2), \
1524 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1525 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1528 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1529 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1531 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1532 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1533 __bv1, __bv2, TPID(__id1, __id2), \
1536 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1537 __eid1, __eid2, __ev1, __ev2) \
1538 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1539 __bv1, __bv2, TPID(__eid1, __eid2), \
1542 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1543 /* Numeric models ------------------ */
1544 /* FW MODEL BIOS VERS */
1545 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1546 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1547 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1548 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1549 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1550 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1551 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1552 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1553 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1555 /* A-series ------------------------- */
1556 /* FW MODEL BIOS VERS EC VERS */
1557 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1558 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1559 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1560 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1561 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1562 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1563 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1564 TPV_QI0('1', '3', '2', '0'), /* A22m */
1565 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1566 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1567 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1569 /* G-series ------------------------- */
1570 /* FW MODEL BIOS VERS */
1571 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1572 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1574 /* R-series, T-series --------------- */
1575 /* FW MODEL BIOS VERS EC VERS */
1576 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1577 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1578 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1579 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1580 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1581 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1582 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1583 T40/p, T41/p, T42/p (1) */
1584 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1585 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1586 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1587 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1589 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1590 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1591 TPV_QI0('1', '6', '3', '2'), /* T22 */
1592 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1593 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1594 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1596 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1597 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1598 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1600 /* BIOS FW BIOS VERS EC FW EC VERS */
1601 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1602 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1604 /* X-series ------------------------- */
1605 /* FW MODEL BIOS VERS EC VERS */
1606 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1607 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1608 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1609 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1610 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1611 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1612 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1614 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1615 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1617 /* (0) - older versions lack DMI EC fw string and functionality */
1618 /* (1) - older versions known to lack functionality */
1629 static void __init tpacpi_check_outdated_fw(void)
1631 unsigned long fwvers;
1632 u16 ec_version, bios_version;
1634 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1635 ARRAY_SIZE(tpacpi_bios_version_qtable));
1640 bios_version = fwvers & 0xffffU;
1641 ec_version = (fwvers >> 16) & 0xffffU;
1643 /* note that unknown versions are set to 0x0000 and we use that */
1644 if ((bios_version > thinkpad_id.bios_release) ||
1645 (ec_version > thinkpad_id.ec_release &&
1646 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1648 * The changelogs would let us track down the exact
1649 * reason, but it is just too much of a pain to track
1650 * it. We only list BIOSes that are either really
1651 * broken, or really stable to begin with, so it is
1652 * best if the user upgrades the firmware anyway.
1654 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1655 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1659 static bool __init tpacpi_is_fw_known(void)
1661 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1662 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1665 /****************************************************************************
1666 ****************************************************************************
1670 ****************************************************************************
1671 ****************************************************************************/
1673 /*************************************************************************
1674 * thinkpad-acpi metadata subdriver
1677 static int thinkpad_acpi_driver_read(struct seq_file *m)
1679 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1680 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1684 static struct ibm_struct thinkpad_acpi_driver_data = {
1686 .read = thinkpad_acpi_driver_read,
1689 /*************************************************************************
1694 * ThinkPad firmware event model
1696 * The ThinkPad firmware has two main event interfaces: normal ACPI
1697 * notifications (which follow the ACPI standard), and a private event
1700 * The private event interface also issues events for the hotkeys. As
1701 * the driver gained features, the event handling code ended up being
1702 * built around the hotkey subdriver. This will need to be refactored
1703 * to a more formal event API eventually.
1705 * Some "hotkeys" are actually supposed to be used as event reports,
1706 * such as "brightness has changed", "volume has changed", depending on
1707 * the ThinkPad model and how the firmware is operating.
1709 * Unlike other classes, hotkey-class events have mask/unmask control on
1710 * non-ancient firmware. However, how it behaves changes a lot with the
1711 * firmware model and version.
1714 enum { /* hot key scan codes (derived from ACPI DSDT) */
1715 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1716 TP_ACPI_HOTKEYSCAN_FNF2,
1717 TP_ACPI_HOTKEYSCAN_FNF3,
1718 TP_ACPI_HOTKEYSCAN_FNF4,
1719 TP_ACPI_HOTKEYSCAN_FNF5,
1720 TP_ACPI_HOTKEYSCAN_FNF6,
1721 TP_ACPI_HOTKEYSCAN_FNF7,
1722 TP_ACPI_HOTKEYSCAN_FNF8,
1723 TP_ACPI_HOTKEYSCAN_FNF9,
1724 TP_ACPI_HOTKEYSCAN_FNF10,
1725 TP_ACPI_HOTKEYSCAN_FNF11,
1726 TP_ACPI_HOTKEYSCAN_FNF12,
1727 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1728 TP_ACPI_HOTKEYSCAN_FNINSERT,
1729 TP_ACPI_HOTKEYSCAN_FNDELETE,
1730 TP_ACPI_HOTKEYSCAN_FNHOME,
1731 TP_ACPI_HOTKEYSCAN_FNEND,
1732 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1733 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1734 TP_ACPI_HOTKEYSCAN_FNSPACE,
1735 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1736 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1737 TP_ACPI_HOTKEYSCAN_MUTE,
1738 TP_ACPI_HOTKEYSCAN_THINKPAD,
1739 TP_ACPI_HOTKEYSCAN_UNK1,
1740 TP_ACPI_HOTKEYSCAN_UNK2,
1741 TP_ACPI_HOTKEYSCAN_UNK3,
1742 TP_ACPI_HOTKEYSCAN_UNK4,
1743 TP_ACPI_HOTKEYSCAN_UNK5,
1744 TP_ACPI_HOTKEYSCAN_UNK6,
1745 TP_ACPI_HOTKEYSCAN_UNK7,
1746 TP_ACPI_HOTKEYSCAN_UNK8,
1748 /* Adaptive keyboard keycodes */
1749 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1750 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1751 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1752 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1753 TP_ACPI_HOTKEYSCAN_CLOUD,
1754 TP_ACPI_HOTKEYSCAN_UNK9,
1755 TP_ACPI_HOTKEYSCAN_VOICE,
1756 TP_ACPI_HOTKEYSCAN_UNK10,
1757 TP_ACPI_HOTKEYSCAN_GESTURES,
1758 TP_ACPI_HOTKEYSCAN_UNK11,
1759 TP_ACPI_HOTKEYSCAN_UNK12,
1760 TP_ACPI_HOTKEYSCAN_UNK13,
1761 TP_ACPI_HOTKEYSCAN_CONFIG,
1762 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1763 TP_ACPI_HOTKEYSCAN_RELOAD,
1764 TP_ACPI_HOTKEYSCAN_BACK,
1765 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1766 TP_ACPI_HOTKEYSCAN_MIC_UP,
1767 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1768 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1769 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1771 /* Lenovo extended keymap, starting at 0x1300 */
1772 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1773 /* first new observed key (star, favorites) is 0x1311 */
1774 TP_ACPI_HOTKEYSCAN_STAR = 69,
1775 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1776 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1777 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1778 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1779 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1780 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1781 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1782 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1784 /* Hotkey keymap size */
1785 TPACPI_HOTKEY_MAP_LEN
1788 enum { /* Keys/events available through NVRAM polling */
1789 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1790 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1793 enum { /* Positions of some of the keys in hotkey masks */
1794 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1795 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1796 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1797 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1798 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1799 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1800 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1801 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1802 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1803 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1804 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1807 enum { /* NVRAM to ACPI HKEY group map */
1808 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1809 TP_ACPI_HKEY_ZOOM_MASK |
1810 TP_ACPI_HKEY_DISPSWTCH_MASK |
1811 TP_ACPI_HKEY_HIBERNATE_MASK,
1812 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1813 TP_ACPI_HKEY_BRGHTDWN_MASK,
1814 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1815 TP_ACPI_HKEY_VOLDWN_MASK |
1816 TP_ACPI_HKEY_MUTE_MASK,
1819 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1820 struct tp_nvram_state {
1821 u16 thinkpad_toggle:1;
1823 u16 display_toggle:1;
1824 u16 thinklight_toggle:1;
1825 u16 hibernate_toggle:1;
1826 u16 displayexp_toggle:1;
1827 u16 display_state:1;
1828 u16 brightness_toggle:1;
1829 u16 volume_toggle:1;
1832 u8 brightness_level;
1836 /* kthread for the hotkey poller */
1837 static struct task_struct *tpacpi_hotkey_task;
1840 * Acquire mutex to write poller control variables as an
1843 * Increment hotkey_config_change when changing them if you
1844 * want the kthread to forget old state.
1846 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1848 static struct mutex hotkey_thread_data_mutex;
1849 static unsigned int hotkey_config_change;
1852 * hotkey poller control variables
1854 * Must be atomic or readers will also need to acquire mutex
1856 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1857 * should be used only when the changes need to be taken as
1858 * a block, OR when one needs to force the kthread to forget
1861 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1862 static unsigned int hotkey_poll_freq = 10; /* Hz */
1864 #define HOTKEY_CONFIG_CRITICAL_START \
1866 mutex_lock(&hotkey_thread_data_mutex); \
1867 hotkey_config_change++; \
1869 #define HOTKEY_CONFIG_CRITICAL_END \
1870 mutex_unlock(&hotkey_thread_data_mutex);
1872 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1874 #define hotkey_source_mask 0U
1875 #define HOTKEY_CONFIG_CRITICAL_START
1876 #define HOTKEY_CONFIG_CRITICAL_END
1878 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1880 static struct mutex hotkey_mutex;
1882 static enum { /* Reasons for waking up */
1883 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1884 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1885 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1886 } hotkey_wakeup_reason;
1888 static int hotkey_autosleep_ack;
1890 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1891 static u32 hotkey_all_mask; /* all events supported in fw */
1892 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
1893 static u32 hotkey_reserved_mask; /* events better left disabled */
1894 static u32 hotkey_driver_mask; /* events needed by the driver */
1895 static u32 hotkey_user_mask; /* events visible to userspace */
1896 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1898 static u16 *hotkey_keycode_map;
1900 static void tpacpi_driver_event(const unsigned int hkey_event);
1901 static void hotkey_driver_event(const unsigned int scancode);
1902 static void hotkey_poll_setup(const bool may_warn);
1904 /* HKEY.MHKG() return bits */
1905 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1907 TP_ACPI_MULTI_MODE_INVALID = 0,
1908 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1909 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1910 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1911 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1912 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1913 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1914 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1918 /* The following modes are considered tablet mode for the purpose of
1919 * reporting the status to userspace. i.e. in all these modes it makes
1920 * sense to disable the laptop input devices such as touchpad and
1923 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1924 TP_ACPI_MULTI_MODE_STAND |
1925 TP_ACPI_MULTI_MODE_TENT |
1926 TP_ACPI_MULTI_MODE_STAND_TENT,
1929 static int hotkey_get_wlsw(void)
1933 if (!tp_features.hotkey_wlsw)
1936 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1938 return (tpacpi_wlsw_emulstate) ?
1939 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1942 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1945 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1948 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1950 int type = (s >> 16) & 0xffff;
1951 int value = s & 0xffff;
1952 int mode = TP_ACPI_MULTI_MODE_INVALID;
1953 int valid_modes = 0;
1955 if (has_tablet_mode)
1956 *has_tablet_mode = 0;
1960 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1961 TP_ACPI_MULTI_MODE_TABLET |
1962 TP_ACPI_MULTI_MODE_STAND_TENT;
1965 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1966 TP_ACPI_MULTI_MODE_FLAT |
1967 TP_ACPI_MULTI_MODE_TABLET |
1968 TP_ACPI_MULTI_MODE_STAND |
1969 TP_ACPI_MULTI_MODE_TENT;
1972 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1973 TP_ACPI_MULTI_MODE_FLAT;
1977 /* In mode 4, FLAT is not specified as a valid mode. However,
1978 * it can be seen at least on the X1 Yoga 2nd Generation.
1980 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1981 TP_ACPI_MULTI_MODE_FLAT |
1982 TP_ACPI_MULTI_MODE_TABLET |
1983 TP_ACPI_MULTI_MODE_STAND |
1984 TP_ACPI_MULTI_MODE_TENT;
1987 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
1988 type, value, TPACPI_MAIL);
1992 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
1993 *has_tablet_mode = 1;
1997 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2000 mode = TP_ACPI_MULTI_MODE_FLAT;
2003 mode = TP_ACPI_MULTI_MODE_TABLET;
2007 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2009 mode = TP_ACPI_MULTI_MODE_STAND;
2012 mode = TP_ACPI_MULTI_MODE_TENT;
2015 if (type == 5 && value == 0xffff) {
2016 pr_warn("Multi mode status is undetected, assuming laptop\n");
2021 if (!(mode & valid_modes)) {
2022 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2023 value, type, TPACPI_MAIL);
2027 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2030 static int hotkey_get_tablet_mode(int *status)
2034 switch (tp_features.hotkey_tablet) {
2035 case TP_HOTKEY_TABLET_USES_MHKG:
2036 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2039 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2041 case TP_HOTKEY_TABLET_USES_GMMS:
2042 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2045 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2055 * Reads current event mask from firmware, and updates
2056 * hotkey_acpi_mask accordingly. Also resets any bits
2057 * from hotkey_user_mask that are unavailable to be
2058 * delivered (shadow requirement of the userspace ABI).
2060 * Call with hotkey_mutex held
2062 static int hotkey_mask_get(void)
2064 if (tp_features.hotkey_mask) {
2067 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2070 hotkey_acpi_mask = m;
2072 /* no mask support doesn't mean no event support... */
2073 hotkey_acpi_mask = hotkey_all_mask;
2076 /* sync userspace-visible mask */
2077 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2082 static void hotkey_mask_warn_incomplete_mask(void)
2084 /* log only what the user can fix... */
2085 const u32 wantedmask = hotkey_driver_mask &
2086 ~(hotkey_acpi_mask | hotkey_source_mask) &
2087 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2090 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2094 * Set the firmware mask when supported
2096 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2098 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2100 * Call with hotkey_mutex held
2102 static int hotkey_mask_set(u32 mask)
2107 const u32 fwmask = mask & ~hotkey_source_mask;
2109 if (tp_features.hotkey_mask) {
2110 for (i = 0; i < 32; i++) {
2111 if (!acpi_evalf(hkey_handle,
2112 NULL, "MHKM", "vdd", i + 1,
2113 !!(mask & (1 << i)))) {
2121 * We *must* make an inconditional call to hotkey_mask_get to
2122 * refresh hotkey_acpi_mask and update hotkey_user_mask
2124 * Take the opportunity to also log when we cannot _enable_
2127 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2128 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2129 fwmask, hotkey_acpi_mask);
2132 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2133 hotkey_mask_warn_incomplete_mask();
2139 * Sets hotkey_user_mask and tries to set the firmware mask
2141 * Call with hotkey_mutex held
2143 static int hotkey_user_mask_set(const u32 mask)
2147 /* Give people a chance to notice they are doing something that
2148 * is bound to go boom on their users sooner or later */
2149 if (!tp_warned.hotkey_mask_ff &&
2150 (mask == 0xffff || mask == 0xffffff ||
2151 mask == 0xffffffff)) {
2152 tp_warned.hotkey_mask_ff = 1;
2153 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2155 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2158 /* Try to enable what the user asked for, plus whatever we need.
2159 * this syncs everything but won't enable bits in hotkey_user_mask */
2160 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2162 /* Enable the available bits in hotkey_user_mask */
2163 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2169 * Sets the driver hotkey mask.
2171 * Can be called even if the hotkey subdriver is inactive
2173 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2177 /* Do the right thing if hotkey_init has not been called yet */
2178 if (!tp_features.hotkey) {
2179 hotkey_driver_mask = mask;
2183 mutex_lock(&hotkey_mutex);
2185 HOTKEY_CONFIG_CRITICAL_START
2186 hotkey_driver_mask = mask;
2187 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2188 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2190 HOTKEY_CONFIG_CRITICAL_END
2192 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2193 ~hotkey_source_mask);
2194 hotkey_poll_setup(true);
2196 mutex_unlock(&hotkey_mutex);
2201 static int hotkey_status_get(int *status)
2203 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2209 static int hotkey_status_set(bool enable)
2211 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2217 static void tpacpi_input_send_tabletsw(void)
2221 if (tp_features.hotkey_tablet &&
2222 !hotkey_get_tablet_mode(&state)) {
2223 mutex_lock(&tpacpi_inputdev_send_mutex);
2225 input_report_switch(tpacpi_inputdev,
2226 SW_TABLET_MODE, !!state);
2227 input_sync(tpacpi_inputdev);
2229 mutex_unlock(&tpacpi_inputdev_send_mutex);
2233 /* Do NOT call without validating scancode first */
2234 static void tpacpi_input_send_key(const unsigned int scancode)
2236 const unsigned int keycode = hotkey_keycode_map[scancode];
2238 if (keycode != KEY_RESERVED) {
2239 mutex_lock(&tpacpi_inputdev_send_mutex);
2241 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2242 input_report_key(tpacpi_inputdev, keycode, 1);
2243 input_sync(tpacpi_inputdev);
2245 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2246 input_report_key(tpacpi_inputdev, keycode, 0);
2247 input_sync(tpacpi_inputdev);
2249 mutex_unlock(&tpacpi_inputdev_send_mutex);
2253 /* Do NOT call without validating scancode first */
2254 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2256 hotkey_driver_event(scancode);
2257 if (hotkey_user_mask & (1 << scancode))
2258 tpacpi_input_send_key(scancode);
2261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2264 /* Do NOT call without validating scancode first */
2265 static void tpacpi_hotkey_send_key(unsigned int scancode)
2267 tpacpi_input_send_key_masked(scancode);
2270 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2274 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2275 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2276 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2277 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2278 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2279 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2281 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2282 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2283 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2285 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2286 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2287 n->displayexp_toggle =
2288 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2290 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2291 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2292 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2293 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2294 n->brightness_toggle =
2295 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2297 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2298 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2299 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2300 >> TP_NVRAM_POS_LEVEL_VOLUME;
2301 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2302 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2306 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2308 if ((event_mask & (1 << __scancode)) && \
2309 oldn->__member != newn->__member) \
2310 tpacpi_hotkey_send_key(__scancode); \
2313 #define TPACPI_MAY_SEND_KEY(__scancode) \
2315 if (event_mask & (1 << __scancode)) \
2316 tpacpi_hotkey_send_key(__scancode); \
2319 static void issue_volchange(const unsigned int oldvol,
2320 const unsigned int newvol,
2321 const u32 event_mask)
2323 unsigned int i = oldvol;
2325 while (i > newvol) {
2326 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2329 while (i < newvol) {
2330 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2335 static void issue_brightnesschange(const unsigned int oldbrt,
2336 const unsigned int newbrt,
2337 const u32 event_mask)
2339 unsigned int i = oldbrt;
2341 while (i > newbrt) {
2342 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2345 while (i < newbrt) {
2346 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2351 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2352 struct tp_nvram_state *newn,
2353 const u32 event_mask)
2356 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2357 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2358 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2359 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2361 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2363 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2368 * This code is supposed to duplicate the IBM firmware behaviour:
2369 * - Pressing MUTE issues mute hotkey message, even when already mute
2370 * - Pressing Volume up/down issues volume up/down hotkey messages,
2371 * even when already at maximum or minimum volume
2372 * - The act of unmuting issues volume up/down notification,
2373 * depending which key was used to unmute
2375 * We are constrained to what the NVRAM can tell us, which is not much
2376 * and certainly not enough if more than one volume hotkey was pressed
2377 * since the last poll cycle.
2379 * Just to make our life interesting, some newer Lenovo ThinkPads have
2380 * bugs in the BIOS and may fail to update volume_toggle properly.
2385 oldn->volume_toggle != newn->volume_toggle ||
2386 oldn->volume_level != newn->volume_level) {
2387 /* recently muted, or repeated mute keypress, or
2388 * multiple presses ending in mute */
2389 issue_volchange(oldn->volume_level, newn->volume_level,
2391 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2396 /* recently unmuted, issue 'unmute' keypress */
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2399 if (oldn->volume_level != newn->volume_level) {
2400 issue_volchange(oldn->volume_level, newn->volume_level,
2402 } else if (oldn->volume_toggle != newn->volume_toggle) {
2403 /* repeated vol up/down keypress at end of scale ? */
2404 if (newn->volume_level == 0)
2405 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2406 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2411 /* handle brightness */
2412 if (oldn->brightness_level != newn->brightness_level) {
2413 issue_brightnesschange(oldn->brightness_level,
2414 newn->brightness_level, event_mask);
2415 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2416 /* repeated key presses that didn't change state */
2417 if (newn->brightness_level == 0)
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2419 else if (newn->brightness_level >= bright_maxlvl
2420 && !tp_features.bright_unkfw)
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2424 #undef TPACPI_COMPARE_KEY
2425 #undef TPACPI_MAY_SEND_KEY
2431 * We track all events in hotkey_source_mask all the time, since
2432 * most of them are edge-based. We only issue those requested by
2433 * hotkey_user_mask or hotkey_driver_mask, though.
2435 static int hotkey_kthread(void *data)
2437 struct tp_nvram_state s[2] = { 0 };
2438 u32 poll_mask, event_mask;
2439 unsigned int si, so;
2441 unsigned int change_detector;
2442 unsigned int poll_freq;
2445 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2454 /* Initial state for compares */
2455 mutex_lock(&hotkey_thread_data_mutex);
2456 change_detector = hotkey_config_change;
2457 poll_mask = hotkey_source_mask;
2458 event_mask = hotkey_source_mask &
2459 (hotkey_driver_mask | hotkey_user_mask);
2460 poll_freq = hotkey_poll_freq;
2461 mutex_unlock(&hotkey_thread_data_mutex);
2462 hotkey_read_nvram(&s[so], poll_mask);
2464 while (!kthread_should_stop()) {
2466 if (likely(poll_freq))
2469 t = 100; /* should never happen... */
2471 t = msleep_interruptible(t);
2472 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2475 if (t > 0 && !was_frozen)
2478 mutex_lock(&hotkey_thread_data_mutex);
2479 if (was_frozen || hotkey_config_change != change_detector) {
2480 /* forget old state on thaw or config change */
2483 change_detector = hotkey_config_change;
2485 poll_mask = hotkey_source_mask;
2486 event_mask = hotkey_source_mask &
2487 (hotkey_driver_mask | hotkey_user_mask);
2488 poll_freq = hotkey_poll_freq;
2489 mutex_unlock(&hotkey_thread_data_mutex);
2491 if (likely(poll_mask)) {
2492 hotkey_read_nvram(&s[si], poll_mask);
2493 if (likely(si != so)) {
2494 hotkey_compare_and_issue_event(&s[so], &s[si],
2507 /* call with hotkey_mutex held */
2508 static void hotkey_poll_stop_sync(void)
2510 if (tpacpi_hotkey_task) {
2511 kthread_stop(tpacpi_hotkey_task);
2512 tpacpi_hotkey_task = NULL;
2516 /* call with hotkey_mutex held */
2517 static void hotkey_poll_setup(const bool may_warn)
2519 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2520 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2522 if (hotkey_poll_freq > 0 &&
2523 (poll_driver_mask ||
2524 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2525 if (!tpacpi_hotkey_task) {
2526 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2527 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2528 if (IS_ERR(tpacpi_hotkey_task)) {
2529 tpacpi_hotkey_task = NULL;
2530 pr_err("could not create kernel thread for hotkey polling\n");
2534 hotkey_poll_stop_sync();
2535 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2536 hotkey_poll_freq == 0) {
2537 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2538 poll_user_mask, poll_driver_mask);
2543 static void hotkey_poll_setup_safe(const bool may_warn)
2545 mutex_lock(&hotkey_mutex);
2546 hotkey_poll_setup(may_warn);
2547 mutex_unlock(&hotkey_mutex);
2550 /* call with hotkey_mutex held */
2551 static void hotkey_poll_set_freq(unsigned int freq)
2554 hotkey_poll_stop_sync();
2556 hotkey_poll_freq = freq;
2559 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2561 static void hotkey_poll_setup(const bool __unused)
2565 static void hotkey_poll_setup_safe(const bool __unused)
2569 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2571 static int hotkey_inputdev_open(struct input_dev *dev)
2573 switch (tpacpi_lifecycle) {
2574 case TPACPI_LIFE_INIT:
2575 case TPACPI_LIFE_RUNNING:
2576 hotkey_poll_setup_safe(false);
2578 case TPACPI_LIFE_EXITING:
2582 /* Should only happen if tpacpi_lifecycle is corrupt */
2587 static void hotkey_inputdev_close(struct input_dev *dev)
2589 /* disable hotkey polling when possible */
2590 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2591 !(hotkey_source_mask & hotkey_driver_mask))
2592 hotkey_poll_setup_safe(false);
2595 /* sysfs hotkey enable ------------------------------------------------- */
2596 static ssize_t hotkey_enable_show(struct device *dev,
2597 struct device_attribute *attr,
2602 printk_deprecated_attribute("hotkey_enable",
2603 "Hotkey reporting is always enabled");
2605 res = hotkey_status_get(&status);
2609 return sysfs_emit(buf, "%d\n", status);
2612 static ssize_t hotkey_enable_store(struct device *dev,
2613 struct device_attribute *attr,
2614 const char *buf, size_t count)
2618 printk_deprecated_attribute("hotkey_enable",
2619 "Hotkeys can be disabled through hotkey_mask");
2621 if (parse_strtoul(buf, 1, &t))
2630 static DEVICE_ATTR_RW(hotkey_enable);
2632 /* sysfs hotkey mask --------------------------------------------------- */
2633 static ssize_t hotkey_mask_show(struct device *dev,
2634 struct device_attribute *attr,
2637 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
2640 static ssize_t hotkey_mask_store(struct device *dev,
2641 struct device_attribute *attr,
2642 const char *buf, size_t count)
2647 if (parse_strtoul(buf, 0xffffffffUL, &t))
2650 if (mutex_lock_killable(&hotkey_mutex))
2651 return -ERESTARTSYS;
2653 res = hotkey_user_mask_set(t);
2655 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2656 hotkey_poll_setup(true);
2659 mutex_unlock(&hotkey_mutex);
2661 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2663 return (res) ? res : count;
2666 static DEVICE_ATTR_RW(hotkey_mask);
2668 /* sysfs hotkey bios_enabled ------------------------------------------- */
2669 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2670 struct device_attribute *attr,
2673 return sprintf(buf, "0\n");
2676 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2678 /* sysfs hotkey bios_mask ---------------------------------------------- */
2679 static ssize_t hotkey_bios_mask_show(struct device *dev,
2680 struct device_attribute *attr,
2683 printk_deprecated_attribute("hotkey_bios_mask",
2684 "This attribute is useless.");
2685 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
2688 static DEVICE_ATTR_RO(hotkey_bios_mask);
2690 /* sysfs hotkey all_mask ----------------------------------------------- */
2691 static ssize_t hotkey_all_mask_show(struct device *dev,
2692 struct device_attribute *attr,
2695 return sysfs_emit(buf, "0x%08x\n",
2696 hotkey_all_mask | hotkey_source_mask);
2699 static DEVICE_ATTR_RO(hotkey_all_mask);
2701 /* sysfs hotkey all_mask ----------------------------------------------- */
2702 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2703 struct device_attribute *attr,
2706 return sysfs_emit(buf, "0x%08x\n",
2707 hotkey_adaptive_all_mask | hotkey_source_mask);
2710 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2712 /* sysfs hotkey recommended_mask --------------------------------------- */
2713 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2714 struct device_attribute *attr,
2717 return sysfs_emit(buf, "0x%08x\n",
2718 (hotkey_all_mask | hotkey_source_mask)
2719 & ~hotkey_reserved_mask);
2722 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2724 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2726 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2727 static ssize_t hotkey_source_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2731 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
2734 static ssize_t hotkey_source_mask_store(struct device *dev,
2735 struct device_attribute *attr,
2736 const char *buf, size_t count)
2742 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2743 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2746 if (mutex_lock_killable(&hotkey_mutex))
2747 return -ERESTARTSYS;
2749 HOTKEY_CONFIG_CRITICAL_START
2750 hotkey_source_mask = t;
2751 HOTKEY_CONFIG_CRITICAL_END
2753 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2754 ~hotkey_source_mask);
2755 hotkey_poll_setup(true);
2757 /* check if events needed by the driver got disabled */
2758 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2759 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2761 mutex_unlock(&hotkey_mutex);
2764 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2767 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2770 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2772 return (rc < 0) ? rc : count;
2775 static DEVICE_ATTR_RW(hotkey_source_mask);
2777 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2778 static ssize_t hotkey_poll_freq_show(struct device *dev,
2779 struct device_attribute *attr,
2782 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
2785 static ssize_t hotkey_poll_freq_store(struct device *dev,
2786 struct device_attribute *attr,
2787 const char *buf, size_t count)
2791 if (parse_strtoul(buf, 25, &t))
2794 if (mutex_lock_killable(&hotkey_mutex))
2795 return -ERESTARTSYS;
2797 hotkey_poll_set_freq(t);
2798 hotkey_poll_setup(true);
2800 mutex_unlock(&hotkey_mutex);
2802 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2807 static DEVICE_ATTR_RW(hotkey_poll_freq);
2809 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2811 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2812 static ssize_t hotkey_radio_sw_show(struct device *dev,
2813 struct device_attribute *attr,
2817 res = hotkey_get_wlsw();
2821 /* Opportunistic update */
2822 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2824 return sysfs_emit(buf, "%d\n",
2825 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2828 static DEVICE_ATTR_RO(hotkey_radio_sw);
2830 static void hotkey_radio_sw_notify_change(void)
2832 if (tp_features.hotkey_wlsw)
2833 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2837 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2838 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2839 struct device_attribute *attr,
2843 res = hotkey_get_tablet_mode(&s);
2847 return sysfs_emit(buf, "%d\n", !!s);
2850 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2852 static void hotkey_tablet_mode_notify_change(void)
2854 if (tp_features.hotkey_tablet)
2855 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2856 "hotkey_tablet_mode");
2859 /* sysfs wakeup reason (pollable) -------------------------------------- */
2860 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2861 struct device_attribute *attr,
2864 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
2867 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2869 static void hotkey_wakeup_reason_notify_change(void)
2871 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2875 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2876 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2877 struct device_attribute *attr,
2880 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
2883 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2884 hotkey_wakeup_hotunplug_complete_show, NULL);
2886 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2888 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2889 "wakeup_hotunplug_complete");
2892 /* sysfs adaptive kbd mode --------------------------------------------- */
2894 static int adaptive_keyboard_get_mode(void);
2895 static int adaptive_keyboard_set_mode(int new_mode);
2897 enum ADAPTIVE_KEY_MODE {
2900 WEB_CONFERENCE_MODE,
2905 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2906 struct device_attribute *attr,
2911 current_mode = adaptive_keyboard_get_mode();
2912 if (current_mode < 0)
2913 return current_mode;
2915 return sysfs_emit(buf, "%d\n", current_mode);
2918 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2919 struct device_attribute *attr,
2920 const char *buf, size_t count)
2925 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2928 res = adaptive_keyboard_set_mode(t);
2929 return (res < 0) ? res : count;
2932 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2934 static struct attribute *adaptive_kbd_attributes[] = {
2935 &dev_attr_adaptive_kbd_mode.attr,
2939 static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2940 struct attribute *attr, int n)
2942 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2945 static const struct attribute_group adaptive_kbd_attr_group = {
2946 .is_visible = hadaptive_kbd_attr_is_visible,
2947 .attrs = adaptive_kbd_attributes,
2950 /* --------------------------------------------------------------------- */
2952 static struct attribute *hotkey_attributes[] = {
2953 &dev_attr_hotkey_enable.attr,
2954 &dev_attr_hotkey_bios_enabled.attr,
2955 &dev_attr_hotkey_bios_mask.attr,
2956 &dev_attr_wakeup_reason.attr,
2957 &dev_attr_wakeup_hotunplug_complete.attr,
2958 &dev_attr_hotkey_mask.attr,
2959 &dev_attr_hotkey_all_mask.attr,
2960 &dev_attr_hotkey_adaptive_all_mask.attr,
2961 &dev_attr_hotkey_recommended_mask.attr,
2962 &dev_attr_hotkey_tablet_mode.attr,
2963 &dev_attr_hotkey_radio_sw.attr,
2964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2965 &dev_attr_hotkey_source_mask.attr,
2966 &dev_attr_hotkey_poll_freq.attr,
2971 static umode_t hotkey_attr_is_visible(struct kobject *kobj,
2972 struct attribute *attr, int n)
2974 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
2975 if (!tp_features.hotkey_tablet)
2977 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
2978 if (!tp_features.hotkey_wlsw)
2985 static const struct attribute_group hotkey_attr_group = {
2986 .is_visible = hotkey_attr_is_visible,
2987 .attrs = hotkey_attributes,
2991 * Sync both the hw and sw blocking state of all switches
2993 static void tpacpi_send_radiosw_update(void)
2998 * We must sync all rfkill controllers *before* issuing any
2999 * rfkill input events, or we will race the rfkill core input
3002 * tpacpi_inputdev_send_mutex works as a synchronization point
3005 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3008 wlsw = hotkey_get_wlsw();
3010 /* Sync hw blocking state first if it is hw-blocked */
3011 if (wlsw == TPACPI_RFK_RADIO_OFF)
3012 tpacpi_rfk_update_hwblock_state(true);
3014 /* Sync hw blocking state last if it is hw-unblocked */
3015 if (wlsw == TPACPI_RFK_RADIO_ON)
3016 tpacpi_rfk_update_hwblock_state(false);
3018 /* Issue rfkill input event for WLSW switch */
3020 mutex_lock(&tpacpi_inputdev_send_mutex);
3022 input_report_switch(tpacpi_inputdev,
3023 SW_RFKILL_ALL, (wlsw > 0));
3024 input_sync(tpacpi_inputdev);
3026 mutex_unlock(&tpacpi_inputdev_send_mutex);
3030 * this can be unconditional, as we will poll state again
3031 * if userspace uses the notify to read data
3033 hotkey_radio_sw_notify_change();
3036 static void hotkey_exit(void)
3038 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3039 mutex_lock(&hotkey_mutex);
3040 hotkey_poll_stop_sync();
3041 mutex_unlock(&hotkey_mutex);
3043 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3044 "restoring original HKEY status and mask\n");
3045 /* yes, there is a bitwise or below, we want the
3046 * functions to be called even if one of them fail */
3047 if (((tp_features.hotkey_mask &&
3048 hotkey_mask_set(hotkey_orig_mask)) |
3049 hotkey_status_set(false)) != 0)
3050 pr_err("failed to restore hot key mask to BIOS defaults\n");
3053 static void __init hotkey_unmap(const unsigned int scancode)
3055 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3056 clear_bit(hotkey_keycode_map[scancode],
3057 tpacpi_inputdev->keybit);
3058 hotkey_keycode_map[scancode] = KEY_RESERVED;
3064 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3067 #define TPACPI_HK_Q_INIMASK 0x0001
3069 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3070 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3071 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3072 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3073 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3074 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3075 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3076 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3077 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3078 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3079 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3080 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3081 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3082 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3083 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3084 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3085 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3086 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3087 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3088 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3091 typedef u16 tpacpi_keymap_entry_t;
3092 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3094 static int hotkey_init_tablet_mode(void)
3096 int in_tablet_mode = 0, res;
3099 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3100 int has_tablet_mode;
3102 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3105 * The Yoga 11e series has 2 accelerometers described by a
3106 * BOSC0200 ACPI node. This setup relies on a Windows service
3107 * which calls special ACPI methods on this node to report
3108 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3109 * does not support this, so skip the hotkey on these models.
3111 if (has_tablet_mode && !dual_accel_detect())
3112 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3114 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3115 /* For X41t, X60t, X61t Tablets... */
3116 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3117 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3121 if (!tp_features.hotkey_tablet)
3124 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3125 type, in_tablet_mode ? "tablet" : "laptop");
3127 return in_tablet_mode;
3130 static int __init hotkey_init(struct ibm_init_struct *iibm)
3132 /* Requirements for changing the default keymaps:
3134 * 1. Many of the keys are mapped to KEY_RESERVED for very
3135 * good reasons. Do not change them unless you have deep
3136 * knowledge on the IBM and Lenovo ThinkPad firmware for
3137 * the various ThinkPad models. The driver behaves
3138 * differently for KEY_RESERVED: such keys have their
3139 * hot key mask *unset* in mask_recommended, and also
3140 * in the initial hot key mask programmed into the
3141 * firmware at driver load time, which means the firm-
3142 * ware may react very differently if you change them to
3145 * 2. You must be subscribed to the linux-thinkpad and
3146 * ibm-acpi-devel mailing lists, and you should read the
3147 * list archives since 2007 if you want to change the
3148 * keymaps. This requirement exists so that you will
3149 * know the past history of problems with the thinkpad-
3150 * acpi driver keymaps, and also that you will be
3151 * listening to any bug reports;
3153 * 3. Do not send thinkpad-acpi specific patches directly to
3154 * for merging, *ever*. Send them to the linux-acpi
3155 * mailinglist for comments. Merging is to be done only
3156 * through acpi-test and the ACPI maintainer.
3158 * If the above is too much to ask, don't change the keymap.
3159 * Ask the thinkpad-acpi maintainer to do it, instead.
3163 TPACPI_KEYMAP_IBM_GENERIC = 0,
3164 TPACPI_KEYMAP_LENOVO_GENERIC,
3167 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3168 /* Generic keymap for IBM ThinkPads */
3169 [TPACPI_KEYMAP_IBM_GENERIC] = {
3170 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3171 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3172 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3173 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3175 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3176 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3177 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3178 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3180 /* brightness: firmware always reacts to them */
3181 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3182 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3184 /* Thinklight: firmware always react to it */
3185 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3187 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3188 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3190 /* Volume: firmware always react to it and reprograms
3191 * the built-in *extra* mixer. Never map it to control
3192 * another mixer by default. */
3193 KEY_RESERVED, /* 0x14: VOLUME UP */
3194 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3195 KEY_RESERVED, /* 0x16: MUTE */
3197 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3199 /* (assignments unknown, please report if found) */
3200 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3201 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3203 /* No assignments, only used for Adaptive keyboards. */
3204 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3205 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3206 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3207 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3208 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3210 /* No assignment, used for newer Lenovo models */
3211 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3212 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3213 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3214 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3215 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3216 KEY_UNKNOWN, KEY_UNKNOWN
3220 /* Generic keymap for Lenovo ThinkPads */
3221 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3222 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3223 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3224 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3225 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3227 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3228 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3229 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3230 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3232 /* These should be enabled --only-- when ACPI video
3233 * is disabled (i.e. in "vendor" mode), and are handled
3234 * in a special way by the init code */
3235 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3236 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3238 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3240 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3241 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3243 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3244 * react to it and reprograms the built-in *extra* mixer.
3245 * Never map it to control another mixer by default.
3247 * T60?, T61, R60?, R61: firmware and EC tries to send
3248 * these over the regular keyboard, so these are no-ops,
3249 * but there are still weird bugs re. MUTE, so do not
3250 * change unless you get test reports from all Lenovo
3251 * models. May cause the BIOS to interfere with the
3254 KEY_RESERVED, /* 0x14: VOLUME UP */
3255 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3256 KEY_RESERVED, /* 0x16: MUTE */
3258 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3260 /* (assignments unknown, please report if found) */
3261 KEY_UNKNOWN, KEY_UNKNOWN,
3264 * The mic mute button only sends 0x1a. It does not
3265 * automatically mute the mic or change the mute light.
3267 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3269 /* (assignments unknown, please report if found) */
3272 /* Extra keys in use since the X240 / T440 / T540 */
3273 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3276 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3277 * The first item in this list is the Mute button which is
3278 * emitted with 0x103 through
3279 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3281 * We'll need to offset those by 0x20.
3283 KEY_RESERVED, /* Mute held, 0x103 */
3284 KEY_BRIGHTNESS_MIN, /* Backlight off */
3285 KEY_RESERVED, /* Clipping tool */
3286 KEY_RESERVED, /* Cloud */
3288 KEY_VOICECOMMAND, /* Voice */
3290 KEY_RESERVED, /* Gestures */
3294 KEY_CONFIG, /* Settings */
3295 KEY_RESERVED, /* New tab */
3296 KEY_REFRESH, /* Reload */
3297 KEY_BACK, /* Back */
3298 KEY_RESERVED, /* Microphone down */
3299 KEY_RESERVED, /* Microphone up */
3300 KEY_RESERVED, /* Microphone cancellation */
3301 KEY_RESERVED, /* Camera mode */
3302 KEY_RESERVED, /* Rotate display, 0x116 */
3305 * These are found in 2017 models (e.g. T470s, X270).
3306 * The lowest known value is 0x311, which according to
3307 * the manual should launch a user defined favorite
3310 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3311 * corresponding to 0x34.
3314 /* (assignments unknown, please report if found) */
3315 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3316 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3318 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3321 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3322 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
3323 KEY_CALC, /* Calculator (above numpad, P52) */
3324 KEY_BLUETOOTH, /* Bluetooth */
3325 KEY_KEYBOARD, /* Keyboard, 0x315 */
3326 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3327 KEY_NOTIFICATION_CENTER, /* Notification Center */
3328 KEY_PICKUP_PHONE, /* Answer incoming call */
3329 KEY_HANGUP_PHONE, /* Decline incoming call */
3333 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3334 /* Generic maps (fallback) */
3336 .vendor = PCI_VENDOR_ID_IBM,
3337 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3338 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3341 .vendor = PCI_VENDOR_ID_LENOVO,
3342 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3343 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3347 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3348 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3353 bool radiosw_state = false;
3354 bool tabletsw_state = false;
3356 unsigned long quirks;
3357 unsigned long keymap_id;
3359 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3360 "initializing hotkey subdriver\n");
3362 BUG_ON(!tpacpi_inputdev);
3363 BUG_ON(tpacpi_inputdev->open != NULL ||
3364 tpacpi_inputdev->close != NULL);
3366 TPACPI_ACPIHANDLE_INIT(hkey);
3367 mutex_init(&hotkey_mutex);
3369 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3370 mutex_init(&hotkey_thread_data_mutex);
3373 /* hotkey not supported on 570 */
3374 tp_features.hotkey = hkey_handle != NULL;
3376 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3378 str_supported(tp_features.hotkey));
3380 if (!tp_features.hotkey)
3383 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3384 ARRAY_SIZE(tpacpi_hotkey_qtable));
3386 tpacpi_disable_brightness_delay();
3388 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3389 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3390 for HKEY interface version 0x100 */
3391 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3392 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3393 "firmware HKEY interface version: 0x%x\n",
3396 switch (hkeyv >> 8) {
3399 * MHKV 0x100 in A31, R40, R40e,
3400 * T4x, X31, and later
3403 /* Paranoia check AND init hotkey_all_mask */
3404 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3406 pr_err("missing MHKA handler, please report this to %s\n",
3408 /* Fallback: pre-init for FN+F3,F4,F12 */
3409 hotkey_all_mask = 0x080cU;
3411 tp_features.hotkey_mask = 1;
3417 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3420 /* Paranoia check AND init hotkey_all_mask */
3421 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3423 pr_err("missing MHKA handler, please report this to %s\n",
3425 /* Fallback: pre-init for FN+F3,F4,F12 */
3426 hotkey_all_mask = 0x080cU;
3428 tp_features.hotkey_mask = 1;
3432 * Check if we have an adaptive keyboard, like on the
3433 * Lenovo Carbon X1 2014 (2nd Gen).
3435 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3437 if (hotkey_adaptive_all_mask != 0)
3438 tp_features.has_adaptive_kbd = true;
3440 tp_features.has_adaptive_kbd = false;
3441 hotkey_adaptive_all_mask = 0x0U;
3446 pr_err("unknown version of the HKEY interface: 0x%x\n",
3448 pr_err("please report this to %s\n", TPACPI_MAIL);
3453 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3454 "hotkey masks are %s\n",
3455 str_supported(tp_features.hotkey_mask));
3457 /* Init hotkey_all_mask if not initialized yet */
3458 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3459 (quirks & TPACPI_HK_Q_INIMASK))
3460 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3462 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3463 if (tp_features.hotkey_mask) {
3464 /* hotkey_source_mask *must* be zero for
3465 * the first hotkey_mask_get to return hotkey_orig_mask */
3466 res = hotkey_mask_get();
3470 hotkey_orig_mask = hotkey_acpi_mask;
3472 hotkey_orig_mask = hotkey_all_mask;
3473 hotkey_acpi_mask = hotkey_all_mask;
3476 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3478 tp_features.hotkey_wlsw = 1;
3479 radiosw_state = !!tpacpi_wlsw_emulstate;
3480 pr_info("radio switch emulation enabled\n");
3483 /* Not all thinkpads have a hardware radio switch */
3484 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3485 tp_features.hotkey_wlsw = 1;
3486 radiosw_state = !!status;
3487 pr_info("radio switch found; radios are %s\n",
3488 enabled(status, 0));
3491 tabletsw_state = hotkey_init_tablet_mode();
3493 /* Set up key map */
3494 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3495 ARRAY_SIZE(tpacpi_keymap_qtable));
3496 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3497 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3498 "using keymap number %lu\n", keymap_id);
3500 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3501 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3502 if (!hotkey_keycode_map) {
3503 pr_err("failed to allocate memory for key map\n");
3507 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3508 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3509 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3510 tpacpi_inputdev->keycode = hotkey_keycode_map;
3511 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3512 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3513 input_set_capability(tpacpi_inputdev, EV_KEY,
3514 hotkey_keycode_map[i]);
3516 if (i < sizeof(hotkey_reserved_mask)*8)
3517 hotkey_reserved_mask |= 1 << i;
3521 if (tp_features.hotkey_wlsw) {
3522 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3523 input_report_switch(tpacpi_inputdev,
3524 SW_RFKILL_ALL, radiosw_state);
3526 if (tp_features.hotkey_tablet) {
3527 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3528 input_report_switch(tpacpi_inputdev,
3529 SW_TABLET_MODE, tabletsw_state);
3532 /* Do not issue duplicate brightness change events to
3533 * userspace. tpacpi_detect_brightness_capabilities() must have
3534 * been called before this point */
3535 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3536 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3537 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3539 /* Disable brightness up/down on Lenovo thinkpads when
3540 * ACPI is handling them, otherwise it is plain impossible
3541 * for userspace to do something even remotely sane */
3542 hotkey_reserved_mask |=
3543 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3544 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3545 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3546 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3549 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3550 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3552 & ~hotkey_reserved_mask;
3554 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3555 "hotkey source mask 0x%08x, polling freq %u\n",
3556 hotkey_source_mask, hotkey_poll_freq);
3559 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3560 "enabling firmware HKEY event interface...\n");
3561 res = hotkey_status_set(true);
3566 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3567 | hotkey_driver_mask)
3568 & ~hotkey_source_mask);
3569 if (res < 0 && res != -ENXIO) {
3573 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3574 & ~hotkey_reserved_mask;
3575 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3576 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3577 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3579 tpacpi_inputdev->open = &hotkey_inputdev_open;
3580 tpacpi_inputdev->close = &hotkey_inputdev_close;
3582 hotkey_poll_setup_safe(true);
3587 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3588 * mode, Web conference mode, Function mode and Lay-flat mode.
3589 * We support Home mode and Function mode currently.
3591 * Will consider support rest of modes in future.
3594 static const int adaptive_keyboard_modes[] = {
3596 /* WEB_BROWSER_MODE = 2,
3597 WEB_CONFERENCE_MODE = 3, */
3601 #define DFR_CHANGE_ROW 0x101
3602 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3603 #define FIRST_ADAPTIVE_KEY 0x103
3605 /* press Fn key a while second, it will switch to Function Mode. Then
3606 * release Fn key, previous mode be restored.
3608 static bool adaptive_keyboard_mode_is_saved;
3609 static int adaptive_keyboard_prev_mode;
3611 static int adaptive_keyboard_get_mode(void)
3615 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3616 pr_err("Cannot read adaptive keyboard mode\n");
3623 static int adaptive_keyboard_set_mode(int new_mode)
3626 new_mode > LAYFLAT_MODE)
3629 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3630 pr_err("Cannot set adaptive keyboard mode\n");
3637 static int adaptive_keyboard_get_next_mode(int mode)
3640 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3642 for (i = 0; i <= max_mode; i++) {
3643 if (adaptive_keyboard_modes[i] == mode)
3652 return adaptive_keyboard_modes[i];
3655 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3657 int current_mode = 0;
3662 case DFR_CHANGE_ROW:
3663 if (adaptive_keyboard_mode_is_saved) {
3664 new_mode = adaptive_keyboard_prev_mode;
3665 adaptive_keyboard_mode_is_saved = false;
3667 current_mode = adaptive_keyboard_get_mode();
3668 if (current_mode < 0)
3670 new_mode = adaptive_keyboard_get_next_mode(
3674 if (adaptive_keyboard_set_mode(new_mode) < 0)
3679 case DFR_SHOW_QUICKVIEW_ROW:
3680 current_mode = adaptive_keyboard_get_mode();
3681 if (current_mode < 0)
3684 adaptive_keyboard_prev_mode = current_mode;
3685 adaptive_keyboard_mode_is_saved = true;
3687 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3692 if (scancode < FIRST_ADAPTIVE_KEY ||
3693 scancode >= FIRST_ADAPTIVE_KEY +
3694 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3695 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3696 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3700 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3701 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3702 if (keycode != KEY_RESERVED) {
3703 mutex_lock(&tpacpi_inputdev_send_mutex);
3705 input_report_key(tpacpi_inputdev, keycode, 1);
3706 input_sync(tpacpi_inputdev);
3708 input_report_key(tpacpi_inputdev, keycode, 0);
3709 input_sync(tpacpi_inputdev);
3711 mutex_unlock(&tpacpi_inputdev_send_mutex);
3717 static bool hotkey_notify_extended_hotkey(const u32 hkey)
3719 unsigned int scancode;
3722 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3723 tpacpi_driver_event(hkey);
3727 /* Extended keycodes start at 0x300 and our offset into the map
3728 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3729 * will be positive, but might not be in the correct range.
3731 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3732 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3733 scancode < TPACPI_HOTKEY_MAP_LEN) {
3734 tpacpi_input_send_key(scancode);
3741 static bool hotkey_notify_hotkey(const u32 hkey,
3743 bool *ignore_acpi_ev)
3745 /* 0x1000-0x1FFF: key presses */
3746 unsigned int scancode = hkey & 0xfff;
3747 *send_acpi_ev = true;
3748 *ignore_acpi_ev = false;
3751 * Original events are in the 0x10XX range, the adaptive keyboard
3752 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3753 * models, additional keys are emitted through 0x13XX.
3755 switch ((hkey >> 8) & 0xf) {
3758 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3759 /* HKEY event 0x1001 is scancode 0x00 */
3761 if (!(hotkey_source_mask & (1 << scancode))) {
3762 tpacpi_input_send_key_masked(scancode);
3763 *send_acpi_ev = false;
3765 *ignore_acpi_ev = true;
3772 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3775 return hotkey_notify_extended_hotkey(hkey);
3781 static bool hotkey_notify_wakeup(const u32 hkey,
3783 bool *ignore_acpi_ev)
3785 /* 0x2000-0x2FFF: Wakeup reason */
3786 *send_acpi_ev = true;
3787 *ignore_acpi_ev = false;
3790 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3791 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3792 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3793 *ignore_acpi_ev = true;
3796 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3797 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3798 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3799 *ignore_acpi_ev = true;
3802 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3803 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3804 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3805 /* how to auto-heal: */
3806 /* 2313: woke up from S3, go to S4/S5 */
3807 /* 2413: woke up from S4, go to S5 */
3814 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3815 pr_info("woke up due to a hot-unplug request...\n");
3816 hotkey_wakeup_reason_notify_change();
3821 static bool hotkey_notify_dockevent(const u32 hkey,
3823 bool *ignore_acpi_ev)
3825 /* 0x4000-0x4FFF: dock-related events */
3826 *send_acpi_ev = true;
3827 *ignore_acpi_ev = false;
3830 case TP_HKEY_EV_UNDOCK_ACK:
3831 /* ACPI undock operation completed after wakeup */
3832 hotkey_autosleep_ack = 1;
3833 pr_info("undocked\n");
3834 hotkey_wakeup_hotunplug_complete_notify_change();
3837 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3838 pr_info("docked into hotplug port replicator\n");
3840 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3841 pr_info("undocked from hotplug port replicator\n");
3845 * Deliberately ignore attaching and detaching the keybord cover to avoid
3846 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3849 * Please refer to the following thread for more information and a preliminary
3850 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3851 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3852 * the Pico cartridge dock module:
3853 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
3855 case TP_HKEY_EV_KBD_COVER_ATTACH:
3856 case TP_HKEY_EV_KBD_COVER_DETACH:
3857 *send_acpi_ev = false;
3858 *ignore_acpi_ev = true;
3866 static bool hotkey_notify_usrevent(const u32 hkey,
3868 bool *ignore_acpi_ev)
3870 /* 0x5000-0x5FFF: human interface helpers */
3871 *send_acpi_ev = true;
3872 *ignore_acpi_ev = false;
3875 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3876 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3879 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3880 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3881 tpacpi_input_send_tabletsw();
3882 hotkey_tablet_mode_notify_change();
3883 *send_acpi_ev = false;
3886 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3887 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3888 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3889 /* do not propagate these events */
3890 *ignore_acpi_ev = true;
3898 static void thermal_dump_all_sensors(void);
3899 static void palmsensor_refresh(void);
3901 static bool hotkey_notify_6xxx(const u32 hkey,
3903 bool *ignore_acpi_ev)
3905 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3906 *send_acpi_ev = true;
3907 *ignore_acpi_ev = false;
3910 case TP_HKEY_EV_THM_TABLE_CHANGED:
3911 pr_debug("EC reports: Thermal Table has changed\n");
3912 /* recommended action: do nothing, we don't have
3913 * Lenovo ATM information */
3915 case TP_HKEY_EV_THM_CSM_COMPLETED:
3916 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
3917 /* Thermal event - pass on to event handler */
3918 tpacpi_driver_event(hkey);
3920 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3921 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
3922 /* recommended action: do nothing, we don't have
3923 * Lenovo ATM information */
3925 case TP_HKEY_EV_ALARM_BAT_HOT:
3926 pr_crit("THERMAL ALARM: battery is too hot!\n");
3927 /* recommended action: warn user through gui */
3929 case TP_HKEY_EV_ALARM_BAT_XHOT:
3930 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3931 /* recommended action: immediate sleep/hibernate */
3933 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3934 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3935 /* recommended action: warn user through gui, that */
3936 /* some internal component is too hot */
3938 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3939 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3940 /* recommended action: immediate sleep/hibernate */
3942 case TP_HKEY_EV_AC_CHANGED:
3943 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3944 * AC status changed; can be triggered by plugging or
3945 * unplugging AC adapter, docking or undocking. */
3949 case TP_HKEY_EV_KEY_NUMLOCK:
3950 case TP_HKEY_EV_KEY_FN:
3951 /* key press events, we just ignore them as long as the EC
3952 * is still reporting them in the normal keyboard stream */
3953 *send_acpi_ev = false;
3954 *ignore_acpi_ev = true;
3957 case TP_HKEY_EV_KEY_FN_ESC:
3958 /* Get the media key status to force the status LED to update */
3959 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3960 *send_acpi_ev = false;
3961 *ignore_acpi_ev = true;
3964 case TP_HKEY_EV_TABLET_CHANGED:
3965 tpacpi_input_send_tabletsw();
3966 hotkey_tablet_mode_notify_change();
3967 *send_acpi_ev = false;
3970 case TP_HKEY_EV_PALM_DETECTED:
3971 case TP_HKEY_EV_PALM_UNDETECTED:
3972 /* palm detected - pass on to event handler */
3973 palmsensor_refresh();
3977 /* report simply as unknown, no sensor dump */
3981 thermal_dump_all_sensors();
3985 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3989 bool ignore_acpi_ev;
3992 if (event != 0x80) {
3993 pr_err("unknown HKEY notification event %d\n", event);
3994 /* forward it to userspace, maybe it knows how to handle it */
3995 acpi_bus_generate_netlink_event(
3996 ibm->acpi->device->pnp.device_class,
3997 dev_name(&ibm->acpi->device->dev),
4003 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4004 pr_err("failed to retrieve HKEY event\n");
4013 send_acpi_ev = true;
4014 ignore_acpi_ev = false;
4016 switch (hkey >> 12) {
4018 /* 0x1000-0x1FFF: key presses */
4019 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4023 /* 0x2000-0x2FFF: Wakeup reason */
4024 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4028 /* 0x3000-0x3FFF: bay-related wakeups */
4030 case TP_HKEY_EV_BAYEJ_ACK:
4031 hotkey_autosleep_ack = 1;
4032 pr_info("bay ejected\n");
4033 hotkey_wakeup_hotunplug_complete_notify_change();
4036 case TP_HKEY_EV_OPTDRV_EJ:
4037 /* FIXME: kick libata if SATA link offline */
4045 /* 0x4000-0x4FFF: dock-related events */
4046 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4050 /* 0x5000-0x5FFF: human interface helpers */
4051 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4055 /* 0x6000-0x6FFF: thermal alarms/notices and
4056 * keyboard events */
4057 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4061 /* 0x7000-0x7FFF: misc */
4062 if (tp_features.hotkey_wlsw &&
4063 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4064 tpacpi_send_radiosw_update();
4069 fallthrough; /* to default */
4074 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4075 pr_notice("please report the conditions when this event happened to %s\n",
4079 /* netlink events */
4080 if (!ignore_acpi_ev && send_acpi_ev) {
4081 acpi_bus_generate_netlink_event(
4082 ibm->acpi->device->pnp.device_class,
4083 dev_name(&ibm->acpi->device->dev),
4089 static void hotkey_suspend(void)
4091 /* Do these on suspend, we get the events on early resume! */
4092 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4093 hotkey_autosleep_ack = 0;
4095 /* save previous mode of adaptive keyboard of X1 Carbon */
4096 if (tp_features.has_adaptive_kbd) {
4097 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4099 pr_err("Cannot read adaptive keyboard mode.\n");
4104 static void hotkey_resume(void)
4106 tpacpi_disable_brightness_delay();
4108 if (hotkey_status_set(true) < 0 ||
4109 hotkey_mask_set(hotkey_acpi_mask) < 0)
4110 pr_err("error while attempting to reset the event firmware interface\n");
4112 tpacpi_send_radiosw_update();
4113 tpacpi_input_send_tabletsw();
4114 hotkey_tablet_mode_notify_change();
4115 hotkey_wakeup_reason_notify_change();
4116 hotkey_wakeup_hotunplug_complete_notify_change();
4117 hotkey_poll_setup_safe(false);
4119 /* restore previous mode of adapive keyboard of X1 Carbon */
4120 if (tp_features.has_adaptive_kbd) {
4121 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4122 adaptive_keyboard_prev_mode)) {
4123 pr_err("Cannot set adaptive keyboard mode.\n");
4128 /* procfs -------------------------------------------------------------- */
4129 static int hotkey_read(struct seq_file *m)
4133 if (!tp_features.hotkey) {
4134 seq_printf(m, "status:\t\tnot supported\n");
4138 if (mutex_lock_killable(&hotkey_mutex))
4139 return -ERESTARTSYS;
4140 res = hotkey_status_get(&status);
4142 res = hotkey_mask_get();
4143 mutex_unlock(&hotkey_mutex);
4147 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4148 if (hotkey_all_mask) {
4149 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4150 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4152 seq_printf(m, "mask:\t\tnot supported\n");
4153 seq_printf(m, "commands:\tenable, disable, reset\n");
4159 static void hotkey_enabledisable_warn(bool enable)
4161 tpacpi_log_usertask("procfs hotkey enable/disable");
4162 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4163 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4164 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4167 static int hotkey_write(char *buf)
4173 if (!tp_features.hotkey)
4176 if (mutex_lock_killable(&hotkey_mutex))
4177 return -ERESTARTSYS;
4179 mask = hotkey_user_mask;
4182 while ((cmd = strsep(&buf, ","))) {
4183 if (strlencmp(cmd, "enable") == 0) {
4184 hotkey_enabledisable_warn(1);
4185 } else if (strlencmp(cmd, "disable") == 0) {
4186 hotkey_enabledisable_warn(0);
4188 } else if (strlencmp(cmd, "reset") == 0) {
4189 mask = (hotkey_all_mask | hotkey_source_mask)
4190 & ~hotkey_reserved_mask;
4191 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4193 } else if (sscanf(cmd, "%x", &mask) == 1) {
4202 tpacpi_disclose_usertask("procfs hotkey",
4203 "set mask to 0x%08x\n", mask);
4204 res = hotkey_user_mask_set(mask);
4208 mutex_unlock(&hotkey_mutex);
4212 static const struct acpi_device_id ibm_htk_device_ids[] = {
4213 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4214 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4215 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4219 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4220 .hid = ibm_htk_device_ids,
4221 .notify = hotkey_notify,
4222 .handle = &hkey_handle,
4223 .type = ACPI_DEVICE_NOTIFY,
4226 static struct ibm_struct hotkey_driver_data = {
4228 .read = hotkey_read,
4229 .write = hotkey_write,
4230 .exit = hotkey_exit,
4231 .resume = hotkey_resume,
4232 .suspend = hotkey_suspend,
4233 .acpi = &ibm_hotkey_acpidriver,
4236 /*************************************************************************
4237 * Bluetooth subdriver
4241 /* ACPI GBDC/SBDC bits */
4242 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4243 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4244 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4245 0 = disable, 1 = enable */
4249 /* ACPI \BLTH commands */
4250 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4251 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4252 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4253 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4254 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4257 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4259 static int bluetooth_get_status(void)
4263 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4264 if (dbg_bluetoothemul)
4265 return (tpacpi_bluetooth_emulstate) ?
4266 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4269 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4272 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4273 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4276 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4280 vdbg_printk(TPACPI_DBG_RFKILL,
4281 "will attempt to %s bluetooth\n",
4282 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4284 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4285 if (dbg_bluetoothemul) {
4286 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4291 if (state == TPACPI_RFK_RADIO_ON)
4292 status = TP_ACPI_BLUETOOTH_RADIOSSW
4293 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4297 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4303 /* sysfs bluetooth enable ---------------------------------------------- */
4304 static ssize_t bluetooth_enable_show(struct device *dev,
4305 struct device_attribute *attr,
4308 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4312 static ssize_t bluetooth_enable_store(struct device *dev,
4313 struct device_attribute *attr,
4314 const char *buf, size_t count)
4316 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4320 static DEVICE_ATTR_RW(bluetooth_enable);
4322 /* --------------------------------------------------------------------- */
4324 static struct attribute *bluetooth_attributes[] = {
4325 &dev_attr_bluetooth_enable.attr,
4329 static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
4330 struct attribute *attr, int n)
4332 return tp_features.bluetooth ? attr->mode : 0;
4335 static const struct attribute_group bluetooth_attr_group = {
4336 .is_visible = bluetooth_attr_is_visible,
4337 .attrs = bluetooth_attributes,
4340 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4341 .get_status = bluetooth_get_status,
4342 .set_status = bluetooth_set_status,
4345 static void bluetooth_shutdown(void)
4347 /* Order firmware to save current state to NVRAM */
4348 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4349 TP_ACPI_BLTH_SAVE_STATE))
4350 pr_notice("failed to save bluetooth state to NVRAM\n");
4352 vdbg_printk(TPACPI_DBG_RFKILL,
4353 "bluetooth state saved to NVRAM\n");
4356 static void bluetooth_exit(void)
4358 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4359 bluetooth_shutdown();
4362 static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4364 .ident = "ThinkPad E485",
4366 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4367 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4371 .ident = "ThinkPad E585",
4373 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4374 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4378 .ident = "ThinkPad A285 - 20MW",
4380 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4381 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4385 .ident = "ThinkPad A285 - 20MX",
4387 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4388 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4392 .ident = "ThinkPad A485 - 20MU",
4394 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4395 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4399 .ident = "ThinkPad A485 - 20MV",
4401 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4402 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4408 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4409 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4410 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4411 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4416 static int __init have_bt_fwbug(void)
4419 * Some AMD based ThinkPads have a firmware bug that calling
4420 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4422 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4423 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4424 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4430 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4435 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4436 "initializing bluetooth subdriver\n");
4438 TPACPI_ACPIHANDLE_INIT(hkey);
4440 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4441 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4442 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4443 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4445 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4446 "bluetooth is %s, status 0x%02x\n",
4447 str_supported(tp_features.bluetooth),
4450 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4451 if (dbg_bluetoothemul) {
4452 tp_features.bluetooth = 1;
4453 pr_info("bluetooth switch emulation enabled\n");
4456 if (tp_features.bluetooth &&
4457 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4458 /* no bluetooth hardware present in system */
4459 tp_features.bluetooth = 0;
4460 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4461 "bluetooth hardware not installed\n");
4464 if (!tp_features.bluetooth)
4467 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4468 &bluetooth_tprfk_ops,
4469 RFKILL_TYPE_BLUETOOTH,
4470 TPACPI_RFK_BLUETOOTH_SW_NAME,
4475 /* procfs -------------------------------------------------------------- */
4476 static int bluetooth_read(struct seq_file *m)
4478 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4481 static int bluetooth_write(char *buf)
4483 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4486 static struct ibm_struct bluetooth_driver_data = {
4487 .name = "bluetooth",
4488 .read = bluetooth_read,
4489 .write = bluetooth_write,
4490 .exit = bluetooth_exit,
4491 .shutdown = bluetooth_shutdown,
4494 /*************************************************************************
4499 /* ACPI GWAN/SWAN bits */
4500 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4501 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4502 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4503 0 = disable, 1 = enable */
4506 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4508 static int wan_get_status(void)
4512 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4514 return (tpacpi_wwan_emulstate) ?
4515 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4518 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4521 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4522 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4525 static int wan_set_status(enum tpacpi_rfkill_state state)
4529 vdbg_printk(TPACPI_DBG_RFKILL,
4530 "will attempt to %s wwan\n",
4531 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4533 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4535 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4540 if (state == TPACPI_RFK_RADIO_ON)
4541 status = TP_ACPI_WANCARD_RADIOSSW
4542 | TP_ACPI_WANCARD_RESUMECTRL;
4546 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4552 /* sysfs wan enable ---------------------------------------------------- */
4553 static ssize_t wan_enable_show(struct device *dev,
4554 struct device_attribute *attr,
4557 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4561 static ssize_t wan_enable_store(struct device *dev,
4562 struct device_attribute *attr,
4563 const char *buf, size_t count)
4565 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4569 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4570 wan_enable_show, wan_enable_store);
4572 /* --------------------------------------------------------------------- */
4574 static struct attribute *wan_attributes[] = {
4575 &dev_attr_wwan_enable.attr,
4579 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4582 return tp_features.wan ? attr->mode : 0;
4585 static const struct attribute_group wan_attr_group = {
4586 .is_visible = wan_attr_is_visible,
4587 .attrs = wan_attributes,
4590 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4591 .get_status = wan_get_status,
4592 .set_status = wan_set_status,
4595 static void wan_shutdown(void)
4597 /* Order firmware to save current state to NVRAM */
4598 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4599 TP_ACPI_WGSV_SAVE_STATE))
4600 pr_notice("failed to save WWAN state to NVRAM\n");
4602 vdbg_printk(TPACPI_DBG_RFKILL,
4603 "WWAN state saved to NVRAM\n");
4606 static void wan_exit(void)
4608 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4612 static int __init wan_init(struct ibm_init_struct *iibm)
4617 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4618 "initializing wan subdriver\n");
4620 TPACPI_ACPIHANDLE_INIT(hkey);
4622 tp_features.wan = hkey_handle &&
4623 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4625 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4626 "wan is %s, status 0x%02x\n",
4627 str_supported(tp_features.wan),
4630 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4632 tp_features.wan = 1;
4633 pr_info("wwan switch emulation enabled\n");
4636 if (tp_features.wan &&
4637 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4638 /* no wan hardware present in system */
4639 tp_features.wan = 0;
4640 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4641 "wan hardware not installed\n");
4644 if (!tp_features.wan)
4647 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4650 TPACPI_RFK_WWAN_SW_NAME,
4655 /* procfs -------------------------------------------------------------- */
4656 static int wan_read(struct seq_file *m)
4658 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4661 static int wan_write(char *buf)
4663 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4666 static struct ibm_struct wan_driver_data = {
4671 .shutdown = wan_shutdown,
4674 /*************************************************************************
4679 /* ACPI GUWB/SUWB bits */
4680 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4681 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4684 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4686 static int uwb_get_status(void)
4690 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4692 return (tpacpi_uwb_emulstate) ?
4693 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4696 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4699 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4700 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4703 static int uwb_set_status(enum tpacpi_rfkill_state state)
4707 vdbg_printk(TPACPI_DBG_RFKILL,
4708 "will attempt to %s UWB\n",
4709 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4711 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4713 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4718 if (state == TPACPI_RFK_RADIO_ON)
4719 status = TP_ACPI_UWB_RADIOSSW;
4723 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4729 /* --------------------------------------------------------------------- */
4731 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4732 .get_status = uwb_get_status,
4733 .set_status = uwb_set_status,
4736 static void uwb_exit(void)
4738 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4741 static int __init uwb_init(struct ibm_init_struct *iibm)
4746 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4747 "initializing uwb subdriver\n");
4749 TPACPI_ACPIHANDLE_INIT(hkey);
4751 tp_features.uwb = hkey_handle &&
4752 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4754 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4755 "uwb is %s, status 0x%02x\n",
4756 str_supported(tp_features.uwb),
4759 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4761 tp_features.uwb = 1;
4762 pr_info("uwb switch emulation enabled\n");
4765 if (tp_features.uwb &&
4766 !(status & TP_ACPI_UWB_HWPRESENT)) {
4767 /* no uwb hardware present in system */
4768 tp_features.uwb = 0;
4769 dbg_printk(TPACPI_DBG_INIT,
4770 "uwb hardware not installed\n");
4773 if (!tp_features.uwb)
4776 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4779 TPACPI_RFK_UWB_SW_NAME,
4784 static struct ibm_struct uwb_driver_data = {
4787 .flags.experimental = 1,
4790 /*************************************************************************
4794 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4796 enum video_access_mode {
4797 TPACPI_VIDEO_NONE = 0,
4798 TPACPI_VIDEO_570, /* 570 */
4799 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4800 TPACPI_VIDEO_NEW, /* all others */
4803 enum { /* video status flags, based on VIDEO_570 */
4804 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4805 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4806 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4809 enum { /* TPACPI_VIDEO_570 constants */
4810 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4811 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4812 * video_status_flags */
4813 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4814 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4817 static enum video_access_mode video_supported;
4818 static int video_orig_autosw;
4820 static int video_autosw_get(void);
4821 static int video_autosw_set(int enable);
4823 TPACPI_HANDLE(vid, root,
4824 "\\_SB.PCI.AGP.VGA", /* 570 */
4825 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4826 "\\_SB.PCI0.VID0", /* 770e */
4827 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4828 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4829 "\\_SB.PCI0.AGP.VID", /* all others */
4832 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4834 static int __init video_init(struct ibm_init_struct *iibm)
4838 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4840 TPACPI_ACPIHANDLE_INIT(vid);
4841 if (tpacpi_is_ibm())
4842 TPACPI_ACPIHANDLE_INIT(vid2);
4844 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4845 /* G41, assume IVGA doesn't change */
4846 vid_handle = vid2_handle;
4849 /* video switching not supported on R30, R31 */
4850 video_supported = TPACPI_VIDEO_NONE;
4851 else if (tpacpi_is_ibm() &&
4852 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4854 video_supported = TPACPI_VIDEO_570;
4855 else if (tpacpi_is_ibm() &&
4856 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4857 /* 600e/x, 770e, 770x */
4858 video_supported = TPACPI_VIDEO_770;
4861 video_supported = TPACPI_VIDEO_NEW;
4863 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4864 str_supported(video_supported != TPACPI_VIDEO_NONE),
4867 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
4870 static void video_exit(void)
4872 dbg_printk(TPACPI_DBG_EXIT,
4873 "restoring original video autoswitch mode\n");
4874 if (video_autosw_set(video_orig_autosw))
4875 pr_err("error while trying to restore original video autoswitch mode\n");
4878 static int video_outputsw_get(void)
4883 switch (video_supported) {
4884 case TPACPI_VIDEO_570:
4885 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4886 TP_ACPI_VIDEO_570_PHSCMD))
4888 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4890 case TPACPI_VIDEO_770:
4891 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4894 status |= TP_ACPI_VIDEO_S_LCD;
4895 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4898 status |= TP_ACPI_VIDEO_S_CRT;
4900 case TPACPI_VIDEO_NEW:
4901 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4902 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4905 status |= TP_ACPI_VIDEO_S_CRT;
4907 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4908 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4911 status |= TP_ACPI_VIDEO_S_LCD;
4912 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4915 status |= TP_ACPI_VIDEO_S_DVI;
4924 static int video_outputsw_set(int status)
4929 switch (video_supported) {
4930 case TPACPI_VIDEO_570:
4931 res = acpi_evalf(NULL, NULL,
4932 "\\_SB.PHS2", "vdd",
4933 TP_ACPI_VIDEO_570_PHS2CMD,
4934 status | TP_ACPI_VIDEO_570_PHS2SET);
4936 case TPACPI_VIDEO_770:
4937 autosw = video_autosw_get();
4941 res = video_autosw_set(1);
4944 res = acpi_evalf(vid_handle, NULL,
4945 "ASWT", "vdd", status * 0x100, 0);
4946 if (!autosw && video_autosw_set(autosw)) {
4947 pr_err("video auto-switch left enabled due to error\n");
4951 case TPACPI_VIDEO_NEW:
4952 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4953 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4959 return (res) ? 0 : -EIO;
4962 static int video_autosw_get(void)
4966 switch (video_supported) {
4967 case TPACPI_VIDEO_570:
4968 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4971 case TPACPI_VIDEO_770:
4972 case TPACPI_VIDEO_NEW:
4973 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4983 static int video_autosw_set(int enable)
4985 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4990 static int video_outputsw_cycle(void)
4992 int autosw = video_autosw_get();
4998 switch (video_supported) {
4999 case TPACPI_VIDEO_570:
5000 res = video_autosw_set(1);
5003 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5005 case TPACPI_VIDEO_770:
5006 case TPACPI_VIDEO_NEW:
5007 res = video_autosw_set(1);
5010 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5015 if (!autosw && video_autosw_set(autosw)) {
5016 pr_err("video auto-switch left enabled due to error\n");
5020 return (res) ? 0 : -EIO;
5023 static int video_expand_toggle(void)
5025 switch (video_supported) {
5026 case TPACPI_VIDEO_570:
5027 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5029 case TPACPI_VIDEO_770:
5030 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5032 case TPACPI_VIDEO_NEW:
5033 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5041 static int video_read(struct seq_file *m)
5045 if (video_supported == TPACPI_VIDEO_NONE) {
5046 seq_printf(m, "status:\t\tnot supported\n");
5050 /* Even reads can crash X.org, so... */
5051 if (!capable(CAP_SYS_ADMIN))
5054 status = video_outputsw_get();
5058 autosw = video_autosw_get();
5062 seq_printf(m, "status:\t\tsupported\n");
5063 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5064 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5065 if (video_supported == TPACPI_VIDEO_NEW)
5066 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5067 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5068 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5069 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5070 if (video_supported == TPACPI_VIDEO_NEW)
5071 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5072 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5073 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5078 static int video_write(char *buf)
5081 int enable, disable, status;
5084 if (video_supported == TPACPI_VIDEO_NONE)
5087 /* Even reads can crash X.org, let alone writes... */
5088 if (!capable(CAP_SYS_ADMIN))
5094 while ((cmd = strsep(&buf, ","))) {
5095 if (strlencmp(cmd, "lcd_enable") == 0) {
5096 enable |= TP_ACPI_VIDEO_S_LCD;
5097 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5098 disable |= TP_ACPI_VIDEO_S_LCD;
5099 } else if (strlencmp(cmd, "crt_enable") == 0) {
5100 enable |= TP_ACPI_VIDEO_S_CRT;
5101 } else if (strlencmp(cmd, "crt_disable") == 0) {
5102 disable |= TP_ACPI_VIDEO_S_CRT;
5103 } else if (video_supported == TPACPI_VIDEO_NEW &&
5104 strlencmp(cmd, "dvi_enable") == 0) {
5105 enable |= TP_ACPI_VIDEO_S_DVI;
5106 } else if (video_supported == TPACPI_VIDEO_NEW &&
5107 strlencmp(cmd, "dvi_disable") == 0) {
5108 disable |= TP_ACPI_VIDEO_S_DVI;
5109 } else if (strlencmp(cmd, "auto_enable") == 0) {
5110 res = video_autosw_set(1);
5113 } else if (strlencmp(cmd, "auto_disable") == 0) {
5114 res = video_autosw_set(0);
5117 } else if (strlencmp(cmd, "video_switch") == 0) {
5118 res = video_outputsw_cycle();
5121 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5122 res = video_expand_toggle();
5129 if (enable || disable) {
5130 status = video_outputsw_get();
5133 res = video_outputsw_set((status & ~disable) | enable);
5141 static struct ibm_struct video_driver_data = {
5144 .write = video_write,
5148 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5150 /*************************************************************************
5151 * Keyboard backlight subdriver
5154 static enum led_brightness kbdlight_brightness;
5155 static DEFINE_MUTEX(kbdlight_mutex);
5157 static int kbdlight_set_level(int level)
5164 mutex_lock(&kbdlight_mutex);
5166 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5169 kbdlight_brightness = level;
5171 mutex_unlock(&kbdlight_mutex);
5176 static int kbdlight_get_level(void)
5183 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5189 return status & 0x3;
5192 static bool kbdlight_is_supported(void)
5199 if (!acpi_has_method(hkey_handle, "MLCG")) {
5200 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5204 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5205 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5210 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5214 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5216 * Guessed test for keyboard backlight:
5218 * Machines with backlight keyboard return:
5219 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5220 * b110100010010000000XX - ThinkPad x230
5221 * b010100000010000000XX - ThinkPad x240
5222 * b010100000010000000XX - ThinkPad W541
5223 * (XX is current backlight level)
5225 * Machines without backlight keyboard return:
5226 * b10100001000000000000 - ThinkPad x230
5227 * b10110001000000000000 - ThinkPad E430
5228 * b00000000000000000000 - ThinkPad E450
5230 * Candidate BITs for detection test (XOR):
5231 * b01000000001000000000
5234 return status & BIT(9);
5237 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5238 enum led_brightness brightness)
5240 return kbdlight_set_level(brightness);
5243 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5247 level = kbdlight_get_level();
5254 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5256 .name = "tpacpi::kbd_backlight",
5257 .max_brightness = 2,
5258 .flags = LED_BRIGHT_HW_CHANGED,
5259 .brightness_set_blocking = &kbdlight_sysfs_set,
5260 .brightness_get = &kbdlight_sysfs_get,
5264 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5268 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5270 TPACPI_ACPIHANDLE_INIT(hkey);
5272 if (!kbdlight_is_supported()) {
5273 tp_features.kbdlight = 0;
5274 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5278 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5279 tp_features.kbdlight = 1;
5281 rc = led_classdev_register(&tpacpi_pdev->dev,
5282 &tpacpi_led_kbdlight.led_classdev);
5284 tp_features.kbdlight = 0;
5288 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5289 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5293 static void kbdlight_exit(void)
5295 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5298 static int kbdlight_set_level_and_update(int level)
5301 struct led_classdev *led_cdev;
5303 ret = kbdlight_set_level(level);
5304 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5306 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5307 led_cdev->brightness = level;
5312 static int kbdlight_read(struct seq_file *m)
5316 if (!tp_features.kbdlight) {
5317 seq_printf(m, "status:\t\tnot supported\n");
5319 level = kbdlight_get_level();
5321 seq_printf(m, "status:\t\terror %d\n", level);
5323 seq_printf(m, "status:\t\t%d\n", level);
5324 seq_printf(m, "commands:\t0, 1, 2\n");
5330 static int kbdlight_write(char *buf)
5333 int res, level = -EINVAL;
5335 if (!tp_features.kbdlight)
5338 while ((cmd = strsep(&buf, ","))) {
5339 res = kstrtoint(cmd, 10, &level);
5344 if (level >= 3 || level < 0)
5347 return kbdlight_set_level_and_update(level);
5350 static void kbdlight_suspend(void)
5352 struct led_classdev *led_cdev;
5354 if (!tp_features.kbdlight)
5357 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5358 led_update_brightness(led_cdev);
5359 led_classdev_suspend(led_cdev);
5362 static void kbdlight_resume(void)
5364 if (!tp_features.kbdlight)
5367 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5370 static struct ibm_struct kbdlight_driver_data = {
5372 .read = kbdlight_read,
5373 .write = kbdlight_write,
5374 .suspend = kbdlight_suspend,
5375 .resume = kbdlight_resume,
5376 .exit = kbdlight_exit,
5379 /*************************************************************************
5380 * Light (thinklight) subdriver
5383 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5384 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5386 static int light_get_status(void)
5390 if (tp_features.light_status) {
5391 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5399 static int light_set_status(int status)
5403 if (tp_features.light) {
5405 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5407 TP_CMOS_THINKLIGHT_ON :
5408 TP_CMOS_THINKLIGHT_OFF);
5410 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5413 return (rc) ? 0 : -EIO;
5419 static int light_sysfs_set(struct led_classdev *led_cdev,
5420 enum led_brightness brightness)
5422 return light_set_status((brightness != LED_OFF) ?
5423 TPACPI_LED_ON : TPACPI_LED_OFF);
5426 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5428 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5431 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5433 .name = "tpacpi::thinklight",
5434 .brightness_set_blocking = &light_sysfs_set,
5435 .brightness_get = &light_sysfs_get,
5439 static int __init light_init(struct ibm_init_struct *iibm)
5443 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5445 if (tpacpi_is_ibm()) {
5446 TPACPI_ACPIHANDLE_INIT(ledb);
5447 TPACPI_ACPIHANDLE_INIT(lght);
5449 TPACPI_ACPIHANDLE_INIT(cmos);
5451 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5452 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5454 if (tp_features.light)
5455 /* light status not supported on
5456 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5457 tp_features.light_status =
5458 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5460 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5461 str_supported(tp_features.light),
5462 str_supported(tp_features.light_status));
5464 if (!tp_features.light)
5467 rc = led_classdev_register(&tpacpi_pdev->dev,
5468 &tpacpi_led_thinklight.led_classdev);
5471 tp_features.light = 0;
5472 tp_features.light_status = 0;
5480 static void light_exit(void)
5482 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5485 static int light_read(struct seq_file *m)
5489 if (!tp_features.light) {
5490 seq_printf(m, "status:\t\tnot supported\n");
5491 } else if (!tp_features.light_status) {
5492 seq_printf(m, "status:\t\tunknown\n");
5493 seq_printf(m, "commands:\ton, off\n");
5495 status = light_get_status();
5498 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5499 seq_printf(m, "commands:\ton, off\n");
5505 static int light_write(char *buf)
5510 if (!tp_features.light)
5513 while ((cmd = strsep(&buf, ","))) {
5514 if (strlencmp(cmd, "on") == 0) {
5516 } else if (strlencmp(cmd, "off") == 0) {
5522 return light_set_status(newstatus);
5525 static struct ibm_struct light_driver_data = {
5528 .write = light_write,
5532 /*************************************************************************
5536 /* sysfs cmos_command -------------------------------------------------- */
5537 static ssize_t cmos_command_store(struct device *dev,
5538 struct device_attribute *attr,
5539 const char *buf, size_t count)
5541 unsigned long cmos_cmd;
5544 if (parse_strtoul(buf, 21, &cmos_cmd))
5547 res = issue_thinkpad_cmos_command(cmos_cmd);
5548 return (res) ? res : count;
5551 static DEVICE_ATTR_WO(cmos_command);
5553 static struct attribute *cmos_attributes[] = {
5554 &dev_attr_cmos_command.attr,
5558 static umode_t cmos_attr_is_visible(struct kobject *kobj,
5559 struct attribute *attr, int n)
5561 return cmos_handle ? attr->mode : 0;
5564 static const struct attribute_group cmos_attr_group = {
5565 .is_visible = cmos_attr_is_visible,
5566 .attrs = cmos_attributes,
5569 /* --------------------------------------------------------------------- */
5571 static int __init cmos_init(struct ibm_init_struct *iibm)
5573 vdbg_printk(TPACPI_DBG_INIT,
5574 "initializing cmos commands subdriver\n");
5576 TPACPI_ACPIHANDLE_INIT(cmos);
5578 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5579 str_supported(cmos_handle != NULL));
5581 return cmos_handle ? 0 : -ENODEV;
5584 static int cmos_read(struct seq_file *m)
5586 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5587 R30, R31, T20-22, X20-21 */
5589 seq_printf(m, "status:\t\tnot supported\n");
5591 seq_printf(m, "status:\t\tsupported\n");
5592 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5598 static int cmos_write(char *buf)
5603 while ((cmd = strsep(&buf, ","))) {
5604 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5605 cmos_cmd >= 0 && cmos_cmd <= 21) {
5610 res = issue_thinkpad_cmos_command(cmos_cmd);
5618 static struct ibm_struct cmos_driver_data = {
5621 .write = cmos_write,
5624 /*************************************************************************
5628 enum led_access_mode {
5629 TPACPI_LED_NONE = 0,
5630 TPACPI_LED_570, /* 570 */
5631 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5632 TPACPI_LED_NEW, /* all others */
5635 enum { /* For TPACPI_LED_OLD */
5636 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5637 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5638 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5641 static enum led_access_mode led_supported;
5643 static acpi_handle led_handle;
5645 #define TPACPI_LED_NUMLEDS 16
5646 static struct tpacpi_led_classdev *tpacpi_leds;
5647 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5648 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5649 /* there's a limit of 19 chars + NULL before 2.6.26 */
5651 "tpacpi:orange:batt",
5652 "tpacpi:green:batt",
5653 "tpacpi::dock_active",
5654 "tpacpi::bay_active",
5655 "tpacpi::dock_batt",
5656 "tpacpi::unknown_led",
5658 "tpacpi::dock_status1",
5659 "tpacpi::dock_status2",
5660 "tpacpi::lid_logo_dot",
5661 "tpacpi::unknown_led3",
5662 "tpacpi::thinkvantage",
5664 #define TPACPI_SAFE_LEDS 0x1481U
5666 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5668 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5671 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5675 static int led_get_status(const unsigned int led)
5678 enum led_status_t led_s;
5680 switch (led_supported) {
5681 case TPACPI_LED_570:
5682 if (!acpi_evalf(ec_handle,
5683 &status, "GLED", "dd", 1 << led))
5685 led_s = (status == 0) ?
5690 tpacpi_led_state_cache[led] = led_s;
5699 static int led_set_status(const unsigned int led,
5700 const enum led_status_t ledstatus)
5702 /* off, on, blink. Index is led_status_t */
5703 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5704 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5708 switch (led_supported) {
5709 case TPACPI_LED_570:
5711 if (unlikely(led > 7))
5713 if (unlikely(tpacpi_is_led_restricted(led)))
5715 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5716 (1 << led), led_sled_arg1[ledstatus]))
5719 case TPACPI_LED_OLD:
5720 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5721 if (unlikely(led > 7))
5723 if (unlikely(tpacpi_is_led_restricted(led)))
5725 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5727 rc = ec_write(TPACPI_LED_EC_HLBL,
5728 (ledstatus == TPACPI_LED_BLINK) << led);
5730 rc = ec_write(TPACPI_LED_EC_HLCL,
5731 (ledstatus != TPACPI_LED_OFF) << led);
5733 case TPACPI_LED_NEW:
5735 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5737 if (unlikely(tpacpi_is_led_restricted(led)))
5739 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5740 led, led_led_arg1[ledstatus]))
5748 tpacpi_led_state_cache[led] = ledstatus;
5753 static int led_sysfs_set(struct led_classdev *led_cdev,
5754 enum led_brightness brightness)
5756 struct tpacpi_led_classdev *data = container_of(led_cdev,
5757 struct tpacpi_led_classdev, led_classdev);
5758 enum led_status_t new_state;
5760 if (brightness == LED_OFF)
5761 new_state = TPACPI_LED_OFF;
5762 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5763 new_state = TPACPI_LED_ON;
5765 new_state = TPACPI_LED_BLINK;
5767 return led_set_status(data->led, new_state);
5770 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5771 unsigned long *delay_on, unsigned long *delay_off)
5773 struct tpacpi_led_classdev *data = container_of(led_cdev,
5774 struct tpacpi_led_classdev, led_classdev);
5776 /* Can we choose the flash rate? */
5777 if (*delay_on == 0 && *delay_off == 0) {
5778 /* yes. set them to the hardware blink rate (1 Hz) */
5779 *delay_on = 500; /* ms */
5780 *delay_off = 500; /* ms */
5781 } else if ((*delay_on != 500) || (*delay_off != 500))
5784 return led_set_status(data->led, TPACPI_LED_BLINK);
5787 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5791 struct tpacpi_led_classdev *data = container_of(led_cdev,
5792 struct tpacpi_led_classdev, led_classdev);
5794 rc = led_get_status(data->led);
5796 if (rc == TPACPI_LED_OFF || rc < 0)
5797 rc = LED_OFF; /* no error handling in led class :( */
5804 static void led_exit(void)
5808 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5809 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5814 static int __init tpacpi_init_led(unsigned int led)
5816 /* LEDs with no name don't get registered */
5817 if (!tpacpi_led_names[led])
5820 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5821 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5822 if (led_supported == TPACPI_LED_570)
5823 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5825 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5826 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
5827 tpacpi_leds[led].led = led;
5829 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5832 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5833 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5834 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5835 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5837 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5838 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5839 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5840 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5841 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5842 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5843 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5844 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5846 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5847 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5848 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5849 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5850 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5852 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5853 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5854 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5855 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5857 /* (1) - may have excess leds enabled on MSB */
5859 /* Defaults (order matters, keep last, don't reorder!) */
5861 .vendor = PCI_VENDOR_ID_LENOVO,
5862 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5865 { /* IBM ThinkPads with no EC version string */
5866 .vendor = PCI_VENDOR_ID_IBM,
5867 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5870 { /* IBM ThinkPads with EC version string */
5871 .vendor = PCI_VENDOR_ID_IBM,
5872 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5877 static enum led_access_mode __init led_init_detect_mode(void)
5881 if (tpacpi_is_ibm()) {
5883 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5884 if (ACPI_SUCCESS(status))
5885 return TPACPI_LED_570;
5887 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5888 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5889 if (ACPI_SUCCESS(status))
5890 return TPACPI_LED_OLD;
5894 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5895 if (ACPI_SUCCESS(status))
5896 return TPACPI_LED_NEW;
5898 /* R30, R31, and unknown firmwares */
5900 return TPACPI_LED_NONE;
5903 static int __init led_init(struct ibm_init_struct *iibm)
5907 unsigned long useful_leds;
5909 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5911 led_supported = led_init_detect_mode();
5913 if (led_supported != TPACPI_LED_NONE) {
5914 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5915 ARRAY_SIZE(led_useful_qtable));
5919 led_supported = TPACPI_LED_NONE;
5923 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5924 str_supported(led_supported), led_supported);
5926 if (led_supported == TPACPI_LED_NONE)
5929 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
5932 pr_err("Out of memory for LED data\n");
5936 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5937 tpacpi_leds[i].led = -1;
5939 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
5940 rc = tpacpi_init_led(i);
5948 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5949 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
5954 #define str_led_status(s) \
5955 ((s) == TPACPI_LED_OFF ? "off" : \
5956 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5958 static int led_read(struct seq_file *m)
5960 if (!led_supported) {
5961 seq_printf(m, "status:\t\tnot supported\n");
5964 seq_printf(m, "status:\t\tsupported\n");
5966 if (led_supported == TPACPI_LED_570) {
5969 for (i = 0; i < 8; i++) {
5970 status = led_get_status(i);
5973 seq_printf(m, "%d:\t\t%s\n",
5974 i, str_led_status(status));
5978 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5983 static int led_write(char *buf)
5987 enum led_status_t s;
5992 while ((cmd = strsep(&buf, ","))) {
5993 if (sscanf(cmd, "%d", &led) != 1)
5996 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
5999 if (tpacpi_leds[led].led < 0)
6002 if (strstr(cmd, "off")) {
6004 } else if (strstr(cmd, "on")) {
6006 } else if (strstr(cmd, "blink")) {
6007 s = TPACPI_LED_BLINK;
6012 rc = led_set_status(led, s);
6020 static struct ibm_struct led_driver_data = {
6027 /*************************************************************************
6031 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6033 #define TPACPI_BEEP_Q1 0x0001
6035 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6036 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6037 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6040 static int __init beep_init(struct ibm_init_struct *iibm)
6042 unsigned long quirks;
6044 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6046 TPACPI_ACPIHANDLE_INIT(beep);
6048 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6049 str_supported(beep_handle != NULL));
6051 quirks = tpacpi_check_quirks(beep_quirk_table,
6052 ARRAY_SIZE(beep_quirk_table));
6054 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6056 return (beep_handle) ? 0 : -ENODEV;
6059 static int beep_read(struct seq_file *m)
6062 seq_printf(m, "status:\t\tnot supported\n");
6064 seq_printf(m, "status:\t\tsupported\n");
6065 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6071 static int beep_write(char *buf)
6079 while ((cmd = strsep(&buf, ","))) {
6080 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6081 beep_cmd >= 0 && beep_cmd <= 17) {
6085 if (tp_features.beep_needs_two_args) {
6086 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6090 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6099 static struct ibm_struct beep_driver_data = {
6102 .write = beep_write,
6105 /*************************************************************************
6109 enum thermal_access_mode {
6110 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6111 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6112 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6113 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6114 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6117 enum { /* TPACPI_THERMAL_TPEC_* */
6118 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6119 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6120 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6121 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6123 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6127 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6128 struct ibm_thermal_sensors_struct {
6129 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6132 static enum thermal_access_mode thermal_read_mode;
6133 static bool thermal_use_labels;
6135 /* idx is zero-based */
6136 static int thermal_get_sensor(int idx, s32 *value)
6142 t = TP_EC_THERMAL_TMP0;
6144 switch (thermal_read_mode) {
6145 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6146 case TPACPI_THERMAL_TPEC_16:
6147 if (idx >= 8 && idx <= 15) {
6148 t = TP_EC_THERMAL_TMP8;
6153 case TPACPI_THERMAL_TPEC_8:
6155 if (!acpi_ec_read(t + idx, &tmp))
6157 *value = tmp * 1000;
6162 case TPACPI_THERMAL_ACPI_UPDT:
6164 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6165 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6167 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6169 *value = (t - 2732) * 100;
6174 case TPACPI_THERMAL_ACPI_TMP07:
6176 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6177 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6179 if (t > 127 || t < -127)
6180 t = TP_EC_THERMAL_TMP_NA;
6186 case TPACPI_THERMAL_NONE:
6194 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6205 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6208 for (i = 0 ; i < n; i++) {
6209 res = thermal_get_sensor(i, &s->temp[i]);
6217 static void thermal_dump_all_sensors(void)
6220 struct ibm_thermal_sensors_struct t;
6222 n = thermal_get_sensors(&t);
6226 pr_notice("temperatures (Celsius):");
6228 for (i = 0; i < n; i++) {
6229 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6230 pr_cont(" %d", (int)(t.temp[i] / 1000));
6238 /* sysfs temp##_input -------------------------------------------------- */
6240 static ssize_t thermal_temp_input_show(struct device *dev,
6241 struct device_attribute *attr,
6244 struct sensor_device_attribute *sensor_attr =
6245 to_sensor_dev_attr(attr);
6246 int idx = sensor_attr->index;
6250 res = thermal_get_sensor(idx, &value);
6253 if (value == TPACPI_THERMAL_SENSOR_NA)
6256 return sysfs_emit(buf, "%d\n", value);
6259 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6260 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6261 thermal_temp_input_show, NULL, _idxB)
6263 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6264 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6265 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6266 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6267 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6268 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6269 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6270 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6271 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6272 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6273 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6274 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6275 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6276 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6277 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6278 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6279 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6282 #define THERMAL_ATTRS(X) \
6283 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6285 static struct attribute *thermal_temp_input_attr[] = {
6305 static umode_t thermal_attr_is_visible(struct kobject *kobj,
6306 struct attribute *attr, int n)
6308 if (thermal_read_mode == TPACPI_THERMAL_NONE)
6311 if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) ||
6312 attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) ||
6313 attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) ||
6314 attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) {
6315 if (thermal_read_mode != TPACPI_THERMAL_TPEC_16)
6322 static const struct attribute_group thermal_attr_group = {
6323 .is_visible = thermal_attr_is_visible,
6324 .attrs = thermal_temp_input_attr,
6327 #undef THERMAL_SENSOR_ATTR_TEMP
6328 #undef THERMAL_ATTRS
6330 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6332 return sysfs_emit(buf, "CPU\n");
6334 static DEVICE_ATTR_RO(temp1_label);
6336 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6338 return sysfs_emit(buf, "GPU\n");
6340 static DEVICE_ATTR_RO(temp2_label);
6342 static struct attribute *temp_label_attributes[] = {
6343 &dev_attr_temp1_label.attr,
6344 &dev_attr_temp2_label.attr,
6348 static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6349 struct attribute *attr, int n)
6351 return thermal_use_labels ? attr->mode : 0;
6354 static const struct attribute_group temp_label_attr_group = {
6355 .is_visible = temp_label_attr_is_visible,
6356 .attrs = temp_label_attributes,
6359 /* --------------------------------------------------------------------- */
6361 static int __init thermal_init(struct ibm_init_struct *iibm)
6363 u8 t, ta1, ta2, ver = 0;
6367 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6369 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6371 if (thinkpad_id.ec_model) {
6373 * Direct EC access mode: sensors at registers
6374 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6375 * non-implemented, thermal sensors return 0x80 when
6377 * The above rule is unfortunately flawed. This has been seen with
6378 * 0xC2 (power supply ID) causing thermal control problems.
6379 * The EC version can be determined by offset 0xEF and at least for
6380 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6381 * are not thermal registers.
6383 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6384 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6387 for (i = 0; i < 8; i++) {
6388 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6395 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6404 /* This is sheer paranoia, but we handle it anyway */
6406 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6407 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6409 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6410 thermal_read_mode = TPACPI_THERMAL_NONE;
6414 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6415 thermal_use_labels = true;
6419 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6422 } else if (acpi_tmp7) {
6423 if (tpacpi_is_ibm() &&
6424 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6425 /* 600e/x, 770e, 770x */
6426 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6428 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6429 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6432 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6433 thermal_read_mode = TPACPI_THERMAL_NONE;
6436 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6437 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6440 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
6443 static int thermal_read(struct seq_file *m)
6446 struct ibm_thermal_sensors_struct t;
6448 n = thermal_get_sensors(&t);
6449 if (unlikely(n < 0))
6452 seq_printf(m, "temperatures:\t");
6455 for (i = 0; i < (n - 1); i++)
6456 seq_printf(m, "%d ", t.temp[i] / 1000);
6457 seq_printf(m, "%d\n", t.temp[i] / 1000);
6459 seq_printf(m, "not supported\n");
6464 static struct ibm_struct thermal_driver_data = {
6466 .read = thermal_read,
6469 /*************************************************************************
6470 * Backlight/brightness subdriver
6473 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6476 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6477 * CMOS NVRAM byte 0x5E, bits 0-3.
6479 * EC HBRV (0x31) has the following layout
6480 * Bit 7: unknown function
6481 * Bit 6: unknown function
6482 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6483 * Bit 4: must be set to zero to avoid problems
6484 * Bit 3-0: backlight brightness level
6486 * brightness_get_raw returns status data in the HBRV layout
6488 * WARNING: The X61 has been verified to use HBRV for something else, so
6489 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6490 * testing on the very early *60 Lenovo models...
6494 TP_EC_BACKLIGHT = 0x31,
6496 /* TP_EC_BACKLIGHT bitmasks */
6497 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6498 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6499 TP_EC_BACKLIGHT_MAPSW = 0x20,
6502 enum tpacpi_brightness_access_mode {
6503 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6504 TPACPI_BRGHT_MODE_EC, /* EC control */
6505 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6506 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6507 TPACPI_BRGHT_MODE_MAX
6510 static struct backlight_device *ibm_backlight_device;
6512 static enum tpacpi_brightness_access_mode brightness_mode =
6513 TPACPI_BRGHT_MODE_MAX;
6515 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6517 static struct mutex brightness_mutex;
6519 /* NVRAM brightness access,
6520 * call with brightness_mutex held! */
6521 static unsigned int tpacpi_brightness_nvram_get(void)
6525 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6526 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6527 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6528 lnvram &= bright_maxlvl;
6533 static void tpacpi_brightness_checkpoint_nvram(void)
6538 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6541 vdbg_printk(TPACPI_DBG_BRGHT,
6542 "trying to checkpoint backlight level to NVRAM...\n");
6544 if (mutex_lock_killable(&brightness_mutex) < 0)
6547 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6549 lec &= TP_EC_BACKLIGHT_LVLMSK;
6550 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6552 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6553 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6554 /* NVRAM needs update */
6555 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6556 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6558 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6559 dbg_printk(TPACPI_DBG_BRGHT,
6560 "updated NVRAM backlight level to %u (0x%02x)\n",
6561 (unsigned int) lec, (unsigned int) b_nvram);
6563 vdbg_printk(TPACPI_DBG_BRGHT,
6564 "NVRAM backlight level already is %u (0x%02x)\n",
6565 (unsigned int) lec, (unsigned int) b_nvram);
6568 mutex_unlock(&brightness_mutex);
6572 /* call with brightness_mutex held! */
6573 static int tpacpi_brightness_get_raw(int *status)
6577 switch (brightness_mode) {
6578 case TPACPI_BRGHT_MODE_UCMS_STEP:
6579 *status = tpacpi_brightness_nvram_get();
6581 case TPACPI_BRGHT_MODE_EC:
6582 case TPACPI_BRGHT_MODE_ECNVRAM:
6583 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6592 /* call with brightness_mutex held! */
6593 /* do NOT call with illegal backlight level value */
6594 static int tpacpi_brightness_set_ec(unsigned int value)
6598 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6601 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6602 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6603 (value & TP_EC_BACKLIGHT_LVLMSK))))
6609 /* call with brightness_mutex held! */
6610 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6613 unsigned int current_value, i;
6615 current_value = tpacpi_brightness_nvram_get();
6617 if (value == current_value)
6620 cmos_cmd = (value > current_value) ?
6621 TP_CMOS_BRIGHTNESS_UP :
6622 TP_CMOS_BRIGHTNESS_DOWN;
6623 inc = (value > current_value) ? 1 : -1;
6625 for (i = current_value; i != value; i += inc)
6626 if (issue_thinkpad_cmos_command(cmos_cmd))
6632 /* May return EINTR which can always be mapped to ERESTARTSYS */
6633 static int brightness_set(unsigned int value)
6637 if (value > bright_maxlvl)
6640 vdbg_printk(TPACPI_DBG_BRGHT,
6641 "set backlight level to %d\n", value);
6643 res = mutex_lock_killable(&brightness_mutex);
6647 switch (brightness_mode) {
6648 case TPACPI_BRGHT_MODE_EC:
6649 case TPACPI_BRGHT_MODE_ECNVRAM:
6650 res = tpacpi_brightness_set_ec(value);
6652 case TPACPI_BRGHT_MODE_UCMS_STEP:
6653 res = tpacpi_brightness_set_ucmsstep(value);
6659 mutex_unlock(&brightness_mutex);
6663 /* sysfs backlight class ----------------------------------------------- */
6665 static int brightness_update_status(struct backlight_device *bd)
6667 unsigned int level =
6668 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6669 bd->props.power == FB_BLANK_UNBLANK) ?
6670 bd->props.brightness : 0;
6672 dbg_printk(TPACPI_DBG_BRGHT,
6673 "backlight: attempt to set level to %d\n",
6676 /* it is the backlight class's job (caller) to handle
6677 * EINTR and other errors properly */
6678 return brightness_set(level);
6681 static int brightness_get(struct backlight_device *bd)
6685 res = mutex_lock_killable(&brightness_mutex);
6689 res = tpacpi_brightness_get_raw(&status);
6691 mutex_unlock(&brightness_mutex);
6696 return status & TP_EC_BACKLIGHT_LVLMSK;
6699 static void tpacpi_brightness_notify_change(void)
6701 backlight_force_update(ibm_backlight_device,
6702 BACKLIGHT_UPDATE_HOTKEY);
6705 static const struct backlight_ops ibm_backlight_data = {
6706 .get_brightness = brightness_get,
6707 .update_status = brightness_update_status,
6710 /* --------------------------------------------------------------------- */
6713 * Call _BCL method of video device. On some ThinkPads this will
6714 * switch the firmware to the ACPI brightness control mode.
6717 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6719 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6720 union acpi_object *obj;
6721 struct acpi_device *device, *child;
6724 device = acpi_fetch_acpi_dev(handle);
6729 list_for_each_entry(child, &device->children, node) {
6730 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6732 if (ACPI_FAILURE(status)) {
6733 buffer.length = ACPI_ALLOCATE_BUFFER;
6737 obj = (union acpi_object *)buffer.pointer;
6738 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6739 pr_err("Unknown _BCL data, please report this to %s\n",
6743 rc = obj->package.count;
6748 kfree(buffer.pointer);
6754 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6756 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6758 acpi_handle video_device;
6761 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6763 bcl_levels = tpacpi_query_bcl_levels(video_device);
6765 tp_features.bright_acpimode = (bcl_levels > 0);
6767 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6771 * These are only useful for models that have only one possibility
6772 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6775 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6776 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6777 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6779 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6780 /* Models with ATI GPUs known to require ECNVRAM mode */
6781 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6783 /* Models with ATI GPUs that can use ECNVRAM */
6784 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6785 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6786 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6787 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6789 /* Models with Intel Extreme Graphics 2 */
6790 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6791 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6792 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6794 /* Models with Intel GMA900 */
6795 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6796 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6797 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6801 * Returns < 0 for error, otherwise sets tp_features.bright_*
6802 * and bright_maxlvl.
6804 static void __init tpacpi_detect_brightness_capabilities(void)
6808 vdbg_printk(TPACPI_DBG_INIT,
6809 "detecting firmware brightness interface capabilities\n");
6811 /* we could run a quirks check here (same table used by
6812 * brightness_init) if needed */
6815 * We always attempt to detect acpi support, so as to switch
6816 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6817 * going to publish a backlight interface
6819 b = tpacpi_check_std_acpi_brightness_support();
6829 tp_features.bright_unkfw = 1;
6830 bright_maxlvl = b - 1;
6832 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6835 static int __init brightness_init(struct ibm_init_struct *iibm)
6837 struct backlight_properties props;
6839 unsigned long quirks;
6841 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6843 mutex_init(&brightness_mutex);
6845 quirks = tpacpi_check_quirks(brightness_quirk_table,
6846 ARRAY_SIZE(brightness_quirk_table));
6848 /* tpacpi_detect_brightness_capabilities() must have run already */
6850 /* if it is unknown, we don't handle it: it wouldn't be safe */
6851 if (tp_features.bright_unkfw)
6854 if (!brightness_enable) {
6855 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6856 "brightness support disabled by module parameter\n");
6860 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6861 if (brightness_enable > 1) {
6862 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6864 } else if (brightness_enable == 1) {
6865 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6868 } else if (!tp_features.bright_acpimode) {
6869 pr_notice("ACPI backlight interface not available\n");
6873 pr_notice("ACPI native brightness control enabled\n");
6876 * Check for module parameter bogosity, note that we
6877 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6878 * able to detect "unspecified"
6880 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6883 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6884 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6885 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6886 if (quirks & TPACPI_BRGHT_Q_EC)
6887 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6889 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6891 dbg_printk(TPACPI_DBG_BRGHT,
6892 "driver auto-selected brightness_mode=%d\n",
6897 if (!tpacpi_is_ibm() &&
6898 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6899 brightness_mode == TPACPI_BRGHT_MODE_EC))
6902 if (tpacpi_brightness_get_raw(&b) < 0)
6905 memset(&props, 0, sizeof(struct backlight_properties));
6906 props.type = BACKLIGHT_PLATFORM;
6907 props.max_brightness = bright_maxlvl;
6908 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6909 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6911 &ibm_backlight_data,
6913 if (IS_ERR(ibm_backlight_device)) {
6914 int rc = PTR_ERR(ibm_backlight_device);
6915 ibm_backlight_device = NULL;
6916 pr_err("Could not register backlight device\n");
6919 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6920 "brightness is supported\n");
6922 if (quirks & TPACPI_BRGHT_Q_ASK) {
6923 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6925 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6929 /* Added by mistake in early 2007. Probably useless, but it could
6930 * be working around some unknown firmware problem where the value
6931 * read at startup doesn't match the real hardware state... so leave
6932 * it in place just in case */
6933 backlight_update_status(ibm_backlight_device);
6935 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6936 "brightness: registering brightness hotkeys as change notification\n");
6937 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6938 | TP_ACPI_HKEY_BRGHTUP_MASK
6939 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6943 static void brightness_suspend(void)
6945 tpacpi_brightness_checkpoint_nvram();
6948 static void brightness_shutdown(void)
6950 tpacpi_brightness_checkpoint_nvram();
6953 static void brightness_exit(void)
6955 if (ibm_backlight_device) {
6956 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6957 "calling backlight_device_unregister()\n");
6958 backlight_device_unregister(ibm_backlight_device);
6961 tpacpi_brightness_checkpoint_nvram();
6964 static int brightness_read(struct seq_file *m)
6968 level = brightness_get(NULL);
6970 seq_printf(m, "level:\t\tunreadable\n");
6972 seq_printf(m, "level:\t\t%d\n", level);
6973 seq_printf(m, "commands:\tup, down\n");
6974 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6981 static int brightness_write(char *buf)
6987 level = brightness_get(NULL);
6991 while ((cmd = strsep(&buf, ","))) {
6992 if (strlencmp(cmd, "up") == 0) {
6993 if (level < bright_maxlvl)
6995 } else if (strlencmp(cmd, "down") == 0) {
6998 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6999 level >= 0 && level <= bright_maxlvl) {
7005 tpacpi_disclose_usertask("procfs brightness",
7006 "set level to %d\n", level);
7009 * Now we know what the final level should be, so we try to set it.
7010 * Doing it this way makes the syscall restartable in case of EINTR
7012 rc = brightness_set(level);
7013 if (!rc && ibm_backlight_device)
7014 backlight_force_update(ibm_backlight_device,
7015 BACKLIGHT_UPDATE_SYSFS);
7016 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7019 static struct ibm_struct brightness_driver_data = {
7020 .name = "brightness",
7021 .read = brightness_read,
7022 .write = brightness_write,
7023 .exit = brightness_exit,
7024 .suspend = brightness_suspend,
7025 .shutdown = brightness_shutdown,
7028 /*************************************************************************
7033 * IBM ThinkPads have a simple volume controller with MUTE gating.
7034 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7036 * Since the *61 series (and probably also the later *60 series), Lenovo
7037 * ThinkPads only implement the MUTE gate.
7040 * Bit 6: MUTE (1 mutes sound)
7042 * Other bits should be zero as far as we know.
7044 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7045 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7046 * such as bit 7 which is used to detect repeated presses of MUTE,
7047 * and we leave them unchanged.
7049 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7050 * in response to user input. Unfortunately, this rarely works well.
7051 * The laptop changes the state of its internal MUTE gate and, on some
7052 * models, sends KEY_MUTE, causing any user code that responds to the
7053 * mute button to get confused. The hardware MUTE gate is also
7054 * unnecessary, since user code can handle the mute button without
7055 * kernel or EC help.
7057 * To avoid confusing userspace, we simply disable all EC-based mute
7058 * and volume controls when possible.
7061 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7063 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7064 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7065 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7067 #if SNDRV_CARDS <= 32
7068 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7070 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7072 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7073 static char *alsa_id = "ThinkPadEC";
7074 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7076 struct tpacpi_alsa_data {
7077 struct snd_card *card;
7078 struct snd_ctl_elem_id *ctl_mute_id;
7079 struct snd_ctl_elem_id *ctl_vol_id;
7082 static struct snd_card *alsa_card;
7087 /* TP_EC_AUDIO bits */
7088 TP_EC_AUDIO_MUTESW = 6,
7090 /* TP_EC_AUDIO bitmasks */
7091 TP_EC_AUDIO_LVL_MSK = 0x0F,
7092 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7094 /* Maximum volume */
7095 TP_EC_VOLUME_MAX = 14,
7098 enum tpacpi_volume_access_mode {
7099 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7100 TPACPI_VOL_MODE_EC, /* Pure EC control */
7101 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7102 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7106 enum tpacpi_volume_capabilities {
7107 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7108 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7109 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7113 enum tpacpi_mute_btn_mode {
7114 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7115 /* We don't know what mode 1 is. */
7116 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7117 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7120 static enum tpacpi_volume_access_mode volume_mode =
7121 TPACPI_VOL_MODE_MAX;
7123 static enum tpacpi_volume_capabilities volume_capabilities;
7124 static bool volume_control_allowed;
7125 static bool software_mute_requested = true;
7126 static bool software_mute_active;
7127 static int software_mute_orig_mode;
7130 * Used to syncronize writers to TP_EC_AUDIO and
7131 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7133 static struct mutex volume_mutex;
7135 static void tpacpi_volume_checkpoint_nvram(void)
7141 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7143 if (!volume_control_allowed)
7145 if (software_mute_active)
7148 vdbg_printk(TPACPI_DBG_MIXER,
7149 "trying to checkpoint mixer state to NVRAM...\n");
7151 if (tp_features.mixer_no_level_control)
7152 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7154 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7156 if (mutex_lock_killable(&volume_mutex) < 0)
7159 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7162 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7164 if (lec != (b_nvram & ec_mask)) {
7165 /* NVRAM needs update */
7166 b_nvram &= ~ec_mask;
7168 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7169 dbg_printk(TPACPI_DBG_MIXER,
7170 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7171 (unsigned int) lec, (unsigned int) b_nvram);
7173 vdbg_printk(TPACPI_DBG_MIXER,
7174 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7175 (unsigned int) lec, (unsigned int) b_nvram);
7179 mutex_unlock(&volume_mutex);
7182 static int volume_get_status_ec(u8 *status)
7186 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7191 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7196 static int volume_get_status(u8 *status)
7198 return volume_get_status_ec(status);
7201 static int volume_set_status_ec(const u8 status)
7203 if (!acpi_ec_write(TP_EC_AUDIO, status))
7206 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7209 * On X200s, and possibly on others, it can take a while for
7210 * reads to become correct.
7217 static int volume_set_status(const u8 status)
7219 return volume_set_status_ec(status);
7222 /* returns < 0 on error, 0 on no change, 1 on change */
7223 static int __volume_set_mute_ec(const bool mute)
7228 if (mutex_lock_killable(&volume_mutex) < 0)
7231 rc = volume_get_status_ec(&s);
7235 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7236 s & ~TP_EC_AUDIO_MUTESW_MSK;
7239 rc = volume_set_status_ec(n);
7245 mutex_unlock(&volume_mutex);
7249 static int volume_alsa_set_mute(const bool mute)
7251 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7252 (mute) ? "" : "un");
7253 return __volume_set_mute_ec(mute);
7256 static int volume_set_mute(const bool mute)
7260 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7261 (mute) ? "" : "un");
7263 rc = __volume_set_mute_ec(mute);
7264 return (rc < 0) ? rc : 0;
7267 /* returns < 0 on error, 0 on no change, 1 on change */
7268 static int __volume_set_volume_ec(const u8 vol)
7273 if (vol > TP_EC_VOLUME_MAX)
7276 if (mutex_lock_killable(&volume_mutex) < 0)
7279 rc = volume_get_status_ec(&s);
7283 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7286 rc = volume_set_status_ec(n);
7292 mutex_unlock(&volume_mutex);
7296 static int volume_set_software_mute(bool startup)
7300 if (!tpacpi_is_lenovo())
7304 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7308 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7309 "Initial HAUM setting was %d\n",
7310 software_mute_orig_mode);
7313 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7314 (int)TP_EC_MUTE_BTN_NONE))
7317 if (result != TP_EC_MUTE_BTN_NONE)
7318 pr_warn("Unexpected SAUM result %d\n",
7322 * In software mute mode, the standard codec controls take
7323 * precendence, so we unmute the ThinkPad HW switch at
7324 * startup. Just on case there are SAUM-capable ThinkPads
7325 * with level controls, set max HW volume as well.
7327 if (tp_features.mixer_no_level_control)
7328 result = volume_set_mute(false);
7330 result = volume_set_status(TP_EC_VOLUME_MAX);
7333 pr_warn("Failed to unmute the HW mute switch\n");
7338 static void volume_exit_software_mute(void)
7342 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7343 || r != software_mute_orig_mode)
7344 pr_warn("Failed to restore mute mode\n");
7347 static int volume_alsa_set_volume(const u8 vol)
7349 dbg_printk(TPACPI_DBG_MIXER,
7350 "ALSA: trying to set volume level to %hu\n", vol);
7351 return __volume_set_volume_ec(vol);
7354 static void volume_alsa_notify_change(void)
7356 struct tpacpi_alsa_data *d;
7358 if (alsa_card && alsa_card->private_data) {
7359 d = alsa_card->private_data;
7361 snd_ctl_notify(alsa_card,
7362 SNDRV_CTL_EVENT_MASK_VALUE,
7365 snd_ctl_notify(alsa_card,
7366 SNDRV_CTL_EVENT_MASK_VALUE,
7371 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7372 struct snd_ctl_elem_info *uinfo)
7374 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7376 uinfo->value.integer.min = 0;
7377 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7381 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7382 struct snd_ctl_elem_value *ucontrol)
7387 rc = volume_get_status(&s);
7391 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7395 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7396 struct snd_ctl_elem_value *ucontrol)
7398 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7399 ucontrol->value.integer.value[0]);
7400 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7403 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7405 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7406 struct snd_ctl_elem_value *ucontrol)
7411 rc = volume_get_status(&s);
7415 ucontrol->value.integer.value[0] =
7416 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7420 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7421 struct snd_ctl_elem_value *ucontrol)
7423 tpacpi_disclose_usertask("ALSA", "%smute\n",
7424 ucontrol->value.integer.value[0] ?
7426 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7429 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7430 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7431 .name = "Console Playback Volume",
7433 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7434 .info = volume_alsa_vol_info,
7435 .get = volume_alsa_vol_get,
7438 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7439 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7440 .name = "Console Playback Switch",
7442 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7443 .info = volume_alsa_mute_info,
7444 .get = volume_alsa_mute_get,
7447 static void volume_suspend(void)
7449 tpacpi_volume_checkpoint_nvram();
7452 static void volume_resume(void)
7454 if (software_mute_active) {
7455 if (volume_set_software_mute(false) < 0)
7456 pr_warn("Failed to restore software mute\n");
7458 volume_alsa_notify_change();
7462 static void volume_shutdown(void)
7464 tpacpi_volume_checkpoint_nvram();
7467 static void volume_exit(void)
7470 snd_card_free(alsa_card);
7474 tpacpi_volume_checkpoint_nvram();
7476 if (software_mute_active)
7477 volume_exit_software_mute();
7480 static int __init volume_create_alsa_mixer(void)
7482 struct snd_card *card;
7483 struct tpacpi_alsa_data *data;
7484 struct snd_kcontrol *ctl_vol;
7485 struct snd_kcontrol *ctl_mute;
7488 rc = snd_card_new(&tpacpi_pdev->dev,
7489 alsa_index, alsa_id, THIS_MODULE,
7490 sizeof(struct tpacpi_alsa_data), &card);
7491 if (rc < 0 || !card) {
7492 pr_err("Failed to create ALSA card structures: %d\n", rc);
7496 BUG_ON(!card->private_data);
7497 data = card->private_data;
7500 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7501 sizeof(card->driver));
7502 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7503 sizeof(card->shortname));
7504 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7505 (thinkpad_id.ec_version_str) ?
7506 thinkpad_id.ec_version_str : "(unknown)");
7507 snprintf(card->longname, sizeof(card->longname),
7508 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7509 (thinkpad_id.ec_version_str) ?
7510 thinkpad_id.ec_version_str : "unknown");
7512 if (volume_control_allowed) {
7513 volume_alsa_control_vol.put = volume_alsa_vol_put;
7514 volume_alsa_control_vol.access =
7515 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7517 volume_alsa_control_mute.put = volume_alsa_mute_put;
7518 volume_alsa_control_mute.access =
7519 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7522 if (!tp_features.mixer_no_level_control) {
7523 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7524 rc = snd_ctl_add(card, ctl_vol);
7526 pr_err("Failed to create ALSA volume control: %d\n",
7530 data->ctl_vol_id = &ctl_vol->id;
7533 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7534 rc = snd_ctl_add(card, ctl_mute);
7536 pr_err("Failed to create ALSA mute control: %d\n", rc);
7539 data->ctl_mute_id = &ctl_mute->id;
7541 rc = snd_card_register(card);
7543 pr_err("Failed to register ALSA card: %d\n", rc);
7551 snd_card_free(card);
7555 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7556 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7558 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7559 /* Whitelist volume level on all IBM by default */
7560 { .vendor = PCI_VENDOR_ID_IBM,
7561 .bios = TPACPI_MATCH_ANY,
7562 .ec = TPACPI_MATCH_ANY,
7563 .quirks = TPACPI_VOL_Q_LEVEL },
7565 /* Lenovo models with volume control (needs confirmation) */
7566 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7567 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7568 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7569 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7570 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7571 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7572 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7574 /* Whitelist mute-only on all Lenovo by default */
7575 { .vendor = PCI_VENDOR_ID_LENOVO,
7576 .bios = TPACPI_MATCH_ANY,
7577 .ec = TPACPI_MATCH_ANY,
7578 .quirks = TPACPI_VOL_Q_MUTEONLY }
7581 static int __init volume_init(struct ibm_init_struct *iibm)
7583 unsigned long quirks;
7586 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7588 mutex_init(&volume_mutex);
7591 * Check for module parameter bogosity, note that we
7592 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7593 * able to detect "unspecified"
7595 if (volume_mode > TPACPI_VOL_MODE_MAX)
7598 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7599 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7604 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7608 * The ALSA mixer is our primary interface.
7609 * When disabled, don't install the subdriver at all
7612 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7613 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7617 quirks = tpacpi_check_quirks(volume_quirk_table,
7618 ARRAY_SIZE(volume_quirk_table));
7620 switch (volume_capabilities) {
7621 case TPACPI_VOL_CAP_AUTO:
7622 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7623 tp_features.mixer_no_level_control = 1;
7624 else if (quirks & TPACPI_VOL_Q_LEVEL)
7625 tp_features.mixer_no_level_control = 0;
7627 return -ENODEV; /* no mixer */
7629 case TPACPI_VOL_CAP_VOLMUTE:
7630 tp_features.mixer_no_level_control = 0;
7632 case TPACPI_VOL_CAP_MUTEONLY:
7633 tp_features.mixer_no_level_control = 1;
7639 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7640 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7641 "using user-supplied volume_capabilities=%d\n",
7642 volume_capabilities);
7644 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7645 volume_mode == TPACPI_VOL_MODE_MAX) {
7646 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7648 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7649 "driver auto-selected volume_mode=%d\n",
7652 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7653 "using user-supplied volume_mode=%d\n",
7657 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7658 "mute is supported, volume control is %s\n",
7659 str_supported(!tp_features.mixer_no_level_control));
7661 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7662 software_mute_active = true;
7664 rc = volume_create_alsa_mixer();
7666 pr_err("Could not create the ALSA mixer interface\n");
7670 pr_info("Console audio control enabled, mode: %s\n",
7671 (volume_control_allowed) ?
7672 "override (read/write)" :
7673 "monitor (read only)");
7676 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7677 "registering volume hotkeys as change notification\n");
7678 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7679 | TP_ACPI_HKEY_VOLUP_MASK
7680 | TP_ACPI_HKEY_VOLDWN_MASK
7681 | TP_ACPI_HKEY_MUTE_MASK);
7686 static int volume_read(struct seq_file *m)
7690 if (volume_get_status(&status) < 0) {
7691 seq_printf(m, "level:\t\tunreadable\n");
7693 if (tp_features.mixer_no_level_control)
7694 seq_printf(m, "level:\t\tunsupported\n");
7696 seq_printf(m, "level:\t\t%d\n",
7697 status & TP_EC_AUDIO_LVL_MSK);
7699 seq_printf(m, "mute:\t\t%s\n",
7700 onoff(status, TP_EC_AUDIO_MUTESW));
7702 if (volume_control_allowed) {
7703 seq_printf(m, "commands:\tunmute, mute\n");
7704 if (!tp_features.mixer_no_level_control) {
7705 seq_printf(m, "commands:\tup, down\n");
7706 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7715 static int volume_write(char *buf)
7718 u8 new_level, new_mute;
7724 * We do allow volume control at driver startup, so that the
7725 * user can set initial state through the volume=... parameter hack.
7727 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7728 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7729 tp_warned.volume_ctrl_forbidden = 1;
7730 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7731 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7736 rc = volume_get_status(&s);
7740 new_level = s & TP_EC_AUDIO_LVL_MSK;
7741 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7743 while ((cmd = strsep(&buf, ","))) {
7744 if (!tp_features.mixer_no_level_control) {
7745 if (strlencmp(cmd, "up") == 0) {
7748 else if (new_level < TP_EC_VOLUME_MAX)
7751 } else if (strlencmp(cmd, "down") == 0) {
7754 else if (new_level > 0)
7757 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7758 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7763 if (strlencmp(cmd, "mute") == 0)
7764 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7765 else if (strlencmp(cmd, "unmute") == 0)
7771 if (tp_features.mixer_no_level_control) {
7772 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7773 new_mute ? "" : "un");
7774 rc = volume_set_mute(!!new_mute);
7776 tpacpi_disclose_usertask("procfs volume",
7777 "%smute and set level to %d\n",
7778 new_mute ? "" : "un", new_level);
7779 rc = volume_set_status(new_mute | new_level);
7781 volume_alsa_notify_change();
7783 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7786 static struct ibm_struct volume_driver_data = {
7788 .read = volume_read,
7789 .write = volume_write,
7790 .exit = volume_exit,
7791 .suspend = volume_suspend,
7792 .resume = volume_resume,
7793 .shutdown = volume_shutdown,
7796 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7798 #define alsa_card NULL
7800 static inline void volume_alsa_notify_change(void)
7804 static int __init volume_init(struct ibm_init_struct *iibm)
7806 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7811 static struct ibm_struct volume_driver_data = {
7815 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7817 /*************************************************************************
7824 * TPACPI_FAN_RD_ACPI_GFAN:
7825 * ACPI GFAN method: returns fan level
7827 * see TPACPI_FAN_WR_ACPI_SFAN
7828 * EC 0x2f (HFSP) not available if GFAN exists
7830 * TPACPI_FAN_WR_ACPI_SFAN:
7831 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7833 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7836 * TPACPI_FAN_WR_TPEC:
7837 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7838 * Supported on almost all ThinkPads
7840 * Fan speed changes of any sort (including those caused by the
7841 * disengaged mode) are usually done slowly by the firmware as the
7842 * maximum amount of fan duty cycle change per second seems to be
7845 * Reading is not available if GFAN exists.
7846 * Writing is not available if SFAN exists.
7849 * 7 automatic mode engaged;
7850 * (default operation mode of the ThinkPad)
7851 * fan level is ignored in this mode.
7852 * 6 full speed mode (takes precedence over bit 7);
7853 * not available on all thinkpads. May disable
7854 * the tachometer while the fan controller ramps up
7855 * the speed (which can take up to a few *minutes*).
7856 * Speeds up fan to 100% duty-cycle, which is far above
7857 * the standard RPM levels. It is not impossible that
7858 * it could cause hardware damage.
7859 * 5-3 unused in some models. Extra bits for fan level
7860 * in others, but still useless as all values above
7861 * 7 map to the same speed as level 7 in these models.
7862 * 2-0 fan level (0..7 usually)
7864 * 0x07 = max (set when temperatures critical)
7865 * Some ThinkPads may have other levels, see
7866 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7868 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7869 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7870 * does so, its initial value is meaningless (0x07).
7872 * For firmware bugs, refer to:
7873 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7877 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7878 * Main fan tachometer reading (in RPM)
7880 * This register is present on all ThinkPads with a new-style EC, and
7881 * it is known not to be present on the A21m/e, and T22, as there is
7882 * something else in offset 0x84 according to the ACPI DSDT. Other
7883 * ThinkPads from this same time period (and earlier) probably lack the
7884 * tachometer as well.
7886 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7887 * was never fixed by IBM to report the EC firmware version string
7888 * probably support the tachometer (like the early X models), so
7889 * detecting it is quite hard. We need more data to know for sure.
7891 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7894 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7897 * For firmware bugs, refer to:
7898 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7902 * ThinkPad EC register 0x31 bit 0 (only on select models)
7904 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7905 * show the speed of the main fan. When bit 0 of EC register 0x31
7906 * is one, the tachometer registers show the speed of the auxiliary
7909 * Fan control seems to affect both fans, regardless of the state
7912 * So far, only the firmware for the X60/X61 non-tablet versions
7913 * seem to support this (firmware TP-7M).
7915 * TPACPI_FAN_WR_ACPI_FANS:
7916 * ThinkPad X31, X40, X41. Not available in the X60.
7918 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7919 * high speed. ACPI DSDT seems to map these three speeds to levels
7920 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7921 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7923 * The speeds are stored on handles
7924 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7926 * There are three default speed sets, accessible as handles:
7927 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7929 * ACPI DSDT switches which set is in use depending on various
7932 * TPACPI_FAN_WR_TPEC is also available and should be used to
7933 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7934 * but the ACPI tables just mention level 7.
7937 enum { /* Fan control constants */
7938 fan_status_offset = 0x2f, /* EC register 0x2f */
7939 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7940 * 0x84 must be read before 0x85 */
7941 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7942 bit 0 selects which fan is active */
7944 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7945 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7947 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7950 enum fan_status_access_mode {
7951 TPACPI_FAN_NONE = 0, /* No fan status or control */
7952 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7953 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7956 enum fan_control_access_mode {
7957 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7958 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7959 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7960 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7963 enum fan_control_commands {
7964 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7965 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7966 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7967 * and also watchdog cmd */
7970 static bool fan_control_allowed;
7972 static enum fan_status_access_mode fan_status_access_mode;
7973 static enum fan_control_access_mode fan_control_access_mode;
7974 static enum fan_control_commands fan_control_commands;
7976 static u8 fan_control_initial_status;
7977 static u8 fan_control_desired_level;
7978 static u8 fan_control_resume_level;
7979 static int fan_watchdog_maxinterval;
7981 static struct mutex fan_mutex;
7983 static void fan_watchdog_fire(struct work_struct *ignored);
7984 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7986 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7987 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7988 "\\FSPD", /* 600e/x, 770e, 770x */
7990 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7991 "JFNS", /* 770x-JL */
7995 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7996 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7997 * be in auto mode (0x80).
7999 * This is corrected by any write to HFSP either by the driver, or
8002 * We assume 0x07 really means auto mode while this quirk is active,
8003 * as this is far more likely than the ThinkPad being in level 7,
8004 * which is only used by the firmware during thermal emergencies.
8006 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8007 * TP-70 (T43, R52), which are known to be buggy.
8010 static void fan_quirk1_setup(void)
8012 if (fan_control_initial_status == 0x07) {
8013 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8014 tp_features.fan_ctrl_status_undef = 1;
8018 static void fan_quirk1_handle(u8 *fan_status)
8020 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8021 if (*fan_status != fan_control_initial_status) {
8022 /* something changed the HFSP regisnter since
8023 * driver init time, so it is not undefined
8025 tp_features.fan_ctrl_status_undef = 0;
8027 /* Return most likely status. In fact, it
8028 * might be the only possible status */
8029 *fan_status = TP_EC_FAN_AUTO;
8034 /* Select main fan on X60/X61, NOOP on others */
8035 static bool fan_select_fan1(void)
8037 if (tp_features.second_fan) {
8040 if (ec_read(fan_select_offset, &val) < 0)
8043 if (ec_write(fan_select_offset, val) < 0)
8049 /* Select secondary fan on X60/X61 */
8050 static bool fan_select_fan2(void)
8054 if (!tp_features.second_fan)
8057 if (ec_read(fan_select_offset, &val) < 0)
8060 if (ec_write(fan_select_offset, val) < 0)
8067 * Call with fan_mutex held
8069 static void fan_update_desired_level(u8 status)
8072 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8074 fan_control_desired_level = 7;
8076 fan_control_desired_level = status;
8080 static int fan_get_status(u8 *status)
8085 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8087 switch (fan_status_access_mode) {
8088 case TPACPI_FAN_RD_ACPI_GFAN: {
8089 /* 570, 600e/x, 770e, 770x */
8092 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8096 *status = res & 0x07;
8100 case TPACPI_FAN_RD_TPEC:
8101 /* all except 570, 600e/x, 770e, 770x */
8102 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8105 if (likely(status)) {
8107 fan_quirk1_handle(status);
8119 static int fan_get_status_safe(u8 *status)
8124 if (mutex_lock_killable(&fan_mutex))
8125 return -ERESTARTSYS;
8126 rc = fan_get_status(&s);
8128 fan_update_desired_level(s);
8129 mutex_unlock(&fan_mutex);
8139 static int fan_get_speed(unsigned int *speed)
8143 switch (fan_status_access_mode) {
8144 case TPACPI_FAN_RD_TPEC:
8145 /* all except 570, 600e/x, 770e, 770x */
8146 if (unlikely(!fan_select_fan1()))
8148 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8149 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8153 *speed = (hi << 8) | lo;
8164 static int fan2_get_speed(unsigned int *speed)
8169 switch (fan_status_access_mode) {
8170 case TPACPI_FAN_RD_TPEC:
8171 /* all except 570, 600e/x, 770e, 770x */
8172 if (unlikely(!fan_select_fan2()))
8174 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8175 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8176 fan_select_fan1(); /* play it safe */
8181 *speed = (hi << 8) | lo;
8192 static int fan_set_level(int level)
8194 if (!fan_control_allowed)
8197 switch (fan_control_access_mode) {
8198 case TPACPI_FAN_WR_ACPI_SFAN:
8199 if ((level < 0) || (level > 7))
8202 if (tp_features.second_fan_ctl) {
8203 if (!fan_select_fan2() ||
8204 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8205 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8206 tp_features.second_fan_ctl = 0;
8210 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8214 case TPACPI_FAN_WR_ACPI_FANS:
8215 case TPACPI_FAN_WR_TPEC:
8216 if (!(level & TP_EC_FAN_AUTO) &&
8217 !(level & TP_EC_FAN_FULLSPEED) &&
8218 ((level < 0) || (level > 7)))
8221 /* safety net should the EC not support AUTO
8222 * or FULLSPEED mode bits and just ignore them */
8223 if (level & TP_EC_FAN_FULLSPEED)
8224 level |= 7; /* safety min speed 7 */
8225 else if (level & TP_EC_FAN_AUTO)
8226 level |= 4; /* safety min speed 4 */
8228 if (tp_features.second_fan_ctl) {
8229 if (!fan_select_fan2() ||
8230 !acpi_ec_write(fan_status_offset, level)) {
8231 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8232 tp_features.second_fan_ctl = 0;
8237 if (!acpi_ec_write(fan_status_offset, level))
8240 tp_features.fan_ctrl_status_undef = 0;
8247 vdbg_printk(TPACPI_DBG_FAN,
8248 "fan control: set fan control register to 0x%02x\n", level);
8252 static int fan_set_level_safe(int level)
8256 if (!fan_control_allowed)
8259 if (mutex_lock_killable(&fan_mutex))
8260 return -ERESTARTSYS;
8262 if (level == TPACPI_FAN_LAST_LEVEL)
8263 level = fan_control_desired_level;
8265 rc = fan_set_level(level);
8267 fan_update_desired_level(level);
8269 mutex_unlock(&fan_mutex);
8273 static int fan_set_enable(void)
8278 if (!fan_control_allowed)
8281 if (mutex_lock_killable(&fan_mutex))
8282 return -ERESTARTSYS;
8284 switch (fan_control_access_mode) {
8285 case TPACPI_FAN_WR_ACPI_FANS:
8286 case TPACPI_FAN_WR_TPEC:
8287 rc = fan_get_status(&s);
8291 /* Don't go out of emergency fan mode */
8294 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8297 if (!acpi_ec_write(fan_status_offset, s))
8300 tp_features.fan_ctrl_status_undef = 0;
8305 case TPACPI_FAN_WR_ACPI_SFAN:
8306 rc = fan_get_status(&s);
8312 /* Set fan to at least level 4 */
8315 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8325 mutex_unlock(&fan_mutex);
8328 vdbg_printk(TPACPI_DBG_FAN,
8329 "fan control: set fan control register to 0x%02x\n",
8334 static int fan_set_disable(void)
8338 if (!fan_control_allowed)
8341 if (mutex_lock_killable(&fan_mutex))
8342 return -ERESTARTSYS;
8345 switch (fan_control_access_mode) {
8346 case TPACPI_FAN_WR_ACPI_FANS:
8347 case TPACPI_FAN_WR_TPEC:
8348 if (!acpi_ec_write(fan_status_offset, 0x00))
8351 fan_control_desired_level = 0;
8352 tp_features.fan_ctrl_status_undef = 0;
8356 case TPACPI_FAN_WR_ACPI_SFAN:
8357 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8360 fan_control_desired_level = 0;
8368 vdbg_printk(TPACPI_DBG_FAN,
8369 "fan control: set fan control register to 0\n");
8371 mutex_unlock(&fan_mutex);
8375 static int fan_set_speed(int speed)
8379 if (!fan_control_allowed)
8382 if (mutex_lock_killable(&fan_mutex))
8383 return -ERESTARTSYS;
8386 switch (fan_control_access_mode) {
8387 case TPACPI_FAN_WR_ACPI_FANS:
8388 if (speed >= 0 && speed <= 65535) {
8389 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8390 speed, speed, speed))
8400 mutex_unlock(&fan_mutex);
8404 static void fan_watchdog_reset(void)
8406 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8409 if (fan_watchdog_maxinterval > 0 &&
8410 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8411 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8412 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8414 cancel_delayed_work(&fan_watchdog_task);
8417 static void fan_watchdog_fire(struct work_struct *ignored)
8421 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8424 pr_notice("fan watchdog: enabling fan\n");
8425 rc = fan_set_enable();
8427 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8429 /* reschedule for later */
8430 fan_watchdog_reset();
8435 * SYSFS fan layout: hwmon compatible (device)
8438 * 0: "disengaged" mode
8440 * 2: native EC "auto" mode (recommended, hardware default)
8442 * pwm*: set speed in manual mode, ignored otherwise.
8443 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8446 * fan*_input: tachometer reading, RPM
8449 * SYSFS fan layout: extensions
8451 * fan_watchdog (driver):
8452 * fan watchdog interval in seconds, 0 disables (default), max 120
8455 /* sysfs fan pwm1_enable ----------------------------------------------- */
8456 static ssize_t fan_pwm1_enable_show(struct device *dev,
8457 struct device_attribute *attr,
8463 res = fan_get_status_safe(&status);
8467 if (status & TP_EC_FAN_FULLSPEED) {
8469 } else if (status & TP_EC_FAN_AUTO) {
8474 return sysfs_emit(buf, "%d\n", mode);
8477 static ssize_t fan_pwm1_enable_store(struct device *dev,
8478 struct device_attribute *attr,
8479 const char *buf, size_t count)
8484 if (parse_strtoul(buf, 2, &t))
8487 tpacpi_disclose_usertask("hwmon pwm1_enable",
8488 "set fan mode to %lu\n", t);
8492 level = TP_EC_FAN_FULLSPEED;
8495 level = TPACPI_FAN_LAST_LEVEL;
8498 level = TP_EC_FAN_AUTO;
8501 /* reserved for software-controlled auto mode */
8507 res = fan_set_level_safe(level);
8513 fan_watchdog_reset();
8518 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8519 fan_pwm1_enable_show, fan_pwm1_enable_store);
8521 /* sysfs fan pwm1 ------------------------------------------------------ */
8522 static ssize_t fan_pwm1_show(struct device *dev,
8523 struct device_attribute *attr,
8529 res = fan_get_status_safe(&status);
8534 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8535 status = fan_control_desired_level;
8540 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
8543 static ssize_t fan_pwm1_store(struct device *dev,
8544 struct device_attribute *attr,
8545 const char *buf, size_t count)
8549 u8 status, newlevel;
8551 if (parse_strtoul(buf, 255, &s))
8554 tpacpi_disclose_usertask("hwmon pwm1",
8555 "set fan speed to %lu\n", s);
8557 /* scale down from 0-255 to 0-7 */
8558 newlevel = (s >> 5) & 0x07;
8560 if (mutex_lock_killable(&fan_mutex))
8561 return -ERESTARTSYS;
8563 rc = fan_get_status(&status);
8564 if (!rc && (status &
8565 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8566 rc = fan_set_level(newlevel);
8570 fan_update_desired_level(newlevel);
8571 fan_watchdog_reset();
8575 mutex_unlock(&fan_mutex);
8576 return (rc) ? rc : count;
8579 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8581 /* sysfs fan fan1_input ------------------------------------------------ */
8582 static ssize_t fan_fan1_input_show(struct device *dev,
8583 struct device_attribute *attr,
8589 res = fan_get_speed(&speed);
8593 return sysfs_emit(buf, "%u\n", speed);
8596 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8598 /* sysfs fan fan2_input ------------------------------------------------ */
8599 static ssize_t fan_fan2_input_show(struct device *dev,
8600 struct device_attribute *attr,
8606 res = fan2_get_speed(&speed);
8610 return sysfs_emit(buf, "%u\n", speed);
8613 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8615 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8616 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8618 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
8621 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8626 if (parse_strtoul(buf, 120, &t))
8629 if (!fan_control_allowed)
8632 fan_watchdog_maxinterval = t;
8633 fan_watchdog_reset();
8635 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8639 static DRIVER_ATTR_RW(fan_watchdog);
8641 /* --------------------------------------------------------------------- */
8643 static struct attribute *fan_attributes[] = {
8644 &dev_attr_pwm1_enable.attr,
8645 &dev_attr_pwm1.attr,
8646 &dev_attr_fan1_input.attr,
8647 &dev_attr_fan2_input.attr,
8651 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8654 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8655 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8658 if (attr == &dev_attr_fan2_input.attr) {
8659 if (!tp_features.second_fan)
8666 static const struct attribute_group fan_attr_group = {
8667 .is_visible = fan_attr_is_visible,
8668 .attrs = fan_attributes,
8671 static struct attribute *fan_driver_attributes[] = {
8672 &driver_attr_fan_watchdog.attr,
8676 static const struct attribute_group fan_driver_attr_group = {
8677 .is_visible = fan_attr_is_visible,
8678 .attrs = fan_driver_attributes,
8681 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
8682 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8683 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8684 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
8686 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8687 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8688 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8689 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8690 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8691 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8692 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8693 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8694 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8695 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8696 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8697 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8698 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8699 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8700 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8701 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8702 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
8703 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
8706 static int __init fan_init(struct ibm_init_struct *iibm)
8708 unsigned long quirks;
8710 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8711 "initializing fan subdriver\n");
8713 mutex_init(&fan_mutex);
8714 fan_status_access_mode = TPACPI_FAN_NONE;
8715 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8716 fan_control_commands = 0;
8717 fan_watchdog_maxinterval = 0;
8718 tp_features.fan_ctrl_status_undef = 0;
8719 tp_features.second_fan = 0;
8720 tp_features.second_fan_ctl = 0;
8721 fan_control_desired_level = 7;
8723 if (tpacpi_is_ibm()) {
8724 TPACPI_ACPIHANDLE_INIT(fans);
8725 TPACPI_ACPIHANDLE_INIT(gfan);
8726 TPACPI_ACPIHANDLE_INIT(sfan);
8729 quirks = tpacpi_check_quirks(fan_quirk_table,
8730 ARRAY_SIZE(fan_quirk_table));
8732 if (quirks & TPACPI_FAN_NOFAN) {
8733 pr_info("No integrated ThinkPad fan available\n");
8738 /* 570, 600e/x, 770e, 770x */
8739 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8741 /* all other ThinkPads: note that even old-style
8742 * ThinkPad ECs supports the fan control register */
8743 if (likely(acpi_ec_read(fan_status_offset,
8744 &fan_control_initial_status))) {
8748 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8749 if (quirks & TPACPI_FAN_Q1)
8751 if (quirks & TPACPI_FAN_2FAN) {
8752 tp_features.second_fan = 1;
8753 pr_info("secondary fan support enabled\n");
8755 if (quirks & TPACPI_FAN_2CTL) {
8756 tp_features.second_fan = 1;
8757 tp_features.second_fan_ctl = 1;
8758 pr_info("secondary fan control enabled\n");
8760 /* Try and probe the 2nd fan */
8761 res = fan2_get_speed(&speed);
8763 /* It responded - so let's assume it's there */
8764 tp_features.second_fan = 1;
8765 tp_features.second_fan_ctl = 1;
8766 pr_info("secondary fan control detected & enabled\n");
8770 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8777 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8778 fan_control_commands |=
8779 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8782 /* gfan without sfan means no fan control */
8783 /* all other models implement TP EC 0x2f control */
8787 fan_control_access_mode =
8788 TPACPI_FAN_WR_ACPI_FANS;
8789 fan_control_commands |=
8790 TPACPI_FAN_CMD_SPEED |
8791 TPACPI_FAN_CMD_LEVEL |
8792 TPACPI_FAN_CMD_ENABLE;
8794 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8795 fan_control_commands |=
8796 TPACPI_FAN_CMD_LEVEL |
8797 TPACPI_FAN_CMD_ENABLE;
8802 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8803 "fan is %s, modes %d, %d\n",
8804 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8805 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8806 fan_status_access_mode, fan_control_access_mode);
8808 /* fan control master switch */
8809 if (!fan_control_allowed) {
8810 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8811 fan_control_commands = 0;
8812 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8813 "fan control features disabled by parameter\n");
8816 /* update fan_control_desired_level */
8817 if (fan_status_access_mode != TPACPI_FAN_NONE)
8818 fan_get_status_safe(NULL);
8820 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8821 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8827 static void fan_exit(void)
8829 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8830 "cancelling any pending fan watchdog tasks\n");
8832 cancel_delayed_work(&fan_watchdog_task);
8833 flush_workqueue(tpacpi_wq);
8836 static void fan_suspend(void)
8840 if (!fan_control_allowed)
8843 /* Store fan status in cache */
8844 fan_control_resume_level = 0;
8845 rc = fan_get_status_safe(&fan_control_resume_level);
8847 pr_notice("failed to read fan level for later restore during resume: %d\n",
8850 /* if it is undefined, don't attempt to restore it.
8852 if (tp_features.fan_ctrl_status_undef)
8853 fan_control_resume_level = 0;
8856 static void fan_resume(void)
8858 u8 current_level = 7;
8859 bool do_set = false;
8862 /* DSDT *always* updates status on resume */
8863 tp_features.fan_ctrl_status_undef = 0;
8865 if (!fan_control_allowed ||
8866 !fan_control_resume_level ||
8867 fan_get_status_safe(¤t_level))
8870 switch (fan_control_access_mode) {
8871 case TPACPI_FAN_WR_ACPI_SFAN:
8872 /* never decrease fan level */
8873 do_set = (fan_control_resume_level > current_level);
8875 case TPACPI_FAN_WR_ACPI_FANS:
8876 case TPACPI_FAN_WR_TPEC:
8877 /* never decrease fan level, scale is:
8878 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8880 * We expect the firmware to set either 7 or AUTO, but we
8881 * handle FULLSPEED out of paranoia.
8883 * So, we can safely only restore FULLSPEED or 7, anything
8884 * else could slow the fan. Restoring AUTO is useless, at
8885 * best that's exactly what the DSDT already set (it is the
8888 * Always keep in mind that the DSDT *will* have set the
8889 * fans to what the vendor supposes is the best level. We
8890 * muck with it only to speed the fan up.
8892 if (fan_control_resume_level != 7 &&
8893 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8896 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8897 (current_level != fan_control_resume_level);
8903 pr_notice("restoring fan level to 0x%02x\n",
8904 fan_control_resume_level);
8905 rc = fan_set_level_safe(fan_control_resume_level);
8907 pr_notice("failed to restore fan level: %d\n", rc);
8911 static int fan_read(struct seq_file *m)
8915 unsigned int speed = 0;
8917 switch (fan_status_access_mode) {
8918 case TPACPI_FAN_RD_ACPI_GFAN:
8919 /* 570, 600e/x, 770e, 770x */
8920 rc = fan_get_status_safe(&status);
8924 seq_printf(m, "status:\t\t%s\n"
8926 (status != 0) ? "enabled" : "disabled", status);
8929 case TPACPI_FAN_RD_TPEC:
8930 /* all except 570, 600e/x, 770e, 770x */
8931 rc = fan_get_status_safe(&status);
8935 seq_printf(m, "status:\t\t%s\n",
8936 (status != 0) ? "enabled" : "disabled");
8938 rc = fan_get_speed(&speed);
8942 seq_printf(m, "speed:\t\t%d\n", speed);
8944 if (status & TP_EC_FAN_FULLSPEED)
8945 /* Disengaged mode takes precedence */
8946 seq_printf(m, "level:\t\tdisengaged\n");
8947 else if (status & TP_EC_FAN_AUTO)
8948 seq_printf(m, "level:\t\tauto\n");
8950 seq_printf(m, "level:\t\t%d\n", status);
8953 case TPACPI_FAN_NONE:
8955 seq_printf(m, "status:\t\tnot supported\n");
8958 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8959 seq_printf(m, "commands:\tlevel <level>");
8961 switch (fan_control_access_mode) {
8962 case TPACPI_FAN_WR_ACPI_SFAN:
8963 seq_printf(m, " (<level> is 0-7)\n");
8967 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8972 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8973 seq_printf(m, "commands:\tenable, disable\n"
8974 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8976 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8977 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8982 static int fan_write_cmd_level(const char *cmd, int *rc)
8986 if (strlencmp(cmd, "level auto") == 0)
8987 level = TP_EC_FAN_AUTO;
8988 else if ((strlencmp(cmd, "level disengaged") == 0) ||
8989 (strlencmp(cmd, "level full-speed") == 0))
8990 level = TP_EC_FAN_FULLSPEED;
8991 else if (sscanf(cmd, "level %d", &level) != 1)
8994 *rc = fan_set_level_safe(level);
8996 pr_err("level command accepted for unsupported access mode %d\n",
8997 fan_control_access_mode);
8999 tpacpi_disclose_usertask("procfs fan",
9000 "set level to %d\n", level);
9005 static int fan_write_cmd_enable(const char *cmd, int *rc)
9007 if (strlencmp(cmd, "enable") != 0)
9010 *rc = fan_set_enable();
9012 pr_err("enable command accepted for unsupported access mode %d\n",
9013 fan_control_access_mode);
9015 tpacpi_disclose_usertask("procfs fan", "enable\n");
9020 static int fan_write_cmd_disable(const char *cmd, int *rc)
9022 if (strlencmp(cmd, "disable") != 0)
9025 *rc = fan_set_disable();
9027 pr_err("disable command accepted for unsupported access mode %d\n",
9028 fan_control_access_mode);
9030 tpacpi_disclose_usertask("procfs fan", "disable\n");
9035 static int fan_write_cmd_speed(const char *cmd, int *rc)
9040 * Support speed <low> <medium> <high> ? */
9042 if (sscanf(cmd, "speed %d", &speed) != 1)
9045 *rc = fan_set_speed(speed);
9047 pr_err("speed command accepted for unsupported access mode %d\n",
9048 fan_control_access_mode);
9050 tpacpi_disclose_usertask("procfs fan",
9051 "set speed to %d\n", speed);
9056 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9060 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9063 if (interval < 0 || interval > 120)
9066 fan_watchdog_maxinterval = interval;
9067 tpacpi_disclose_usertask("procfs fan",
9068 "set watchdog timer to %d\n",
9075 static int fan_write(char *buf)
9080 while (!rc && (cmd = strsep(&buf, ","))) {
9081 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9082 fan_write_cmd_level(cmd, &rc)) &&
9083 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9084 (fan_write_cmd_enable(cmd, &rc) ||
9085 fan_write_cmd_disable(cmd, &rc) ||
9086 fan_write_cmd_watchdog(cmd, &rc))) &&
9087 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9088 fan_write_cmd_speed(cmd, &rc))
9092 fan_watchdog_reset();
9098 static struct ibm_struct fan_driver_data = {
9103 .suspend = fan_suspend,
9104 .resume = fan_resume,
9107 /*************************************************************************
9108 * Mute LED subdriver
9111 #define TPACPI_LED_MAX 2
9113 struct tp_led_table {
9120 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9121 [LED_AUDIO_MUTE] = {
9126 [LED_AUDIO_MICMUTE] = {
9133 static int mute_led_on_off(struct tp_led_table *t, bool state)
9138 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9139 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9143 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9144 state ? t->on_value : t->off_value))
9151 static int tpacpi_led_set(int whichled, bool on)
9153 struct tp_led_table *t;
9155 t = &led_tables[whichled];
9156 if (t->state < 0 || t->state == on)
9158 return mute_led_on_off(t, on);
9161 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9162 enum led_brightness brightness)
9164 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9167 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9168 enum led_brightness brightness)
9170 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9173 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9174 [LED_AUDIO_MUTE] = {
9175 .name = "platform::mute",
9176 .max_brightness = 1,
9177 .brightness_set_blocking = tpacpi_led_mute_set,
9178 .default_trigger = "audio-mute",
9180 [LED_AUDIO_MICMUTE] = {
9181 .name = "platform::micmute",
9182 .max_brightness = 1,
9183 .brightness_set_blocking = tpacpi_led_micmute_set,
9184 .default_trigger = "audio-micmute",
9188 static int mute_led_init(struct ibm_init_struct *iibm)
9193 for (i = 0; i < TPACPI_LED_MAX; i++) {
9194 struct tp_led_table *t = &led_tables[i];
9195 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9200 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9201 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9204 led_classdev_unregister(&mute_led_cdev[i]);
9211 static void mute_led_exit(void)
9215 for (i = 0; i < TPACPI_LED_MAX; i++) {
9216 led_classdev_unregister(&mute_led_cdev[i]);
9217 tpacpi_led_set(i, false);
9221 static void mute_led_resume(void)
9225 for (i = 0; i < TPACPI_LED_MAX; i++) {
9226 struct tp_led_table *t = &led_tables[i];
9228 mute_led_on_off(t, t->state);
9232 static struct ibm_struct mute_led_driver_data = {
9234 .exit = mute_led_exit,
9235 .resume = mute_led_resume,
9239 * Battery Wear Control Driver
9240 * Contact: Ognjen Galic <smclt30p@gmail.com>
9245 #define GET_START "BCTG"
9246 #define SET_START "BCCS"
9247 #define GET_STOP "BCSG"
9248 #define SET_STOP "BCSS"
9249 #define GET_DISCHARGE "BDSG"
9250 #define SET_DISCHARGE "BDSS"
9251 #define GET_INHIBIT "BICG"
9252 #define SET_INHIBIT "BICS"
9261 /* Error condition bit */
9262 METHOD_ERR = BIT(31),
9266 /* This is used in the get/set helpers */
9273 struct tpacpi_battery_data {
9278 unsigned int charge_behaviours;
9281 struct tpacpi_battery_driver_data {
9282 struct tpacpi_battery_data batteries[3];
9283 int individual_addressing;
9286 static struct tpacpi_battery_driver_data battery_info;
9288 /* ACPI helpers/functions/probes */
9291 * This evaluates a ACPI method call specific to the battery
9292 * ACPI extension. The specifics are that an error is marked
9293 * in the 32rd bit of the response, so we just check that here.
9295 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9299 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9300 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9303 if (response & METHOD_ERR) {
9304 acpi_handle_err(hkey_handle,
9305 "%s evaluated but flagged as error", method);
9312 static int tpacpi_battery_get(int what, int battery, int *ret)
9315 case THRESHOLD_START:
9316 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9319 /* The value is in the low 8 bits of the response */
9322 case THRESHOLD_STOP:
9323 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9325 /* Value is in lower 8 bits */
9328 * On the stop value, if we return 0 that
9329 * does not make any sense. 0 means Default, which
9330 * means that charging stops at 100%, so we return
9336 case FORCE_DISCHARGE:
9337 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9339 /* The force discharge status is in bit 0 */
9342 case INHIBIT_CHARGE:
9343 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9345 /* The inhibit charge status is in bit 0 */
9349 pr_crit("wrong parameter: %d", what);
9354 static int tpacpi_battery_set(int what, int battery, int value)
9357 /* The first 8 bits are the value of the threshold */
9359 /* The battery ID is in bits 8-9, 2 bits */
9360 param |= battery << 8;
9363 case THRESHOLD_START:
9364 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9365 pr_err("failed to set charge threshold on battery %d",
9370 case THRESHOLD_STOP:
9371 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9372 pr_err("failed to set stop threshold: %d", battery);
9376 case FORCE_DISCHARGE:
9377 /* Force discharge is in bit 0,
9378 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9379 * battery ID is in bits 8-9, 2 bits.
9381 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9382 pr_err("failed to set force discharge on %d", battery);
9386 case INHIBIT_CHARGE:
9387 /* When setting inhibit charge, we set a default value of
9388 * always breaking on AC detach and the effective time is set to
9390 * The battery ID is in bits 4-5, 2 bits,
9391 * the effective time is in bits 8-23, 2 bytes.
9392 * A time of FFFF indicates forever.
9395 param |= battery << 4;
9396 param |= 0xFFFF << 8;
9397 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9398 pr_err("failed to set inhibit charge on %d", battery);
9403 pr_crit("wrong parameter: %d", what);
9408 static int tpacpi_battery_set_validate(int what, int battery, int value)
9412 ret = tpacpi_battery_set(what, battery, value);
9416 ret = tpacpi_battery_get(what, battery, &v);
9425 ret = tpacpi_battery_get(what, battery, &v);
9435 static int tpacpi_battery_probe(int battery)
9439 memset(&battery_info.batteries[battery], 0,
9440 sizeof(battery_info.batteries[battery]));
9443 * 1) Get the current start threshold
9444 * 2) Check for support
9445 * 3) Get the current stop threshold
9446 * 4) Check for support
9447 * 5) Get the current force discharge status
9448 * 6) Check for support
9449 * 7) Get the current inhibit charge status
9450 * 8) Check for support
9452 if (acpi_has_method(hkey_handle, GET_START)) {
9453 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9454 pr_err("Error probing battery %d\n", battery);
9457 /* Individual addressing is in bit 9 */
9459 battery_info.individual_addressing = true;
9460 /* Support is marked in bit 8 */
9462 battery_info.batteries[battery].start_support = 1;
9465 if (tpacpi_battery_get(THRESHOLD_START, battery,
9466 &battery_info.batteries[battery].charge_start)) {
9467 pr_err("Error probing battery %d\n", battery);
9471 if (acpi_has_method(hkey_handle, GET_STOP)) {
9472 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9473 pr_err("Error probing battery stop; %d\n", battery);
9476 /* Support is marked in bit 8 */
9478 battery_info.batteries[battery].stop_support = 1;
9481 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9482 &battery_info.batteries[battery].charge_stop)) {
9483 pr_err("Error probing battery stop: %d\n", battery);
9487 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9488 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9489 pr_err("Error probing battery discharge; %d\n", battery);
9492 /* Support is marked in bit 8 */
9494 battery_info.batteries[battery].charge_behaviours |=
9495 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9497 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9498 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9499 pr_err("Error probing battery inhibit charge; %d\n", battery);
9502 /* Support is marked in bit 5 */
9504 battery_info.batteries[battery].charge_behaviours |=
9505 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9508 battery_info.batteries[battery].charge_behaviours |=
9509 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9511 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9513 battery_info.batteries[battery].charge_start,
9514 battery_info.batteries[battery].charge_stop,
9515 battery_info.batteries[battery].charge_behaviours);
9520 /* General helper functions */
9522 static int tpacpi_battery_get_id(const char *battery_name)
9525 if (strcmp(battery_name, "BAT0") == 0 ||
9526 tp_features.battery_force_primary)
9528 if (strcmp(battery_name, "BAT1") == 0)
9529 return BAT_SECONDARY;
9531 * If for some reason the battery is not BAT0 nor is it
9532 * BAT1, we will assume it's the default, first battery,
9535 pr_warn("unknown battery %s, assuming primary", battery_name);
9539 /* sysfs interface */
9541 static ssize_t tpacpi_battery_store(int what,
9543 const char *buf, size_t count)
9545 struct power_supply *supply = to_power_supply(dev);
9546 unsigned long value;
9549 * Some systems have support for more than
9550 * one battery. If that is the case,
9551 * tpacpi_battery_probe marked that addressing
9552 * them individually is supported, so we do that
9553 * based on the device struct.
9555 * On systems that are not supported, we assume
9556 * the primary as most of the ACPI calls fail
9557 * with "Any Battery" as the parameter.
9559 if (battery_info.individual_addressing)
9560 /* BAT_PRIMARY or BAT_SECONDARY */
9561 battery = tpacpi_battery_get_id(supply->desc->name);
9563 battery = BAT_PRIMARY;
9565 rval = kstrtoul(buf, 10, &value);
9570 case THRESHOLD_START:
9571 if (!battery_info.batteries[battery].start_support)
9573 /* valid values are [0, 99] */
9576 if (value > battery_info.batteries[battery].charge_stop)
9578 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9580 battery_info.batteries[battery].charge_start = value;
9583 case THRESHOLD_STOP:
9584 if (!battery_info.batteries[battery].stop_support)
9586 /* valid values are [1, 100] */
9587 if (value < 1 || value > 100)
9589 if (value < battery_info.batteries[battery].charge_start)
9591 battery_info.batteries[battery].charge_stop = value;
9593 * When 100 is passed to stop, we need to flip
9594 * it to 0 as that the EC understands that as
9595 * "Default", which will charge to 100%
9599 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9603 pr_crit("Wrong parameter: %d", what);
9609 static ssize_t tpacpi_battery_show(int what,
9613 struct power_supply *supply = to_power_supply(dev);
9616 * Some systems have support for more than
9617 * one battery. If that is the case,
9618 * tpacpi_battery_probe marked that addressing
9619 * them individually is supported, so we;
9620 * based on the device struct.
9622 * On systems that are not supported, we assume
9623 * the primary as most of the ACPI calls fail
9624 * with "Any Battery" as the parameter.
9626 if (battery_info.individual_addressing)
9627 /* BAT_PRIMARY or BAT_SECONDARY */
9628 battery = tpacpi_battery_get_id(supply->desc->name);
9630 battery = BAT_PRIMARY;
9631 if (tpacpi_battery_get(what, battery, &ret))
9633 return sprintf(buf, "%d\n", ret);
9636 static ssize_t charge_control_start_threshold_show(struct device *device,
9637 struct device_attribute *attr,
9640 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9643 static ssize_t charge_control_end_threshold_show(struct device *device,
9644 struct device_attribute *attr,
9647 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9650 static ssize_t charge_behaviour_show(struct device *dev,
9651 struct device_attribute *attr,
9654 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9655 struct power_supply *supply = to_power_supply(dev);
9656 unsigned int available;
9659 battery = tpacpi_battery_get_id(supply->desc->name);
9660 available = battery_info.batteries[battery].charge_behaviours;
9662 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9663 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9666 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
9671 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9672 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9675 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9681 return power_supply_charge_behaviour_show(dev, available, active, buf);
9684 static ssize_t charge_control_start_threshold_store(struct device *dev,
9685 struct device_attribute *attr,
9686 const char *buf, size_t count)
9688 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9691 static ssize_t charge_control_end_threshold_store(struct device *dev,
9692 struct device_attribute *attr,
9693 const char *buf, size_t count)
9695 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9698 static ssize_t charge_behaviour_store(struct device *dev,
9699 struct device_attribute *attr,
9700 const char *buf, size_t count)
9702 struct power_supply *supply = to_power_supply(dev);
9703 int selected, battery, ret = 0;
9704 unsigned int available;
9706 battery = tpacpi_battery_get_id(supply->desc->name);
9707 available = battery_info.batteries[battery].charge_behaviours;
9708 selected = power_supply_charge_behaviour_parse(available, buf);
9714 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9715 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9716 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9717 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9718 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
9722 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
9723 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9724 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9725 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9729 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9730 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9731 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9732 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
9737 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9744 static DEVICE_ATTR_RW(charge_control_start_threshold);
9745 static DEVICE_ATTR_RW(charge_control_end_threshold);
9746 static DEVICE_ATTR_RW(charge_behaviour);
9747 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9748 charge_start_threshold,
9750 charge_control_start_threshold_show,
9751 charge_control_start_threshold_store
9753 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9754 charge_stop_threshold,
9756 charge_control_end_threshold_show,
9757 charge_control_end_threshold_store
9760 static struct attribute *tpacpi_battery_attrs[] = {
9761 &dev_attr_charge_control_start_threshold.attr,
9762 &dev_attr_charge_control_end_threshold.attr,
9763 &dev_attr_charge_start_threshold.attr,
9764 &dev_attr_charge_stop_threshold.attr,
9765 &dev_attr_charge_behaviour.attr,
9769 ATTRIBUTE_GROUPS(tpacpi_battery);
9771 /* ACPI battery hooking */
9773 static int tpacpi_battery_add(struct power_supply *battery)
9775 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9777 if (tpacpi_battery_probe(batteryid))
9779 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9784 static int tpacpi_battery_remove(struct power_supply *battery)
9786 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9790 static struct acpi_battery_hook battery_hook = {
9791 .add_battery = tpacpi_battery_add,
9792 .remove_battery = tpacpi_battery_remove,
9793 .name = "ThinkPad Battery Extension",
9796 /* Subdriver init/exit */
9798 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9800 * Individual addressing is broken on models that expose the
9801 * primary battery as BAT1.
9803 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9804 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9805 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9806 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9807 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9808 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9811 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9813 memset(&battery_info, 0, sizeof(battery_info));
9815 tp_features.battery_force_primary = tpacpi_check_quirks(
9816 battery_quirk_table,
9817 ARRAY_SIZE(battery_quirk_table));
9819 battery_hook_register(&battery_hook);
9823 static void tpacpi_battery_exit(void)
9825 battery_hook_unregister(&battery_hook);
9828 static struct ibm_struct battery_driver_data = {
9830 .exit = tpacpi_battery_exit,
9833 /*************************************************************************
9834 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9837 static struct drm_privacy_screen *lcdshadow_dev;
9838 static acpi_handle lcdshadow_get_handle;
9839 static acpi_handle lcdshadow_set_handle;
9841 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
9842 enum drm_privacy_screen_status state)
9846 if (WARN_ON(!mutex_is_locked(&priv->lock)))
9849 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
9852 priv->hw_state = priv->sw_state = state;
9856 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
9860 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9863 priv->hw_state = priv->sw_state = output & 0x1;
9866 static const struct drm_privacy_screen_ops lcdshadow_ops = {
9867 .set_sw_state = lcdshadow_set_sw_state,
9868 .get_hw_state = lcdshadow_get_hw_state,
9871 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9873 acpi_status status1, status2;
9876 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
9877 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
9878 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
9881 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9884 if (!(output & 0x10000))
9887 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
9888 &lcdshadow_ops, NULL);
9889 if (IS_ERR(lcdshadow_dev))
9890 return PTR_ERR(lcdshadow_dev);
9895 static void lcdshadow_exit(void)
9897 drm_privacy_screen_unregister(lcdshadow_dev);
9900 static void lcdshadow_resume(void)
9905 mutex_lock(&lcdshadow_dev->lock);
9906 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
9907 mutex_unlock(&lcdshadow_dev->lock);
9910 static int lcdshadow_read(struct seq_file *m)
9912 if (!lcdshadow_dev) {
9913 seq_puts(m, "status:\t\tnot supported\n");
9915 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
9916 seq_puts(m, "commands:\t0, 1\n");
9922 static int lcdshadow_write(char *buf)
9925 int res, state = -EINVAL;
9930 while ((cmd = strsep(&buf, ","))) {
9931 res = kstrtoint(cmd, 10, &state);
9936 if (state >= 2 || state < 0)
9939 mutex_lock(&lcdshadow_dev->lock);
9940 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
9941 mutex_unlock(&lcdshadow_dev->lock);
9943 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
9948 static struct ibm_struct lcdshadow_driver_data = {
9949 .name = "lcdshadow",
9950 .exit = lcdshadow_exit,
9951 .resume = lcdshadow_resume,
9952 .read = lcdshadow_read,
9953 .write = lcdshadow_write,
9956 /*************************************************************************
9957 * Thinkpad sensor interfaces
9960 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
9961 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
9962 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
9963 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
9965 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
9966 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
9968 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
9969 #define PALMSENSOR_ON_BIT 1 /* psensor status */
9971 static bool has_palmsensor;
9972 static bool has_lapsensor;
9973 static bool palm_state;
9974 static bool lap_state;
9975 static int dytc_version;
9977 static int dytc_command(int command, int *output)
9979 acpi_handle dytc_handle;
9981 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
9982 /* Platform doesn't support DYTC */
9985 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
9990 static int lapsensor_get(bool *present, bool *state)
9995 err = dytc_command(DYTC_CMD_GET, &output);
9999 *present = true; /*If we get his far, we have lapmode support*/
10000 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
10004 static int palmsensor_get(bool *present, bool *state)
10006 acpi_handle psensor_handle;
10010 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
10012 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
10015 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10016 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10020 static void lapsensor_refresh(void)
10025 if (has_lapsensor) {
10026 err = lapsensor_get(&has_lapsensor, &state);
10029 if (lap_state != state) {
10031 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10036 static void palmsensor_refresh(void)
10041 if (has_palmsensor) {
10042 err = palmsensor_get(&has_palmsensor, &state);
10045 if (palm_state != state) {
10046 palm_state = state;
10047 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10052 static ssize_t dytc_lapmode_show(struct device *dev,
10053 struct device_attribute *attr,
10057 return sysfs_emit(buf, "%d\n", lap_state);
10058 return sysfs_emit(buf, "\n");
10060 static DEVICE_ATTR_RO(dytc_lapmode);
10062 static ssize_t palmsensor_show(struct device *dev,
10063 struct device_attribute *attr,
10066 if (has_palmsensor)
10067 return sysfs_emit(buf, "%d\n", palm_state);
10068 return sysfs_emit(buf, "\n");
10070 static DEVICE_ATTR_RO(palmsensor);
10072 static struct attribute *proxsensor_attributes[] = {
10073 &dev_attr_dytc_lapmode.attr,
10074 &dev_attr_palmsensor.attr,
10078 static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10079 struct attribute *attr, int n)
10081 if (attr == &dev_attr_dytc_lapmode.attr) {
10083 * Platforms before DYTC version 5 claim to have a lap sensor,
10084 * but it doesn't work, so we ignore them.
10086 if (!has_lapsensor || dytc_version < 5)
10088 } else if (attr == &dev_attr_palmsensor.attr) {
10089 if (!has_palmsensor)
10096 static const struct attribute_group proxsensor_attr_group = {
10097 .is_visible = proxsensor_attr_is_visible,
10098 .attrs = proxsensor_attributes,
10101 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10103 int palm_err, lap_err;
10105 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10106 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10107 /* If support isn't available for both devices return -ENODEV */
10108 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10110 /* Otherwise, if there was an error return it */
10111 if (palm_err && (palm_err != -ENODEV))
10113 if (lap_err && (lap_err != -ENODEV))
10119 static struct ibm_struct proxsensor_driver_data = {
10120 .name = "proximity-sensor",
10123 /*************************************************************************
10124 * DYTC Platform Profile interface
10127 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10128 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10129 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10131 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
10132 #define DYTC_FC_MMC 27 /* MMC Mode supported */
10133 #define DYTC_FC_PSC 29 /* PSC Mode supported */
10135 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10136 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10138 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10139 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10140 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10142 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10143 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10144 #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
10145 #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
10147 #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
10148 #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
10149 #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
10150 #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
10152 #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
10153 #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
10154 #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
10156 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10157 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10159 #define DYTC_SET_COMMAND(function, mode, on) \
10160 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10161 (mode) << DYTC_SET_MODE_BIT | \
10162 (on) << DYTC_SET_VALID_BIT)
10164 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
10165 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
10167 enum dytc_profile_funcmode {
10168 DYTC_FUNCMODE_NONE = 0,
10173 static enum dytc_profile_funcmode dytc_profile_available;
10174 static enum platform_profile_option dytc_current_profile;
10175 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10176 static DEFINE_MUTEX(dytc_mutex);
10177 static bool dytc_mmc_get_available;
10179 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
10181 if (dytc_profile_available == DYTC_FUNCMODE_MMC) {
10182 switch (dytcmode) {
10183 case DYTC_MODE_MMC_LOWPOWER:
10184 *profile = PLATFORM_PROFILE_LOW_POWER;
10186 case DYTC_MODE_MMC_DEFAULT:
10187 case DYTC_MODE_MMC_BALANCE:
10188 *profile = PLATFORM_PROFILE_BALANCED;
10190 case DYTC_MODE_MMC_PERFORM:
10191 *profile = PLATFORM_PROFILE_PERFORMANCE;
10193 default: /* Unknown mode */
10198 if (dytc_profile_available == DYTC_FUNCMODE_PSC) {
10199 switch (dytcmode) {
10200 case DYTC_MODE_PSC_LOWPOWER:
10201 *profile = PLATFORM_PROFILE_LOW_POWER;
10203 case DYTC_MODE_PSC_BALANCE:
10204 *profile = PLATFORM_PROFILE_BALANCED;
10206 case DYTC_MODE_PSC_PERFORM:
10207 *profile = PLATFORM_PROFILE_PERFORMANCE;
10209 default: /* Unknown mode */
10216 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10219 case PLATFORM_PROFILE_LOW_POWER:
10220 if (dytc_profile_available == DYTC_FUNCMODE_MMC)
10221 *perfmode = DYTC_MODE_MMC_LOWPOWER;
10222 else if (dytc_profile_available == DYTC_FUNCMODE_PSC)
10223 *perfmode = DYTC_MODE_PSC_LOWPOWER;
10225 case PLATFORM_PROFILE_BALANCED:
10226 if (dytc_profile_available == DYTC_FUNCMODE_MMC)
10227 *perfmode = DYTC_MODE_MMC_BALANCE;
10228 else if (dytc_profile_available == DYTC_FUNCMODE_PSC)
10229 *perfmode = DYTC_MODE_PSC_BALANCE;
10231 case PLATFORM_PROFILE_PERFORMANCE:
10232 if (dytc_profile_available == DYTC_FUNCMODE_MMC)
10233 *perfmode = DYTC_MODE_MMC_PERFORM;
10234 else if (dytc_profile_available == DYTC_FUNCMODE_PSC)
10235 *perfmode = DYTC_MODE_PSC_PERFORM;
10237 default: /* Unknown profile */
10238 return -EOPNOTSUPP;
10244 * dytc_profile_get: Function to register with platform_profile
10245 * handler. Returns current platform profile.
10247 static int dytc_profile_get(struct platform_profile_handler *pprof,
10248 enum platform_profile_option *profile)
10250 *profile = dytc_current_profile;
10255 * Helper function - check if we are in CQL mode and if we are
10257 * - run the command
10259 * If not in CQL mode, just run the command
10261 static int dytc_cql_command(int command, int *output)
10263 int err, cmd_err, dummy;
10266 /* Determine if we are in CQL mode. This alters the commands we do */
10267 err = dytc_command(DYTC_CMD_GET, output);
10271 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10272 /* Check if we're OK to return immediately */
10273 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10276 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10277 atomic_inc(&dytc_ignore_event);
10278 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10283 cmd_err = dytc_command(command, output);
10284 /* Check return condition after we've restored CQL state */
10286 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10287 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10295 * dytc_profile_set: Function to register with platform_profile
10296 * handler. Sets current platform profile.
10298 static int dytc_profile_set(struct platform_profile_handler *pprof,
10299 enum platform_profile_option profile)
10305 err = mutex_lock_interruptible(&dytc_mutex);
10309 err = convert_profile_to_dytc(profile, &perfmode);
10313 if (dytc_profile_available == DYTC_FUNCMODE_MMC) {
10314 if (profile == PLATFORM_PROFILE_BALANCED) {
10316 * To get back to balanced mode we need to issue a reset command.
10317 * Note we still need to disable CQL mode before hand and re-enable
10318 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10319 * stuck at 0 for aprox. 30 minutes.
10321 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10325 /* Determine if we are in CQL mode. This alters the commands we do */
10326 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
10332 if (dytc_profile_available == DYTC_FUNCMODE_PSC) {
10333 err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
10337 /* Success - update current profile */
10338 dytc_current_profile = profile;
10340 mutex_unlock(&dytc_mutex);
10344 static void dytc_profile_refresh(void)
10346 enum platform_profile_option profile;
10347 int output, err = 0;
10350 mutex_lock(&dytc_mutex);
10351 if (dytc_profile_available == DYTC_FUNCMODE_MMC) {
10352 if (dytc_mmc_get_available)
10353 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10355 err = dytc_cql_command(DYTC_CMD_GET, &output);
10356 } else if (dytc_profile_available == DYTC_FUNCMODE_PSC)
10357 err = dytc_command(DYTC_CMD_GET, &output);
10359 mutex_unlock(&dytc_mutex);
10363 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10364 convert_dytc_to_profile(perfmode, &profile);
10365 if (profile != dytc_current_profile) {
10366 dytc_current_profile = profile;
10367 platform_profile_notify();
10371 static struct platform_profile_handler dytc_profile = {
10372 .profile_get = dytc_profile_get,
10373 .profile_set = dytc_profile_set,
10376 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10380 /* Setup supported modes */
10381 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10382 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10383 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10385 dytc_profile_available = DYTC_FUNCMODE_NONE;
10386 err = dytc_command(DYTC_CMD_QUERY, &output);
10390 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10391 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
10393 /* Check DYTC is enabled and supports mode setting */
10394 if (dytc_version < 5)
10397 /* Check what capabilities are supported */
10398 err = dytc_command(DYTC_CMD_FUNC_CAP, &output);
10402 if (output & BIT(DYTC_FC_MMC)) { /* MMC MODE */
10403 dytc_profile_available = DYTC_FUNCMODE_MMC;
10406 * Check if MMC_GET functionality available
10407 * Version > 6 and return success from MMC_GET command
10409 dytc_mmc_get_available = false;
10410 if (dytc_version >= 6) {
10411 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10412 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10413 dytc_mmc_get_available = true;
10415 } else if (output & BIT(DYTC_FC_PSC)) { /* PSC MODE */
10416 dytc_profile_available = DYTC_FUNCMODE_PSC;
10418 dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
10422 dbg_printk(TPACPI_DBG_INIT,
10423 "DYTC version %d: thermal mode available\n", dytc_version);
10425 /* Create platform_profile structure and register */
10426 err = platform_profile_register(&dytc_profile);
10428 * If for some reason platform_profiles aren't enabled
10429 * don't quit terminally.
10434 /* Ensure initial values are correct */
10435 dytc_profile_refresh();
10440 static void dytc_profile_exit(void)
10442 dytc_profile_available = DYTC_FUNCMODE_NONE;
10443 platform_profile_remove();
10446 static struct ibm_struct dytc_profile_driver_data = {
10447 .name = "dytc-profile",
10448 .exit = dytc_profile_exit,
10451 /*************************************************************************
10452 * Keyboard language interface
10455 struct keyboard_lang_data {
10456 const char *lang_str;
10460 static const struct keyboard_lang_data keyboard_lang_data[] = {
10482 static int set_keyboard_lang_command(int command)
10484 acpi_handle sskl_handle;
10487 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10488 /* Platform doesn't support SSKL */
10492 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10498 static int get_keyboard_lang(int *output)
10500 acpi_handle gskl_handle;
10503 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10504 /* Platform doesn't support GSKL */
10508 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10512 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10513 * '(' and ')') keys which use layout dependent key-press emulation.
10515 if (kbd_lang & METHOD_ERR)
10518 *output = kbd_lang;
10523 /* sysfs keyboard language entry */
10524 static ssize_t keyboard_lang_show(struct device *dev,
10525 struct device_attribute *attr,
10528 int output, err, i, len = 0;
10530 err = get_keyboard_lang(&output);
10534 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10536 len += sysfs_emit_at(buf, len, "%s", " ");
10538 if (output == keyboard_lang_data[i].lang_code) {
10539 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10541 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10544 len += sysfs_emit_at(buf, len, "\n");
10549 static ssize_t keyboard_lang_store(struct device *dev,
10550 struct device_attribute *attr,
10551 const char *buf, size_t count)
10554 bool lang_found = false;
10557 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10558 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10559 lang_code = keyboard_lang_data[i].lang_code;
10566 lang_code = lang_code | 1 << 24;
10568 /* Set language code */
10569 err = set_keyboard_lang_command(lang_code);
10573 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10577 tpacpi_disclose_usertask(attr->attr.name,
10578 "keyboard language is set to %s\n", buf);
10580 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10584 static DEVICE_ATTR_RW(keyboard_lang);
10586 static struct attribute *kbdlang_attributes[] = {
10587 &dev_attr_keyboard_lang.attr,
10591 static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10592 struct attribute *attr, int n)
10594 return tp_features.kbd_lang ? attr->mode : 0;
10597 static const struct attribute_group kbdlang_attr_group = {
10598 .is_visible = kbdlang_attr_is_visible,
10599 .attrs = kbdlang_attributes,
10602 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10606 err = get_keyboard_lang(&output);
10607 tp_features.kbd_lang = !err;
10611 static struct ibm_struct kbdlang_driver_data = {
10615 /*************************************************************************
10616 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10617 * and WLAN feature.
10619 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10620 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10621 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10622 static bool has_antennatype;
10623 static int wwan_antennatype;
10625 static int dprc_command(int command, int *output)
10627 acpi_handle dprc_handle;
10629 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10630 /* Platform doesn't support DPRC */
10634 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10638 * METHOD_ERR gets returned on devices where few commands are not supported
10639 * for example command to get WWAN Antenna type command is not supported on
10642 if (*output & METHOD_ERR)
10648 static int get_wwan_antenna(int *wwan_antennatype)
10652 /* Get current Antenna type */
10653 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10657 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10658 *wwan_antennatype = 1;
10659 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10660 *wwan_antennatype = 2;
10667 /* sysfs wwan antenna type entry */
10668 static ssize_t wwan_antenna_type_show(struct device *dev,
10669 struct device_attribute *attr,
10672 switch (wwan_antennatype) {
10674 return sysfs_emit(buf, "type a\n");
10676 return sysfs_emit(buf, "type b\n");
10681 static DEVICE_ATTR_RO(wwan_antenna_type);
10683 static struct attribute *dprc_attributes[] = {
10684 &dev_attr_wwan_antenna_type.attr,
10688 static umode_t dprc_attr_is_visible(struct kobject *kobj,
10689 struct attribute *attr, int n)
10691 return has_antennatype ? attr->mode : 0;
10694 static const struct attribute_group dprc_attr_group = {
10695 .is_visible = dprc_attr_is_visible,
10696 .attrs = dprc_attributes,
10699 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10703 err = get_wwan_antenna(&wwan_antennatype);
10707 has_antennatype = true;
10711 static struct ibm_struct dprc_driver_data = {
10715 /* --------------------------------------------------------------------- */
10717 static struct attribute *tpacpi_driver_attributes[] = {
10718 &driver_attr_debug_level.attr,
10719 &driver_attr_version.attr,
10720 &driver_attr_interface_version.attr,
10721 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10722 &driver_attr_wlsw_emulstate.attr,
10723 &driver_attr_bluetooth_emulstate.attr,
10724 &driver_attr_wwan_emulstate.attr,
10725 &driver_attr_uwb_emulstate.attr,
10730 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10731 static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
10732 struct attribute *attr, int n)
10734 if (attr == &driver_attr_wlsw_emulstate.attr) {
10737 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
10738 if (!dbg_bluetoothemul)
10740 } else if (attr == &driver_attr_wwan_emulstate.attr) {
10743 } else if (attr == &driver_attr_uwb_emulstate.attr) {
10752 static const struct attribute_group tpacpi_driver_attr_group = {
10753 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10754 .is_visible = tpacpi_attr_is_visible,
10756 .attrs = tpacpi_driver_attributes,
10759 static const struct attribute_group *tpacpi_driver_groups[] = {
10760 &tpacpi_driver_attr_group,
10764 static const struct attribute_group *tpacpi_groups[] = {
10765 &adaptive_kbd_attr_group,
10766 &hotkey_attr_group,
10767 &bluetooth_attr_group,
10770 &proxsensor_attr_group,
10771 &kbdlang_attr_group,
10776 static const struct attribute_group *tpacpi_hwmon_groups[] = {
10777 &thermal_attr_group,
10778 &temp_label_attr_group,
10783 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
10784 &fan_driver_attr_group,
10788 /****************************************************************************
10789 ****************************************************************************
10793 ****************************************************************************
10794 ****************************************************************************/
10796 static struct platform_driver tpacpi_pdriver = {
10798 .name = TPACPI_DRVR_NAME,
10800 .groups = tpacpi_driver_groups,
10801 .dev_groups = tpacpi_groups,
10803 .shutdown = tpacpi_shutdown_handler,
10806 static struct platform_driver tpacpi_hwmon_pdriver = {
10808 .name = TPACPI_HWMON_DRVR_NAME,
10809 .groups = tpacpi_hwmon_driver_groups,
10813 /****************************************************************************
10814 ****************************************************************************
10818 ****************************************************************************
10819 ****************************************************************************/
10822 * HKEY event callout for other subdrivers go here
10823 * (yes, it is ugly, but it is quick, safe, and gets the job done
10825 static void tpacpi_driver_event(const unsigned int hkey_event)
10827 if (ibm_backlight_device) {
10828 switch (hkey_event) {
10829 case TP_HKEY_EV_BRGHT_UP:
10830 case TP_HKEY_EV_BRGHT_DOWN:
10831 tpacpi_brightness_notify_change();
10835 switch (hkey_event) {
10836 case TP_HKEY_EV_VOL_UP:
10837 case TP_HKEY_EV_VOL_DOWN:
10838 case TP_HKEY_EV_VOL_MUTE:
10839 volume_alsa_notify_change();
10842 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
10843 enum led_brightness brightness;
10845 mutex_lock(&kbdlight_mutex);
10848 * Check the brightness actually changed, setting the brightness
10849 * through kbdlight_set_level() also triggers this event.
10851 brightness = kbdlight_sysfs_get(NULL);
10852 if (kbdlight_brightness != brightness) {
10853 kbdlight_brightness = brightness;
10854 led_classdev_notify_brightness_hw_changed(
10855 &tpacpi_led_kbdlight.led_classdev, brightness);
10858 mutex_unlock(&kbdlight_mutex);
10861 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
10862 lapsensor_refresh();
10863 /* If we are already accessing DYTC then skip dytc update */
10864 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
10865 dytc_profile_refresh();
10868 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
10869 enum drm_privacy_screen_status old_hw_state;
10872 mutex_lock(&lcdshadow_dev->lock);
10873 old_hw_state = lcdshadow_dev->hw_state;
10874 lcdshadow_get_hw_state(lcdshadow_dev);
10875 changed = lcdshadow_dev->hw_state != old_hw_state;
10876 mutex_unlock(&lcdshadow_dev->lock);
10879 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10883 static void hotkey_driver_event(const unsigned int scancode)
10885 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
10888 /* --------------------------------------------------------------------- */
10890 /* /proc support */
10891 static struct proc_dir_entry *proc_dir;
10894 * Module and infrastructure proble, init and exit handling
10897 static bool force_load;
10899 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
10900 static const char * __init str_supported(int is_supported)
10902 static char text_unsupported[] __initdata = "not supported";
10904 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
10906 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
10908 static void ibm_exit(struct ibm_struct *ibm)
10910 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
10912 list_del_init(&ibm->all_drivers);
10914 if (ibm->flags.acpi_notify_installed) {
10915 dbg_printk(TPACPI_DBG_EXIT,
10916 "%s: acpi_remove_notify_handler\n", ibm->name);
10917 BUG_ON(!ibm->acpi);
10918 acpi_remove_notify_handler(*ibm->acpi->handle,
10920 dispatch_acpi_notify);
10921 ibm->flags.acpi_notify_installed = 0;
10924 if (ibm->flags.proc_created) {
10925 dbg_printk(TPACPI_DBG_EXIT,
10926 "%s: remove_proc_entry\n", ibm->name);
10927 remove_proc_entry(ibm->name, proc_dir);
10928 ibm->flags.proc_created = 0;
10931 if (ibm->flags.acpi_driver_registered) {
10932 dbg_printk(TPACPI_DBG_EXIT,
10933 "%s: acpi_bus_unregister_driver\n", ibm->name);
10934 BUG_ON(!ibm->acpi);
10935 acpi_bus_unregister_driver(ibm->acpi->driver);
10936 kfree(ibm->acpi->driver);
10937 ibm->acpi->driver = NULL;
10938 ibm->flags.acpi_driver_registered = 0;
10941 if (ibm->flags.init_called && ibm->exit) {
10943 ibm->flags.init_called = 0;
10946 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
10949 static int __init ibm_init(struct ibm_init_struct *iibm)
10952 struct ibm_struct *ibm = iibm->data;
10953 struct proc_dir_entry *entry;
10955 BUG_ON(ibm == NULL);
10957 INIT_LIST_HEAD(&ibm->all_drivers);
10959 if (ibm->flags.experimental && !experimental)
10962 dbg_printk(TPACPI_DBG_INIT,
10963 "probing for %s\n", ibm->name);
10966 ret = iibm->init(iibm);
10967 if (ret > 0 || ret == -ENODEV)
10968 return 0; /* subdriver functionality not available */
10972 ibm->flags.init_called = 1;
10976 if (ibm->acpi->hid) {
10977 ret = register_tpacpi_subdriver(ibm);
10982 if (ibm->acpi->notify) {
10983 ret = setup_acpi_notify(ibm);
10984 if (ret == -ENODEV) {
10985 pr_notice("disabling subdriver %s\n",
10995 dbg_printk(TPACPI_DBG_INIT,
10996 "%s installed\n", ibm->name);
10999 umode_t mode = iibm->base_procfs_mode;
11005 entry = proc_create_data(ibm->name, mode, proc_dir,
11006 &dispatch_proc_ops, ibm);
11008 pr_err("unable to create proc entry %s\n", ibm->name);
11012 ibm->flags.proc_created = 1;
11015 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
11020 dbg_printk(TPACPI_DBG_INIT,
11021 "%s: at error exit path with result %d\n",
11025 return (ret < 0) ? ret : 0;
11030 static char __init tpacpi_parse_fw_id(const char * const s,
11031 u32 *model, u16 *release)
11035 if (!s || strlen(s) < 8)
11038 for (i = 0; i < 8; i++)
11039 if (!((s[i] >= '0' && s[i] <= '9') ||
11040 (s[i] >= 'A' && s[i] <= 'Z')))
11044 * Most models: xxyTkkWW (#.##c)
11045 * Ancient 570/600 and -SL lacks (#.##c)
11047 if (s[3] == 'T' || s[3] == 'N') {
11048 *model = TPID(s[0], s[1]);
11049 *release = TPVER(s[4], s[5]);
11052 /* New models: xxxyTkkW (#.##c); T550 and some others */
11053 } else if (s[4] == 'T' || s[4] == 'N') {
11054 *model = TPID3(s[0], s[1], s[2]);
11055 *release = TPVER(s[5], s[6]);
11063 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
11065 char *ec_fw_string = (char *) private;
11066 const char *dmi_data = (const char *)dm;
11068 * ThinkPad Embedded Controller Program Table on newer models
11070 * Offset | Name | Width | Description
11071 * ----------------------------------------------------
11072 * 0x00 | Type | BYTE | 0x8C
11073 * 0x01 | Length | BYTE |
11074 * 0x02 | Handle | WORD | Varies
11075 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
11076 * 0x0A | OEM struct offset | BYTE | 0x0B
11077 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
11078 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
11079 * 0x0D | ECP version ID | STR ID |
11080 * 0x0E | ECP release date | STR ID |
11083 /* Return if data structure not match */
11084 if (dm->type != 140 || dm->length < 0x0F ||
11085 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
11086 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11087 dmi_data[0x0C] != 0x01)
11090 /* fwstr is the first 8byte string */
11091 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
11094 /* returns 0 - probe ok, or < 0 - probe error.
11095 * Probe ok doesn't mean thinkpad found.
11096 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
11097 static int __must_check __init get_thinkpad_model_data(
11098 struct thinkpad_id_data *tp)
11100 const struct dmi_device *dev = NULL;
11101 char ec_fw_string[18] = {0};
11108 memset(tp, 0, sizeof(*tp));
11110 if (dmi_name_in_vendors("IBM"))
11111 tp->vendor = PCI_VENDOR_ID_IBM;
11112 else if (dmi_name_in_vendors("LENOVO"))
11113 tp->vendor = PCI_VENDOR_ID_LENOVO;
11117 s = dmi_get_system_info(DMI_BIOS_VERSION);
11118 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11119 if (s && !tp->bios_version_str)
11122 /* Really ancient ThinkPad 240X will fail this, which is fine */
11123 t = tpacpi_parse_fw_id(tp->bios_version_str,
11124 &tp->bios_model, &tp->bios_release);
11125 if (t != 'E' && t != 'C')
11129 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11130 * X32 or newer, all Z series; Some models must have an
11131 * up-to-date BIOS or they will not be detected.
11133 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11135 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
11136 if (sscanf(dev->name,
11137 "IBM ThinkPad Embedded Controller -[%17c",
11138 ec_fw_string) == 1) {
11139 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11140 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
11145 /* Newer ThinkPads have different EC program info table */
11146 if (!ec_fw_string[0])
11147 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11149 if (ec_fw_string[0]) {
11150 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11151 if (!tp->ec_version_str)
11154 t = tpacpi_parse_fw_id(ec_fw_string,
11155 &tp->ec_model, &tp->ec_release);
11157 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11159 pr_notice("please report this to %s\n", TPACPI_MAIL);
11163 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
11164 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
11165 tp->model_str = kstrdup(s, GFP_KERNEL);
11166 if (!tp->model_str)
11169 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11170 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11171 tp->model_str = kstrdup(s, GFP_KERNEL);
11172 if (!tp->model_str)
11177 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11178 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11179 if (s && !tp->nummodel_str)
11185 static int __init probe_for_thinkpad(void)
11192 /* It would be dangerous to run the driver in this case */
11193 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11197 * Non-ancient models have better DMI tagging, but very old models
11198 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11200 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11201 (thinkpad_id.ec_model != 0) ||
11202 tpacpi_is_fw_known();
11204 /* The EC handler is required */
11205 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11208 pr_err("Not yet supported ThinkPad detected!\n");
11212 if (!is_thinkpad && !force_load)
11218 static void __init thinkpad_acpi_init_banner(void)
11220 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11221 pr_info("%s\n", TPACPI_URL);
11223 pr_info("ThinkPad BIOS %s, EC %s\n",
11224 (thinkpad_id.bios_version_str) ?
11225 thinkpad_id.bios_version_str : "unknown",
11226 (thinkpad_id.ec_version_str) ?
11227 thinkpad_id.ec_version_str : "unknown");
11229 BUG_ON(!thinkpad_id.vendor);
11231 if (thinkpad_id.model_str)
11232 pr_info("%s %s, model %s\n",
11233 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11234 "IBM" : ((thinkpad_id.vendor ==
11235 PCI_VENDOR_ID_LENOVO) ?
11236 "Lenovo" : "Unknown vendor"),
11237 thinkpad_id.model_str,
11238 (thinkpad_id.nummodel_str) ?
11239 thinkpad_id.nummodel_str : "unknown");
11242 /* Module init, exit, parameters */
11244 static struct ibm_init_struct ibms_init[] __initdata = {
11246 .data = &thinkpad_acpi_driver_data,
11249 .init = hotkey_init,
11250 .data = &hotkey_driver_data,
11253 .init = bluetooth_init,
11254 .data = &bluetooth_driver_data,
11258 .data = &wan_driver_data,
11262 .data = &uwb_driver_data,
11264 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
11266 .init = video_init,
11267 .base_procfs_mode = S_IRUSR,
11268 .data = &video_driver_data,
11272 .init = kbdlight_init,
11273 .data = &kbdlight_driver_data,
11276 .init = light_init,
11277 .data = &light_driver_data,
11281 .data = &cmos_driver_data,
11285 .data = &led_driver_data,
11289 .data = &beep_driver_data,
11292 .init = thermal_init,
11293 .data = &thermal_driver_data,
11296 .init = brightness_init,
11297 .data = &brightness_driver_data,
11300 .init = volume_init,
11301 .data = &volume_driver_data,
11305 .data = &fan_driver_data,
11308 .init = mute_led_init,
11309 .data = &mute_led_driver_data,
11312 .init = tpacpi_battery_init,
11313 .data = &battery_driver_data,
11316 .init = tpacpi_lcdshadow_init,
11317 .data = &lcdshadow_driver_data,
11320 .init = tpacpi_proxsensor_init,
11321 .data = &proxsensor_driver_data,
11324 .init = tpacpi_dytc_profile_init,
11325 .data = &dytc_profile_driver_data,
11328 .init = tpacpi_kbdlang_init,
11329 .data = &kbdlang_driver_data,
11332 .init = tpacpi_dprc_init,
11333 .data = &dprc_driver_data,
11337 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
11340 struct ibm_struct *ibm;
11342 if (!kp || !kp->name || !val)
11345 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11346 ibm = ibms_init[i].data;
11347 if (!ibm || !ibm->name)
11350 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
11351 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
11353 strcpy(ibms_init[i].param, val);
11361 module_param(experimental, int, 0444);
11362 MODULE_PARM_DESC(experimental,
11363 "Enables experimental features when non-zero");
11365 module_param_named(debug, dbg_level, uint, 0);
11366 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
11368 module_param(force_load, bool, 0444);
11369 MODULE_PARM_DESC(force_load,
11370 "Attempts to load the driver even on a mis-identified ThinkPad when true");
11372 module_param_named(fan_control, fan_control_allowed, bool, 0444);
11373 MODULE_PARM_DESC(fan_control,
11374 "Enables setting fan parameters features when true");
11376 module_param_named(brightness_mode, brightness_mode, uint, 0444);
11377 MODULE_PARM_DESC(brightness_mode,
11378 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
11380 module_param(brightness_enable, uint, 0444);
11381 MODULE_PARM_DESC(brightness_enable,
11382 "Enables backlight control when 1, disables when 0");
11384 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
11385 module_param_named(volume_mode, volume_mode, uint, 0444);
11386 MODULE_PARM_DESC(volume_mode,
11387 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
11389 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11390 MODULE_PARM_DESC(volume_capabilities,
11391 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
11393 module_param_named(volume_control, volume_control_allowed, bool, 0444);
11394 MODULE_PARM_DESC(volume_control,
11395 "Enables software override for the console audio control when true");
11397 module_param_named(software_mute, software_mute_requested, bool, 0444);
11398 MODULE_PARM_DESC(software_mute,
11399 "Request full software mute control");
11401 /* ALSA module API parameters */
11402 module_param_named(index, alsa_index, int, 0444);
11403 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11404 module_param_named(id, alsa_id, charp, 0444);
11405 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11406 module_param_named(enable, alsa_enable, bool, 0444);
11407 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
11408 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
11410 /* The module parameter can't be read back, that's why 0 is used here */
11411 #define TPACPI_PARAM(feature) \
11412 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
11413 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
11415 TPACPI_PARAM(hotkey);
11416 TPACPI_PARAM(bluetooth);
11417 TPACPI_PARAM(video);
11418 TPACPI_PARAM(light);
11419 TPACPI_PARAM(cmos);
11421 TPACPI_PARAM(beep);
11422 TPACPI_PARAM(brightness);
11423 TPACPI_PARAM(volume);
11426 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11427 module_param(dbg_wlswemul, uint, 0444);
11428 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11429 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11430 MODULE_PARM_DESC(wlsw_state,
11431 "Initial state of the emulated WLSW switch");
11433 module_param(dbg_bluetoothemul, uint, 0444);
11434 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11435 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11436 MODULE_PARM_DESC(bluetooth_state,
11437 "Initial state of the emulated bluetooth switch");
11439 module_param(dbg_wwanemul, uint, 0444);
11440 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11441 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11442 MODULE_PARM_DESC(wwan_state,
11443 "Initial state of the emulated WWAN switch");
11445 module_param(dbg_uwbemul, uint, 0444);
11446 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11447 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11448 MODULE_PARM_DESC(uwb_state,
11449 "Initial state of the emulated UWB switch");
11452 static void thinkpad_acpi_module_exit(void)
11454 struct ibm_struct *ibm, *itmp;
11456 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11459 hwmon_device_unregister(tpacpi_hwmon);
11460 if (tp_features.sensors_pdrv_registered)
11461 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11462 if (tp_features.platform_drv_registered)
11463 platform_driver_unregister(&tpacpi_pdriver);
11465 list_for_each_entry_safe_reverse(ibm, itmp,
11466 &tpacpi_all_drivers,
11471 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11473 if (tpacpi_inputdev) {
11474 if (tp_features.input_device_registered)
11475 input_unregister_device(tpacpi_inputdev);
11477 input_free_device(tpacpi_inputdev);
11478 kfree(hotkey_keycode_map);
11481 if (tpacpi_sensors_pdev)
11482 platform_device_unregister(tpacpi_sensors_pdev);
11484 platform_device_unregister(tpacpi_pdev);
11486 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
11488 destroy_workqueue(tpacpi_wq);
11490 kfree(thinkpad_id.bios_version_str);
11491 kfree(thinkpad_id.ec_version_str);
11492 kfree(thinkpad_id.model_str);
11493 kfree(thinkpad_id.nummodel_str);
11497 static int __init thinkpad_acpi_module_init(void)
11501 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11503 /* Driver-level probe */
11505 ret = get_thinkpad_model_data(&thinkpad_id);
11507 pr_err("unable to get DMI data: %d\n", ret);
11508 thinkpad_acpi_module_exit();
11511 ret = probe_for_thinkpad();
11513 thinkpad_acpi_module_exit();
11517 /* Driver initialization */
11519 thinkpad_acpi_init_banner();
11520 tpacpi_check_outdated_fw();
11522 TPACPI_ACPIHANDLE_INIT(ecrd);
11523 TPACPI_ACPIHANDLE_INIT(ecwr);
11525 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11527 thinkpad_acpi_module_exit();
11531 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
11533 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
11534 thinkpad_acpi_module_exit();
11538 /* Device initialization */
11539 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
11541 if (IS_ERR(tpacpi_pdev)) {
11542 ret = PTR_ERR(tpacpi_pdev);
11543 tpacpi_pdev = NULL;
11544 pr_err("unable to register platform device\n");
11545 thinkpad_acpi_module_exit();
11548 tpacpi_sensors_pdev = platform_device_register_simple(
11549 TPACPI_HWMON_DRVR_NAME,
11551 if (IS_ERR(tpacpi_sensors_pdev)) {
11552 ret = PTR_ERR(tpacpi_sensors_pdev);
11553 tpacpi_sensors_pdev = NULL;
11554 pr_err("unable to register hwmon platform device\n");
11555 thinkpad_acpi_module_exit();
11559 mutex_init(&tpacpi_inputdev_send_mutex);
11560 tpacpi_inputdev = input_allocate_device();
11561 if (!tpacpi_inputdev) {
11562 thinkpad_acpi_module_exit();
11565 /* Prepare input device, but don't register */
11566 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
11567 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
11568 tpacpi_inputdev->id.bustype = BUS_HOST;
11569 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
11570 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11571 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
11572 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
11575 /* Init subdriver dependencies */
11576 tpacpi_detect_brightness_capabilities();
11578 /* Init subdrivers */
11579 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11580 ret = ibm_init(&ibms_init[i]);
11581 if (ret >= 0 && *ibms_init[i].param)
11582 ret = ibms_init[i].data->write(ibms_init[i].param);
11584 thinkpad_acpi_module_exit();
11589 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11591 ret = platform_driver_register(&tpacpi_pdriver);
11593 pr_err("unable to register main platform driver\n");
11594 thinkpad_acpi_module_exit();
11597 tp_features.platform_drv_registered = 1;
11599 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11601 pr_err("unable to register hwmon platform driver\n");
11602 thinkpad_acpi_module_exit();
11605 tp_features.sensors_pdrv_registered = 1;
11607 tpacpi_hwmon = hwmon_device_register_with_groups(
11608 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
11609 if (IS_ERR(tpacpi_hwmon)) {
11610 ret = PTR_ERR(tpacpi_hwmon);
11611 tpacpi_hwmon = NULL;
11612 pr_err("unable to register hwmon device\n");
11613 thinkpad_acpi_module_exit();
11617 ret = input_register_device(tpacpi_inputdev);
11619 pr_err("unable to register input device\n");
11620 thinkpad_acpi_module_exit();
11623 tp_features.input_device_registered = 1;
11629 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11632 * This will autoload the driver in almost every ThinkPad
11633 * in widespread use.
11635 * Only _VERY_ old models, like the 240, 240x and 570 lack
11636 * the HKEY event interface.
11638 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11641 * DMI matching for module autoloading
11643 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11644 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
11646 * Only models listed in thinkwiki will be supported, so add yours
11647 * if it is not there yet.
11649 #define IBM_BIOS_MODULE_ALIAS(__type) \
11650 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
11652 /* Ancient thinkpad BIOSes have to be identified by
11653 * BIOS type or model number, and there are far less
11654 * BIOS types than model numbers... */
11655 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
11657 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
11658 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
11659 MODULE_DESCRIPTION(TPACPI_DESC);
11660 MODULE_VERSION(TPACPI_VERSION);
11661 MODULE_LICENSE("GPL");
11663 module_init(thinkpad_acpi_module_init);
11664 module_exit(thinkpad_acpi_module_exit);