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 struct acpi_device *dev;
732 if (!strcmp(context, "video")) {
733 if (acpi_bus_get_device(handle, &dev))
735 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
739 *(acpi_handle *)return_value = handle;
741 return AE_CTRL_TERMINATE;
744 static void __init tpacpi_acpi_handle_locate(const char *name,
749 acpi_handle device_found;
751 BUG_ON(!name || !handle);
752 vdbg_printk(TPACPI_DBG_INIT,
753 "trying to locate ACPI handle for %s, using HID %s\n",
754 name, hid ? hid : "NULL");
756 memset(&device_found, 0, sizeof(device_found));
757 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
758 (void *)name, &device_found);
762 if (ACPI_SUCCESS(status)) {
763 *handle = device_found;
764 dbg_printk(TPACPI_DBG_INIT,
765 "Found ACPI handle for %s\n", name);
767 vdbg_printk(TPACPI_DBG_INIT,
768 "Could not locate an ACPI handle for %s: %s\n",
769 name, acpi_format_exception(status));
773 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
775 struct ibm_struct *ibm = data;
777 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
780 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
783 ibm->acpi->notify(ibm, event);
786 static int __init setup_acpi_notify(struct ibm_struct *ibm)
793 if (!*ibm->acpi->handle)
796 vdbg_printk(TPACPI_DBG_INIT,
797 "setting up ACPI notify for %s\n", ibm->name);
799 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
801 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
805 ibm->acpi->device->driver_data = ibm;
806 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
807 TPACPI_ACPI_EVENT_PREFIX,
810 status = acpi_install_notify_handler(*ibm->acpi->handle,
811 ibm->acpi->type, dispatch_acpi_notify, ibm);
812 if (ACPI_FAILURE(status)) {
813 if (status == AE_ALREADY_EXISTS) {
814 pr_notice("another device driver is already handling %s events\n",
817 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
818 ibm->name, acpi_format_exception(status));
822 ibm->flags.acpi_notify_installed = 1;
826 static int __init tpacpi_device_add(struct acpi_device *device)
831 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
835 dbg_printk(TPACPI_DBG_INIT,
836 "registering %s as an ACPI driver\n", ibm->name);
840 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
841 if (!ibm->acpi->driver) {
842 pr_err("failed to allocate memory for ibm->acpi->driver\n");
846 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
847 ibm->acpi->driver->ids = ibm->acpi->hid;
849 ibm->acpi->driver->ops.add = &tpacpi_device_add;
851 rc = acpi_bus_register_driver(ibm->acpi->driver);
853 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
855 kfree(ibm->acpi->driver);
856 ibm->acpi->driver = NULL;
858 ibm->flags.acpi_driver_registered = 1;
864 /****************************************************************************
865 ****************************************************************************
869 ****************************************************************************
870 ****************************************************************************/
872 static int dispatch_proc_show(struct seq_file *m, void *v)
874 struct ibm_struct *ibm = m->private;
876 if (!ibm || !ibm->read)
881 static int dispatch_proc_open(struct inode *inode, struct file *file)
883 return single_open(file, dispatch_proc_show, pde_data(inode));
886 static ssize_t dispatch_proc_write(struct file *file,
887 const char __user *userbuf,
888 size_t count, loff_t *pos)
890 struct ibm_struct *ibm = pde_data(file_inode(file));
894 if (!ibm || !ibm->write)
896 if (count > PAGE_SIZE - 1)
899 kernbuf = kmalloc(count + 1, GFP_KERNEL);
903 if (copy_from_user(kernbuf, userbuf, count)) {
909 ret = ibm->write(kernbuf);
918 static const struct proc_ops dispatch_proc_ops = {
919 .proc_open = dispatch_proc_open,
920 .proc_read = seq_read,
921 .proc_lseek = seq_lseek,
922 .proc_release = single_release,
923 .proc_write = dispatch_proc_write,
926 /****************************************************************************
927 ****************************************************************************
929 * Device model: input, hwmon and platform
931 ****************************************************************************
932 ****************************************************************************/
934 static struct platform_device *tpacpi_pdev;
935 static struct platform_device *tpacpi_sensors_pdev;
936 static struct device *tpacpi_hwmon;
937 static struct input_dev *tpacpi_inputdev;
938 static struct mutex tpacpi_inputdev_send_mutex;
939 static LIST_HEAD(tpacpi_all_drivers);
941 #ifdef CONFIG_PM_SLEEP
942 static int tpacpi_suspend_handler(struct device *dev)
944 struct ibm_struct *ibm, *itmp;
946 list_for_each_entry_safe(ibm, itmp,
956 static int tpacpi_resume_handler(struct device *dev)
958 struct ibm_struct *ibm, *itmp;
960 list_for_each_entry_safe(ibm, itmp,
971 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
972 tpacpi_suspend_handler, tpacpi_resume_handler);
974 static void tpacpi_shutdown_handler(struct platform_device *pdev)
976 struct ibm_struct *ibm, *itmp;
978 list_for_each_entry_safe(ibm, itmp,
986 /*************************************************************************
987 * sysfs support helpers
990 static int parse_strtoul(const char *buf,
991 unsigned long max, unsigned long *value)
995 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
996 endp = skip_spaces(endp);
997 if (*endp || *value > max)
1003 static void tpacpi_disable_brightness_delay(void)
1005 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1006 pr_notice("ACPI backlight control delay disabled\n");
1009 static void printk_deprecated_attribute(const char * const what,
1010 const char * const details)
1012 tpacpi_log_usertask("deprecated sysfs attribute");
1013 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1017 /*************************************************************************
1018 * rfkill and radio control support helpers
1022 * ThinkPad-ACPI firmware handling model:
1024 * WLSW (master wireless switch) is event-driven, and is common to all
1025 * firmware-controlled radios. It cannot be controlled, just monitored,
1026 * as expected. It overrides all radio state in firmware
1028 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1029 * (TODO: verify how WLSW interacts with the returned radio state).
1031 * The only time there are shadow radio state changes, is when
1032 * masked-off hotkeys are used.
1036 * Internal driver API for radio state:
1038 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1039 * bool: true means radio blocked (off)
1041 enum tpacpi_rfkill_state {
1042 TPACPI_RFK_RADIO_OFF = 0,
1046 /* rfkill switches */
1047 enum tpacpi_rfk_id {
1048 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1049 TPACPI_RFK_WWAN_SW_ID,
1050 TPACPI_RFK_UWB_SW_ID,
1054 static const char *tpacpi_rfkill_names[] = {
1055 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1056 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1057 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1058 [TPACPI_RFK_SW_MAX] = NULL
1061 /* ThinkPad-ACPI rfkill subdriver */
1063 struct rfkill *rfkill;
1064 enum tpacpi_rfk_id id;
1065 const struct tpacpi_rfk_ops *ops;
1068 struct tpacpi_rfk_ops {
1069 /* firmware interface */
1070 int (*get_status)(void);
1071 int (*set_status)(const enum tpacpi_rfkill_state);
1074 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1076 /* Query FW and update rfkill sw state for a given rfkill switch */
1077 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1084 status = (tp_rfk->ops->get_status)();
1088 rfkill_set_sw_state(tp_rfk->rfkill,
1089 (status == TPACPI_RFK_RADIO_OFF));
1095 * Sync the HW-blocking state of all rfkill switches,
1096 * do notice it causes the rfkill core to schedule uevents
1098 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1101 struct tpacpi_rfk *tp_rfk;
1103 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1104 tp_rfk = tpacpi_rfkill_switches[i];
1106 if (rfkill_set_hw_state(tp_rfk->rfkill,
1108 /* ignore -- we track sw block */
1114 /* Call to get the WLSW state from the firmware */
1115 static int hotkey_get_wlsw(void);
1117 /* Call to query WLSW state and update all rfkill switches */
1118 static bool tpacpi_rfk_check_hwblock_state(void)
1120 int res = hotkey_get_wlsw();
1123 /* When unknown or unsupported, we have to assume it is unblocked */
1127 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1128 tpacpi_rfk_update_hwblock_state(hw_blocked);
1133 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1135 struct tpacpi_rfk *tp_rfk = data;
1138 dbg_printk(TPACPI_DBG_RFKILL,
1139 "request to change radio state to %s\n",
1140 blocked ? "blocked" : "unblocked");
1142 /* try to set radio state */
1143 res = (tp_rfk->ops->set_status)(blocked ?
1144 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1146 /* and update the rfkill core with whatever the FW really did */
1147 tpacpi_rfk_update_swstate(tp_rfk);
1149 return (res < 0) ? res : 0;
1152 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1153 .set_block = tpacpi_rfk_hook_set_block,
1156 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1157 const struct tpacpi_rfk_ops *tp_rfkops,
1158 const enum rfkill_type rfktype,
1160 const bool set_default)
1162 struct tpacpi_rfk *atp_rfk;
1164 bool sw_state = false;
1168 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1170 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1172 atp_rfk->rfkill = rfkill_alloc(name,
1175 &tpacpi_rfk_rfkill_ops,
1177 if (!atp_rfk || !atp_rfk->rfkill) {
1178 pr_err("failed to allocate memory for rfkill class\n");
1184 atp_rfk->ops = tp_rfkops;
1186 sw_status = (tp_rfkops->get_status)();
1187 if (sw_status < 0) {
1188 pr_err("failed to read initial state for %s, error %d\n",
1191 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1193 /* try to keep the initial state, since we ask the
1194 * firmware to preserve it across S5 in NVRAM */
1195 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1198 hw_state = tpacpi_rfk_check_hwblock_state();
1199 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1201 res = rfkill_register(atp_rfk->rfkill);
1203 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1204 rfkill_destroy(atp_rfk->rfkill);
1209 tpacpi_rfkill_switches[id] = atp_rfk;
1211 pr_info("rfkill switch %s: radio is %sblocked\n",
1212 name, (sw_state || hw_state) ? "" : "un");
1216 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1218 struct tpacpi_rfk *tp_rfk;
1220 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1222 tp_rfk = tpacpi_rfkill_switches[id];
1224 rfkill_unregister(tp_rfk->rfkill);
1225 rfkill_destroy(tp_rfk->rfkill);
1226 tpacpi_rfkill_switches[id] = NULL;
1231 static void printk_deprecated_rfkill_attribute(const char * const what)
1233 printk_deprecated_attribute(what,
1234 "Please switch to generic rfkill before year 2010");
1237 /* sysfs <radio> enable ------------------------------------------------ */
1238 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1239 struct device_attribute *attr,
1244 printk_deprecated_rfkill_attribute(attr->attr.name);
1246 /* This is in the ABI... */
1247 if (tpacpi_rfk_check_hwblock_state()) {
1248 status = TPACPI_RFK_RADIO_OFF;
1250 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1255 return sysfs_emit(buf, "%d\n",
1256 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1259 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1266 printk_deprecated_rfkill_attribute(attr->attr.name);
1268 if (parse_strtoul(buf, 1, &t))
1271 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1273 /* This is in the ABI... */
1274 if (tpacpi_rfk_check_hwblock_state() && !!t)
1277 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1278 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1279 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1281 return (res < 0) ? res : count;
1284 /* procfs -------------------------------------------------------------- */
1285 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1287 if (id >= TPACPI_RFK_SW_MAX)
1288 seq_printf(m, "status:\t\tnot supported\n");
1292 /* This is in the ABI... */
1293 if (tpacpi_rfk_check_hwblock_state()) {
1294 status = TPACPI_RFK_RADIO_OFF;
1296 status = tpacpi_rfk_update_swstate(
1297 tpacpi_rfkill_switches[id]);
1302 seq_printf(m, "status:\t\t%s\n",
1303 (status == TPACPI_RFK_RADIO_ON) ?
1304 "enabled" : "disabled");
1305 seq_printf(m, "commands:\tenable, disable\n");
1311 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1317 if (id >= TPACPI_RFK_SW_MAX)
1320 while ((cmd = strsep(&buf, ","))) {
1321 if (strlencmp(cmd, "enable") == 0)
1322 status = TPACPI_RFK_RADIO_ON;
1323 else if (strlencmp(cmd, "disable") == 0)
1324 status = TPACPI_RFK_RADIO_OFF;
1330 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1331 (status == TPACPI_RFK_RADIO_ON) ?
1332 "enable" : "disable",
1333 tpacpi_rfkill_names[id]);
1334 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1335 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1341 /*************************************************************************
1342 * thinkpad-acpi driver attributes
1345 /* interface_version --------------------------------------------------- */
1346 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1348 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
1350 static DRIVER_ATTR_RO(interface_version);
1352 /* debug_level --------------------------------------------------------- */
1353 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1355 return sysfs_emit(buf, "0x%04x\n", dbg_level);
1358 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1363 if (parse_strtoul(buf, 0xffff, &t))
1370 static DRIVER_ATTR_RW(debug_level);
1372 /* version ------------------------------------------------------------- */
1373 static ssize_t version_show(struct device_driver *drv, char *buf)
1375 return sysfs_emit(buf, "%s v%s\n",
1376 TPACPI_DESC, TPACPI_VERSION);
1378 static DRIVER_ATTR_RO(version);
1380 /* --------------------------------------------------------------------- */
1382 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1384 /* wlsw_emulstate ------------------------------------------------------ */
1385 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1387 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
1390 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1395 if (parse_strtoul(buf, 1, &t))
1398 if (tpacpi_wlsw_emulstate != !!t) {
1399 tpacpi_wlsw_emulstate = !!t;
1400 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1405 static DRIVER_ATTR_RW(wlsw_emulstate);
1407 /* bluetooth_emulstate ------------------------------------------------- */
1408 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1410 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
1413 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1414 const char *buf, size_t count)
1418 if (parse_strtoul(buf, 1, &t))
1421 tpacpi_bluetooth_emulstate = !!t;
1425 static DRIVER_ATTR_RW(bluetooth_emulstate);
1427 /* wwan_emulstate ------------------------------------------------- */
1428 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1430 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
1433 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1438 if (parse_strtoul(buf, 1, &t))
1441 tpacpi_wwan_emulstate = !!t;
1445 static DRIVER_ATTR_RW(wwan_emulstate);
1447 /* uwb_emulstate ------------------------------------------------- */
1448 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1450 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
1453 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1458 if (parse_strtoul(buf, 1, &t))
1461 tpacpi_uwb_emulstate = !!t;
1465 static DRIVER_ATTR_RW(uwb_emulstate);
1468 /*************************************************************************
1473 * Table of recommended minimum BIOS versions
1475 * Reasons for listing:
1476 * 1. Stable BIOS, listed because the unknown amount of
1477 * bugs and bad ACPI behaviour on older versions
1479 * 2. BIOS or EC fw with known bugs that trigger on Linux
1481 * 3. BIOS with known reduced functionality in older versions
1483 * We recommend the latest BIOS and EC version.
1484 * We only support the latest BIOS and EC fw version as a rule.
1486 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1487 * Information from users in ThinkWiki
1489 * WARNING: we use this table also to detect that the machine is
1490 * a ThinkPad in some cases, so don't remove entries lightly.
1493 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1494 { .vendor = (__v), \
1495 .bios = TPID(__id1, __id2), \
1496 .ec = TPACPI_MATCH_ANY, \
1497 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1498 | TPVER(__bv1, __bv2) }
1500 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1501 __eid, __ev1, __ev2) \
1502 { .vendor = (__v), \
1503 .bios = TPID(__bid1, __bid2), \
1505 .quirks = TPVER(__ev1, __ev2) << 16 \
1506 | TPVER(__bv1, __bv2) }
1508 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1509 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1511 /* Outdated IBM BIOSes often lack the EC id string */
1512 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1513 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1514 __bv1, __bv2, TPID(__id1, __id2), \
1516 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1517 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1520 /* Outdated IBM BIOSes often lack the EC id string */
1521 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1522 __eid1, __eid2, __ev1, __ev2) \
1523 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1524 __bv1, __bv2, TPID(__eid1, __eid2), \
1526 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1527 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1530 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1531 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1533 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1534 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1535 __bv1, __bv2, TPID(__id1, __id2), \
1538 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1539 __eid1, __eid2, __ev1, __ev2) \
1540 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1541 __bv1, __bv2, TPID(__eid1, __eid2), \
1544 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1545 /* Numeric models ------------------ */
1546 /* FW MODEL BIOS VERS */
1547 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1548 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1549 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1550 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1551 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1552 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1553 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1554 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1555 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1557 /* A-series ------------------------- */
1558 /* FW MODEL BIOS VERS EC VERS */
1559 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1560 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1561 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1562 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1563 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1564 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1565 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1566 TPV_QI0('1', '3', '2', '0'), /* A22m */
1567 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1568 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1569 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1571 /* G-series ------------------------- */
1572 /* FW MODEL BIOS VERS */
1573 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1574 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1576 /* R-series, T-series --------------- */
1577 /* FW MODEL BIOS VERS EC VERS */
1578 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1579 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1580 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1581 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1582 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1583 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1584 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1585 T40/p, T41/p, T42/p (1) */
1586 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1587 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1588 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1589 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1591 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1592 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1593 TPV_QI0('1', '6', '3', '2'), /* T22 */
1594 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1595 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1596 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1598 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1599 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1600 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1602 /* BIOS FW BIOS VERS EC FW EC VERS */
1603 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1604 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1606 /* X-series ------------------------- */
1607 /* FW MODEL BIOS VERS EC VERS */
1608 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1609 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1610 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1611 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1612 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1613 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1614 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1616 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1617 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1619 /* (0) - older versions lack DMI EC fw string and functionality */
1620 /* (1) - older versions known to lack functionality */
1631 static void __init tpacpi_check_outdated_fw(void)
1633 unsigned long fwvers;
1634 u16 ec_version, bios_version;
1636 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1637 ARRAY_SIZE(tpacpi_bios_version_qtable));
1642 bios_version = fwvers & 0xffffU;
1643 ec_version = (fwvers >> 16) & 0xffffU;
1645 /* note that unknown versions are set to 0x0000 and we use that */
1646 if ((bios_version > thinkpad_id.bios_release) ||
1647 (ec_version > thinkpad_id.ec_release &&
1648 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1650 * The changelogs would let us track down the exact
1651 * reason, but it is just too much of a pain to track
1652 * it. We only list BIOSes that are either really
1653 * broken, or really stable to begin with, so it is
1654 * best if the user upgrades the firmware anyway.
1656 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1657 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1661 static bool __init tpacpi_is_fw_known(void)
1663 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1664 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1667 /****************************************************************************
1668 ****************************************************************************
1672 ****************************************************************************
1673 ****************************************************************************/
1675 /*************************************************************************
1676 * thinkpad-acpi metadata subdriver
1679 static int thinkpad_acpi_driver_read(struct seq_file *m)
1681 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1682 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1686 static struct ibm_struct thinkpad_acpi_driver_data = {
1688 .read = thinkpad_acpi_driver_read,
1691 /*************************************************************************
1696 * ThinkPad firmware event model
1698 * The ThinkPad firmware has two main event interfaces: normal ACPI
1699 * notifications (which follow the ACPI standard), and a private event
1702 * The private event interface also issues events for the hotkeys. As
1703 * the driver gained features, the event handling code ended up being
1704 * built around the hotkey subdriver. This will need to be refactored
1705 * to a more formal event API eventually.
1707 * Some "hotkeys" are actually supposed to be used as event reports,
1708 * such as "brightness has changed", "volume has changed", depending on
1709 * the ThinkPad model and how the firmware is operating.
1711 * Unlike other classes, hotkey-class events have mask/unmask control on
1712 * non-ancient firmware. However, how it behaves changes a lot with the
1713 * firmware model and version.
1716 enum { /* hot key scan codes (derived from ACPI DSDT) */
1717 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1718 TP_ACPI_HOTKEYSCAN_FNF2,
1719 TP_ACPI_HOTKEYSCAN_FNF3,
1720 TP_ACPI_HOTKEYSCAN_FNF4,
1721 TP_ACPI_HOTKEYSCAN_FNF5,
1722 TP_ACPI_HOTKEYSCAN_FNF6,
1723 TP_ACPI_HOTKEYSCAN_FNF7,
1724 TP_ACPI_HOTKEYSCAN_FNF8,
1725 TP_ACPI_HOTKEYSCAN_FNF9,
1726 TP_ACPI_HOTKEYSCAN_FNF10,
1727 TP_ACPI_HOTKEYSCAN_FNF11,
1728 TP_ACPI_HOTKEYSCAN_FNF12,
1729 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1730 TP_ACPI_HOTKEYSCAN_FNINSERT,
1731 TP_ACPI_HOTKEYSCAN_FNDELETE,
1732 TP_ACPI_HOTKEYSCAN_FNHOME,
1733 TP_ACPI_HOTKEYSCAN_FNEND,
1734 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1735 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1736 TP_ACPI_HOTKEYSCAN_FNSPACE,
1737 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1738 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1739 TP_ACPI_HOTKEYSCAN_MUTE,
1740 TP_ACPI_HOTKEYSCAN_THINKPAD,
1741 TP_ACPI_HOTKEYSCAN_UNK1,
1742 TP_ACPI_HOTKEYSCAN_UNK2,
1743 TP_ACPI_HOTKEYSCAN_UNK3,
1744 TP_ACPI_HOTKEYSCAN_UNK4,
1745 TP_ACPI_HOTKEYSCAN_UNK5,
1746 TP_ACPI_HOTKEYSCAN_UNK6,
1747 TP_ACPI_HOTKEYSCAN_UNK7,
1748 TP_ACPI_HOTKEYSCAN_UNK8,
1750 /* Adaptive keyboard keycodes */
1751 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1752 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1753 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1754 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1755 TP_ACPI_HOTKEYSCAN_CLOUD,
1756 TP_ACPI_HOTKEYSCAN_UNK9,
1757 TP_ACPI_HOTKEYSCAN_VOICE,
1758 TP_ACPI_HOTKEYSCAN_UNK10,
1759 TP_ACPI_HOTKEYSCAN_GESTURES,
1760 TP_ACPI_HOTKEYSCAN_UNK11,
1761 TP_ACPI_HOTKEYSCAN_UNK12,
1762 TP_ACPI_HOTKEYSCAN_UNK13,
1763 TP_ACPI_HOTKEYSCAN_CONFIG,
1764 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1765 TP_ACPI_HOTKEYSCAN_RELOAD,
1766 TP_ACPI_HOTKEYSCAN_BACK,
1767 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1768 TP_ACPI_HOTKEYSCAN_MIC_UP,
1769 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1770 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1771 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1773 /* Lenovo extended keymap, starting at 0x1300 */
1774 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1775 /* first new observed key (star, favorites) is 0x1311 */
1776 TP_ACPI_HOTKEYSCAN_STAR = 69,
1777 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1778 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1779 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1780 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1781 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1782 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1783 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1784 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1786 /* Hotkey keymap size */
1787 TPACPI_HOTKEY_MAP_LEN
1790 enum { /* Keys/events available through NVRAM polling */
1791 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1792 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1795 enum { /* Positions of some of the keys in hotkey masks */
1796 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1797 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1798 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1799 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1800 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1801 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1802 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1803 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1804 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1805 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1806 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1809 enum { /* NVRAM to ACPI HKEY group map */
1810 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1811 TP_ACPI_HKEY_ZOOM_MASK |
1812 TP_ACPI_HKEY_DISPSWTCH_MASK |
1813 TP_ACPI_HKEY_HIBERNATE_MASK,
1814 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1815 TP_ACPI_HKEY_BRGHTDWN_MASK,
1816 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1817 TP_ACPI_HKEY_VOLDWN_MASK |
1818 TP_ACPI_HKEY_MUTE_MASK,
1821 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1822 struct tp_nvram_state {
1823 u16 thinkpad_toggle:1;
1825 u16 display_toggle:1;
1826 u16 thinklight_toggle:1;
1827 u16 hibernate_toggle:1;
1828 u16 displayexp_toggle:1;
1829 u16 display_state:1;
1830 u16 brightness_toggle:1;
1831 u16 volume_toggle:1;
1834 u8 brightness_level;
1838 /* kthread for the hotkey poller */
1839 static struct task_struct *tpacpi_hotkey_task;
1842 * Acquire mutex to write poller control variables as an
1845 * Increment hotkey_config_change when changing them if you
1846 * want the kthread to forget old state.
1848 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1850 static struct mutex hotkey_thread_data_mutex;
1851 static unsigned int hotkey_config_change;
1854 * hotkey poller control variables
1856 * Must be atomic or readers will also need to acquire mutex
1858 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1859 * should be used only when the changes need to be taken as
1860 * a block, OR when one needs to force the kthread to forget
1863 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1864 static unsigned int hotkey_poll_freq = 10; /* Hz */
1866 #define HOTKEY_CONFIG_CRITICAL_START \
1868 mutex_lock(&hotkey_thread_data_mutex); \
1869 hotkey_config_change++; \
1871 #define HOTKEY_CONFIG_CRITICAL_END \
1872 mutex_unlock(&hotkey_thread_data_mutex);
1874 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1876 #define hotkey_source_mask 0U
1877 #define HOTKEY_CONFIG_CRITICAL_START
1878 #define HOTKEY_CONFIG_CRITICAL_END
1880 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1882 static struct mutex hotkey_mutex;
1884 static enum { /* Reasons for waking up */
1885 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1886 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1887 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1888 } hotkey_wakeup_reason;
1890 static int hotkey_autosleep_ack;
1892 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1893 static u32 hotkey_all_mask; /* all events supported in fw */
1894 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
1895 static u32 hotkey_reserved_mask; /* events better left disabled */
1896 static u32 hotkey_driver_mask; /* events needed by the driver */
1897 static u32 hotkey_user_mask; /* events visible to userspace */
1898 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1900 static u16 *hotkey_keycode_map;
1902 static void tpacpi_driver_event(const unsigned int hkey_event);
1903 static void hotkey_driver_event(const unsigned int scancode);
1904 static void hotkey_poll_setup(const bool may_warn);
1906 /* HKEY.MHKG() return bits */
1907 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1909 TP_ACPI_MULTI_MODE_INVALID = 0,
1910 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1911 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1912 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1913 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1914 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1915 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1916 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1920 /* The following modes are considered tablet mode for the purpose of
1921 * reporting the status to userspace. i.e. in all these modes it makes
1922 * sense to disable the laptop input devices such as touchpad and
1925 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1926 TP_ACPI_MULTI_MODE_STAND |
1927 TP_ACPI_MULTI_MODE_TENT |
1928 TP_ACPI_MULTI_MODE_STAND_TENT,
1931 static int hotkey_get_wlsw(void)
1935 if (!tp_features.hotkey_wlsw)
1938 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1940 return (tpacpi_wlsw_emulstate) ?
1941 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1944 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1947 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1950 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1952 int type = (s >> 16) & 0xffff;
1953 int value = s & 0xffff;
1954 int mode = TP_ACPI_MULTI_MODE_INVALID;
1955 int valid_modes = 0;
1957 if (has_tablet_mode)
1958 *has_tablet_mode = 0;
1962 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1963 TP_ACPI_MULTI_MODE_TABLET |
1964 TP_ACPI_MULTI_MODE_STAND_TENT;
1967 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1968 TP_ACPI_MULTI_MODE_FLAT |
1969 TP_ACPI_MULTI_MODE_TABLET |
1970 TP_ACPI_MULTI_MODE_STAND |
1971 TP_ACPI_MULTI_MODE_TENT;
1974 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1975 TP_ACPI_MULTI_MODE_FLAT;
1979 /* In mode 4, FLAT is not specified as a valid mode. However,
1980 * it can be seen at least on the X1 Yoga 2nd Generation.
1982 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1983 TP_ACPI_MULTI_MODE_FLAT |
1984 TP_ACPI_MULTI_MODE_TABLET |
1985 TP_ACPI_MULTI_MODE_STAND |
1986 TP_ACPI_MULTI_MODE_TENT;
1989 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
1990 type, value, TPACPI_MAIL);
1994 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
1995 *has_tablet_mode = 1;
1999 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2002 mode = TP_ACPI_MULTI_MODE_FLAT;
2005 mode = TP_ACPI_MULTI_MODE_TABLET;
2009 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2011 mode = TP_ACPI_MULTI_MODE_STAND;
2014 mode = TP_ACPI_MULTI_MODE_TENT;
2017 if (type == 5 && value == 0xffff) {
2018 pr_warn("Multi mode status is undetected, assuming laptop\n");
2023 if (!(mode & valid_modes)) {
2024 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2025 value, type, TPACPI_MAIL);
2029 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2032 static int hotkey_get_tablet_mode(int *status)
2036 switch (tp_features.hotkey_tablet) {
2037 case TP_HOTKEY_TABLET_USES_MHKG:
2038 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2041 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2043 case TP_HOTKEY_TABLET_USES_GMMS:
2044 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2047 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2057 * Reads current event mask from firmware, and updates
2058 * hotkey_acpi_mask accordingly. Also resets any bits
2059 * from hotkey_user_mask that are unavailable to be
2060 * delivered (shadow requirement of the userspace ABI).
2062 * Call with hotkey_mutex held
2064 static int hotkey_mask_get(void)
2066 if (tp_features.hotkey_mask) {
2069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2072 hotkey_acpi_mask = m;
2074 /* no mask support doesn't mean no event support... */
2075 hotkey_acpi_mask = hotkey_all_mask;
2078 /* sync userspace-visible mask */
2079 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2084 static void hotkey_mask_warn_incomplete_mask(void)
2086 /* log only what the user can fix... */
2087 const u32 wantedmask = hotkey_driver_mask &
2088 ~(hotkey_acpi_mask | hotkey_source_mask) &
2089 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2092 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2096 * Set the firmware mask when supported
2098 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2100 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2102 * Call with hotkey_mutex held
2104 static int hotkey_mask_set(u32 mask)
2109 const u32 fwmask = mask & ~hotkey_source_mask;
2111 if (tp_features.hotkey_mask) {
2112 for (i = 0; i < 32; i++) {
2113 if (!acpi_evalf(hkey_handle,
2114 NULL, "MHKM", "vdd", i + 1,
2115 !!(mask & (1 << i)))) {
2123 * We *must* make an inconditional call to hotkey_mask_get to
2124 * refresh hotkey_acpi_mask and update hotkey_user_mask
2126 * Take the opportunity to also log when we cannot _enable_
2129 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2130 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2131 fwmask, hotkey_acpi_mask);
2134 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2135 hotkey_mask_warn_incomplete_mask();
2141 * Sets hotkey_user_mask and tries to set the firmware mask
2143 * Call with hotkey_mutex held
2145 static int hotkey_user_mask_set(const u32 mask)
2149 /* Give people a chance to notice they are doing something that
2150 * is bound to go boom on their users sooner or later */
2151 if (!tp_warned.hotkey_mask_ff &&
2152 (mask == 0xffff || mask == 0xffffff ||
2153 mask == 0xffffffff)) {
2154 tp_warned.hotkey_mask_ff = 1;
2155 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2157 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2160 /* Try to enable what the user asked for, plus whatever we need.
2161 * this syncs everything but won't enable bits in hotkey_user_mask */
2162 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2164 /* Enable the available bits in hotkey_user_mask */
2165 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2171 * Sets the driver hotkey mask.
2173 * Can be called even if the hotkey subdriver is inactive
2175 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2179 /* Do the right thing if hotkey_init has not been called yet */
2180 if (!tp_features.hotkey) {
2181 hotkey_driver_mask = mask;
2185 mutex_lock(&hotkey_mutex);
2187 HOTKEY_CONFIG_CRITICAL_START
2188 hotkey_driver_mask = mask;
2189 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2190 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2192 HOTKEY_CONFIG_CRITICAL_END
2194 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2195 ~hotkey_source_mask);
2196 hotkey_poll_setup(true);
2198 mutex_unlock(&hotkey_mutex);
2203 static int hotkey_status_get(int *status)
2205 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2211 static int hotkey_status_set(bool enable)
2213 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2219 static void tpacpi_input_send_tabletsw(void)
2223 if (tp_features.hotkey_tablet &&
2224 !hotkey_get_tablet_mode(&state)) {
2225 mutex_lock(&tpacpi_inputdev_send_mutex);
2227 input_report_switch(tpacpi_inputdev,
2228 SW_TABLET_MODE, !!state);
2229 input_sync(tpacpi_inputdev);
2231 mutex_unlock(&tpacpi_inputdev_send_mutex);
2235 /* Do NOT call without validating scancode first */
2236 static void tpacpi_input_send_key(const unsigned int scancode)
2238 const unsigned int keycode = hotkey_keycode_map[scancode];
2240 if (keycode != KEY_RESERVED) {
2241 mutex_lock(&tpacpi_inputdev_send_mutex);
2243 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2244 input_report_key(tpacpi_inputdev, keycode, 1);
2245 input_sync(tpacpi_inputdev);
2247 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2248 input_report_key(tpacpi_inputdev, keycode, 0);
2249 input_sync(tpacpi_inputdev);
2251 mutex_unlock(&tpacpi_inputdev_send_mutex);
2255 /* Do NOT call without validating scancode first */
2256 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2258 hotkey_driver_event(scancode);
2259 if (hotkey_user_mask & (1 << scancode))
2260 tpacpi_input_send_key(scancode);
2263 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2264 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2266 /* Do NOT call without validating scancode first */
2267 static void tpacpi_hotkey_send_key(unsigned int scancode)
2269 tpacpi_input_send_key_masked(scancode);
2272 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2276 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2277 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2278 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2279 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2280 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2281 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2283 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2284 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2285 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2287 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2288 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2289 n->displayexp_toggle =
2290 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2292 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2293 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2294 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2295 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2296 n->brightness_toggle =
2297 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2299 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2301 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2302 >> TP_NVRAM_POS_LEVEL_VOLUME;
2303 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2304 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2308 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2310 if ((event_mask & (1 << __scancode)) && \
2311 oldn->__member != newn->__member) \
2312 tpacpi_hotkey_send_key(__scancode); \
2315 #define TPACPI_MAY_SEND_KEY(__scancode) \
2317 if (event_mask & (1 << __scancode)) \
2318 tpacpi_hotkey_send_key(__scancode); \
2321 static void issue_volchange(const unsigned int oldvol,
2322 const unsigned int newvol,
2323 const u32 event_mask)
2325 unsigned int i = oldvol;
2327 while (i > newvol) {
2328 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2331 while (i < newvol) {
2332 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2337 static void issue_brightnesschange(const unsigned int oldbrt,
2338 const unsigned int newbrt,
2339 const u32 event_mask)
2341 unsigned int i = oldbrt;
2343 while (i > newbrt) {
2344 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2347 while (i < newbrt) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2353 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2354 struct tp_nvram_state *newn,
2355 const u32 event_mask)
2358 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2359 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2360 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2361 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2363 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2370 * This code is supposed to duplicate the IBM firmware behaviour:
2371 * - Pressing MUTE issues mute hotkey message, even when already mute
2372 * - Pressing Volume up/down issues volume up/down hotkey messages,
2373 * even when already at maximum or minimum volume
2374 * - The act of unmuting issues volume up/down notification,
2375 * depending which key was used to unmute
2377 * We are constrained to what the NVRAM can tell us, which is not much
2378 * and certainly not enough if more than one volume hotkey was pressed
2379 * since the last poll cycle.
2381 * Just to make our life interesting, some newer Lenovo ThinkPads have
2382 * bugs in the BIOS and may fail to update volume_toggle properly.
2387 oldn->volume_toggle != newn->volume_toggle ||
2388 oldn->volume_level != newn->volume_level) {
2389 /* recently muted, or repeated mute keypress, or
2390 * multiple presses ending in mute */
2391 issue_volchange(oldn->volume_level, newn->volume_level,
2393 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2398 /* recently unmuted, issue 'unmute' keypress */
2399 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2401 if (oldn->volume_level != newn->volume_level) {
2402 issue_volchange(oldn->volume_level, newn->volume_level,
2404 } else if (oldn->volume_toggle != newn->volume_toggle) {
2405 /* repeated vol up/down keypress at end of scale ? */
2406 if (newn->volume_level == 0)
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2408 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2413 /* handle brightness */
2414 if (oldn->brightness_level != newn->brightness_level) {
2415 issue_brightnesschange(oldn->brightness_level,
2416 newn->brightness_level, event_mask);
2417 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2418 /* repeated key presses that didn't change state */
2419 if (newn->brightness_level == 0)
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2421 else if (newn->brightness_level >= bright_maxlvl
2422 && !tp_features.bright_unkfw)
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2426 #undef TPACPI_COMPARE_KEY
2427 #undef TPACPI_MAY_SEND_KEY
2433 * We track all events in hotkey_source_mask all the time, since
2434 * most of them are edge-based. We only issue those requested by
2435 * hotkey_user_mask or hotkey_driver_mask, though.
2437 static int hotkey_kthread(void *data)
2439 struct tp_nvram_state s[2] = { 0 };
2440 u32 poll_mask, event_mask;
2441 unsigned int si, so;
2443 unsigned int change_detector;
2444 unsigned int poll_freq;
2447 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2456 /* Initial state for compares */
2457 mutex_lock(&hotkey_thread_data_mutex);
2458 change_detector = hotkey_config_change;
2459 poll_mask = hotkey_source_mask;
2460 event_mask = hotkey_source_mask &
2461 (hotkey_driver_mask | hotkey_user_mask);
2462 poll_freq = hotkey_poll_freq;
2463 mutex_unlock(&hotkey_thread_data_mutex);
2464 hotkey_read_nvram(&s[so], poll_mask);
2466 while (!kthread_should_stop()) {
2468 if (likely(poll_freq))
2471 t = 100; /* should never happen... */
2473 t = msleep_interruptible(t);
2474 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2477 if (t > 0 && !was_frozen)
2480 mutex_lock(&hotkey_thread_data_mutex);
2481 if (was_frozen || hotkey_config_change != change_detector) {
2482 /* forget old state on thaw or config change */
2485 change_detector = hotkey_config_change;
2487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
2490 poll_freq = hotkey_poll_freq;
2491 mutex_unlock(&hotkey_thread_data_mutex);
2493 if (likely(poll_mask)) {
2494 hotkey_read_nvram(&s[si], poll_mask);
2495 if (likely(si != so)) {
2496 hotkey_compare_and_issue_event(&s[so], &s[si],
2509 /* call with hotkey_mutex held */
2510 static void hotkey_poll_stop_sync(void)
2512 if (tpacpi_hotkey_task) {
2513 kthread_stop(tpacpi_hotkey_task);
2514 tpacpi_hotkey_task = NULL;
2518 /* call with hotkey_mutex held */
2519 static void hotkey_poll_setup(const bool may_warn)
2521 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2522 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2524 if (hotkey_poll_freq > 0 &&
2525 (poll_driver_mask ||
2526 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2527 if (!tpacpi_hotkey_task) {
2528 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2529 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2530 if (IS_ERR(tpacpi_hotkey_task)) {
2531 tpacpi_hotkey_task = NULL;
2532 pr_err("could not create kernel thread for hotkey polling\n");
2536 hotkey_poll_stop_sync();
2537 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2538 hotkey_poll_freq == 0) {
2539 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2540 poll_user_mask, poll_driver_mask);
2545 static void hotkey_poll_setup_safe(const bool may_warn)
2547 mutex_lock(&hotkey_mutex);
2548 hotkey_poll_setup(may_warn);
2549 mutex_unlock(&hotkey_mutex);
2552 /* call with hotkey_mutex held */
2553 static void hotkey_poll_set_freq(unsigned int freq)
2556 hotkey_poll_stop_sync();
2558 hotkey_poll_freq = freq;
2561 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2563 static void hotkey_poll_setup(const bool __unused)
2567 static void hotkey_poll_setup_safe(const bool __unused)
2571 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2573 static int hotkey_inputdev_open(struct input_dev *dev)
2575 switch (tpacpi_lifecycle) {
2576 case TPACPI_LIFE_INIT:
2577 case TPACPI_LIFE_RUNNING:
2578 hotkey_poll_setup_safe(false);
2580 case TPACPI_LIFE_EXITING:
2584 /* Should only happen if tpacpi_lifecycle is corrupt */
2589 static void hotkey_inputdev_close(struct input_dev *dev)
2591 /* disable hotkey polling when possible */
2592 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2593 !(hotkey_source_mask & hotkey_driver_mask))
2594 hotkey_poll_setup_safe(false);
2597 /* sysfs hotkey enable ------------------------------------------------- */
2598 static ssize_t hotkey_enable_show(struct device *dev,
2599 struct device_attribute *attr,
2604 printk_deprecated_attribute("hotkey_enable",
2605 "Hotkey reporting is always enabled");
2607 res = hotkey_status_get(&status);
2611 return sysfs_emit(buf, "%d\n", status);
2614 static ssize_t hotkey_enable_store(struct device *dev,
2615 struct device_attribute *attr,
2616 const char *buf, size_t count)
2620 printk_deprecated_attribute("hotkey_enable",
2621 "Hotkeys can be disabled through hotkey_mask");
2623 if (parse_strtoul(buf, 1, &t))
2632 static DEVICE_ATTR_RW(hotkey_enable);
2634 /* sysfs hotkey mask --------------------------------------------------- */
2635 static ssize_t hotkey_mask_show(struct device *dev,
2636 struct device_attribute *attr,
2639 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
2642 static ssize_t hotkey_mask_store(struct device *dev,
2643 struct device_attribute *attr,
2644 const char *buf, size_t count)
2649 if (parse_strtoul(buf, 0xffffffffUL, &t))
2652 if (mutex_lock_killable(&hotkey_mutex))
2653 return -ERESTARTSYS;
2655 res = hotkey_user_mask_set(t);
2657 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2658 hotkey_poll_setup(true);
2661 mutex_unlock(&hotkey_mutex);
2663 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2665 return (res) ? res : count;
2668 static DEVICE_ATTR_RW(hotkey_mask);
2670 /* sysfs hotkey bios_enabled ------------------------------------------- */
2671 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2672 struct device_attribute *attr,
2675 return sprintf(buf, "0\n");
2678 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2680 /* sysfs hotkey bios_mask ---------------------------------------------- */
2681 static ssize_t hotkey_bios_mask_show(struct device *dev,
2682 struct device_attribute *attr,
2685 printk_deprecated_attribute("hotkey_bios_mask",
2686 "This attribute is useless.");
2687 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
2690 static DEVICE_ATTR_RO(hotkey_bios_mask);
2692 /* sysfs hotkey all_mask ----------------------------------------------- */
2693 static ssize_t hotkey_all_mask_show(struct device *dev,
2694 struct device_attribute *attr,
2697 return sysfs_emit(buf, "0x%08x\n",
2698 hotkey_all_mask | hotkey_source_mask);
2701 static DEVICE_ATTR_RO(hotkey_all_mask);
2703 /* sysfs hotkey all_mask ----------------------------------------------- */
2704 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2705 struct device_attribute *attr,
2708 return sysfs_emit(buf, "0x%08x\n",
2709 hotkey_adaptive_all_mask | hotkey_source_mask);
2712 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2714 /* sysfs hotkey recommended_mask --------------------------------------- */
2715 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2716 struct device_attribute *attr,
2719 return sysfs_emit(buf, "0x%08x\n",
2720 (hotkey_all_mask | hotkey_source_mask)
2721 & ~hotkey_reserved_mask);
2724 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2726 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2728 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2729 static ssize_t hotkey_source_mask_show(struct device *dev,
2730 struct device_attribute *attr,
2733 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
2736 static ssize_t hotkey_source_mask_store(struct device *dev,
2737 struct device_attribute *attr,
2738 const char *buf, size_t count)
2744 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2745 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2748 if (mutex_lock_killable(&hotkey_mutex))
2749 return -ERESTARTSYS;
2751 HOTKEY_CONFIG_CRITICAL_START
2752 hotkey_source_mask = t;
2753 HOTKEY_CONFIG_CRITICAL_END
2755 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2756 ~hotkey_source_mask);
2757 hotkey_poll_setup(true);
2759 /* check if events needed by the driver got disabled */
2760 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2761 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2763 mutex_unlock(&hotkey_mutex);
2766 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2769 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2772 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2774 return (rc < 0) ? rc : count;
2777 static DEVICE_ATTR_RW(hotkey_source_mask);
2779 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2780 static ssize_t hotkey_poll_freq_show(struct device *dev,
2781 struct device_attribute *attr,
2784 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
2787 static ssize_t hotkey_poll_freq_store(struct device *dev,
2788 struct device_attribute *attr,
2789 const char *buf, size_t count)
2793 if (parse_strtoul(buf, 25, &t))
2796 if (mutex_lock_killable(&hotkey_mutex))
2797 return -ERESTARTSYS;
2799 hotkey_poll_set_freq(t);
2800 hotkey_poll_setup(true);
2802 mutex_unlock(&hotkey_mutex);
2804 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2809 static DEVICE_ATTR_RW(hotkey_poll_freq);
2811 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2813 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2814 static ssize_t hotkey_radio_sw_show(struct device *dev,
2815 struct device_attribute *attr,
2819 res = hotkey_get_wlsw();
2823 /* Opportunistic update */
2824 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2826 return sysfs_emit(buf, "%d\n",
2827 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2830 static DEVICE_ATTR_RO(hotkey_radio_sw);
2832 static void hotkey_radio_sw_notify_change(void)
2834 if (tp_features.hotkey_wlsw)
2835 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2839 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2840 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2841 struct device_attribute *attr,
2845 res = hotkey_get_tablet_mode(&s);
2849 return sysfs_emit(buf, "%d\n", !!s);
2852 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2854 static void hotkey_tablet_mode_notify_change(void)
2856 if (tp_features.hotkey_tablet)
2857 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2858 "hotkey_tablet_mode");
2861 /* sysfs wakeup reason (pollable) -------------------------------------- */
2862 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2863 struct device_attribute *attr,
2866 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
2869 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2871 static void hotkey_wakeup_reason_notify_change(void)
2873 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2877 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2878 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2879 struct device_attribute *attr,
2882 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
2885 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2886 hotkey_wakeup_hotunplug_complete_show, NULL);
2888 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2890 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2891 "wakeup_hotunplug_complete");
2894 /* sysfs adaptive kbd mode --------------------------------------------- */
2896 static int adaptive_keyboard_get_mode(void);
2897 static int adaptive_keyboard_set_mode(int new_mode);
2899 enum ADAPTIVE_KEY_MODE {
2902 WEB_CONFERENCE_MODE,
2907 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2908 struct device_attribute *attr,
2913 current_mode = adaptive_keyboard_get_mode();
2914 if (current_mode < 0)
2915 return current_mode;
2917 return sysfs_emit(buf, "%d\n", current_mode);
2920 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2921 struct device_attribute *attr,
2922 const char *buf, size_t count)
2927 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2930 res = adaptive_keyboard_set_mode(t);
2931 return (res < 0) ? res : count;
2934 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2936 static struct attribute *adaptive_kbd_attributes[] = {
2937 &dev_attr_adaptive_kbd_mode.attr,
2941 static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2942 struct attribute *attr, int n)
2944 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2947 static const struct attribute_group adaptive_kbd_attr_group = {
2948 .is_visible = hadaptive_kbd_attr_is_visible,
2949 .attrs = adaptive_kbd_attributes,
2952 /* --------------------------------------------------------------------- */
2954 static struct attribute *hotkey_attributes[] = {
2955 &dev_attr_hotkey_enable.attr,
2956 &dev_attr_hotkey_bios_enabled.attr,
2957 &dev_attr_hotkey_bios_mask.attr,
2958 &dev_attr_wakeup_reason.attr,
2959 &dev_attr_wakeup_hotunplug_complete.attr,
2960 &dev_attr_hotkey_mask.attr,
2961 &dev_attr_hotkey_all_mask.attr,
2962 &dev_attr_hotkey_adaptive_all_mask.attr,
2963 &dev_attr_hotkey_recommended_mask.attr,
2964 &dev_attr_hotkey_tablet_mode.attr,
2965 &dev_attr_hotkey_radio_sw.attr,
2966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2973 static umode_t hotkey_attr_is_visible(struct kobject *kobj,
2974 struct attribute *attr, int n)
2976 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
2977 if (!tp_features.hotkey_tablet)
2979 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
2980 if (!tp_features.hotkey_wlsw)
2987 static const struct attribute_group hotkey_attr_group = {
2988 .is_visible = hotkey_attr_is_visible,
2989 .attrs = hotkey_attributes,
2993 * Sync both the hw and sw blocking state of all switches
2995 static void tpacpi_send_radiosw_update(void)
3000 * We must sync all rfkill controllers *before* issuing any
3001 * rfkill input events, or we will race the rfkill core input
3004 * tpacpi_inputdev_send_mutex works as a synchronization point
3007 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3010 wlsw = hotkey_get_wlsw();
3012 /* Sync hw blocking state first if it is hw-blocked */
3013 if (wlsw == TPACPI_RFK_RADIO_OFF)
3014 tpacpi_rfk_update_hwblock_state(true);
3016 /* Sync hw blocking state last if it is hw-unblocked */
3017 if (wlsw == TPACPI_RFK_RADIO_ON)
3018 tpacpi_rfk_update_hwblock_state(false);
3020 /* Issue rfkill input event for WLSW switch */
3022 mutex_lock(&tpacpi_inputdev_send_mutex);
3024 input_report_switch(tpacpi_inputdev,
3025 SW_RFKILL_ALL, (wlsw > 0));
3026 input_sync(tpacpi_inputdev);
3028 mutex_unlock(&tpacpi_inputdev_send_mutex);
3032 * this can be unconditional, as we will poll state again
3033 * if userspace uses the notify to read data
3035 hotkey_radio_sw_notify_change();
3038 static void hotkey_exit(void)
3040 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3041 mutex_lock(&hotkey_mutex);
3042 hotkey_poll_stop_sync();
3043 mutex_unlock(&hotkey_mutex);
3045 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3046 "restoring original HKEY status and mask\n");
3047 /* yes, there is a bitwise or below, we want the
3048 * functions to be called even if one of them fail */
3049 if (((tp_features.hotkey_mask &&
3050 hotkey_mask_set(hotkey_orig_mask)) |
3051 hotkey_status_set(false)) != 0)
3052 pr_err("failed to restore hot key mask to BIOS defaults\n");
3055 static void __init hotkey_unmap(const unsigned int scancode)
3057 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3058 clear_bit(hotkey_keycode_map[scancode],
3059 tpacpi_inputdev->keybit);
3060 hotkey_keycode_map[scancode] = KEY_RESERVED;
3066 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3069 #define TPACPI_HK_Q_INIMASK 0x0001
3071 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3072 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3073 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3074 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3075 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3076 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3077 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3078 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3079 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3080 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3081 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3082 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3083 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3084 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3085 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3086 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3087 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3088 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3089 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3090 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3093 typedef u16 tpacpi_keymap_entry_t;
3094 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3096 static int hotkey_init_tablet_mode(void)
3098 int in_tablet_mode = 0, res;
3101 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3102 int has_tablet_mode;
3104 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3107 * The Yoga 11e series has 2 accelerometers described by a
3108 * BOSC0200 ACPI node. This setup relies on a Windows service
3109 * which calls special ACPI methods on this node to report
3110 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3111 * does not support this, so skip the hotkey on these models.
3113 if (has_tablet_mode && !dual_accel_detect())
3114 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3116 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3117 /* For X41t, X60t, X61t Tablets... */
3118 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3119 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3123 if (!tp_features.hotkey_tablet)
3126 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3127 type, in_tablet_mode ? "tablet" : "laptop");
3129 return in_tablet_mode;
3132 static int __init hotkey_init(struct ibm_init_struct *iibm)
3134 /* Requirements for changing the default keymaps:
3136 * 1. Many of the keys are mapped to KEY_RESERVED for very
3137 * good reasons. Do not change them unless you have deep
3138 * knowledge on the IBM and Lenovo ThinkPad firmware for
3139 * the various ThinkPad models. The driver behaves
3140 * differently for KEY_RESERVED: such keys have their
3141 * hot key mask *unset* in mask_recommended, and also
3142 * in the initial hot key mask programmed into the
3143 * firmware at driver load time, which means the firm-
3144 * ware may react very differently if you change them to
3147 * 2. You must be subscribed to the linux-thinkpad and
3148 * ibm-acpi-devel mailing lists, and you should read the
3149 * list archives since 2007 if you want to change the
3150 * keymaps. This requirement exists so that you will
3151 * know the past history of problems with the thinkpad-
3152 * acpi driver keymaps, and also that you will be
3153 * listening to any bug reports;
3155 * 3. Do not send thinkpad-acpi specific patches directly to
3156 * for merging, *ever*. Send them to the linux-acpi
3157 * mailinglist for comments. Merging is to be done only
3158 * through acpi-test and the ACPI maintainer.
3160 * If the above is too much to ask, don't change the keymap.
3161 * Ask the thinkpad-acpi maintainer to do it, instead.
3165 TPACPI_KEYMAP_IBM_GENERIC = 0,
3166 TPACPI_KEYMAP_LENOVO_GENERIC,
3169 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3170 /* Generic keymap for IBM ThinkPads */
3171 [TPACPI_KEYMAP_IBM_GENERIC] = {
3172 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3173 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3174 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3175 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3177 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3178 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3179 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3180 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3182 /* brightness: firmware always reacts to them */
3183 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3184 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3186 /* Thinklight: firmware always react to it */
3187 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3189 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3190 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3192 /* Volume: firmware always react to it and reprograms
3193 * the built-in *extra* mixer. Never map it to control
3194 * another mixer by default. */
3195 KEY_RESERVED, /* 0x14: VOLUME UP */
3196 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3197 KEY_RESERVED, /* 0x16: MUTE */
3199 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3201 /* (assignments unknown, please report if found) */
3202 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3203 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3205 /* No assignments, only used for Adaptive keyboards. */
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, KEY_UNKNOWN,
3209 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3210 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3212 /* No assignment, used for newer Lenovo models */
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, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3218 KEY_UNKNOWN, KEY_UNKNOWN
3222 /* Generic keymap for Lenovo ThinkPads */
3223 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3224 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3225 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3226 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3227 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3229 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3230 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3231 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3232 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3234 /* These should be enabled --only-- when ACPI video
3235 * is disabled (i.e. in "vendor" mode), and are handled
3236 * in a special way by the init code */
3237 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3238 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3240 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3242 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3243 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3245 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3246 * react to it and reprograms the built-in *extra* mixer.
3247 * Never map it to control another mixer by default.
3249 * T60?, T61, R60?, R61: firmware and EC tries to send
3250 * these over the regular keyboard, so these are no-ops,
3251 * but there are still weird bugs re. MUTE, so do not
3252 * change unless you get test reports from all Lenovo
3253 * models. May cause the BIOS to interfere with the
3256 KEY_RESERVED, /* 0x14: VOLUME UP */
3257 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3258 KEY_RESERVED, /* 0x16: MUTE */
3260 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3262 /* (assignments unknown, please report if found) */
3263 KEY_UNKNOWN, KEY_UNKNOWN,
3266 * The mic mute button only sends 0x1a. It does not
3267 * automatically mute the mic or change the mute light.
3269 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3271 /* (assignments unknown, please report if found) */
3274 /* Extra keys in use since the X240 / T440 / T540 */
3275 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3278 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3279 * The first item in this list is the Mute button which is
3280 * emitted with 0x103 through
3281 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3283 * We'll need to offset those by 0x20.
3285 KEY_RESERVED, /* Mute held, 0x103 */
3286 KEY_BRIGHTNESS_MIN, /* Backlight off */
3287 KEY_RESERVED, /* Clipping tool */
3288 KEY_RESERVED, /* Cloud */
3290 KEY_VOICECOMMAND, /* Voice */
3292 KEY_RESERVED, /* Gestures */
3296 KEY_CONFIG, /* Settings */
3297 KEY_RESERVED, /* New tab */
3298 KEY_REFRESH, /* Reload */
3299 KEY_BACK, /* Back */
3300 KEY_RESERVED, /* Microphone down */
3301 KEY_RESERVED, /* Microphone up */
3302 KEY_RESERVED, /* Microphone cancellation */
3303 KEY_RESERVED, /* Camera mode */
3304 KEY_RESERVED, /* Rotate display, 0x116 */
3307 * These are found in 2017 models (e.g. T470s, X270).
3308 * The lowest known value is 0x311, which according to
3309 * the manual should launch a user defined favorite
3312 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3313 * corresponding to 0x34.
3316 /* (assignments unknown, please report if found) */
3317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3318 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3319 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3320 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3323 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3324 KEY_SELECTIVE_SCREENSHOT, /* Clipping tool */
3325 KEY_CALC, /* Calculator (above numpad, P52) */
3326 KEY_BLUETOOTH, /* Bluetooth */
3327 KEY_KEYBOARD, /* Keyboard, 0x315 */
3328 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3329 KEY_NOTIFICATION_CENTER, /* Notification Center */
3330 KEY_PICKUP_PHONE, /* Answer incoming call */
3331 KEY_HANGUP_PHONE, /* Decline incoming call */
3335 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3336 /* Generic maps (fallback) */
3338 .vendor = PCI_VENDOR_ID_IBM,
3339 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3340 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3343 .vendor = PCI_VENDOR_ID_LENOVO,
3344 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3345 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3349 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3350 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3355 bool radiosw_state = false;
3356 bool tabletsw_state = false;
3358 unsigned long quirks;
3359 unsigned long keymap_id;
3361 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3362 "initializing hotkey subdriver\n");
3364 BUG_ON(!tpacpi_inputdev);
3365 BUG_ON(tpacpi_inputdev->open != NULL ||
3366 tpacpi_inputdev->close != NULL);
3368 TPACPI_ACPIHANDLE_INIT(hkey);
3369 mutex_init(&hotkey_mutex);
3371 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3372 mutex_init(&hotkey_thread_data_mutex);
3375 /* hotkey not supported on 570 */
3376 tp_features.hotkey = hkey_handle != NULL;
3378 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3380 str_supported(tp_features.hotkey));
3382 if (!tp_features.hotkey)
3385 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3386 ARRAY_SIZE(tpacpi_hotkey_qtable));
3388 tpacpi_disable_brightness_delay();
3390 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3391 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3392 for HKEY interface version 0x100 */
3393 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3394 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3395 "firmware HKEY interface version: 0x%x\n",
3398 switch (hkeyv >> 8) {
3401 * MHKV 0x100 in A31, R40, R40e,
3402 * T4x, X31, and later
3405 /* Paranoia check AND init hotkey_all_mask */
3406 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3408 pr_err("missing MHKA handler, please report this to %s\n",
3410 /* Fallback: pre-init for FN+F3,F4,F12 */
3411 hotkey_all_mask = 0x080cU;
3413 tp_features.hotkey_mask = 1;
3419 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3422 /* Paranoia check AND init hotkey_all_mask */
3423 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3425 pr_err("missing MHKA handler, please report this to %s\n",
3427 /* Fallback: pre-init for FN+F3,F4,F12 */
3428 hotkey_all_mask = 0x080cU;
3430 tp_features.hotkey_mask = 1;
3434 * Check if we have an adaptive keyboard, like on the
3435 * Lenovo Carbon X1 2014 (2nd Gen).
3437 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3439 if (hotkey_adaptive_all_mask != 0)
3440 tp_features.has_adaptive_kbd = true;
3442 tp_features.has_adaptive_kbd = false;
3443 hotkey_adaptive_all_mask = 0x0U;
3448 pr_err("unknown version of the HKEY interface: 0x%x\n",
3450 pr_err("please report this to %s\n", TPACPI_MAIL);
3455 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3456 "hotkey masks are %s\n",
3457 str_supported(tp_features.hotkey_mask));
3459 /* Init hotkey_all_mask if not initialized yet */
3460 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3461 (quirks & TPACPI_HK_Q_INIMASK))
3462 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3464 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3465 if (tp_features.hotkey_mask) {
3466 /* hotkey_source_mask *must* be zero for
3467 * the first hotkey_mask_get to return hotkey_orig_mask */
3468 res = hotkey_mask_get();
3472 hotkey_orig_mask = hotkey_acpi_mask;
3474 hotkey_orig_mask = hotkey_all_mask;
3475 hotkey_acpi_mask = hotkey_all_mask;
3478 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3480 tp_features.hotkey_wlsw = 1;
3481 radiosw_state = !!tpacpi_wlsw_emulstate;
3482 pr_info("radio switch emulation enabled\n");
3485 /* Not all thinkpads have a hardware radio switch */
3486 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3487 tp_features.hotkey_wlsw = 1;
3488 radiosw_state = !!status;
3489 pr_info("radio switch found; radios are %s\n",
3490 enabled(status, 0));
3493 tabletsw_state = hotkey_init_tablet_mode();
3495 /* Set up key map */
3496 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3497 ARRAY_SIZE(tpacpi_keymap_qtable));
3498 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3499 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3500 "using keymap number %lu\n", keymap_id);
3502 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3503 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3504 if (!hotkey_keycode_map) {
3505 pr_err("failed to allocate memory for key map\n");
3509 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3510 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3511 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3512 tpacpi_inputdev->keycode = hotkey_keycode_map;
3513 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3514 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3515 input_set_capability(tpacpi_inputdev, EV_KEY,
3516 hotkey_keycode_map[i]);
3518 if (i < sizeof(hotkey_reserved_mask)*8)
3519 hotkey_reserved_mask |= 1 << i;
3523 if (tp_features.hotkey_wlsw) {
3524 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3525 input_report_switch(tpacpi_inputdev,
3526 SW_RFKILL_ALL, radiosw_state);
3528 if (tp_features.hotkey_tablet) {
3529 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3530 input_report_switch(tpacpi_inputdev,
3531 SW_TABLET_MODE, tabletsw_state);
3534 /* Do not issue duplicate brightness change events to
3535 * userspace. tpacpi_detect_brightness_capabilities() must have
3536 * been called before this point */
3537 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3538 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3539 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3541 /* Disable brightness up/down on Lenovo thinkpads when
3542 * ACPI is handling them, otherwise it is plain impossible
3543 * for userspace to do something even remotely sane */
3544 hotkey_reserved_mask |=
3545 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3546 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3547 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3548 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3551 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3552 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3554 & ~hotkey_reserved_mask;
3556 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3557 "hotkey source mask 0x%08x, polling freq %u\n",
3558 hotkey_source_mask, hotkey_poll_freq);
3561 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3562 "enabling firmware HKEY event interface...\n");
3563 res = hotkey_status_set(true);
3568 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3569 | hotkey_driver_mask)
3570 & ~hotkey_source_mask);
3571 if (res < 0 && res != -ENXIO) {
3575 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3576 & ~hotkey_reserved_mask;
3577 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3578 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3579 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3581 tpacpi_inputdev->open = &hotkey_inputdev_open;
3582 tpacpi_inputdev->close = &hotkey_inputdev_close;
3584 hotkey_poll_setup_safe(true);
3589 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3590 * mode, Web conference mode, Function mode and Lay-flat mode.
3591 * We support Home mode and Function mode currently.
3593 * Will consider support rest of modes in future.
3596 static const int adaptive_keyboard_modes[] = {
3598 /* WEB_BROWSER_MODE = 2,
3599 WEB_CONFERENCE_MODE = 3, */
3603 #define DFR_CHANGE_ROW 0x101
3604 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3605 #define FIRST_ADAPTIVE_KEY 0x103
3607 /* press Fn key a while second, it will switch to Function Mode. Then
3608 * release Fn key, previous mode be restored.
3610 static bool adaptive_keyboard_mode_is_saved;
3611 static int adaptive_keyboard_prev_mode;
3613 static int adaptive_keyboard_get_mode(void)
3617 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3618 pr_err("Cannot read adaptive keyboard mode\n");
3625 static int adaptive_keyboard_set_mode(int new_mode)
3628 new_mode > LAYFLAT_MODE)
3631 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3632 pr_err("Cannot set adaptive keyboard mode\n");
3639 static int adaptive_keyboard_get_next_mode(int mode)
3642 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3644 for (i = 0; i <= max_mode; i++) {
3645 if (adaptive_keyboard_modes[i] == mode)
3654 return adaptive_keyboard_modes[i];
3657 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3659 int current_mode = 0;
3664 case DFR_CHANGE_ROW:
3665 if (adaptive_keyboard_mode_is_saved) {
3666 new_mode = adaptive_keyboard_prev_mode;
3667 adaptive_keyboard_mode_is_saved = false;
3669 current_mode = adaptive_keyboard_get_mode();
3670 if (current_mode < 0)
3672 new_mode = adaptive_keyboard_get_next_mode(
3676 if (adaptive_keyboard_set_mode(new_mode) < 0)
3681 case DFR_SHOW_QUICKVIEW_ROW:
3682 current_mode = adaptive_keyboard_get_mode();
3683 if (current_mode < 0)
3686 adaptive_keyboard_prev_mode = current_mode;
3687 adaptive_keyboard_mode_is_saved = true;
3689 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3694 if (scancode < FIRST_ADAPTIVE_KEY ||
3695 scancode >= FIRST_ADAPTIVE_KEY +
3696 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3697 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3698 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3702 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3703 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3704 if (keycode != KEY_RESERVED) {
3705 mutex_lock(&tpacpi_inputdev_send_mutex);
3707 input_report_key(tpacpi_inputdev, keycode, 1);
3708 input_sync(tpacpi_inputdev);
3710 input_report_key(tpacpi_inputdev, keycode, 0);
3711 input_sync(tpacpi_inputdev);
3713 mutex_unlock(&tpacpi_inputdev_send_mutex);
3719 static bool hotkey_notify_extended_hotkey(const u32 hkey)
3721 unsigned int scancode;
3724 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
3725 tpacpi_driver_event(hkey);
3729 /* Extended keycodes start at 0x300 and our offset into the map
3730 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3731 * will be positive, but might not be in the correct range.
3733 scancode = (hkey & 0xfff) - (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3734 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3735 scancode < TPACPI_HOTKEY_MAP_LEN) {
3736 tpacpi_input_send_key(scancode);
3743 static bool hotkey_notify_hotkey(const u32 hkey,
3745 bool *ignore_acpi_ev)
3747 /* 0x1000-0x1FFF: key presses */
3748 unsigned int scancode = hkey & 0xfff;
3749 *send_acpi_ev = true;
3750 *ignore_acpi_ev = false;
3753 * Original events are in the 0x10XX range, the adaptive keyboard
3754 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3755 * models, additional keys are emitted through 0x13XX.
3757 switch ((hkey >> 8) & 0xf) {
3760 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3761 /* HKEY event 0x1001 is scancode 0x00 */
3763 if (!(hotkey_source_mask & (1 << scancode))) {
3764 tpacpi_input_send_key_masked(scancode);
3765 *send_acpi_ev = false;
3767 *ignore_acpi_ev = true;
3774 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3777 return hotkey_notify_extended_hotkey(hkey);
3783 static bool hotkey_notify_wakeup(const u32 hkey,
3785 bool *ignore_acpi_ev)
3787 /* 0x2000-0x2FFF: Wakeup reason */
3788 *send_acpi_ev = true;
3789 *ignore_acpi_ev = false;
3792 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3793 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3794 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3795 *ignore_acpi_ev = true;
3798 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3799 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3800 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3801 *ignore_acpi_ev = true;
3804 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3805 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3806 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3807 /* how to auto-heal: */
3808 /* 2313: woke up from S3, go to S4/S5 */
3809 /* 2413: woke up from S4, go to S5 */
3816 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3817 pr_info("woke up due to a hot-unplug request...\n");
3818 hotkey_wakeup_reason_notify_change();
3823 static bool hotkey_notify_dockevent(const u32 hkey,
3825 bool *ignore_acpi_ev)
3827 /* 0x4000-0x4FFF: dock-related events */
3828 *send_acpi_ev = true;
3829 *ignore_acpi_ev = false;
3832 case TP_HKEY_EV_UNDOCK_ACK:
3833 /* ACPI undock operation completed after wakeup */
3834 hotkey_autosleep_ack = 1;
3835 pr_info("undocked\n");
3836 hotkey_wakeup_hotunplug_complete_notify_change();
3839 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3840 pr_info("docked into hotplug port replicator\n");
3842 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3843 pr_info("undocked from hotplug port replicator\n");
3847 * Deliberately ignore attaching and detaching the keybord cover to avoid
3848 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3851 * Please refer to the following thread for more information and a preliminary
3852 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3853 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3854 * the Pico cartridge dock module:
3855 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
3857 case TP_HKEY_EV_KBD_COVER_ATTACH:
3858 case TP_HKEY_EV_KBD_COVER_DETACH:
3859 *send_acpi_ev = false;
3860 *ignore_acpi_ev = true;
3868 static bool hotkey_notify_usrevent(const u32 hkey,
3870 bool *ignore_acpi_ev)
3872 /* 0x5000-0x5FFF: human interface helpers */
3873 *send_acpi_ev = true;
3874 *ignore_acpi_ev = false;
3877 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3878 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3881 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3882 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3883 tpacpi_input_send_tabletsw();
3884 hotkey_tablet_mode_notify_change();
3885 *send_acpi_ev = false;
3888 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3889 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3890 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3891 /* do not propagate these events */
3892 *ignore_acpi_ev = true;
3900 static void thermal_dump_all_sensors(void);
3901 static void palmsensor_refresh(void);
3903 static bool hotkey_notify_6xxx(const u32 hkey,
3905 bool *ignore_acpi_ev)
3907 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3908 *send_acpi_ev = true;
3909 *ignore_acpi_ev = false;
3912 case TP_HKEY_EV_THM_TABLE_CHANGED:
3913 pr_debug("EC reports: Thermal Table has changed\n");
3914 /* recommended action: do nothing, we don't have
3915 * Lenovo ATM information */
3917 case TP_HKEY_EV_THM_CSM_COMPLETED:
3918 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
3919 /* Thermal event - pass on to event handler */
3920 tpacpi_driver_event(hkey);
3922 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3923 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
3924 /* recommended action: do nothing, we don't have
3925 * Lenovo ATM information */
3927 case TP_HKEY_EV_ALARM_BAT_HOT:
3928 pr_crit("THERMAL ALARM: battery is too hot!\n");
3929 /* recommended action: warn user through gui */
3931 case TP_HKEY_EV_ALARM_BAT_XHOT:
3932 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3933 /* recommended action: immediate sleep/hibernate */
3935 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3936 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3937 /* recommended action: warn user through gui, that */
3938 /* some internal component is too hot */
3940 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3941 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3942 /* recommended action: immediate sleep/hibernate */
3944 case TP_HKEY_EV_AC_CHANGED:
3945 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3946 * AC status changed; can be triggered by plugging or
3947 * unplugging AC adapter, docking or undocking. */
3951 case TP_HKEY_EV_KEY_NUMLOCK:
3952 case TP_HKEY_EV_KEY_FN:
3953 /* key press events, we just ignore them as long as the EC
3954 * is still reporting them in the normal keyboard stream */
3955 *send_acpi_ev = false;
3956 *ignore_acpi_ev = true;
3959 case TP_HKEY_EV_KEY_FN_ESC:
3960 /* Get the media key status to force the status LED to update */
3961 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3962 *send_acpi_ev = false;
3963 *ignore_acpi_ev = true;
3966 case TP_HKEY_EV_TABLET_CHANGED:
3967 tpacpi_input_send_tabletsw();
3968 hotkey_tablet_mode_notify_change();
3969 *send_acpi_ev = false;
3972 case TP_HKEY_EV_PALM_DETECTED:
3973 case TP_HKEY_EV_PALM_UNDETECTED:
3974 /* palm detected - pass on to event handler */
3975 palmsensor_refresh();
3979 /* report simply as unknown, no sensor dump */
3983 thermal_dump_all_sensors();
3987 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3991 bool ignore_acpi_ev;
3994 if (event != 0x80) {
3995 pr_err("unknown HKEY notification event %d\n", event);
3996 /* forward it to userspace, maybe it knows how to handle it */
3997 acpi_bus_generate_netlink_event(
3998 ibm->acpi->device->pnp.device_class,
3999 dev_name(&ibm->acpi->device->dev),
4005 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4006 pr_err("failed to retrieve HKEY event\n");
4015 send_acpi_ev = true;
4016 ignore_acpi_ev = false;
4018 switch (hkey >> 12) {
4020 /* 0x1000-0x1FFF: key presses */
4021 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4025 /* 0x2000-0x2FFF: Wakeup reason */
4026 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4030 /* 0x3000-0x3FFF: bay-related wakeups */
4032 case TP_HKEY_EV_BAYEJ_ACK:
4033 hotkey_autosleep_ack = 1;
4034 pr_info("bay ejected\n");
4035 hotkey_wakeup_hotunplug_complete_notify_change();
4038 case TP_HKEY_EV_OPTDRV_EJ:
4039 /* FIXME: kick libata if SATA link offline */
4047 /* 0x4000-0x4FFF: dock-related events */
4048 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4052 /* 0x5000-0x5FFF: human interface helpers */
4053 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4057 /* 0x6000-0x6FFF: thermal alarms/notices and
4058 * keyboard events */
4059 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4063 /* 0x7000-0x7FFF: misc */
4064 if (tp_features.hotkey_wlsw &&
4065 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4066 tpacpi_send_radiosw_update();
4071 fallthrough; /* to default */
4076 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4077 pr_notice("please report the conditions when this event happened to %s\n",
4081 /* netlink events */
4082 if (!ignore_acpi_ev && send_acpi_ev) {
4083 acpi_bus_generate_netlink_event(
4084 ibm->acpi->device->pnp.device_class,
4085 dev_name(&ibm->acpi->device->dev),
4091 static void hotkey_suspend(void)
4093 /* Do these on suspend, we get the events on early resume! */
4094 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4095 hotkey_autosleep_ack = 0;
4097 /* save previous mode of adaptive keyboard of X1 Carbon */
4098 if (tp_features.has_adaptive_kbd) {
4099 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4101 pr_err("Cannot read adaptive keyboard mode.\n");
4106 static void hotkey_resume(void)
4108 tpacpi_disable_brightness_delay();
4110 if (hotkey_status_set(true) < 0 ||
4111 hotkey_mask_set(hotkey_acpi_mask) < 0)
4112 pr_err("error while attempting to reset the event firmware interface\n");
4114 tpacpi_send_radiosw_update();
4115 tpacpi_input_send_tabletsw();
4116 hotkey_tablet_mode_notify_change();
4117 hotkey_wakeup_reason_notify_change();
4118 hotkey_wakeup_hotunplug_complete_notify_change();
4119 hotkey_poll_setup_safe(false);
4121 /* restore previous mode of adapive keyboard of X1 Carbon */
4122 if (tp_features.has_adaptive_kbd) {
4123 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4124 adaptive_keyboard_prev_mode)) {
4125 pr_err("Cannot set adaptive keyboard mode.\n");
4130 /* procfs -------------------------------------------------------------- */
4131 static int hotkey_read(struct seq_file *m)
4135 if (!tp_features.hotkey) {
4136 seq_printf(m, "status:\t\tnot supported\n");
4140 if (mutex_lock_killable(&hotkey_mutex))
4141 return -ERESTARTSYS;
4142 res = hotkey_status_get(&status);
4144 res = hotkey_mask_get();
4145 mutex_unlock(&hotkey_mutex);
4149 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4150 if (hotkey_all_mask) {
4151 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4152 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4154 seq_printf(m, "mask:\t\tnot supported\n");
4155 seq_printf(m, "commands:\tenable, disable, reset\n");
4161 static void hotkey_enabledisable_warn(bool enable)
4163 tpacpi_log_usertask("procfs hotkey enable/disable");
4164 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4165 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4166 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4169 static int hotkey_write(char *buf)
4175 if (!tp_features.hotkey)
4178 if (mutex_lock_killable(&hotkey_mutex))
4179 return -ERESTARTSYS;
4181 mask = hotkey_user_mask;
4184 while ((cmd = strsep(&buf, ","))) {
4185 if (strlencmp(cmd, "enable") == 0) {
4186 hotkey_enabledisable_warn(1);
4187 } else if (strlencmp(cmd, "disable") == 0) {
4188 hotkey_enabledisable_warn(0);
4190 } else if (strlencmp(cmd, "reset") == 0) {
4191 mask = (hotkey_all_mask | hotkey_source_mask)
4192 & ~hotkey_reserved_mask;
4193 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4195 } else if (sscanf(cmd, "%x", &mask) == 1) {
4204 tpacpi_disclose_usertask("procfs hotkey",
4205 "set mask to 0x%08x\n", mask);
4206 res = hotkey_user_mask_set(mask);
4210 mutex_unlock(&hotkey_mutex);
4214 static const struct acpi_device_id ibm_htk_device_ids[] = {
4215 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4216 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4217 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4221 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4222 .hid = ibm_htk_device_ids,
4223 .notify = hotkey_notify,
4224 .handle = &hkey_handle,
4225 .type = ACPI_DEVICE_NOTIFY,
4228 static struct ibm_struct hotkey_driver_data = {
4230 .read = hotkey_read,
4231 .write = hotkey_write,
4232 .exit = hotkey_exit,
4233 .resume = hotkey_resume,
4234 .suspend = hotkey_suspend,
4235 .acpi = &ibm_hotkey_acpidriver,
4238 /*************************************************************************
4239 * Bluetooth subdriver
4243 /* ACPI GBDC/SBDC bits */
4244 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4245 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4246 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4247 0 = disable, 1 = enable */
4251 /* ACPI \BLTH commands */
4252 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4253 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4254 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4255 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4256 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4259 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4261 static int bluetooth_get_status(void)
4265 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4266 if (dbg_bluetoothemul)
4267 return (tpacpi_bluetooth_emulstate) ?
4268 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4271 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4274 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4275 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4278 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4282 vdbg_printk(TPACPI_DBG_RFKILL,
4283 "will attempt to %s bluetooth\n",
4284 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4286 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4287 if (dbg_bluetoothemul) {
4288 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4293 if (state == TPACPI_RFK_RADIO_ON)
4294 status = TP_ACPI_BLUETOOTH_RADIOSSW
4295 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4299 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4305 /* sysfs bluetooth enable ---------------------------------------------- */
4306 static ssize_t bluetooth_enable_show(struct device *dev,
4307 struct device_attribute *attr,
4310 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4314 static ssize_t bluetooth_enable_store(struct device *dev,
4315 struct device_attribute *attr,
4316 const char *buf, size_t count)
4318 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4322 static DEVICE_ATTR_RW(bluetooth_enable);
4324 /* --------------------------------------------------------------------- */
4326 static struct attribute *bluetooth_attributes[] = {
4327 &dev_attr_bluetooth_enable.attr,
4331 static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
4332 struct attribute *attr, int n)
4334 return tp_features.bluetooth ? attr->mode : 0;
4337 static const struct attribute_group bluetooth_attr_group = {
4338 .is_visible = bluetooth_attr_is_visible,
4339 .attrs = bluetooth_attributes,
4342 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4343 .get_status = bluetooth_get_status,
4344 .set_status = bluetooth_set_status,
4347 static void bluetooth_shutdown(void)
4349 /* Order firmware to save current state to NVRAM */
4350 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4351 TP_ACPI_BLTH_SAVE_STATE))
4352 pr_notice("failed to save bluetooth state to NVRAM\n");
4354 vdbg_printk(TPACPI_DBG_RFKILL,
4355 "bluetooth state saved to NVRAM\n");
4358 static void bluetooth_exit(void)
4360 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4361 bluetooth_shutdown();
4364 static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4366 .ident = "ThinkPad E485",
4368 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4369 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4373 .ident = "ThinkPad E585",
4375 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4376 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4380 .ident = "ThinkPad A285 - 20MW",
4382 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4383 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4387 .ident = "ThinkPad A285 - 20MX",
4389 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4390 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4394 .ident = "ThinkPad A485 - 20MU",
4396 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4397 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4401 .ident = "ThinkPad A485 - 20MV",
4403 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4404 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4410 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4411 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4412 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4413 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4418 static int __init have_bt_fwbug(void)
4421 * Some AMD based ThinkPads have a firmware bug that calling
4422 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4424 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4425 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4426 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4432 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4437 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4438 "initializing bluetooth subdriver\n");
4440 TPACPI_ACPIHANDLE_INIT(hkey);
4442 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4443 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4444 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4445 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4447 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4448 "bluetooth is %s, status 0x%02x\n",
4449 str_supported(tp_features.bluetooth),
4452 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4453 if (dbg_bluetoothemul) {
4454 tp_features.bluetooth = 1;
4455 pr_info("bluetooth switch emulation enabled\n");
4458 if (tp_features.bluetooth &&
4459 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4460 /* no bluetooth hardware present in system */
4461 tp_features.bluetooth = 0;
4462 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4463 "bluetooth hardware not installed\n");
4466 if (!tp_features.bluetooth)
4469 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4470 &bluetooth_tprfk_ops,
4471 RFKILL_TYPE_BLUETOOTH,
4472 TPACPI_RFK_BLUETOOTH_SW_NAME,
4477 /* procfs -------------------------------------------------------------- */
4478 static int bluetooth_read(struct seq_file *m)
4480 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4483 static int bluetooth_write(char *buf)
4485 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4488 static struct ibm_struct bluetooth_driver_data = {
4489 .name = "bluetooth",
4490 .read = bluetooth_read,
4491 .write = bluetooth_write,
4492 .exit = bluetooth_exit,
4493 .shutdown = bluetooth_shutdown,
4496 /*************************************************************************
4501 /* ACPI GWAN/SWAN bits */
4502 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4503 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4504 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4505 0 = disable, 1 = enable */
4508 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4510 static int wan_get_status(void)
4514 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4516 return (tpacpi_wwan_emulstate) ?
4517 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4520 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4523 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4524 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4527 static int wan_set_status(enum tpacpi_rfkill_state state)
4531 vdbg_printk(TPACPI_DBG_RFKILL,
4532 "will attempt to %s wwan\n",
4533 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4535 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4537 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4542 if (state == TPACPI_RFK_RADIO_ON)
4543 status = TP_ACPI_WANCARD_RADIOSSW
4544 | TP_ACPI_WANCARD_RESUMECTRL;
4548 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4554 /* sysfs wan enable ---------------------------------------------------- */
4555 static ssize_t wan_enable_show(struct device *dev,
4556 struct device_attribute *attr,
4559 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4563 static ssize_t wan_enable_store(struct device *dev,
4564 struct device_attribute *attr,
4565 const char *buf, size_t count)
4567 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4571 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4572 wan_enable_show, wan_enable_store);
4574 /* --------------------------------------------------------------------- */
4576 static struct attribute *wan_attributes[] = {
4577 &dev_attr_wwan_enable.attr,
4581 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4584 return tp_features.wan ? attr->mode : 0;
4587 static const struct attribute_group wan_attr_group = {
4588 .is_visible = wan_attr_is_visible,
4589 .attrs = wan_attributes,
4592 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4593 .get_status = wan_get_status,
4594 .set_status = wan_set_status,
4597 static void wan_shutdown(void)
4599 /* Order firmware to save current state to NVRAM */
4600 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4601 TP_ACPI_WGSV_SAVE_STATE))
4602 pr_notice("failed to save WWAN state to NVRAM\n");
4604 vdbg_printk(TPACPI_DBG_RFKILL,
4605 "WWAN state saved to NVRAM\n");
4608 static void wan_exit(void)
4610 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4614 static int __init wan_init(struct ibm_init_struct *iibm)
4619 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4620 "initializing wan subdriver\n");
4622 TPACPI_ACPIHANDLE_INIT(hkey);
4624 tp_features.wan = hkey_handle &&
4625 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4627 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4628 "wan is %s, status 0x%02x\n",
4629 str_supported(tp_features.wan),
4632 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4634 tp_features.wan = 1;
4635 pr_info("wwan switch emulation enabled\n");
4638 if (tp_features.wan &&
4639 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4640 /* no wan hardware present in system */
4641 tp_features.wan = 0;
4642 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4643 "wan hardware not installed\n");
4646 if (!tp_features.wan)
4649 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4652 TPACPI_RFK_WWAN_SW_NAME,
4657 /* procfs -------------------------------------------------------------- */
4658 static int wan_read(struct seq_file *m)
4660 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4663 static int wan_write(char *buf)
4665 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4668 static struct ibm_struct wan_driver_data = {
4673 .shutdown = wan_shutdown,
4676 /*************************************************************************
4681 /* ACPI GUWB/SUWB bits */
4682 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4683 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4686 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4688 static int uwb_get_status(void)
4692 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4694 return (tpacpi_uwb_emulstate) ?
4695 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4698 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4701 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4702 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4705 static int uwb_set_status(enum tpacpi_rfkill_state state)
4709 vdbg_printk(TPACPI_DBG_RFKILL,
4710 "will attempt to %s UWB\n",
4711 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4713 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4715 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4720 if (state == TPACPI_RFK_RADIO_ON)
4721 status = TP_ACPI_UWB_RADIOSSW;
4725 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4731 /* --------------------------------------------------------------------- */
4733 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4734 .get_status = uwb_get_status,
4735 .set_status = uwb_set_status,
4738 static void uwb_exit(void)
4740 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4743 static int __init uwb_init(struct ibm_init_struct *iibm)
4748 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4749 "initializing uwb subdriver\n");
4751 TPACPI_ACPIHANDLE_INIT(hkey);
4753 tp_features.uwb = hkey_handle &&
4754 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4756 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4757 "uwb is %s, status 0x%02x\n",
4758 str_supported(tp_features.uwb),
4761 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4763 tp_features.uwb = 1;
4764 pr_info("uwb switch emulation enabled\n");
4767 if (tp_features.uwb &&
4768 !(status & TP_ACPI_UWB_HWPRESENT)) {
4769 /* no uwb hardware present in system */
4770 tp_features.uwb = 0;
4771 dbg_printk(TPACPI_DBG_INIT,
4772 "uwb hardware not installed\n");
4775 if (!tp_features.uwb)
4778 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4781 TPACPI_RFK_UWB_SW_NAME,
4786 static struct ibm_struct uwb_driver_data = {
4789 .flags.experimental = 1,
4792 /*************************************************************************
4796 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4798 enum video_access_mode {
4799 TPACPI_VIDEO_NONE = 0,
4800 TPACPI_VIDEO_570, /* 570 */
4801 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4802 TPACPI_VIDEO_NEW, /* all others */
4805 enum { /* video status flags, based on VIDEO_570 */
4806 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4807 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4808 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4811 enum { /* TPACPI_VIDEO_570 constants */
4812 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4813 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4814 * video_status_flags */
4815 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4816 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4819 static enum video_access_mode video_supported;
4820 static int video_orig_autosw;
4822 static int video_autosw_get(void);
4823 static int video_autosw_set(int enable);
4825 TPACPI_HANDLE(vid, root,
4826 "\\_SB.PCI.AGP.VGA", /* 570 */
4827 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4828 "\\_SB.PCI0.VID0", /* 770e */
4829 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4830 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4831 "\\_SB.PCI0.AGP.VID", /* all others */
4834 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4836 static int __init video_init(struct ibm_init_struct *iibm)
4840 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4842 TPACPI_ACPIHANDLE_INIT(vid);
4843 if (tpacpi_is_ibm())
4844 TPACPI_ACPIHANDLE_INIT(vid2);
4846 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4847 /* G41, assume IVGA doesn't change */
4848 vid_handle = vid2_handle;
4851 /* video switching not supported on R30, R31 */
4852 video_supported = TPACPI_VIDEO_NONE;
4853 else if (tpacpi_is_ibm() &&
4854 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4856 video_supported = TPACPI_VIDEO_570;
4857 else if (tpacpi_is_ibm() &&
4858 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4859 /* 600e/x, 770e, 770x */
4860 video_supported = TPACPI_VIDEO_770;
4863 video_supported = TPACPI_VIDEO_NEW;
4865 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4866 str_supported(video_supported != TPACPI_VIDEO_NONE),
4869 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
4872 static void video_exit(void)
4874 dbg_printk(TPACPI_DBG_EXIT,
4875 "restoring original video autoswitch mode\n");
4876 if (video_autosw_set(video_orig_autosw))
4877 pr_err("error while trying to restore original video autoswitch mode\n");
4880 static int video_outputsw_get(void)
4885 switch (video_supported) {
4886 case TPACPI_VIDEO_570:
4887 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4888 TP_ACPI_VIDEO_570_PHSCMD))
4890 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4892 case TPACPI_VIDEO_770:
4893 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4896 status |= TP_ACPI_VIDEO_S_LCD;
4897 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4900 status |= TP_ACPI_VIDEO_S_CRT;
4902 case TPACPI_VIDEO_NEW:
4903 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4904 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4907 status |= TP_ACPI_VIDEO_S_CRT;
4909 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4910 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4913 status |= TP_ACPI_VIDEO_S_LCD;
4914 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4917 status |= TP_ACPI_VIDEO_S_DVI;
4926 static int video_outputsw_set(int status)
4931 switch (video_supported) {
4932 case TPACPI_VIDEO_570:
4933 res = acpi_evalf(NULL, NULL,
4934 "\\_SB.PHS2", "vdd",
4935 TP_ACPI_VIDEO_570_PHS2CMD,
4936 status | TP_ACPI_VIDEO_570_PHS2SET);
4938 case TPACPI_VIDEO_770:
4939 autosw = video_autosw_get();
4943 res = video_autosw_set(1);
4946 res = acpi_evalf(vid_handle, NULL,
4947 "ASWT", "vdd", status * 0x100, 0);
4948 if (!autosw && video_autosw_set(autosw)) {
4949 pr_err("video auto-switch left enabled due to error\n");
4953 case TPACPI_VIDEO_NEW:
4954 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4955 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4961 return (res) ? 0 : -EIO;
4964 static int video_autosw_get(void)
4968 switch (video_supported) {
4969 case TPACPI_VIDEO_570:
4970 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4973 case TPACPI_VIDEO_770:
4974 case TPACPI_VIDEO_NEW:
4975 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4985 static int video_autosw_set(int enable)
4987 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4992 static int video_outputsw_cycle(void)
4994 int autosw = video_autosw_get();
5000 switch (video_supported) {
5001 case TPACPI_VIDEO_570:
5002 res = video_autosw_set(1);
5005 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5007 case TPACPI_VIDEO_770:
5008 case TPACPI_VIDEO_NEW:
5009 res = video_autosw_set(1);
5012 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5017 if (!autosw && video_autosw_set(autosw)) {
5018 pr_err("video auto-switch left enabled due to error\n");
5022 return (res) ? 0 : -EIO;
5025 static int video_expand_toggle(void)
5027 switch (video_supported) {
5028 case TPACPI_VIDEO_570:
5029 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5031 case TPACPI_VIDEO_770:
5032 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5034 case TPACPI_VIDEO_NEW:
5035 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5043 static int video_read(struct seq_file *m)
5047 if (video_supported == TPACPI_VIDEO_NONE) {
5048 seq_printf(m, "status:\t\tnot supported\n");
5052 /* Even reads can crash X.org, so... */
5053 if (!capable(CAP_SYS_ADMIN))
5056 status = video_outputsw_get();
5060 autosw = video_autosw_get();
5064 seq_printf(m, "status:\t\tsupported\n");
5065 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5066 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5067 if (video_supported == TPACPI_VIDEO_NEW)
5068 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5069 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5070 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5071 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5072 if (video_supported == TPACPI_VIDEO_NEW)
5073 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5074 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5075 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5080 static int video_write(char *buf)
5083 int enable, disable, status;
5086 if (video_supported == TPACPI_VIDEO_NONE)
5089 /* Even reads can crash X.org, let alone writes... */
5090 if (!capable(CAP_SYS_ADMIN))
5096 while ((cmd = strsep(&buf, ","))) {
5097 if (strlencmp(cmd, "lcd_enable") == 0) {
5098 enable |= TP_ACPI_VIDEO_S_LCD;
5099 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5100 disable |= TP_ACPI_VIDEO_S_LCD;
5101 } else if (strlencmp(cmd, "crt_enable") == 0) {
5102 enable |= TP_ACPI_VIDEO_S_CRT;
5103 } else if (strlencmp(cmd, "crt_disable") == 0) {
5104 disable |= TP_ACPI_VIDEO_S_CRT;
5105 } else if (video_supported == TPACPI_VIDEO_NEW &&
5106 strlencmp(cmd, "dvi_enable") == 0) {
5107 enable |= TP_ACPI_VIDEO_S_DVI;
5108 } else if (video_supported == TPACPI_VIDEO_NEW &&
5109 strlencmp(cmd, "dvi_disable") == 0) {
5110 disable |= TP_ACPI_VIDEO_S_DVI;
5111 } else if (strlencmp(cmd, "auto_enable") == 0) {
5112 res = video_autosw_set(1);
5115 } else if (strlencmp(cmd, "auto_disable") == 0) {
5116 res = video_autosw_set(0);
5119 } else if (strlencmp(cmd, "video_switch") == 0) {
5120 res = video_outputsw_cycle();
5123 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5124 res = video_expand_toggle();
5131 if (enable || disable) {
5132 status = video_outputsw_get();
5135 res = video_outputsw_set((status & ~disable) | enable);
5143 static struct ibm_struct video_driver_data = {
5146 .write = video_write,
5150 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5152 /*************************************************************************
5153 * Keyboard backlight subdriver
5156 static enum led_brightness kbdlight_brightness;
5157 static DEFINE_MUTEX(kbdlight_mutex);
5159 static int kbdlight_set_level(int level)
5166 mutex_lock(&kbdlight_mutex);
5168 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5171 kbdlight_brightness = level;
5173 mutex_unlock(&kbdlight_mutex);
5178 static int kbdlight_get_level(void)
5185 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5191 return status & 0x3;
5194 static bool kbdlight_is_supported(void)
5201 if (!acpi_has_method(hkey_handle, "MLCG")) {
5202 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5206 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5207 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5212 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5216 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5218 * Guessed test for keyboard backlight:
5220 * Machines with backlight keyboard return:
5221 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5222 * b110100010010000000XX - ThinkPad x230
5223 * b010100000010000000XX - ThinkPad x240
5224 * b010100000010000000XX - ThinkPad W541
5225 * (XX is current backlight level)
5227 * Machines without backlight keyboard return:
5228 * b10100001000000000000 - ThinkPad x230
5229 * b10110001000000000000 - ThinkPad E430
5230 * b00000000000000000000 - ThinkPad E450
5232 * Candidate BITs for detection test (XOR):
5233 * b01000000001000000000
5236 return status & BIT(9);
5239 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5240 enum led_brightness brightness)
5242 return kbdlight_set_level(brightness);
5245 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5249 level = kbdlight_get_level();
5256 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5258 .name = "tpacpi::kbd_backlight",
5259 .max_brightness = 2,
5260 .flags = LED_BRIGHT_HW_CHANGED,
5261 .brightness_set_blocking = &kbdlight_sysfs_set,
5262 .brightness_get = &kbdlight_sysfs_get,
5266 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5270 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5272 TPACPI_ACPIHANDLE_INIT(hkey);
5274 if (!kbdlight_is_supported()) {
5275 tp_features.kbdlight = 0;
5276 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5280 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5281 tp_features.kbdlight = 1;
5283 rc = led_classdev_register(&tpacpi_pdev->dev,
5284 &tpacpi_led_kbdlight.led_classdev);
5286 tp_features.kbdlight = 0;
5290 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5291 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5295 static void kbdlight_exit(void)
5297 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5300 static int kbdlight_set_level_and_update(int level)
5303 struct led_classdev *led_cdev;
5305 ret = kbdlight_set_level(level);
5306 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5308 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5309 led_cdev->brightness = level;
5314 static int kbdlight_read(struct seq_file *m)
5318 if (!tp_features.kbdlight) {
5319 seq_printf(m, "status:\t\tnot supported\n");
5321 level = kbdlight_get_level();
5323 seq_printf(m, "status:\t\terror %d\n", level);
5325 seq_printf(m, "status:\t\t%d\n", level);
5326 seq_printf(m, "commands:\t0, 1, 2\n");
5332 static int kbdlight_write(char *buf)
5335 int res, level = -EINVAL;
5337 if (!tp_features.kbdlight)
5340 while ((cmd = strsep(&buf, ","))) {
5341 res = kstrtoint(cmd, 10, &level);
5346 if (level >= 3 || level < 0)
5349 return kbdlight_set_level_and_update(level);
5352 static void kbdlight_suspend(void)
5354 struct led_classdev *led_cdev;
5356 if (!tp_features.kbdlight)
5359 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5360 led_update_brightness(led_cdev);
5361 led_classdev_suspend(led_cdev);
5364 static void kbdlight_resume(void)
5366 if (!tp_features.kbdlight)
5369 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5372 static struct ibm_struct kbdlight_driver_data = {
5374 .read = kbdlight_read,
5375 .write = kbdlight_write,
5376 .suspend = kbdlight_suspend,
5377 .resume = kbdlight_resume,
5378 .exit = kbdlight_exit,
5381 /*************************************************************************
5382 * Light (thinklight) subdriver
5385 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5386 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5388 static int light_get_status(void)
5392 if (tp_features.light_status) {
5393 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5401 static int light_set_status(int status)
5405 if (tp_features.light) {
5407 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5409 TP_CMOS_THINKLIGHT_ON :
5410 TP_CMOS_THINKLIGHT_OFF);
5412 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5415 return (rc) ? 0 : -EIO;
5421 static int light_sysfs_set(struct led_classdev *led_cdev,
5422 enum led_brightness brightness)
5424 return light_set_status((brightness != LED_OFF) ?
5425 TPACPI_LED_ON : TPACPI_LED_OFF);
5428 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5430 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5433 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5435 .name = "tpacpi::thinklight",
5436 .brightness_set_blocking = &light_sysfs_set,
5437 .brightness_get = &light_sysfs_get,
5441 static int __init light_init(struct ibm_init_struct *iibm)
5445 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5447 if (tpacpi_is_ibm()) {
5448 TPACPI_ACPIHANDLE_INIT(ledb);
5449 TPACPI_ACPIHANDLE_INIT(lght);
5451 TPACPI_ACPIHANDLE_INIT(cmos);
5453 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5454 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5456 if (tp_features.light)
5457 /* light status not supported on
5458 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5459 tp_features.light_status =
5460 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5462 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5463 str_supported(tp_features.light),
5464 str_supported(tp_features.light_status));
5466 if (!tp_features.light)
5469 rc = led_classdev_register(&tpacpi_pdev->dev,
5470 &tpacpi_led_thinklight.led_classdev);
5473 tp_features.light = 0;
5474 tp_features.light_status = 0;
5482 static void light_exit(void)
5484 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5487 static int light_read(struct seq_file *m)
5491 if (!tp_features.light) {
5492 seq_printf(m, "status:\t\tnot supported\n");
5493 } else if (!tp_features.light_status) {
5494 seq_printf(m, "status:\t\tunknown\n");
5495 seq_printf(m, "commands:\ton, off\n");
5497 status = light_get_status();
5500 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5501 seq_printf(m, "commands:\ton, off\n");
5507 static int light_write(char *buf)
5512 if (!tp_features.light)
5515 while ((cmd = strsep(&buf, ","))) {
5516 if (strlencmp(cmd, "on") == 0) {
5518 } else if (strlencmp(cmd, "off") == 0) {
5524 return light_set_status(newstatus);
5527 static struct ibm_struct light_driver_data = {
5530 .write = light_write,
5534 /*************************************************************************
5538 /* sysfs cmos_command -------------------------------------------------- */
5539 static ssize_t cmos_command_store(struct device *dev,
5540 struct device_attribute *attr,
5541 const char *buf, size_t count)
5543 unsigned long cmos_cmd;
5546 if (parse_strtoul(buf, 21, &cmos_cmd))
5549 res = issue_thinkpad_cmos_command(cmos_cmd);
5550 return (res) ? res : count;
5553 static DEVICE_ATTR_WO(cmos_command);
5555 static struct attribute *cmos_attributes[] = {
5556 &dev_attr_cmos_command.attr,
5560 static umode_t cmos_attr_is_visible(struct kobject *kobj,
5561 struct attribute *attr, int n)
5563 return cmos_handle ? attr->mode : 0;
5566 static const struct attribute_group cmos_attr_group = {
5567 .is_visible = cmos_attr_is_visible,
5568 .attrs = cmos_attributes,
5571 /* --------------------------------------------------------------------- */
5573 static int __init cmos_init(struct ibm_init_struct *iibm)
5575 vdbg_printk(TPACPI_DBG_INIT,
5576 "initializing cmos commands subdriver\n");
5578 TPACPI_ACPIHANDLE_INIT(cmos);
5580 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5581 str_supported(cmos_handle != NULL));
5583 return cmos_handle ? 0 : -ENODEV;
5586 static int cmos_read(struct seq_file *m)
5588 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5589 R30, R31, T20-22, X20-21 */
5591 seq_printf(m, "status:\t\tnot supported\n");
5593 seq_printf(m, "status:\t\tsupported\n");
5594 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5600 static int cmos_write(char *buf)
5605 while ((cmd = strsep(&buf, ","))) {
5606 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5607 cmos_cmd >= 0 && cmos_cmd <= 21) {
5612 res = issue_thinkpad_cmos_command(cmos_cmd);
5620 static struct ibm_struct cmos_driver_data = {
5623 .write = cmos_write,
5626 /*************************************************************************
5630 enum led_access_mode {
5631 TPACPI_LED_NONE = 0,
5632 TPACPI_LED_570, /* 570 */
5633 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5634 TPACPI_LED_NEW, /* all others */
5637 enum { /* For TPACPI_LED_OLD */
5638 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5639 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5640 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5643 static enum led_access_mode led_supported;
5645 static acpi_handle led_handle;
5647 #define TPACPI_LED_NUMLEDS 16
5648 static struct tpacpi_led_classdev *tpacpi_leds;
5649 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5650 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5651 /* there's a limit of 19 chars + NULL before 2.6.26 */
5653 "tpacpi:orange:batt",
5654 "tpacpi:green:batt",
5655 "tpacpi::dock_active",
5656 "tpacpi::bay_active",
5657 "tpacpi::dock_batt",
5658 "tpacpi::unknown_led",
5660 "tpacpi::dock_status1",
5661 "tpacpi::dock_status2",
5662 "tpacpi::lid_logo_dot",
5663 "tpacpi::unknown_led3",
5664 "tpacpi::thinkvantage",
5666 #define TPACPI_SAFE_LEDS 0x1481U
5668 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5670 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5673 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5677 static int led_get_status(const unsigned int led)
5680 enum led_status_t led_s;
5682 switch (led_supported) {
5683 case TPACPI_LED_570:
5684 if (!acpi_evalf(ec_handle,
5685 &status, "GLED", "dd", 1 << led))
5687 led_s = (status == 0) ?
5692 tpacpi_led_state_cache[led] = led_s;
5701 static int led_set_status(const unsigned int led,
5702 const enum led_status_t ledstatus)
5704 /* off, on, blink. Index is led_status_t */
5705 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5706 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5710 switch (led_supported) {
5711 case TPACPI_LED_570:
5713 if (unlikely(led > 7))
5715 if (unlikely(tpacpi_is_led_restricted(led)))
5717 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5718 (1 << led), led_sled_arg1[ledstatus]))
5721 case TPACPI_LED_OLD:
5722 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5723 if (unlikely(led > 7))
5725 if (unlikely(tpacpi_is_led_restricted(led)))
5727 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5729 rc = ec_write(TPACPI_LED_EC_HLBL,
5730 (ledstatus == TPACPI_LED_BLINK) << led);
5732 rc = ec_write(TPACPI_LED_EC_HLCL,
5733 (ledstatus != TPACPI_LED_OFF) << led);
5735 case TPACPI_LED_NEW:
5737 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5739 if (unlikely(tpacpi_is_led_restricted(led)))
5741 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5742 led, led_led_arg1[ledstatus]))
5750 tpacpi_led_state_cache[led] = ledstatus;
5755 static int led_sysfs_set(struct led_classdev *led_cdev,
5756 enum led_brightness brightness)
5758 struct tpacpi_led_classdev *data = container_of(led_cdev,
5759 struct tpacpi_led_classdev, led_classdev);
5760 enum led_status_t new_state;
5762 if (brightness == LED_OFF)
5763 new_state = TPACPI_LED_OFF;
5764 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5765 new_state = TPACPI_LED_ON;
5767 new_state = TPACPI_LED_BLINK;
5769 return led_set_status(data->led, new_state);
5772 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5773 unsigned long *delay_on, unsigned long *delay_off)
5775 struct tpacpi_led_classdev *data = container_of(led_cdev,
5776 struct tpacpi_led_classdev, led_classdev);
5778 /* Can we choose the flash rate? */
5779 if (*delay_on == 0 && *delay_off == 0) {
5780 /* yes. set them to the hardware blink rate (1 Hz) */
5781 *delay_on = 500; /* ms */
5782 *delay_off = 500; /* ms */
5783 } else if ((*delay_on != 500) || (*delay_off != 500))
5786 return led_set_status(data->led, TPACPI_LED_BLINK);
5789 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5793 struct tpacpi_led_classdev *data = container_of(led_cdev,
5794 struct tpacpi_led_classdev, led_classdev);
5796 rc = led_get_status(data->led);
5798 if (rc == TPACPI_LED_OFF || rc < 0)
5799 rc = LED_OFF; /* no error handling in led class :( */
5806 static void led_exit(void)
5810 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5811 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5816 static int __init tpacpi_init_led(unsigned int led)
5818 /* LEDs with no name don't get registered */
5819 if (!tpacpi_led_names[led])
5822 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5823 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5824 if (led_supported == TPACPI_LED_570)
5825 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5827 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5828 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
5829 tpacpi_leds[led].led = led;
5831 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5834 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5835 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5836 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5837 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5839 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5840 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5841 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5842 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5843 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5844 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5845 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5846 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5848 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5849 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5850 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5851 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5852 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5854 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5855 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5856 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5857 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5859 /* (1) - may have excess leds enabled on MSB */
5861 /* Defaults (order matters, keep last, don't reorder!) */
5863 .vendor = PCI_VENDOR_ID_LENOVO,
5864 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5867 { /* IBM ThinkPads with no EC version string */
5868 .vendor = PCI_VENDOR_ID_IBM,
5869 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5872 { /* IBM ThinkPads with EC version string */
5873 .vendor = PCI_VENDOR_ID_IBM,
5874 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5879 static enum led_access_mode __init led_init_detect_mode(void)
5883 if (tpacpi_is_ibm()) {
5885 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5886 if (ACPI_SUCCESS(status))
5887 return TPACPI_LED_570;
5889 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5890 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5891 if (ACPI_SUCCESS(status))
5892 return TPACPI_LED_OLD;
5896 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5897 if (ACPI_SUCCESS(status))
5898 return TPACPI_LED_NEW;
5900 /* R30, R31, and unknown firmwares */
5902 return TPACPI_LED_NONE;
5905 static int __init led_init(struct ibm_init_struct *iibm)
5909 unsigned long useful_leds;
5911 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5913 led_supported = led_init_detect_mode();
5915 if (led_supported != TPACPI_LED_NONE) {
5916 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5917 ARRAY_SIZE(led_useful_qtable));
5921 led_supported = TPACPI_LED_NONE;
5925 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5926 str_supported(led_supported), led_supported);
5928 if (led_supported == TPACPI_LED_NONE)
5931 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
5934 pr_err("Out of memory for LED data\n");
5938 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5939 tpacpi_leds[i].led = -1;
5941 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
5942 rc = tpacpi_init_led(i);
5950 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5951 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
5956 #define str_led_status(s) \
5957 ((s) == TPACPI_LED_OFF ? "off" : \
5958 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5960 static int led_read(struct seq_file *m)
5962 if (!led_supported) {
5963 seq_printf(m, "status:\t\tnot supported\n");
5966 seq_printf(m, "status:\t\tsupported\n");
5968 if (led_supported == TPACPI_LED_570) {
5971 for (i = 0; i < 8; i++) {
5972 status = led_get_status(i);
5975 seq_printf(m, "%d:\t\t%s\n",
5976 i, str_led_status(status));
5980 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5985 static int led_write(char *buf)
5989 enum led_status_t s;
5994 while ((cmd = strsep(&buf, ","))) {
5995 if (sscanf(cmd, "%d", &led) != 1)
5998 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
6001 if (tpacpi_leds[led].led < 0)
6004 if (strstr(cmd, "off")) {
6006 } else if (strstr(cmd, "on")) {
6008 } else if (strstr(cmd, "blink")) {
6009 s = TPACPI_LED_BLINK;
6014 rc = led_set_status(led, s);
6022 static struct ibm_struct led_driver_data = {
6029 /*************************************************************************
6033 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6035 #define TPACPI_BEEP_Q1 0x0001
6037 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6038 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6039 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6042 static int __init beep_init(struct ibm_init_struct *iibm)
6044 unsigned long quirks;
6046 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6048 TPACPI_ACPIHANDLE_INIT(beep);
6050 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6051 str_supported(beep_handle != NULL));
6053 quirks = tpacpi_check_quirks(beep_quirk_table,
6054 ARRAY_SIZE(beep_quirk_table));
6056 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6058 return (beep_handle) ? 0 : -ENODEV;
6061 static int beep_read(struct seq_file *m)
6064 seq_printf(m, "status:\t\tnot supported\n");
6066 seq_printf(m, "status:\t\tsupported\n");
6067 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6073 static int beep_write(char *buf)
6081 while ((cmd = strsep(&buf, ","))) {
6082 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6083 beep_cmd >= 0 && beep_cmd <= 17) {
6087 if (tp_features.beep_needs_two_args) {
6088 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6092 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6101 static struct ibm_struct beep_driver_data = {
6104 .write = beep_write,
6107 /*************************************************************************
6111 enum thermal_access_mode {
6112 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6113 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6114 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6115 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6116 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6119 enum { /* TPACPI_THERMAL_TPEC_* */
6120 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6121 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6122 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6123 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6125 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6129 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6130 struct ibm_thermal_sensors_struct {
6131 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6134 static enum thermal_access_mode thermal_read_mode;
6135 static bool thermal_use_labels;
6137 /* idx is zero-based */
6138 static int thermal_get_sensor(int idx, s32 *value)
6144 t = TP_EC_THERMAL_TMP0;
6146 switch (thermal_read_mode) {
6147 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6148 case TPACPI_THERMAL_TPEC_16:
6149 if (idx >= 8 && idx <= 15) {
6150 t = TP_EC_THERMAL_TMP8;
6155 case TPACPI_THERMAL_TPEC_8:
6157 if (!acpi_ec_read(t + idx, &tmp))
6159 *value = tmp * 1000;
6164 case TPACPI_THERMAL_ACPI_UPDT:
6166 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6167 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6169 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6171 *value = (t - 2732) * 100;
6176 case TPACPI_THERMAL_ACPI_TMP07:
6178 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6179 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6181 if (t > 127 || t < -127)
6182 t = TP_EC_THERMAL_TMP_NA;
6188 case TPACPI_THERMAL_NONE:
6196 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6207 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6210 for (i = 0 ; i < n; i++) {
6211 res = thermal_get_sensor(i, &s->temp[i]);
6219 static void thermal_dump_all_sensors(void)
6222 struct ibm_thermal_sensors_struct t;
6224 n = thermal_get_sensors(&t);
6228 pr_notice("temperatures (Celsius):");
6230 for (i = 0; i < n; i++) {
6231 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6232 pr_cont(" %d", (int)(t.temp[i] / 1000));
6240 /* sysfs temp##_input -------------------------------------------------- */
6242 static ssize_t thermal_temp_input_show(struct device *dev,
6243 struct device_attribute *attr,
6246 struct sensor_device_attribute *sensor_attr =
6247 to_sensor_dev_attr(attr);
6248 int idx = sensor_attr->index;
6252 res = thermal_get_sensor(idx, &value);
6255 if (value == TPACPI_THERMAL_SENSOR_NA)
6258 return sysfs_emit(buf, "%d\n", value);
6261 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6262 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6263 thermal_temp_input_show, NULL, _idxB)
6265 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6266 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6267 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6268 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6269 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6270 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6271 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6272 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6273 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6274 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6275 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6276 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6277 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6278 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6279 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6280 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6281 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6284 #define THERMAL_ATTRS(X) \
6285 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6287 static struct attribute *thermal_temp_input_attr[] = {
6307 static umode_t thermal_attr_is_visible(struct kobject *kobj,
6308 struct attribute *attr, int n)
6310 if (thermal_read_mode == TPACPI_THERMAL_NONE)
6313 if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) ||
6314 attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) ||
6315 attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) ||
6316 attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) {
6317 if (thermal_read_mode != TPACPI_THERMAL_TPEC_16)
6324 static const struct attribute_group thermal_attr_group = {
6325 .is_visible = thermal_attr_is_visible,
6326 .attrs = thermal_temp_input_attr,
6329 #undef THERMAL_SENSOR_ATTR_TEMP
6330 #undef THERMAL_ATTRS
6332 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6334 return sysfs_emit(buf, "CPU\n");
6336 static DEVICE_ATTR_RO(temp1_label);
6338 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6340 return sysfs_emit(buf, "GPU\n");
6342 static DEVICE_ATTR_RO(temp2_label);
6344 static struct attribute *temp_label_attributes[] = {
6345 &dev_attr_temp1_label.attr,
6346 &dev_attr_temp2_label.attr,
6350 static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6351 struct attribute *attr, int n)
6353 return thermal_use_labels ? attr->mode : 0;
6356 static const struct attribute_group temp_label_attr_group = {
6357 .is_visible = temp_label_attr_is_visible,
6358 .attrs = temp_label_attributes,
6361 /* --------------------------------------------------------------------- */
6363 static int __init thermal_init(struct ibm_init_struct *iibm)
6365 u8 t, ta1, ta2, ver = 0;
6369 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6371 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6373 if (thinkpad_id.ec_model) {
6375 * Direct EC access mode: sensors at registers
6376 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6377 * non-implemented, thermal sensors return 0x80 when
6379 * The above rule is unfortunately flawed. This has been seen with
6380 * 0xC2 (power supply ID) causing thermal control problems.
6381 * The EC version can be determined by offset 0xEF and at least for
6382 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6383 * are not thermal registers.
6385 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6386 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6389 for (i = 0; i < 8; i++) {
6390 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6397 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6406 /* This is sheer paranoia, but we handle it anyway */
6408 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6409 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6411 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6412 thermal_read_mode = TPACPI_THERMAL_NONE;
6416 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6417 thermal_use_labels = true;
6421 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6424 } else if (acpi_tmp7) {
6425 if (tpacpi_is_ibm() &&
6426 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6427 /* 600e/x, 770e, 770x */
6428 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6430 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6431 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6434 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6435 thermal_read_mode = TPACPI_THERMAL_NONE;
6438 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6439 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6442 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
6445 static int thermal_read(struct seq_file *m)
6448 struct ibm_thermal_sensors_struct t;
6450 n = thermal_get_sensors(&t);
6451 if (unlikely(n < 0))
6454 seq_printf(m, "temperatures:\t");
6457 for (i = 0; i < (n - 1); i++)
6458 seq_printf(m, "%d ", t.temp[i] / 1000);
6459 seq_printf(m, "%d\n", t.temp[i] / 1000);
6461 seq_printf(m, "not supported\n");
6466 static struct ibm_struct thermal_driver_data = {
6468 .read = thermal_read,
6471 /*************************************************************************
6472 * Backlight/brightness subdriver
6475 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6478 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6479 * CMOS NVRAM byte 0x5E, bits 0-3.
6481 * EC HBRV (0x31) has the following layout
6482 * Bit 7: unknown function
6483 * Bit 6: unknown function
6484 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6485 * Bit 4: must be set to zero to avoid problems
6486 * Bit 3-0: backlight brightness level
6488 * brightness_get_raw returns status data in the HBRV layout
6490 * WARNING: The X61 has been verified to use HBRV for something else, so
6491 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6492 * testing on the very early *60 Lenovo models...
6496 TP_EC_BACKLIGHT = 0x31,
6498 /* TP_EC_BACKLIGHT bitmasks */
6499 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6500 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6501 TP_EC_BACKLIGHT_MAPSW = 0x20,
6504 enum tpacpi_brightness_access_mode {
6505 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6506 TPACPI_BRGHT_MODE_EC, /* EC control */
6507 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6508 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6509 TPACPI_BRGHT_MODE_MAX
6512 static struct backlight_device *ibm_backlight_device;
6514 static enum tpacpi_brightness_access_mode brightness_mode =
6515 TPACPI_BRGHT_MODE_MAX;
6517 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6519 static struct mutex brightness_mutex;
6521 /* NVRAM brightness access,
6522 * call with brightness_mutex held! */
6523 static unsigned int tpacpi_brightness_nvram_get(void)
6527 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6528 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6529 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6530 lnvram &= bright_maxlvl;
6535 static void tpacpi_brightness_checkpoint_nvram(void)
6540 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6543 vdbg_printk(TPACPI_DBG_BRGHT,
6544 "trying to checkpoint backlight level to NVRAM...\n");
6546 if (mutex_lock_killable(&brightness_mutex) < 0)
6549 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6551 lec &= TP_EC_BACKLIGHT_LVLMSK;
6552 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6554 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6555 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6556 /* NVRAM needs update */
6557 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6558 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6560 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6561 dbg_printk(TPACPI_DBG_BRGHT,
6562 "updated NVRAM backlight level to %u (0x%02x)\n",
6563 (unsigned int) lec, (unsigned int) b_nvram);
6565 vdbg_printk(TPACPI_DBG_BRGHT,
6566 "NVRAM backlight level already is %u (0x%02x)\n",
6567 (unsigned int) lec, (unsigned int) b_nvram);
6570 mutex_unlock(&brightness_mutex);
6574 /* call with brightness_mutex held! */
6575 static int tpacpi_brightness_get_raw(int *status)
6579 switch (brightness_mode) {
6580 case TPACPI_BRGHT_MODE_UCMS_STEP:
6581 *status = tpacpi_brightness_nvram_get();
6583 case TPACPI_BRGHT_MODE_EC:
6584 case TPACPI_BRGHT_MODE_ECNVRAM:
6585 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6594 /* call with brightness_mutex held! */
6595 /* do NOT call with illegal backlight level value */
6596 static int tpacpi_brightness_set_ec(unsigned int value)
6600 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6603 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6604 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6605 (value & TP_EC_BACKLIGHT_LVLMSK))))
6611 /* call with brightness_mutex held! */
6612 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6615 unsigned int current_value, i;
6617 current_value = tpacpi_brightness_nvram_get();
6619 if (value == current_value)
6622 cmos_cmd = (value > current_value) ?
6623 TP_CMOS_BRIGHTNESS_UP :
6624 TP_CMOS_BRIGHTNESS_DOWN;
6625 inc = (value > current_value) ? 1 : -1;
6627 for (i = current_value; i != value; i += inc)
6628 if (issue_thinkpad_cmos_command(cmos_cmd))
6634 /* May return EINTR which can always be mapped to ERESTARTSYS */
6635 static int brightness_set(unsigned int value)
6639 if (value > bright_maxlvl)
6642 vdbg_printk(TPACPI_DBG_BRGHT,
6643 "set backlight level to %d\n", value);
6645 res = mutex_lock_killable(&brightness_mutex);
6649 switch (brightness_mode) {
6650 case TPACPI_BRGHT_MODE_EC:
6651 case TPACPI_BRGHT_MODE_ECNVRAM:
6652 res = tpacpi_brightness_set_ec(value);
6654 case TPACPI_BRGHT_MODE_UCMS_STEP:
6655 res = tpacpi_brightness_set_ucmsstep(value);
6661 mutex_unlock(&brightness_mutex);
6665 /* sysfs backlight class ----------------------------------------------- */
6667 static int brightness_update_status(struct backlight_device *bd)
6669 unsigned int level =
6670 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6671 bd->props.power == FB_BLANK_UNBLANK) ?
6672 bd->props.brightness : 0;
6674 dbg_printk(TPACPI_DBG_BRGHT,
6675 "backlight: attempt to set level to %d\n",
6678 /* it is the backlight class's job (caller) to handle
6679 * EINTR and other errors properly */
6680 return brightness_set(level);
6683 static int brightness_get(struct backlight_device *bd)
6687 res = mutex_lock_killable(&brightness_mutex);
6691 res = tpacpi_brightness_get_raw(&status);
6693 mutex_unlock(&brightness_mutex);
6698 return status & TP_EC_BACKLIGHT_LVLMSK;
6701 static void tpacpi_brightness_notify_change(void)
6703 backlight_force_update(ibm_backlight_device,
6704 BACKLIGHT_UPDATE_HOTKEY);
6707 static const struct backlight_ops ibm_backlight_data = {
6708 .get_brightness = brightness_get,
6709 .update_status = brightness_update_status,
6712 /* --------------------------------------------------------------------- */
6715 * Call _BCL method of video device. On some ThinkPads this will
6716 * switch the firmware to the ACPI brightness control mode.
6719 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6721 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6722 union acpi_object *obj;
6723 struct acpi_device *device, *child;
6726 if (acpi_bus_get_device(handle, &device))
6730 list_for_each_entry(child, &device->children, node) {
6731 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6733 if (ACPI_FAILURE(status)) {
6734 buffer.length = ACPI_ALLOCATE_BUFFER;
6738 obj = (union acpi_object *)buffer.pointer;
6739 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6740 pr_err("Unknown _BCL data, please report this to %s\n",
6744 rc = obj->package.count;
6749 kfree(buffer.pointer);
6755 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6757 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6759 acpi_handle video_device;
6762 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6764 bcl_levels = tpacpi_query_bcl_levels(video_device);
6766 tp_features.bright_acpimode = (bcl_levels > 0);
6768 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6772 * These are only useful for models that have only one possibility
6773 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6776 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6777 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6778 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6780 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6781 /* Models with ATI GPUs known to require ECNVRAM mode */
6782 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6784 /* Models with ATI GPUs that can use ECNVRAM */
6785 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6786 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6787 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6788 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6790 /* Models with Intel Extreme Graphics 2 */
6791 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6792 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6793 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6795 /* Models with Intel GMA900 */
6796 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6797 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6798 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6802 * Returns < 0 for error, otherwise sets tp_features.bright_*
6803 * and bright_maxlvl.
6805 static void __init tpacpi_detect_brightness_capabilities(void)
6809 vdbg_printk(TPACPI_DBG_INIT,
6810 "detecting firmware brightness interface capabilities\n");
6812 /* we could run a quirks check here (same table used by
6813 * brightness_init) if needed */
6816 * We always attempt to detect acpi support, so as to switch
6817 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6818 * going to publish a backlight interface
6820 b = tpacpi_check_std_acpi_brightness_support();
6830 tp_features.bright_unkfw = 1;
6831 bright_maxlvl = b - 1;
6833 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6836 static int __init brightness_init(struct ibm_init_struct *iibm)
6838 struct backlight_properties props;
6840 unsigned long quirks;
6842 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6844 mutex_init(&brightness_mutex);
6846 quirks = tpacpi_check_quirks(brightness_quirk_table,
6847 ARRAY_SIZE(brightness_quirk_table));
6849 /* tpacpi_detect_brightness_capabilities() must have run already */
6851 /* if it is unknown, we don't handle it: it wouldn't be safe */
6852 if (tp_features.bright_unkfw)
6855 if (!brightness_enable) {
6856 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6857 "brightness support disabled by module parameter\n");
6861 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6862 if (brightness_enable > 1) {
6863 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6865 } else if (brightness_enable == 1) {
6866 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6869 } else if (!tp_features.bright_acpimode) {
6870 pr_notice("ACPI backlight interface not available\n");
6874 pr_notice("ACPI native brightness control enabled\n");
6877 * Check for module parameter bogosity, note that we
6878 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6879 * able to detect "unspecified"
6881 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6884 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6885 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6886 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6887 if (quirks & TPACPI_BRGHT_Q_EC)
6888 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6890 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6892 dbg_printk(TPACPI_DBG_BRGHT,
6893 "driver auto-selected brightness_mode=%d\n",
6898 if (!tpacpi_is_ibm() &&
6899 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6900 brightness_mode == TPACPI_BRGHT_MODE_EC))
6903 if (tpacpi_brightness_get_raw(&b) < 0)
6906 memset(&props, 0, sizeof(struct backlight_properties));
6907 props.type = BACKLIGHT_PLATFORM;
6908 props.max_brightness = bright_maxlvl;
6909 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6910 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6912 &ibm_backlight_data,
6914 if (IS_ERR(ibm_backlight_device)) {
6915 int rc = PTR_ERR(ibm_backlight_device);
6916 ibm_backlight_device = NULL;
6917 pr_err("Could not register backlight device\n");
6920 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6921 "brightness is supported\n");
6923 if (quirks & TPACPI_BRGHT_Q_ASK) {
6924 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6926 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6930 /* Added by mistake in early 2007. Probably useless, but it could
6931 * be working around some unknown firmware problem where the value
6932 * read at startup doesn't match the real hardware state... so leave
6933 * it in place just in case */
6934 backlight_update_status(ibm_backlight_device);
6936 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6937 "brightness: registering brightness hotkeys as change notification\n");
6938 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6939 | TP_ACPI_HKEY_BRGHTUP_MASK
6940 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6944 static void brightness_suspend(void)
6946 tpacpi_brightness_checkpoint_nvram();
6949 static void brightness_shutdown(void)
6951 tpacpi_brightness_checkpoint_nvram();
6954 static void brightness_exit(void)
6956 if (ibm_backlight_device) {
6957 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6958 "calling backlight_device_unregister()\n");
6959 backlight_device_unregister(ibm_backlight_device);
6962 tpacpi_brightness_checkpoint_nvram();
6965 static int brightness_read(struct seq_file *m)
6969 level = brightness_get(NULL);
6971 seq_printf(m, "level:\t\tunreadable\n");
6973 seq_printf(m, "level:\t\t%d\n", level);
6974 seq_printf(m, "commands:\tup, down\n");
6975 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6982 static int brightness_write(char *buf)
6988 level = brightness_get(NULL);
6992 while ((cmd = strsep(&buf, ","))) {
6993 if (strlencmp(cmd, "up") == 0) {
6994 if (level < bright_maxlvl)
6996 } else if (strlencmp(cmd, "down") == 0) {
6999 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7000 level >= 0 && level <= bright_maxlvl) {
7006 tpacpi_disclose_usertask("procfs brightness",
7007 "set level to %d\n", level);
7010 * Now we know what the final level should be, so we try to set it.
7011 * Doing it this way makes the syscall restartable in case of EINTR
7013 rc = brightness_set(level);
7014 if (!rc && ibm_backlight_device)
7015 backlight_force_update(ibm_backlight_device,
7016 BACKLIGHT_UPDATE_SYSFS);
7017 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7020 static struct ibm_struct brightness_driver_data = {
7021 .name = "brightness",
7022 .read = brightness_read,
7023 .write = brightness_write,
7024 .exit = brightness_exit,
7025 .suspend = brightness_suspend,
7026 .shutdown = brightness_shutdown,
7029 /*************************************************************************
7034 * IBM ThinkPads have a simple volume controller with MUTE gating.
7035 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7037 * Since the *61 series (and probably also the later *60 series), Lenovo
7038 * ThinkPads only implement the MUTE gate.
7041 * Bit 6: MUTE (1 mutes sound)
7043 * Other bits should be zero as far as we know.
7045 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7046 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7047 * such as bit 7 which is used to detect repeated presses of MUTE,
7048 * and we leave them unchanged.
7050 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7051 * in response to user input. Unfortunately, this rarely works well.
7052 * The laptop changes the state of its internal MUTE gate and, on some
7053 * models, sends KEY_MUTE, causing any user code that responds to the
7054 * mute button to get confused. The hardware MUTE gate is also
7055 * unnecessary, since user code can handle the mute button without
7056 * kernel or EC help.
7058 * To avoid confusing userspace, we simply disable all EC-based mute
7059 * and volume controls when possible.
7062 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7064 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7065 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7066 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7068 #if SNDRV_CARDS <= 32
7069 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7071 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7073 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7074 static char *alsa_id = "ThinkPadEC";
7075 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7077 struct tpacpi_alsa_data {
7078 struct snd_card *card;
7079 struct snd_ctl_elem_id *ctl_mute_id;
7080 struct snd_ctl_elem_id *ctl_vol_id;
7083 static struct snd_card *alsa_card;
7088 /* TP_EC_AUDIO bits */
7089 TP_EC_AUDIO_MUTESW = 6,
7091 /* TP_EC_AUDIO bitmasks */
7092 TP_EC_AUDIO_LVL_MSK = 0x0F,
7093 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7095 /* Maximum volume */
7096 TP_EC_VOLUME_MAX = 14,
7099 enum tpacpi_volume_access_mode {
7100 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7101 TPACPI_VOL_MODE_EC, /* Pure EC control */
7102 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7103 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7107 enum tpacpi_volume_capabilities {
7108 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7109 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7110 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7114 enum tpacpi_mute_btn_mode {
7115 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7116 /* We don't know what mode 1 is. */
7117 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7118 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7121 static enum tpacpi_volume_access_mode volume_mode =
7122 TPACPI_VOL_MODE_MAX;
7124 static enum tpacpi_volume_capabilities volume_capabilities;
7125 static bool volume_control_allowed;
7126 static bool software_mute_requested = true;
7127 static bool software_mute_active;
7128 static int software_mute_orig_mode;
7131 * Used to syncronize writers to TP_EC_AUDIO and
7132 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7134 static struct mutex volume_mutex;
7136 static void tpacpi_volume_checkpoint_nvram(void)
7142 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7144 if (!volume_control_allowed)
7146 if (software_mute_active)
7149 vdbg_printk(TPACPI_DBG_MIXER,
7150 "trying to checkpoint mixer state to NVRAM...\n");
7152 if (tp_features.mixer_no_level_control)
7153 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7155 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7157 if (mutex_lock_killable(&volume_mutex) < 0)
7160 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7163 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7165 if (lec != (b_nvram & ec_mask)) {
7166 /* NVRAM needs update */
7167 b_nvram &= ~ec_mask;
7169 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7170 dbg_printk(TPACPI_DBG_MIXER,
7171 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7172 (unsigned int) lec, (unsigned int) b_nvram);
7174 vdbg_printk(TPACPI_DBG_MIXER,
7175 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7176 (unsigned int) lec, (unsigned int) b_nvram);
7180 mutex_unlock(&volume_mutex);
7183 static int volume_get_status_ec(u8 *status)
7187 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7192 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7197 static int volume_get_status(u8 *status)
7199 return volume_get_status_ec(status);
7202 static int volume_set_status_ec(const u8 status)
7204 if (!acpi_ec_write(TP_EC_AUDIO, status))
7207 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7210 * On X200s, and possibly on others, it can take a while for
7211 * reads to become correct.
7218 static int volume_set_status(const u8 status)
7220 return volume_set_status_ec(status);
7223 /* returns < 0 on error, 0 on no change, 1 on change */
7224 static int __volume_set_mute_ec(const bool mute)
7229 if (mutex_lock_killable(&volume_mutex) < 0)
7232 rc = volume_get_status_ec(&s);
7236 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7237 s & ~TP_EC_AUDIO_MUTESW_MSK;
7240 rc = volume_set_status_ec(n);
7246 mutex_unlock(&volume_mutex);
7250 static int volume_alsa_set_mute(const bool mute)
7252 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7253 (mute) ? "" : "un");
7254 return __volume_set_mute_ec(mute);
7257 static int volume_set_mute(const bool mute)
7261 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7262 (mute) ? "" : "un");
7264 rc = __volume_set_mute_ec(mute);
7265 return (rc < 0) ? rc : 0;
7268 /* returns < 0 on error, 0 on no change, 1 on change */
7269 static int __volume_set_volume_ec(const u8 vol)
7274 if (vol > TP_EC_VOLUME_MAX)
7277 if (mutex_lock_killable(&volume_mutex) < 0)
7280 rc = volume_get_status_ec(&s);
7284 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7287 rc = volume_set_status_ec(n);
7293 mutex_unlock(&volume_mutex);
7297 static int volume_set_software_mute(bool startup)
7301 if (!tpacpi_is_lenovo())
7305 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7309 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7310 "Initial HAUM setting was %d\n",
7311 software_mute_orig_mode);
7314 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7315 (int)TP_EC_MUTE_BTN_NONE))
7318 if (result != TP_EC_MUTE_BTN_NONE)
7319 pr_warn("Unexpected SAUM result %d\n",
7323 * In software mute mode, the standard codec controls take
7324 * precendence, so we unmute the ThinkPad HW switch at
7325 * startup. Just on case there are SAUM-capable ThinkPads
7326 * with level controls, set max HW volume as well.
7328 if (tp_features.mixer_no_level_control)
7329 result = volume_set_mute(false);
7331 result = volume_set_status(TP_EC_VOLUME_MAX);
7334 pr_warn("Failed to unmute the HW mute switch\n");
7339 static void volume_exit_software_mute(void)
7343 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7344 || r != software_mute_orig_mode)
7345 pr_warn("Failed to restore mute mode\n");
7348 static int volume_alsa_set_volume(const u8 vol)
7350 dbg_printk(TPACPI_DBG_MIXER,
7351 "ALSA: trying to set volume level to %hu\n", vol);
7352 return __volume_set_volume_ec(vol);
7355 static void volume_alsa_notify_change(void)
7357 struct tpacpi_alsa_data *d;
7359 if (alsa_card && alsa_card->private_data) {
7360 d = alsa_card->private_data;
7362 snd_ctl_notify(alsa_card,
7363 SNDRV_CTL_EVENT_MASK_VALUE,
7366 snd_ctl_notify(alsa_card,
7367 SNDRV_CTL_EVENT_MASK_VALUE,
7372 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7373 struct snd_ctl_elem_info *uinfo)
7375 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7377 uinfo->value.integer.min = 0;
7378 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7382 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7383 struct snd_ctl_elem_value *ucontrol)
7388 rc = volume_get_status(&s);
7392 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7396 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7397 struct snd_ctl_elem_value *ucontrol)
7399 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7400 ucontrol->value.integer.value[0]);
7401 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7404 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7406 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7407 struct snd_ctl_elem_value *ucontrol)
7412 rc = volume_get_status(&s);
7416 ucontrol->value.integer.value[0] =
7417 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7421 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7422 struct snd_ctl_elem_value *ucontrol)
7424 tpacpi_disclose_usertask("ALSA", "%smute\n",
7425 ucontrol->value.integer.value[0] ?
7427 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7430 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7431 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7432 .name = "Console Playback Volume",
7434 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7435 .info = volume_alsa_vol_info,
7436 .get = volume_alsa_vol_get,
7439 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7441 .name = "Console Playback Switch",
7443 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7444 .info = volume_alsa_mute_info,
7445 .get = volume_alsa_mute_get,
7448 static void volume_suspend(void)
7450 tpacpi_volume_checkpoint_nvram();
7453 static void volume_resume(void)
7455 if (software_mute_active) {
7456 if (volume_set_software_mute(false) < 0)
7457 pr_warn("Failed to restore software mute\n");
7459 volume_alsa_notify_change();
7463 static void volume_shutdown(void)
7465 tpacpi_volume_checkpoint_nvram();
7468 static void volume_exit(void)
7471 snd_card_free(alsa_card);
7475 tpacpi_volume_checkpoint_nvram();
7477 if (software_mute_active)
7478 volume_exit_software_mute();
7481 static int __init volume_create_alsa_mixer(void)
7483 struct snd_card *card;
7484 struct tpacpi_alsa_data *data;
7485 struct snd_kcontrol *ctl_vol;
7486 struct snd_kcontrol *ctl_mute;
7489 rc = snd_card_new(&tpacpi_pdev->dev,
7490 alsa_index, alsa_id, THIS_MODULE,
7491 sizeof(struct tpacpi_alsa_data), &card);
7492 if (rc < 0 || !card) {
7493 pr_err("Failed to create ALSA card structures: %d\n", rc);
7497 BUG_ON(!card->private_data);
7498 data = card->private_data;
7501 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7502 sizeof(card->driver));
7503 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7504 sizeof(card->shortname));
7505 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7506 (thinkpad_id.ec_version_str) ?
7507 thinkpad_id.ec_version_str : "(unknown)");
7508 snprintf(card->longname, sizeof(card->longname),
7509 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7510 (thinkpad_id.ec_version_str) ?
7511 thinkpad_id.ec_version_str : "unknown");
7513 if (volume_control_allowed) {
7514 volume_alsa_control_vol.put = volume_alsa_vol_put;
7515 volume_alsa_control_vol.access =
7516 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7518 volume_alsa_control_mute.put = volume_alsa_mute_put;
7519 volume_alsa_control_mute.access =
7520 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7523 if (!tp_features.mixer_no_level_control) {
7524 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7525 rc = snd_ctl_add(card, ctl_vol);
7527 pr_err("Failed to create ALSA volume control: %d\n",
7531 data->ctl_vol_id = &ctl_vol->id;
7534 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7535 rc = snd_ctl_add(card, ctl_mute);
7537 pr_err("Failed to create ALSA mute control: %d\n", rc);
7540 data->ctl_mute_id = &ctl_mute->id;
7542 rc = snd_card_register(card);
7544 pr_err("Failed to register ALSA card: %d\n", rc);
7552 snd_card_free(card);
7556 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7557 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7559 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7560 /* Whitelist volume level on all IBM by default */
7561 { .vendor = PCI_VENDOR_ID_IBM,
7562 .bios = TPACPI_MATCH_ANY,
7563 .ec = TPACPI_MATCH_ANY,
7564 .quirks = TPACPI_VOL_Q_LEVEL },
7566 /* Lenovo models with volume control (needs confirmation) */
7567 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7568 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7569 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7570 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7571 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7572 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7573 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7575 /* Whitelist mute-only on all Lenovo by default */
7576 { .vendor = PCI_VENDOR_ID_LENOVO,
7577 .bios = TPACPI_MATCH_ANY,
7578 .ec = TPACPI_MATCH_ANY,
7579 .quirks = TPACPI_VOL_Q_MUTEONLY }
7582 static int __init volume_init(struct ibm_init_struct *iibm)
7584 unsigned long quirks;
7587 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7589 mutex_init(&volume_mutex);
7592 * Check for module parameter bogosity, note that we
7593 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7594 * able to detect "unspecified"
7596 if (volume_mode > TPACPI_VOL_MODE_MAX)
7599 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7600 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7605 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7609 * The ALSA mixer is our primary interface.
7610 * When disabled, don't install the subdriver at all
7613 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7614 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7618 quirks = tpacpi_check_quirks(volume_quirk_table,
7619 ARRAY_SIZE(volume_quirk_table));
7621 switch (volume_capabilities) {
7622 case TPACPI_VOL_CAP_AUTO:
7623 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7624 tp_features.mixer_no_level_control = 1;
7625 else if (quirks & TPACPI_VOL_Q_LEVEL)
7626 tp_features.mixer_no_level_control = 0;
7628 return -ENODEV; /* no mixer */
7630 case TPACPI_VOL_CAP_VOLMUTE:
7631 tp_features.mixer_no_level_control = 0;
7633 case TPACPI_VOL_CAP_MUTEONLY:
7634 tp_features.mixer_no_level_control = 1;
7640 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7641 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7642 "using user-supplied volume_capabilities=%d\n",
7643 volume_capabilities);
7645 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7646 volume_mode == TPACPI_VOL_MODE_MAX) {
7647 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7649 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7650 "driver auto-selected volume_mode=%d\n",
7653 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7654 "using user-supplied volume_mode=%d\n",
7658 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7659 "mute is supported, volume control is %s\n",
7660 str_supported(!tp_features.mixer_no_level_control));
7662 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7663 software_mute_active = true;
7665 rc = volume_create_alsa_mixer();
7667 pr_err("Could not create the ALSA mixer interface\n");
7671 pr_info("Console audio control enabled, mode: %s\n",
7672 (volume_control_allowed) ?
7673 "override (read/write)" :
7674 "monitor (read only)");
7677 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7678 "registering volume hotkeys as change notification\n");
7679 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7680 | TP_ACPI_HKEY_VOLUP_MASK
7681 | TP_ACPI_HKEY_VOLDWN_MASK
7682 | TP_ACPI_HKEY_MUTE_MASK);
7687 static int volume_read(struct seq_file *m)
7691 if (volume_get_status(&status) < 0) {
7692 seq_printf(m, "level:\t\tunreadable\n");
7694 if (tp_features.mixer_no_level_control)
7695 seq_printf(m, "level:\t\tunsupported\n");
7697 seq_printf(m, "level:\t\t%d\n",
7698 status & TP_EC_AUDIO_LVL_MSK);
7700 seq_printf(m, "mute:\t\t%s\n",
7701 onoff(status, TP_EC_AUDIO_MUTESW));
7703 if (volume_control_allowed) {
7704 seq_printf(m, "commands:\tunmute, mute\n");
7705 if (!tp_features.mixer_no_level_control) {
7706 seq_printf(m, "commands:\tup, down\n");
7707 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7716 static int volume_write(char *buf)
7719 u8 new_level, new_mute;
7725 * We do allow volume control at driver startup, so that the
7726 * user can set initial state through the volume=... parameter hack.
7728 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7729 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7730 tp_warned.volume_ctrl_forbidden = 1;
7731 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7732 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7737 rc = volume_get_status(&s);
7741 new_level = s & TP_EC_AUDIO_LVL_MSK;
7742 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7744 while ((cmd = strsep(&buf, ","))) {
7745 if (!tp_features.mixer_no_level_control) {
7746 if (strlencmp(cmd, "up") == 0) {
7749 else if (new_level < TP_EC_VOLUME_MAX)
7752 } else if (strlencmp(cmd, "down") == 0) {
7755 else if (new_level > 0)
7758 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7759 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7764 if (strlencmp(cmd, "mute") == 0)
7765 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7766 else if (strlencmp(cmd, "unmute") == 0)
7772 if (tp_features.mixer_no_level_control) {
7773 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7774 new_mute ? "" : "un");
7775 rc = volume_set_mute(!!new_mute);
7777 tpacpi_disclose_usertask("procfs volume",
7778 "%smute and set level to %d\n",
7779 new_mute ? "" : "un", new_level);
7780 rc = volume_set_status(new_mute | new_level);
7782 volume_alsa_notify_change();
7784 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7787 static struct ibm_struct volume_driver_data = {
7789 .read = volume_read,
7790 .write = volume_write,
7791 .exit = volume_exit,
7792 .suspend = volume_suspend,
7793 .resume = volume_resume,
7794 .shutdown = volume_shutdown,
7797 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7799 #define alsa_card NULL
7801 static inline void volume_alsa_notify_change(void)
7805 static int __init volume_init(struct ibm_init_struct *iibm)
7807 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7812 static struct ibm_struct volume_driver_data = {
7816 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7818 /*************************************************************************
7825 * TPACPI_FAN_RD_ACPI_GFAN:
7826 * ACPI GFAN method: returns fan level
7828 * see TPACPI_FAN_WR_ACPI_SFAN
7829 * EC 0x2f (HFSP) not available if GFAN exists
7831 * TPACPI_FAN_WR_ACPI_SFAN:
7832 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7834 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7837 * TPACPI_FAN_WR_TPEC:
7838 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7839 * Supported on almost all ThinkPads
7841 * Fan speed changes of any sort (including those caused by the
7842 * disengaged mode) are usually done slowly by the firmware as the
7843 * maximum amount of fan duty cycle change per second seems to be
7846 * Reading is not available if GFAN exists.
7847 * Writing is not available if SFAN exists.
7850 * 7 automatic mode engaged;
7851 * (default operation mode of the ThinkPad)
7852 * fan level is ignored in this mode.
7853 * 6 full speed mode (takes precedence over bit 7);
7854 * not available on all thinkpads. May disable
7855 * the tachometer while the fan controller ramps up
7856 * the speed (which can take up to a few *minutes*).
7857 * Speeds up fan to 100% duty-cycle, which is far above
7858 * the standard RPM levels. It is not impossible that
7859 * it could cause hardware damage.
7860 * 5-3 unused in some models. Extra bits for fan level
7861 * in others, but still useless as all values above
7862 * 7 map to the same speed as level 7 in these models.
7863 * 2-0 fan level (0..7 usually)
7865 * 0x07 = max (set when temperatures critical)
7866 * Some ThinkPads may have other levels, see
7867 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7869 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7870 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7871 * does so, its initial value is meaningless (0x07).
7873 * For firmware bugs, refer to:
7874 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7878 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7879 * Main fan tachometer reading (in RPM)
7881 * This register is present on all ThinkPads with a new-style EC, and
7882 * it is known not to be present on the A21m/e, and T22, as there is
7883 * something else in offset 0x84 according to the ACPI DSDT. Other
7884 * ThinkPads from this same time period (and earlier) probably lack the
7885 * tachometer as well.
7887 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7888 * was never fixed by IBM to report the EC firmware version string
7889 * probably support the tachometer (like the early X models), so
7890 * detecting it is quite hard. We need more data to know for sure.
7892 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7895 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7898 * For firmware bugs, refer to:
7899 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7903 * ThinkPad EC register 0x31 bit 0 (only on select models)
7905 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7906 * show the speed of the main fan. When bit 0 of EC register 0x31
7907 * is one, the tachometer registers show the speed of the auxiliary
7910 * Fan control seems to affect both fans, regardless of the state
7913 * So far, only the firmware for the X60/X61 non-tablet versions
7914 * seem to support this (firmware TP-7M).
7916 * TPACPI_FAN_WR_ACPI_FANS:
7917 * ThinkPad X31, X40, X41. Not available in the X60.
7919 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7920 * high speed. ACPI DSDT seems to map these three speeds to levels
7921 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7922 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7924 * The speeds are stored on handles
7925 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7927 * There are three default speed sets, accessible as handles:
7928 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7930 * ACPI DSDT switches which set is in use depending on various
7933 * TPACPI_FAN_WR_TPEC is also available and should be used to
7934 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7935 * but the ACPI tables just mention level 7.
7938 enum { /* Fan control constants */
7939 fan_status_offset = 0x2f, /* EC register 0x2f */
7940 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7941 * 0x84 must be read before 0x85 */
7942 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7943 bit 0 selects which fan is active */
7945 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7946 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7948 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7951 enum fan_status_access_mode {
7952 TPACPI_FAN_NONE = 0, /* No fan status or control */
7953 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7954 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7957 enum fan_control_access_mode {
7958 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7959 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7960 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7961 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7964 enum fan_control_commands {
7965 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7966 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7967 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7968 * and also watchdog cmd */
7971 static bool fan_control_allowed;
7973 static enum fan_status_access_mode fan_status_access_mode;
7974 static enum fan_control_access_mode fan_control_access_mode;
7975 static enum fan_control_commands fan_control_commands;
7977 static u8 fan_control_initial_status;
7978 static u8 fan_control_desired_level;
7979 static u8 fan_control_resume_level;
7980 static int fan_watchdog_maxinterval;
7982 static struct mutex fan_mutex;
7984 static void fan_watchdog_fire(struct work_struct *ignored);
7985 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7987 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7988 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7989 "\\FSPD", /* 600e/x, 770e, 770x */
7991 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7992 "JFNS", /* 770x-JL */
7996 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7997 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7998 * be in auto mode (0x80).
8000 * This is corrected by any write to HFSP either by the driver, or
8003 * We assume 0x07 really means auto mode while this quirk is active,
8004 * as this is far more likely than the ThinkPad being in level 7,
8005 * which is only used by the firmware during thermal emergencies.
8007 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8008 * TP-70 (T43, R52), which are known to be buggy.
8011 static void fan_quirk1_setup(void)
8013 if (fan_control_initial_status == 0x07) {
8014 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8015 tp_features.fan_ctrl_status_undef = 1;
8019 static void fan_quirk1_handle(u8 *fan_status)
8021 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8022 if (*fan_status != fan_control_initial_status) {
8023 /* something changed the HFSP regisnter since
8024 * driver init time, so it is not undefined
8026 tp_features.fan_ctrl_status_undef = 0;
8028 /* Return most likely status. In fact, it
8029 * might be the only possible status */
8030 *fan_status = TP_EC_FAN_AUTO;
8035 /* Select main fan on X60/X61, NOOP on others */
8036 static bool fan_select_fan1(void)
8038 if (tp_features.second_fan) {
8041 if (ec_read(fan_select_offset, &val) < 0)
8044 if (ec_write(fan_select_offset, val) < 0)
8050 /* Select secondary fan on X60/X61 */
8051 static bool fan_select_fan2(void)
8055 if (!tp_features.second_fan)
8058 if (ec_read(fan_select_offset, &val) < 0)
8061 if (ec_write(fan_select_offset, val) < 0)
8068 * Call with fan_mutex held
8070 static void fan_update_desired_level(u8 status)
8073 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8075 fan_control_desired_level = 7;
8077 fan_control_desired_level = status;
8081 static int fan_get_status(u8 *status)
8086 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8088 switch (fan_status_access_mode) {
8089 case TPACPI_FAN_RD_ACPI_GFAN: {
8090 /* 570, 600e/x, 770e, 770x */
8093 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8097 *status = res & 0x07;
8101 case TPACPI_FAN_RD_TPEC:
8102 /* all except 570, 600e/x, 770e, 770x */
8103 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8106 if (likely(status)) {
8108 fan_quirk1_handle(status);
8120 static int fan_get_status_safe(u8 *status)
8125 if (mutex_lock_killable(&fan_mutex))
8126 return -ERESTARTSYS;
8127 rc = fan_get_status(&s);
8129 fan_update_desired_level(s);
8130 mutex_unlock(&fan_mutex);
8140 static int fan_get_speed(unsigned int *speed)
8144 switch (fan_status_access_mode) {
8145 case TPACPI_FAN_RD_TPEC:
8146 /* all except 570, 600e/x, 770e, 770x */
8147 if (unlikely(!fan_select_fan1()))
8149 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8150 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8154 *speed = (hi << 8) | lo;
8165 static int fan2_get_speed(unsigned int *speed)
8170 switch (fan_status_access_mode) {
8171 case TPACPI_FAN_RD_TPEC:
8172 /* all except 570, 600e/x, 770e, 770x */
8173 if (unlikely(!fan_select_fan2()))
8175 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8176 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8177 fan_select_fan1(); /* play it safe */
8182 *speed = (hi << 8) | lo;
8193 static int fan_set_level(int level)
8195 if (!fan_control_allowed)
8198 switch (fan_control_access_mode) {
8199 case TPACPI_FAN_WR_ACPI_SFAN:
8200 if ((level < 0) || (level > 7))
8203 if (tp_features.second_fan_ctl) {
8204 if (!fan_select_fan2() ||
8205 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8206 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8207 tp_features.second_fan_ctl = 0;
8211 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8215 case TPACPI_FAN_WR_ACPI_FANS:
8216 case TPACPI_FAN_WR_TPEC:
8217 if (!(level & TP_EC_FAN_AUTO) &&
8218 !(level & TP_EC_FAN_FULLSPEED) &&
8219 ((level < 0) || (level > 7)))
8222 /* safety net should the EC not support AUTO
8223 * or FULLSPEED mode bits and just ignore them */
8224 if (level & TP_EC_FAN_FULLSPEED)
8225 level |= 7; /* safety min speed 7 */
8226 else if (level & TP_EC_FAN_AUTO)
8227 level |= 4; /* safety min speed 4 */
8229 if (tp_features.second_fan_ctl) {
8230 if (!fan_select_fan2() ||
8231 !acpi_ec_write(fan_status_offset, level)) {
8232 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8233 tp_features.second_fan_ctl = 0;
8238 if (!acpi_ec_write(fan_status_offset, level))
8241 tp_features.fan_ctrl_status_undef = 0;
8248 vdbg_printk(TPACPI_DBG_FAN,
8249 "fan control: set fan control register to 0x%02x\n", level);
8253 static int fan_set_level_safe(int level)
8257 if (!fan_control_allowed)
8260 if (mutex_lock_killable(&fan_mutex))
8261 return -ERESTARTSYS;
8263 if (level == TPACPI_FAN_LAST_LEVEL)
8264 level = fan_control_desired_level;
8266 rc = fan_set_level(level);
8268 fan_update_desired_level(level);
8270 mutex_unlock(&fan_mutex);
8274 static int fan_set_enable(void)
8279 if (!fan_control_allowed)
8282 if (mutex_lock_killable(&fan_mutex))
8283 return -ERESTARTSYS;
8285 switch (fan_control_access_mode) {
8286 case TPACPI_FAN_WR_ACPI_FANS:
8287 case TPACPI_FAN_WR_TPEC:
8288 rc = fan_get_status(&s);
8292 /* Don't go out of emergency fan mode */
8295 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8298 if (!acpi_ec_write(fan_status_offset, s))
8301 tp_features.fan_ctrl_status_undef = 0;
8306 case TPACPI_FAN_WR_ACPI_SFAN:
8307 rc = fan_get_status(&s);
8313 /* Set fan to at least level 4 */
8316 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8326 mutex_unlock(&fan_mutex);
8329 vdbg_printk(TPACPI_DBG_FAN,
8330 "fan control: set fan control register to 0x%02x\n",
8335 static int fan_set_disable(void)
8339 if (!fan_control_allowed)
8342 if (mutex_lock_killable(&fan_mutex))
8343 return -ERESTARTSYS;
8346 switch (fan_control_access_mode) {
8347 case TPACPI_FAN_WR_ACPI_FANS:
8348 case TPACPI_FAN_WR_TPEC:
8349 if (!acpi_ec_write(fan_status_offset, 0x00))
8352 fan_control_desired_level = 0;
8353 tp_features.fan_ctrl_status_undef = 0;
8357 case TPACPI_FAN_WR_ACPI_SFAN:
8358 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8361 fan_control_desired_level = 0;
8369 vdbg_printk(TPACPI_DBG_FAN,
8370 "fan control: set fan control register to 0\n");
8372 mutex_unlock(&fan_mutex);
8376 static int fan_set_speed(int speed)
8380 if (!fan_control_allowed)
8383 if (mutex_lock_killable(&fan_mutex))
8384 return -ERESTARTSYS;
8387 switch (fan_control_access_mode) {
8388 case TPACPI_FAN_WR_ACPI_FANS:
8389 if (speed >= 0 && speed <= 65535) {
8390 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8391 speed, speed, speed))
8401 mutex_unlock(&fan_mutex);
8405 static void fan_watchdog_reset(void)
8407 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8410 if (fan_watchdog_maxinterval > 0 &&
8411 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8412 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8413 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8415 cancel_delayed_work(&fan_watchdog_task);
8418 static void fan_watchdog_fire(struct work_struct *ignored)
8422 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8425 pr_notice("fan watchdog: enabling fan\n");
8426 rc = fan_set_enable();
8428 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8430 /* reschedule for later */
8431 fan_watchdog_reset();
8436 * SYSFS fan layout: hwmon compatible (device)
8439 * 0: "disengaged" mode
8441 * 2: native EC "auto" mode (recommended, hardware default)
8443 * pwm*: set speed in manual mode, ignored otherwise.
8444 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8447 * fan*_input: tachometer reading, RPM
8450 * SYSFS fan layout: extensions
8452 * fan_watchdog (driver):
8453 * fan watchdog interval in seconds, 0 disables (default), max 120
8456 /* sysfs fan pwm1_enable ----------------------------------------------- */
8457 static ssize_t fan_pwm1_enable_show(struct device *dev,
8458 struct device_attribute *attr,
8464 res = fan_get_status_safe(&status);
8468 if (status & TP_EC_FAN_FULLSPEED) {
8470 } else if (status & TP_EC_FAN_AUTO) {
8475 return sysfs_emit(buf, "%d\n", mode);
8478 static ssize_t fan_pwm1_enable_store(struct device *dev,
8479 struct device_attribute *attr,
8480 const char *buf, size_t count)
8485 if (parse_strtoul(buf, 2, &t))
8488 tpacpi_disclose_usertask("hwmon pwm1_enable",
8489 "set fan mode to %lu\n", t);
8493 level = TP_EC_FAN_FULLSPEED;
8496 level = TPACPI_FAN_LAST_LEVEL;
8499 level = TP_EC_FAN_AUTO;
8502 /* reserved for software-controlled auto mode */
8508 res = fan_set_level_safe(level);
8514 fan_watchdog_reset();
8519 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8520 fan_pwm1_enable_show, fan_pwm1_enable_store);
8522 /* sysfs fan pwm1 ------------------------------------------------------ */
8523 static ssize_t fan_pwm1_show(struct device *dev,
8524 struct device_attribute *attr,
8530 res = fan_get_status_safe(&status);
8535 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8536 status = fan_control_desired_level;
8541 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
8544 static ssize_t fan_pwm1_store(struct device *dev,
8545 struct device_attribute *attr,
8546 const char *buf, size_t count)
8550 u8 status, newlevel;
8552 if (parse_strtoul(buf, 255, &s))
8555 tpacpi_disclose_usertask("hwmon pwm1",
8556 "set fan speed to %lu\n", s);
8558 /* scale down from 0-255 to 0-7 */
8559 newlevel = (s >> 5) & 0x07;
8561 if (mutex_lock_killable(&fan_mutex))
8562 return -ERESTARTSYS;
8564 rc = fan_get_status(&status);
8565 if (!rc && (status &
8566 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8567 rc = fan_set_level(newlevel);
8571 fan_update_desired_level(newlevel);
8572 fan_watchdog_reset();
8576 mutex_unlock(&fan_mutex);
8577 return (rc) ? rc : count;
8580 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8582 /* sysfs fan fan1_input ------------------------------------------------ */
8583 static ssize_t fan_fan1_input_show(struct device *dev,
8584 struct device_attribute *attr,
8590 res = fan_get_speed(&speed);
8594 return sysfs_emit(buf, "%u\n", speed);
8597 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8599 /* sysfs fan fan2_input ------------------------------------------------ */
8600 static ssize_t fan_fan2_input_show(struct device *dev,
8601 struct device_attribute *attr,
8607 res = fan2_get_speed(&speed);
8611 return sysfs_emit(buf, "%u\n", speed);
8614 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8616 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8617 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8619 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
8622 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8627 if (parse_strtoul(buf, 120, &t))
8630 if (!fan_control_allowed)
8633 fan_watchdog_maxinterval = t;
8634 fan_watchdog_reset();
8636 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8640 static DRIVER_ATTR_RW(fan_watchdog);
8642 /* --------------------------------------------------------------------- */
8644 static struct attribute *fan_attributes[] = {
8645 &dev_attr_pwm1_enable.attr,
8646 &dev_attr_pwm1.attr,
8647 &dev_attr_fan1_input.attr,
8648 &dev_attr_fan2_input.attr,
8652 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8655 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8656 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8659 if (attr == &dev_attr_fan2_input.attr) {
8660 if (!tp_features.second_fan)
8667 static const struct attribute_group fan_attr_group = {
8668 .is_visible = fan_attr_is_visible,
8669 .attrs = fan_attributes,
8672 static struct attribute *fan_driver_attributes[] = {
8673 &driver_attr_fan_watchdog.attr,
8677 static const struct attribute_group fan_driver_attr_group = {
8678 .is_visible = fan_attr_is_visible,
8679 .attrs = fan_driver_attributes,
8682 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
8683 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8684 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8685 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
8687 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8688 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8689 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8690 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8691 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8692 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8693 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8694 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8695 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8696 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8697 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8698 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8699 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8700 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8701 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8702 TPACPI_Q_LNV3('N', '2', 'V', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (3nd gen) */
8703 TPACPI_Q_LNV3('N', '4', '0', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (4nd gen) */
8704 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8705 TPACPI_Q_LNV3('N', '3', '2', TPACPI_FAN_2CTL), /* X1 Carbon (9th gen) */
8706 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
8707 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
8710 static int __init fan_init(struct ibm_init_struct *iibm)
8712 unsigned long quirks;
8714 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8715 "initializing fan subdriver\n");
8717 mutex_init(&fan_mutex);
8718 fan_status_access_mode = TPACPI_FAN_NONE;
8719 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8720 fan_control_commands = 0;
8721 fan_watchdog_maxinterval = 0;
8722 tp_features.fan_ctrl_status_undef = 0;
8723 tp_features.second_fan = 0;
8724 tp_features.second_fan_ctl = 0;
8725 fan_control_desired_level = 7;
8727 if (tpacpi_is_ibm()) {
8728 TPACPI_ACPIHANDLE_INIT(fans);
8729 TPACPI_ACPIHANDLE_INIT(gfan);
8730 TPACPI_ACPIHANDLE_INIT(sfan);
8733 quirks = tpacpi_check_quirks(fan_quirk_table,
8734 ARRAY_SIZE(fan_quirk_table));
8736 if (quirks & TPACPI_FAN_NOFAN) {
8737 pr_info("No integrated ThinkPad fan available\n");
8742 /* 570, 600e/x, 770e, 770x */
8743 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8745 /* all other ThinkPads: note that even old-style
8746 * ThinkPad ECs supports the fan control register */
8747 if (likely(acpi_ec_read(fan_status_offset,
8748 &fan_control_initial_status))) {
8749 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8750 if (quirks & TPACPI_FAN_Q1)
8752 if (quirks & TPACPI_FAN_2FAN) {
8753 tp_features.second_fan = 1;
8754 pr_info("secondary fan support enabled\n");
8756 if (quirks & TPACPI_FAN_2CTL) {
8757 tp_features.second_fan = 1;
8758 tp_features.second_fan_ctl = 1;
8759 pr_info("secondary fan control enabled\n");
8762 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8769 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8770 fan_control_commands |=
8771 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8774 /* gfan without sfan means no fan control */
8775 /* all other models implement TP EC 0x2f control */
8779 fan_control_access_mode =
8780 TPACPI_FAN_WR_ACPI_FANS;
8781 fan_control_commands |=
8782 TPACPI_FAN_CMD_SPEED |
8783 TPACPI_FAN_CMD_LEVEL |
8784 TPACPI_FAN_CMD_ENABLE;
8786 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8787 fan_control_commands |=
8788 TPACPI_FAN_CMD_LEVEL |
8789 TPACPI_FAN_CMD_ENABLE;
8794 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8795 "fan is %s, modes %d, %d\n",
8796 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8797 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8798 fan_status_access_mode, fan_control_access_mode);
8800 /* fan control master switch */
8801 if (!fan_control_allowed) {
8802 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8803 fan_control_commands = 0;
8804 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8805 "fan control features disabled by parameter\n");
8808 /* update fan_control_desired_level */
8809 if (fan_status_access_mode != TPACPI_FAN_NONE)
8810 fan_get_status_safe(NULL);
8812 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8813 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8819 static void fan_exit(void)
8821 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8822 "cancelling any pending fan watchdog tasks\n");
8824 cancel_delayed_work(&fan_watchdog_task);
8825 flush_workqueue(tpacpi_wq);
8828 static void fan_suspend(void)
8832 if (!fan_control_allowed)
8835 /* Store fan status in cache */
8836 fan_control_resume_level = 0;
8837 rc = fan_get_status_safe(&fan_control_resume_level);
8839 pr_notice("failed to read fan level for later restore during resume: %d\n",
8842 /* if it is undefined, don't attempt to restore it.
8844 if (tp_features.fan_ctrl_status_undef)
8845 fan_control_resume_level = 0;
8848 static void fan_resume(void)
8850 u8 current_level = 7;
8851 bool do_set = false;
8854 /* DSDT *always* updates status on resume */
8855 tp_features.fan_ctrl_status_undef = 0;
8857 if (!fan_control_allowed ||
8858 !fan_control_resume_level ||
8859 (fan_get_status_safe(¤t_level) < 0))
8862 switch (fan_control_access_mode) {
8863 case TPACPI_FAN_WR_ACPI_SFAN:
8864 /* never decrease fan level */
8865 do_set = (fan_control_resume_level > current_level);
8867 case TPACPI_FAN_WR_ACPI_FANS:
8868 case TPACPI_FAN_WR_TPEC:
8869 /* never decrease fan level, scale is:
8870 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8872 * We expect the firmware to set either 7 or AUTO, but we
8873 * handle FULLSPEED out of paranoia.
8875 * So, we can safely only restore FULLSPEED or 7, anything
8876 * else could slow the fan. Restoring AUTO is useless, at
8877 * best that's exactly what the DSDT already set (it is the
8880 * Always keep in mind that the DSDT *will* have set the
8881 * fans to what the vendor supposes is the best level. We
8882 * muck with it only to speed the fan up.
8884 if (fan_control_resume_level != 7 &&
8885 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8888 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8889 (current_level != fan_control_resume_level);
8895 pr_notice("restoring fan level to 0x%02x\n",
8896 fan_control_resume_level);
8897 rc = fan_set_level_safe(fan_control_resume_level);
8899 pr_notice("failed to restore fan level: %d\n", rc);
8903 static int fan_read(struct seq_file *m)
8907 unsigned int speed = 0;
8909 switch (fan_status_access_mode) {
8910 case TPACPI_FAN_RD_ACPI_GFAN:
8911 /* 570, 600e/x, 770e, 770x */
8912 rc = fan_get_status_safe(&status);
8916 seq_printf(m, "status:\t\t%s\n"
8918 (status != 0) ? "enabled" : "disabled", status);
8921 case TPACPI_FAN_RD_TPEC:
8922 /* all except 570, 600e/x, 770e, 770x */
8923 rc = fan_get_status_safe(&status);
8927 seq_printf(m, "status:\t\t%s\n",
8928 (status != 0) ? "enabled" : "disabled");
8930 rc = fan_get_speed(&speed);
8934 seq_printf(m, "speed:\t\t%d\n", speed);
8936 if (status & TP_EC_FAN_FULLSPEED)
8937 /* Disengaged mode takes precedence */
8938 seq_printf(m, "level:\t\tdisengaged\n");
8939 else if (status & TP_EC_FAN_AUTO)
8940 seq_printf(m, "level:\t\tauto\n");
8942 seq_printf(m, "level:\t\t%d\n", status);
8945 case TPACPI_FAN_NONE:
8947 seq_printf(m, "status:\t\tnot supported\n");
8950 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8951 seq_printf(m, "commands:\tlevel <level>");
8953 switch (fan_control_access_mode) {
8954 case TPACPI_FAN_WR_ACPI_SFAN:
8955 seq_printf(m, " (<level> is 0-7)\n");
8959 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8964 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8965 seq_printf(m, "commands:\tenable, disable\n"
8966 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8968 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8969 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8974 static int fan_write_cmd_level(const char *cmd, int *rc)
8978 if (strlencmp(cmd, "level auto") == 0)
8979 level = TP_EC_FAN_AUTO;
8980 else if ((strlencmp(cmd, "level disengaged") == 0) ||
8981 (strlencmp(cmd, "level full-speed") == 0))
8982 level = TP_EC_FAN_FULLSPEED;
8983 else if (sscanf(cmd, "level %d", &level) != 1)
8986 *rc = fan_set_level_safe(level);
8988 pr_err("level command accepted for unsupported access mode %d\n",
8989 fan_control_access_mode);
8991 tpacpi_disclose_usertask("procfs fan",
8992 "set level to %d\n", level);
8997 static int fan_write_cmd_enable(const char *cmd, int *rc)
8999 if (strlencmp(cmd, "enable") != 0)
9002 *rc = fan_set_enable();
9004 pr_err("enable command accepted for unsupported access mode %d\n",
9005 fan_control_access_mode);
9007 tpacpi_disclose_usertask("procfs fan", "enable\n");
9012 static int fan_write_cmd_disable(const char *cmd, int *rc)
9014 if (strlencmp(cmd, "disable") != 0)
9017 *rc = fan_set_disable();
9019 pr_err("disable command accepted for unsupported access mode %d\n",
9020 fan_control_access_mode);
9022 tpacpi_disclose_usertask("procfs fan", "disable\n");
9027 static int fan_write_cmd_speed(const char *cmd, int *rc)
9032 * Support speed <low> <medium> <high> ? */
9034 if (sscanf(cmd, "speed %d", &speed) != 1)
9037 *rc = fan_set_speed(speed);
9039 pr_err("speed command accepted for unsupported access mode %d\n",
9040 fan_control_access_mode);
9042 tpacpi_disclose_usertask("procfs fan",
9043 "set speed to %d\n", speed);
9048 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9052 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9055 if (interval < 0 || interval > 120)
9058 fan_watchdog_maxinterval = interval;
9059 tpacpi_disclose_usertask("procfs fan",
9060 "set watchdog timer to %d\n",
9067 static int fan_write(char *buf)
9072 while (!rc && (cmd = strsep(&buf, ","))) {
9073 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9074 fan_write_cmd_level(cmd, &rc)) &&
9075 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9076 (fan_write_cmd_enable(cmd, &rc) ||
9077 fan_write_cmd_disable(cmd, &rc) ||
9078 fan_write_cmd_watchdog(cmd, &rc))) &&
9079 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9080 fan_write_cmd_speed(cmd, &rc))
9084 fan_watchdog_reset();
9090 static struct ibm_struct fan_driver_data = {
9095 .suspend = fan_suspend,
9096 .resume = fan_resume,
9099 /*************************************************************************
9100 * Mute LED subdriver
9103 #define TPACPI_LED_MAX 2
9105 struct tp_led_table {
9112 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9113 [LED_AUDIO_MUTE] = {
9118 [LED_AUDIO_MICMUTE] = {
9125 static int mute_led_on_off(struct tp_led_table *t, bool state)
9130 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9131 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9135 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9136 state ? t->on_value : t->off_value))
9143 static int tpacpi_led_set(int whichled, bool on)
9145 struct tp_led_table *t;
9147 t = &led_tables[whichled];
9148 if (t->state < 0 || t->state == on)
9150 return mute_led_on_off(t, on);
9153 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9154 enum led_brightness brightness)
9156 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9159 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9160 enum led_brightness brightness)
9162 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9165 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9166 [LED_AUDIO_MUTE] = {
9167 .name = "platform::mute",
9168 .max_brightness = 1,
9169 .brightness_set_blocking = tpacpi_led_mute_set,
9170 .default_trigger = "audio-mute",
9172 [LED_AUDIO_MICMUTE] = {
9173 .name = "platform::micmute",
9174 .max_brightness = 1,
9175 .brightness_set_blocking = tpacpi_led_micmute_set,
9176 .default_trigger = "audio-micmute",
9180 static int mute_led_init(struct ibm_init_struct *iibm)
9185 for (i = 0; i < TPACPI_LED_MAX; i++) {
9186 struct tp_led_table *t = &led_tables[i];
9187 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9192 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9193 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9196 led_classdev_unregister(&mute_led_cdev[i]);
9203 static void mute_led_exit(void)
9207 for (i = 0; i < TPACPI_LED_MAX; i++) {
9208 led_classdev_unregister(&mute_led_cdev[i]);
9209 tpacpi_led_set(i, false);
9213 static void mute_led_resume(void)
9217 for (i = 0; i < TPACPI_LED_MAX; i++) {
9218 struct tp_led_table *t = &led_tables[i];
9220 mute_led_on_off(t, t->state);
9224 static struct ibm_struct mute_led_driver_data = {
9226 .exit = mute_led_exit,
9227 .resume = mute_led_resume,
9231 * Battery Wear Control Driver
9232 * Contact: Ognjen Galic <smclt30p@gmail.com>
9237 #define GET_START "BCTG"
9238 #define SET_START "BCCS"
9239 #define GET_STOP "BCSG"
9240 #define SET_STOP "BCSS"
9241 #define GET_DISCHARGE "BDSG"
9242 #define SET_DISCHARGE "BDSS"
9243 #define GET_INHIBIT "BICG"
9244 #define SET_INHIBIT "BICS"
9253 /* Error condition bit */
9254 METHOD_ERR = BIT(31),
9258 /* This is used in the get/set helpers */
9265 struct tpacpi_battery_data {
9270 unsigned int charge_behaviours;
9273 struct tpacpi_battery_driver_data {
9274 struct tpacpi_battery_data batteries[3];
9275 int individual_addressing;
9278 static struct tpacpi_battery_driver_data battery_info;
9280 /* ACPI helpers/functions/probes */
9283 * This evaluates a ACPI method call specific to the battery
9284 * ACPI extension. The specifics are that an error is marked
9285 * in the 32rd bit of the response, so we just check that here.
9287 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9291 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9292 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9295 if (response & METHOD_ERR) {
9296 acpi_handle_err(hkey_handle,
9297 "%s evaluated but flagged as error", method);
9304 static int tpacpi_battery_get(int what, int battery, int *ret)
9307 case THRESHOLD_START:
9308 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9311 /* The value is in the low 8 bits of the response */
9314 case THRESHOLD_STOP:
9315 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9317 /* Value is in lower 8 bits */
9320 * On the stop value, if we return 0 that
9321 * does not make any sense. 0 means Default, which
9322 * means that charging stops at 100%, so we return
9328 case FORCE_DISCHARGE:
9329 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9331 /* The force discharge status is in bit 0 */
9334 case INHIBIT_CHARGE:
9335 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9337 /* The inhibit charge status is in bit 0 */
9341 pr_crit("wrong parameter: %d", what);
9346 static int tpacpi_battery_set(int what, int battery, int value)
9349 /* The first 8 bits are the value of the threshold */
9351 /* The battery ID is in bits 8-9, 2 bits */
9352 param |= battery << 8;
9355 case THRESHOLD_START:
9356 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9357 pr_err("failed to set charge threshold on battery %d",
9362 case THRESHOLD_STOP:
9363 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9364 pr_err("failed to set stop threshold: %d", battery);
9368 case FORCE_DISCHARGE:
9369 /* Force discharge is in bit 0,
9370 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9371 * battery ID is in bits 8-9, 2 bits.
9373 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9374 pr_err("failed to set force discharge on %d", battery);
9378 case INHIBIT_CHARGE:
9379 /* When setting inhibit charge, we set a default value of
9380 * always breaking on AC detach and the effective time is set to
9382 * The battery ID is in bits 4-5, 2 bits,
9383 * the effective time is in bits 8-23, 2 bytes.
9384 * A time of FFFF indicates forever.
9387 param |= battery << 4;
9388 param |= 0xFFFF << 8;
9389 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9390 pr_err("failed to set inhibit charge on %d", battery);
9395 pr_crit("wrong parameter: %d", what);
9400 static int tpacpi_battery_set_validate(int what, int battery, int value)
9404 ret = tpacpi_battery_set(what, battery, value);
9408 ret = tpacpi_battery_get(what, battery, &v);
9417 ret = tpacpi_battery_get(what, battery, &v);
9427 static int tpacpi_battery_probe(int battery)
9431 memset(&battery_info.batteries[battery], 0,
9432 sizeof(battery_info.batteries[battery]));
9435 * 1) Get the current start threshold
9436 * 2) Check for support
9437 * 3) Get the current stop threshold
9438 * 4) Check for support
9439 * 5) Get the current force discharge status
9440 * 6) Check for support
9441 * 7) Get the current inhibit charge status
9442 * 8) Check for support
9444 if (acpi_has_method(hkey_handle, GET_START)) {
9445 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9446 pr_err("Error probing battery %d\n", battery);
9449 /* Individual addressing is in bit 9 */
9451 battery_info.individual_addressing = true;
9452 /* Support is marked in bit 8 */
9454 battery_info.batteries[battery].start_support = 1;
9457 if (tpacpi_battery_get(THRESHOLD_START, battery,
9458 &battery_info.batteries[battery].charge_start)) {
9459 pr_err("Error probing battery %d\n", battery);
9463 if (acpi_has_method(hkey_handle, GET_STOP)) {
9464 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9465 pr_err("Error probing battery stop; %d\n", battery);
9468 /* Support is marked in bit 8 */
9470 battery_info.batteries[battery].stop_support = 1;
9473 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9474 &battery_info.batteries[battery].charge_stop)) {
9475 pr_err("Error probing battery stop: %d\n", battery);
9479 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9480 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9481 pr_err("Error probing battery discharge; %d\n", battery);
9484 /* Support is marked in bit 8 */
9486 battery_info.batteries[battery].charge_behaviours |=
9487 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9489 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9490 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9491 pr_err("Error probing battery inhibit charge; %d\n", battery);
9494 /* Support is marked in bit 5 */
9496 battery_info.batteries[battery].charge_behaviours |=
9497 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9500 battery_info.batteries[battery].charge_behaviours |=
9501 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9503 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9505 battery_info.batteries[battery].charge_start,
9506 battery_info.batteries[battery].charge_stop,
9507 battery_info.batteries[battery].charge_behaviours);
9512 /* General helper functions */
9514 static int tpacpi_battery_get_id(const char *battery_name)
9517 if (strcmp(battery_name, "BAT0") == 0 ||
9518 tp_features.battery_force_primary)
9520 if (strcmp(battery_name, "BAT1") == 0)
9521 return BAT_SECONDARY;
9523 * If for some reason the battery is not BAT0 nor is it
9524 * BAT1, we will assume it's the default, first battery,
9527 pr_warn("unknown battery %s, assuming primary", battery_name);
9531 /* sysfs interface */
9533 static ssize_t tpacpi_battery_store(int what,
9535 const char *buf, size_t count)
9537 struct power_supply *supply = to_power_supply(dev);
9538 unsigned long value;
9541 * Some systems have support for more than
9542 * one battery. If that is the case,
9543 * tpacpi_battery_probe marked that addressing
9544 * them individually is supported, so we do that
9545 * based on the device struct.
9547 * On systems that are not supported, we assume
9548 * the primary as most of the ACPI calls fail
9549 * with "Any Battery" as the parameter.
9551 if (battery_info.individual_addressing)
9552 /* BAT_PRIMARY or BAT_SECONDARY */
9553 battery = tpacpi_battery_get_id(supply->desc->name);
9555 battery = BAT_PRIMARY;
9557 rval = kstrtoul(buf, 10, &value);
9562 case THRESHOLD_START:
9563 if (!battery_info.batteries[battery].start_support)
9565 /* valid values are [0, 99] */
9568 if (value > battery_info.batteries[battery].charge_stop)
9570 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9572 battery_info.batteries[battery].charge_start = value;
9575 case THRESHOLD_STOP:
9576 if (!battery_info.batteries[battery].stop_support)
9578 /* valid values are [1, 100] */
9579 if (value < 1 || value > 100)
9581 if (value < battery_info.batteries[battery].charge_start)
9583 battery_info.batteries[battery].charge_stop = value;
9585 * When 100 is passed to stop, we need to flip
9586 * it to 0 as that the EC understands that as
9587 * "Default", which will charge to 100%
9591 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9595 pr_crit("Wrong parameter: %d", what);
9601 static ssize_t tpacpi_battery_show(int what,
9605 struct power_supply *supply = to_power_supply(dev);
9608 * Some systems have support for more than
9609 * one battery. If that is the case,
9610 * tpacpi_battery_probe marked that addressing
9611 * them individually is supported, so we;
9612 * based on the device struct.
9614 * On systems that are not supported, we assume
9615 * the primary as most of the ACPI calls fail
9616 * with "Any Battery" as the parameter.
9618 if (battery_info.individual_addressing)
9619 /* BAT_PRIMARY or BAT_SECONDARY */
9620 battery = tpacpi_battery_get_id(supply->desc->name);
9622 battery = BAT_PRIMARY;
9623 if (tpacpi_battery_get(what, battery, &ret))
9625 return sprintf(buf, "%d\n", ret);
9628 static ssize_t charge_control_start_threshold_show(struct device *device,
9629 struct device_attribute *attr,
9632 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9635 static ssize_t charge_control_end_threshold_show(struct device *device,
9636 struct device_attribute *attr,
9639 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9642 static ssize_t charge_behaviour_show(struct device *dev,
9643 struct device_attribute *attr,
9646 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9647 struct power_supply *supply = to_power_supply(dev);
9648 unsigned int available;
9651 battery = tpacpi_battery_get_id(supply->desc->name);
9652 available = battery_info.batteries[battery].charge_behaviours;
9654 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9655 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9658 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
9663 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9664 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9667 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9673 return power_supply_charge_behaviour_show(dev, available, active, buf);
9676 static ssize_t charge_control_start_threshold_store(struct device *dev,
9677 struct device_attribute *attr,
9678 const char *buf, size_t count)
9680 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9683 static ssize_t charge_control_end_threshold_store(struct device *dev,
9684 struct device_attribute *attr,
9685 const char *buf, size_t count)
9687 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9690 static ssize_t charge_behaviour_store(struct device *dev,
9691 struct device_attribute *attr,
9692 const char *buf, size_t count)
9694 struct power_supply *supply = to_power_supply(dev);
9695 int selected, battery, ret = 0;
9696 unsigned int available;
9698 battery = tpacpi_battery_get_id(supply->desc->name);
9699 available = battery_info.batteries[battery].charge_behaviours;
9700 selected = power_supply_charge_behaviour_parse(available, buf);
9706 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9707 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9708 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9709 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9710 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
9714 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
9715 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9716 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9717 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9721 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9722 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9723 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9724 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
9729 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9736 static DEVICE_ATTR_RW(charge_control_start_threshold);
9737 static DEVICE_ATTR_RW(charge_control_end_threshold);
9738 static DEVICE_ATTR_RW(charge_behaviour);
9739 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9740 charge_start_threshold,
9742 charge_control_start_threshold_show,
9743 charge_control_start_threshold_store
9745 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9746 charge_stop_threshold,
9748 charge_control_end_threshold_show,
9749 charge_control_end_threshold_store
9752 static struct attribute *tpacpi_battery_attrs[] = {
9753 &dev_attr_charge_control_start_threshold.attr,
9754 &dev_attr_charge_control_end_threshold.attr,
9755 &dev_attr_charge_start_threshold.attr,
9756 &dev_attr_charge_stop_threshold.attr,
9757 &dev_attr_charge_behaviour.attr,
9761 ATTRIBUTE_GROUPS(tpacpi_battery);
9763 /* ACPI battery hooking */
9765 static int tpacpi_battery_add(struct power_supply *battery)
9767 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9769 if (tpacpi_battery_probe(batteryid))
9771 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9776 static int tpacpi_battery_remove(struct power_supply *battery)
9778 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9782 static struct acpi_battery_hook battery_hook = {
9783 .add_battery = tpacpi_battery_add,
9784 .remove_battery = tpacpi_battery_remove,
9785 .name = "ThinkPad Battery Extension",
9788 /* Subdriver init/exit */
9790 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9792 * Individual addressing is broken on models that expose the
9793 * primary battery as BAT1.
9795 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9796 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9797 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9798 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9799 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9800 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9803 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9805 memset(&battery_info, 0, sizeof(battery_info));
9807 tp_features.battery_force_primary = tpacpi_check_quirks(
9808 battery_quirk_table,
9809 ARRAY_SIZE(battery_quirk_table));
9811 battery_hook_register(&battery_hook);
9815 static void tpacpi_battery_exit(void)
9817 battery_hook_unregister(&battery_hook);
9820 static struct ibm_struct battery_driver_data = {
9822 .exit = tpacpi_battery_exit,
9825 /*************************************************************************
9826 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9829 static struct drm_privacy_screen *lcdshadow_dev;
9830 static acpi_handle lcdshadow_get_handle;
9831 static acpi_handle lcdshadow_set_handle;
9833 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
9834 enum drm_privacy_screen_status state)
9838 if (WARN_ON(!mutex_is_locked(&priv->lock)))
9841 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
9844 priv->hw_state = priv->sw_state = state;
9848 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
9852 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9855 priv->hw_state = priv->sw_state = output & 0x1;
9858 static const struct drm_privacy_screen_ops lcdshadow_ops = {
9859 .set_sw_state = lcdshadow_set_sw_state,
9860 .get_hw_state = lcdshadow_get_hw_state,
9863 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9865 acpi_status status1, status2;
9868 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
9869 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
9870 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
9873 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
9876 if (!(output & 0x10000))
9879 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
9881 if (IS_ERR(lcdshadow_dev))
9882 return PTR_ERR(lcdshadow_dev);
9887 static void lcdshadow_exit(void)
9889 drm_privacy_screen_unregister(lcdshadow_dev);
9892 static void lcdshadow_resume(void)
9897 mutex_lock(&lcdshadow_dev->lock);
9898 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
9899 mutex_unlock(&lcdshadow_dev->lock);
9902 static int lcdshadow_read(struct seq_file *m)
9904 if (!lcdshadow_dev) {
9905 seq_puts(m, "status:\t\tnot supported\n");
9907 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
9908 seq_puts(m, "commands:\t0, 1\n");
9914 static int lcdshadow_write(char *buf)
9917 int res, state = -EINVAL;
9922 while ((cmd = strsep(&buf, ","))) {
9923 res = kstrtoint(cmd, 10, &state);
9928 if (state >= 2 || state < 0)
9931 mutex_lock(&lcdshadow_dev->lock);
9932 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
9933 mutex_unlock(&lcdshadow_dev->lock);
9935 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
9940 static struct ibm_struct lcdshadow_driver_data = {
9941 .name = "lcdshadow",
9942 .exit = lcdshadow_exit,
9943 .resume = lcdshadow_resume,
9944 .read = lcdshadow_read,
9945 .write = lcdshadow_write,
9948 /*************************************************************************
9949 * Thinkpad sensor interfaces
9952 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
9953 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
9954 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
9955 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
9957 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
9958 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
9960 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
9961 #define PALMSENSOR_ON_BIT 1 /* psensor status */
9963 static bool has_palmsensor;
9964 static bool has_lapsensor;
9965 static bool palm_state;
9966 static bool lap_state;
9967 static int dytc_version;
9969 static int dytc_command(int command, int *output)
9971 acpi_handle dytc_handle;
9973 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
9974 /* Platform doesn't support DYTC */
9977 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
9982 static int lapsensor_get(bool *present, bool *state)
9987 err = dytc_command(DYTC_CMD_GET, &output);
9991 *present = true; /*If we get his far, we have lapmode support*/
9992 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
9996 static int palmsensor_get(bool *present, bool *state)
9998 acpi_handle psensor_handle;
10002 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
10004 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
10007 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10008 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10012 static void lapsensor_refresh(void)
10017 if (has_lapsensor) {
10018 err = lapsensor_get(&has_lapsensor, &state);
10021 if (lap_state != state) {
10023 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10028 static void palmsensor_refresh(void)
10033 if (has_palmsensor) {
10034 err = palmsensor_get(&has_palmsensor, &state);
10037 if (palm_state != state) {
10038 palm_state = state;
10039 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10044 static ssize_t dytc_lapmode_show(struct device *dev,
10045 struct device_attribute *attr,
10049 return sysfs_emit(buf, "%d\n", lap_state);
10050 return sysfs_emit(buf, "\n");
10052 static DEVICE_ATTR_RO(dytc_lapmode);
10054 static ssize_t palmsensor_show(struct device *dev,
10055 struct device_attribute *attr,
10058 if (has_palmsensor)
10059 return sysfs_emit(buf, "%d\n", palm_state);
10060 return sysfs_emit(buf, "\n");
10062 static DEVICE_ATTR_RO(palmsensor);
10064 static struct attribute *proxsensor_attributes[] = {
10065 &dev_attr_dytc_lapmode.attr,
10066 &dev_attr_palmsensor.attr,
10070 static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10071 struct attribute *attr, int n)
10073 if (attr == &dev_attr_dytc_lapmode.attr) {
10075 * Platforms before DYTC version 5 claim to have a lap sensor,
10076 * but it doesn't work, so we ignore them.
10078 if (!has_lapsensor || dytc_version < 5)
10080 } else if (attr == &dev_attr_palmsensor.attr) {
10081 if (!has_palmsensor)
10088 static const struct attribute_group proxsensor_attr_group = {
10089 .is_visible = proxsensor_attr_is_visible,
10090 .attrs = proxsensor_attributes,
10093 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10095 int palm_err, lap_err;
10097 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10098 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10099 /* If support isn't available for both devices return -ENODEV */
10100 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10102 /* Otherwise, if there was an error return it */
10103 if (palm_err && (palm_err != -ENODEV))
10105 if (lap_err && (lap_err != -ENODEV))
10111 static struct ibm_struct proxsensor_driver_data = {
10112 .name = "proximity-sensor",
10115 /*************************************************************************
10116 * DYTC Platform Profile interface
10119 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10120 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10121 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10123 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
10124 #define DYTC_FC_MMC 27 /* MMC Mode supported */
10126 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10127 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10129 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10130 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10131 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10133 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10134 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10135 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
10137 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
10138 #define DYTC_MODE_LOWPOWER 3 /* Low power mode */
10139 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
10140 #define DYTC_MODE_MMC_BALANCE 0 /* Default mode from MMC_GET, aka balanced */
10142 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10143 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10145 #define DYTC_SET_COMMAND(function, mode, on) \
10146 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10147 (mode) << DYTC_SET_MODE_BIT | \
10148 (on) << DYTC_SET_VALID_BIT)
10150 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
10152 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
10154 static enum platform_profile_option dytc_current_profile;
10155 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10156 static DEFINE_MUTEX(dytc_mutex);
10157 static bool dytc_mmc_get_available;
10159 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
10161 switch (dytcmode) {
10162 case DYTC_MODE_LOWPOWER:
10163 *profile = PLATFORM_PROFILE_LOW_POWER;
10165 case DYTC_MODE_BALANCE:
10166 case DYTC_MODE_MMC_BALANCE:
10167 *profile = PLATFORM_PROFILE_BALANCED;
10169 case DYTC_MODE_PERFORM:
10170 *profile = PLATFORM_PROFILE_PERFORMANCE;
10172 default: /* Unknown mode */
10178 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10181 case PLATFORM_PROFILE_LOW_POWER:
10182 *perfmode = DYTC_MODE_LOWPOWER;
10184 case PLATFORM_PROFILE_BALANCED:
10185 *perfmode = DYTC_MODE_BALANCE;
10187 case PLATFORM_PROFILE_PERFORMANCE:
10188 *perfmode = DYTC_MODE_PERFORM;
10190 default: /* Unknown profile */
10191 return -EOPNOTSUPP;
10197 * dytc_profile_get: Function to register with platform_profile
10198 * handler. Returns current platform profile.
10200 static int dytc_profile_get(struct platform_profile_handler *pprof,
10201 enum platform_profile_option *profile)
10203 *profile = dytc_current_profile;
10208 * Helper function - check if we are in CQL mode and if we are
10210 * - run the command
10212 * If not in CQL mode, just run the command
10214 static int dytc_cql_command(int command, int *output)
10216 int err, cmd_err, dummy;
10219 /* Determine if we are in CQL mode. This alters the commands we do */
10220 err = dytc_command(DYTC_CMD_GET, output);
10224 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10225 /* Check if we're OK to return immediately */
10226 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10229 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10230 atomic_inc(&dytc_ignore_event);
10231 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10236 cmd_err = dytc_command(command, output);
10237 /* Check return condition after we've restored CQL state */
10239 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10240 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10248 * dytc_profile_set: Function to register with platform_profile
10249 * handler. Sets current platform profile.
10251 static int dytc_profile_set(struct platform_profile_handler *pprof,
10252 enum platform_profile_option profile)
10257 err = mutex_lock_interruptible(&dytc_mutex);
10261 if (profile == PLATFORM_PROFILE_BALANCED) {
10263 * To get back to balanced mode we need to issue a reset command.
10264 * Note we still need to disable CQL mode before hand and re-enable
10265 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10266 * stuck at 0 for aprox. 30 minutes.
10268 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10274 err = convert_profile_to_dytc(profile, &perfmode);
10278 /* Determine if we are in CQL mode. This alters the commands we do */
10279 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1), &output);
10283 /* Success - update current profile */
10284 dytc_current_profile = profile;
10286 mutex_unlock(&dytc_mutex);
10290 static void dytc_profile_refresh(void)
10292 enum platform_profile_option profile;
10296 mutex_lock(&dytc_mutex);
10297 if (dytc_mmc_get_available)
10298 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10300 err = dytc_cql_command(DYTC_CMD_GET, &output);
10301 mutex_unlock(&dytc_mutex);
10305 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10306 convert_dytc_to_profile(perfmode, &profile);
10307 if (profile != dytc_current_profile) {
10308 dytc_current_profile = profile;
10309 platform_profile_notify();
10313 static struct platform_profile_handler dytc_profile = {
10314 .profile_get = dytc_profile_get,
10315 .profile_set = dytc_profile_set,
10318 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10322 /* Setup supported modes */
10323 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
10324 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
10325 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
10327 err = dytc_command(DYTC_CMD_QUERY, &output);
10331 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10332 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
10334 /* Check DYTC is enabled and supports mode setting */
10335 if (dytc_version < 5)
10338 /* Check what capabilities are supported. Currently MMC is needed */
10339 err = dytc_command(DYTC_CMD_FUNC_CAP, &output);
10342 if (!(output & BIT(DYTC_FC_MMC))) {
10343 dbg_printk(TPACPI_DBG_INIT, " DYTC MMC mode not supported\n");
10347 dbg_printk(TPACPI_DBG_INIT,
10348 "DYTC version %d: thermal mode available\n", dytc_version);
10350 * Check if MMC_GET functionality available
10351 * Version > 6 and return success from MMC_GET command
10353 dytc_mmc_get_available = false;
10354 if (dytc_version >= 6) {
10355 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10356 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10357 dytc_mmc_get_available = true;
10359 /* Create platform_profile structure and register */
10360 err = platform_profile_register(&dytc_profile);
10362 * If for some reason platform_profiles aren't enabled
10363 * don't quit terminally.
10368 /* Ensure initial values are correct */
10369 dytc_profile_refresh();
10374 static void dytc_profile_exit(void)
10376 platform_profile_remove();
10379 static struct ibm_struct dytc_profile_driver_data = {
10380 .name = "dytc-profile",
10381 .exit = dytc_profile_exit,
10384 /*************************************************************************
10385 * Keyboard language interface
10388 struct keyboard_lang_data {
10389 const char *lang_str;
10393 static const struct keyboard_lang_data keyboard_lang_data[] = {
10415 static int set_keyboard_lang_command(int command)
10417 acpi_handle sskl_handle;
10420 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10421 /* Platform doesn't support SSKL */
10425 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10431 static int get_keyboard_lang(int *output)
10433 acpi_handle gskl_handle;
10436 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10437 /* Platform doesn't support GSKL */
10441 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10445 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10446 * '(' and ')') keys which use layout dependent key-press emulation.
10448 if (kbd_lang & METHOD_ERR)
10451 *output = kbd_lang;
10456 /* sysfs keyboard language entry */
10457 static ssize_t keyboard_lang_show(struct device *dev,
10458 struct device_attribute *attr,
10461 int output, err, i, len = 0;
10463 err = get_keyboard_lang(&output);
10467 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10469 len += sysfs_emit_at(buf, len, "%s", " ");
10471 if (output == keyboard_lang_data[i].lang_code) {
10472 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10474 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10477 len += sysfs_emit_at(buf, len, "\n");
10482 static ssize_t keyboard_lang_store(struct device *dev,
10483 struct device_attribute *attr,
10484 const char *buf, size_t count)
10487 bool lang_found = false;
10490 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10491 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10492 lang_code = keyboard_lang_data[i].lang_code;
10499 lang_code = lang_code | 1 << 24;
10501 /* Set language code */
10502 err = set_keyboard_lang_command(lang_code);
10506 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10510 tpacpi_disclose_usertask(attr->attr.name,
10511 "keyboard language is set to %s\n", buf);
10513 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10517 static DEVICE_ATTR_RW(keyboard_lang);
10519 static struct attribute *kbdlang_attributes[] = {
10520 &dev_attr_keyboard_lang.attr,
10524 static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10525 struct attribute *attr, int n)
10527 return tp_features.kbd_lang ? attr->mode : 0;
10530 static const struct attribute_group kbdlang_attr_group = {
10531 .is_visible = kbdlang_attr_is_visible,
10532 .attrs = kbdlang_attributes,
10535 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10539 err = get_keyboard_lang(&output);
10540 tp_features.kbd_lang = !err;
10544 static struct ibm_struct kbdlang_driver_data = {
10548 /*************************************************************************
10549 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10550 * and WLAN feature.
10552 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10553 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10554 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10555 static bool has_antennatype;
10556 static int wwan_antennatype;
10558 static int dprc_command(int command, int *output)
10560 acpi_handle dprc_handle;
10562 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10563 /* Platform doesn't support DPRC */
10567 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10571 * METHOD_ERR gets returned on devices where few commands are not supported
10572 * for example command to get WWAN Antenna type command is not supported on
10575 if (*output & METHOD_ERR)
10581 static int get_wwan_antenna(int *wwan_antennatype)
10585 /* Get current Antenna type */
10586 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10590 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10591 *wwan_antennatype = 1;
10592 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10593 *wwan_antennatype = 2;
10600 /* sysfs wwan antenna type entry */
10601 static ssize_t wwan_antenna_type_show(struct device *dev,
10602 struct device_attribute *attr,
10605 switch (wwan_antennatype) {
10607 return sysfs_emit(buf, "type a\n");
10609 return sysfs_emit(buf, "type b\n");
10614 static DEVICE_ATTR_RO(wwan_antenna_type);
10616 static struct attribute *dprc_attributes[] = {
10617 &dev_attr_wwan_antenna_type.attr,
10621 static umode_t dprc_attr_is_visible(struct kobject *kobj,
10622 struct attribute *attr, int n)
10624 return has_antennatype ? attr->mode : 0;
10627 static const struct attribute_group dprc_attr_group = {
10628 .is_visible = dprc_attr_is_visible,
10629 .attrs = dprc_attributes,
10632 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10636 err = get_wwan_antenna(&wwan_antennatype);
10640 has_antennatype = true;
10644 static struct ibm_struct dprc_driver_data = {
10648 /* --------------------------------------------------------------------- */
10650 static struct attribute *tpacpi_driver_attributes[] = {
10651 &driver_attr_debug_level.attr,
10652 &driver_attr_version.attr,
10653 &driver_attr_interface_version.attr,
10654 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10655 &driver_attr_wlsw_emulstate.attr,
10656 &driver_attr_bluetooth_emulstate.attr,
10657 &driver_attr_wwan_emulstate.attr,
10658 &driver_attr_uwb_emulstate.attr,
10663 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10664 static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
10665 struct attribute *attr, int n)
10667 if (attr == &driver_attr_wlsw_emulstate.attr) {
10670 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
10671 if (!dbg_bluetoothemul)
10673 } else if (attr == &driver_attr_wwan_emulstate.attr) {
10676 } else if (attr == &driver_attr_uwb_emulstate.attr) {
10685 static const struct attribute_group tpacpi_driver_attr_group = {
10686 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10687 .is_visible = tpacpi_attr_is_visible,
10689 .attrs = tpacpi_driver_attributes,
10692 static const struct attribute_group *tpacpi_driver_groups[] = {
10693 &tpacpi_driver_attr_group,
10697 static const struct attribute_group *tpacpi_groups[] = {
10698 &adaptive_kbd_attr_group,
10699 &hotkey_attr_group,
10700 &bluetooth_attr_group,
10703 &proxsensor_attr_group,
10704 &kbdlang_attr_group,
10709 static const struct attribute_group *tpacpi_hwmon_groups[] = {
10710 &thermal_attr_group,
10711 &temp_label_attr_group,
10716 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
10717 &fan_driver_attr_group,
10721 /****************************************************************************
10722 ****************************************************************************
10726 ****************************************************************************
10727 ****************************************************************************/
10729 static struct platform_driver tpacpi_pdriver = {
10731 .name = TPACPI_DRVR_NAME,
10733 .groups = tpacpi_driver_groups,
10734 .dev_groups = tpacpi_groups,
10736 .shutdown = tpacpi_shutdown_handler,
10739 static struct platform_driver tpacpi_hwmon_pdriver = {
10741 .name = TPACPI_HWMON_DRVR_NAME,
10742 .groups = tpacpi_hwmon_driver_groups,
10746 /****************************************************************************
10747 ****************************************************************************
10751 ****************************************************************************
10752 ****************************************************************************/
10755 * HKEY event callout for other subdrivers go here
10756 * (yes, it is ugly, but it is quick, safe, and gets the job done
10758 static void tpacpi_driver_event(const unsigned int hkey_event)
10760 if (ibm_backlight_device) {
10761 switch (hkey_event) {
10762 case TP_HKEY_EV_BRGHT_UP:
10763 case TP_HKEY_EV_BRGHT_DOWN:
10764 tpacpi_brightness_notify_change();
10768 switch (hkey_event) {
10769 case TP_HKEY_EV_VOL_UP:
10770 case TP_HKEY_EV_VOL_DOWN:
10771 case TP_HKEY_EV_VOL_MUTE:
10772 volume_alsa_notify_change();
10775 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
10776 enum led_brightness brightness;
10778 mutex_lock(&kbdlight_mutex);
10781 * Check the brightness actually changed, setting the brightness
10782 * through kbdlight_set_level() also triggers this event.
10784 brightness = kbdlight_sysfs_get(NULL);
10785 if (kbdlight_brightness != brightness) {
10786 kbdlight_brightness = brightness;
10787 led_classdev_notify_brightness_hw_changed(
10788 &tpacpi_led_kbdlight.led_classdev, brightness);
10791 mutex_unlock(&kbdlight_mutex);
10794 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) {
10795 lapsensor_refresh();
10796 /* If we are already accessing DYTC then skip dytc update */
10797 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
10798 dytc_profile_refresh();
10801 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) {
10802 enum drm_privacy_screen_status old_hw_state;
10805 mutex_lock(&lcdshadow_dev->lock);
10806 old_hw_state = lcdshadow_dev->hw_state;
10807 lcdshadow_get_hw_state(lcdshadow_dev);
10808 changed = lcdshadow_dev->hw_state != old_hw_state;
10809 mutex_unlock(&lcdshadow_dev->lock);
10812 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10816 static void hotkey_driver_event(const unsigned int scancode)
10818 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
10821 /* --------------------------------------------------------------------- */
10823 /* /proc support */
10824 static struct proc_dir_entry *proc_dir;
10827 * Module and infrastructure proble, init and exit handling
10830 static bool force_load;
10832 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
10833 static const char * __init str_supported(int is_supported)
10835 static char text_unsupported[] __initdata = "not supported";
10837 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
10839 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
10841 static void ibm_exit(struct ibm_struct *ibm)
10843 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
10845 list_del_init(&ibm->all_drivers);
10847 if (ibm->flags.acpi_notify_installed) {
10848 dbg_printk(TPACPI_DBG_EXIT,
10849 "%s: acpi_remove_notify_handler\n", ibm->name);
10850 BUG_ON(!ibm->acpi);
10851 acpi_remove_notify_handler(*ibm->acpi->handle,
10853 dispatch_acpi_notify);
10854 ibm->flags.acpi_notify_installed = 0;
10857 if (ibm->flags.proc_created) {
10858 dbg_printk(TPACPI_DBG_EXIT,
10859 "%s: remove_proc_entry\n", ibm->name);
10860 remove_proc_entry(ibm->name, proc_dir);
10861 ibm->flags.proc_created = 0;
10864 if (ibm->flags.acpi_driver_registered) {
10865 dbg_printk(TPACPI_DBG_EXIT,
10866 "%s: acpi_bus_unregister_driver\n", ibm->name);
10867 BUG_ON(!ibm->acpi);
10868 acpi_bus_unregister_driver(ibm->acpi->driver);
10869 kfree(ibm->acpi->driver);
10870 ibm->acpi->driver = NULL;
10871 ibm->flags.acpi_driver_registered = 0;
10874 if (ibm->flags.init_called && ibm->exit) {
10876 ibm->flags.init_called = 0;
10879 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
10882 static int __init ibm_init(struct ibm_init_struct *iibm)
10885 struct ibm_struct *ibm = iibm->data;
10886 struct proc_dir_entry *entry;
10888 BUG_ON(ibm == NULL);
10890 INIT_LIST_HEAD(&ibm->all_drivers);
10892 if (ibm->flags.experimental && !experimental)
10895 dbg_printk(TPACPI_DBG_INIT,
10896 "probing for %s\n", ibm->name);
10899 ret = iibm->init(iibm);
10900 if (ret > 0 || ret == -ENODEV)
10901 return 0; /* subdriver functionality not available */
10905 ibm->flags.init_called = 1;
10909 if (ibm->acpi->hid) {
10910 ret = register_tpacpi_subdriver(ibm);
10915 if (ibm->acpi->notify) {
10916 ret = setup_acpi_notify(ibm);
10917 if (ret == -ENODEV) {
10918 pr_notice("disabling subdriver %s\n",
10928 dbg_printk(TPACPI_DBG_INIT,
10929 "%s installed\n", ibm->name);
10932 umode_t mode = iibm->base_procfs_mode;
10938 entry = proc_create_data(ibm->name, mode, proc_dir,
10939 &dispatch_proc_ops, ibm);
10941 pr_err("unable to create proc entry %s\n", ibm->name);
10945 ibm->flags.proc_created = 1;
10948 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10953 dbg_printk(TPACPI_DBG_INIT,
10954 "%s: at error exit path with result %d\n",
10958 return (ret < 0) ? ret : 0;
10963 static char __init tpacpi_parse_fw_id(const char * const s,
10964 u32 *model, u16 *release)
10968 if (!s || strlen(s) < 8)
10971 for (i = 0; i < 8; i++)
10972 if (!((s[i] >= '0' && s[i] <= '9') ||
10973 (s[i] >= 'A' && s[i] <= 'Z')))
10977 * Most models: xxyTkkWW (#.##c)
10978 * Ancient 570/600 and -SL lacks (#.##c)
10980 if (s[3] == 'T' || s[3] == 'N') {
10981 *model = TPID(s[0], s[1]);
10982 *release = TPVER(s[4], s[5]);
10985 /* New models: xxxyTkkW (#.##c); T550 and some others */
10986 } else if (s[4] == 'T' || s[4] == 'N') {
10987 *model = TPID3(s[0], s[1], s[2]);
10988 *release = TPVER(s[5], s[6]);
10996 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10998 char *ec_fw_string = (char *) private;
10999 const char *dmi_data = (const char *)dm;
11001 * ThinkPad Embedded Controller Program Table on newer models
11003 * Offset | Name | Width | Description
11004 * ----------------------------------------------------
11005 * 0x00 | Type | BYTE | 0x8C
11006 * 0x01 | Length | BYTE |
11007 * 0x02 | Handle | WORD | Varies
11008 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
11009 * 0x0A | OEM struct offset | BYTE | 0x0B
11010 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
11011 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
11012 * 0x0D | ECP version ID | STR ID |
11013 * 0x0E | ECP release date | STR ID |
11016 /* Return if data structure not match */
11017 if (dm->type != 140 || dm->length < 0x0F ||
11018 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
11019 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11020 dmi_data[0x0C] != 0x01)
11023 /* fwstr is the first 8byte string */
11024 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
11027 /* returns 0 - probe ok, or < 0 - probe error.
11028 * Probe ok doesn't mean thinkpad found.
11029 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
11030 static int __must_check __init get_thinkpad_model_data(
11031 struct thinkpad_id_data *tp)
11033 const struct dmi_device *dev = NULL;
11034 char ec_fw_string[18] = {0};
11041 memset(tp, 0, sizeof(*tp));
11043 if (dmi_name_in_vendors("IBM"))
11044 tp->vendor = PCI_VENDOR_ID_IBM;
11045 else if (dmi_name_in_vendors("LENOVO"))
11046 tp->vendor = PCI_VENDOR_ID_LENOVO;
11050 s = dmi_get_system_info(DMI_BIOS_VERSION);
11051 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11052 if (s && !tp->bios_version_str)
11055 /* Really ancient ThinkPad 240X will fail this, which is fine */
11056 t = tpacpi_parse_fw_id(tp->bios_version_str,
11057 &tp->bios_model, &tp->bios_release);
11058 if (t != 'E' && t != 'C')
11062 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11063 * X32 or newer, all Z series; Some models must have an
11064 * up-to-date BIOS or they will not be detected.
11066 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11068 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
11069 if (sscanf(dev->name,
11070 "IBM ThinkPad Embedded Controller -[%17c",
11071 ec_fw_string) == 1) {
11072 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11073 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
11078 /* Newer ThinkPads have different EC program info table */
11079 if (!ec_fw_string[0])
11080 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11082 if (ec_fw_string[0]) {
11083 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11084 if (!tp->ec_version_str)
11087 t = tpacpi_parse_fw_id(ec_fw_string,
11088 &tp->ec_model, &tp->ec_release);
11090 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11092 pr_notice("please report this to %s\n", TPACPI_MAIL);
11096 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
11097 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
11098 tp->model_str = kstrdup(s, GFP_KERNEL);
11099 if (!tp->model_str)
11102 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11103 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11104 tp->model_str = kstrdup(s, GFP_KERNEL);
11105 if (!tp->model_str)
11110 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11111 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11112 if (s && !tp->nummodel_str)
11118 static int __init probe_for_thinkpad(void)
11125 /* It would be dangerous to run the driver in this case */
11126 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11130 * Non-ancient models have better DMI tagging, but very old models
11131 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11133 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11134 (thinkpad_id.ec_model != 0) ||
11135 tpacpi_is_fw_known();
11137 /* The EC handler is required */
11138 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11141 pr_err("Not yet supported ThinkPad detected!\n");
11145 if (!is_thinkpad && !force_load)
11151 static void __init thinkpad_acpi_init_banner(void)
11153 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11154 pr_info("%s\n", TPACPI_URL);
11156 pr_info("ThinkPad BIOS %s, EC %s\n",
11157 (thinkpad_id.bios_version_str) ?
11158 thinkpad_id.bios_version_str : "unknown",
11159 (thinkpad_id.ec_version_str) ?
11160 thinkpad_id.ec_version_str : "unknown");
11162 BUG_ON(!thinkpad_id.vendor);
11164 if (thinkpad_id.model_str)
11165 pr_info("%s %s, model %s\n",
11166 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11167 "IBM" : ((thinkpad_id.vendor ==
11168 PCI_VENDOR_ID_LENOVO) ?
11169 "Lenovo" : "Unknown vendor"),
11170 thinkpad_id.model_str,
11171 (thinkpad_id.nummodel_str) ?
11172 thinkpad_id.nummodel_str : "unknown");
11175 /* Module init, exit, parameters */
11177 static struct ibm_init_struct ibms_init[] __initdata = {
11179 .data = &thinkpad_acpi_driver_data,
11182 .init = hotkey_init,
11183 .data = &hotkey_driver_data,
11186 .init = bluetooth_init,
11187 .data = &bluetooth_driver_data,
11191 .data = &wan_driver_data,
11195 .data = &uwb_driver_data,
11197 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
11199 .init = video_init,
11200 .base_procfs_mode = S_IRUSR,
11201 .data = &video_driver_data,
11205 .init = kbdlight_init,
11206 .data = &kbdlight_driver_data,
11209 .init = light_init,
11210 .data = &light_driver_data,
11214 .data = &cmos_driver_data,
11218 .data = &led_driver_data,
11222 .data = &beep_driver_data,
11225 .init = thermal_init,
11226 .data = &thermal_driver_data,
11229 .init = brightness_init,
11230 .data = &brightness_driver_data,
11233 .init = volume_init,
11234 .data = &volume_driver_data,
11238 .data = &fan_driver_data,
11241 .init = mute_led_init,
11242 .data = &mute_led_driver_data,
11245 .init = tpacpi_battery_init,
11246 .data = &battery_driver_data,
11249 .init = tpacpi_lcdshadow_init,
11250 .data = &lcdshadow_driver_data,
11253 .init = tpacpi_proxsensor_init,
11254 .data = &proxsensor_driver_data,
11257 .init = tpacpi_dytc_profile_init,
11258 .data = &dytc_profile_driver_data,
11261 .init = tpacpi_kbdlang_init,
11262 .data = &kbdlang_driver_data,
11265 .init = tpacpi_dprc_init,
11266 .data = &dprc_driver_data,
11270 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
11273 struct ibm_struct *ibm;
11275 if (!kp || !kp->name || !val)
11278 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11279 ibm = ibms_init[i].data;
11280 if (!ibm || !ibm->name)
11283 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
11284 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
11286 strcpy(ibms_init[i].param, val);
11294 module_param(experimental, int, 0444);
11295 MODULE_PARM_DESC(experimental,
11296 "Enables experimental features when non-zero");
11298 module_param_named(debug, dbg_level, uint, 0);
11299 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
11301 module_param(force_load, bool, 0444);
11302 MODULE_PARM_DESC(force_load,
11303 "Attempts to load the driver even on a mis-identified ThinkPad when true");
11305 module_param_named(fan_control, fan_control_allowed, bool, 0444);
11306 MODULE_PARM_DESC(fan_control,
11307 "Enables setting fan parameters features when true");
11309 module_param_named(brightness_mode, brightness_mode, uint, 0444);
11310 MODULE_PARM_DESC(brightness_mode,
11311 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
11313 module_param(brightness_enable, uint, 0444);
11314 MODULE_PARM_DESC(brightness_enable,
11315 "Enables backlight control when 1, disables when 0");
11317 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
11318 module_param_named(volume_mode, volume_mode, uint, 0444);
11319 MODULE_PARM_DESC(volume_mode,
11320 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
11322 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
11323 MODULE_PARM_DESC(volume_capabilities,
11324 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
11326 module_param_named(volume_control, volume_control_allowed, bool, 0444);
11327 MODULE_PARM_DESC(volume_control,
11328 "Enables software override for the console audio control when true");
11330 module_param_named(software_mute, software_mute_requested, bool, 0444);
11331 MODULE_PARM_DESC(software_mute,
11332 "Request full software mute control");
11334 /* ALSA module API parameters */
11335 module_param_named(index, alsa_index, int, 0444);
11336 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
11337 module_param_named(id, alsa_id, charp, 0444);
11338 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
11339 module_param_named(enable, alsa_enable, bool, 0444);
11340 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
11341 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
11343 /* The module parameter can't be read back, that's why 0 is used here */
11344 #define TPACPI_PARAM(feature) \
11345 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
11346 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
11348 TPACPI_PARAM(hotkey);
11349 TPACPI_PARAM(bluetooth);
11350 TPACPI_PARAM(video);
11351 TPACPI_PARAM(light);
11352 TPACPI_PARAM(cmos);
11354 TPACPI_PARAM(beep);
11355 TPACPI_PARAM(brightness);
11356 TPACPI_PARAM(volume);
11359 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11360 module_param(dbg_wlswemul, uint, 0444);
11361 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
11362 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
11363 MODULE_PARM_DESC(wlsw_state,
11364 "Initial state of the emulated WLSW switch");
11366 module_param(dbg_bluetoothemul, uint, 0444);
11367 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
11368 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
11369 MODULE_PARM_DESC(bluetooth_state,
11370 "Initial state of the emulated bluetooth switch");
11372 module_param(dbg_wwanemul, uint, 0444);
11373 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
11374 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
11375 MODULE_PARM_DESC(wwan_state,
11376 "Initial state of the emulated WWAN switch");
11378 module_param(dbg_uwbemul, uint, 0444);
11379 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
11380 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
11381 MODULE_PARM_DESC(uwb_state,
11382 "Initial state of the emulated UWB switch");
11385 static void thinkpad_acpi_module_exit(void)
11387 struct ibm_struct *ibm, *itmp;
11389 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
11392 hwmon_device_unregister(tpacpi_hwmon);
11393 if (tp_features.sensors_pdrv_registered)
11394 platform_driver_unregister(&tpacpi_hwmon_pdriver);
11395 if (tp_features.platform_drv_registered)
11396 platform_driver_unregister(&tpacpi_pdriver);
11398 list_for_each_entry_safe_reverse(ibm, itmp,
11399 &tpacpi_all_drivers,
11404 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
11406 if (tpacpi_inputdev) {
11407 if (tp_features.input_device_registered)
11408 input_unregister_device(tpacpi_inputdev);
11410 input_free_device(tpacpi_inputdev);
11411 kfree(hotkey_keycode_map);
11414 if (tpacpi_sensors_pdev)
11415 platform_device_unregister(tpacpi_sensors_pdev);
11417 platform_device_unregister(tpacpi_pdev);
11419 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
11421 destroy_workqueue(tpacpi_wq);
11423 kfree(thinkpad_id.bios_version_str);
11424 kfree(thinkpad_id.ec_version_str);
11425 kfree(thinkpad_id.model_str);
11426 kfree(thinkpad_id.nummodel_str);
11430 static int __init thinkpad_acpi_module_init(void)
11434 tpacpi_lifecycle = TPACPI_LIFE_INIT;
11436 /* Driver-level probe */
11438 ret = get_thinkpad_model_data(&thinkpad_id);
11440 pr_err("unable to get DMI data: %d\n", ret);
11441 thinkpad_acpi_module_exit();
11444 ret = probe_for_thinkpad();
11446 thinkpad_acpi_module_exit();
11450 /* Driver initialization */
11452 thinkpad_acpi_init_banner();
11453 tpacpi_check_outdated_fw();
11455 TPACPI_ACPIHANDLE_INIT(ecrd);
11456 TPACPI_ACPIHANDLE_INIT(ecwr);
11458 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
11460 thinkpad_acpi_module_exit();
11464 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
11466 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
11467 thinkpad_acpi_module_exit();
11471 /* Device initialization */
11472 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
11474 if (IS_ERR(tpacpi_pdev)) {
11475 ret = PTR_ERR(tpacpi_pdev);
11476 tpacpi_pdev = NULL;
11477 pr_err("unable to register platform device\n");
11478 thinkpad_acpi_module_exit();
11481 tpacpi_sensors_pdev = platform_device_register_simple(
11482 TPACPI_HWMON_DRVR_NAME,
11484 if (IS_ERR(tpacpi_sensors_pdev)) {
11485 ret = PTR_ERR(tpacpi_sensors_pdev);
11486 tpacpi_sensors_pdev = NULL;
11487 pr_err("unable to register hwmon platform device\n");
11488 thinkpad_acpi_module_exit();
11492 mutex_init(&tpacpi_inputdev_send_mutex);
11493 tpacpi_inputdev = input_allocate_device();
11494 if (!tpacpi_inputdev) {
11495 thinkpad_acpi_module_exit();
11498 /* Prepare input device, but don't register */
11499 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
11500 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
11501 tpacpi_inputdev->id.bustype = BUS_HOST;
11502 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
11503 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
11504 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
11505 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
11508 /* Init subdriver dependencies */
11509 tpacpi_detect_brightness_capabilities();
11511 /* Init subdrivers */
11512 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
11513 ret = ibm_init(&ibms_init[i]);
11514 if (ret >= 0 && *ibms_init[i].param)
11515 ret = ibms_init[i].data->write(ibms_init[i].param);
11517 thinkpad_acpi_module_exit();
11522 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
11524 ret = platform_driver_register(&tpacpi_pdriver);
11526 pr_err("unable to register main platform driver\n");
11527 thinkpad_acpi_module_exit();
11530 tp_features.platform_drv_registered = 1;
11532 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
11534 pr_err("unable to register hwmon platform driver\n");
11535 thinkpad_acpi_module_exit();
11538 tp_features.sensors_pdrv_registered = 1;
11540 tpacpi_hwmon = hwmon_device_register_with_groups(
11541 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
11542 if (IS_ERR(tpacpi_hwmon)) {
11543 ret = PTR_ERR(tpacpi_hwmon);
11544 tpacpi_hwmon = NULL;
11545 pr_err("unable to register hwmon device\n");
11546 thinkpad_acpi_module_exit();
11550 ret = input_register_device(tpacpi_inputdev);
11552 pr_err("unable to register input device\n");
11553 thinkpad_acpi_module_exit();
11556 tp_features.input_device_registered = 1;
11562 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
11565 * This will autoload the driver in almost every ThinkPad
11566 * in widespread use.
11568 * Only _VERY_ old models, like the 240, 240x and 570 lack
11569 * the HKEY event interface.
11571 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
11574 * DMI matching for module autoloading
11576 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11577 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
11579 * Only models listed in thinkwiki will be supported, so add yours
11580 * if it is not there yet.
11582 #define IBM_BIOS_MODULE_ALIAS(__type) \
11583 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
11585 /* Ancient thinkpad BIOSes have to be identified by
11586 * BIOS type or model number, and there are far less
11587 * BIOS types than model numbers... */
11588 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
11590 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
11591 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
11592 MODULE_DESCRIPTION(TPACPI_DESC);
11593 MODULE_VERSION(TPACPI_VERSION);
11594 MODULE_LICENSE("GPL");
11596 module_init(thinkpad_acpi_module_init);
11597 module_exit(thinkpad_acpi_module_exit);