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 <sound/core.h>
70 #include <sound/control.h>
71 #include <sound/initval.h>
72 #include <linux/uaccess.h>
73 #include <acpi/battery.h>
74 #include <acpi/video.h>
76 /* ThinkPad CMOS commands */
77 #define TP_CMOS_VOLUME_DOWN 0
78 #define TP_CMOS_VOLUME_UP 1
79 #define TP_CMOS_VOLUME_MUTE 2
80 #define TP_CMOS_BRIGHTNESS_UP 4
81 #define TP_CMOS_BRIGHTNESS_DOWN 5
82 #define TP_CMOS_THINKLIGHT_ON 12
83 #define TP_CMOS_THINKLIGHT_OFF 13
87 TP_NVRAM_ADDR_HK2 = 0x57,
88 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
89 TP_NVRAM_ADDR_VIDEO = 0x59,
90 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
91 TP_NVRAM_ADDR_MIXER = 0x60,
96 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
97 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
98 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
99 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
100 TP_NVRAM_MASK_THINKLIGHT = 0x10,
101 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
102 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
103 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
104 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
105 TP_NVRAM_MASK_MUTE = 0x40,
106 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
107 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
108 TP_NVRAM_POS_LEVEL_VOLUME = 0,
111 /* Misc NVRAM-related */
113 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
117 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
118 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
119 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
120 #define TPACPI_ACPI_EC_HID "PNP0C09"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
149 enum tpacpi_hkey_event_t {
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
155 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
156 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
157 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
159 /* Reasons for waking up from S3/S4 */
160 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
161 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
162 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
163 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
164 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
165 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
167 /* Auto-sleep after eject request */
168 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
169 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
171 /* Misc bay events */
172 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
174 or port replicator */
175 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
176 dock or port replicator */
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
183 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
184 * enter/leave tablet mode
186 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
187 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
188 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
190 /* Key-related user-interface events */
191 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
192 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
193 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
196 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
197 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
198 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
199 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
200 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
201 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
202 * command completed. Related to
204 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
205 * changed. Related to AML GMTS */
207 /* AC-related events */
208 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
210 /* Further user-interface events */
211 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
212 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
215 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
218 /****************************************************************************
222 #define TPACPI_NAME "thinkpad"
223 #define TPACPI_DESC "ThinkPad ACPI Extras"
224 #define TPACPI_FILE TPACPI_NAME "_acpi"
225 #define TPACPI_URL "http://ibm-acpi.sf.net/"
226 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
228 #define TPACPI_PROC_DIR "ibm"
229 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
230 #define TPACPI_DRVR_NAME TPACPI_FILE
231 #define TPACPI_DRVR_SHORTNAME "tpacpi"
232 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
234 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
235 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
237 #define TPACPI_MAX_ACPI_ARGS 3
239 /* Debugging printk groups */
240 #define TPACPI_DBG_ALL 0xffff
241 #define TPACPI_DBG_DISCLOSETASK 0x8000
242 #define TPACPI_DBG_INIT 0x0001
243 #define TPACPI_DBG_EXIT 0x0002
244 #define TPACPI_DBG_RFKILL 0x0004
245 #define TPACPI_DBG_HKEY 0x0008
246 #define TPACPI_DBG_FAN 0x0010
247 #define TPACPI_DBG_BRGHT 0x0020
248 #define TPACPI_DBG_MIXER 0x0040
250 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
251 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
252 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
255 /****************************************************************************
256 * Driver-wide structs and misc. variables
261 struct tp_acpi_drv_struct {
262 const struct acpi_device_id *hid;
263 struct acpi_driver *driver;
265 void (*notify) (struct ibm_struct *, u32);
268 struct acpi_device *device;
274 int (*read) (struct seq_file *);
275 int (*write) (char *);
277 void (*resume) (void);
278 void (*suspend) (void);
279 void (*shutdown) (void);
281 struct list_head all_drivers;
283 struct tp_acpi_drv_struct *acpi;
286 u8 acpi_driver_registered:1;
287 u8 acpi_notify_installed:1;
294 struct ibm_init_struct {
297 int (*init) (struct ibm_init_struct *);
298 umode_t base_procfs_mode;
299 struct ibm_struct *data;
308 TP_HOTKEY_TABLET_NONE = 0,
309 TP_HOTKEY_TABLET_USES_MHKG,
310 TP_HOTKEY_TABLET_USES_GMMS,
315 u32 bright_acpimode:1;
319 u32 fan_ctrl_status_undef:1;
321 u32 beep_needs_two_args:1;
322 u32 mixer_no_level_control:1;
323 u32 battery_force_primary:1;
324 u32 input_device_registered:1;
325 u32 platform_drv_registered:1;
326 u32 platform_drv_attrs_registered:1;
327 u32 sensors_pdrv_registered:1;
328 u32 sensors_pdrv_attrs_registered:1;
329 u32 sensors_pdev_attrs_registered:1;
330 u32 hotkey_poll_active:1;
331 u32 has_adaptive_kbd:1;
335 u16 hotkey_mask_ff:1;
336 u16 volume_ctrl_forbidden:1;
339 struct thinkpad_id_data {
340 unsigned int vendor; /* ThinkPad vendor:
341 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
343 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
344 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
346 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
348 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
351 char *model_str; /* ThinkPad T43 */
352 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
354 static struct thinkpad_id_data thinkpad_id;
357 TPACPI_LIFE_INIT = 0,
362 static int experimental;
363 static u32 dbg_level;
365 static struct workqueue_struct *tpacpi_wq;
373 /* tpacpi LED class */
374 struct tpacpi_led_classdev {
375 struct led_classdev led_classdev;
379 /* brightness level capabilities */
380 static unsigned int bright_maxlvl; /* 0 = unknown */
382 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
383 static int dbg_wlswemul;
384 static bool tpacpi_wlsw_emulstate;
385 static int dbg_bluetoothemul;
386 static bool tpacpi_bluetooth_emulstate;
387 static int dbg_wwanemul;
388 static bool tpacpi_wwan_emulstate;
389 static int dbg_uwbemul;
390 static bool tpacpi_uwb_emulstate;
394 /*************************************************************************
398 #define dbg_printk(a_dbg_level, format, arg...) \
400 if (dbg_level & (a_dbg_level)) \
401 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
406 #define vdbg_printk dbg_printk
407 static const char *str_supported(int is_supported);
409 static inline const char *str_supported(int is_supported) { return ""; }
410 #define vdbg_printk(a_dbg_level, format, arg...) \
411 do { if (0) no_printk(format, ##arg); } while (0)
414 static void tpacpi_log_usertask(const char * const what)
416 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
417 what, task_tgid_vnr(current));
420 #define tpacpi_disclose_usertask(what, format, arg...) \
422 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
423 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
424 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
425 what, task_tgid_vnr(current), ## arg); \
430 * Quirk handling helpers
432 * ThinkPad IDs and versions seen in the field so far are
433 * two or three characters from the set [0-9A-Z], i.e. base 36.
435 * We use values well outside that range as specials.
438 #define TPACPI_MATCH_ANY 0xffffffffU
439 #define TPACPI_MATCH_ANY_VERSION 0xffffU
440 #define TPACPI_MATCH_UNKNOWN 0U
442 /* TPID('1', 'Y') == 0x3159 */
443 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
444 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
447 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
453 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
459 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPID3(__id1, __id2, __id3), \
462 .ec = TPACPI_MATCH_ANY, \
463 .quirks = (__quirk) }
465 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
466 { .vendor = PCI_VENDOR_ID_IBM, \
467 .bios = TPACPI_MATCH_ANY, \
468 .ec = TPID(__id1, __id2), \
469 .quirks = (__quirk) }
471 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
472 { .vendor = PCI_VENDOR_ID_LENOVO, \
473 .bios = TPACPI_MATCH_ANY, \
474 .ec = TPID(__id1, __id2), \
475 .quirks = (__quirk) }
477 struct tpacpi_quirk {
481 unsigned long quirks;
485 * tpacpi_check_quirks() - search BIOS/EC version on a list
486 * @qlist: array of &struct tpacpi_quirk
487 * @qlist_size: number of elements in @qlist
489 * Iterates over a quirks list until one is found that matches the
490 * ThinkPad's vendor, BIOS and EC model.
492 * Returns 0 if nothing matches, otherwise returns the quirks field of
493 * the matching &struct tpacpi_quirk entry.
495 * The match criteria is: vendor, ec and bios much match.
497 static unsigned long __init tpacpi_check_quirks(
498 const struct tpacpi_quirk *qlist,
499 unsigned int qlist_size)
502 if ((qlist->vendor == thinkpad_id.vendor ||
503 qlist->vendor == TPACPI_MATCH_ANY) &&
504 (qlist->bios == thinkpad_id.bios_model ||
505 qlist->bios == TPACPI_MATCH_ANY) &&
506 (qlist->ec == thinkpad_id.ec_model ||
507 qlist->ec == TPACPI_MATCH_ANY))
508 return qlist->quirks;
516 static inline bool __pure __init tpacpi_is_lenovo(void)
518 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
521 static inline bool __pure __init tpacpi_is_ibm(void)
523 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
526 /****************************************************************************
527 ****************************************************************************
529 * ACPI Helpers and device model
531 ****************************************************************************
532 ****************************************************************************/
534 /*************************************************************************
538 static acpi_handle root_handle;
539 static acpi_handle ec_handle;
541 #define TPACPI_HANDLE(object, parent, paths...) \
542 static acpi_handle object##_handle; \
543 static const acpi_handle * const object##_parent __initconst = \
545 static char *object##_paths[] __initdata = { paths }
547 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
548 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
550 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
552 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
553 "\\CMS", /* R40, R40e */
556 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
557 "^HKEY", /* R30, R31 */
558 "HKEY", /* all others */
561 /*************************************************************************
565 static int acpi_evalf(acpi_handle handle,
566 int *res, char *method, char *fmt, ...)
569 struct acpi_object_list params;
570 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
571 struct acpi_buffer result, *resultp;
572 union acpi_object out_obj;
580 pr_err("acpi_evalf() called with empty format\n");
593 params.pointer = &in_objs[0];
600 in_objs[params.count].integer.value = va_arg(ap, int);
601 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
603 /* add more types as needed */
605 pr_err("acpi_evalf() called with invalid format character '%c'\n",
613 if (res_type != 'v') {
614 result.length = sizeof(out_obj);
615 result.pointer = &out_obj;
620 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
624 success = (status == AE_OK &&
625 out_obj.type == ACPI_TYPE_INTEGER);
627 *res = out_obj.integer.value;
630 success = status == AE_OK;
632 /* add more types as needed */
634 pr_err("acpi_evalf() called with invalid format character '%c'\n",
639 if (!success && !quiet)
640 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
641 method, fmt0, acpi_format_exception(status));
646 static int acpi_ec_read(int i, u8 *p)
651 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
655 if (ec_read(i, p) < 0)
662 static int acpi_ec_write(int i, u8 v)
665 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
668 if (ec_write(i, v) < 0)
675 static int issue_thinkpad_cmos_command(int cmos_cmd)
680 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
686 /*************************************************************************
690 #define TPACPI_ACPIHANDLE_INIT(object) \
691 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
692 object##_paths, ARRAY_SIZE(object##_paths))
694 static void __init drv_acpi_handle_init(const char *name,
695 acpi_handle *handle, const acpi_handle parent,
696 char **paths, const int num_paths)
701 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
704 for (i = 0; i < num_paths; i++) {
705 status = acpi_get_handle(parent, paths[i], handle);
706 if (ACPI_SUCCESS(status)) {
707 dbg_printk(TPACPI_DBG_INIT,
708 "Found ACPI handle %s for %s\n",
714 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
719 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
720 u32 level, void *context, void **return_value)
722 struct acpi_device *dev;
723 if (!strcmp(context, "video")) {
724 if (acpi_bus_get_device(handle, &dev))
726 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
730 *(acpi_handle *)return_value = handle;
732 return AE_CTRL_TERMINATE;
735 static void __init tpacpi_acpi_handle_locate(const char *name,
740 acpi_handle device_found;
742 BUG_ON(!name || !handle);
743 vdbg_printk(TPACPI_DBG_INIT,
744 "trying to locate ACPI handle for %s, using HID %s\n",
745 name, hid ? hid : "NULL");
747 memset(&device_found, 0, sizeof(device_found));
748 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
749 (void *)name, &device_found);
753 if (ACPI_SUCCESS(status)) {
754 *handle = device_found;
755 dbg_printk(TPACPI_DBG_INIT,
756 "Found ACPI handle for %s\n", name);
758 vdbg_printk(TPACPI_DBG_INIT,
759 "Could not locate an ACPI handle for %s: %s\n",
760 name, acpi_format_exception(status));
764 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
766 struct ibm_struct *ibm = data;
768 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
771 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
774 ibm->acpi->notify(ibm, event);
777 static int __init setup_acpi_notify(struct ibm_struct *ibm)
784 if (!*ibm->acpi->handle)
787 vdbg_printk(TPACPI_DBG_INIT,
788 "setting up ACPI notify for %s\n", ibm->name);
790 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
792 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
796 ibm->acpi->device->driver_data = ibm;
797 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
798 TPACPI_ACPI_EVENT_PREFIX,
801 status = acpi_install_notify_handler(*ibm->acpi->handle,
802 ibm->acpi->type, dispatch_acpi_notify, ibm);
803 if (ACPI_FAILURE(status)) {
804 if (status == AE_ALREADY_EXISTS) {
805 pr_notice("another device driver is already handling %s events\n",
808 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
809 ibm->name, acpi_format_exception(status));
813 ibm->flags.acpi_notify_installed = 1;
817 static int __init tpacpi_device_add(struct acpi_device *device)
822 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
826 dbg_printk(TPACPI_DBG_INIT,
827 "registering %s as an ACPI driver\n", ibm->name);
831 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
832 if (!ibm->acpi->driver) {
833 pr_err("failed to allocate memory for ibm->acpi->driver\n");
837 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
838 ibm->acpi->driver->ids = ibm->acpi->hid;
840 ibm->acpi->driver->ops.add = &tpacpi_device_add;
842 rc = acpi_bus_register_driver(ibm->acpi->driver);
844 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
846 kfree(ibm->acpi->driver);
847 ibm->acpi->driver = NULL;
849 ibm->flags.acpi_driver_registered = 1;
855 /****************************************************************************
856 ****************************************************************************
860 ****************************************************************************
861 ****************************************************************************/
863 static int dispatch_proc_show(struct seq_file *m, void *v)
865 struct ibm_struct *ibm = m->private;
867 if (!ibm || !ibm->read)
872 static int dispatch_proc_open(struct inode *inode, struct file *file)
874 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
877 static ssize_t dispatch_proc_write(struct file *file,
878 const char __user *userbuf,
879 size_t count, loff_t *pos)
881 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
885 if (!ibm || !ibm->write)
887 if (count > PAGE_SIZE - 2)
890 kernbuf = kmalloc(count + 2, GFP_KERNEL);
894 if (copy_from_user(kernbuf, userbuf, count)) {
900 strcat(kernbuf, ",");
901 ret = ibm->write(kernbuf);
910 static const struct proc_ops dispatch_proc_ops = {
911 .proc_open = dispatch_proc_open,
912 .proc_read = seq_read,
913 .proc_lseek = seq_lseek,
914 .proc_release = single_release,
915 .proc_write = dispatch_proc_write,
918 static char *next_cmd(char **cmds)
923 while ((end = strchr(start, ',')) && end == start)
935 /****************************************************************************
936 ****************************************************************************
938 * Device model: input, hwmon and platform
940 ****************************************************************************
941 ****************************************************************************/
943 static struct platform_device *tpacpi_pdev;
944 static struct platform_device *tpacpi_sensors_pdev;
945 static struct device *tpacpi_hwmon;
946 static struct input_dev *tpacpi_inputdev;
947 static struct mutex tpacpi_inputdev_send_mutex;
948 static LIST_HEAD(tpacpi_all_drivers);
950 #ifdef CONFIG_PM_SLEEP
951 static int tpacpi_suspend_handler(struct device *dev)
953 struct ibm_struct *ibm, *itmp;
955 list_for_each_entry_safe(ibm, itmp,
965 static int tpacpi_resume_handler(struct device *dev)
967 struct ibm_struct *ibm, *itmp;
969 list_for_each_entry_safe(ibm, itmp,
980 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
981 tpacpi_suspend_handler, tpacpi_resume_handler);
983 static void tpacpi_shutdown_handler(struct platform_device *pdev)
985 struct ibm_struct *ibm, *itmp;
987 list_for_each_entry_safe(ibm, itmp,
995 static struct platform_driver tpacpi_pdriver = {
997 .name = TPACPI_DRVR_NAME,
1000 .shutdown = tpacpi_shutdown_handler,
1003 static struct platform_driver tpacpi_hwmon_pdriver = {
1005 .name = TPACPI_HWMON_DRVR_NAME,
1009 /*************************************************************************
1010 * sysfs support helpers
1013 struct attribute_set {
1014 unsigned int members, max_members;
1015 struct attribute_group group;
1018 struct attribute_set_obj {
1019 struct attribute_set s;
1020 struct attribute *a;
1021 } __attribute__((packed));
1023 static struct attribute_set *create_attr_set(unsigned int max_members,
1026 struct attribute_set_obj *sobj;
1028 if (max_members == 0)
1031 /* Allocates space for implicit NULL at the end too */
1032 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1033 max_members * sizeof(struct attribute *),
1037 sobj->s.max_members = max_members;
1038 sobj->s.group.attrs = &sobj->a;
1039 sobj->s.group.name = name;
1044 #define destroy_attr_set(_set) \
1047 /* not multi-threaded safe, use it in a single thread per set */
1048 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1053 if (s->members >= s->max_members)
1056 s->group.attrs[s->members] = attr;
1062 static int add_many_to_attr_set(struct attribute_set *s,
1063 struct attribute **attr,
1068 for (i = 0; i < count; i++) {
1069 res = add_to_attr_set(s, attr[i]);
1077 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1079 sysfs_remove_group(kobj, &s->group);
1080 destroy_attr_set(s);
1083 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1084 sysfs_create_group(_kobj, &_attr_set->group)
1086 static int parse_strtoul(const char *buf,
1087 unsigned long max, unsigned long *value)
1091 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1092 endp = skip_spaces(endp);
1093 if (*endp || *value > max)
1099 static void tpacpi_disable_brightness_delay(void)
1101 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1102 pr_notice("ACPI backlight control delay disabled\n");
1105 static void printk_deprecated_attribute(const char * const what,
1106 const char * const details)
1108 tpacpi_log_usertask("deprecated sysfs attribute");
1109 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1113 /*************************************************************************
1114 * rfkill and radio control support helpers
1118 * ThinkPad-ACPI firmware handling model:
1120 * WLSW (master wireless switch) is event-driven, and is common to all
1121 * firmware-controlled radios. It cannot be controlled, just monitored,
1122 * as expected. It overrides all radio state in firmware
1124 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1125 * (TODO: verify how WLSW interacts with the returned radio state).
1127 * The only time there are shadow radio state changes, is when
1128 * masked-off hotkeys are used.
1132 * Internal driver API for radio state:
1134 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1135 * bool: true means radio blocked (off)
1137 enum tpacpi_rfkill_state {
1138 TPACPI_RFK_RADIO_OFF = 0,
1142 /* rfkill switches */
1143 enum tpacpi_rfk_id {
1144 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1145 TPACPI_RFK_WWAN_SW_ID,
1146 TPACPI_RFK_UWB_SW_ID,
1150 static const char *tpacpi_rfkill_names[] = {
1151 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1152 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1153 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1154 [TPACPI_RFK_SW_MAX] = NULL
1157 /* ThinkPad-ACPI rfkill subdriver */
1159 struct rfkill *rfkill;
1160 enum tpacpi_rfk_id id;
1161 const struct tpacpi_rfk_ops *ops;
1164 struct tpacpi_rfk_ops {
1165 /* firmware interface */
1166 int (*get_status)(void);
1167 int (*set_status)(const enum tpacpi_rfkill_state);
1170 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1172 /* Query FW and update rfkill sw state for a given rfkill switch */
1173 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1180 status = (tp_rfk->ops->get_status)();
1184 rfkill_set_sw_state(tp_rfk->rfkill,
1185 (status == TPACPI_RFK_RADIO_OFF));
1190 /* Query FW and update rfkill sw state for all rfkill switches */
1191 static void tpacpi_rfk_update_swstate_all(void)
1195 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1196 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1200 * Sync the HW-blocking state of all rfkill switches,
1201 * do notice it causes the rfkill core to schedule uevents
1203 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1206 struct tpacpi_rfk *tp_rfk;
1208 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1209 tp_rfk = tpacpi_rfkill_switches[i];
1211 if (rfkill_set_hw_state(tp_rfk->rfkill,
1213 /* ignore -- we track sw block */
1219 /* Call to get the WLSW state from the firmware */
1220 static int hotkey_get_wlsw(void);
1222 /* Call to query WLSW state and update all rfkill switches */
1223 static bool tpacpi_rfk_check_hwblock_state(void)
1225 int res = hotkey_get_wlsw();
1228 /* When unknown or unsupported, we have to assume it is unblocked */
1232 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1233 tpacpi_rfk_update_hwblock_state(hw_blocked);
1238 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1240 struct tpacpi_rfk *tp_rfk = data;
1243 dbg_printk(TPACPI_DBG_RFKILL,
1244 "request to change radio state to %s\n",
1245 blocked ? "blocked" : "unblocked");
1247 /* try to set radio state */
1248 res = (tp_rfk->ops->set_status)(blocked ?
1249 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1251 /* and update the rfkill core with whatever the FW really did */
1252 tpacpi_rfk_update_swstate(tp_rfk);
1254 return (res < 0) ? res : 0;
1257 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1258 .set_block = tpacpi_rfk_hook_set_block,
1261 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1262 const struct tpacpi_rfk_ops *tp_rfkops,
1263 const enum rfkill_type rfktype,
1265 const bool set_default)
1267 struct tpacpi_rfk *atp_rfk;
1269 bool sw_state = false;
1273 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1275 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1277 atp_rfk->rfkill = rfkill_alloc(name,
1280 &tpacpi_rfk_rfkill_ops,
1282 if (!atp_rfk || !atp_rfk->rfkill) {
1283 pr_err("failed to allocate memory for rfkill class\n");
1289 atp_rfk->ops = tp_rfkops;
1291 sw_status = (tp_rfkops->get_status)();
1292 if (sw_status < 0) {
1293 pr_err("failed to read initial state for %s, error %d\n",
1296 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1298 /* try to keep the initial state, since we ask the
1299 * firmware to preserve it across S5 in NVRAM */
1300 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1303 hw_state = tpacpi_rfk_check_hwblock_state();
1304 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1306 res = rfkill_register(atp_rfk->rfkill);
1308 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1309 rfkill_destroy(atp_rfk->rfkill);
1314 tpacpi_rfkill_switches[id] = atp_rfk;
1316 pr_info("rfkill switch %s: radio is %sblocked\n",
1317 name, (sw_state || hw_state) ? "" : "un");
1321 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1323 struct tpacpi_rfk *tp_rfk;
1325 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1327 tp_rfk = tpacpi_rfkill_switches[id];
1329 rfkill_unregister(tp_rfk->rfkill);
1330 rfkill_destroy(tp_rfk->rfkill);
1331 tpacpi_rfkill_switches[id] = NULL;
1336 static void printk_deprecated_rfkill_attribute(const char * const what)
1338 printk_deprecated_attribute(what,
1339 "Please switch to generic rfkill before year 2010");
1342 /* sysfs <radio> enable ------------------------------------------------ */
1343 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1344 struct device_attribute *attr,
1349 printk_deprecated_rfkill_attribute(attr->attr.name);
1351 /* This is in the ABI... */
1352 if (tpacpi_rfk_check_hwblock_state()) {
1353 status = TPACPI_RFK_RADIO_OFF;
1355 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1360 return snprintf(buf, PAGE_SIZE, "%d\n",
1361 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1364 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1365 struct device_attribute *attr,
1366 const char *buf, size_t count)
1371 printk_deprecated_rfkill_attribute(attr->attr.name);
1373 if (parse_strtoul(buf, 1, &t))
1376 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1378 /* This is in the ABI... */
1379 if (tpacpi_rfk_check_hwblock_state() && !!t)
1382 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1383 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1384 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1386 return (res < 0) ? res : count;
1389 /* procfs -------------------------------------------------------------- */
1390 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1392 if (id >= TPACPI_RFK_SW_MAX)
1393 seq_printf(m, "status:\t\tnot supported\n");
1397 /* This is in the ABI... */
1398 if (tpacpi_rfk_check_hwblock_state()) {
1399 status = TPACPI_RFK_RADIO_OFF;
1401 status = tpacpi_rfk_update_swstate(
1402 tpacpi_rfkill_switches[id]);
1407 seq_printf(m, "status:\t\t%s\n",
1408 (status == TPACPI_RFK_RADIO_ON) ?
1409 "enabled" : "disabled");
1410 seq_printf(m, "commands:\tenable, disable\n");
1416 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1422 if (id >= TPACPI_RFK_SW_MAX)
1425 while ((cmd = next_cmd(&buf))) {
1426 if (strlencmp(cmd, "enable") == 0)
1427 status = TPACPI_RFK_RADIO_ON;
1428 else if (strlencmp(cmd, "disable") == 0)
1429 status = TPACPI_RFK_RADIO_OFF;
1435 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1436 (status == TPACPI_RFK_RADIO_ON) ?
1437 "enable" : "disable",
1438 tpacpi_rfkill_names[id]);
1439 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1440 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1446 /*************************************************************************
1447 * thinkpad-acpi driver attributes
1450 /* interface_version --------------------------------------------------- */
1451 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1453 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1455 static DRIVER_ATTR_RO(interface_version);
1457 /* debug_level --------------------------------------------------------- */
1458 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1460 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1463 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1468 if (parse_strtoul(buf, 0xffff, &t))
1475 static DRIVER_ATTR_RW(debug_level);
1477 /* version ------------------------------------------------------------- */
1478 static ssize_t version_show(struct device_driver *drv, char *buf)
1480 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1481 TPACPI_DESC, TPACPI_VERSION);
1483 static DRIVER_ATTR_RO(version);
1485 /* --------------------------------------------------------------------- */
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1492 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1495 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1500 if (parse_strtoul(buf, 1, &t))
1503 if (tpacpi_wlsw_emulstate != !!t) {
1504 tpacpi_wlsw_emulstate = !!t;
1505 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1510 static DRIVER_ATTR_RW(wlsw_emulstate);
1512 /* bluetooth_emulstate ------------------------------------------------- */
1513 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1515 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1518 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1519 const char *buf, size_t count)
1523 if (parse_strtoul(buf, 1, &t))
1526 tpacpi_bluetooth_emulstate = !!t;
1530 static DRIVER_ATTR_RW(bluetooth_emulstate);
1532 /* wwan_emulstate ------------------------------------------------- */
1533 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1538 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1543 if (parse_strtoul(buf, 1, &t))
1546 tpacpi_wwan_emulstate = !!t;
1550 static DRIVER_ATTR_RW(wwan_emulstate);
1552 /* uwb_emulstate ------------------------------------------------- */
1553 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1555 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1558 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1563 if (parse_strtoul(buf, 1, &t))
1566 tpacpi_uwb_emulstate = !!t;
1570 static DRIVER_ATTR_RW(uwb_emulstate);
1573 /* --------------------------------------------------------------------- */
1575 static struct driver_attribute *tpacpi_driver_attributes[] = {
1576 &driver_attr_debug_level, &driver_attr_version,
1577 &driver_attr_interface_version,
1580 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1586 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1587 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1591 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1592 if (!res && dbg_wlswemul)
1593 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1594 if (!res && dbg_bluetoothemul)
1595 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1596 if (!res && dbg_wwanemul)
1597 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1598 if (!res && dbg_uwbemul)
1599 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1605 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1609 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1610 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1612 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1613 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1614 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1615 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1616 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1620 /*************************************************************************
1625 * Table of recommended minimum BIOS versions
1627 * Reasons for listing:
1628 * 1. Stable BIOS, listed because the unknown amount of
1629 * bugs and bad ACPI behaviour on older versions
1631 * 2. BIOS or EC fw with known bugs that trigger on Linux
1633 * 3. BIOS with known reduced functionality in older versions
1635 * We recommend the latest BIOS and EC version.
1636 * We only support the latest BIOS and EC fw version as a rule.
1638 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1639 * Information from users in ThinkWiki
1641 * WARNING: we use this table also to detect that the machine is
1642 * a ThinkPad in some cases, so don't remove entries lightly.
1645 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1646 { .vendor = (__v), \
1647 .bios = TPID(__id1, __id2), \
1648 .ec = TPACPI_MATCH_ANY, \
1649 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1650 | TPVER(__bv1, __bv2) }
1652 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1653 __eid, __ev1, __ev2) \
1654 { .vendor = (__v), \
1655 .bios = TPID(__bid1, __bid2), \
1657 .quirks = TPVER(__ev1, __ev2) << 16 \
1658 | TPVER(__bv1, __bv2) }
1660 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1661 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1663 /* Outdated IBM BIOSes often lack the EC id string */
1664 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1665 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1666 __bv1, __bv2, TPID(__id1, __id2), \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1669 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1672 /* Outdated IBM BIOSes often lack the EC id string */
1673 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1674 __eid1, __eid2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1676 __bv1, __bv2, TPID(__eid1, __eid2), \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1679 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1682 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1685 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1687 __bv1, __bv2, TPID(__id1, __id2), \
1690 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1691 __eid1, __eid2, __ev1, __ev2) \
1692 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1693 __bv1, __bv2, TPID(__eid1, __eid2), \
1696 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1697 /* Numeric models ------------------ */
1698 /* FW MODEL BIOS VERS */
1699 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1700 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1701 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1702 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1703 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1704 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1705 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1706 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1707 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1709 /* A-series ------------------------- */
1710 /* FW MODEL BIOS VERS EC VERS */
1711 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1712 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1713 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1714 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1715 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1716 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1717 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1718 TPV_QI0('1', '3', '2', '0'), /* A22m */
1719 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1720 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1721 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1723 /* G-series ------------------------- */
1724 /* FW MODEL BIOS VERS */
1725 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1726 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1728 /* R-series, T-series --------------- */
1729 /* FW MODEL BIOS VERS EC VERS */
1730 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1731 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1732 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1733 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1734 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1735 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1736 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1737 T40/p, T41/p, T42/p (1) */
1738 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1739 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1740 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1741 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1743 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1744 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1745 TPV_QI0('1', '6', '3', '2'), /* T22 */
1746 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1747 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1748 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1750 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1751 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1752 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1754 /* BIOS FW BIOS VERS EC FW EC VERS */
1755 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1756 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1758 /* X-series ------------------------- */
1759 /* FW MODEL BIOS VERS EC VERS */
1760 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1761 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1762 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1763 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1764 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1765 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1766 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1768 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1769 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1771 /* (0) - older versions lack DMI EC fw string and functionality */
1772 /* (1) - older versions known to lack functionality */
1783 static void __init tpacpi_check_outdated_fw(void)
1785 unsigned long fwvers;
1786 u16 ec_version, bios_version;
1788 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1789 ARRAY_SIZE(tpacpi_bios_version_qtable));
1794 bios_version = fwvers & 0xffffU;
1795 ec_version = (fwvers >> 16) & 0xffffU;
1797 /* note that unknown versions are set to 0x0000 and we use that */
1798 if ((bios_version > thinkpad_id.bios_release) ||
1799 (ec_version > thinkpad_id.ec_release &&
1800 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1802 * The changelogs would let us track down the exact
1803 * reason, but it is just too much of a pain to track
1804 * it. We only list BIOSes that are either really
1805 * broken, or really stable to begin with, so it is
1806 * best if the user upgrades the firmware anyway.
1808 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1809 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1813 static bool __init tpacpi_is_fw_known(void)
1815 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1816 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1819 /****************************************************************************
1820 ****************************************************************************
1824 ****************************************************************************
1825 ****************************************************************************/
1827 /*************************************************************************
1828 * thinkpad-acpi metadata subdriver
1831 static int thinkpad_acpi_driver_read(struct seq_file *m)
1833 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1834 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1838 static struct ibm_struct thinkpad_acpi_driver_data = {
1840 .read = thinkpad_acpi_driver_read,
1843 /*************************************************************************
1848 * ThinkPad firmware event model
1850 * The ThinkPad firmware has two main event interfaces: normal ACPI
1851 * notifications (which follow the ACPI standard), and a private event
1854 * The private event interface also issues events for the hotkeys. As
1855 * the driver gained features, the event handling code ended up being
1856 * built around the hotkey subdriver. This will need to be refactored
1857 * to a more formal event API eventually.
1859 * Some "hotkeys" are actually supposed to be used as event reports,
1860 * such as "brightness has changed", "volume has changed", depending on
1861 * the ThinkPad model and how the firmware is operating.
1863 * Unlike other classes, hotkey-class events have mask/unmask control on
1864 * non-ancient firmware. However, how it behaves changes a lot with the
1865 * firmware model and version.
1868 enum { /* hot key scan codes (derived from ACPI DSDT) */
1869 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1870 TP_ACPI_HOTKEYSCAN_FNF2,
1871 TP_ACPI_HOTKEYSCAN_FNF3,
1872 TP_ACPI_HOTKEYSCAN_FNF4,
1873 TP_ACPI_HOTKEYSCAN_FNF5,
1874 TP_ACPI_HOTKEYSCAN_FNF6,
1875 TP_ACPI_HOTKEYSCAN_FNF7,
1876 TP_ACPI_HOTKEYSCAN_FNF8,
1877 TP_ACPI_HOTKEYSCAN_FNF9,
1878 TP_ACPI_HOTKEYSCAN_FNF10,
1879 TP_ACPI_HOTKEYSCAN_FNF11,
1880 TP_ACPI_HOTKEYSCAN_FNF12,
1881 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1882 TP_ACPI_HOTKEYSCAN_FNINSERT,
1883 TP_ACPI_HOTKEYSCAN_FNDELETE,
1884 TP_ACPI_HOTKEYSCAN_FNHOME,
1885 TP_ACPI_HOTKEYSCAN_FNEND,
1886 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1887 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1888 TP_ACPI_HOTKEYSCAN_FNSPACE,
1889 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1890 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1891 TP_ACPI_HOTKEYSCAN_MUTE,
1892 TP_ACPI_HOTKEYSCAN_THINKPAD,
1893 TP_ACPI_HOTKEYSCAN_UNK1,
1894 TP_ACPI_HOTKEYSCAN_UNK2,
1895 TP_ACPI_HOTKEYSCAN_UNK3,
1896 TP_ACPI_HOTKEYSCAN_UNK4,
1897 TP_ACPI_HOTKEYSCAN_UNK5,
1898 TP_ACPI_HOTKEYSCAN_UNK6,
1899 TP_ACPI_HOTKEYSCAN_UNK7,
1900 TP_ACPI_HOTKEYSCAN_UNK8,
1902 /* Adaptive keyboard keycodes */
1903 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1904 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1905 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1906 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1907 TP_ACPI_HOTKEYSCAN_CLOUD,
1908 TP_ACPI_HOTKEYSCAN_UNK9,
1909 TP_ACPI_HOTKEYSCAN_VOICE,
1910 TP_ACPI_HOTKEYSCAN_UNK10,
1911 TP_ACPI_HOTKEYSCAN_GESTURES,
1912 TP_ACPI_HOTKEYSCAN_UNK11,
1913 TP_ACPI_HOTKEYSCAN_UNK12,
1914 TP_ACPI_HOTKEYSCAN_UNK13,
1915 TP_ACPI_HOTKEYSCAN_CONFIG,
1916 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1917 TP_ACPI_HOTKEYSCAN_RELOAD,
1918 TP_ACPI_HOTKEYSCAN_BACK,
1919 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1920 TP_ACPI_HOTKEYSCAN_MIC_UP,
1921 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1922 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1923 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1925 /* Lenovo extended keymap, starting at 0x1300 */
1926 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1927 /* first new observed key (star, favorites) is 0x1311 */
1928 TP_ACPI_HOTKEYSCAN_STAR = 69,
1929 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1930 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1931 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1932 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1933 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1934 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1935 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1936 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1938 /* Hotkey keymap size */
1939 TPACPI_HOTKEY_MAP_LEN
1942 enum { /* Keys/events available through NVRAM polling */
1943 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1944 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1947 enum { /* Positions of some of the keys in hotkey masks */
1948 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1949 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1950 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1951 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1952 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1953 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1954 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1955 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1956 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1957 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1958 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1961 enum { /* NVRAM to ACPI HKEY group map */
1962 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1963 TP_ACPI_HKEY_ZOOM_MASK |
1964 TP_ACPI_HKEY_DISPSWTCH_MASK |
1965 TP_ACPI_HKEY_HIBERNATE_MASK,
1966 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1967 TP_ACPI_HKEY_BRGHTDWN_MASK,
1968 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1969 TP_ACPI_HKEY_VOLDWN_MASK |
1970 TP_ACPI_HKEY_MUTE_MASK,
1973 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1974 struct tp_nvram_state {
1975 u16 thinkpad_toggle:1;
1977 u16 display_toggle:1;
1978 u16 thinklight_toggle:1;
1979 u16 hibernate_toggle:1;
1980 u16 displayexp_toggle:1;
1981 u16 display_state:1;
1982 u16 brightness_toggle:1;
1983 u16 volume_toggle:1;
1986 u8 brightness_level;
1990 /* kthread for the hotkey poller */
1991 static struct task_struct *tpacpi_hotkey_task;
1994 * Acquire mutex to write poller control variables as an
1997 * Increment hotkey_config_change when changing them if you
1998 * want the kthread to forget old state.
2000 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2002 static struct mutex hotkey_thread_data_mutex;
2003 static unsigned int hotkey_config_change;
2006 * hotkey poller control variables
2008 * Must be atomic or readers will also need to acquire mutex
2010 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2011 * should be used only when the changes need to be taken as
2012 * a block, OR when one needs to force the kthread to forget
2015 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2016 static unsigned int hotkey_poll_freq = 10; /* Hz */
2018 #define HOTKEY_CONFIG_CRITICAL_START \
2020 mutex_lock(&hotkey_thread_data_mutex); \
2021 hotkey_config_change++; \
2023 #define HOTKEY_CONFIG_CRITICAL_END \
2024 mutex_unlock(&hotkey_thread_data_mutex);
2026 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2028 #define hotkey_source_mask 0U
2029 #define HOTKEY_CONFIG_CRITICAL_START
2030 #define HOTKEY_CONFIG_CRITICAL_END
2032 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2034 static struct mutex hotkey_mutex;
2036 static enum { /* Reasons for waking up */
2037 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2038 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2039 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2040 } hotkey_wakeup_reason;
2042 static int hotkey_autosleep_ack;
2044 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2045 static u32 hotkey_all_mask; /* all events supported in fw */
2046 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2047 static u32 hotkey_reserved_mask; /* events better left disabled */
2048 static u32 hotkey_driver_mask; /* events needed by the driver */
2049 static u32 hotkey_user_mask; /* events visible to userspace */
2050 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2052 static u16 *hotkey_keycode_map;
2054 static struct attribute_set *hotkey_dev_attributes;
2056 static void tpacpi_driver_event(const unsigned int hkey_event);
2057 static void hotkey_driver_event(const unsigned int scancode);
2058 static void hotkey_poll_setup(const bool may_warn);
2060 /* HKEY.MHKG() return bits */
2061 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2063 TP_ACPI_MULTI_MODE_INVALID = 0,
2064 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2065 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2066 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2067 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2068 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2069 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2070 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2074 /* The following modes are considered tablet mode for the purpose of
2075 * reporting the status to userspace. i.e. in all these modes it makes
2076 * sense to disable the laptop input devices such as touchpad and
2079 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2080 TP_ACPI_MULTI_MODE_STAND |
2081 TP_ACPI_MULTI_MODE_TENT |
2082 TP_ACPI_MULTI_MODE_STAND_TENT,
2085 static int hotkey_get_wlsw(void)
2089 if (!tp_features.hotkey_wlsw)
2092 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2094 return (tpacpi_wlsw_emulstate) ?
2095 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2098 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2101 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2104 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2106 int type = (s >> 16) & 0xffff;
2107 int value = s & 0xffff;
2108 int mode = TP_ACPI_MULTI_MODE_INVALID;
2109 int valid_modes = 0;
2111 if (has_tablet_mode)
2112 *has_tablet_mode = 0;
2116 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2117 TP_ACPI_MULTI_MODE_TABLET |
2118 TP_ACPI_MULTI_MODE_STAND_TENT;
2121 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2122 TP_ACPI_MULTI_MODE_FLAT |
2123 TP_ACPI_MULTI_MODE_TABLET |
2124 TP_ACPI_MULTI_MODE_STAND |
2125 TP_ACPI_MULTI_MODE_TENT;
2128 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2129 TP_ACPI_MULTI_MODE_FLAT;
2133 /* In mode 4, FLAT is not specified as a valid mode. However,
2134 * it can be seen at least on the X1 Yoga 2nd Generation.
2136 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2137 TP_ACPI_MULTI_MODE_FLAT |
2138 TP_ACPI_MULTI_MODE_TABLET |
2139 TP_ACPI_MULTI_MODE_STAND |
2140 TP_ACPI_MULTI_MODE_TENT;
2143 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2144 type, value, TPACPI_MAIL);
2148 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2149 *has_tablet_mode = 1;
2153 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2156 mode = TP_ACPI_MULTI_MODE_FLAT;
2159 mode = TP_ACPI_MULTI_MODE_TABLET;
2163 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2165 mode = TP_ACPI_MULTI_MODE_STAND;
2168 mode = TP_ACPI_MULTI_MODE_TENT;
2171 if (type == 5 && value == 0xffff) {
2172 pr_warn("Multi mode status is undetected, assuming laptop\n");
2177 if (!(mode & valid_modes)) {
2178 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2179 value, type, TPACPI_MAIL);
2183 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2186 static int hotkey_get_tablet_mode(int *status)
2190 switch (tp_features.hotkey_tablet) {
2191 case TP_HOTKEY_TABLET_USES_MHKG:
2192 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2195 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2197 case TP_HOTKEY_TABLET_USES_GMMS:
2198 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2201 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2211 * Reads current event mask from firmware, and updates
2212 * hotkey_acpi_mask accordingly. Also resets any bits
2213 * from hotkey_user_mask that are unavailable to be
2214 * delivered (shadow requirement of the userspace ABI).
2216 * Call with hotkey_mutex held
2218 static int hotkey_mask_get(void)
2220 if (tp_features.hotkey_mask) {
2223 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2226 hotkey_acpi_mask = m;
2228 /* no mask support doesn't mean no event support... */
2229 hotkey_acpi_mask = hotkey_all_mask;
2232 /* sync userspace-visible mask */
2233 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2238 static void hotkey_mask_warn_incomplete_mask(void)
2240 /* log only what the user can fix... */
2241 const u32 wantedmask = hotkey_driver_mask &
2242 ~(hotkey_acpi_mask | hotkey_source_mask) &
2243 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2246 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2250 * Set the firmware mask when supported
2252 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2254 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2256 * Call with hotkey_mutex held
2258 static int hotkey_mask_set(u32 mask)
2263 const u32 fwmask = mask & ~hotkey_source_mask;
2265 if (tp_features.hotkey_mask) {
2266 for (i = 0; i < 32; i++) {
2267 if (!acpi_evalf(hkey_handle,
2268 NULL, "MHKM", "vdd", i + 1,
2269 !!(mask & (1 << i)))) {
2277 * We *must* make an inconditional call to hotkey_mask_get to
2278 * refresh hotkey_acpi_mask and update hotkey_user_mask
2280 * Take the opportunity to also log when we cannot _enable_
2283 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2284 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2285 fwmask, hotkey_acpi_mask);
2288 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2289 hotkey_mask_warn_incomplete_mask();
2295 * Sets hotkey_user_mask and tries to set the firmware mask
2297 * Call with hotkey_mutex held
2299 static int hotkey_user_mask_set(const u32 mask)
2303 /* Give people a chance to notice they are doing something that
2304 * is bound to go boom on their users sooner or later */
2305 if (!tp_warned.hotkey_mask_ff &&
2306 (mask == 0xffff || mask == 0xffffff ||
2307 mask == 0xffffffff)) {
2308 tp_warned.hotkey_mask_ff = 1;
2309 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2311 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2314 /* Try to enable what the user asked for, plus whatever we need.
2315 * this syncs everything but won't enable bits in hotkey_user_mask */
2316 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2318 /* Enable the available bits in hotkey_user_mask */
2319 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2325 * Sets the driver hotkey mask.
2327 * Can be called even if the hotkey subdriver is inactive
2329 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2333 /* Do the right thing if hotkey_init has not been called yet */
2334 if (!tp_features.hotkey) {
2335 hotkey_driver_mask = mask;
2339 mutex_lock(&hotkey_mutex);
2341 HOTKEY_CONFIG_CRITICAL_START
2342 hotkey_driver_mask = mask;
2343 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2344 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2346 HOTKEY_CONFIG_CRITICAL_END
2348 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2349 ~hotkey_source_mask);
2350 hotkey_poll_setup(true);
2352 mutex_unlock(&hotkey_mutex);
2357 static int hotkey_status_get(int *status)
2359 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2365 static int hotkey_status_set(bool enable)
2367 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2373 static void tpacpi_input_send_tabletsw(void)
2377 if (tp_features.hotkey_tablet &&
2378 !hotkey_get_tablet_mode(&state)) {
2379 mutex_lock(&tpacpi_inputdev_send_mutex);
2381 input_report_switch(tpacpi_inputdev,
2382 SW_TABLET_MODE, !!state);
2383 input_sync(tpacpi_inputdev);
2385 mutex_unlock(&tpacpi_inputdev_send_mutex);
2389 /* Do NOT call without validating scancode first */
2390 static void tpacpi_input_send_key(const unsigned int scancode)
2392 const unsigned int keycode = hotkey_keycode_map[scancode];
2394 if (keycode != KEY_RESERVED) {
2395 mutex_lock(&tpacpi_inputdev_send_mutex);
2397 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2398 input_report_key(tpacpi_inputdev, keycode, 1);
2399 input_sync(tpacpi_inputdev);
2401 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2402 input_report_key(tpacpi_inputdev, keycode, 0);
2403 input_sync(tpacpi_inputdev);
2405 mutex_unlock(&tpacpi_inputdev_send_mutex);
2409 /* Do NOT call without validating scancode first */
2410 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2412 hotkey_driver_event(scancode);
2413 if (hotkey_user_mask & (1 << scancode))
2414 tpacpi_input_send_key(scancode);
2417 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2418 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2420 /* Do NOT call without validating scancode first */
2421 static void tpacpi_hotkey_send_key(unsigned int scancode)
2423 tpacpi_input_send_key_masked(scancode);
2426 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2430 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2431 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2432 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2433 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2434 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2435 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2437 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2438 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2439 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2441 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2442 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2443 n->displayexp_toggle =
2444 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2446 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2447 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2448 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2449 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2450 n->brightness_toggle =
2451 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2453 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2454 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2455 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2456 >> TP_NVRAM_POS_LEVEL_VOLUME;
2457 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2458 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2462 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2464 if ((event_mask & (1 << __scancode)) && \
2465 oldn->__member != newn->__member) \
2466 tpacpi_hotkey_send_key(__scancode); \
2469 #define TPACPI_MAY_SEND_KEY(__scancode) \
2471 if (event_mask & (1 << __scancode)) \
2472 tpacpi_hotkey_send_key(__scancode); \
2475 static void issue_volchange(const unsigned int oldvol,
2476 const unsigned int newvol,
2477 const u32 event_mask)
2479 unsigned int i = oldvol;
2481 while (i > newvol) {
2482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2485 while (i < newvol) {
2486 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2491 static void issue_brightnesschange(const unsigned int oldbrt,
2492 const unsigned int newbrt,
2493 const u32 event_mask)
2495 unsigned int i = oldbrt;
2497 while (i > newbrt) {
2498 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2501 while (i < newbrt) {
2502 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2507 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2508 struct tp_nvram_state *newn,
2509 const u32 event_mask)
2512 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2513 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2514 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2515 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2517 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2519 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2524 * This code is supposed to duplicate the IBM firmware behaviour:
2525 * - Pressing MUTE issues mute hotkey message, even when already mute
2526 * - Pressing Volume up/down issues volume up/down hotkey messages,
2527 * even when already at maximum or minimum volume
2528 * - The act of unmuting issues volume up/down notification,
2529 * depending which key was used to unmute
2531 * We are constrained to what the NVRAM can tell us, which is not much
2532 * and certainly not enough if more than one volume hotkey was pressed
2533 * since the last poll cycle.
2535 * Just to make our life interesting, some newer Lenovo ThinkPads have
2536 * bugs in the BIOS and may fail to update volume_toggle properly.
2541 oldn->volume_toggle != newn->volume_toggle ||
2542 oldn->volume_level != newn->volume_level) {
2543 /* recently muted, or repeated mute keypress, or
2544 * multiple presses ending in mute */
2545 issue_volchange(oldn->volume_level, newn->volume_level,
2547 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2552 /* recently unmuted, issue 'unmute' keypress */
2553 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2555 if (oldn->volume_level != newn->volume_level) {
2556 issue_volchange(oldn->volume_level, newn->volume_level,
2558 } else if (oldn->volume_toggle != newn->volume_toggle) {
2559 /* repeated vol up/down keypress at end of scale ? */
2560 if (newn->volume_level == 0)
2561 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2562 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2563 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2567 /* handle brightness */
2568 if (oldn->brightness_level != newn->brightness_level) {
2569 issue_brightnesschange(oldn->brightness_level,
2570 newn->brightness_level, event_mask);
2571 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2572 /* repeated key presses that didn't change state */
2573 if (newn->brightness_level == 0)
2574 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2575 else if (newn->brightness_level >= bright_maxlvl
2576 && !tp_features.bright_unkfw)
2577 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2580 #undef TPACPI_COMPARE_KEY
2581 #undef TPACPI_MAY_SEND_KEY
2587 * We track all events in hotkey_source_mask all the time, since
2588 * most of them are edge-based. We only issue those requested by
2589 * hotkey_user_mask or hotkey_driver_mask, though.
2591 static int hotkey_kthread(void *data)
2593 struct tp_nvram_state s[2];
2594 u32 poll_mask, event_mask;
2595 unsigned int si, so;
2597 unsigned int change_detector;
2598 unsigned int poll_freq;
2601 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2610 /* Initial state for compares */
2611 mutex_lock(&hotkey_thread_data_mutex);
2612 change_detector = hotkey_config_change;
2613 poll_mask = hotkey_source_mask;
2614 event_mask = hotkey_source_mask &
2615 (hotkey_driver_mask | hotkey_user_mask);
2616 poll_freq = hotkey_poll_freq;
2617 mutex_unlock(&hotkey_thread_data_mutex);
2618 hotkey_read_nvram(&s[so], poll_mask);
2620 while (!kthread_should_stop()) {
2622 if (likely(poll_freq))
2625 t = 100; /* should never happen... */
2627 t = msleep_interruptible(t);
2628 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2631 if (t > 0 && !was_frozen)
2634 mutex_lock(&hotkey_thread_data_mutex);
2635 if (was_frozen || hotkey_config_change != change_detector) {
2636 /* forget old state on thaw or config change */
2639 change_detector = hotkey_config_change;
2641 poll_mask = hotkey_source_mask;
2642 event_mask = hotkey_source_mask &
2643 (hotkey_driver_mask | hotkey_user_mask);
2644 poll_freq = hotkey_poll_freq;
2645 mutex_unlock(&hotkey_thread_data_mutex);
2647 if (likely(poll_mask)) {
2648 hotkey_read_nvram(&s[si], poll_mask);
2649 if (likely(si != so)) {
2650 hotkey_compare_and_issue_event(&s[so], &s[si],
2663 /* call with hotkey_mutex held */
2664 static void hotkey_poll_stop_sync(void)
2666 if (tpacpi_hotkey_task) {
2667 kthread_stop(tpacpi_hotkey_task);
2668 tpacpi_hotkey_task = NULL;
2672 /* call with hotkey_mutex held */
2673 static void hotkey_poll_setup(const bool may_warn)
2675 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2676 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2678 if (hotkey_poll_freq > 0 &&
2679 (poll_driver_mask ||
2680 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2681 if (!tpacpi_hotkey_task) {
2682 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2683 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2684 if (IS_ERR(tpacpi_hotkey_task)) {
2685 tpacpi_hotkey_task = NULL;
2686 pr_err("could not create kernel thread for hotkey polling\n");
2690 hotkey_poll_stop_sync();
2691 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2692 hotkey_poll_freq == 0) {
2693 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2694 poll_user_mask, poll_driver_mask);
2699 static void hotkey_poll_setup_safe(const bool may_warn)
2701 mutex_lock(&hotkey_mutex);
2702 hotkey_poll_setup(may_warn);
2703 mutex_unlock(&hotkey_mutex);
2706 /* call with hotkey_mutex held */
2707 static void hotkey_poll_set_freq(unsigned int freq)
2710 hotkey_poll_stop_sync();
2712 hotkey_poll_freq = freq;
2715 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2717 static void hotkey_poll_setup(const bool __unused)
2721 static void hotkey_poll_setup_safe(const bool __unused)
2725 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2727 static int hotkey_inputdev_open(struct input_dev *dev)
2729 switch (tpacpi_lifecycle) {
2730 case TPACPI_LIFE_INIT:
2731 case TPACPI_LIFE_RUNNING:
2732 hotkey_poll_setup_safe(false);
2734 case TPACPI_LIFE_EXITING:
2738 /* Should only happen if tpacpi_lifecycle is corrupt */
2743 static void hotkey_inputdev_close(struct input_dev *dev)
2745 /* disable hotkey polling when possible */
2746 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2747 !(hotkey_source_mask & hotkey_driver_mask))
2748 hotkey_poll_setup_safe(false);
2751 /* sysfs hotkey enable ------------------------------------------------- */
2752 static ssize_t hotkey_enable_show(struct device *dev,
2753 struct device_attribute *attr,
2758 printk_deprecated_attribute("hotkey_enable",
2759 "Hotkey reporting is always enabled");
2761 res = hotkey_status_get(&status);
2765 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2768 static ssize_t hotkey_enable_store(struct device *dev,
2769 struct device_attribute *attr,
2770 const char *buf, size_t count)
2774 printk_deprecated_attribute("hotkey_enable",
2775 "Hotkeys can be disabled through hotkey_mask");
2777 if (parse_strtoul(buf, 1, &t))
2786 static DEVICE_ATTR_RW(hotkey_enable);
2788 /* sysfs hotkey mask --------------------------------------------------- */
2789 static ssize_t hotkey_mask_show(struct device *dev,
2790 struct device_attribute *attr,
2793 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2796 static ssize_t hotkey_mask_store(struct device *dev,
2797 struct device_attribute *attr,
2798 const char *buf, size_t count)
2803 if (parse_strtoul(buf, 0xffffffffUL, &t))
2806 if (mutex_lock_killable(&hotkey_mutex))
2807 return -ERESTARTSYS;
2809 res = hotkey_user_mask_set(t);
2811 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2812 hotkey_poll_setup(true);
2815 mutex_unlock(&hotkey_mutex);
2817 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2819 return (res) ? res : count;
2822 static DEVICE_ATTR_RW(hotkey_mask);
2824 /* sysfs hotkey bios_enabled ------------------------------------------- */
2825 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2826 struct device_attribute *attr,
2829 return sprintf(buf, "0\n");
2832 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2834 /* sysfs hotkey bios_mask ---------------------------------------------- */
2835 static ssize_t hotkey_bios_mask_show(struct device *dev,
2836 struct device_attribute *attr,
2839 printk_deprecated_attribute("hotkey_bios_mask",
2840 "This attribute is useless.");
2841 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2844 static DEVICE_ATTR_RO(hotkey_bios_mask);
2846 /* sysfs hotkey all_mask ----------------------------------------------- */
2847 static ssize_t hotkey_all_mask_show(struct device *dev,
2848 struct device_attribute *attr,
2851 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2852 hotkey_all_mask | hotkey_source_mask);
2855 static DEVICE_ATTR_RO(hotkey_all_mask);
2857 /* sysfs hotkey all_mask ----------------------------------------------- */
2858 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2859 struct device_attribute *attr,
2862 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2863 hotkey_adaptive_all_mask | hotkey_source_mask);
2866 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2868 /* sysfs hotkey recommended_mask --------------------------------------- */
2869 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2870 struct device_attribute *attr,
2873 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2874 (hotkey_all_mask | hotkey_source_mask)
2875 & ~hotkey_reserved_mask);
2878 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2880 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2882 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2883 static ssize_t hotkey_source_mask_show(struct device *dev,
2884 struct device_attribute *attr,
2887 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2890 static ssize_t hotkey_source_mask_store(struct device *dev,
2891 struct device_attribute *attr,
2892 const char *buf, size_t count)
2898 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2899 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2902 if (mutex_lock_killable(&hotkey_mutex))
2903 return -ERESTARTSYS;
2905 HOTKEY_CONFIG_CRITICAL_START
2906 hotkey_source_mask = t;
2907 HOTKEY_CONFIG_CRITICAL_END
2909 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2910 ~hotkey_source_mask);
2911 hotkey_poll_setup(true);
2913 /* check if events needed by the driver got disabled */
2914 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2915 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2917 mutex_unlock(&hotkey_mutex);
2920 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2923 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2926 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2928 return (rc < 0) ? rc : count;
2931 static DEVICE_ATTR_RW(hotkey_source_mask);
2933 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2934 static ssize_t hotkey_poll_freq_show(struct device *dev,
2935 struct device_attribute *attr,
2938 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2941 static ssize_t hotkey_poll_freq_store(struct device *dev,
2942 struct device_attribute *attr,
2943 const char *buf, size_t count)
2947 if (parse_strtoul(buf, 25, &t))
2950 if (mutex_lock_killable(&hotkey_mutex))
2951 return -ERESTARTSYS;
2953 hotkey_poll_set_freq(t);
2954 hotkey_poll_setup(true);
2956 mutex_unlock(&hotkey_mutex);
2958 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2963 static DEVICE_ATTR_RW(hotkey_poll_freq);
2965 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2967 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2968 static ssize_t hotkey_radio_sw_show(struct device *dev,
2969 struct device_attribute *attr,
2973 res = hotkey_get_wlsw();
2977 /* Opportunistic update */
2978 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2980 return snprintf(buf, PAGE_SIZE, "%d\n",
2981 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2984 static DEVICE_ATTR_RO(hotkey_radio_sw);
2986 static void hotkey_radio_sw_notify_change(void)
2988 if (tp_features.hotkey_wlsw)
2989 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2993 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2994 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2995 struct device_attribute *attr,
2999 res = hotkey_get_tablet_mode(&s);
3003 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3006 static DEVICE_ATTR_RO(hotkey_tablet_mode);
3008 static void hotkey_tablet_mode_notify_change(void)
3010 if (tp_features.hotkey_tablet)
3011 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3012 "hotkey_tablet_mode");
3015 /* sysfs wakeup reason (pollable) -------------------------------------- */
3016 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3017 struct device_attribute *attr,
3020 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3023 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3025 static void hotkey_wakeup_reason_notify_change(void)
3027 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3031 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3032 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3033 struct device_attribute *attr,
3036 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3039 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3040 hotkey_wakeup_hotunplug_complete_show, NULL);
3042 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3044 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3045 "wakeup_hotunplug_complete");
3048 /* sysfs adaptive kbd mode --------------------------------------------- */
3050 static int adaptive_keyboard_get_mode(void);
3051 static int adaptive_keyboard_set_mode(int new_mode);
3053 enum ADAPTIVE_KEY_MODE {
3056 WEB_CONFERENCE_MODE,
3061 static ssize_t adaptive_kbd_mode_show(struct device *dev,
3062 struct device_attribute *attr,
3067 current_mode = adaptive_keyboard_get_mode();
3068 if (current_mode < 0)
3069 return current_mode;
3071 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3074 static ssize_t adaptive_kbd_mode_store(struct device *dev,
3075 struct device_attribute *attr,
3076 const char *buf, size_t count)
3081 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3084 res = adaptive_keyboard_set_mode(t);
3085 return (res < 0) ? res : count;
3088 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3090 static struct attribute *adaptive_kbd_attributes[] = {
3091 &dev_attr_adaptive_kbd_mode.attr,
3095 static const struct attribute_group adaptive_kbd_attr_group = {
3096 .attrs = adaptive_kbd_attributes,
3099 /* --------------------------------------------------------------------- */
3101 static struct attribute *hotkey_attributes[] __initdata = {
3102 &dev_attr_hotkey_enable.attr,
3103 &dev_attr_hotkey_bios_enabled.attr,
3104 &dev_attr_hotkey_bios_mask.attr,
3105 &dev_attr_wakeup_reason.attr,
3106 &dev_attr_wakeup_hotunplug_complete.attr,
3107 &dev_attr_hotkey_mask.attr,
3108 &dev_attr_hotkey_all_mask.attr,
3109 &dev_attr_hotkey_adaptive_all_mask.attr,
3110 &dev_attr_hotkey_recommended_mask.attr,
3111 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3112 &dev_attr_hotkey_source_mask.attr,
3113 &dev_attr_hotkey_poll_freq.attr,
3118 * Sync both the hw and sw blocking state of all switches
3120 static void tpacpi_send_radiosw_update(void)
3125 * We must sync all rfkill controllers *before* issuing any
3126 * rfkill input events, or we will race the rfkill core input
3129 * tpacpi_inputdev_send_mutex works as a synchronization point
3132 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3135 wlsw = hotkey_get_wlsw();
3137 /* Sync hw blocking state first if it is hw-blocked */
3138 if (wlsw == TPACPI_RFK_RADIO_OFF)
3139 tpacpi_rfk_update_hwblock_state(true);
3141 /* Sync sw blocking state */
3142 tpacpi_rfk_update_swstate_all();
3144 /* Sync hw blocking state last if it is hw-unblocked */
3145 if (wlsw == TPACPI_RFK_RADIO_ON)
3146 tpacpi_rfk_update_hwblock_state(false);
3148 /* Issue rfkill input event for WLSW switch */
3150 mutex_lock(&tpacpi_inputdev_send_mutex);
3152 input_report_switch(tpacpi_inputdev,
3153 SW_RFKILL_ALL, (wlsw > 0));
3154 input_sync(tpacpi_inputdev);
3156 mutex_unlock(&tpacpi_inputdev_send_mutex);
3160 * this can be unconditional, as we will poll state again
3161 * if userspace uses the notify to read data
3163 hotkey_radio_sw_notify_change();
3166 static void hotkey_exit(void)
3168 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3169 mutex_lock(&hotkey_mutex);
3170 hotkey_poll_stop_sync();
3171 mutex_unlock(&hotkey_mutex);
3174 if (hotkey_dev_attributes)
3175 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3177 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3178 "restoring original HKEY status and mask\n");
3179 /* yes, there is a bitwise or below, we want the
3180 * functions to be called even if one of them fail */
3181 if (((tp_features.hotkey_mask &&
3182 hotkey_mask_set(hotkey_orig_mask)) |
3183 hotkey_status_set(false)) != 0)
3184 pr_err("failed to restore hot key mask to BIOS defaults\n");
3187 static void __init hotkey_unmap(const unsigned int scancode)
3189 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3190 clear_bit(hotkey_keycode_map[scancode],
3191 tpacpi_inputdev->keybit);
3192 hotkey_keycode_map[scancode] = KEY_RESERVED;
3198 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3201 #define TPACPI_HK_Q_INIMASK 0x0001
3203 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3204 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3205 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3206 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3207 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3208 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3209 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3210 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3211 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3212 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3213 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3214 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3215 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3216 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3217 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3218 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3219 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3220 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3221 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3222 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3225 typedef u16 tpacpi_keymap_entry_t;
3226 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3228 static int hotkey_init_tablet_mode(void)
3230 int in_tablet_mode = 0, res;
3233 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3234 int has_tablet_mode;
3236 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3238 if (has_tablet_mode)
3239 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3241 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3242 /* For X41t, X60t, X61t Tablets... */
3243 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3244 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3248 if (!tp_features.hotkey_tablet)
3251 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3252 type, in_tablet_mode ? "tablet" : "laptop");
3254 res = add_to_attr_set(hotkey_dev_attributes,
3255 &dev_attr_hotkey_tablet_mode.attr);
3259 return in_tablet_mode;
3262 static int __init hotkey_init(struct ibm_init_struct *iibm)
3264 /* Requirements for changing the default keymaps:
3266 * 1. Many of the keys are mapped to KEY_RESERVED for very
3267 * good reasons. Do not change them unless you have deep
3268 * knowledge on the IBM and Lenovo ThinkPad firmware for
3269 * the various ThinkPad models. The driver behaves
3270 * differently for KEY_RESERVED: such keys have their
3271 * hot key mask *unset* in mask_recommended, and also
3272 * in the initial hot key mask programmed into the
3273 * firmware at driver load time, which means the firm-
3274 * ware may react very differently if you change them to
3277 * 2. You must be subscribed to the linux-thinkpad and
3278 * ibm-acpi-devel mailing lists, and you should read the
3279 * list archives since 2007 if you want to change the
3280 * keymaps. This requirement exists so that you will
3281 * know the past history of problems with the thinkpad-
3282 * acpi driver keymaps, and also that you will be
3283 * listening to any bug reports;
3285 * 3. Do not send thinkpad-acpi specific patches directly to
3286 * for merging, *ever*. Send them to the linux-acpi
3287 * mailinglist for comments. Merging is to be done only
3288 * through acpi-test and the ACPI maintainer.
3290 * If the above is too much to ask, don't change the keymap.
3291 * Ask the thinkpad-acpi maintainer to do it, instead.
3295 TPACPI_KEYMAP_IBM_GENERIC = 0,
3296 TPACPI_KEYMAP_LENOVO_GENERIC,
3299 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3300 /* Generic keymap for IBM ThinkPads */
3301 [TPACPI_KEYMAP_IBM_GENERIC] = {
3302 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3303 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3304 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3305 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3307 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3308 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3309 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3310 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3312 /* brightness: firmware always reacts to them */
3313 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3314 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3316 /* Thinklight: firmware always react to it */
3317 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3319 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3320 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3322 /* Volume: firmware always react to it and reprograms
3323 * the built-in *extra* mixer. Never map it to control
3324 * another mixer by default. */
3325 KEY_RESERVED, /* 0x14: VOLUME UP */
3326 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3327 KEY_RESERVED, /* 0x16: MUTE */
3329 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3331 /* (assignments unknown, please report if found) */
3332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3335 /* No assignments, only used for Adaptive keyboards. */
3336 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3337 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3338 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3342 /* No assignment, used for newer Lenovo models */
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3345 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3346 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3347 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3348 KEY_UNKNOWN, KEY_UNKNOWN
3352 /* Generic keymap for Lenovo ThinkPads */
3353 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3354 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3355 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3356 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3357 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3359 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3360 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3361 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3362 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3364 /* These should be enabled --only-- when ACPI video
3365 * is disabled (i.e. in "vendor" mode), and are handled
3366 * in a special way by the init code */
3367 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3368 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3370 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3372 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3373 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3375 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3376 * react to it and reprograms the built-in *extra* mixer.
3377 * Never map it to control another mixer by default.
3379 * T60?, T61, R60?, R61: firmware and EC tries to send
3380 * these over the regular keyboard, so these are no-ops,
3381 * but there are still weird bugs re. MUTE, so do not
3382 * change unless you get test reports from all Lenovo
3383 * models. May cause the BIOS to interfere with the
3386 KEY_RESERVED, /* 0x14: VOLUME UP */
3387 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3388 KEY_RESERVED, /* 0x16: MUTE */
3390 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3392 /* (assignments unknown, please report if found) */
3393 KEY_UNKNOWN, KEY_UNKNOWN,
3396 * The mic mute button only sends 0x1a. It does not
3397 * automatically mute the mic or change the mute light.
3399 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3401 /* (assignments unknown, please report if found) */
3404 /* Extra keys in use since the X240 / T440 / T540 */
3405 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3408 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3409 * The first item in this list is the Mute button which is
3410 * emitted with 0x103 through
3411 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3413 * We'll need to offset those by 0x20.
3415 KEY_RESERVED, /* Mute held, 0x103 */
3416 KEY_BRIGHTNESS_MIN, /* Backlight off */
3417 KEY_RESERVED, /* Clipping tool */
3418 KEY_RESERVED, /* Cloud */
3420 KEY_VOICECOMMAND, /* Voice */
3422 KEY_RESERVED, /* Gestures */
3426 KEY_CONFIG, /* Settings */
3427 KEY_RESERVED, /* New tab */
3428 KEY_REFRESH, /* Reload */
3429 KEY_BACK, /* Back */
3430 KEY_RESERVED, /* Microphone down */
3431 KEY_RESERVED, /* Microphone up */
3432 KEY_RESERVED, /* Microphone cancellation */
3433 KEY_RESERVED, /* Camera mode */
3434 KEY_RESERVED, /* Rotate display, 0x116 */
3437 * These are found in 2017 models (e.g. T470s, X270).
3438 * The lowest known value is 0x311, which according to
3439 * the manual should launch a user defined favorite
3442 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3443 * corresponding to 0x34.
3446 /* (assignments unknown, please report if found) */
3447 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3448 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3449 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3450 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3453 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3454 KEY_RESERVED, /* Clipping tool */
3455 KEY_CALC, /* Calculator (above numpad, P52) */
3456 KEY_BLUETOOTH, /* Bluetooth */
3457 KEY_KEYBOARD, /* Keyboard, 0x315 */
3458 KEY_FN_RIGHT_SHIFT, /* Fn + right Shift */
3459 KEY_NOTIFICATION_CENTER, /* Notification Center */
3460 KEY_PICKUP_PHONE, /* Answer incoming call */
3461 KEY_HANGUP_PHONE, /* Decline incoming call */
3465 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3466 /* Generic maps (fallback) */
3468 .vendor = PCI_VENDOR_ID_IBM,
3469 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3470 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3473 .vendor = PCI_VENDOR_ID_LENOVO,
3474 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3475 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3479 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3480 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3485 bool radiosw_state = false;
3486 bool tabletsw_state = false;
3488 unsigned long quirks;
3489 unsigned long keymap_id;
3491 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3492 "initializing hotkey subdriver\n");
3494 BUG_ON(!tpacpi_inputdev);
3495 BUG_ON(tpacpi_inputdev->open != NULL ||
3496 tpacpi_inputdev->close != NULL);
3498 TPACPI_ACPIHANDLE_INIT(hkey);
3499 mutex_init(&hotkey_mutex);
3501 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3502 mutex_init(&hotkey_thread_data_mutex);
3505 /* hotkey not supported on 570 */
3506 tp_features.hotkey = hkey_handle != NULL;
3508 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3510 str_supported(tp_features.hotkey));
3512 if (!tp_features.hotkey)
3515 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3516 ARRAY_SIZE(tpacpi_hotkey_qtable));
3518 tpacpi_disable_brightness_delay();
3520 /* MUST have enough space for all attributes to be added to
3521 * hotkey_dev_attributes */
3522 hotkey_dev_attributes = create_attr_set(
3523 ARRAY_SIZE(hotkey_attributes) + 2,
3525 if (!hotkey_dev_attributes)
3527 res = add_many_to_attr_set(hotkey_dev_attributes,
3529 ARRAY_SIZE(hotkey_attributes));
3533 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3534 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3535 for HKEY interface version 0x100 */
3536 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3537 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3538 "firmware HKEY interface version: 0x%x\n",
3541 switch (hkeyv >> 8) {
3544 * MHKV 0x100 in A31, R40, R40e,
3545 * T4x, X31, and later
3548 /* Paranoia check AND init hotkey_all_mask */
3549 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3551 pr_err("missing MHKA handler, please report this to %s\n",
3553 /* Fallback: pre-init for FN+F3,F4,F12 */
3554 hotkey_all_mask = 0x080cU;
3556 tp_features.hotkey_mask = 1;
3562 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3565 /* Paranoia check AND init hotkey_all_mask */
3566 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3568 pr_err("missing MHKA handler, please report this to %s\n",
3570 /* Fallback: pre-init for FN+F3,F4,F12 */
3571 hotkey_all_mask = 0x080cU;
3573 tp_features.hotkey_mask = 1;
3577 * Check if we have an adaptive keyboard, like on the
3578 * Lenovo Carbon X1 2014 (2nd Gen).
3580 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3582 if (hotkey_adaptive_all_mask != 0) {
3583 tp_features.has_adaptive_kbd = true;
3584 res = sysfs_create_group(
3585 &tpacpi_pdev->dev.kobj,
3586 &adaptive_kbd_attr_group);
3591 tp_features.has_adaptive_kbd = false;
3592 hotkey_adaptive_all_mask = 0x0U;
3597 pr_err("unknown version of the HKEY interface: 0x%x\n",
3599 pr_err("please report this to %s\n", TPACPI_MAIL);
3604 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3605 "hotkey masks are %s\n",
3606 str_supported(tp_features.hotkey_mask));
3608 /* Init hotkey_all_mask if not initialized yet */
3609 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3610 (quirks & TPACPI_HK_Q_INIMASK))
3611 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3613 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3614 if (tp_features.hotkey_mask) {
3615 /* hotkey_source_mask *must* be zero for
3616 * the first hotkey_mask_get to return hotkey_orig_mask */
3617 res = hotkey_mask_get();
3621 hotkey_orig_mask = hotkey_acpi_mask;
3623 hotkey_orig_mask = hotkey_all_mask;
3624 hotkey_acpi_mask = hotkey_all_mask;
3627 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3629 tp_features.hotkey_wlsw = 1;
3630 radiosw_state = !!tpacpi_wlsw_emulstate;
3631 pr_info("radio switch emulation enabled\n");
3634 /* Not all thinkpads have a hardware radio switch */
3635 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3636 tp_features.hotkey_wlsw = 1;
3637 radiosw_state = !!status;
3638 pr_info("radio switch found; radios are %s\n",
3639 enabled(status, 0));
3641 if (tp_features.hotkey_wlsw)
3642 res = add_to_attr_set(hotkey_dev_attributes,
3643 &dev_attr_hotkey_radio_sw.attr);
3645 res = hotkey_init_tablet_mode();
3649 tabletsw_state = res;
3651 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3652 &tpacpi_pdev->dev.kobj);
3656 /* Set up key map */
3657 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3658 ARRAY_SIZE(tpacpi_keymap_qtable));
3659 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3660 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3661 "using keymap number %lu\n", keymap_id);
3663 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3664 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3665 if (!hotkey_keycode_map) {
3666 pr_err("failed to allocate memory for key map\n");
3671 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3672 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3673 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3674 tpacpi_inputdev->keycode = hotkey_keycode_map;
3675 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3676 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3677 input_set_capability(tpacpi_inputdev, EV_KEY,
3678 hotkey_keycode_map[i]);
3680 if (i < sizeof(hotkey_reserved_mask)*8)
3681 hotkey_reserved_mask |= 1 << i;
3685 if (tp_features.hotkey_wlsw) {
3686 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3687 input_report_switch(tpacpi_inputdev,
3688 SW_RFKILL_ALL, radiosw_state);
3690 if (tp_features.hotkey_tablet) {
3691 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3692 input_report_switch(tpacpi_inputdev,
3693 SW_TABLET_MODE, tabletsw_state);
3696 /* Do not issue duplicate brightness change events to
3697 * userspace. tpacpi_detect_brightness_capabilities() must have
3698 * been called before this point */
3699 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3700 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3701 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3703 /* Disable brightness up/down on Lenovo thinkpads when
3704 * ACPI is handling them, otherwise it is plain impossible
3705 * for userspace to do something even remotely sane */
3706 hotkey_reserved_mask |=
3707 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3708 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3709 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3710 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3713 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3714 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3716 & ~hotkey_reserved_mask;
3718 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3719 "hotkey source mask 0x%08x, polling freq %u\n",
3720 hotkey_source_mask, hotkey_poll_freq);
3723 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3724 "enabling firmware HKEY event interface...\n");
3725 res = hotkey_status_set(true);
3730 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3731 | hotkey_driver_mask)
3732 & ~hotkey_source_mask);
3733 if (res < 0 && res != -ENXIO) {
3737 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3738 & ~hotkey_reserved_mask;
3739 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3740 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3741 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3743 tpacpi_inputdev->open = &hotkey_inputdev_open;
3744 tpacpi_inputdev->close = &hotkey_inputdev_close;
3746 hotkey_poll_setup_safe(true);
3751 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3752 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3753 &adaptive_kbd_attr_group);
3755 hotkey_dev_attributes = NULL;
3757 return (res < 0) ? res : 1;
3760 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3761 * mode, Web conference mode, Function mode and Lay-flat mode.
3762 * We support Home mode and Function mode currently.
3764 * Will consider support rest of modes in future.
3767 static const int adaptive_keyboard_modes[] = {
3769 /* WEB_BROWSER_MODE = 2,
3770 WEB_CONFERENCE_MODE = 3, */
3774 #define DFR_CHANGE_ROW 0x101
3775 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3776 #define FIRST_ADAPTIVE_KEY 0x103
3778 /* press Fn key a while second, it will switch to Function Mode. Then
3779 * release Fn key, previous mode be restored.
3781 static bool adaptive_keyboard_mode_is_saved;
3782 static int adaptive_keyboard_prev_mode;
3784 static int adaptive_keyboard_get_mode(void)
3788 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3789 pr_err("Cannot read adaptive keyboard mode\n");
3796 static int adaptive_keyboard_set_mode(int new_mode)
3799 new_mode > LAYFLAT_MODE)
3802 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3803 pr_err("Cannot set adaptive keyboard mode\n");
3810 static int adaptive_keyboard_get_next_mode(int mode)
3813 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3815 for (i = 0; i <= max_mode; i++) {
3816 if (adaptive_keyboard_modes[i] == mode)
3825 return adaptive_keyboard_modes[i];
3828 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3830 int current_mode = 0;
3835 case DFR_CHANGE_ROW:
3836 if (adaptive_keyboard_mode_is_saved) {
3837 new_mode = adaptive_keyboard_prev_mode;
3838 adaptive_keyboard_mode_is_saved = false;
3840 current_mode = adaptive_keyboard_get_mode();
3841 if (current_mode < 0)
3843 new_mode = adaptive_keyboard_get_next_mode(
3847 if (adaptive_keyboard_set_mode(new_mode) < 0)
3852 case DFR_SHOW_QUICKVIEW_ROW:
3853 current_mode = adaptive_keyboard_get_mode();
3854 if (current_mode < 0)
3857 adaptive_keyboard_prev_mode = current_mode;
3858 adaptive_keyboard_mode_is_saved = true;
3860 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3865 if (scancode < FIRST_ADAPTIVE_KEY ||
3866 scancode >= FIRST_ADAPTIVE_KEY +
3867 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3868 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3869 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3873 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3874 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3875 if (keycode != KEY_RESERVED) {
3876 mutex_lock(&tpacpi_inputdev_send_mutex);
3878 input_report_key(tpacpi_inputdev, keycode, 1);
3879 input_sync(tpacpi_inputdev);
3881 input_report_key(tpacpi_inputdev, keycode, 0);
3882 input_sync(tpacpi_inputdev);
3884 mutex_unlock(&tpacpi_inputdev_send_mutex);
3890 static bool hotkey_notify_hotkey(const u32 hkey,
3892 bool *ignore_acpi_ev)
3894 /* 0x1000-0x1FFF: key presses */
3895 unsigned int scancode = hkey & 0xfff;
3896 *send_acpi_ev = true;
3897 *ignore_acpi_ev = false;
3900 * Original events are in the 0x10XX range, the adaptive keyboard
3901 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3902 * models, additional keys are emitted through 0x13XX.
3904 switch ((hkey >> 8) & 0xf) {
3907 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3908 /* HKEY event 0x1001 is scancode 0x00 */
3910 if (!(hotkey_source_mask & (1 << scancode))) {
3911 tpacpi_input_send_key_masked(scancode);
3912 *send_acpi_ev = false;
3914 *ignore_acpi_ev = true;
3921 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3924 /* Extended keycodes start at 0x300 and our offset into the map
3925 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3926 * will be positive, but might not be in the correct range.
3928 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3929 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3930 scancode < TPACPI_HOTKEY_MAP_LEN) {
3931 tpacpi_input_send_key(scancode);
3940 static bool hotkey_notify_wakeup(const u32 hkey,
3942 bool *ignore_acpi_ev)
3944 /* 0x2000-0x2FFF: Wakeup reason */
3945 *send_acpi_ev = true;
3946 *ignore_acpi_ev = false;
3949 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3950 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3951 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3952 *ignore_acpi_ev = true;
3955 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3956 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3957 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3958 *ignore_acpi_ev = true;
3961 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3962 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3963 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3964 /* how to auto-heal: */
3965 /* 2313: woke up from S3, go to S4/S5 */
3966 /* 2413: woke up from S4, go to S5 */
3973 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3974 pr_info("woke up due to a hot-unplug request...\n");
3975 hotkey_wakeup_reason_notify_change();
3980 static bool hotkey_notify_dockevent(const u32 hkey,
3982 bool *ignore_acpi_ev)
3984 /* 0x4000-0x4FFF: dock-related events */
3985 *send_acpi_ev = true;
3986 *ignore_acpi_ev = false;
3989 case TP_HKEY_EV_UNDOCK_ACK:
3990 /* ACPI undock operation completed after wakeup */
3991 hotkey_autosleep_ack = 1;
3992 pr_info("undocked\n");
3993 hotkey_wakeup_hotunplug_complete_notify_change();
3996 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3997 pr_info("docked into hotplug port replicator\n");
3999 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
4000 pr_info("undocked from hotplug port replicator\n");
4008 static bool hotkey_notify_usrevent(const u32 hkey,
4010 bool *ignore_acpi_ev)
4012 /* 0x5000-0x5FFF: human interface helpers */
4013 *send_acpi_ev = true;
4014 *ignore_acpi_ev = false;
4017 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4018 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
4021 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4022 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4023 tpacpi_input_send_tabletsw();
4024 hotkey_tablet_mode_notify_change();
4025 *send_acpi_ev = false;
4028 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4029 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4030 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
4031 /* do not propagate these events */
4032 *ignore_acpi_ev = true;
4040 static void thermal_dump_all_sensors(void);
4042 static bool hotkey_notify_6xxx(const u32 hkey,
4044 bool *ignore_acpi_ev)
4046 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4047 *send_acpi_ev = true;
4048 *ignore_acpi_ev = false;
4051 case TP_HKEY_EV_THM_TABLE_CHANGED:
4052 pr_debug("EC reports: Thermal Table has changed\n");
4053 /* recommended action: do nothing, we don't have
4054 * Lenovo ATM information */
4056 case TP_HKEY_EV_THM_CSM_COMPLETED:
4057 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4058 /* recommended action: do nothing, we don't have
4059 * Lenovo ATM information */
4061 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4062 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
4063 /* recommended action: do nothing, we don't have
4064 * Lenovo ATM information */
4066 case TP_HKEY_EV_ALARM_BAT_HOT:
4067 pr_crit("THERMAL ALARM: battery is too hot!\n");
4068 /* recommended action: warn user through gui */
4070 case TP_HKEY_EV_ALARM_BAT_XHOT:
4071 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4072 /* recommended action: immediate sleep/hibernate */
4074 case TP_HKEY_EV_ALARM_SENSOR_HOT:
4075 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4076 /* recommended action: warn user through gui, that */
4077 /* some internal component is too hot */
4079 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
4080 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4081 /* recommended action: immediate sleep/hibernate */
4083 case TP_HKEY_EV_AC_CHANGED:
4084 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4085 * AC status changed; can be triggered by plugging or
4086 * unplugging AC adapter, docking or undocking. */
4090 case TP_HKEY_EV_KEY_NUMLOCK:
4091 case TP_HKEY_EV_KEY_FN:
4092 case TP_HKEY_EV_KEY_FN_ESC:
4093 /* key press events, we just ignore them as long as the EC
4094 * is still reporting them in the normal keyboard stream */
4095 *send_acpi_ev = false;
4096 *ignore_acpi_ev = true;
4099 case TP_HKEY_EV_TABLET_CHANGED:
4100 tpacpi_input_send_tabletsw();
4101 hotkey_tablet_mode_notify_change();
4102 *send_acpi_ev = false;
4105 case TP_HKEY_EV_PALM_DETECTED:
4106 case TP_HKEY_EV_PALM_UNDETECTED:
4107 /* palm detected hovering the keyboard, forward to user-space
4108 * via netlink for consumption */
4112 /* report simply as unknown, no sensor dump */
4116 thermal_dump_all_sensors();
4120 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4124 bool ignore_acpi_ev;
4127 if (event != 0x80) {
4128 pr_err("unknown HKEY notification event %d\n", event);
4129 /* forward it to userspace, maybe it knows how to handle it */
4130 acpi_bus_generate_netlink_event(
4131 ibm->acpi->device->pnp.device_class,
4132 dev_name(&ibm->acpi->device->dev),
4138 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4139 pr_err("failed to retrieve HKEY event\n");
4148 send_acpi_ev = true;
4149 ignore_acpi_ev = false;
4151 switch (hkey >> 12) {
4153 /* 0x1000-0x1FFF: key presses */
4154 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4158 /* 0x2000-0x2FFF: Wakeup reason */
4159 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4163 /* 0x3000-0x3FFF: bay-related wakeups */
4165 case TP_HKEY_EV_BAYEJ_ACK:
4166 hotkey_autosleep_ack = 1;
4167 pr_info("bay ejected\n");
4168 hotkey_wakeup_hotunplug_complete_notify_change();
4171 case TP_HKEY_EV_OPTDRV_EJ:
4172 /* FIXME: kick libata if SATA link offline */
4180 /* 0x4000-0x4FFF: dock-related events */
4181 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4185 /* 0x5000-0x5FFF: human interface helpers */
4186 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4190 /* 0x6000-0x6FFF: thermal alarms/notices and
4191 * keyboard events */
4192 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4196 /* 0x7000-0x7FFF: misc */
4197 if (tp_features.hotkey_wlsw &&
4198 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4199 tpacpi_send_radiosw_update();
4204 /* fallthrough - to default */
4209 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4210 pr_notice("please report the conditions when this event happened to %s\n",
4214 /* netlink events */
4215 if (!ignore_acpi_ev && send_acpi_ev) {
4216 acpi_bus_generate_netlink_event(
4217 ibm->acpi->device->pnp.device_class,
4218 dev_name(&ibm->acpi->device->dev),
4224 static void hotkey_suspend(void)
4226 /* Do these on suspend, we get the events on early resume! */
4227 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4228 hotkey_autosleep_ack = 0;
4230 /* save previous mode of adaptive keyboard of X1 Carbon */
4231 if (tp_features.has_adaptive_kbd) {
4232 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4234 pr_err("Cannot read adaptive keyboard mode.\n");
4239 static void hotkey_resume(void)
4241 tpacpi_disable_brightness_delay();
4243 if (hotkey_status_set(true) < 0 ||
4244 hotkey_mask_set(hotkey_acpi_mask) < 0)
4245 pr_err("error while attempting to reset the event firmware interface\n");
4247 tpacpi_send_radiosw_update();
4248 hotkey_tablet_mode_notify_change();
4249 hotkey_wakeup_reason_notify_change();
4250 hotkey_wakeup_hotunplug_complete_notify_change();
4251 hotkey_poll_setup_safe(false);
4253 /* restore previous mode of adapive keyboard of X1 Carbon */
4254 if (tp_features.has_adaptive_kbd) {
4255 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4256 adaptive_keyboard_prev_mode)) {
4257 pr_err("Cannot set adaptive keyboard mode.\n");
4262 /* procfs -------------------------------------------------------------- */
4263 static int hotkey_read(struct seq_file *m)
4267 if (!tp_features.hotkey) {
4268 seq_printf(m, "status:\t\tnot supported\n");
4272 if (mutex_lock_killable(&hotkey_mutex))
4273 return -ERESTARTSYS;
4274 res = hotkey_status_get(&status);
4276 res = hotkey_mask_get();
4277 mutex_unlock(&hotkey_mutex);
4281 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4282 if (hotkey_all_mask) {
4283 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4284 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4286 seq_printf(m, "mask:\t\tnot supported\n");
4287 seq_printf(m, "commands:\tenable, disable, reset\n");
4293 static void hotkey_enabledisable_warn(bool enable)
4295 tpacpi_log_usertask("procfs hotkey enable/disable");
4296 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4297 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4298 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4301 static int hotkey_write(char *buf)
4307 if (!tp_features.hotkey)
4310 if (mutex_lock_killable(&hotkey_mutex))
4311 return -ERESTARTSYS;
4313 mask = hotkey_user_mask;
4316 while ((cmd = next_cmd(&buf))) {
4317 if (strlencmp(cmd, "enable") == 0) {
4318 hotkey_enabledisable_warn(1);
4319 } else if (strlencmp(cmd, "disable") == 0) {
4320 hotkey_enabledisable_warn(0);
4322 } else if (strlencmp(cmd, "reset") == 0) {
4323 mask = (hotkey_all_mask | hotkey_source_mask)
4324 & ~hotkey_reserved_mask;
4325 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4327 } else if (sscanf(cmd, "%x", &mask) == 1) {
4336 tpacpi_disclose_usertask("procfs hotkey",
4337 "set mask to 0x%08x\n", mask);
4338 res = hotkey_user_mask_set(mask);
4342 mutex_unlock(&hotkey_mutex);
4346 static const struct acpi_device_id ibm_htk_device_ids[] = {
4347 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4348 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4349 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4353 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4354 .hid = ibm_htk_device_ids,
4355 .notify = hotkey_notify,
4356 .handle = &hkey_handle,
4357 .type = ACPI_DEVICE_NOTIFY,
4360 static struct ibm_struct hotkey_driver_data = {
4362 .read = hotkey_read,
4363 .write = hotkey_write,
4364 .exit = hotkey_exit,
4365 .resume = hotkey_resume,
4366 .suspend = hotkey_suspend,
4367 .acpi = &ibm_hotkey_acpidriver,
4370 /*************************************************************************
4371 * Bluetooth subdriver
4375 /* ACPI GBDC/SBDC bits */
4376 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4377 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4378 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4379 0 = disable, 1 = enable */
4383 /* ACPI \BLTH commands */
4384 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4385 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4386 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4387 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4388 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4391 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4393 static int bluetooth_get_status(void)
4397 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4398 if (dbg_bluetoothemul)
4399 return (tpacpi_bluetooth_emulstate) ?
4400 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4403 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4406 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4407 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4410 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4414 vdbg_printk(TPACPI_DBG_RFKILL,
4415 "will attempt to %s bluetooth\n",
4416 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4418 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4419 if (dbg_bluetoothemul) {
4420 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4425 if (state == TPACPI_RFK_RADIO_ON)
4426 status = TP_ACPI_BLUETOOTH_RADIOSSW
4427 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4431 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4437 /* sysfs bluetooth enable ---------------------------------------------- */
4438 static ssize_t bluetooth_enable_show(struct device *dev,
4439 struct device_attribute *attr,
4442 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4446 static ssize_t bluetooth_enable_store(struct device *dev,
4447 struct device_attribute *attr,
4448 const char *buf, size_t count)
4450 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4454 static DEVICE_ATTR_RW(bluetooth_enable);
4456 /* --------------------------------------------------------------------- */
4458 static struct attribute *bluetooth_attributes[] = {
4459 &dev_attr_bluetooth_enable.attr,
4463 static const struct attribute_group bluetooth_attr_group = {
4464 .attrs = bluetooth_attributes,
4467 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4468 .get_status = bluetooth_get_status,
4469 .set_status = bluetooth_set_status,
4472 static void bluetooth_shutdown(void)
4474 /* Order firmware to save current state to NVRAM */
4475 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4476 TP_ACPI_BLTH_SAVE_STATE))
4477 pr_notice("failed to save bluetooth state to NVRAM\n");
4479 vdbg_printk(TPACPI_DBG_RFKILL,
4480 "bluetooth state saved to NVRAM\n");
4483 static void bluetooth_exit(void)
4485 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4486 &bluetooth_attr_group);
4488 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4490 bluetooth_shutdown();
4493 static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4495 .ident = "ThinkPad E485",
4497 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4498 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4502 .ident = "ThinkPad E585",
4504 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4505 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4509 .ident = "ThinkPad A285 - 20MW",
4511 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4512 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4516 .ident = "ThinkPad A285 - 20MX",
4518 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4519 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4523 .ident = "ThinkPad A485 - 20MU",
4525 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4526 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4530 .ident = "ThinkPad A485 - 20MV",
4532 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4533 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4539 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4540 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4541 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4542 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4547 static int __init have_bt_fwbug(void)
4550 * Some AMD based ThinkPads have a firmware bug that calling
4551 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4553 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4554 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4555 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4561 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4566 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4567 "initializing bluetooth subdriver\n");
4569 TPACPI_ACPIHANDLE_INIT(hkey);
4571 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4572 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4573 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4574 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4576 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4577 "bluetooth is %s, status 0x%02x\n",
4578 str_supported(tp_features.bluetooth),
4581 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4582 if (dbg_bluetoothemul) {
4583 tp_features.bluetooth = 1;
4584 pr_info("bluetooth switch emulation enabled\n");
4587 if (tp_features.bluetooth &&
4588 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4589 /* no bluetooth hardware present in system */
4590 tp_features.bluetooth = 0;
4591 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4592 "bluetooth hardware not installed\n");
4595 if (!tp_features.bluetooth)
4598 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4599 &bluetooth_tprfk_ops,
4600 RFKILL_TYPE_BLUETOOTH,
4601 TPACPI_RFK_BLUETOOTH_SW_NAME,
4606 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4607 &bluetooth_attr_group);
4609 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4616 /* procfs -------------------------------------------------------------- */
4617 static int bluetooth_read(struct seq_file *m)
4619 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4622 static int bluetooth_write(char *buf)
4624 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4627 static struct ibm_struct bluetooth_driver_data = {
4628 .name = "bluetooth",
4629 .read = bluetooth_read,
4630 .write = bluetooth_write,
4631 .exit = bluetooth_exit,
4632 .shutdown = bluetooth_shutdown,
4635 /*************************************************************************
4640 /* ACPI GWAN/SWAN bits */
4641 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4642 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4643 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4644 0 = disable, 1 = enable */
4647 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4649 static int wan_get_status(void)
4653 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4655 return (tpacpi_wwan_emulstate) ?
4656 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4659 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4662 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4663 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4666 static int wan_set_status(enum tpacpi_rfkill_state state)
4670 vdbg_printk(TPACPI_DBG_RFKILL,
4671 "will attempt to %s wwan\n",
4672 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4674 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4676 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4681 if (state == TPACPI_RFK_RADIO_ON)
4682 status = TP_ACPI_WANCARD_RADIOSSW
4683 | TP_ACPI_WANCARD_RESUMECTRL;
4687 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4693 /* sysfs wan enable ---------------------------------------------------- */
4694 static ssize_t wan_enable_show(struct device *dev,
4695 struct device_attribute *attr,
4698 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4702 static ssize_t wan_enable_store(struct device *dev,
4703 struct device_attribute *attr,
4704 const char *buf, size_t count)
4706 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4710 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4711 wan_enable_show, wan_enable_store);
4713 /* --------------------------------------------------------------------- */
4715 static struct attribute *wan_attributes[] = {
4716 &dev_attr_wwan_enable.attr,
4720 static const struct attribute_group wan_attr_group = {
4721 .attrs = wan_attributes,
4724 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4725 .get_status = wan_get_status,
4726 .set_status = wan_set_status,
4729 static void wan_shutdown(void)
4731 /* Order firmware to save current state to NVRAM */
4732 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4733 TP_ACPI_WGSV_SAVE_STATE))
4734 pr_notice("failed to save WWAN state to NVRAM\n");
4736 vdbg_printk(TPACPI_DBG_RFKILL,
4737 "WWAN state saved to NVRAM\n");
4740 static void wan_exit(void)
4742 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4745 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4750 static int __init wan_init(struct ibm_init_struct *iibm)
4755 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4756 "initializing wan subdriver\n");
4758 TPACPI_ACPIHANDLE_INIT(hkey);
4760 tp_features.wan = hkey_handle &&
4761 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4763 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4764 "wan is %s, status 0x%02x\n",
4765 str_supported(tp_features.wan),
4768 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4770 tp_features.wan = 1;
4771 pr_info("wwan switch emulation enabled\n");
4774 if (tp_features.wan &&
4775 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4776 /* no wan hardware present in system */
4777 tp_features.wan = 0;
4778 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4779 "wan hardware not installed\n");
4782 if (!tp_features.wan)
4785 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4788 TPACPI_RFK_WWAN_SW_NAME,
4793 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4797 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4804 /* procfs -------------------------------------------------------------- */
4805 static int wan_read(struct seq_file *m)
4807 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4810 static int wan_write(char *buf)
4812 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4815 static struct ibm_struct wan_driver_data = {
4820 .shutdown = wan_shutdown,
4823 /*************************************************************************
4828 /* ACPI GUWB/SUWB bits */
4829 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4830 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4833 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4835 static int uwb_get_status(void)
4839 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4841 return (tpacpi_uwb_emulstate) ?
4842 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4845 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4848 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4849 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4852 static int uwb_set_status(enum tpacpi_rfkill_state state)
4856 vdbg_printk(TPACPI_DBG_RFKILL,
4857 "will attempt to %s UWB\n",
4858 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4860 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4862 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4867 if (state == TPACPI_RFK_RADIO_ON)
4868 status = TP_ACPI_UWB_RADIOSSW;
4872 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4878 /* --------------------------------------------------------------------- */
4880 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4881 .get_status = uwb_get_status,
4882 .set_status = uwb_set_status,
4885 static void uwb_exit(void)
4887 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4890 static int __init uwb_init(struct ibm_init_struct *iibm)
4895 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4896 "initializing uwb subdriver\n");
4898 TPACPI_ACPIHANDLE_INIT(hkey);
4900 tp_features.uwb = hkey_handle &&
4901 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4903 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4904 "uwb is %s, status 0x%02x\n",
4905 str_supported(tp_features.uwb),
4908 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4910 tp_features.uwb = 1;
4911 pr_info("uwb switch emulation enabled\n");
4914 if (tp_features.uwb &&
4915 !(status & TP_ACPI_UWB_HWPRESENT)) {
4916 /* no uwb hardware present in system */
4917 tp_features.uwb = 0;
4918 dbg_printk(TPACPI_DBG_INIT,
4919 "uwb hardware not installed\n");
4922 if (!tp_features.uwb)
4925 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4928 TPACPI_RFK_UWB_SW_NAME,
4933 static struct ibm_struct uwb_driver_data = {
4936 .flags.experimental = 1,
4939 /*************************************************************************
4943 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4945 enum video_access_mode {
4946 TPACPI_VIDEO_NONE = 0,
4947 TPACPI_VIDEO_570, /* 570 */
4948 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4949 TPACPI_VIDEO_NEW, /* all others */
4952 enum { /* video status flags, based on VIDEO_570 */
4953 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4954 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4955 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4958 enum { /* TPACPI_VIDEO_570 constants */
4959 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4960 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4961 * video_status_flags */
4962 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4963 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4966 static enum video_access_mode video_supported;
4967 static int video_orig_autosw;
4969 static int video_autosw_get(void);
4970 static int video_autosw_set(int enable);
4972 TPACPI_HANDLE(vid, root,
4973 "\\_SB.PCI.AGP.VGA", /* 570 */
4974 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4975 "\\_SB.PCI0.VID0", /* 770e */
4976 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4977 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4978 "\\_SB.PCI0.AGP.VID", /* all others */
4981 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4983 static int __init video_init(struct ibm_init_struct *iibm)
4987 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4989 TPACPI_ACPIHANDLE_INIT(vid);
4990 if (tpacpi_is_ibm())
4991 TPACPI_ACPIHANDLE_INIT(vid2);
4993 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4994 /* G41, assume IVGA doesn't change */
4995 vid_handle = vid2_handle;
4998 /* video switching not supported on R30, R31 */
4999 video_supported = TPACPI_VIDEO_NONE;
5000 else if (tpacpi_is_ibm() &&
5001 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
5003 video_supported = TPACPI_VIDEO_570;
5004 else if (tpacpi_is_ibm() &&
5005 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
5006 /* 600e/x, 770e, 770x */
5007 video_supported = TPACPI_VIDEO_770;
5010 video_supported = TPACPI_VIDEO_NEW;
5012 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
5013 str_supported(video_supported != TPACPI_VIDEO_NONE),
5016 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
5019 static void video_exit(void)
5021 dbg_printk(TPACPI_DBG_EXIT,
5022 "restoring original video autoswitch mode\n");
5023 if (video_autosw_set(video_orig_autosw))
5024 pr_err("error while trying to restore original video autoswitch mode\n");
5027 static int video_outputsw_get(void)
5032 switch (video_supported) {
5033 case TPACPI_VIDEO_570:
5034 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5035 TP_ACPI_VIDEO_570_PHSCMD))
5037 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5039 case TPACPI_VIDEO_770:
5040 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5043 status |= TP_ACPI_VIDEO_S_LCD;
5044 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5047 status |= TP_ACPI_VIDEO_S_CRT;
5049 case TPACPI_VIDEO_NEW:
5050 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5051 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5054 status |= TP_ACPI_VIDEO_S_CRT;
5056 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5057 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5060 status |= TP_ACPI_VIDEO_S_LCD;
5061 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5064 status |= TP_ACPI_VIDEO_S_DVI;
5073 static int video_outputsw_set(int status)
5078 switch (video_supported) {
5079 case TPACPI_VIDEO_570:
5080 res = acpi_evalf(NULL, NULL,
5081 "\\_SB.PHS2", "vdd",
5082 TP_ACPI_VIDEO_570_PHS2CMD,
5083 status | TP_ACPI_VIDEO_570_PHS2SET);
5085 case TPACPI_VIDEO_770:
5086 autosw = video_autosw_get();
5090 res = video_autosw_set(1);
5093 res = acpi_evalf(vid_handle, NULL,
5094 "ASWT", "vdd", status * 0x100, 0);
5095 if (!autosw && video_autosw_set(autosw)) {
5096 pr_err("video auto-switch left enabled due to error\n");
5100 case TPACPI_VIDEO_NEW:
5101 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5102 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5108 return (res) ? 0 : -EIO;
5111 static int video_autosw_get(void)
5115 switch (video_supported) {
5116 case TPACPI_VIDEO_570:
5117 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5120 case TPACPI_VIDEO_770:
5121 case TPACPI_VIDEO_NEW:
5122 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5132 static int video_autosw_set(int enable)
5134 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5139 static int video_outputsw_cycle(void)
5141 int autosw = video_autosw_get();
5147 switch (video_supported) {
5148 case TPACPI_VIDEO_570:
5149 res = video_autosw_set(1);
5152 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5154 case TPACPI_VIDEO_770:
5155 case TPACPI_VIDEO_NEW:
5156 res = video_autosw_set(1);
5159 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5164 if (!autosw && video_autosw_set(autosw)) {
5165 pr_err("video auto-switch left enabled due to error\n");
5169 return (res) ? 0 : -EIO;
5172 static int video_expand_toggle(void)
5174 switch (video_supported) {
5175 case TPACPI_VIDEO_570:
5176 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5178 case TPACPI_VIDEO_770:
5179 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5181 case TPACPI_VIDEO_NEW:
5182 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5190 static int video_read(struct seq_file *m)
5194 if (video_supported == TPACPI_VIDEO_NONE) {
5195 seq_printf(m, "status:\t\tnot supported\n");
5199 /* Even reads can crash X.org, so... */
5200 if (!capable(CAP_SYS_ADMIN))
5203 status = video_outputsw_get();
5207 autosw = video_autosw_get();
5211 seq_printf(m, "status:\t\tsupported\n");
5212 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5213 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5214 if (video_supported == TPACPI_VIDEO_NEW)
5215 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5216 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5217 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5218 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5219 if (video_supported == TPACPI_VIDEO_NEW)
5220 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5221 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5222 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5227 static int video_write(char *buf)
5230 int enable, disable, status;
5233 if (video_supported == TPACPI_VIDEO_NONE)
5236 /* Even reads can crash X.org, let alone writes... */
5237 if (!capable(CAP_SYS_ADMIN))
5243 while ((cmd = next_cmd(&buf))) {
5244 if (strlencmp(cmd, "lcd_enable") == 0) {
5245 enable |= TP_ACPI_VIDEO_S_LCD;
5246 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5247 disable |= TP_ACPI_VIDEO_S_LCD;
5248 } else if (strlencmp(cmd, "crt_enable") == 0) {
5249 enable |= TP_ACPI_VIDEO_S_CRT;
5250 } else if (strlencmp(cmd, "crt_disable") == 0) {
5251 disable |= TP_ACPI_VIDEO_S_CRT;
5252 } else if (video_supported == TPACPI_VIDEO_NEW &&
5253 strlencmp(cmd, "dvi_enable") == 0) {
5254 enable |= TP_ACPI_VIDEO_S_DVI;
5255 } else if (video_supported == TPACPI_VIDEO_NEW &&
5256 strlencmp(cmd, "dvi_disable") == 0) {
5257 disable |= TP_ACPI_VIDEO_S_DVI;
5258 } else if (strlencmp(cmd, "auto_enable") == 0) {
5259 res = video_autosw_set(1);
5262 } else if (strlencmp(cmd, "auto_disable") == 0) {
5263 res = video_autosw_set(0);
5266 } else if (strlencmp(cmd, "video_switch") == 0) {
5267 res = video_outputsw_cycle();
5270 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5271 res = video_expand_toggle();
5278 if (enable || disable) {
5279 status = video_outputsw_get();
5282 res = video_outputsw_set((status & ~disable) | enable);
5290 static struct ibm_struct video_driver_data = {
5293 .write = video_write,
5297 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5299 /*************************************************************************
5300 * Keyboard backlight subdriver
5303 static enum led_brightness kbdlight_brightness;
5304 static DEFINE_MUTEX(kbdlight_mutex);
5306 static int kbdlight_set_level(int level)
5313 mutex_lock(&kbdlight_mutex);
5315 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5318 kbdlight_brightness = level;
5320 mutex_unlock(&kbdlight_mutex);
5325 static int kbdlight_get_level(void)
5332 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5338 return status & 0x3;
5341 static bool kbdlight_is_supported(void)
5348 if (!acpi_has_method(hkey_handle, "MLCG")) {
5349 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5353 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5354 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5359 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5363 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5365 * Guessed test for keyboard backlight:
5367 * Machines with backlight keyboard return:
5368 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5369 * b110100010010000000XX - ThinkPad x230
5370 * b010100000010000000XX - ThinkPad x240
5371 * b010100000010000000XX - ThinkPad W541
5372 * (XX is current backlight level)
5374 * Machines without backlight keyboard return:
5375 * b10100001000000000000 - ThinkPad x230
5376 * b10110001000000000000 - ThinkPad E430
5377 * b00000000000000000000 - ThinkPad E450
5379 * Candidate BITs for detection test (XOR):
5380 * b01000000001000000000
5383 return status & BIT(9);
5386 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5387 enum led_brightness brightness)
5389 return kbdlight_set_level(brightness);
5392 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5396 level = kbdlight_get_level();
5403 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5405 .name = "tpacpi::kbd_backlight",
5406 .max_brightness = 2,
5407 .flags = LED_BRIGHT_HW_CHANGED,
5408 .brightness_set_blocking = &kbdlight_sysfs_set,
5409 .brightness_get = &kbdlight_sysfs_get,
5413 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5417 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5419 TPACPI_ACPIHANDLE_INIT(hkey);
5421 if (!kbdlight_is_supported()) {
5422 tp_features.kbdlight = 0;
5423 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5427 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5428 tp_features.kbdlight = 1;
5430 rc = led_classdev_register(&tpacpi_pdev->dev,
5431 &tpacpi_led_kbdlight.led_classdev);
5433 tp_features.kbdlight = 0;
5437 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5438 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5442 static void kbdlight_exit(void)
5444 if (tp_features.kbdlight)
5445 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5448 static int kbdlight_set_level_and_update(int level)
5451 struct led_classdev *led_cdev;
5453 ret = kbdlight_set_level(level);
5454 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5456 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5457 led_cdev->brightness = level;
5462 static int kbdlight_read(struct seq_file *m)
5466 if (!tp_features.kbdlight) {
5467 seq_printf(m, "status:\t\tnot supported\n");
5469 level = kbdlight_get_level();
5471 seq_printf(m, "status:\t\terror %d\n", level);
5473 seq_printf(m, "status:\t\t%d\n", level);
5474 seq_printf(m, "commands:\t0, 1, 2\n");
5480 static int kbdlight_write(char *buf)
5485 if (!tp_features.kbdlight)
5488 while ((cmd = next_cmd(&buf))) {
5489 if (strlencmp(cmd, "0") == 0)
5491 else if (strlencmp(cmd, "1") == 0)
5493 else if (strlencmp(cmd, "2") == 0)
5502 return kbdlight_set_level_and_update(level);
5505 static void kbdlight_suspend(void)
5507 struct led_classdev *led_cdev;
5509 if (!tp_features.kbdlight)
5512 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5513 led_update_brightness(led_cdev);
5514 led_classdev_suspend(led_cdev);
5517 static void kbdlight_resume(void)
5519 if (!tp_features.kbdlight)
5522 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5525 static struct ibm_struct kbdlight_driver_data = {
5527 .read = kbdlight_read,
5528 .write = kbdlight_write,
5529 .suspend = kbdlight_suspend,
5530 .resume = kbdlight_resume,
5531 .exit = kbdlight_exit,
5534 /*************************************************************************
5535 * Light (thinklight) subdriver
5538 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5539 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5541 static int light_get_status(void)
5545 if (tp_features.light_status) {
5546 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5554 static int light_set_status(int status)
5558 if (tp_features.light) {
5560 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5562 TP_CMOS_THINKLIGHT_ON :
5563 TP_CMOS_THINKLIGHT_OFF);
5565 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5568 return (rc) ? 0 : -EIO;
5574 static int light_sysfs_set(struct led_classdev *led_cdev,
5575 enum led_brightness brightness)
5577 return light_set_status((brightness != LED_OFF) ?
5578 TPACPI_LED_ON : TPACPI_LED_OFF);
5581 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5583 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5586 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5588 .name = "tpacpi::thinklight",
5589 .brightness_set_blocking = &light_sysfs_set,
5590 .brightness_get = &light_sysfs_get,
5594 static int __init light_init(struct ibm_init_struct *iibm)
5598 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5600 if (tpacpi_is_ibm()) {
5601 TPACPI_ACPIHANDLE_INIT(ledb);
5602 TPACPI_ACPIHANDLE_INIT(lght);
5604 TPACPI_ACPIHANDLE_INIT(cmos);
5606 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5607 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5609 if (tp_features.light)
5610 /* light status not supported on
5611 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5612 tp_features.light_status =
5613 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5615 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5616 str_supported(tp_features.light),
5617 str_supported(tp_features.light_status));
5619 if (!tp_features.light)
5622 rc = led_classdev_register(&tpacpi_pdev->dev,
5623 &tpacpi_led_thinklight.led_classdev);
5626 tp_features.light = 0;
5627 tp_features.light_status = 0;
5635 static void light_exit(void)
5637 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5640 static int light_read(struct seq_file *m)
5644 if (!tp_features.light) {
5645 seq_printf(m, "status:\t\tnot supported\n");
5646 } else if (!tp_features.light_status) {
5647 seq_printf(m, "status:\t\tunknown\n");
5648 seq_printf(m, "commands:\ton, off\n");
5650 status = light_get_status();
5653 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5654 seq_printf(m, "commands:\ton, off\n");
5660 static int light_write(char *buf)
5665 if (!tp_features.light)
5668 while ((cmd = next_cmd(&buf))) {
5669 if (strlencmp(cmd, "on") == 0) {
5671 } else if (strlencmp(cmd, "off") == 0) {
5677 return light_set_status(newstatus);
5680 static struct ibm_struct light_driver_data = {
5683 .write = light_write,
5687 /*************************************************************************
5691 /* sysfs cmos_command -------------------------------------------------- */
5692 static ssize_t cmos_command_store(struct device *dev,
5693 struct device_attribute *attr,
5694 const char *buf, size_t count)
5696 unsigned long cmos_cmd;
5699 if (parse_strtoul(buf, 21, &cmos_cmd))
5702 res = issue_thinkpad_cmos_command(cmos_cmd);
5703 return (res) ? res : count;
5706 static DEVICE_ATTR_WO(cmos_command);
5708 /* --------------------------------------------------------------------- */
5710 static int __init cmos_init(struct ibm_init_struct *iibm)
5714 vdbg_printk(TPACPI_DBG_INIT,
5715 "initializing cmos commands subdriver\n");
5717 TPACPI_ACPIHANDLE_INIT(cmos);
5719 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5720 str_supported(cmos_handle != NULL));
5722 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5726 return (cmos_handle) ? 0 : 1;
5729 static void cmos_exit(void)
5731 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5734 static int cmos_read(struct seq_file *m)
5736 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5737 R30, R31, T20-22, X20-21 */
5739 seq_printf(m, "status:\t\tnot supported\n");
5741 seq_printf(m, "status:\t\tsupported\n");
5742 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5748 static int cmos_write(char *buf)
5753 while ((cmd = next_cmd(&buf))) {
5754 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5755 cmos_cmd >= 0 && cmos_cmd <= 21) {
5760 res = issue_thinkpad_cmos_command(cmos_cmd);
5768 static struct ibm_struct cmos_driver_data = {
5771 .write = cmos_write,
5775 /*************************************************************************
5779 enum led_access_mode {
5780 TPACPI_LED_NONE = 0,
5781 TPACPI_LED_570, /* 570 */
5782 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5783 TPACPI_LED_NEW, /* all others */
5786 enum { /* For TPACPI_LED_OLD */
5787 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5788 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5789 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5792 static enum led_access_mode led_supported;
5794 static acpi_handle led_handle;
5796 #define TPACPI_LED_NUMLEDS 16
5797 static struct tpacpi_led_classdev *tpacpi_leds;
5798 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5799 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5800 /* there's a limit of 19 chars + NULL before 2.6.26 */
5802 "tpacpi:orange:batt",
5803 "tpacpi:green:batt",
5804 "tpacpi::dock_active",
5805 "tpacpi::bay_active",
5806 "tpacpi::dock_batt",
5807 "tpacpi::unknown_led",
5809 "tpacpi::dock_status1",
5810 "tpacpi::dock_status2",
5811 "tpacpi::unknown_led2",
5812 "tpacpi::unknown_led3",
5813 "tpacpi::thinkvantage",
5815 #define TPACPI_SAFE_LEDS 0x1081U
5817 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5819 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5822 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5826 static int led_get_status(const unsigned int led)
5829 enum led_status_t led_s;
5831 switch (led_supported) {
5832 case TPACPI_LED_570:
5833 if (!acpi_evalf(ec_handle,
5834 &status, "GLED", "dd", 1 << led))
5836 led_s = (status == 0) ?
5841 tpacpi_led_state_cache[led] = led_s;
5850 static int led_set_status(const unsigned int led,
5851 const enum led_status_t ledstatus)
5853 /* off, on, blink. Index is led_status_t */
5854 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5855 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5859 switch (led_supported) {
5860 case TPACPI_LED_570:
5862 if (unlikely(led > 7))
5864 if (unlikely(tpacpi_is_led_restricted(led)))
5866 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5867 (1 << led), led_sled_arg1[ledstatus]))
5870 case TPACPI_LED_OLD:
5871 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5872 if (unlikely(led > 7))
5874 if (unlikely(tpacpi_is_led_restricted(led)))
5876 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5878 rc = ec_write(TPACPI_LED_EC_HLBL,
5879 (ledstatus == TPACPI_LED_BLINK) << led);
5881 rc = ec_write(TPACPI_LED_EC_HLCL,
5882 (ledstatus != TPACPI_LED_OFF) << led);
5884 case TPACPI_LED_NEW:
5886 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5888 if (unlikely(tpacpi_is_led_restricted(led)))
5890 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5891 led, led_led_arg1[ledstatus]))
5899 tpacpi_led_state_cache[led] = ledstatus;
5904 static int led_sysfs_set(struct led_classdev *led_cdev,
5905 enum led_brightness brightness)
5907 struct tpacpi_led_classdev *data = container_of(led_cdev,
5908 struct tpacpi_led_classdev, led_classdev);
5909 enum led_status_t new_state;
5911 if (brightness == LED_OFF)
5912 new_state = TPACPI_LED_OFF;
5913 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5914 new_state = TPACPI_LED_ON;
5916 new_state = TPACPI_LED_BLINK;
5918 return led_set_status(data->led, new_state);
5921 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5922 unsigned long *delay_on, unsigned long *delay_off)
5924 struct tpacpi_led_classdev *data = container_of(led_cdev,
5925 struct tpacpi_led_classdev, led_classdev);
5927 /* Can we choose the flash rate? */
5928 if (*delay_on == 0 && *delay_off == 0) {
5929 /* yes. set them to the hardware blink rate (1 Hz) */
5930 *delay_on = 500; /* ms */
5931 *delay_off = 500; /* ms */
5932 } else if ((*delay_on != 500) || (*delay_off != 500))
5935 return led_set_status(data->led, TPACPI_LED_BLINK);
5938 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5942 struct tpacpi_led_classdev *data = container_of(led_cdev,
5943 struct tpacpi_led_classdev, led_classdev);
5945 rc = led_get_status(data->led);
5947 if (rc == TPACPI_LED_OFF || rc < 0)
5948 rc = LED_OFF; /* no error handling in led class :( */
5955 static void led_exit(void)
5959 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5960 if (tpacpi_leds[i].led_classdev.name)
5961 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5967 static int __init tpacpi_init_led(unsigned int led)
5971 tpacpi_leds[led].led = led;
5973 /* LEDs with no name don't get registered */
5974 if (!tpacpi_led_names[led])
5977 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5978 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5979 if (led_supported == TPACPI_LED_570)
5980 tpacpi_leds[led].led_classdev.brightness_get =
5983 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5985 rc = led_classdev_register(&tpacpi_pdev->dev,
5986 &tpacpi_leds[led].led_classdev);
5988 tpacpi_leds[led].led_classdev.name = NULL;
5993 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5994 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5995 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5996 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5998 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5999 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
6000 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
6001 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
6002 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
6003 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
6004 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
6005 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
6007 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
6008 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
6009 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
6010 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
6011 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
6013 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
6014 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
6015 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
6016 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
6018 /* (1) - may have excess leds enabled on MSB */
6020 /* Defaults (order matters, keep last, don't reorder!) */
6022 .vendor = PCI_VENDOR_ID_LENOVO,
6023 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6026 { /* IBM ThinkPads with no EC version string */
6027 .vendor = PCI_VENDOR_ID_IBM,
6028 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6031 { /* IBM ThinkPads with EC version string */
6032 .vendor = PCI_VENDOR_ID_IBM,
6033 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6038 static enum led_access_mode __init led_init_detect_mode(void)
6042 if (tpacpi_is_ibm()) {
6044 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6045 if (ACPI_SUCCESS(status))
6046 return TPACPI_LED_570;
6048 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6049 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6050 if (ACPI_SUCCESS(status))
6051 return TPACPI_LED_OLD;
6055 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6056 if (ACPI_SUCCESS(status))
6057 return TPACPI_LED_NEW;
6059 /* R30, R31, and unknown firmwares */
6061 return TPACPI_LED_NONE;
6064 static int __init led_init(struct ibm_init_struct *iibm)
6068 unsigned long useful_leds;
6070 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6072 led_supported = led_init_detect_mode();
6074 if (led_supported != TPACPI_LED_NONE) {
6075 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6076 ARRAY_SIZE(led_useful_qtable));
6080 led_supported = TPACPI_LED_NONE;
6084 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6085 str_supported(led_supported), led_supported);
6087 if (led_supported == TPACPI_LED_NONE)
6090 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6093 pr_err("Out of memory for LED data\n");
6097 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6098 tpacpi_leds[i].led = -1;
6100 if (!tpacpi_is_led_restricted(i) &&
6101 test_bit(i, &useful_leds)) {
6102 rc = tpacpi_init_led(i);
6110 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6111 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6116 #define str_led_status(s) \
6117 ((s) == TPACPI_LED_OFF ? "off" : \
6118 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6120 static int led_read(struct seq_file *m)
6122 if (!led_supported) {
6123 seq_printf(m, "status:\t\tnot supported\n");
6126 seq_printf(m, "status:\t\tsupported\n");
6128 if (led_supported == TPACPI_LED_570) {
6131 for (i = 0; i < 8; i++) {
6132 status = led_get_status(i);
6135 seq_printf(m, "%d:\t\t%s\n",
6136 i, str_led_status(status));
6140 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6145 static int led_write(char *buf)
6149 enum led_status_t s;
6154 while ((cmd = next_cmd(&buf))) {
6155 if (sscanf(cmd, "%d", &led) != 1)
6158 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6159 tpacpi_leds[led].led < 0)
6162 if (strstr(cmd, "off")) {
6164 } else if (strstr(cmd, "on")) {
6166 } else if (strstr(cmd, "blink")) {
6167 s = TPACPI_LED_BLINK;
6172 rc = led_set_status(led, s);
6180 static struct ibm_struct led_driver_data = {
6187 /*************************************************************************
6191 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6193 #define TPACPI_BEEP_Q1 0x0001
6195 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6196 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6197 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6200 static int __init beep_init(struct ibm_init_struct *iibm)
6202 unsigned long quirks;
6204 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6206 TPACPI_ACPIHANDLE_INIT(beep);
6208 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6209 str_supported(beep_handle != NULL));
6211 quirks = tpacpi_check_quirks(beep_quirk_table,
6212 ARRAY_SIZE(beep_quirk_table));
6214 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6216 return (beep_handle) ? 0 : 1;
6219 static int beep_read(struct seq_file *m)
6222 seq_printf(m, "status:\t\tnot supported\n");
6224 seq_printf(m, "status:\t\tsupported\n");
6225 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6231 static int beep_write(char *buf)
6239 while ((cmd = next_cmd(&buf))) {
6240 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6241 beep_cmd >= 0 && beep_cmd <= 17) {
6245 if (tp_features.beep_needs_two_args) {
6246 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6250 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6259 static struct ibm_struct beep_driver_data = {
6262 .write = beep_write,
6265 /*************************************************************************
6269 enum thermal_access_mode {
6270 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6271 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6272 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6273 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6274 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6277 enum { /* TPACPI_THERMAL_TPEC_* */
6278 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6279 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6280 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6282 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6286 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6287 struct ibm_thermal_sensors_struct {
6288 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6291 static enum thermal_access_mode thermal_read_mode;
6293 /* idx is zero-based */
6294 static int thermal_get_sensor(int idx, s32 *value)
6300 t = TP_EC_THERMAL_TMP0;
6302 switch (thermal_read_mode) {
6303 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6304 case TPACPI_THERMAL_TPEC_16:
6305 if (idx >= 8 && idx <= 15) {
6306 t = TP_EC_THERMAL_TMP8;
6311 case TPACPI_THERMAL_TPEC_8:
6313 if (!acpi_ec_read(t + idx, &tmp))
6315 *value = tmp * 1000;
6320 case TPACPI_THERMAL_ACPI_UPDT:
6322 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6323 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6325 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6327 *value = (t - 2732) * 100;
6332 case TPACPI_THERMAL_ACPI_TMP07:
6334 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6335 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6337 if (t > 127 || t < -127)
6338 t = TP_EC_THERMAL_TMP_NA;
6344 case TPACPI_THERMAL_NONE:
6352 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6363 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6366 for (i = 0 ; i < n; i++) {
6367 res = thermal_get_sensor(i, &s->temp[i]);
6375 static void thermal_dump_all_sensors(void)
6378 struct ibm_thermal_sensors_struct t;
6380 n = thermal_get_sensors(&t);
6384 pr_notice("temperatures (Celsius):");
6386 for (i = 0; i < n; i++) {
6387 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6388 pr_cont(" %d", (int)(t.temp[i] / 1000));
6396 /* sysfs temp##_input -------------------------------------------------- */
6398 static ssize_t thermal_temp_input_show(struct device *dev,
6399 struct device_attribute *attr,
6402 struct sensor_device_attribute *sensor_attr =
6403 to_sensor_dev_attr(attr);
6404 int idx = sensor_attr->index;
6408 res = thermal_get_sensor(idx, &value);
6411 if (value == TPACPI_THERMAL_SENSOR_NA)
6414 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6417 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6418 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6419 thermal_temp_input_show, NULL, _idxB)
6421 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6422 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6423 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6424 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6425 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6426 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6427 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6428 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6429 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6430 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6431 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6432 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6433 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6434 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6435 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6436 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6437 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6440 #define THERMAL_ATTRS(X) \
6441 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6443 static struct attribute *thermal_temp_input_attr[] = {
6463 static const struct attribute_group thermal_temp_input16_group = {
6464 .attrs = thermal_temp_input_attr
6467 static const struct attribute_group thermal_temp_input8_group = {
6468 .attrs = &thermal_temp_input_attr[8]
6471 #undef THERMAL_SENSOR_ATTR_TEMP
6472 #undef THERMAL_ATTRS
6474 /* --------------------------------------------------------------------- */
6476 static int __init thermal_init(struct ibm_init_struct *iibm)
6483 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6485 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6487 if (thinkpad_id.ec_model) {
6489 * Direct EC access mode: sensors at registers
6490 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6491 * non-implemented, thermal sensors return 0x80 when
6496 for (i = 0; i < 8; i++) {
6497 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6503 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6511 /* This is sheer paranoia, but we handle it anyway */
6513 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6514 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6516 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6517 thermal_read_mode = TPACPI_THERMAL_NONE;
6522 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6524 } else if (acpi_tmp7) {
6525 if (tpacpi_is_ibm() &&
6526 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6527 /* 600e/x, 770e, 770x */
6528 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6530 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6531 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6534 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6535 thermal_read_mode = TPACPI_THERMAL_NONE;
6538 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6539 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6542 switch (thermal_read_mode) {
6543 case TPACPI_THERMAL_TPEC_16:
6544 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6545 &thermal_temp_input16_group);
6549 case TPACPI_THERMAL_TPEC_8:
6550 case TPACPI_THERMAL_ACPI_TMP07:
6551 case TPACPI_THERMAL_ACPI_UPDT:
6552 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6553 &thermal_temp_input8_group);
6557 case TPACPI_THERMAL_NONE:
6565 static void thermal_exit(void)
6567 switch (thermal_read_mode) {
6568 case TPACPI_THERMAL_TPEC_16:
6569 sysfs_remove_group(&tpacpi_hwmon->kobj,
6570 &thermal_temp_input16_group);
6572 case TPACPI_THERMAL_TPEC_8:
6573 case TPACPI_THERMAL_ACPI_TMP07:
6574 case TPACPI_THERMAL_ACPI_UPDT:
6575 sysfs_remove_group(&tpacpi_hwmon->kobj,
6576 &thermal_temp_input8_group);
6578 case TPACPI_THERMAL_NONE:
6584 static int thermal_read(struct seq_file *m)
6587 struct ibm_thermal_sensors_struct t;
6589 n = thermal_get_sensors(&t);
6590 if (unlikely(n < 0))
6593 seq_printf(m, "temperatures:\t");
6596 for (i = 0; i < (n - 1); i++)
6597 seq_printf(m, "%d ", t.temp[i] / 1000);
6598 seq_printf(m, "%d\n", t.temp[i] / 1000);
6600 seq_printf(m, "not supported\n");
6605 static struct ibm_struct thermal_driver_data = {
6607 .read = thermal_read,
6608 .exit = thermal_exit,
6611 /*************************************************************************
6612 * Backlight/brightness subdriver
6615 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6618 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6619 * CMOS NVRAM byte 0x5E, bits 0-3.
6621 * EC HBRV (0x31) has the following layout
6622 * Bit 7: unknown function
6623 * Bit 6: unknown function
6624 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6625 * Bit 4: must be set to zero to avoid problems
6626 * Bit 3-0: backlight brightness level
6628 * brightness_get_raw returns status data in the HBRV layout
6630 * WARNING: The X61 has been verified to use HBRV for something else, so
6631 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6632 * testing on the very early *60 Lenovo models...
6636 TP_EC_BACKLIGHT = 0x31,
6638 /* TP_EC_BACKLIGHT bitmasks */
6639 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6640 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6641 TP_EC_BACKLIGHT_MAPSW = 0x20,
6644 enum tpacpi_brightness_access_mode {
6645 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6646 TPACPI_BRGHT_MODE_EC, /* EC control */
6647 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6648 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6649 TPACPI_BRGHT_MODE_MAX
6652 static struct backlight_device *ibm_backlight_device;
6654 static enum tpacpi_brightness_access_mode brightness_mode =
6655 TPACPI_BRGHT_MODE_MAX;
6657 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6659 static struct mutex brightness_mutex;
6661 /* NVRAM brightness access,
6662 * call with brightness_mutex held! */
6663 static unsigned int tpacpi_brightness_nvram_get(void)
6667 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6668 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6669 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6670 lnvram &= bright_maxlvl;
6675 static void tpacpi_brightness_checkpoint_nvram(void)
6680 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6683 vdbg_printk(TPACPI_DBG_BRGHT,
6684 "trying to checkpoint backlight level to NVRAM...\n");
6686 if (mutex_lock_killable(&brightness_mutex) < 0)
6689 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6691 lec &= TP_EC_BACKLIGHT_LVLMSK;
6692 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6694 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6695 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6696 /* NVRAM needs update */
6697 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6698 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6700 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6701 dbg_printk(TPACPI_DBG_BRGHT,
6702 "updated NVRAM backlight level to %u (0x%02x)\n",
6703 (unsigned int) lec, (unsigned int) b_nvram);
6705 vdbg_printk(TPACPI_DBG_BRGHT,
6706 "NVRAM backlight level already is %u (0x%02x)\n",
6707 (unsigned int) lec, (unsigned int) b_nvram);
6710 mutex_unlock(&brightness_mutex);
6714 /* call with brightness_mutex held! */
6715 static int tpacpi_brightness_get_raw(int *status)
6719 switch (brightness_mode) {
6720 case TPACPI_BRGHT_MODE_UCMS_STEP:
6721 *status = tpacpi_brightness_nvram_get();
6723 case TPACPI_BRGHT_MODE_EC:
6724 case TPACPI_BRGHT_MODE_ECNVRAM:
6725 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6734 /* call with brightness_mutex held! */
6735 /* do NOT call with illegal backlight level value */
6736 static int tpacpi_brightness_set_ec(unsigned int value)
6740 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6743 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6744 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6745 (value & TP_EC_BACKLIGHT_LVLMSK))))
6751 /* call with brightness_mutex held! */
6752 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6755 unsigned int current_value, i;
6757 current_value = tpacpi_brightness_nvram_get();
6759 if (value == current_value)
6762 cmos_cmd = (value > current_value) ?
6763 TP_CMOS_BRIGHTNESS_UP :
6764 TP_CMOS_BRIGHTNESS_DOWN;
6765 inc = (value > current_value) ? 1 : -1;
6767 for (i = current_value; i != value; i += inc)
6768 if (issue_thinkpad_cmos_command(cmos_cmd))
6774 /* May return EINTR which can always be mapped to ERESTARTSYS */
6775 static int brightness_set(unsigned int value)
6779 if (value > bright_maxlvl)
6782 vdbg_printk(TPACPI_DBG_BRGHT,
6783 "set backlight level to %d\n", value);
6785 res = mutex_lock_killable(&brightness_mutex);
6789 switch (brightness_mode) {
6790 case TPACPI_BRGHT_MODE_EC:
6791 case TPACPI_BRGHT_MODE_ECNVRAM:
6792 res = tpacpi_brightness_set_ec(value);
6794 case TPACPI_BRGHT_MODE_UCMS_STEP:
6795 res = tpacpi_brightness_set_ucmsstep(value);
6801 mutex_unlock(&brightness_mutex);
6805 /* sysfs backlight class ----------------------------------------------- */
6807 static int brightness_update_status(struct backlight_device *bd)
6809 unsigned int level =
6810 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6811 bd->props.power == FB_BLANK_UNBLANK) ?
6812 bd->props.brightness : 0;
6814 dbg_printk(TPACPI_DBG_BRGHT,
6815 "backlight: attempt to set level to %d\n",
6818 /* it is the backlight class's job (caller) to handle
6819 * EINTR and other errors properly */
6820 return brightness_set(level);
6823 static int brightness_get(struct backlight_device *bd)
6827 res = mutex_lock_killable(&brightness_mutex);
6831 res = tpacpi_brightness_get_raw(&status);
6833 mutex_unlock(&brightness_mutex);
6838 return status & TP_EC_BACKLIGHT_LVLMSK;
6841 static void tpacpi_brightness_notify_change(void)
6843 backlight_force_update(ibm_backlight_device,
6844 BACKLIGHT_UPDATE_HOTKEY);
6847 static const struct backlight_ops ibm_backlight_data = {
6848 .get_brightness = brightness_get,
6849 .update_status = brightness_update_status,
6852 /* --------------------------------------------------------------------- */
6855 * Call _BCL method of video device. On some ThinkPads this will
6856 * switch the firmware to the ACPI brightness control mode.
6859 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6861 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6862 union acpi_object *obj;
6863 struct acpi_device *device, *child;
6866 if (acpi_bus_get_device(handle, &device))
6870 list_for_each_entry(child, &device->children, node) {
6871 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6873 if (ACPI_FAILURE(status))
6876 obj = (union acpi_object *)buffer.pointer;
6877 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6878 pr_err("Unknown _BCL data, please report this to %s\n",
6882 rc = obj->package.count;
6887 kfree(buffer.pointer);
6893 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6895 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6897 acpi_handle video_device;
6900 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6902 bcl_levels = tpacpi_query_bcl_levels(video_device);
6904 tp_features.bright_acpimode = (bcl_levels > 0);
6906 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6910 * These are only useful for models that have only one possibility
6911 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6914 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6915 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6916 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6918 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6919 /* Models with ATI GPUs known to require ECNVRAM mode */
6920 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6922 /* Models with ATI GPUs that can use ECNVRAM */
6923 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6924 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6925 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6926 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6928 /* Models with Intel Extreme Graphics 2 */
6929 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6930 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6931 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6933 /* Models with Intel GMA900 */
6934 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6935 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6936 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6940 * Returns < 0 for error, otherwise sets tp_features.bright_*
6941 * and bright_maxlvl.
6943 static void __init tpacpi_detect_brightness_capabilities(void)
6947 vdbg_printk(TPACPI_DBG_INIT,
6948 "detecting firmware brightness interface capabilities\n");
6950 /* we could run a quirks check here (same table used by
6951 * brightness_init) if needed */
6954 * We always attempt to detect acpi support, so as to switch
6955 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6956 * going to publish a backlight interface
6958 b = tpacpi_check_std_acpi_brightness_support();
6968 tp_features.bright_unkfw = 1;
6969 bright_maxlvl = b - 1;
6971 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6974 static int __init brightness_init(struct ibm_init_struct *iibm)
6976 struct backlight_properties props;
6978 unsigned long quirks;
6980 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6982 mutex_init(&brightness_mutex);
6984 quirks = tpacpi_check_quirks(brightness_quirk_table,
6985 ARRAY_SIZE(brightness_quirk_table));
6987 /* tpacpi_detect_brightness_capabilities() must have run already */
6989 /* if it is unknown, we don't handle it: it wouldn't be safe */
6990 if (tp_features.bright_unkfw)
6993 if (!brightness_enable) {
6994 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6995 "brightness support disabled by module parameter\n");
6999 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
7000 if (brightness_enable > 1) {
7001 pr_info("Standard ACPI backlight interface available, not loading native one\n");
7003 } else if (brightness_enable == 1) {
7004 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
7007 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
7008 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
7012 * Check for module parameter bogosity, note that we
7013 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7014 * able to detect "unspecified"
7016 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7019 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7020 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7021 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7022 if (quirks & TPACPI_BRGHT_Q_EC)
7023 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7025 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7027 dbg_printk(TPACPI_DBG_BRGHT,
7028 "driver auto-selected brightness_mode=%d\n",
7033 if (!tpacpi_is_ibm() &&
7034 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7035 brightness_mode == TPACPI_BRGHT_MODE_EC))
7038 if (tpacpi_brightness_get_raw(&b) < 0)
7041 memset(&props, 0, sizeof(struct backlight_properties));
7042 props.type = BACKLIGHT_PLATFORM;
7043 props.max_brightness = bright_maxlvl;
7044 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7045 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7047 &ibm_backlight_data,
7049 if (IS_ERR(ibm_backlight_device)) {
7050 int rc = PTR_ERR(ibm_backlight_device);
7051 ibm_backlight_device = NULL;
7052 pr_err("Could not register backlight device\n");
7055 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7056 "brightness is supported\n");
7058 if (quirks & TPACPI_BRGHT_Q_ASK) {
7059 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7061 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7065 /* Added by mistake in early 2007. Probably useless, but it could
7066 * be working around some unknown firmware problem where the value
7067 * read at startup doesn't match the real hardware state... so leave
7068 * it in place just in case */
7069 backlight_update_status(ibm_backlight_device);
7071 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7072 "brightness: registering brightness hotkeys as change notification\n");
7073 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7074 | TP_ACPI_HKEY_BRGHTUP_MASK
7075 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7079 static void brightness_suspend(void)
7081 tpacpi_brightness_checkpoint_nvram();
7084 static void brightness_shutdown(void)
7086 tpacpi_brightness_checkpoint_nvram();
7089 static void brightness_exit(void)
7091 if (ibm_backlight_device) {
7092 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7093 "calling backlight_device_unregister()\n");
7094 backlight_device_unregister(ibm_backlight_device);
7097 tpacpi_brightness_checkpoint_nvram();
7100 static int brightness_read(struct seq_file *m)
7104 level = brightness_get(NULL);
7106 seq_printf(m, "level:\t\tunreadable\n");
7108 seq_printf(m, "level:\t\t%d\n", level);
7109 seq_printf(m, "commands:\tup, down\n");
7110 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7117 static int brightness_write(char *buf)
7123 level = brightness_get(NULL);
7127 while ((cmd = next_cmd(&buf))) {
7128 if (strlencmp(cmd, "up") == 0) {
7129 if (level < bright_maxlvl)
7131 } else if (strlencmp(cmd, "down") == 0) {
7134 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7135 level >= 0 && level <= bright_maxlvl) {
7141 tpacpi_disclose_usertask("procfs brightness",
7142 "set level to %d\n", level);
7145 * Now we know what the final level should be, so we try to set it.
7146 * Doing it this way makes the syscall restartable in case of EINTR
7148 rc = brightness_set(level);
7149 if (!rc && ibm_backlight_device)
7150 backlight_force_update(ibm_backlight_device,
7151 BACKLIGHT_UPDATE_SYSFS);
7152 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7155 static struct ibm_struct brightness_driver_data = {
7156 .name = "brightness",
7157 .read = brightness_read,
7158 .write = brightness_write,
7159 .exit = brightness_exit,
7160 .suspend = brightness_suspend,
7161 .shutdown = brightness_shutdown,
7164 /*************************************************************************
7169 * IBM ThinkPads have a simple volume controller with MUTE gating.
7170 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7172 * Since the *61 series (and probably also the later *60 series), Lenovo
7173 * ThinkPads only implement the MUTE gate.
7176 * Bit 6: MUTE (1 mutes sound)
7178 * Other bits should be zero as far as we know.
7180 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7181 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7182 * such as bit 7 which is used to detect repeated presses of MUTE,
7183 * and we leave them unchanged.
7185 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7186 * in response to user input. Unfortunately, this rarely works well.
7187 * The laptop changes the state of its internal MUTE gate and, on some
7188 * models, sends KEY_MUTE, causing any user code that responds to the
7189 * mute button to get confused. The hardware MUTE gate is also
7190 * unnecessary, since user code can handle the mute button without
7191 * kernel or EC help.
7193 * To avoid confusing userspace, we simply disable all EC-based mute
7194 * and volume controls when possible.
7197 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7199 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7200 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7201 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7203 #if SNDRV_CARDS <= 32
7204 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7206 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7208 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7209 static char *alsa_id = "ThinkPadEC";
7210 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7212 struct tpacpi_alsa_data {
7213 struct snd_card *card;
7214 struct snd_ctl_elem_id *ctl_mute_id;
7215 struct snd_ctl_elem_id *ctl_vol_id;
7218 static struct snd_card *alsa_card;
7223 /* TP_EC_AUDIO bits */
7224 TP_EC_AUDIO_MUTESW = 6,
7226 /* TP_EC_AUDIO bitmasks */
7227 TP_EC_AUDIO_LVL_MSK = 0x0F,
7228 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7230 /* Maximum volume */
7231 TP_EC_VOLUME_MAX = 14,
7234 enum tpacpi_volume_access_mode {
7235 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7236 TPACPI_VOL_MODE_EC, /* Pure EC control */
7237 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7238 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7242 enum tpacpi_volume_capabilities {
7243 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7244 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7245 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7249 enum tpacpi_mute_btn_mode {
7250 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7251 /* We don't know what mode 1 is. */
7252 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7253 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7256 static enum tpacpi_volume_access_mode volume_mode =
7257 TPACPI_VOL_MODE_MAX;
7259 static enum tpacpi_volume_capabilities volume_capabilities;
7260 static bool volume_control_allowed;
7261 static bool software_mute_requested = true;
7262 static bool software_mute_active;
7263 static int software_mute_orig_mode;
7266 * Used to syncronize writers to TP_EC_AUDIO and
7267 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7269 static struct mutex volume_mutex;
7271 static void tpacpi_volume_checkpoint_nvram(void)
7277 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7279 if (!volume_control_allowed)
7281 if (software_mute_active)
7284 vdbg_printk(TPACPI_DBG_MIXER,
7285 "trying to checkpoint mixer state to NVRAM...\n");
7287 if (tp_features.mixer_no_level_control)
7288 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7290 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7292 if (mutex_lock_killable(&volume_mutex) < 0)
7295 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7298 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7300 if (lec != (b_nvram & ec_mask)) {
7301 /* NVRAM needs update */
7302 b_nvram &= ~ec_mask;
7304 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7305 dbg_printk(TPACPI_DBG_MIXER,
7306 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7307 (unsigned int) lec, (unsigned int) b_nvram);
7309 vdbg_printk(TPACPI_DBG_MIXER,
7310 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7311 (unsigned int) lec, (unsigned int) b_nvram);
7315 mutex_unlock(&volume_mutex);
7318 static int volume_get_status_ec(u8 *status)
7322 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7327 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7332 static int volume_get_status(u8 *status)
7334 return volume_get_status_ec(status);
7337 static int volume_set_status_ec(const u8 status)
7339 if (!acpi_ec_write(TP_EC_AUDIO, status))
7342 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7345 * On X200s, and possibly on others, it can take a while for
7346 * reads to become correct.
7353 static int volume_set_status(const u8 status)
7355 return volume_set_status_ec(status);
7358 /* returns < 0 on error, 0 on no change, 1 on change */
7359 static int __volume_set_mute_ec(const bool mute)
7364 if (mutex_lock_killable(&volume_mutex) < 0)
7367 rc = volume_get_status_ec(&s);
7371 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7372 s & ~TP_EC_AUDIO_MUTESW_MSK;
7375 rc = volume_set_status_ec(n);
7381 mutex_unlock(&volume_mutex);
7385 static int volume_alsa_set_mute(const bool mute)
7387 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7388 (mute) ? "" : "un");
7389 return __volume_set_mute_ec(mute);
7392 static int volume_set_mute(const bool mute)
7396 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7397 (mute) ? "" : "un");
7399 rc = __volume_set_mute_ec(mute);
7400 return (rc < 0) ? rc : 0;
7403 /* returns < 0 on error, 0 on no change, 1 on change */
7404 static int __volume_set_volume_ec(const u8 vol)
7409 if (vol > TP_EC_VOLUME_MAX)
7412 if (mutex_lock_killable(&volume_mutex) < 0)
7415 rc = volume_get_status_ec(&s);
7419 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7422 rc = volume_set_status_ec(n);
7428 mutex_unlock(&volume_mutex);
7432 static int volume_set_software_mute(bool startup)
7436 if (!tpacpi_is_lenovo())
7440 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7444 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7445 "Initial HAUM setting was %d\n",
7446 software_mute_orig_mode);
7449 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7450 (int)TP_EC_MUTE_BTN_NONE))
7453 if (result != TP_EC_MUTE_BTN_NONE)
7454 pr_warn("Unexpected SAUM result %d\n",
7458 * In software mute mode, the standard codec controls take
7459 * precendence, so we unmute the ThinkPad HW switch at
7460 * startup. Just on case there are SAUM-capable ThinkPads
7461 * with level controls, set max HW volume as well.
7463 if (tp_features.mixer_no_level_control)
7464 result = volume_set_mute(false);
7466 result = volume_set_status(TP_EC_VOLUME_MAX);
7469 pr_warn("Failed to unmute the HW mute switch\n");
7474 static void volume_exit_software_mute(void)
7478 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7479 || r != software_mute_orig_mode)
7480 pr_warn("Failed to restore mute mode\n");
7483 static int volume_alsa_set_volume(const u8 vol)
7485 dbg_printk(TPACPI_DBG_MIXER,
7486 "ALSA: trying to set volume level to %hu\n", vol);
7487 return __volume_set_volume_ec(vol);
7490 static void volume_alsa_notify_change(void)
7492 struct tpacpi_alsa_data *d;
7494 if (alsa_card && alsa_card->private_data) {
7495 d = alsa_card->private_data;
7497 snd_ctl_notify(alsa_card,
7498 SNDRV_CTL_EVENT_MASK_VALUE,
7501 snd_ctl_notify(alsa_card,
7502 SNDRV_CTL_EVENT_MASK_VALUE,
7507 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7508 struct snd_ctl_elem_info *uinfo)
7510 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7512 uinfo->value.integer.min = 0;
7513 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7517 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7518 struct snd_ctl_elem_value *ucontrol)
7523 rc = volume_get_status(&s);
7527 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7531 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7532 struct snd_ctl_elem_value *ucontrol)
7534 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7535 ucontrol->value.integer.value[0]);
7536 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7539 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7541 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7542 struct snd_ctl_elem_value *ucontrol)
7547 rc = volume_get_status(&s);
7551 ucontrol->value.integer.value[0] =
7552 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7556 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7557 struct snd_ctl_elem_value *ucontrol)
7559 tpacpi_disclose_usertask("ALSA", "%smute\n",
7560 ucontrol->value.integer.value[0] ?
7562 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7565 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7566 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7567 .name = "Console Playback Volume",
7569 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7570 .info = volume_alsa_vol_info,
7571 .get = volume_alsa_vol_get,
7574 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7575 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7576 .name = "Console Playback Switch",
7578 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7579 .info = volume_alsa_mute_info,
7580 .get = volume_alsa_mute_get,
7583 static void volume_suspend(void)
7585 tpacpi_volume_checkpoint_nvram();
7588 static void volume_resume(void)
7590 if (software_mute_active) {
7591 if (volume_set_software_mute(false) < 0)
7592 pr_warn("Failed to restore software mute\n");
7594 volume_alsa_notify_change();
7598 static void volume_shutdown(void)
7600 tpacpi_volume_checkpoint_nvram();
7603 static void volume_exit(void)
7606 snd_card_free(alsa_card);
7610 tpacpi_volume_checkpoint_nvram();
7612 if (software_mute_active)
7613 volume_exit_software_mute();
7616 static int __init volume_create_alsa_mixer(void)
7618 struct snd_card *card;
7619 struct tpacpi_alsa_data *data;
7620 struct snd_kcontrol *ctl_vol;
7621 struct snd_kcontrol *ctl_mute;
7624 rc = snd_card_new(&tpacpi_pdev->dev,
7625 alsa_index, alsa_id, THIS_MODULE,
7626 sizeof(struct tpacpi_alsa_data), &card);
7627 if (rc < 0 || !card) {
7628 pr_err("Failed to create ALSA card structures: %d\n", rc);
7632 BUG_ON(!card->private_data);
7633 data = card->private_data;
7636 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7637 sizeof(card->driver));
7638 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7639 sizeof(card->shortname));
7640 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7641 (thinkpad_id.ec_version_str) ?
7642 thinkpad_id.ec_version_str : "(unknown)");
7643 snprintf(card->longname, sizeof(card->longname),
7644 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7645 (thinkpad_id.ec_version_str) ?
7646 thinkpad_id.ec_version_str : "unknown");
7648 if (volume_control_allowed) {
7649 volume_alsa_control_vol.put = volume_alsa_vol_put;
7650 volume_alsa_control_vol.access =
7651 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7653 volume_alsa_control_mute.put = volume_alsa_mute_put;
7654 volume_alsa_control_mute.access =
7655 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7658 if (!tp_features.mixer_no_level_control) {
7659 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7660 rc = snd_ctl_add(card, ctl_vol);
7662 pr_err("Failed to create ALSA volume control: %d\n",
7666 data->ctl_vol_id = &ctl_vol->id;
7669 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7670 rc = snd_ctl_add(card, ctl_mute);
7672 pr_err("Failed to create ALSA mute control: %d\n", rc);
7675 data->ctl_mute_id = &ctl_mute->id;
7677 rc = snd_card_register(card);
7679 pr_err("Failed to register ALSA card: %d\n", rc);
7687 snd_card_free(card);
7691 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7692 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7694 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7695 /* Whitelist volume level on all IBM by default */
7696 { .vendor = PCI_VENDOR_ID_IBM,
7697 .bios = TPACPI_MATCH_ANY,
7698 .ec = TPACPI_MATCH_ANY,
7699 .quirks = TPACPI_VOL_Q_LEVEL },
7701 /* Lenovo models with volume control (needs confirmation) */
7702 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7703 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7704 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7705 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7706 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7707 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7708 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7710 /* Whitelist mute-only on all Lenovo by default */
7711 { .vendor = PCI_VENDOR_ID_LENOVO,
7712 .bios = TPACPI_MATCH_ANY,
7713 .ec = TPACPI_MATCH_ANY,
7714 .quirks = TPACPI_VOL_Q_MUTEONLY }
7717 static int __init volume_init(struct ibm_init_struct *iibm)
7719 unsigned long quirks;
7722 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7724 mutex_init(&volume_mutex);
7727 * Check for module parameter bogosity, note that we
7728 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7729 * able to detect "unspecified"
7731 if (volume_mode > TPACPI_VOL_MODE_MAX)
7734 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7735 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7740 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7744 * The ALSA mixer is our primary interface.
7745 * When disabled, don't install the subdriver at all
7748 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7749 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7753 quirks = tpacpi_check_quirks(volume_quirk_table,
7754 ARRAY_SIZE(volume_quirk_table));
7756 switch (volume_capabilities) {
7757 case TPACPI_VOL_CAP_AUTO:
7758 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7759 tp_features.mixer_no_level_control = 1;
7760 else if (quirks & TPACPI_VOL_Q_LEVEL)
7761 tp_features.mixer_no_level_control = 0;
7763 return 1; /* no mixer */
7765 case TPACPI_VOL_CAP_VOLMUTE:
7766 tp_features.mixer_no_level_control = 0;
7768 case TPACPI_VOL_CAP_MUTEONLY:
7769 tp_features.mixer_no_level_control = 1;
7775 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7776 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7777 "using user-supplied volume_capabilities=%d\n",
7778 volume_capabilities);
7780 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7781 volume_mode == TPACPI_VOL_MODE_MAX) {
7782 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7784 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7785 "driver auto-selected volume_mode=%d\n",
7788 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7789 "using user-supplied volume_mode=%d\n",
7793 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7794 "mute is supported, volume control is %s\n",
7795 str_supported(!tp_features.mixer_no_level_control));
7797 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7798 software_mute_active = true;
7800 rc = volume_create_alsa_mixer();
7802 pr_err("Could not create the ALSA mixer interface\n");
7806 pr_info("Console audio control enabled, mode: %s\n",
7807 (volume_control_allowed) ?
7808 "override (read/write)" :
7809 "monitor (read only)");
7812 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7813 "registering volume hotkeys as change notification\n");
7814 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7815 | TP_ACPI_HKEY_VOLUP_MASK
7816 | TP_ACPI_HKEY_VOLDWN_MASK
7817 | TP_ACPI_HKEY_MUTE_MASK);
7822 static int volume_read(struct seq_file *m)
7826 if (volume_get_status(&status) < 0) {
7827 seq_printf(m, "level:\t\tunreadable\n");
7829 if (tp_features.mixer_no_level_control)
7830 seq_printf(m, "level:\t\tunsupported\n");
7832 seq_printf(m, "level:\t\t%d\n",
7833 status & TP_EC_AUDIO_LVL_MSK);
7835 seq_printf(m, "mute:\t\t%s\n",
7836 onoff(status, TP_EC_AUDIO_MUTESW));
7838 if (volume_control_allowed) {
7839 seq_printf(m, "commands:\tunmute, mute\n");
7840 if (!tp_features.mixer_no_level_control) {
7841 seq_printf(m, "commands:\tup, down\n");
7842 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7851 static int volume_write(char *buf)
7854 u8 new_level, new_mute;
7860 * We do allow volume control at driver startup, so that the
7861 * user can set initial state through the volume=... parameter hack.
7863 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7864 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7865 tp_warned.volume_ctrl_forbidden = 1;
7866 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7867 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7872 rc = volume_get_status(&s);
7876 new_level = s & TP_EC_AUDIO_LVL_MSK;
7877 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7879 while ((cmd = next_cmd(&buf))) {
7880 if (!tp_features.mixer_no_level_control) {
7881 if (strlencmp(cmd, "up") == 0) {
7884 else if (new_level < TP_EC_VOLUME_MAX)
7887 } else if (strlencmp(cmd, "down") == 0) {
7890 else if (new_level > 0)
7893 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7894 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7899 if (strlencmp(cmd, "mute") == 0)
7900 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7901 else if (strlencmp(cmd, "unmute") == 0)
7907 if (tp_features.mixer_no_level_control) {
7908 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7909 new_mute ? "" : "un");
7910 rc = volume_set_mute(!!new_mute);
7912 tpacpi_disclose_usertask("procfs volume",
7913 "%smute and set level to %d\n",
7914 new_mute ? "" : "un", new_level);
7915 rc = volume_set_status(new_mute | new_level);
7917 volume_alsa_notify_change();
7919 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7922 static struct ibm_struct volume_driver_data = {
7924 .read = volume_read,
7925 .write = volume_write,
7926 .exit = volume_exit,
7927 .suspend = volume_suspend,
7928 .resume = volume_resume,
7929 .shutdown = volume_shutdown,
7932 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7934 #define alsa_card NULL
7936 static inline void volume_alsa_notify_change(void)
7940 static int __init volume_init(struct ibm_init_struct *iibm)
7942 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7947 static struct ibm_struct volume_driver_data = {
7951 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7953 /*************************************************************************
7960 * TPACPI_FAN_RD_ACPI_GFAN:
7961 * ACPI GFAN method: returns fan level
7963 * see TPACPI_FAN_WR_ACPI_SFAN
7964 * EC 0x2f (HFSP) not available if GFAN exists
7966 * TPACPI_FAN_WR_ACPI_SFAN:
7967 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7969 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7972 * TPACPI_FAN_WR_TPEC:
7973 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7974 * Supported on almost all ThinkPads
7976 * Fan speed changes of any sort (including those caused by the
7977 * disengaged mode) are usually done slowly by the firmware as the
7978 * maximum amount of fan duty cycle change per second seems to be
7981 * Reading is not available if GFAN exists.
7982 * Writing is not available if SFAN exists.
7985 * 7 automatic mode engaged;
7986 * (default operation mode of the ThinkPad)
7987 * fan level is ignored in this mode.
7988 * 6 full speed mode (takes precedence over bit 7);
7989 * not available on all thinkpads. May disable
7990 * the tachometer while the fan controller ramps up
7991 * the speed (which can take up to a few *minutes*).
7992 * Speeds up fan to 100% duty-cycle, which is far above
7993 * the standard RPM levels. It is not impossible that
7994 * it could cause hardware damage.
7995 * 5-3 unused in some models. Extra bits for fan level
7996 * in others, but still useless as all values above
7997 * 7 map to the same speed as level 7 in these models.
7998 * 2-0 fan level (0..7 usually)
8000 * 0x07 = max (set when temperatures critical)
8001 * Some ThinkPads may have other levels, see
8002 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
8004 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
8005 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
8006 * does so, its initial value is meaningless (0x07).
8008 * For firmware bugs, refer to:
8009 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8013 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8014 * Main fan tachometer reading (in RPM)
8016 * This register is present on all ThinkPads with a new-style EC, and
8017 * it is known not to be present on the A21m/e, and T22, as there is
8018 * something else in offset 0x84 according to the ACPI DSDT. Other
8019 * ThinkPads from this same time period (and earlier) probably lack the
8020 * tachometer as well.
8022 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8023 * was never fixed by IBM to report the EC firmware version string
8024 * probably support the tachometer (like the early X models), so
8025 * detecting it is quite hard. We need more data to know for sure.
8027 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8030 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8033 * For firmware bugs, refer to:
8034 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8038 * ThinkPad EC register 0x31 bit 0 (only on select models)
8040 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8041 * show the speed of the main fan. When bit 0 of EC register 0x31
8042 * is one, the tachometer registers show the speed of the auxiliary
8045 * Fan control seems to affect both fans, regardless of the state
8048 * So far, only the firmware for the X60/X61 non-tablet versions
8049 * seem to support this (firmware TP-7M).
8051 * TPACPI_FAN_WR_ACPI_FANS:
8052 * ThinkPad X31, X40, X41. Not available in the X60.
8054 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8055 * high speed. ACPI DSDT seems to map these three speeds to levels
8056 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8057 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8059 * The speeds are stored on handles
8060 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8062 * There are three default speed sets, accessible as handles:
8063 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8065 * ACPI DSDT switches which set is in use depending on various
8068 * TPACPI_FAN_WR_TPEC is also available and should be used to
8069 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8070 * but the ACPI tables just mention level 7.
8073 enum { /* Fan control constants */
8074 fan_status_offset = 0x2f, /* EC register 0x2f */
8075 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8076 * 0x84 must be read before 0x85 */
8077 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8078 bit 0 selects which fan is active */
8080 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8081 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8083 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8086 enum fan_status_access_mode {
8087 TPACPI_FAN_NONE = 0, /* No fan status or control */
8088 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8089 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8092 enum fan_control_access_mode {
8093 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8094 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8095 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8096 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8099 enum fan_control_commands {
8100 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8101 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8102 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8103 * and also watchdog cmd */
8106 static bool fan_control_allowed;
8108 static enum fan_status_access_mode fan_status_access_mode;
8109 static enum fan_control_access_mode fan_control_access_mode;
8110 static enum fan_control_commands fan_control_commands;
8112 static u8 fan_control_initial_status;
8113 static u8 fan_control_desired_level;
8114 static u8 fan_control_resume_level;
8115 static int fan_watchdog_maxinterval;
8117 static struct mutex fan_mutex;
8119 static void fan_watchdog_fire(struct work_struct *ignored);
8120 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8122 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8123 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8124 "\\FSPD", /* 600e/x, 770e, 770x */
8126 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8127 "JFNS", /* 770x-JL */
8131 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8132 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8133 * be in auto mode (0x80).
8135 * This is corrected by any write to HFSP either by the driver, or
8138 * We assume 0x07 really means auto mode while this quirk is active,
8139 * as this is far more likely than the ThinkPad being in level 7,
8140 * which is only used by the firmware during thermal emergencies.
8142 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8143 * TP-70 (T43, R52), which are known to be buggy.
8146 static void fan_quirk1_setup(void)
8148 if (fan_control_initial_status == 0x07) {
8149 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8150 tp_features.fan_ctrl_status_undef = 1;
8154 static void fan_quirk1_handle(u8 *fan_status)
8156 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8157 if (*fan_status != fan_control_initial_status) {
8158 /* something changed the HFSP regisnter since
8159 * driver init time, so it is not undefined
8161 tp_features.fan_ctrl_status_undef = 0;
8163 /* Return most likely status. In fact, it
8164 * might be the only possible status */
8165 *fan_status = TP_EC_FAN_AUTO;
8170 /* Select main fan on X60/X61, NOOP on others */
8171 static bool fan_select_fan1(void)
8173 if (tp_features.second_fan) {
8176 if (ec_read(fan_select_offset, &val) < 0)
8179 if (ec_write(fan_select_offset, val) < 0)
8185 /* Select secondary fan on X60/X61 */
8186 static bool fan_select_fan2(void)
8190 if (!tp_features.second_fan)
8193 if (ec_read(fan_select_offset, &val) < 0)
8196 if (ec_write(fan_select_offset, val) < 0)
8203 * Call with fan_mutex held
8205 static void fan_update_desired_level(u8 status)
8208 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8210 fan_control_desired_level = 7;
8212 fan_control_desired_level = status;
8216 static int fan_get_status(u8 *status)
8221 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8223 switch (fan_status_access_mode) {
8224 case TPACPI_FAN_RD_ACPI_GFAN: {
8225 /* 570, 600e/x, 770e, 770x */
8228 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8232 *status = res & 0x07;
8236 case TPACPI_FAN_RD_TPEC:
8237 /* all except 570, 600e/x, 770e, 770x */
8238 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8241 if (likely(status)) {
8243 fan_quirk1_handle(status);
8255 static int fan_get_status_safe(u8 *status)
8260 if (mutex_lock_killable(&fan_mutex))
8261 return -ERESTARTSYS;
8262 rc = fan_get_status(&s);
8264 fan_update_desired_level(s);
8265 mutex_unlock(&fan_mutex);
8275 static int fan_get_speed(unsigned int *speed)
8279 switch (fan_status_access_mode) {
8280 case TPACPI_FAN_RD_TPEC:
8281 /* all except 570, 600e/x, 770e, 770x */
8282 if (unlikely(!fan_select_fan1()))
8284 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8285 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8289 *speed = (hi << 8) | lo;
8300 static int fan2_get_speed(unsigned int *speed)
8305 switch (fan_status_access_mode) {
8306 case TPACPI_FAN_RD_TPEC:
8307 /* all except 570, 600e/x, 770e, 770x */
8308 if (unlikely(!fan_select_fan2()))
8310 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8311 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8312 fan_select_fan1(); /* play it safe */
8317 *speed = (hi << 8) | lo;
8328 static int fan_set_level(int level)
8330 if (!fan_control_allowed)
8333 switch (fan_control_access_mode) {
8334 case TPACPI_FAN_WR_ACPI_SFAN:
8335 if (level >= 0 && level <= 7) {
8336 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8342 case TPACPI_FAN_WR_ACPI_FANS:
8343 case TPACPI_FAN_WR_TPEC:
8344 if (!(level & TP_EC_FAN_AUTO) &&
8345 !(level & TP_EC_FAN_FULLSPEED) &&
8346 ((level < 0) || (level > 7)))
8349 /* safety net should the EC not support AUTO
8350 * or FULLSPEED mode bits and just ignore them */
8351 if (level & TP_EC_FAN_FULLSPEED)
8352 level |= 7; /* safety min speed 7 */
8353 else if (level & TP_EC_FAN_AUTO)
8354 level |= 4; /* safety min speed 4 */
8356 if (!acpi_ec_write(fan_status_offset, level))
8359 tp_features.fan_ctrl_status_undef = 0;
8366 vdbg_printk(TPACPI_DBG_FAN,
8367 "fan control: set fan control register to 0x%02x\n", level);
8371 static int fan_set_level_safe(int level)
8375 if (!fan_control_allowed)
8378 if (mutex_lock_killable(&fan_mutex))
8379 return -ERESTARTSYS;
8381 if (level == TPACPI_FAN_LAST_LEVEL)
8382 level = fan_control_desired_level;
8384 rc = fan_set_level(level);
8386 fan_update_desired_level(level);
8388 mutex_unlock(&fan_mutex);
8392 static int fan_set_enable(void)
8397 if (!fan_control_allowed)
8400 if (mutex_lock_killable(&fan_mutex))
8401 return -ERESTARTSYS;
8403 switch (fan_control_access_mode) {
8404 case TPACPI_FAN_WR_ACPI_FANS:
8405 case TPACPI_FAN_WR_TPEC:
8406 rc = fan_get_status(&s);
8410 /* Don't go out of emergency fan mode */
8413 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8416 if (!acpi_ec_write(fan_status_offset, s))
8419 tp_features.fan_ctrl_status_undef = 0;
8424 case TPACPI_FAN_WR_ACPI_SFAN:
8425 rc = fan_get_status(&s);
8431 /* Set fan to at least level 4 */
8434 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8444 mutex_unlock(&fan_mutex);
8447 vdbg_printk(TPACPI_DBG_FAN,
8448 "fan control: set fan control register to 0x%02x\n",
8453 static int fan_set_disable(void)
8457 if (!fan_control_allowed)
8460 if (mutex_lock_killable(&fan_mutex))
8461 return -ERESTARTSYS;
8464 switch (fan_control_access_mode) {
8465 case TPACPI_FAN_WR_ACPI_FANS:
8466 case TPACPI_FAN_WR_TPEC:
8467 if (!acpi_ec_write(fan_status_offset, 0x00))
8470 fan_control_desired_level = 0;
8471 tp_features.fan_ctrl_status_undef = 0;
8475 case TPACPI_FAN_WR_ACPI_SFAN:
8476 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8479 fan_control_desired_level = 0;
8487 vdbg_printk(TPACPI_DBG_FAN,
8488 "fan control: set fan control register to 0\n");
8490 mutex_unlock(&fan_mutex);
8494 static int fan_set_speed(int speed)
8498 if (!fan_control_allowed)
8501 if (mutex_lock_killable(&fan_mutex))
8502 return -ERESTARTSYS;
8505 switch (fan_control_access_mode) {
8506 case TPACPI_FAN_WR_ACPI_FANS:
8507 if (speed >= 0 && speed <= 65535) {
8508 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8509 speed, speed, speed))
8519 mutex_unlock(&fan_mutex);
8523 static void fan_watchdog_reset(void)
8525 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8528 if (fan_watchdog_maxinterval > 0 &&
8529 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8530 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8531 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8533 cancel_delayed_work(&fan_watchdog_task);
8536 static void fan_watchdog_fire(struct work_struct *ignored)
8540 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8543 pr_notice("fan watchdog: enabling fan\n");
8544 rc = fan_set_enable();
8546 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8548 /* reschedule for later */
8549 fan_watchdog_reset();
8554 * SYSFS fan layout: hwmon compatible (device)
8557 * 0: "disengaged" mode
8559 * 2: native EC "auto" mode (recommended, hardware default)
8561 * pwm*: set speed in manual mode, ignored otherwise.
8562 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8565 * fan*_input: tachometer reading, RPM
8568 * SYSFS fan layout: extensions
8570 * fan_watchdog (driver):
8571 * fan watchdog interval in seconds, 0 disables (default), max 120
8574 /* sysfs fan pwm1_enable ----------------------------------------------- */
8575 static ssize_t fan_pwm1_enable_show(struct device *dev,
8576 struct device_attribute *attr,
8582 res = fan_get_status_safe(&status);
8586 if (status & TP_EC_FAN_FULLSPEED) {
8588 } else if (status & TP_EC_FAN_AUTO) {
8593 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8596 static ssize_t fan_pwm1_enable_store(struct device *dev,
8597 struct device_attribute *attr,
8598 const char *buf, size_t count)
8603 if (parse_strtoul(buf, 2, &t))
8606 tpacpi_disclose_usertask("hwmon pwm1_enable",
8607 "set fan mode to %lu\n", t);
8611 level = TP_EC_FAN_FULLSPEED;
8614 level = TPACPI_FAN_LAST_LEVEL;
8617 level = TP_EC_FAN_AUTO;
8620 /* reserved for software-controlled auto mode */
8626 res = fan_set_level_safe(level);
8632 fan_watchdog_reset();
8637 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8638 fan_pwm1_enable_show, fan_pwm1_enable_store);
8640 /* sysfs fan pwm1 ------------------------------------------------------ */
8641 static ssize_t fan_pwm1_show(struct device *dev,
8642 struct device_attribute *attr,
8648 res = fan_get_status_safe(&status);
8653 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8654 status = fan_control_desired_level;
8659 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8662 static ssize_t fan_pwm1_store(struct device *dev,
8663 struct device_attribute *attr,
8664 const char *buf, size_t count)
8668 u8 status, newlevel;
8670 if (parse_strtoul(buf, 255, &s))
8673 tpacpi_disclose_usertask("hwmon pwm1",
8674 "set fan speed to %lu\n", s);
8676 /* scale down from 0-255 to 0-7 */
8677 newlevel = (s >> 5) & 0x07;
8679 if (mutex_lock_killable(&fan_mutex))
8680 return -ERESTARTSYS;
8682 rc = fan_get_status(&status);
8683 if (!rc && (status &
8684 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8685 rc = fan_set_level(newlevel);
8689 fan_update_desired_level(newlevel);
8690 fan_watchdog_reset();
8694 mutex_unlock(&fan_mutex);
8695 return (rc) ? rc : count;
8698 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8700 /* sysfs fan fan1_input ------------------------------------------------ */
8701 static ssize_t fan_fan1_input_show(struct device *dev,
8702 struct device_attribute *attr,
8708 res = fan_get_speed(&speed);
8712 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8715 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8717 /* sysfs fan fan2_input ------------------------------------------------ */
8718 static ssize_t fan_fan2_input_show(struct device *dev,
8719 struct device_attribute *attr,
8725 res = fan2_get_speed(&speed);
8729 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8732 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8734 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8735 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8737 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8740 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8745 if (parse_strtoul(buf, 120, &t))
8748 if (!fan_control_allowed)
8751 fan_watchdog_maxinterval = t;
8752 fan_watchdog_reset();
8754 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8758 static DRIVER_ATTR_RW(fan_watchdog);
8760 /* --------------------------------------------------------------------- */
8761 static struct attribute *fan_attributes[] = {
8762 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8763 &dev_attr_fan1_input.attr,
8764 NULL, /* for fan2_input */
8768 static const struct attribute_group fan_attr_group = {
8769 .attrs = fan_attributes,
8772 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8773 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8775 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8776 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8777 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8778 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8779 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8780 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8781 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8784 static int __init fan_init(struct ibm_init_struct *iibm)
8787 unsigned long quirks;
8789 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8790 "initializing fan subdriver\n");
8792 mutex_init(&fan_mutex);
8793 fan_status_access_mode = TPACPI_FAN_NONE;
8794 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8795 fan_control_commands = 0;
8796 fan_watchdog_maxinterval = 0;
8797 tp_features.fan_ctrl_status_undef = 0;
8798 tp_features.second_fan = 0;
8799 fan_control_desired_level = 7;
8801 if (tpacpi_is_ibm()) {
8802 TPACPI_ACPIHANDLE_INIT(fans);
8803 TPACPI_ACPIHANDLE_INIT(gfan);
8804 TPACPI_ACPIHANDLE_INIT(sfan);
8807 quirks = tpacpi_check_quirks(fan_quirk_table,
8808 ARRAY_SIZE(fan_quirk_table));
8811 /* 570, 600e/x, 770e, 770x */
8812 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8814 /* all other ThinkPads: note that even old-style
8815 * ThinkPad ECs supports the fan control register */
8816 if (likely(acpi_ec_read(fan_status_offset,
8817 &fan_control_initial_status))) {
8818 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8819 if (quirks & TPACPI_FAN_Q1)
8821 if (quirks & TPACPI_FAN_2FAN) {
8822 tp_features.second_fan = 1;
8823 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8824 "secondary fan support enabled\n");
8827 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8834 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8835 fan_control_commands |=
8836 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8839 /* gfan without sfan means no fan control */
8840 /* all other models implement TP EC 0x2f control */
8844 fan_control_access_mode =
8845 TPACPI_FAN_WR_ACPI_FANS;
8846 fan_control_commands |=
8847 TPACPI_FAN_CMD_SPEED |
8848 TPACPI_FAN_CMD_LEVEL |
8849 TPACPI_FAN_CMD_ENABLE;
8851 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8852 fan_control_commands |=
8853 TPACPI_FAN_CMD_LEVEL |
8854 TPACPI_FAN_CMD_ENABLE;
8859 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8860 "fan is %s, modes %d, %d\n",
8861 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8862 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8863 fan_status_access_mode, fan_control_access_mode);
8865 /* fan control master switch */
8866 if (!fan_control_allowed) {
8867 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8868 fan_control_commands = 0;
8869 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8870 "fan control features disabled by parameter\n");
8873 /* update fan_control_desired_level */
8874 if (fan_status_access_mode != TPACPI_FAN_NONE)
8875 fan_get_status_safe(NULL);
8877 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8878 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8879 if (tp_features.second_fan) {
8880 /* attach second fan tachometer */
8881 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8882 &dev_attr_fan2_input.attr;
8884 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8889 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8890 &driver_attr_fan_watchdog);
8892 sysfs_remove_group(&tpacpi_hwmon->kobj,
8901 static void fan_exit(void)
8903 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8904 "cancelling any pending fan watchdog tasks\n");
8906 /* FIXME: can we really do this unconditionally? */
8907 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8908 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8909 &driver_attr_fan_watchdog);
8911 cancel_delayed_work(&fan_watchdog_task);
8912 flush_workqueue(tpacpi_wq);
8915 static void fan_suspend(void)
8919 if (!fan_control_allowed)
8922 /* Store fan status in cache */
8923 fan_control_resume_level = 0;
8924 rc = fan_get_status_safe(&fan_control_resume_level);
8926 pr_notice("failed to read fan level for later restore during resume: %d\n",
8929 /* if it is undefined, don't attempt to restore it.
8931 if (tp_features.fan_ctrl_status_undef)
8932 fan_control_resume_level = 0;
8935 static void fan_resume(void)
8937 u8 current_level = 7;
8938 bool do_set = false;
8941 /* DSDT *always* updates status on resume */
8942 tp_features.fan_ctrl_status_undef = 0;
8944 if (!fan_control_allowed ||
8945 !fan_control_resume_level ||
8946 (fan_get_status_safe(¤t_level) < 0))
8949 switch (fan_control_access_mode) {
8950 case TPACPI_FAN_WR_ACPI_SFAN:
8951 /* never decrease fan level */
8952 do_set = (fan_control_resume_level > current_level);
8954 case TPACPI_FAN_WR_ACPI_FANS:
8955 case TPACPI_FAN_WR_TPEC:
8956 /* never decrease fan level, scale is:
8957 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8959 * We expect the firmware to set either 7 or AUTO, but we
8960 * handle FULLSPEED out of paranoia.
8962 * So, we can safely only restore FULLSPEED or 7, anything
8963 * else could slow the fan. Restoring AUTO is useless, at
8964 * best that's exactly what the DSDT already set (it is the
8967 * Always keep in mind that the DSDT *will* have set the
8968 * fans to what the vendor supposes is the best level. We
8969 * muck with it only to speed the fan up.
8971 if (fan_control_resume_level != 7 &&
8972 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8975 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8976 (current_level != fan_control_resume_level);
8982 pr_notice("restoring fan level to 0x%02x\n",
8983 fan_control_resume_level);
8984 rc = fan_set_level_safe(fan_control_resume_level);
8986 pr_notice("failed to restore fan level: %d\n", rc);
8990 static int fan_read(struct seq_file *m)
8994 unsigned int speed = 0;
8996 switch (fan_status_access_mode) {
8997 case TPACPI_FAN_RD_ACPI_GFAN:
8998 /* 570, 600e/x, 770e, 770x */
8999 rc = fan_get_status_safe(&status);
9003 seq_printf(m, "status:\t\t%s\n"
9005 (status != 0) ? "enabled" : "disabled", status);
9008 case TPACPI_FAN_RD_TPEC:
9009 /* all except 570, 600e/x, 770e, 770x */
9010 rc = fan_get_status_safe(&status);
9014 seq_printf(m, "status:\t\t%s\n",
9015 (status != 0) ? "enabled" : "disabled");
9017 rc = fan_get_speed(&speed);
9021 seq_printf(m, "speed:\t\t%d\n", speed);
9023 if (status & TP_EC_FAN_FULLSPEED)
9024 /* Disengaged mode takes precedence */
9025 seq_printf(m, "level:\t\tdisengaged\n");
9026 else if (status & TP_EC_FAN_AUTO)
9027 seq_printf(m, "level:\t\tauto\n");
9029 seq_printf(m, "level:\t\t%d\n", status);
9032 case TPACPI_FAN_NONE:
9034 seq_printf(m, "status:\t\tnot supported\n");
9037 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9038 seq_printf(m, "commands:\tlevel <level>");
9040 switch (fan_control_access_mode) {
9041 case TPACPI_FAN_WR_ACPI_SFAN:
9042 seq_printf(m, " (<level> is 0-7)\n");
9046 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9051 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9052 seq_printf(m, "commands:\tenable, disable\n"
9053 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9055 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9056 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9061 static int fan_write_cmd_level(const char *cmd, int *rc)
9065 if (strlencmp(cmd, "level auto") == 0)
9066 level = TP_EC_FAN_AUTO;
9067 else if ((strlencmp(cmd, "level disengaged") == 0) |
9068 (strlencmp(cmd, "level full-speed") == 0))
9069 level = TP_EC_FAN_FULLSPEED;
9070 else if (sscanf(cmd, "level %d", &level) != 1)
9073 *rc = fan_set_level_safe(level);
9075 pr_err("level command accepted for unsupported access mode %d\n",
9076 fan_control_access_mode);
9078 tpacpi_disclose_usertask("procfs fan",
9079 "set level to %d\n", level);
9084 static int fan_write_cmd_enable(const char *cmd, int *rc)
9086 if (strlencmp(cmd, "enable") != 0)
9089 *rc = fan_set_enable();
9091 pr_err("enable command accepted for unsupported access mode %d\n",
9092 fan_control_access_mode);
9094 tpacpi_disclose_usertask("procfs fan", "enable\n");
9099 static int fan_write_cmd_disable(const char *cmd, int *rc)
9101 if (strlencmp(cmd, "disable") != 0)
9104 *rc = fan_set_disable();
9106 pr_err("disable command accepted for unsupported access mode %d\n",
9107 fan_control_access_mode);
9109 tpacpi_disclose_usertask("procfs fan", "disable\n");
9114 static int fan_write_cmd_speed(const char *cmd, int *rc)
9119 * Support speed <low> <medium> <high> ? */
9121 if (sscanf(cmd, "speed %d", &speed) != 1)
9124 *rc = fan_set_speed(speed);
9126 pr_err("speed command accepted for unsupported access mode %d\n",
9127 fan_control_access_mode);
9129 tpacpi_disclose_usertask("procfs fan",
9130 "set speed to %d\n", speed);
9135 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9139 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9142 if (interval < 0 || interval > 120)
9145 fan_watchdog_maxinterval = interval;
9146 tpacpi_disclose_usertask("procfs fan",
9147 "set watchdog timer to %d\n",
9154 static int fan_write(char *buf)
9159 while (!rc && (cmd = next_cmd(&buf))) {
9160 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9161 fan_write_cmd_level(cmd, &rc)) &&
9162 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9163 (fan_write_cmd_enable(cmd, &rc) ||
9164 fan_write_cmd_disable(cmd, &rc) ||
9165 fan_write_cmd_watchdog(cmd, &rc))) &&
9166 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9167 fan_write_cmd_speed(cmd, &rc))
9171 fan_watchdog_reset();
9177 static struct ibm_struct fan_driver_data = {
9182 .suspend = fan_suspend,
9183 .resume = fan_resume,
9186 /*************************************************************************
9187 * Mute LED subdriver
9190 #define TPACPI_LED_MAX 2
9192 struct tp_led_table {
9199 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9200 [LED_AUDIO_MUTE] = {
9205 [LED_AUDIO_MICMUTE] = {
9212 static int mute_led_on_off(struct tp_led_table *t, bool state)
9217 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9218 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9222 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9223 state ? t->on_value : t->off_value))
9230 static int tpacpi_led_set(int whichled, bool on)
9232 struct tp_led_table *t;
9234 t = &led_tables[whichled];
9235 if (t->state < 0 || t->state == on)
9237 return mute_led_on_off(t, on);
9240 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9241 enum led_brightness brightness)
9243 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9246 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9247 enum led_brightness brightness)
9249 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9252 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9253 [LED_AUDIO_MUTE] = {
9254 .name = "platform::mute",
9255 .max_brightness = 1,
9256 .brightness_set_blocking = tpacpi_led_mute_set,
9257 .default_trigger = "audio-mute",
9259 [LED_AUDIO_MICMUTE] = {
9260 .name = "platform::micmute",
9261 .max_brightness = 1,
9262 .brightness_set_blocking = tpacpi_led_micmute_set,
9263 .default_trigger = "audio-micmute",
9267 static int mute_led_init(struct ibm_init_struct *iibm)
9272 for (i = 0; i < TPACPI_LED_MAX; i++) {
9273 struct tp_led_table *t = &led_tables[i];
9274 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9279 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9280 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9283 if (led_tables[i].state >= 0)
9284 led_classdev_unregister(&mute_led_cdev[i]);
9292 static void mute_led_exit(void)
9296 for (i = 0; i < TPACPI_LED_MAX; i++) {
9297 if (led_tables[i].state >= 0) {
9298 led_classdev_unregister(&mute_led_cdev[i]);
9299 tpacpi_led_set(i, false);
9304 static void mute_led_resume(void)
9308 for (i = 0; i < TPACPI_LED_MAX; i++) {
9309 struct tp_led_table *t = &led_tables[i];
9311 mute_led_on_off(t, t->state);
9315 static struct ibm_struct mute_led_driver_data = {
9317 .exit = mute_led_exit,
9318 .resume = mute_led_resume,
9322 * Battery Wear Control Driver
9323 * Contact: Ognjen Galic <smclt30p@gmail.com>
9328 #define GET_START "BCTG"
9329 #define SET_START "BCCS"
9330 #define GET_STOP "BCSG"
9331 #define SET_STOP "BCSS"
9333 #define START_ATTR "charge_start_threshold"
9334 #define STOP_ATTR "charge_stop_threshold"
9343 /* Error condition bit */
9344 METHOD_ERR = BIT(31),
9348 /* This is used in the get/set helpers */
9353 struct tpacpi_battery_data {
9360 struct tpacpi_battery_driver_data {
9361 struct tpacpi_battery_data batteries[3];
9362 int individual_addressing;
9365 static struct tpacpi_battery_driver_data battery_info;
9367 /* ACPI helpers/functions/probes */
9370 * This evaluates a ACPI method call specific to the battery
9371 * ACPI extension. The specifics are that an error is marked
9372 * in the 32rd bit of the response, so we just check that here.
9374 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9378 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9379 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9382 if (response & METHOD_ERR) {
9383 acpi_handle_err(hkey_handle,
9384 "%s evaluated but flagged as error", method);
9391 static int tpacpi_battery_get(int what, int battery, int *ret)
9394 case THRESHOLD_START:
9395 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9398 /* The value is in the low 8 bits of the response */
9401 case THRESHOLD_STOP:
9402 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9404 /* Value is in lower 8 bits */
9407 * On the stop value, if we return 0 that
9408 * does not make any sense. 0 means Default, which
9409 * means that charging stops at 100%, so we return
9416 pr_crit("wrong parameter: %d", what);
9421 static int tpacpi_battery_set(int what, int battery, int value)
9424 /* The first 8 bits are the value of the threshold */
9426 /* The battery ID is in bits 8-9, 2 bits */
9427 param |= battery << 8;
9430 case THRESHOLD_START:
9431 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9432 pr_err("failed to set charge threshold on battery %d",
9437 case THRESHOLD_STOP:
9438 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9439 pr_err("failed to set stop threshold: %d", battery);
9444 pr_crit("wrong parameter: %d", what);
9449 static int tpacpi_battery_probe(int battery)
9453 memset(&battery_info.batteries[battery], 0,
9454 sizeof(battery_info.batteries[battery]));
9457 * 1) Get the current start threshold
9458 * 2) Check for support
9459 * 3) Get the current stop threshold
9460 * 4) Check for support
9462 if (acpi_has_method(hkey_handle, GET_START)) {
9463 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9464 pr_err("Error probing battery %d\n", battery);
9467 /* Individual addressing is in bit 9 */
9469 battery_info.individual_addressing = true;
9470 /* Support is marked in bit 8 */
9472 battery_info.batteries[battery].start_support = 1;
9475 if (tpacpi_battery_get(THRESHOLD_START, battery,
9476 &battery_info.batteries[battery].charge_start)) {
9477 pr_err("Error probing battery %d\n", battery);
9481 if (acpi_has_method(hkey_handle, GET_STOP)) {
9482 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9483 pr_err("Error probing battery stop; %d\n", battery);
9486 /* Support is marked in bit 8 */
9488 battery_info.batteries[battery].stop_support = 1;
9491 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9492 &battery_info.batteries[battery].charge_stop)) {
9493 pr_err("Error probing battery stop: %d\n", battery);
9497 pr_info("battery %d registered (start %d, stop %d)",
9499 battery_info.batteries[battery].charge_start,
9500 battery_info.batteries[battery].charge_stop);
9505 /* General helper functions */
9507 static int tpacpi_battery_get_id(const char *battery_name)
9510 if (strcmp(battery_name, "BAT0") == 0 ||
9511 tp_features.battery_force_primary)
9513 if (strcmp(battery_name, "BAT1") == 0)
9514 return BAT_SECONDARY;
9516 * If for some reason the battery is not BAT0 nor is it
9517 * BAT1, we will assume it's the default, first battery,
9520 pr_warn("unknown battery %s, assuming primary", battery_name);
9524 /* sysfs interface */
9526 static ssize_t tpacpi_battery_store(int what,
9528 const char *buf, size_t count)
9530 struct power_supply *supply = to_power_supply(dev);
9531 unsigned long value;
9534 * Some systems have support for more than
9535 * one battery. If that is the case,
9536 * tpacpi_battery_probe marked that addressing
9537 * them individually is supported, so we do that
9538 * based on the device struct.
9540 * On systems that are not supported, we assume
9541 * the primary as most of the ACPI calls fail
9542 * with "Any Battery" as the parameter.
9544 if (battery_info.individual_addressing)
9545 /* BAT_PRIMARY or BAT_SECONDARY */
9546 battery = tpacpi_battery_get_id(supply->desc->name);
9548 battery = BAT_PRIMARY;
9550 rval = kstrtoul(buf, 10, &value);
9555 case THRESHOLD_START:
9556 if (!battery_info.batteries[battery].start_support)
9558 /* valid values are [0, 99] */
9561 if (value > battery_info.batteries[battery].charge_stop)
9563 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9565 battery_info.batteries[battery].charge_start = value;
9568 case THRESHOLD_STOP:
9569 if (!battery_info.batteries[battery].stop_support)
9571 /* valid values are [1, 100] */
9572 if (value < 1 || value > 100)
9574 if (value < battery_info.batteries[battery].charge_start)
9576 battery_info.batteries[battery].charge_stop = value;
9578 * When 100 is passed to stop, we need to flip
9579 * it to 0 as that the EC understands that as
9580 * "Default", which will charge to 100%
9584 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9588 pr_crit("Wrong parameter: %d", what);
9594 static ssize_t tpacpi_battery_show(int what,
9598 struct power_supply *supply = to_power_supply(dev);
9601 * Some systems have support for more than
9602 * one battery. If that is the case,
9603 * tpacpi_battery_probe marked that addressing
9604 * them individually is supported, so we;
9605 * based on the device struct.
9607 * On systems that are not supported, we assume
9608 * the primary as most of the ACPI calls fail
9609 * with "Any Battery" as the parameter.
9611 if (battery_info.individual_addressing)
9612 /* BAT_PRIMARY or BAT_SECONDARY */
9613 battery = tpacpi_battery_get_id(supply->desc->name);
9615 battery = BAT_PRIMARY;
9616 if (tpacpi_battery_get(what, battery, &ret))
9618 return sprintf(buf, "%d\n", ret);
9621 static ssize_t charge_start_threshold_show(struct device *device,
9622 struct device_attribute *attr,
9625 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9628 static ssize_t charge_stop_threshold_show(struct device *device,
9629 struct device_attribute *attr,
9632 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9635 static ssize_t charge_start_threshold_store(struct device *dev,
9636 struct device_attribute *attr,
9637 const char *buf, size_t count)
9639 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9642 static ssize_t charge_stop_threshold_store(struct device *dev,
9643 struct device_attribute *attr,
9644 const char *buf, size_t count)
9646 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9649 static DEVICE_ATTR_RW(charge_start_threshold);
9650 static DEVICE_ATTR_RW(charge_stop_threshold);
9652 static struct attribute *tpacpi_battery_attrs[] = {
9653 &dev_attr_charge_start_threshold.attr,
9654 &dev_attr_charge_stop_threshold.attr,
9658 ATTRIBUTE_GROUPS(tpacpi_battery);
9660 /* ACPI battery hooking */
9662 static int tpacpi_battery_add(struct power_supply *battery)
9664 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9666 if (tpacpi_battery_probe(batteryid))
9668 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9673 static int tpacpi_battery_remove(struct power_supply *battery)
9675 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9679 static struct acpi_battery_hook battery_hook = {
9680 .add_battery = tpacpi_battery_add,
9681 .remove_battery = tpacpi_battery_remove,
9682 .name = "ThinkPad Battery Extension",
9685 /* Subdriver init/exit */
9687 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9689 * Individual addressing is broken on models that expose the
9690 * primary battery as BAT1.
9692 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9693 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9694 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9695 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9696 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9699 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9701 memset(&battery_info, 0, sizeof(battery_info));
9703 tp_features.battery_force_primary = tpacpi_check_quirks(
9704 battery_quirk_table,
9705 ARRAY_SIZE(battery_quirk_table));
9707 battery_hook_register(&battery_hook);
9711 static void tpacpi_battery_exit(void)
9713 battery_hook_unregister(&battery_hook);
9716 static struct ibm_struct battery_driver_data = {
9718 .exit = tpacpi_battery_exit,
9721 /*************************************************************************
9722 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9725 static int lcdshadow_state;
9727 static int lcdshadow_on_off(bool state)
9729 acpi_handle set_shadow_handle;
9732 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSSS", &set_shadow_handle))) {
9733 pr_warn("Thinkpad ACPI has no %s interface.\n", "SSSS");
9737 if (!acpi_evalf(set_shadow_handle, &output, NULL, "dd", (int)state))
9740 lcdshadow_state = state;
9744 static int lcdshadow_set(bool on)
9746 if (lcdshadow_state < 0)
9747 return lcdshadow_state;
9748 if (lcdshadow_state == on)
9750 return lcdshadow_on_off(on);
9753 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9755 acpi_handle get_shadow_handle;
9758 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSSS", &get_shadow_handle))) {
9759 lcdshadow_state = -ENODEV;
9763 if (!acpi_evalf(get_shadow_handle, &output, NULL, "dd", 0)) {
9764 lcdshadow_state = -EIO;
9767 if (!(output & 0x10000)) {
9768 lcdshadow_state = -ENODEV;
9771 lcdshadow_state = output & 0x1;
9776 static void lcdshadow_resume(void)
9778 if (lcdshadow_state >= 0)
9779 lcdshadow_on_off(lcdshadow_state);
9782 static int lcdshadow_read(struct seq_file *m)
9784 if (lcdshadow_state < 0) {
9785 seq_puts(m, "status:\t\tnot supported\n");
9787 seq_printf(m, "status:\t\t%d\n", lcdshadow_state);
9788 seq_puts(m, "commands:\t0, 1\n");
9794 static int lcdshadow_write(char *buf)
9799 if (lcdshadow_state < 0)
9802 while ((cmd = next_cmd(&buf))) {
9803 if (strlencmp(cmd, "0") == 0)
9805 else if (strlencmp(cmd, "1") == 0)
9812 return lcdshadow_set(state);
9815 static struct ibm_struct lcdshadow_driver_data = {
9816 .name = "lcdshadow",
9817 .resume = lcdshadow_resume,
9818 .read = lcdshadow_read,
9819 .write = lcdshadow_write,
9822 /****************************************************************************
9823 ****************************************************************************
9827 ****************************************************************************
9828 ****************************************************************************/
9831 * HKEY event callout for other subdrivers go here
9832 * (yes, it is ugly, but it is quick, safe, and gets the job done
9834 static void tpacpi_driver_event(const unsigned int hkey_event)
9836 if (ibm_backlight_device) {
9837 switch (hkey_event) {
9838 case TP_HKEY_EV_BRGHT_UP:
9839 case TP_HKEY_EV_BRGHT_DOWN:
9840 tpacpi_brightness_notify_change();
9844 switch (hkey_event) {
9845 case TP_HKEY_EV_VOL_UP:
9846 case TP_HKEY_EV_VOL_DOWN:
9847 case TP_HKEY_EV_VOL_MUTE:
9848 volume_alsa_notify_change();
9851 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9852 enum led_brightness brightness;
9854 mutex_lock(&kbdlight_mutex);
9857 * Check the brightness actually changed, setting the brightness
9858 * through kbdlight_set_level() also triggers this event.
9860 brightness = kbdlight_sysfs_get(NULL);
9861 if (kbdlight_brightness != brightness) {
9862 kbdlight_brightness = brightness;
9863 led_classdev_notify_brightness_hw_changed(
9864 &tpacpi_led_kbdlight.led_classdev, brightness);
9867 mutex_unlock(&kbdlight_mutex);
9871 static void hotkey_driver_event(const unsigned int scancode)
9873 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9876 /* --------------------------------------------------------------------- */
9879 static struct proc_dir_entry *proc_dir;
9882 * Module and infrastructure proble, init and exit handling
9885 static bool force_load;
9887 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9888 static const char * __init str_supported(int is_supported)
9890 static char text_unsupported[] __initdata = "not supported";
9892 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9894 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9896 static void ibm_exit(struct ibm_struct *ibm)
9898 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9900 list_del_init(&ibm->all_drivers);
9902 if (ibm->flags.acpi_notify_installed) {
9903 dbg_printk(TPACPI_DBG_EXIT,
9904 "%s: acpi_remove_notify_handler\n", ibm->name);
9906 acpi_remove_notify_handler(*ibm->acpi->handle,
9908 dispatch_acpi_notify);
9909 ibm->flags.acpi_notify_installed = 0;
9912 if (ibm->flags.proc_created) {
9913 dbg_printk(TPACPI_DBG_EXIT,
9914 "%s: remove_proc_entry\n", ibm->name);
9915 remove_proc_entry(ibm->name, proc_dir);
9916 ibm->flags.proc_created = 0;
9919 if (ibm->flags.acpi_driver_registered) {
9920 dbg_printk(TPACPI_DBG_EXIT,
9921 "%s: acpi_bus_unregister_driver\n", ibm->name);
9923 acpi_bus_unregister_driver(ibm->acpi->driver);
9924 kfree(ibm->acpi->driver);
9925 ibm->acpi->driver = NULL;
9926 ibm->flags.acpi_driver_registered = 0;
9929 if (ibm->flags.init_called && ibm->exit) {
9931 ibm->flags.init_called = 0;
9934 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9937 static int __init ibm_init(struct ibm_init_struct *iibm)
9940 struct ibm_struct *ibm = iibm->data;
9941 struct proc_dir_entry *entry;
9943 BUG_ON(ibm == NULL);
9945 INIT_LIST_HEAD(&ibm->all_drivers);
9947 if (ibm->flags.experimental && !experimental)
9950 dbg_printk(TPACPI_DBG_INIT,
9951 "probing for %s\n", ibm->name);
9954 ret = iibm->init(iibm);
9956 return 0; /* probe failed */
9960 ibm->flags.init_called = 1;
9964 if (ibm->acpi->hid) {
9965 ret = register_tpacpi_subdriver(ibm);
9970 if (ibm->acpi->notify) {
9971 ret = setup_acpi_notify(ibm);
9972 if (ret == -ENODEV) {
9973 pr_notice("disabling subdriver %s\n",
9983 dbg_printk(TPACPI_DBG_INIT,
9984 "%s installed\n", ibm->name);
9987 umode_t mode = iibm->base_procfs_mode;
9993 entry = proc_create_data(ibm->name, mode, proc_dir,
9994 &dispatch_proc_ops, ibm);
9996 pr_err("unable to create proc entry %s\n", ibm->name);
10000 ibm->flags.proc_created = 1;
10003 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10008 dbg_printk(TPACPI_DBG_INIT,
10009 "%s: at error exit path with result %d\n",
10013 return (ret < 0) ? ret : 0;
10018 static char __init tpacpi_parse_fw_id(const char * const s,
10019 u32 *model, u16 *release)
10023 if (!s || strlen(s) < 8)
10026 for (i = 0; i < 8; i++)
10027 if (!((s[i] >= '0' && s[i] <= '9') ||
10028 (s[i] >= 'A' && s[i] <= 'Z')))
10032 * Most models: xxyTkkWW (#.##c)
10033 * Ancient 570/600 and -SL lacks (#.##c)
10035 if (s[3] == 'T' || s[3] == 'N') {
10036 *model = TPID(s[0], s[1]);
10037 *release = TPVER(s[4], s[5]);
10040 /* New models: xxxyTkkW (#.##c); T550 and some others */
10041 } else if (s[4] == 'T' || s[4] == 'N') {
10042 *model = TPID3(s[0], s[1], s[2]);
10043 *release = TPVER(s[5], s[6]);
10051 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10053 char *ec_fw_string = (char *) private;
10054 const char *dmi_data = (const char *)dm;
10056 * ThinkPad Embedded Controller Program Table on newer models
10058 * Offset | Name | Width | Description
10059 * ----------------------------------------------------
10060 * 0x00 | Type | BYTE | 0x8C
10061 * 0x01 | Length | BYTE |
10062 * 0x02 | Handle | WORD | Varies
10063 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
10064 * 0x0A | OEM struct offset | BYTE | 0x0B
10065 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
10066 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
10067 * 0x0D | ECP version ID | STR ID |
10068 * 0x0E | ECP release date | STR ID |
10071 /* Return if data structure not match */
10072 if (dm->type != 140 || dm->length < 0x0F ||
10073 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10074 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
10075 dmi_data[0x0C] != 0x01)
10078 /* fwstr is the first 8byte string */
10079 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
10082 /* returns 0 - probe ok, or < 0 - probe error.
10083 * Probe ok doesn't mean thinkpad found.
10084 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
10085 static int __must_check __init get_thinkpad_model_data(
10086 struct thinkpad_id_data *tp)
10088 const struct dmi_device *dev = NULL;
10089 char ec_fw_string[18] = {0};
10096 memset(tp, 0, sizeof(*tp));
10098 if (dmi_name_in_vendors("IBM"))
10099 tp->vendor = PCI_VENDOR_ID_IBM;
10100 else if (dmi_name_in_vendors("LENOVO"))
10101 tp->vendor = PCI_VENDOR_ID_LENOVO;
10105 s = dmi_get_system_info(DMI_BIOS_VERSION);
10106 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
10107 if (s && !tp->bios_version_str)
10110 /* Really ancient ThinkPad 240X will fail this, which is fine */
10111 t = tpacpi_parse_fw_id(tp->bios_version_str,
10112 &tp->bios_model, &tp->bios_release);
10113 if (t != 'E' && t != 'C')
10117 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
10118 * X32 or newer, all Z series; Some models must have an
10119 * up-to-date BIOS or they will not be detected.
10121 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10123 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
10124 if (sscanf(dev->name,
10125 "IBM ThinkPad Embedded Controller -[%17c",
10126 ec_fw_string) == 1) {
10127 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10128 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
10133 /* Newer ThinkPads have different EC program info table */
10134 if (!ec_fw_string[0])
10135 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
10137 if (ec_fw_string[0]) {
10138 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
10139 if (!tp->ec_version_str)
10142 t = tpacpi_parse_fw_id(ec_fw_string,
10143 &tp->ec_model, &tp->ec_release);
10145 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
10147 pr_notice("please report this to %s\n", TPACPI_MAIL);
10151 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
10152 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
10153 tp->model_str = kstrdup(s, GFP_KERNEL);
10154 if (!tp->model_str)
10157 s = dmi_get_system_info(DMI_BIOS_VENDOR);
10158 if (s && !(strncasecmp(s, "Lenovo", 6))) {
10159 tp->model_str = kstrdup(s, GFP_KERNEL);
10160 if (!tp->model_str)
10165 s = dmi_get_system_info(DMI_PRODUCT_NAME);
10166 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10167 if (s && !tp->nummodel_str)
10173 static int __init probe_for_thinkpad(void)
10180 /* It would be dangerous to run the driver in this case */
10181 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10185 * Non-ancient models have better DMI tagging, but very old models
10186 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
10188 is_thinkpad = (thinkpad_id.model_str != NULL) ||
10189 (thinkpad_id.ec_model != 0) ||
10190 tpacpi_is_fw_known();
10192 /* The EC handler is required */
10193 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
10196 pr_err("Not yet supported ThinkPad detected!\n");
10200 if (!is_thinkpad && !force_load)
10206 static void __init thinkpad_acpi_init_banner(void)
10208 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
10209 pr_info("%s\n", TPACPI_URL);
10211 pr_info("ThinkPad BIOS %s, EC %s\n",
10212 (thinkpad_id.bios_version_str) ?
10213 thinkpad_id.bios_version_str : "unknown",
10214 (thinkpad_id.ec_version_str) ?
10215 thinkpad_id.ec_version_str : "unknown");
10217 BUG_ON(!thinkpad_id.vendor);
10219 if (thinkpad_id.model_str)
10220 pr_info("%s %s, model %s\n",
10221 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10222 "IBM" : ((thinkpad_id.vendor ==
10223 PCI_VENDOR_ID_LENOVO) ?
10224 "Lenovo" : "Unknown vendor"),
10225 thinkpad_id.model_str,
10226 (thinkpad_id.nummodel_str) ?
10227 thinkpad_id.nummodel_str : "unknown");
10230 /* Module init, exit, parameters */
10232 static struct ibm_init_struct ibms_init[] __initdata = {
10234 .data = &thinkpad_acpi_driver_data,
10237 .init = hotkey_init,
10238 .data = &hotkey_driver_data,
10241 .init = bluetooth_init,
10242 .data = &bluetooth_driver_data,
10246 .data = &wan_driver_data,
10250 .data = &uwb_driver_data,
10252 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
10254 .init = video_init,
10255 .base_procfs_mode = S_IRUSR,
10256 .data = &video_driver_data,
10260 .init = kbdlight_init,
10261 .data = &kbdlight_driver_data,
10264 .init = light_init,
10265 .data = &light_driver_data,
10269 .data = &cmos_driver_data,
10273 .data = &led_driver_data,
10277 .data = &beep_driver_data,
10280 .init = thermal_init,
10281 .data = &thermal_driver_data,
10284 .init = brightness_init,
10285 .data = &brightness_driver_data,
10288 .init = volume_init,
10289 .data = &volume_driver_data,
10293 .data = &fan_driver_data,
10296 .init = mute_led_init,
10297 .data = &mute_led_driver_data,
10300 .init = tpacpi_battery_init,
10301 .data = &battery_driver_data,
10304 .init = tpacpi_lcdshadow_init,
10305 .data = &lcdshadow_driver_data,
10309 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
10312 struct ibm_struct *ibm;
10314 if (!kp || !kp->name || !val)
10317 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10318 ibm = ibms_init[i].data;
10319 WARN_ON(ibm == NULL);
10321 if (!ibm || !ibm->name)
10324 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10325 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
10327 strcpy(ibms_init[i].param, val);
10328 strcat(ibms_init[i].param, ",");
10336 module_param(experimental, int, 0444);
10337 MODULE_PARM_DESC(experimental,
10338 "Enables experimental features when non-zero");
10340 module_param_named(debug, dbg_level, uint, 0);
10341 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
10343 module_param(force_load, bool, 0444);
10344 MODULE_PARM_DESC(force_load,
10345 "Attempts to load the driver even on a mis-identified ThinkPad when true");
10347 module_param_named(fan_control, fan_control_allowed, bool, 0444);
10348 MODULE_PARM_DESC(fan_control,
10349 "Enables setting fan parameters features when true");
10351 module_param_named(brightness_mode, brightness_mode, uint, 0444);
10352 MODULE_PARM_DESC(brightness_mode,
10353 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
10355 module_param(brightness_enable, uint, 0444);
10356 MODULE_PARM_DESC(brightness_enable,
10357 "Enables backlight control when 1, disables when 0");
10359 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
10360 module_param_named(volume_mode, volume_mode, uint, 0444);
10361 MODULE_PARM_DESC(volume_mode,
10362 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
10364 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10365 MODULE_PARM_DESC(volume_capabilities,
10366 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
10368 module_param_named(volume_control, volume_control_allowed, bool, 0444);
10369 MODULE_PARM_DESC(volume_control,
10370 "Enables software override for the console audio control when true");
10372 module_param_named(software_mute, software_mute_requested, bool, 0444);
10373 MODULE_PARM_DESC(software_mute,
10374 "Request full software mute control");
10376 /* ALSA module API parameters */
10377 module_param_named(index, alsa_index, int, 0444);
10378 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10379 module_param_named(id, alsa_id, charp, 0444);
10380 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10381 module_param_named(enable, alsa_enable, bool, 0444);
10382 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
10383 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
10385 /* The module parameter can't be read back, that's why 0 is used here */
10386 #define TPACPI_PARAM(feature) \
10387 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
10388 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
10390 TPACPI_PARAM(hotkey);
10391 TPACPI_PARAM(bluetooth);
10392 TPACPI_PARAM(video);
10393 TPACPI_PARAM(light);
10394 TPACPI_PARAM(cmos);
10396 TPACPI_PARAM(beep);
10397 TPACPI_PARAM(brightness);
10398 TPACPI_PARAM(volume);
10401 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10402 module_param(dbg_wlswemul, uint, 0444);
10403 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10404 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10405 MODULE_PARM_DESC(wlsw_state,
10406 "Initial state of the emulated WLSW switch");
10408 module_param(dbg_bluetoothemul, uint, 0444);
10409 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10410 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10411 MODULE_PARM_DESC(bluetooth_state,
10412 "Initial state of the emulated bluetooth switch");
10414 module_param(dbg_wwanemul, uint, 0444);
10415 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10416 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10417 MODULE_PARM_DESC(wwan_state,
10418 "Initial state of the emulated WWAN switch");
10420 module_param(dbg_uwbemul, uint, 0444);
10421 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10422 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10423 MODULE_PARM_DESC(uwb_state,
10424 "Initial state of the emulated UWB switch");
10427 static void thinkpad_acpi_module_exit(void)
10429 struct ibm_struct *ibm, *itmp;
10431 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10433 list_for_each_entry_safe_reverse(ibm, itmp,
10434 &tpacpi_all_drivers,
10439 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10441 if (tpacpi_inputdev) {
10442 if (tp_features.input_device_registered)
10443 input_unregister_device(tpacpi_inputdev);
10445 input_free_device(tpacpi_inputdev);
10446 kfree(hotkey_keycode_map);
10450 hwmon_device_unregister(tpacpi_hwmon);
10452 if (tpacpi_sensors_pdev)
10453 platform_device_unregister(tpacpi_sensors_pdev);
10455 platform_device_unregister(tpacpi_pdev);
10457 if (tp_features.sensors_pdrv_attrs_registered)
10458 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10459 if (tp_features.platform_drv_attrs_registered)
10460 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10462 if (tp_features.sensors_pdrv_registered)
10463 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10465 if (tp_features.platform_drv_registered)
10466 platform_driver_unregister(&tpacpi_pdriver);
10469 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
10472 destroy_workqueue(tpacpi_wq);
10474 kfree(thinkpad_id.bios_version_str);
10475 kfree(thinkpad_id.ec_version_str);
10476 kfree(thinkpad_id.model_str);
10477 kfree(thinkpad_id.nummodel_str);
10481 static int __init thinkpad_acpi_module_init(void)
10485 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10487 /* Driver-level probe */
10489 ret = get_thinkpad_model_data(&thinkpad_id);
10491 pr_err("unable to get DMI data: %d\n", ret);
10492 thinkpad_acpi_module_exit();
10495 ret = probe_for_thinkpad();
10497 thinkpad_acpi_module_exit();
10501 /* Driver initialization */
10503 thinkpad_acpi_init_banner();
10504 tpacpi_check_outdated_fw();
10506 TPACPI_ACPIHANDLE_INIT(ecrd);
10507 TPACPI_ACPIHANDLE_INIT(ecwr);
10509 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10511 thinkpad_acpi_module_exit();
10515 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
10517 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
10518 thinkpad_acpi_module_exit();
10522 ret = platform_driver_register(&tpacpi_pdriver);
10524 pr_err("unable to register main platform driver\n");
10525 thinkpad_acpi_module_exit();
10528 tp_features.platform_drv_registered = 1;
10530 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10532 pr_err("unable to register hwmon platform driver\n");
10533 thinkpad_acpi_module_exit();
10536 tp_features.sensors_pdrv_registered = 1;
10538 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
10540 tp_features.platform_drv_attrs_registered = 1;
10541 ret = tpacpi_create_driver_attributes(
10542 &tpacpi_hwmon_pdriver.driver);
10545 pr_err("unable to create sysfs driver attributes\n");
10546 thinkpad_acpi_module_exit();
10549 tp_features.sensors_pdrv_attrs_registered = 1;
10552 /* Device initialization */
10553 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
10555 if (IS_ERR(tpacpi_pdev)) {
10556 ret = PTR_ERR(tpacpi_pdev);
10557 tpacpi_pdev = NULL;
10558 pr_err("unable to register platform device\n");
10559 thinkpad_acpi_module_exit();
10562 tpacpi_sensors_pdev = platform_device_register_simple(
10563 TPACPI_HWMON_DRVR_NAME,
10565 if (IS_ERR(tpacpi_sensors_pdev)) {
10566 ret = PTR_ERR(tpacpi_sensors_pdev);
10567 tpacpi_sensors_pdev = NULL;
10568 pr_err("unable to register hwmon platform device\n");
10569 thinkpad_acpi_module_exit();
10572 tp_features.sensors_pdev_attrs_registered = 1;
10573 tpacpi_hwmon = hwmon_device_register_with_groups(
10574 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10576 if (IS_ERR(tpacpi_hwmon)) {
10577 ret = PTR_ERR(tpacpi_hwmon);
10578 tpacpi_hwmon = NULL;
10579 pr_err("unable to register hwmon device\n");
10580 thinkpad_acpi_module_exit();
10583 mutex_init(&tpacpi_inputdev_send_mutex);
10584 tpacpi_inputdev = input_allocate_device();
10585 if (!tpacpi_inputdev) {
10586 thinkpad_acpi_module_exit();
10589 /* Prepare input device, but don't register */
10590 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
10591 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
10592 tpacpi_inputdev->id.bustype = BUS_HOST;
10593 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
10594 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10595 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
10596 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
10599 /* Init subdriver dependencies */
10600 tpacpi_detect_brightness_capabilities();
10602 /* Init subdrivers */
10603 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10604 ret = ibm_init(&ibms_init[i]);
10605 if (ret >= 0 && *ibms_init[i].param)
10606 ret = ibms_init[i].data->write(ibms_init[i].param);
10608 thinkpad_acpi_module_exit();
10613 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10615 ret = input_register_device(tpacpi_inputdev);
10617 pr_err("unable to register input device\n");
10618 thinkpad_acpi_module_exit();
10621 tp_features.input_device_registered = 1;
10627 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10630 * This will autoload the driver in almost every ThinkPad
10631 * in widespread use.
10633 * Only _VERY_ old models, like the 240, 240x and 570 lack
10634 * the HKEY event interface.
10636 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10639 * DMI matching for module autoloading
10641 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10642 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10644 * Only models listed in thinkwiki will be supported, so add yours
10645 * if it is not there yet.
10647 #define IBM_BIOS_MODULE_ALIAS(__type) \
10648 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10650 /* Ancient thinkpad BIOSes have to be identified by
10651 * BIOS type or model number, and there are far less
10652 * BIOS types than model numbers... */
10653 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
10655 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10656 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
10657 MODULE_DESCRIPTION(TPACPI_DESC);
10658 MODULE_VERSION(TPACPI_VERSION);
10659 MODULE_LICENSE("GPL");
10661 module_init(thinkpad_acpi_module_init);
10662 module_exit(thinkpad_acpi_module_exit);