2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.25"
27 #define TPACPI_SYSFS_VERSION 0x030000
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
70 #include <linux/platform_device.h>
71 #include <linux/hwmon.h>
72 #include <linux/hwmon-sysfs.h>
73 #include <linux/input.h>
74 #include <linux/leds.h>
75 #include <linux/rfkill.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
79 #include <linux/acpi.h>
80 #include <linux/pci.h>
81 #include <linux/thinkpad_acpi.h>
82 #include <sound/core.h>
83 #include <sound/control.h>
84 #include <sound/initval.h>
85 #include <linux/uaccess.h>
86 #include <acpi/video.h>
88 /* ThinkPad CMOS commands */
89 #define TP_CMOS_VOLUME_DOWN 0
90 #define TP_CMOS_VOLUME_UP 1
91 #define TP_CMOS_VOLUME_MUTE 2
92 #define TP_CMOS_BRIGHTNESS_UP 4
93 #define TP_CMOS_BRIGHTNESS_DOWN 5
94 #define TP_CMOS_THINKLIGHT_ON 12
95 #define TP_CMOS_THINKLIGHT_OFF 13
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
106 /* NVRAM bit masks */
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
123 /* Misc NVRAM-related */
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161 enum tpacpi_hkey_event_t {
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
217 /* Further user-interface events */
218 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
219 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
222 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
225 /****************************************************************************
229 #define TPACPI_NAME "thinkpad"
230 #define TPACPI_DESC "ThinkPad ACPI Extras"
231 #define TPACPI_FILE TPACPI_NAME "_acpi"
232 #define TPACPI_URL "http://ibm-acpi.sf.net/"
233 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
235 #define TPACPI_PROC_DIR "ibm"
236 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
237 #define TPACPI_DRVR_NAME TPACPI_FILE
238 #define TPACPI_DRVR_SHORTNAME "tpacpi"
239 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
241 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
242 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
244 #define TPACPI_MAX_ACPI_ARGS 3
246 /* Debugging printk groups */
247 #define TPACPI_DBG_ALL 0xffff
248 #define TPACPI_DBG_DISCLOSETASK 0x8000
249 #define TPACPI_DBG_INIT 0x0001
250 #define TPACPI_DBG_EXIT 0x0002
251 #define TPACPI_DBG_RFKILL 0x0004
252 #define TPACPI_DBG_HKEY 0x0008
253 #define TPACPI_DBG_FAN 0x0010
254 #define TPACPI_DBG_BRGHT 0x0020
255 #define TPACPI_DBG_MIXER 0x0040
257 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
258 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
259 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
262 /****************************************************************************
263 * Driver-wide structs and misc. variables
268 struct tp_acpi_drv_struct {
269 const struct acpi_device_id *hid;
270 struct acpi_driver *driver;
272 void (*notify) (struct ibm_struct *, u32);
275 struct acpi_device *device;
281 int (*read) (struct seq_file *);
282 int (*write) (char *);
284 void (*resume) (void);
285 void (*suspend) (void);
286 void (*shutdown) (void);
288 struct list_head all_drivers;
290 struct tp_acpi_drv_struct *acpi;
293 u8 acpi_driver_registered:1;
294 u8 acpi_notify_installed:1;
301 struct ibm_init_struct {
304 int (*init) (struct ibm_init_struct *);
305 umode_t base_procfs_mode;
306 struct ibm_struct *data;
315 TP_HOTKEY_TABLET_NONE = 0,
316 TP_HOTKEY_TABLET_USES_MHKG,
317 /* X1 Yoga 2016, seen on BIOS N1FET44W */
318 TP_HOTKEY_TABLET_USES_CMMD,
323 u32 bright_acpimode:1;
327 u32 fan_ctrl_status_undef:1;
329 u32 beep_needs_two_args:1;
330 u32 mixer_no_level_control:1;
331 u32 input_device_registered:1;
332 u32 platform_drv_registered:1;
333 u32 platform_drv_attrs_registered:1;
334 u32 sensors_pdrv_registered:1;
335 u32 sensors_pdrv_attrs_registered:1;
336 u32 sensors_pdev_attrs_registered:1;
337 u32 hotkey_poll_active:1;
338 u32 has_adaptive_kbd:1;
342 u16 hotkey_mask_ff:1;
343 u16 volume_ctrl_forbidden:1;
346 struct thinkpad_id_data {
347 unsigned int vendor; /* ThinkPad vendor:
348 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
350 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
351 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
353 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
355 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
358 char *model_str; /* ThinkPad T43 */
359 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
361 static struct thinkpad_id_data thinkpad_id;
364 TPACPI_LIFE_INIT = 0,
369 static int experimental;
370 static u32 dbg_level;
372 static struct workqueue_struct *tpacpi_wq;
380 /* tpacpi LED class */
381 struct tpacpi_led_classdev {
382 struct led_classdev led_classdev;
386 /* brightness level capabilities */
387 static unsigned int bright_maxlvl; /* 0 = unknown */
389 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
390 static int dbg_wlswemul;
391 static bool tpacpi_wlsw_emulstate;
392 static int dbg_bluetoothemul;
393 static bool tpacpi_bluetooth_emulstate;
394 static int dbg_wwanemul;
395 static bool tpacpi_wwan_emulstate;
396 static int dbg_uwbemul;
397 static bool tpacpi_uwb_emulstate;
401 /*************************************************************************
405 #define dbg_printk(a_dbg_level, format, arg...) \
407 if (dbg_level & (a_dbg_level)) \
408 printk(KERN_DEBUG pr_fmt("%s: " format), \
412 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
413 #define vdbg_printk dbg_printk
414 static const char *str_supported(int is_supported);
416 static inline const char *str_supported(int is_supported) { return ""; }
417 #define vdbg_printk(a_dbg_level, format, arg...) \
418 do { if (0) no_printk(format, ##arg); } while (0)
421 static void tpacpi_log_usertask(const char * const what)
423 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
424 what, task_tgid_vnr(current));
427 #define tpacpi_disclose_usertask(what, format, arg...) \
429 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
430 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
431 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
432 what, task_tgid_vnr(current), ## arg); \
437 * Quirk handling helpers
439 * ThinkPad IDs and versions seen in the field so far
440 * are two-characters from the set [0-9A-Z], i.e. base 36.
442 * We use values well outside that range as specials.
445 #define TPACPI_MATCH_ANY 0xffffU
446 #define TPACPI_MATCH_UNKNOWN 0U
448 /* TPID('1', 'Y') == 0x5931 */
449 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
451 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
452 { .vendor = PCI_VENDOR_ID_IBM, \
453 .bios = TPID(__id1, __id2), \
454 .ec = TPACPI_MATCH_ANY, \
455 .quirks = (__quirk) }
457 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
458 { .vendor = PCI_VENDOR_ID_LENOVO, \
459 .bios = TPID(__id1, __id2), \
460 .ec = TPACPI_MATCH_ANY, \
461 .quirks = (__quirk) }
463 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
464 { .vendor = PCI_VENDOR_ID_LENOVO, \
465 .bios = TPACPI_MATCH_ANY, \
466 .ec = TPID(__id1, __id2), \
467 .quirks = (__quirk) }
469 struct tpacpi_quirk {
473 unsigned long quirks;
477 * tpacpi_check_quirks() - search BIOS/EC version on a list
478 * @qlist: array of &struct tpacpi_quirk
479 * @qlist_size: number of elements in @qlist
481 * Iterates over a quirks list until one is found that matches the
482 * ThinkPad's vendor, BIOS and EC model.
484 * Returns 0 if nothing matches, otherwise returns the quirks field of
485 * the matching &struct tpacpi_quirk entry.
487 * The match criteria is: vendor, ec and bios much match.
489 static unsigned long __init tpacpi_check_quirks(
490 const struct tpacpi_quirk *qlist,
491 unsigned int qlist_size)
494 if ((qlist->vendor == thinkpad_id.vendor ||
495 qlist->vendor == TPACPI_MATCH_ANY) &&
496 (qlist->bios == thinkpad_id.bios_model ||
497 qlist->bios == TPACPI_MATCH_ANY) &&
498 (qlist->ec == thinkpad_id.ec_model ||
499 qlist->ec == TPACPI_MATCH_ANY))
500 return qlist->quirks;
508 static inline bool __pure __init tpacpi_is_lenovo(void)
510 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
513 static inline bool __pure __init tpacpi_is_ibm(void)
515 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
518 /****************************************************************************
519 ****************************************************************************
521 * ACPI Helpers and device model
523 ****************************************************************************
524 ****************************************************************************/
526 /*************************************************************************
530 static acpi_handle root_handle;
531 static acpi_handle ec_handle;
533 #define TPACPI_HANDLE(object, parent, paths...) \
534 static acpi_handle object##_handle; \
535 static const acpi_handle * const object##_parent __initconst = \
537 static char *object##_paths[] __initdata = { paths }
539 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
540 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
542 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
544 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
545 "\\CMS", /* R40, R40e */
548 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
549 "^HKEY", /* R30, R31 */
550 "HKEY", /* all others */
553 /*************************************************************************
557 static int acpi_evalf(acpi_handle handle,
558 int *res, char *method, char *fmt, ...)
561 struct acpi_object_list params;
562 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
563 struct acpi_buffer result, *resultp;
564 union acpi_object out_obj;
572 pr_err("acpi_evalf() called with empty format\n");
585 params.pointer = &in_objs[0];
592 in_objs[params.count].integer.value = va_arg(ap, int);
593 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
595 /* add more types as needed */
597 pr_err("acpi_evalf() called with invalid format character '%c'\n",
605 if (res_type != 'v') {
606 result.length = sizeof(out_obj);
607 result.pointer = &out_obj;
612 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
616 success = (status == AE_OK &&
617 out_obj.type == ACPI_TYPE_INTEGER);
619 *res = out_obj.integer.value;
622 success = status == AE_OK;
624 /* add more types as needed */
626 pr_err("acpi_evalf() called with invalid format character '%c'\n",
631 if (!success && !quiet)
632 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
633 method, fmt0, acpi_format_exception(status));
638 static int acpi_ec_read(int i, u8 *p)
643 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
647 if (ec_read(i, p) < 0)
654 static int acpi_ec_write(int i, u8 v)
657 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
660 if (ec_write(i, v) < 0)
667 static int issue_thinkpad_cmos_command(int cmos_cmd)
672 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
678 /*************************************************************************
682 #define TPACPI_ACPIHANDLE_INIT(object) \
683 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
684 object##_paths, ARRAY_SIZE(object##_paths))
686 static void __init drv_acpi_handle_init(const char *name,
687 acpi_handle *handle, const acpi_handle parent,
688 char **paths, const int num_paths)
693 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
696 for (i = 0; i < num_paths; i++) {
697 status = acpi_get_handle(parent, paths[i], handle);
698 if (ACPI_SUCCESS(status)) {
699 dbg_printk(TPACPI_DBG_INIT,
700 "Found ACPI handle %s for %s\n",
706 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
711 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
712 u32 level, void *context, void **return_value)
714 struct acpi_device *dev;
715 if (!strcmp(context, "video")) {
716 if (acpi_bus_get_device(handle, &dev))
718 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
722 *(acpi_handle *)return_value = handle;
724 return AE_CTRL_TERMINATE;
727 static void __init tpacpi_acpi_handle_locate(const char *name,
732 acpi_handle device_found;
734 BUG_ON(!name || !handle);
735 vdbg_printk(TPACPI_DBG_INIT,
736 "trying to locate ACPI handle for %s, using HID %s\n",
737 name, hid ? hid : "NULL");
739 memset(&device_found, 0, sizeof(device_found));
740 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
741 (void *)name, &device_found);
745 if (ACPI_SUCCESS(status)) {
746 *handle = device_found;
747 dbg_printk(TPACPI_DBG_INIT,
748 "Found ACPI handle for %s\n", name);
750 vdbg_printk(TPACPI_DBG_INIT,
751 "Could not locate an ACPI handle for %s: %s\n",
752 name, acpi_format_exception(status));
756 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
758 struct ibm_struct *ibm = data;
760 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
763 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
766 ibm->acpi->notify(ibm, event);
769 static int __init setup_acpi_notify(struct ibm_struct *ibm)
776 if (!*ibm->acpi->handle)
779 vdbg_printk(TPACPI_DBG_INIT,
780 "setting up ACPI notify for %s\n", ibm->name);
782 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
784 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
788 ibm->acpi->device->driver_data = ibm;
789 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
790 TPACPI_ACPI_EVENT_PREFIX,
793 status = acpi_install_notify_handler(*ibm->acpi->handle,
794 ibm->acpi->type, dispatch_acpi_notify, ibm);
795 if (ACPI_FAILURE(status)) {
796 if (status == AE_ALREADY_EXISTS) {
797 pr_notice("another device driver is already handling %s events\n",
800 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
801 ibm->name, acpi_format_exception(status));
805 ibm->flags.acpi_notify_installed = 1;
809 static int __init tpacpi_device_add(struct acpi_device *device)
814 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
818 dbg_printk(TPACPI_DBG_INIT,
819 "registering %s as an ACPI driver\n", ibm->name);
823 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
824 if (!ibm->acpi->driver) {
825 pr_err("failed to allocate memory for ibm->acpi->driver\n");
829 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
830 ibm->acpi->driver->ids = ibm->acpi->hid;
832 ibm->acpi->driver->ops.add = &tpacpi_device_add;
834 rc = acpi_bus_register_driver(ibm->acpi->driver);
836 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
838 kfree(ibm->acpi->driver);
839 ibm->acpi->driver = NULL;
841 ibm->flags.acpi_driver_registered = 1;
847 /****************************************************************************
848 ****************************************************************************
852 ****************************************************************************
853 ****************************************************************************/
855 static int dispatch_proc_show(struct seq_file *m, void *v)
857 struct ibm_struct *ibm = m->private;
859 if (!ibm || !ibm->read)
864 static int dispatch_proc_open(struct inode *inode, struct file *file)
866 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
869 static ssize_t dispatch_proc_write(struct file *file,
870 const char __user *userbuf,
871 size_t count, loff_t *pos)
873 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
877 if (!ibm || !ibm->write)
879 if (count > PAGE_SIZE - 2)
882 kernbuf = kmalloc(count + 2, GFP_KERNEL);
886 if (copy_from_user(kernbuf, userbuf, count)) {
892 strcat(kernbuf, ",");
893 ret = ibm->write(kernbuf);
902 static const struct file_operations dispatch_proc_fops = {
903 .owner = THIS_MODULE,
904 .open = dispatch_proc_open,
907 .release = single_release,
908 .write = dispatch_proc_write,
911 static char *next_cmd(char **cmds)
916 while ((end = strchr(start, ',')) && end == start)
928 /****************************************************************************
929 ****************************************************************************
931 * Device model: input, hwmon and platform
933 ****************************************************************************
934 ****************************************************************************/
936 static struct platform_device *tpacpi_pdev;
937 static struct platform_device *tpacpi_sensors_pdev;
938 static struct device *tpacpi_hwmon;
939 static struct input_dev *tpacpi_inputdev;
940 static struct mutex tpacpi_inputdev_send_mutex;
941 static LIST_HEAD(tpacpi_all_drivers);
943 #ifdef CONFIG_PM_SLEEP
944 static int tpacpi_suspend_handler(struct device *dev)
946 struct ibm_struct *ibm, *itmp;
948 list_for_each_entry_safe(ibm, itmp,
958 static int tpacpi_resume_handler(struct device *dev)
960 struct ibm_struct *ibm, *itmp;
962 list_for_each_entry_safe(ibm, itmp,
973 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
974 tpacpi_suspend_handler, tpacpi_resume_handler);
976 static void tpacpi_shutdown_handler(struct platform_device *pdev)
978 struct ibm_struct *ibm, *itmp;
980 list_for_each_entry_safe(ibm, itmp,
988 static struct platform_driver tpacpi_pdriver = {
990 .name = TPACPI_DRVR_NAME,
993 .shutdown = tpacpi_shutdown_handler,
996 static struct platform_driver tpacpi_hwmon_pdriver = {
998 .name = TPACPI_HWMON_DRVR_NAME,
1002 /*************************************************************************
1003 * sysfs support helpers
1006 struct attribute_set {
1007 unsigned int members, max_members;
1008 struct attribute_group group;
1011 struct attribute_set_obj {
1012 struct attribute_set s;
1013 struct attribute *a;
1014 } __attribute__((packed));
1016 static struct attribute_set *create_attr_set(unsigned int max_members,
1019 struct attribute_set_obj *sobj;
1021 if (max_members == 0)
1024 /* Allocates space for implicit NULL at the end too */
1025 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1026 max_members * sizeof(struct attribute *),
1030 sobj->s.max_members = max_members;
1031 sobj->s.group.attrs = &sobj->a;
1032 sobj->s.group.name = name;
1037 #define destroy_attr_set(_set) \
1040 /* not multi-threaded safe, use it in a single thread per set */
1041 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1046 if (s->members >= s->max_members)
1049 s->group.attrs[s->members] = attr;
1055 static int add_many_to_attr_set(struct attribute_set *s,
1056 struct attribute **attr,
1061 for (i = 0; i < count; i++) {
1062 res = add_to_attr_set(s, attr[i]);
1070 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1072 sysfs_remove_group(kobj, &s->group);
1073 destroy_attr_set(s);
1076 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1077 sysfs_create_group(_kobj, &_attr_set->group)
1079 static int parse_strtoul(const char *buf,
1080 unsigned long max, unsigned long *value)
1084 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1085 endp = skip_spaces(endp);
1086 if (*endp || *value > max)
1092 static void tpacpi_disable_brightness_delay(void)
1094 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1095 pr_notice("ACPI backlight control delay disabled\n");
1098 static void printk_deprecated_attribute(const char * const what,
1099 const char * const details)
1101 tpacpi_log_usertask("deprecated sysfs attribute");
1102 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1106 /*************************************************************************
1107 * rfkill and radio control support helpers
1111 * ThinkPad-ACPI firmware handling model:
1113 * WLSW (master wireless switch) is event-driven, and is common to all
1114 * firmware-controlled radios. It cannot be controlled, just monitored,
1115 * as expected. It overrides all radio state in firmware
1117 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1118 * (TODO: verify how WLSW interacts with the returned radio state).
1120 * The only time there are shadow radio state changes, is when
1121 * masked-off hotkeys are used.
1125 * Internal driver API for radio state:
1127 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1128 * bool: true means radio blocked (off)
1130 enum tpacpi_rfkill_state {
1131 TPACPI_RFK_RADIO_OFF = 0,
1135 /* rfkill switches */
1136 enum tpacpi_rfk_id {
1137 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1138 TPACPI_RFK_WWAN_SW_ID,
1139 TPACPI_RFK_UWB_SW_ID,
1143 static const char *tpacpi_rfkill_names[] = {
1144 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1145 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1146 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1147 [TPACPI_RFK_SW_MAX] = NULL
1150 /* ThinkPad-ACPI rfkill subdriver */
1152 struct rfkill *rfkill;
1153 enum tpacpi_rfk_id id;
1154 const struct tpacpi_rfk_ops *ops;
1157 struct tpacpi_rfk_ops {
1158 /* firmware interface */
1159 int (*get_status)(void);
1160 int (*set_status)(const enum tpacpi_rfkill_state);
1163 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1165 /* Query FW and update rfkill sw state for a given rfkill switch */
1166 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1173 status = (tp_rfk->ops->get_status)();
1177 rfkill_set_sw_state(tp_rfk->rfkill,
1178 (status == TPACPI_RFK_RADIO_OFF));
1183 /* Query FW and update rfkill sw state for all rfkill switches */
1184 static void tpacpi_rfk_update_swstate_all(void)
1188 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1189 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1193 * Sync the HW-blocking state of all rfkill switches,
1194 * do notice it causes the rfkill core to schedule uevents
1196 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1199 struct tpacpi_rfk *tp_rfk;
1201 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1202 tp_rfk = tpacpi_rfkill_switches[i];
1204 if (rfkill_set_hw_state(tp_rfk->rfkill,
1206 /* ignore -- we track sw block */
1212 /* Call to get the WLSW state from the firmware */
1213 static int hotkey_get_wlsw(void);
1215 /* Call to query WLSW state and update all rfkill switches */
1216 static bool tpacpi_rfk_check_hwblock_state(void)
1218 int res = hotkey_get_wlsw();
1221 /* When unknown or unsupported, we have to assume it is unblocked */
1225 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1226 tpacpi_rfk_update_hwblock_state(hw_blocked);
1231 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1233 struct tpacpi_rfk *tp_rfk = data;
1236 dbg_printk(TPACPI_DBG_RFKILL,
1237 "request to change radio state to %s\n",
1238 blocked ? "blocked" : "unblocked");
1240 /* try to set radio state */
1241 res = (tp_rfk->ops->set_status)(blocked ?
1242 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1244 /* and update the rfkill core with whatever the FW really did */
1245 tpacpi_rfk_update_swstate(tp_rfk);
1247 return (res < 0) ? res : 0;
1250 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1251 .set_block = tpacpi_rfk_hook_set_block,
1254 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1255 const struct tpacpi_rfk_ops *tp_rfkops,
1256 const enum rfkill_type rfktype,
1258 const bool set_default)
1260 struct tpacpi_rfk *atp_rfk;
1262 bool sw_state = false;
1266 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1268 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1270 atp_rfk->rfkill = rfkill_alloc(name,
1273 &tpacpi_rfk_rfkill_ops,
1275 if (!atp_rfk || !atp_rfk->rfkill) {
1276 pr_err("failed to allocate memory for rfkill class\n");
1282 atp_rfk->ops = tp_rfkops;
1284 sw_status = (tp_rfkops->get_status)();
1285 if (sw_status < 0) {
1286 pr_err("failed to read initial state for %s, error %d\n",
1289 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1291 /* try to keep the initial state, since we ask the
1292 * firmware to preserve it across S5 in NVRAM */
1293 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1296 hw_state = tpacpi_rfk_check_hwblock_state();
1297 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1299 res = rfkill_register(atp_rfk->rfkill);
1301 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1302 rfkill_destroy(atp_rfk->rfkill);
1307 tpacpi_rfkill_switches[id] = atp_rfk;
1309 pr_info("rfkill switch %s: radio is %sblocked\n",
1310 name, (sw_state || hw_state) ? "" : "un");
1314 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1316 struct tpacpi_rfk *tp_rfk;
1318 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1320 tp_rfk = tpacpi_rfkill_switches[id];
1322 rfkill_unregister(tp_rfk->rfkill);
1323 rfkill_destroy(tp_rfk->rfkill);
1324 tpacpi_rfkill_switches[id] = NULL;
1329 static void printk_deprecated_rfkill_attribute(const char * const what)
1331 printk_deprecated_attribute(what,
1332 "Please switch to generic rfkill before year 2010");
1335 /* sysfs <radio> enable ------------------------------------------------ */
1336 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1337 struct device_attribute *attr,
1342 printk_deprecated_rfkill_attribute(attr->attr.name);
1344 /* This is in the ABI... */
1345 if (tpacpi_rfk_check_hwblock_state()) {
1346 status = TPACPI_RFK_RADIO_OFF;
1348 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1353 return snprintf(buf, PAGE_SIZE, "%d\n",
1354 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1357 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1358 struct device_attribute *attr,
1359 const char *buf, size_t count)
1364 printk_deprecated_rfkill_attribute(attr->attr.name);
1366 if (parse_strtoul(buf, 1, &t))
1369 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1371 /* This is in the ABI... */
1372 if (tpacpi_rfk_check_hwblock_state() && !!t)
1375 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1376 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1377 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1379 return (res < 0) ? res : count;
1382 /* procfs -------------------------------------------------------------- */
1383 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1385 if (id >= TPACPI_RFK_SW_MAX)
1386 seq_printf(m, "status:\t\tnot supported\n");
1390 /* This is in the ABI... */
1391 if (tpacpi_rfk_check_hwblock_state()) {
1392 status = TPACPI_RFK_RADIO_OFF;
1394 status = tpacpi_rfk_update_swstate(
1395 tpacpi_rfkill_switches[id]);
1400 seq_printf(m, "status:\t\t%s\n",
1401 (status == TPACPI_RFK_RADIO_ON) ?
1402 "enabled" : "disabled");
1403 seq_printf(m, "commands:\tenable, disable\n");
1409 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1415 if (id >= TPACPI_RFK_SW_MAX)
1418 while ((cmd = next_cmd(&buf))) {
1419 if (strlencmp(cmd, "enable") == 0)
1420 status = TPACPI_RFK_RADIO_ON;
1421 else if (strlencmp(cmd, "disable") == 0)
1422 status = TPACPI_RFK_RADIO_OFF;
1428 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1429 (status == TPACPI_RFK_RADIO_ON) ?
1430 "enable" : "disable",
1431 tpacpi_rfkill_names[id]);
1432 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1433 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1439 /*************************************************************************
1440 * thinkpad-acpi driver attributes
1443 /* interface_version --------------------------------------------------- */
1444 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1446 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1448 static DRIVER_ATTR_RO(interface_version);
1450 /* debug_level --------------------------------------------------------- */
1451 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1453 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1456 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1461 if (parse_strtoul(buf, 0xffff, &t))
1468 static DRIVER_ATTR_RW(debug_level);
1470 /* version ------------------------------------------------------------- */
1471 static ssize_t version_show(struct device_driver *drv, char *buf)
1473 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1474 TPACPI_DESC, TPACPI_VERSION);
1476 static DRIVER_ATTR_RO(version);
1478 /* --------------------------------------------------------------------- */
1480 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1482 /* wlsw_emulstate ------------------------------------------------------ */
1483 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1485 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1488 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1493 if (parse_strtoul(buf, 1, &t))
1496 if (tpacpi_wlsw_emulstate != !!t) {
1497 tpacpi_wlsw_emulstate = !!t;
1498 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1503 static DRIVER_ATTR_RW(wlsw_emulstate);
1505 /* bluetooth_emulstate ------------------------------------------------- */
1506 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1508 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1511 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1512 const char *buf, size_t count)
1516 if (parse_strtoul(buf, 1, &t))
1519 tpacpi_bluetooth_emulstate = !!t;
1523 static DRIVER_ATTR_RW(bluetooth_emulstate);
1525 /* wwan_emulstate ------------------------------------------------- */
1526 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1528 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1531 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1536 if (parse_strtoul(buf, 1, &t))
1539 tpacpi_wwan_emulstate = !!t;
1543 static DRIVER_ATTR_RW(wwan_emulstate);
1545 /* uwb_emulstate ------------------------------------------------- */
1546 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1548 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1551 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1556 if (parse_strtoul(buf, 1, &t))
1559 tpacpi_uwb_emulstate = !!t;
1563 static DRIVER_ATTR_RW(uwb_emulstate);
1566 /* --------------------------------------------------------------------- */
1568 static struct driver_attribute *tpacpi_driver_attributes[] = {
1569 &driver_attr_debug_level, &driver_attr_version,
1570 &driver_attr_interface_version,
1573 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1579 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1580 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1584 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1585 if (!res && dbg_wlswemul)
1586 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1587 if (!res && dbg_bluetoothemul)
1588 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1589 if (!res && dbg_wwanemul)
1590 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1591 if (!res && dbg_uwbemul)
1592 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1598 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1602 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1603 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1605 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1606 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1607 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1608 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1609 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1613 /*************************************************************************
1618 * Table of recommended minimum BIOS versions
1620 * Reasons for listing:
1621 * 1. Stable BIOS, listed because the unknown amount of
1622 * bugs and bad ACPI behaviour on older versions
1624 * 2. BIOS or EC fw with known bugs that trigger on Linux
1626 * 3. BIOS with known reduced functionality in older versions
1628 * We recommend the latest BIOS and EC version.
1629 * We only support the latest BIOS and EC fw version as a rule.
1631 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1632 * Information from users in ThinkWiki
1634 * WARNING: we use this table also to detect that the machine is
1635 * a ThinkPad in some cases, so don't remove entries lightly.
1638 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1639 { .vendor = (__v), \
1640 .bios = TPID(__id1, __id2), \
1641 .ec = TPACPI_MATCH_ANY, \
1642 .quirks = TPACPI_MATCH_ANY << 16 \
1643 | (__bv1) << 8 | (__bv2) }
1645 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1646 __eid, __ev1, __ev2) \
1647 { .vendor = (__v), \
1648 .bios = TPID(__bid1, __bid2), \
1650 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1651 | (__bv1) << 8 | (__bv2) }
1653 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1654 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1656 /* Outdated IBM BIOSes often lack the EC id string */
1657 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1658 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1659 __bv1, __bv2, TPID(__id1, __id2), \
1661 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1662 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1665 /* Outdated IBM BIOSes often lack the EC id string */
1666 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1667 __eid1, __eid2, __ev1, __ev2) \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1669 __bv1, __bv2, TPID(__eid1, __eid2), \
1671 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1672 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1675 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1676 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1678 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1679 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1680 __bv1, __bv2, TPID(__id1, __id2), \
1683 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1684 __eid1, __eid2, __ev1, __ev2) \
1685 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1686 __bv1, __bv2, TPID(__eid1, __eid2), \
1689 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1690 /* Numeric models ------------------ */
1691 /* FW MODEL BIOS VERS */
1692 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1693 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1694 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1695 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1696 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1697 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1698 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1699 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1700 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1702 /* A-series ------------------------- */
1703 /* FW MODEL BIOS VERS EC VERS */
1704 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1705 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1706 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1707 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1708 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1709 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1710 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1711 TPV_QI0('1', '3', '2', '0'), /* A22m */
1712 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1713 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1714 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1716 /* G-series ------------------------- */
1717 /* FW MODEL BIOS VERS */
1718 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1719 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1721 /* R-series, T-series --------------- */
1722 /* FW MODEL BIOS VERS EC VERS */
1723 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1724 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1725 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1726 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1727 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1728 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1729 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1730 T40/p, T41/p, T42/p (1) */
1731 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1732 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1733 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1734 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1736 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1737 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1738 TPV_QI0('1', '6', '3', '2'), /* T22 */
1739 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1740 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1741 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1743 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1744 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1745 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1747 /* BIOS FW BIOS VERS EC FW EC VERS */
1748 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1749 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1751 /* X-series ------------------------- */
1752 /* FW MODEL BIOS VERS EC VERS */
1753 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1754 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1755 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1756 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1757 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1758 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1759 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1761 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1762 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1764 /* (0) - older versions lack DMI EC fw string and functionality */
1765 /* (1) - older versions known to lack functionality */
1776 static void __init tpacpi_check_outdated_fw(void)
1778 unsigned long fwvers;
1779 u16 ec_version, bios_version;
1781 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1782 ARRAY_SIZE(tpacpi_bios_version_qtable));
1787 bios_version = fwvers & 0xffffU;
1788 ec_version = (fwvers >> 16) & 0xffffU;
1790 /* note that unknown versions are set to 0x0000 and we use that */
1791 if ((bios_version > thinkpad_id.bios_release) ||
1792 (ec_version > thinkpad_id.ec_release &&
1793 ec_version != TPACPI_MATCH_ANY)) {
1795 * The changelogs would let us track down the exact
1796 * reason, but it is just too much of a pain to track
1797 * it. We only list BIOSes that are either really
1798 * broken, or really stable to begin with, so it is
1799 * best if the user upgrades the firmware anyway.
1801 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1802 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1806 static bool __init tpacpi_is_fw_known(void)
1808 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1809 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1812 /****************************************************************************
1813 ****************************************************************************
1817 ****************************************************************************
1818 ****************************************************************************/
1820 /*************************************************************************
1821 * thinkpad-acpi metadata subdriver
1824 static int thinkpad_acpi_driver_read(struct seq_file *m)
1826 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1827 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1831 static struct ibm_struct thinkpad_acpi_driver_data = {
1833 .read = thinkpad_acpi_driver_read,
1836 /*************************************************************************
1841 * ThinkPad firmware event model
1843 * The ThinkPad firmware has two main event interfaces: normal ACPI
1844 * notifications (which follow the ACPI standard), and a private event
1847 * The private event interface also issues events for the hotkeys. As
1848 * the driver gained features, the event handling code ended up being
1849 * built around the hotkey subdriver. This will need to be refactored
1850 * to a more formal event API eventually.
1852 * Some "hotkeys" are actually supposed to be used as event reports,
1853 * such as "brightness has changed", "volume has changed", depending on
1854 * the ThinkPad model and how the firmware is operating.
1856 * Unlike other classes, hotkey-class events have mask/unmask control on
1857 * non-ancient firmware. However, how it behaves changes a lot with the
1858 * firmware model and version.
1861 enum { /* hot key scan codes (derived from ACPI DSDT) */
1862 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1863 TP_ACPI_HOTKEYSCAN_FNF2,
1864 TP_ACPI_HOTKEYSCAN_FNF3,
1865 TP_ACPI_HOTKEYSCAN_FNF4,
1866 TP_ACPI_HOTKEYSCAN_FNF5,
1867 TP_ACPI_HOTKEYSCAN_FNF6,
1868 TP_ACPI_HOTKEYSCAN_FNF7,
1869 TP_ACPI_HOTKEYSCAN_FNF8,
1870 TP_ACPI_HOTKEYSCAN_FNF9,
1871 TP_ACPI_HOTKEYSCAN_FNF10,
1872 TP_ACPI_HOTKEYSCAN_FNF11,
1873 TP_ACPI_HOTKEYSCAN_FNF12,
1874 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1875 TP_ACPI_HOTKEYSCAN_FNINSERT,
1876 TP_ACPI_HOTKEYSCAN_FNDELETE,
1877 TP_ACPI_HOTKEYSCAN_FNHOME,
1878 TP_ACPI_HOTKEYSCAN_FNEND,
1879 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1880 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1881 TP_ACPI_HOTKEYSCAN_FNSPACE,
1882 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1883 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1884 TP_ACPI_HOTKEYSCAN_MUTE,
1885 TP_ACPI_HOTKEYSCAN_THINKPAD,
1886 TP_ACPI_HOTKEYSCAN_UNK1,
1887 TP_ACPI_HOTKEYSCAN_UNK2,
1888 TP_ACPI_HOTKEYSCAN_UNK3,
1889 TP_ACPI_HOTKEYSCAN_UNK4,
1890 TP_ACPI_HOTKEYSCAN_UNK5,
1891 TP_ACPI_HOTKEYSCAN_UNK6,
1892 TP_ACPI_HOTKEYSCAN_UNK7,
1893 TP_ACPI_HOTKEYSCAN_UNK8,
1895 /* Adaptive keyboard keycodes */
1896 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1897 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1898 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1899 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1900 TP_ACPI_HOTKEYSCAN_CLOUD,
1901 TP_ACPI_HOTKEYSCAN_UNK9,
1902 TP_ACPI_HOTKEYSCAN_VOICE,
1903 TP_ACPI_HOTKEYSCAN_UNK10,
1904 TP_ACPI_HOTKEYSCAN_GESTURES,
1905 TP_ACPI_HOTKEYSCAN_UNK11,
1906 TP_ACPI_HOTKEYSCAN_UNK12,
1907 TP_ACPI_HOTKEYSCAN_UNK13,
1908 TP_ACPI_HOTKEYSCAN_CONFIG,
1909 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1910 TP_ACPI_HOTKEYSCAN_RELOAD,
1911 TP_ACPI_HOTKEYSCAN_BACK,
1912 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1913 TP_ACPI_HOTKEYSCAN_MIC_UP,
1914 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1915 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1916 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1918 /* Lenovo extended keymap, starting at 0x1300 */
1919 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1920 /* first new observed key (star, favorites) is 0x1311 */
1921 TP_ACPI_HOTKEYSCAN_STAR = 69,
1922 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1923 TP_ACPI_HOTKEYSCAN_UNK25,
1924 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1925 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1927 /* Hotkey keymap size */
1928 TPACPI_HOTKEY_MAP_LEN
1931 enum { /* Keys/events available through NVRAM polling */
1932 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1933 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1936 enum { /* Positions of some of the keys in hotkey masks */
1937 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1938 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1939 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1940 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1941 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1942 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1943 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1944 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1945 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1946 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1947 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1950 enum { /* NVRAM to ACPI HKEY group map */
1951 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1952 TP_ACPI_HKEY_ZOOM_MASK |
1953 TP_ACPI_HKEY_DISPSWTCH_MASK |
1954 TP_ACPI_HKEY_HIBERNATE_MASK,
1955 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1956 TP_ACPI_HKEY_BRGHTDWN_MASK,
1957 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1958 TP_ACPI_HKEY_VOLDWN_MASK |
1959 TP_ACPI_HKEY_MUTE_MASK,
1962 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1963 struct tp_nvram_state {
1964 u16 thinkpad_toggle:1;
1966 u16 display_toggle:1;
1967 u16 thinklight_toggle:1;
1968 u16 hibernate_toggle:1;
1969 u16 displayexp_toggle:1;
1970 u16 display_state:1;
1971 u16 brightness_toggle:1;
1972 u16 volume_toggle:1;
1975 u8 brightness_level;
1979 /* kthread for the hotkey poller */
1980 static struct task_struct *tpacpi_hotkey_task;
1983 * Acquire mutex to write poller control variables as an
1986 * Increment hotkey_config_change when changing them if you
1987 * want the kthread to forget old state.
1989 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1991 static struct mutex hotkey_thread_data_mutex;
1992 static unsigned int hotkey_config_change;
1995 * hotkey poller control variables
1997 * Must be atomic or readers will also need to acquire mutex
1999 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2000 * should be used only when the changes need to be taken as
2001 * a block, OR when one needs to force the kthread to forget
2004 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2005 static unsigned int hotkey_poll_freq = 10; /* Hz */
2007 #define HOTKEY_CONFIG_CRITICAL_START \
2009 mutex_lock(&hotkey_thread_data_mutex); \
2010 hotkey_config_change++; \
2012 #define HOTKEY_CONFIG_CRITICAL_END \
2013 mutex_unlock(&hotkey_thread_data_mutex);
2015 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2017 #define hotkey_source_mask 0U
2018 #define HOTKEY_CONFIG_CRITICAL_START
2019 #define HOTKEY_CONFIG_CRITICAL_END
2021 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2023 static struct mutex hotkey_mutex;
2025 static enum { /* Reasons for waking up */
2026 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2027 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2028 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2029 } hotkey_wakeup_reason;
2031 static int hotkey_autosleep_ack;
2033 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2034 static u32 hotkey_all_mask; /* all events supported in fw */
2035 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2036 static u32 hotkey_reserved_mask; /* events better left disabled */
2037 static u32 hotkey_driver_mask; /* events needed by the driver */
2038 static u32 hotkey_user_mask; /* events visible to userspace */
2039 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2041 static u16 *hotkey_keycode_map;
2043 static struct attribute_set *hotkey_dev_attributes;
2045 static void tpacpi_driver_event(const unsigned int hkey_event);
2046 static void hotkey_driver_event(const unsigned int scancode);
2047 static void hotkey_poll_setup(const bool may_warn);
2049 /* HKEY.MHKG() return bits */
2050 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2051 /* ThinkPad X1 Yoga (2016) */
2052 #define TP_EC_CMMD_TABLET_MODE 0x6
2054 static int hotkey_get_wlsw(void)
2058 if (!tp_features.hotkey_wlsw)
2061 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2063 return (tpacpi_wlsw_emulstate) ?
2064 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2067 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2070 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2073 static int hotkey_get_tablet_mode(int *status)
2077 switch (tp_features.hotkey_tablet) {
2078 case TP_HOTKEY_TABLET_USES_MHKG:
2079 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2082 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2084 case TP_HOTKEY_TABLET_USES_CMMD:
2085 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2088 *status = (s == TP_EC_CMMD_TABLET_MODE);
2098 * Reads current event mask from firmware, and updates
2099 * hotkey_acpi_mask accordingly. Also resets any bits
2100 * from hotkey_user_mask that are unavailable to be
2101 * delivered (shadow requirement of the userspace ABI).
2103 * Call with hotkey_mutex held
2105 static int hotkey_mask_get(void)
2107 if (tp_features.hotkey_mask) {
2110 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2113 hotkey_acpi_mask = m;
2115 /* no mask support doesn't mean no event support... */
2116 hotkey_acpi_mask = hotkey_all_mask;
2119 /* sync userspace-visible mask */
2120 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2125 static void hotkey_mask_warn_incomplete_mask(void)
2127 /* log only what the user can fix... */
2128 const u32 wantedmask = hotkey_driver_mask &
2129 ~(hotkey_acpi_mask | hotkey_source_mask) &
2130 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2133 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2137 * Set the firmware mask when supported
2139 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2141 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2143 * Call with hotkey_mutex held
2145 static int hotkey_mask_set(u32 mask)
2150 const u32 fwmask = mask & ~hotkey_source_mask;
2152 if (tp_features.hotkey_mask) {
2153 for (i = 0; i < 32; i++) {
2154 if (!acpi_evalf(hkey_handle,
2155 NULL, "MHKM", "vdd", i + 1,
2156 !!(mask & (1 << i)))) {
2164 * We *must* make an inconditional call to hotkey_mask_get to
2165 * refresh hotkey_acpi_mask and update hotkey_user_mask
2167 * Take the opportunity to also log when we cannot _enable_
2170 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2171 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2172 fwmask, hotkey_acpi_mask);
2175 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2176 hotkey_mask_warn_incomplete_mask();
2182 * Sets hotkey_user_mask and tries to set the firmware mask
2184 * Call with hotkey_mutex held
2186 static int hotkey_user_mask_set(const u32 mask)
2190 /* Give people a chance to notice they are doing something that
2191 * is bound to go boom on their users sooner or later */
2192 if (!tp_warned.hotkey_mask_ff &&
2193 (mask == 0xffff || mask == 0xffffff ||
2194 mask == 0xffffffff)) {
2195 tp_warned.hotkey_mask_ff = 1;
2196 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2198 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2201 /* Try to enable what the user asked for, plus whatever we need.
2202 * this syncs everything but won't enable bits in hotkey_user_mask */
2203 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2205 /* Enable the available bits in hotkey_user_mask */
2206 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2212 * Sets the driver hotkey mask.
2214 * Can be called even if the hotkey subdriver is inactive
2216 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2220 /* Do the right thing if hotkey_init has not been called yet */
2221 if (!tp_features.hotkey) {
2222 hotkey_driver_mask = mask;
2226 mutex_lock(&hotkey_mutex);
2228 HOTKEY_CONFIG_CRITICAL_START
2229 hotkey_driver_mask = mask;
2230 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2231 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2233 HOTKEY_CONFIG_CRITICAL_END
2235 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2236 ~hotkey_source_mask);
2237 hotkey_poll_setup(true);
2239 mutex_unlock(&hotkey_mutex);
2244 static int hotkey_status_get(int *status)
2246 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2252 static int hotkey_status_set(bool enable)
2254 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2260 static void tpacpi_input_send_tabletsw(void)
2264 if (tp_features.hotkey_tablet &&
2265 !hotkey_get_tablet_mode(&state)) {
2266 mutex_lock(&tpacpi_inputdev_send_mutex);
2268 input_report_switch(tpacpi_inputdev,
2269 SW_TABLET_MODE, !!state);
2270 input_sync(tpacpi_inputdev);
2272 mutex_unlock(&tpacpi_inputdev_send_mutex);
2276 /* Do NOT call without validating scancode first */
2277 static void tpacpi_input_send_key(const unsigned int scancode)
2279 const unsigned int keycode = hotkey_keycode_map[scancode];
2281 if (keycode != KEY_RESERVED) {
2282 mutex_lock(&tpacpi_inputdev_send_mutex);
2284 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2285 input_report_key(tpacpi_inputdev, keycode, 1);
2286 input_sync(tpacpi_inputdev);
2288 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2289 input_report_key(tpacpi_inputdev, keycode, 0);
2290 input_sync(tpacpi_inputdev);
2292 mutex_unlock(&tpacpi_inputdev_send_mutex);
2296 /* Do NOT call without validating scancode first */
2297 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2299 hotkey_driver_event(scancode);
2300 if (hotkey_user_mask & (1 << scancode))
2301 tpacpi_input_send_key(scancode);
2304 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2305 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2307 /* Do NOT call without validating scancode first */
2308 static void tpacpi_hotkey_send_key(unsigned int scancode)
2310 tpacpi_input_send_key_masked(scancode);
2313 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2317 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2318 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2319 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2320 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2321 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2322 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2324 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2326 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2328 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2329 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2330 n->displayexp_toggle =
2331 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2333 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2334 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2335 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2336 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2337 n->brightness_toggle =
2338 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2340 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2341 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2342 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2343 >> TP_NVRAM_POS_LEVEL_VOLUME;
2344 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2345 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2349 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2351 if ((event_mask & (1 << __scancode)) && \
2352 oldn->__member != newn->__member) \
2353 tpacpi_hotkey_send_key(__scancode); \
2356 #define TPACPI_MAY_SEND_KEY(__scancode) \
2358 if (event_mask & (1 << __scancode)) \
2359 tpacpi_hotkey_send_key(__scancode); \
2362 static void issue_volchange(const unsigned int oldvol,
2363 const unsigned int newvol,
2364 const u32 event_mask)
2366 unsigned int i = oldvol;
2368 while (i > newvol) {
2369 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2372 while (i < newvol) {
2373 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2378 static void issue_brightnesschange(const unsigned int oldbrt,
2379 const unsigned int newbrt,
2380 const u32 event_mask)
2382 unsigned int i = oldbrt;
2384 while (i > newbrt) {
2385 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2388 while (i < newbrt) {
2389 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2394 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2395 struct tp_nvram_state *newn,
2396 const u32 event_mask)
2399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2401 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2411 * This code is supposed to duplicate the IBM firmware behaviour:
2412 * - Pressing MUTE issues mute hotkey message, even when already mute
2413 * - Pressing Volume up/down issues volume up/down hotkey messages,
2414 * even when already at maximum or minimum volume
2415 * - The act of unmuting issues volume up/down notification,
2416 * depending which key was used to unmute
2418 * We are constrained to what the NVRAM can tell us, which is not much
2419 * and certainly not enough if more than one volume hotkey was pressed
2420 * since the last poll cycle.
2422 * Just to make our life interesting, some newer Lenovo ThinkPads have
2423 * bugs in the BIOS and may fail to update volume_toggle properly.
2428 oldn->volume_toggle != newn->volume_toggle ||
2429 oldn->volume_level != newn->volume_level) {
2430 /* recently muted, or repeated mute keypress, or
2431 * multiple presses ending in mute */
2432 issue_volchange(oldn->volume_level, newn->volume_level,
2434 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2439 /* recently unmuted, issue 'unmute' keypress */
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2442 if (oldn->volume_level != newn->volume_level) {
2443 issue_volchange(oldn->volume_level, newn->volume_level,
2445 } else if (oldn->volume_toggle != newn->volume_toggle) {
2446 /* repeated vol up/down keypress at end of scale ? */
2447 if (newn->volume_level == 0)
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2449 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2450 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2454 /* handle brightness */
2455 if (oldn->brightness_level != newn->brightness_level) {
2456 issue_brightnesschange(oldn->brightness_level,
2457 newn->brightness_level, event_mask);
2458 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2459 /* repeated key presses that didn't change state */
2460 if (newn->brightness_level == 0)
2461 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2462 else if (newn->brightness_level >= bright_maxlvl
2463 && !tp_features.bright_unkfw)
2464 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2467 #undef TPACPI_COMPARE_KEY
2468 #undef TPACPI_MAY_SEND_KEY
2474 * We track all events in hotkey_source_mask all the time, since
2475 * most of them are edge-based. We only issue those requested by
2476 * hotkey_user_mask or hotkey_driver_mask, though.
2478 static int hotkey_kthread(void *data)
2480 struct tp_nvram_state s[2] = { 0 };
2481 u32 poll_mask, event_mask;
2482 unsigned int si, so;
2484 unsigned int change_detector;
2485 unsigned int poll_freq;
2488 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2497 /* Initial state for compares */
2498 mutex_lock(&hotkey_thread_data_mutex);
2499 change_detector = hotkey_config_change;
2500 poll_mask = hotkey_source_mask;
2501 event_mask = hotkey_source_mask &
2502 (hotkey_driver_mask | hotkey_user_mask);
2503 poll_freq = hotkey_poll_freq;
2504 mutex_unlock(&hotkey_thread_data_mutex);
2505 hotkey_read_nvram(&s[so], poll_mask);
2507 while (!kthread_should_stop()) {
2509 if (likely(poll_freq))
2512 t = 100; /* should never happen... */
2514 t = msleep_interruptible(t);
2515 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2518 if (t > 0 && !was_frozen)
2521 mutex_lock(&hotkey_thread_data_mutex);
2522 if (was_frozen || hotkey_config_change != change_detector) {
2523 /* forget old state on thaw or config change */
2526 change_detector = hotkey_config_change;
2528 poll_mask = hotkey_source_mask;
2529 event_mask = hotkey_source_mask &
2530 (hotkey_driver_mask | hotkey_user_mask);
2531 poll_freq = hotkey_poll_freq;
2532 mutex_unlock(&hotkey_thread_data_mutex);
2534 if (likely(poll_mask)) {
2535 hotkey_read_nvram(&s[si], poll_mask);
2536 if (likely(si != so)) {
2537 hotkey_compare_and_issue_event(&s[so], &s[si],
2550 /* call with hotkey_mutex held */
2551 static void hotkey_poll_stop_sync(void)
2553 if (tpacpi_hotkey_task) {
2554 kthread_stop(tpacpi_hotkey_task);
2555 tpacpi_hotkey_task = NULL;
2559 /* call with hotkey_mutex held */
2560 static void hotkey_poll_setup(const bool may_warn)
2562 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2563 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2565 if (hotkey_poll_freq > 0 &&
2566 (poll_driver_mask ||
2567 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2568 if (!tpacpi_hotkey_task) {
2569 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2570 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2571 if (IS_ERR(tpacpi_hotkey_task)) {
2572 tpacpi_hotkey_task = NULL;
2573 pr_err("could not create kernel thread for hotkey polling\n");
2577 hotkey_poll_stop_sync();
2578 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2579 hotkey_poll_freq == 0) {
2580 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2581 poll_user_mask, poll_driver_mask);
2586 static void hotkey_poll_setup_safe(const bool may_warn)
2588 mutex_lock(&hotkey_mutex);
2589 hotkey_poll_setup(may_warn);
2590 mutex_unlock(&hotkey_mutex);
2593 /* call with hotkey_mutex held */
2594 static void hotkey_poll_set_freq(unsigned int freq)
2597 hotkey_poll_stop_sync();
2599 hotkey_poll_freq = freq;
2602 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2604 static void hotkey_poll_setup(const bool __unused)
2608 static void hotkey_poll_setup_safe(const bool __unused)
2612 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2614 static int hotkey_inputdev_open(struct input_dev *dev)
2616 switch (tpacpi_lifecycle) {
2617 case TPACPI_LIFE_INIT:
2618 case TPACPI_LIFE_RUNNING:
2619 hotkey_poll_setup_safe(false);
2621 case TPACPI_LIFE_EXITING:
2625 /* Should only happen if tpacpi_lifecycle is corrupt */
2630 static void hotkey_inputdev_close(struct input_dev *dev)
2632 /* disable hotkey polling when possible */
2633 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2634 !(hotkey_source_mask & hotkey_driver_mask))
2635 hotkey_poll_setup_safe(false);
2638 /* sysfs hotkey enable ------------------------------------------------- */
2639 static ssize_t hotkey_enable_show(struct device *dev,
2640 struct device_attribute *attr,
2645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkey reporting is always enabled");
2648 res = hotkey_status_get(&status);
2652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2655 static ssize_t hotkey_enable_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2661 printk_deprecated_attribute("hotkey_enable",
2662 "Hotkeys can be disabled through hotkey_mask");
2664 if (parse_strtoul(buf, 1, &t))
2673 static DEVICE_ATTR_RW(hotkey_enable);
2675 /* sysfs hotkey mask --------------------------------------------------- */
2676 static ssize_t hotkey_mask_show(struct device *dev,
2677 struct device_attribute *attr,
2680 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2683 static ssize_t hotkey_mask_store(struct device *dev,
2684 struct device_attribute *attr,
2685 const char *buf, size_t count)
2690 if (parse_strtoul(buf, 0xffffffffUL, &t))
2693 if (mutex_lock_killable(&hotkey_mutex))
2694 return -ERESTARTSYS;
2696 res = hotkey_user_mask_set(t);
2698 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2699 hotkey_poll_setup(true);
2702 mutex_unlock(&hotkey_mutex);
2704 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2706 return (res) ? res : count;
2709 static DEVICE_ATTR_RW(hotkey_mask);
2711 /* sysfs hotkey bios_enabled ------------------------------------------- */
2712 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2713 struct device_attribute *attr,
2716 return sprintf(buf, "0\n");
2719 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2721 /* sysfs hotkey bios_mask ---------------------------------------------- */
2722 static ssize_t hotkey_bios_mask_show(struct device *dev,
2723 struct device_attribute *attr,
2726 printk_deprecated_attribute("hotkey_bios_mask",
2727 "This attribute is useless.");
2728 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2731 static DEVICE_ATTR_RO(hotkey_bios_mask);
2733 /* sysfs hotkey all_mask ----------------------------------------------- */
2734 static ssize_t hotkey_all_mask_show(struct device *dev,
2735 struct device_attribute *attr,
2738 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2739 hotkey_all_mask | hotkey_source_mask);
2742 static DEVICE_ATTR_RO(hotkey_all_mask);
2744 /* sysfs hotkey all_mask ----------------------------------------------- */
2745 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2746 struct device_attribute *attr,
2749 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2750 hotkey_adaptive_all_mask | hotkey_source_mask);
2753 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2755 /* sysfs hotkey recommended_mask --------------------------------------- */
2756 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2757 struct device_attribute *attr,
2760 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2761 (hotkey_all_mask | hotkey_source_mask)
2762 & ~hotkey_reserved_mask);
2765 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2767 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2769 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2770 static ssize_t hotkey_source_mask_show(struct device *dev,
2771 struct device_attribute *attr,
2774 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2777 static ssize_t hotkey_source_mask_store(struct device *dev,
2778 struct device_attribute *attr,
2779 const char *buf, size_t count)
2785 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2786 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2789 if (mutex_lock_killable(&hotkey_mutex))
2790 return -ERESTARTSYS;
2792 HOTKEY_CONFIG_CRITICAL_START
2793 hotkey_source_mask = t;
2794 HOTKEY_CONFIG_CRITICAL_END
2796 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2797 ~hotkey_source_mask);
2798 hotkey_poll_setup(true);
2800 /* check if events needed by the driver got disabled */
2801 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2802 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2804 mutex_unlock(&hotkey_mutex);
2807 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2810 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2813 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2815 return (rc < 0) ? rc : count;
2818 static DEVICE_ATTR_RW(hotkey_source_mask);
2820 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2821 static ssize_t hotkey_poll_freq_show(struct device *dev,
2822 struct device_attribute *attr,
2825 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2828 static ssize_t hotkey_poll_freq_store(struct device *dev,
2829 struct device_attribute *attr,
2830 const char *buf, size_t count)
2834 if (parse_strtoul(buf, 25, &t))
2837 if (mutex_lock_killable(&hotkey_mutex))
2838 return -ERESTARTSYS;
2840 hotkey_poll_set_freq(t);
2841 hotkey_poll_setup(true);
2843 mutex_unlock(&hotkey_mutex);
2845 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2850 static DEVICE_ATTR_RW(hotkey_poll_freq);
2852 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2854 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2855 static ssize_t hotkey_radio_sw_show(struct device *dev,
2856 struct device_attribute *attr,
2860 res = hotkey_get_wlsw();
2864 /* Opportunistic update */
2865 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2867 return snprintf(buf, PAGE_SIZE, "%d\n",
2868 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2871 static DEVICE_ATTR_RO(hotkey_radio_sw);
2873 static void hotkey_radio_sw_notify_change(void)
2875 if (tp_features.hotkey_wlsw)
2876 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2880 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2881 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2882 struct device_attribute *attr,
2886 res = hotkey_get_tablet_mode(&s);
2890 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2893 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2895 static void hotkey_tablet_mode_notify_change(void)
2897 if (tp_features.hotkey_tablet)
2898 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2899 "hotkey_tablet_mode");
2902 /* sysfs wakeup reason (pollable) -------------------------------------- */
2903 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2904 struct device_attribute *attr,
2907 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2910 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2912 static void hotkey_wakeup_reason_notify_change(void)
2914 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2918 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2919 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2920 struct device_attribute *attr,
2923 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2926 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2927 hotkey_wakeup_hotunplug_complete_show, NULL);
2929 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2931 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2932 "wakeup_hotunplug_complete");
2935 /* sysfs adaptive kbd mode --------------------------------------------- */
2937 static int adaptive_keyboard_get_mode(void);
2938 static int adaptive_keyboard_set_mode(int new_mode);
2940 enum ADAPTIVE_KEY_MODE {
2943 WEB_CONFERENCE_MODE,
2948 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2949 struct device_attribute *attr,
2954 current_mode = adaptive_keyboard_get_mode();
2955 if (current_mode < 0)
2956 return current_mode;
2958 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2961 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2962 struct device_attribute *attr,
2963 const char *buf, size_t count)
2968 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2971 res = adaptive_keyboard_set_mode(t);
2972 return (res < 0) ? res : count;
2975 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2977 static struct attribute *adaptive_kbd_attributes[] = {
2978 &dev_attr_adaptive_kbd_mode.attr,
2982 static const struct attribute_group adaptive_kbd_attr_group = {
2983 .attrs = adaptive_kbd_attributes,
2986 /* --------------------------------------------------------------------- */
2988 static struct attribute *hotkey_attributes[] __initdata = {
2989 &dev_attr_hotkey_enable.attr,
2990 &dev_attr_hotkey_bios_enabled.attr,
2991 &dev_attr_hotkey_bios_mask.attr,
2992 &dev_attr_wakeup_reason.attr,
2993 &dev_attr_wakeup_hotunplug_complete.attr,
2994 &dev_attr_hotkey_mask.attr,
2995 &dev_attr_hotkey_all_mask.attr,
2996 &dev_attr_hotkey_adaptive_all_mask.attr,
2997 &dev_attr_hotkey_recommended_mask.attr,
2998 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2999 &dev_attr_hotkey_source_mask.attr,
3000 &dev_attr_hotkey_poll_freq.attr,
3005 * Sync both the hw and sw blocking state of all switches
3007 static void tpacpi_send_radiosw_update(void)
3012 * We must sync all rfkill controllers *before* issuing any
3013 * rfkill input events, or we will race the rfkill core input
3016 * tpacpi_inputdev_send_mutex works as a synchronization point
3019 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3022 wlsw = hotkey_get_wlsw();
3024 /* Sync hw blocking state first if it is hw-blocked */
3025 if (wlsw == TPACPI_RFK_RADIO_OFF)
3026 tpacpi_rfk_update_hwblock_state(true);
3028 /* Sync sw blocking state */
3029 tpacpi_rfk_update_swstate_all();
3031 /* Sync hw blocking state last if it is hw-unblocked */
3032 if (wlsw == TPACPI_RFK_RADIO_ON)
3033 tpacpi_rfk_update_hwblock_state(false);
3035 /* Issue rfkill input event for WLSW switch */
3037 mutex_lock(&tpacpi_inputdev_send_mutex);
3039 input_report_switch(tpacpi_inputdev,
3040 SW_RFKILL_ALL, (wlsw > 0));
3041 input_sync(tpacpi_inputdev);
3043 mutex_unlock(&tpacpi_inputdev_send_mutex);
3047 * this can be unconditional, as we will poll state again
3048 * if userspace uses the notify to read data
3050 hotkey_radio_sw_notify_change();
3053 static void hotkey_exit(void)
3055 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3056 mutex_lock(&hotkey_mutex);
3057 hotkey_poll_stop_sync();
3058 mutex_unlock(&hotkey_mutex);
3061 if (hotkey_dev_attributes)
3062 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3064 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3065 "restoring original HKEY status and mask\n");
3066 /* yes, there is a bitwise or below, we want the
3067 * functions to be called even if one of them fail */
3068 if (((tp_features.hotkey_mask &&
3069 hotkey_mask_set(hotkey_orig_mask)) |
3070 hotkey_status_set(false)) != 0)
3071 pr_err("failed to restore hot key mask to BIOS defaults\n");
3074 static void __init hotkey_unmap(const unsigned int scancode)
3076 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3077 clear_bit(hotkey_keycode_map[scancode],
3078 tpacpi_inputdev->keybit);
3079 hotkey_keycode_map[scancode] = KEY_RESERVED;
3085 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3088 #define TPACPI_HK_Q_INIMASK 0x0001
3090 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3091 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3092 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3093 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3094 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3095 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3096 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3097 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3098 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3099 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3100 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3101 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3102 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3103 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3104 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3105 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3106 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3107 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3108 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3109 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3112 typedef u16 tpacpi_keymap_entry_t;
3113 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3115 static int hotkey_init_tablet_mode(void)
3117 int in_tablet_mode = 0, res;
3120 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3121 /* For X41t, X60t, X61t Tablets... */
3122 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3123 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3125 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3126 /* For X1 Yoga (2016) */
3127 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3128 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3132 if (!tp_features.hotkey_tablet)
3135 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3136 type, in_tablet_mode ? "tablet" : "laptop");
3138 res = add_to_attr_set(hotkey_dev_attributes,
3139 &dev_attr_hotkey_tablet_mode.attr);
3143 return in_tablet_mode;
3146 static int __init hotkey_init(struct ibm_init_struct *iibm)
3148 /* Requirements for changing the default keymaps:
3150 * 1. Many of the keys are mapped to KEY_RESERVED for very
3151 * good reasons. Do not change them unless you have deep
3152 * knowledge on the IBM and Lenovo ThinkPad firmware for
3153 * the various ThinkPad models. The driver behaves
3154 * differently for KEY_RESERVED: such keys have their
3155 * hot key mask *unset* in mask_recommended, and also
3156 * in the initial hot key mask programmed into the
3157 * firmware at driver load time, which means the firm-
3158 * ware may react very differently if you change them to
3161 * 2. You must be subscribed to the linux-thinkpad and
3162 * ibm-acpi-devel mailing lists, and you should read the
3163 * list archives since 2007 if you want to change the
3164 * keymaps. This requirement exists so that you will
3165 * know the past history of problems with the thinkpad-
3166 * acpi driver keymaps, and also that you will be
3167 * listening to any bug reports;
3169 * 3. Do not send thinkpad-acpi specific patches directly to
3170 * for merging, *ever*. Send them to the linux-acpi
3171 * mailinglist for comments. Merging is to be done only
3172 * through acpi-test and the ACPI maintainer.
3174 * If the above is too much to ask, don't change the keymap.
3175 * Ask the thinkpad-acpi maintainer to do it, instead.
3179 TPACPI_KEYMAP_IBM_GENERIC = 0,
3180 TPACPI_KEYMAP_LENOVO_GENERIC,
3183 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3184 /* Generic keymap for IBM ThinkPads */
3185 [TPACPI_KEYMAP_IBM_GENERIC] = {
3186 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3187 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3188 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3189 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3191 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3192 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3193 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3194 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3196 /* brightness: firmware always reacts to them */
3197 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3198 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3200 /* Thinklight: firmware always react to it */
3201 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3203 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3204 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3206 /* Volume: firmware always react to it and reprograms
3207 * the built-in *extra* mixer. Never map it to control
3208 * another mixer by default. */
3209 KEY_RESERVED, /* 0x14: VOLUME UP */
3210 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3211 KEY_RESERVED, /* 0x16: MUTE */
3213 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3215 /* (assignments unknown, please report if found) */
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3219 /* No assignments, only used for Adaptive keyboards. */
3220 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3221 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3222 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3223 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3224 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3226 /* No assignment, used for newer Lenovo models */
3227 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3228 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3229 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3230 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3231 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3232 KEY_UNKNOWN, KEY_UNKNOWN
3236 /* Generic keymap for Lenovo ThinkPads */
3237 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3238 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3239 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3240 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3241 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3243 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3244 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3245 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3246 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3248 /* These should be enabled --only-- when ACPI video
3249 * is disabled (i.e. in "vendor" mode), and are handled
3250 * in a special way by the init code */
3251 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3252 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3254 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3256 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3257 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3259 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3260 * react to it and reprograms the built-in *extra* mixer.
3261 * Never map it to control another mixer by default.
3263 * T60?, T61, R60?, R61: firmware and EC tries to send
3264 * these over the regular keyboard, so these are no-ops,
3265 * but there are still weird bugs re. MUTE, so do not
3266 * change unless you get test reports from all Lenovo
3267 * models. May cause the BIOS to interfere with the
3270 KEY_RESERVED, /* 0x14: VOLUME UP */
3271 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3272 KEY_RESERVED, /* 0x16: MUTE */
3274 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3276 /* (assignments unknown, please report if found) */
3277 KEY_UNKNOWN, KEY_UNKNOWN,
3280 * The mic mute button only sends 0x1a. It does not
3281 * automatically mute the mic or change the mute light.
3283 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3285 /* (assignments unknown, please report if found) */
3288 /* Extra keys in use since the X240 / T440 / T540 */
3289 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3292 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3293 * The first item in this list is the Mute button which is
3294 * emitted with 0x103 through
3295 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3297 * We'll need to offset those by 0x20.
3299 KEY_RESERVED, /* Mute held, 0x103 */
3300 KEY_BRIGHTNESS_MIN, /* Backlight off */
3301 KEY_RESERVED, /* Clipping tool */
3302 KEY_RESERVED, /* Cloud */
3304 KEY_VOICECOMMAND, /* Voice */
3306 KEY_RESERVED, /* Gestures */
3310 KEY_CONFIG, /* Settings */
3311 KEY_RESERVED, /* New tab */
3312 KEY_REFRESH, /* Reload */
3313 KEY_BACK, /* Back */
3314 KEY_RESERVED, /* Microphone down */
3315 KEY_RESERVED, /* Microphone up */
3316 KEY_RESERVED, /* Microphone cancellation */
3317 KEY_RESERVED, /* Camera mode */
3318 KEY_RESERVED, /* Rotate display, 0x116 */
3321 * These are found in 2017 models (e.g. T470s, X270).
3322 * The lowest known value is 0x311, which according to
3323 * the manual should launch a user defined favorite
3326 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3327 * corresponding to 0x34.
3330 /* (assignments unknown, please report if found) */
3331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3337 KEY_FAVORITES, /* Favorite app, 0x311 */
3338 KEY_RESERVED, /* Clipping tool */
3340 KEY_BLUETOOTH, /* Bluetooth */
3341 KEY_KEYBOARD /* Keyboard, 0x315 */
3345 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3346 /* Generic maps (fallback) */
3348 .vendor = PCI_VENDOR_ID_IBM,
3349 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3350 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3353 .vendor = PCI_VENDOR_ID_LENOVO,
3354 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3355 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3359 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3360 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3365 bool radiosw_state = false;
3366 bool tabletsw_state = false;
3368 unsigned long quirks;
3369 unsigned long keymap_id;
3371 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3372 "initializing hotkey subdriver\n");
3374 BUG_ON(!tpacpi_inputdev);
3375 BUG_ON(tpacpi_inputdev->open != NULL ||
3376 tpacpi_inputdev->close != NULL);
3378 TPACPI_ACPIHANDLE_INIT(hkey);
3379 mutex_init(&hotkey_mutex);
3381 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3382 mutex_init(&hotkey_thread_data_mutex);
3385 /* hotkey not supported on 570 */
3386 tp_features.hotkey = hkey_handle != NULL;
3388 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3390 str_supported(tp_features.hotkey));
3392 if (!tp_features.hotkey)
3395 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3396 ARRAY_SIZE(tpacpi_hotkey_qtable));
3398 tpacpi_disable_brightness_delay();
3400 /* MUST have enough space for all attributes to be added to
3401 * hotkey_dev_attributes */
3402 hotkey_dev_attributes = create_attr_set(
3403 ARRAY_SIZE(hotkey_attributes) + 2,
3405 if (!hotkey_dev_attributes)
3407 res = add_many_to_attr_set(hotkey_dev_attributes,
3409 ARRAY_SIZE(hotkey_attributes));
3413 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3414 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3415 for HKEY interface version 0x100 */
3416 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3417 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "firmware HKEY interface version: 0x%x\n",
3421 switch (hkeyv >> 8) {
3424 * MHKV 0x100 in A31, R40, R40e,
3425 * T4x, X31, and later
3428 /* Paranoia check AND init hotkey_all_mask */
3429 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3431 pr_err("missing MHKA handler, please report this to %s\n",
3433 /* Fallback: pre-init for FN+F3,F4,F12 */
3434 hotkey_all_mask = 0x080cU;
3436 tp_features.hotkey_mask = 1;
3442 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3445 /* Paranoia check AND init hotkey_all_mask */
3446 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3448 pr_err("missing MHKA handler, please report this to %s\n",
3450 /* Fallback: pre-init for FN+F3,F4,F12 */
3451 hotkey_all_mask = 0x080cU;
3453 tp_features.hotkey_mask = 1;
3457 * Check if we have an adaptive keyboard, like on the
3458 * Lenovo Carbon X1 2014 (2nd Gen).
3460 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3462 if (hotkey_adaptive_all_mask != 0) {
3463 tp_features.has_adaptive_kbd = true;
3464 res = sysfs_create_group(
3465 &tpacpi_pdev->dev.kobj,
3466 &adaptive_kbd_attr_group);
3471 tp_features.has_adaptive_kbd = false;
3472 hotkey_adaptive_all_mask = 0x0U;
3477 pr_err("unknown version of the HKEY interface: 0x%x\n",
3479 pr_err("please report this to %s\n", TPACPI_MAIL);
3484 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3485 "hotkey masks are %s\n",
3486 str_supported(tp_features.hotkey_mask));
3488 /* Init hotkey_all_mask if not initialized yet */
3489 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3490 (quirks & TPACPI_HK_Q_INIMASK))
3491 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3493 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3494 if (tp_features.hotkey_mask) {
3495 /* hotkey_source_mask *must* be zero for
3496 * the first hotkey_mask_get to return hotkey_orig_mask */
3497 res = hotkey_mask_get();
3501 hotkey_orig_mask = hotkey_acpi_mask;
3503 hotkey_orig_mask = hotkey_all_mask;
3504 hotkey_acpi_mask = hotkey_all_mask;
3507 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3509 tp_features.hotkey_wlsw = 1;
3510 radiosw_state = !!tpacpi_wlsw_emulstate;
3511 pr_info("radio switch emulation enabled\n");
3514 /* Not all thinkpads have a hardware radio switch */
3515 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3516 tp_features.hotkey_wlsw = 1;
3517 radiosw_state = !!status;
3518 pr_info("radio switch found; radios are %s\n",
3519 enabled(status, 0));
3521 if (tp_features.hotkey_wlsw)
3522 res = add_to_attr_set(hotkey_dev_attributes,
3523 &dev_attr_hotkey_radio_sw.attr);
3525 res = hotkey_init_tablet_mode();
3529 tabletsw_state = res;
3531 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3532 &tpacpi_pdev->dev.kobj);
3536 /* Set up key map */
3537 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3539 if (!hotkey_keycode_map) {
3540 pr_err("failed to allocate memory for key map\n");
3545 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3546 ARRAY_SIZE(tpacpi_keymap_qtable));
3547 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3548 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3549 "using keymap number %lu\n", keymap_id);
3551 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3552 TPACPI_HOTKEY_MAP_SIZE);
3554 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3555 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3556 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3557 tpacpi_inputdev->keycode = hotkey_keycode_map;
3558 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3559 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3560 input_set_capability(tpacpi_inputdev, EV_KEY,
3561 hotkey_keycode_map[i]);
3563 if (i < sizeof(hotkey_reserved_mask)*8)
3564 hotkey_reserved_mask |= 1 << i;
3568 if (tp_features.hotkey_wlsw) {
3569 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3570 input_report_switch(tpacpi_inputdev,
3571 SW_RFKILL_ALL, radiosw_state);
3573 if (tp_features.hotkey_tablet) {
3574 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3575 input_report_switch(tpacpi_inputdev,
3576 SW_TABLET_MODE, tabletsw_state);
3579 /* Do not issue duplicate brightness change events to
3580 * userspace. tpacpi_detect_brightness_capabilities() must have
3581 * been called before this point */
3582 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3583 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3584 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3586 /* Disable brightness up/down on Lenovo thinkpads when
3587 * ACPI is handling them, otherwise it is plain impossible
3588 * for userspace to do something even remotely sane */
3589 hotkey_reserved_mask |=
3590 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3591 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3592 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3593 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3596 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3597 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3599 & ~hotkey_reserved_mask;
3601 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3602 "hotkey source mask 0x%08x, polling freq %u\n",
3603 hotkey_source_mask, hotkey_poll_freq);
3606 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3607 "enabling firmware HKEY event interface...\n");
3608 res = hotkey_status_set(true);
3613 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3614 | hotkey_driver_mask)
3615 & ~hotkey_source_mask);
3616 if (res < 0 && res != -ENXIO) {
3620 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3621 & ~hotkey_reserved_mask;
3622 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3623 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3624 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3626 tpacpi_inputdev->open = &hotkey_inputdev_open;
3627 tpacpi_inputdev->close = &hotkey_inputdev_close;
3629 hotkey_poll_setup_safe(true);
3634 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3635 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3636 &adaptive_kbd_attr_group);
3638 hotkey_dev_attributes = NULL;
3640 return (res < 0) ? res : 1;
3643 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3644 * mode, Web conference mode, Function mode and Lay-flat mode.
3645 * We support Home mode and Function mode currently.
3647 * Will consider support rest of modes in future.
3650 static const int adaptive_keyboard_modes[] = {
3652 /* WEB_BROWSER_MODE = 2,
3653 WEB_CONFERENCE_MODE = 3, */
3657 #define DFR_CHANGE_ROW 0x101
3658 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3659 #define FIRST_ADAPTIVE_KEY 0x103
3661 /* press Fn key a while second, it will switch to Function Mode. Then
3662 * release Fn key, previous mode be restored.
3664 static bool adaptive_keyboard_mode_is_saved;
3665 static int adaptive_keyboard_prev_mode;
3667 static int adaptive_keyboard_get_mode(void)
3671 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3672 pr_err("Cannot read adaptive keyboard mode\n");
3679 static int adaptive_keyboard_set_mode(int new_mode)
3682 new_mode > LAYFLAT_MODE)
3685 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3686 pr_err("Cannot set adaptive keyboard mode\n");
3693 static int adaptive_keyboard_get_next_mode(int mode)
3696 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3698 for (i = 0; i <= max_mode; i++) {
3699 if (adaptive_keyboard_modes[i] == mode)
3708 return adaptive_keyboard_modes[i];
3711 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3713 int current_mode = 0;
3718 case DFR_CHANGE_ROW:
3719 if (adaptive_keyboard_mode_is_saved) {
3720 new_mode = adaptive_keyboard_prev_mode;
3721 adaptive_keyboard_mode_is_saved = false;
3723 current_mode = adaptive_keyboard_get_mode();
3724 if (current_mode < 0)
3726 new_mode = adaptive_keyboard_get_next_mode(
3730 if (adaptive_keyboard_set_mode(new_mode) < 0)
3735 case DFR_SHOW_QUICKVIEW_ROW:
3736 current_mode = adaptive_keyboard_get_mode();
3737 if (current_mode < 0)
3740 adaptive_keyboard_prev_mode = current_mode;
3741 adaptive_keyboard_mode_is_saved = true;
3743 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3748 if (scancode < FIRST_ADAPTIVE_KEY ||
3749 scancode >= FIRST_ADAPTIVE_KEY +
3750 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3751 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3752 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3756 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3757 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3758 if (keycode != KEY_RESERVED) {
3759 mutex_lock(&tpacpi_inputdev_send_mutex);
3761 input_report_key(tpacpi_inputdev, keycode, 1);
3762 input_sync(tpacpi_inputdev);
3764 input_report_key(tpacpi_inputdev, keycode, 0);
3765 input_sync(tpacpi_inputdev);
3767 mutex_unlock(&tpacpi_inputdev_send_mutex);
3773 static bool hotkey_notify_hotkey(const u32 hkey,
3775 bool *ignore_acpi_ev)
3777 /* 0x1000-0x1FFF: key presses */
3778 unsigned int scancode = hkey & 0xfff;
3779 *send_acpi_ev = true;
3780 *ignore_acpi_ev = false;
3783 * Original events are in the 0x10XX range, the adaptive keyboard
3784 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3785 * models, additional keys are emitted through 0x13XX.
3787 switch ((hkey >> 8) & 0xf) {
3790 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3791 /* HKEY event 0x1001 is scancode 0x00 */
3793 if (!(hotkey_source_mask & (1 << scancode))) {
3794 tpacpi_input_send_key_masked(scancode);
3795 *send_acpi_ev = false;
3797 *ignore_acpi_ev = true;
3804 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3807 /* Extended keycodes start at 0x300 and our offset into the map
3808 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3809 * will be positive, but might not be in the correct range.
3811 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3812 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3813 scancode < TPACPI_HOTKEY_MAP_LEN) {
3814 tpacpi_input_send_key(scancode);
3823 static bool hotkey_notify_wakeup(const u32 hkey,
3825 bool *ignore_acpi_ev)
3827 /* 0x2000-0x2FFF: Wakeup reason */
3828 *send_acpi_ev = true;
3829 *ignore_acpi_ev = false;
3832 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3833 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3834 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3835 *ignore_acpi_ev = true;
3838 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3839 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3840 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3841 *ignore_acpi_ev = true;
3844 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3845 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3846 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3847 /* how to auto-heal: */
3848 /* 2313: woke up from S3, go to S4/S5 */
3849 /* 2413: woke up from S4, go to S5 */
3856 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3857 pr_info("woke up due to a hot-unplug request...\n");
3858 hotkey_wakeup_reason_notify_change();
3863 static bool hotkey_notify_dockevent(const u32 hkey,
3865 bool *ignore_acpi_ev)
3867 /* 0x4000-0x4FFF: dock-related events */
3868 *send_acpi_ev = true;
3869 *ignore_acpi_ev = false;
3872 case TP_HKEY_EV_UNDOCK_ACK:
3873 /* ACPI undock operation completed after wakeup */
3874 hotkey_autosleep_ack = 1;
3875 pr_info("undocked\n");
3876 hotkey_wakeup_hotunplug_complete_notify_change();
3879 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3880 pr_info("docked into hotplug port replicator\n");
3882 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3883 pr_info("undocked from hotplug port replicator\n");
3891 static bool hotkey_notify_usrevent(const u32 hkey,
3893 bool *ignore_acpi_ev)
3895 /* 0x5000-0x5FFF: human interface helpers */
3896 *send_acpi_ev = true;
3897 *ignore_acpi_ev = false;
3900 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3901 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3904 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3905 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3906 tpacpi_input_send_tabletsw();
3907 hotkey_tablet_mode_notify_change();
3908 *send_acpi_ev = false;
3911 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3912 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3913 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3914 /* do not propagate these events */
3915 *ignore_acpi_ev = true;
3923 static void thermal_dump_all_sensors(void);
3925 static bool hotkey_notify_6xxx(const u32 hkey,
3927 bool *ignore_acpi_ev)
3931 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3932 *send_acpi_ev = true;
3933 *ignore_acpi_ev = false;
3936 case TP_HKEY_EV_THM_TABLE_CHANGED:
3937 pr_info("EC reports that Thermal Table has changed\n");
3938 /* recommended action: do nothing, we don't have
3939 * Lenovo ATM information */
3941 case TP_HKEY_EV_ALARM_BAT_HOT:
3942 pr_crit("THERMAL ALARM: battery is too hot!\n");
3943 /* recommended action: warn user through gui */
3945 case TP_HKEY_EV_ALARM_BAT_XHOT:
3946 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3947 /* recommended action: immediate sleep/hibernate */
3949 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3950 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3951 /* recommended action: warn user through gui, that */
3952 /* some internal component is too hot */
3954 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3955 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3956 /* recommended action: immediate sleep/hibernate */
3958 case TP_HKEY_EV_AC_CHANGED:
3959 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3960 * AC status changed; can be triggered by plugging or
3961 * unplugging AC adapter, docking or undocking. */
3965 case TP_HKEY_EV_KEY_NUMLOCK:
3966 case TP_HKEY_EV_KEY_FN:
3967 /* key press events, we just ignore them as long as the EC
3968 * is still reporting them in the normal keyboard stream */
3969 *send_acpi_ev = false;
3970 *ignore_acpi_ev = true;
3973 case TP_HKEY_EV_KEY_FN_ESC:
3974 /* Get the media key status to foce the status LED to update */
3975 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3976 *send_acpi_ev = false;
3977 *ignore_acpi_ev = true;
3980 case TP_HKEY_EV_TABLET_CHANGED:
3981 tpacpi_input_send_tabletsw();
3982 hotkey_tablet_mode_notify_change();
3983 *send_acpi_ev = false;
3986 case TP_HKEY_EV_PALM_DETECTED:
3987 case TP_HKEY_EV_PALM_UNDETECTED:
3988 /* palm detected hovering the keyboard, forward to user-space
3989 * via netlink for consumption */
3993 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3997 thermal_dump_all_sensors();
4002 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4006 bool ignore_acpi_ev;
4009 if (event != 0x80) {
4010 pr_err("unknown HKEY notification event %d\n", event);
4011 /* forward it to userspace, maybe it knows how to handle it */
4012 acpi_bus_generate_netlink_event(
4013 ibm->acpi->device->pnp.device_class,
4014 dev_name(&ibm->acpi->device->dev),
4020 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4021 pr_err("failed to retrieve HKEY event\n");
4030 send_acpi_ev = true;
4031 ignore_acpi_ev = false;
4033 switch (hkey >> 12) {
4035 /* 0x1000-0x1FFF: key presses */
4036 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4040 /* 0x2000-0x2FFF: Wakeup reason */
4041 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4045 /* 0x3000-0x3FFF: bay-related wakeups */
4047 case TP_HKEY_EV_BAYEJ_ACK:
4048 hotkey_autosleep_ack = 1;
4049 pr_info("bay ejected\n");
4050 hotkey_wakeup_hotunplug_complete_notify_change();
4053 case TP_HKEY_EV_OPTDRV_EJ:
4054 /* FIXME: kick libata if SATA link offline */
4062 /* 0x4000-0x4FFF: dock-related events */
4063 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4067 /* 0x5000-0x5FFF: human interface helpers */
4068 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4072 /* 0x6000-0x6FFF: thermal alarms/notices and
4073 * keyboard events */
4074 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4078 /* 0x7000-0x7FFF: misc */
4079 if (tp_features.hotkey_wlsw &&
4080 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4081 tpacpi_send_radiosw_update();
4086 /* fallthrough to default */
4091 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4092 pr_notice("please report the conditions when this event happened to %s\n",
4096 /* netlink events */
4097 if (!ignore_acpi_ev && send_acpi_ev) {
4098 acpi_bus_generate_netlink_event(
4099 ibm->acpi->device->pnp.device_class,
4100 dev_name(&ibm->acpi->device->dev),
4106 static void hotkey_suspend(void)
4108 /* Do these on suspend, we get the events on early resume! */
4109 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4110 hotkey_autosleep_ack = 0;
4112 /* save previous mode of adaptive keyboard of X1 Carbon */
4113 if (tp_features.has_adaptive_kbd) {
4114 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4116 pr_err("Cannot read adaptive keyboard mode.\n");
4121 static void hotkey_resume(void)
4123 tpacpi_disable_brightness_delay();
4125 if (hotkey_status_set(true) < 0 ||
4126 hotkey_mask_set(hotkey_acpi_mask) < 0)
4127 pr_err("error while attempting to reset the event firmware interface\n");
4129 tpacpi_send_radiosw_update();
4130 hotkey_tablet_mode_notify_change();
4131 hotkey_wakeup_reason_notify_change();
4132 hotkey_wakeup_hotunplug_complete_notify_change();
4133 hotkey_poll_setup_safe(false);
4135 /* restore previous mode of adapive keyboard of X1 Carbon */
4136 if (tp_features.has_adaptive_kbd) {
4137 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4138 adaptive_keyboard_prev_mode)) {
4139 pr_err("Cannot set adaptive keyboard mode.\n");
4144 /* procfs -------------------------------------------------------------- */
4145 static int hotkey_read(struct seq_file *m)
4149 if (!tp_features.hotkey) {
4150 seq_printf(m, "status:\t\tnot supported\n");
4154 if (mutex_lock_killable(&hotkey_mutex))
4155 return -ERESTARTSYS;
4156 res = hotkey_status_get(&status);
4158 res = hotkey_mask_get();
4159 mutex_unlock(&hotkey_mutex);
4163 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4164 if (hotkey_all_mask) {
4165 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4166 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4168 seq_printf(m, "mask:\t\tnot supported\n");
4169 seq_printf(m, "commands:\tenable, disable, reset\n");
4175 static void hotkey_enabledisable_warn(bool enable)
4177 tpacpi_log_usertask("procfs hotkey enable/disable");
4178 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4179 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4180 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4183 static int hotkey_write(char *buf)
4189 if (!tp_features.hotkey)
4192 if (mutex_lock_killable(&hotkey_mutex))
4193 return -ERESTARTSYS;
4195 mask = hotkey_user_mask;
4198 while ((cmd = next_cmd(&buf))) {
4199 if (strlencmp(cmd, "enable") == 0) {
4200 hotkey_enabledisable_warn(1);
4201 } else if (strlencmp(cmd, "disable") == 0) {
4202 hotkey_enabledisable_warn(0);
4204 } else if (strlencmp(cmd, "reset") == 0) {
4205 mask = (hotkey_all_mask | hotkey_source_mask)
4206 & ~hotkey_reserved_mask;
4207 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4209 } else if (sscanf(cmd, "%x", &mask) == 1) {
4218 tpacpi_disclose_usertask("procfs hotkey",
4219 "set mask to 0x%08x\n", mask);
4220 res = hotkey_user_mask_set(mask);
4224 mutex_unlock(&hotkey_mutex);
4228 static const struct acpi_device_id ibm_htk_device_ids[] = {
4229 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4230 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4231 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4235 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4236 .hid = ibm_htk_device_ids,
4237 .notify = hotkey_notify,
4238 .handle = &hkey_handle,
4239 .type = ACPI_DEVICE_NOTIFY,
4242 static struct ibm_struct hotkey_driver_data = {
4244 .read = hotkey_read,
4245 .write = hotkey_write,
4246 .exit = hotkey_exit,
4247 .resume = hotkey_resume,
4248 .suspend = hotkey_suspend,
4249 .acpi = &ibm_hotkey_acpidriver,
4252 /*************************************************************************
4253 * Bluetooth subdriver
4257 /* ACPI GBDC/SBDC bits */
4258 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4259 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4260 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4261 0 = disable, 1 = enable */
4265 /* ACPI \BLTH commands */
4266 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4267 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4268 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4269 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4270 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4273 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4275 static int bluetooth_get_status(void)
4279 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4280 if (dbg_bluetoothemul)
4281 return (tpacpi_bluetooth_emulstate) ?
4282 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4285 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4288 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4289 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4292 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4296 vdbg_printk(TPACPI_DBG_RFKILL,
4297 "will attempt to %s bluetooth\n",
4298 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4300 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4301 if (dbg_bluetoothemul) {
4302 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4307 if (state == TPACPI_RFK_RADIO_ON)
4308 status = TP_ACPI_BLUETOOTH_RADIOSSW
4309 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4313 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4319 /* sysfs bluetooth enable ---------------------------------------------- */
4320 static ssize_t bluetooth_enable_show(struct device *dev,
4321 struct device_attribute *attr,
4324 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4328 static ssize_t bluetooth_enable_store(struct device *dev,
4329 struct device_attribute *attr,
4330 const char *buf, size_t count)
4332 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4336 static DEVICE_ATTR_RW(bluetooth_enable);
4338 /* --------------------------------------------------------------------- */
4340 static struct attribute *bluetooth_attributes[] = {
4341 &dev_attr_bluetooth_enable.attr,
4345 static const struct attribute_group bluetooth_attr_group = {
4346 .attrs = bluetooth_attributes,
4349 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4350 .get_status = bluetooth_get_status,
4351 .set_status = bluetooth_set_status,
4354 static void bluetooth_shutdown(void)
4356 /* Order firmware to save current state to NVRAM */
4357 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4358 TP_ACPI_BLTH_SAVE_STATE))
4359 pr_notice("failed to save bluetooth state to NVRAM\n");
4361 vdbg_printk(TPACPI_DBG_RFKILL,
4362 "bluetooth state saved to NVRAM\n");
4365 static void bluetooth_exit(void)
4367 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4368 &bluetooth_attr_group);
4370 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4372 bluetooth_shutdown();
4375 static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4377 .ident = "ThinkPad E485",
4379 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4380 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4384 .ident = "ThinkPad E585",
4386 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4387 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4391 .ident = "ThinkPad A285 - 20MW",
4393 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4394 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4398 .ident = "ThinkPad A285 - 20MX",
4400 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4401 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4405 .ident = "ThinkPad A485 - 20MU",
4407 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4408 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4412 .ident = "ThinkPad A485 - 20MV",
4414 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4415 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4421 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4422 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4423 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4424 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4429 static int __init have_bt_fwbug(void)
4432 * Some AMD based ThinkPads have a firmware bug that calling
4433 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4435 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4436 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4437 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4443 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4448 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4449 "initializing bluetooth subdriver\n");
4451 TPACPI_ACPIHANDLE_INIT(hkey);
4453 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4454 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4455 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4456 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4458 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4459 "bluetooth is %s, status 0x%02x\n",
4460 str_supported(tp_features.bluetooth),
4463 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4464 if (dbg_bluetoothemul) {
4465 tp_features.bluetooth = 1;
4466 pr_info("bluetooth switch emulation enabled\n");
4469 if (tp_features.bluetooth &&
4470 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4471 /* no bluetooth hardware present in system */
4472 tp_features.bluetooth = 0;
4473 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4474 "bluetooth hardware not installed\n");
4477 if (!tp_features.bluetooth)
4480 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4481 &bluetooth_tprfk_ops,
4482 RFKILL_TYPE_BLUETOOTH,
4483 TPACPI_RFK_BLUETOOTH_SW_NAME,
4488 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4489 &bluetooth_attr_group);
4491 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4498 /* procfs -------------------------------------------------------------- */
4499 static int bluetooth_read(struct seq_file *m)
4501 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4504 static int bluetooth_write(char *buf)
4506 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4509 static struct ibm_struct bluetooth_driver_data = {
4510 .name = "bluetooth",
4511 .read = bluetooth_read,
4512 .write = bluetooth_write,
4513 .exit = bluetooth_exit,
4514 .shutdown = bluetooth_shutdown,
4517 /*************************************************************************
4522 /* ACPI GWAN/SWAN bits */
4523 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4524 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4525 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4526 0 = disable, 1 = enable */
4529 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4531 static int wan_get_status(void)
4535 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4537 return (tpacpi_wwan_emulstate) ?
4538 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4541 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4544 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4545 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4548 static int wan_set_status(enum tpacpi_rfkill_state state)
4552 vdbg_printk(TPACPI_DBG_RFKILL,
4553 "will attempt to %s wwan\n",
4554 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4556 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4558 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4563 if (state == TPACPI_RFK_RADIO_ON)
4564 status = TP_ACPI_WANCARD_RADIOSSW
4565 | TP_ACPI_WANCARD_RESUMECTRL;
4569 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4575 /* sysfs wan enable ---------------------------------------------------- */
4576 static ssize_t wan_enable_show(struct device *dev,
4577 struct device_attribute *attr,
4580 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4584 static ssize_t wan_enable_store(struct device *dev,
4585 struct device_attribute *attr,
4586 const char *buf, size_t count)
4588 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4592 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4593 wan_enable_show, wan_enable_store);
4595 /* --------------------------------------------------------------------- */
4597 static struct attribute *wan_attributes[] = {
4598 &dev_attr_wwan_enable.attr,
4602 static const struct attribute_group wan_attr_group = {
4603 .attrs = wan_attributes,
4606 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4607 .get_status = wan_get_status,
4608 .set_status = wan_set_status,
4611 static void wan_shutdown(void)
4613 /* Order firmware to save current state to NVRAM */
4614 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4615 TP_ACPI_WGSV_SAVE_STATE))
4616 pr_notice("failed to save WWAN state to NVRAM\n");
4618 vdbg_printk(TPACPI_DBG_RFKILL,
4619 "WWAN state saved to NVRAM\n");
4622 static void wan_exit(void)
4624 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4627 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4632 static int __init wan_init(struct ibm_init_struct *iibm)
4637 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4638 "initializing wan subdriver\n");
4640 TPACPI_ACPIHANDLE_INIT(hkey);
4642 tp_features.wan = hkey_handle &&
4643 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4645 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4646 "wan is %s, status 0x%02x\n",
4647 str_supported(tp_features.wan),
4650 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4652 tp_features.wan = 1;
4653 pr_info("wwan switch emulation enabled\n");
4656 if (tp_features.wan &&
4657 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4658 /* no wan hardware present in system */
4659 tp_features.wan = 0;
4660 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4661 "wan hardware not installed\n");
4664 if (!tp_features.wan)
4667 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4670 TPACPI_RFK_WWAN_SW_NAME,
4675 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4679 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4686 /* procfs -------------------------------------------------------------- */
4687 static int wan_read(struct seq_file *m)
4689 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4692 static int wan_write(char *buf)
4694 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4697 static struct ibm_struct wan_driver_data = {
4702 .shutdown = wan_shutdown,
4705 /*************************************************************************
4710 /* ACPI GUWB/SUWB bits */
4711 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4712 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4715 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4717 static int uwb_get_status(void)
4721 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4723 return (tpacpi_uwb_emulstate) ?
4724 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4727 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4730 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4731 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4734 static int uwb_set_status(enum tpacpi_rfkill_state state)
4738 vdbg_printk(TPACPI_DBG_RFKILL,
4739 "will attempt to %s UWB\n",
4740 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4742 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4744 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4749 if (state == TPACPI_RFK_RADIO_ON)
4750 status = TP_ACPI_UWB_RADIOSSW;
4754 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4760 /* --------------------------------------------------------------------- */
4762 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4763 .get_status = uwb_get_status,
4764 .set_status = uwb_set_status,
4767 static void uwb_exit(void)
4769 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4772 static int __init uwb_init(struct ibm_init_struct *iibm)
4777 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4778 "initializing uwb subdriver\n");
4780 TPACPI_ACPIHANDLE_INIT(hkey);
4782 tp_features.uwb = hkey_handle &&
4783 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4785 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4786 "uwb is %s, status 0x%02x\n",
4787 str_supported(tp_features.uwb),
4790 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4792 tp_features.uwb = 1;
4793 pr_info("uwb switch emulation enabled\n");
4796 if (tp_features.uwb &&
4797 !(status & TP_ACPI_UWB_HWPRESENT)) {
4798 /* no uwb hardware present in system */
4799 tp_features.uwb = 0;
4800 dbg_printk(TPACPI_DBG_INIT,
4801 "uwb hardware not installed\n");
4804 if (!tp_features.uwb)
4807 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4810 TPACPI_RFK_UWB_SW_NAME,
4815 static struct ibm_struct uwb_driver_data = {
4818 .flags.experimental = 1,
4821 /*************************************************************************
4825 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4827 enum video_access_mode {
4828 TPACPI_VIDEO_NONE = 0,
4829 TPACPI_VIDEO_570, /* 570 */
4830 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4831 TPACPI_VIDEO_NEW, /* all others */
4834 enum { /* video status flags, based on VIDEO_570 */
4835 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4836 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4837 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4840 enum { /* TPACPI_VIDEO_570 constants */
4841 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4842 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4843 * video_status_flags */
4844 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4845 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4848 static enum video_access_mode video_supported;
4849 static int video_orig_autosw;
4851 static int video_autosw_get(void);
4852 static int video_autosw_set(int enable);
4854 TPACPI_HANDLE(vid, root,
4855 "\\_SB.PCI.AGP.VGA", /* 570 */
4856 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4857 "\\_SB.PCI0.VID0", /* 770e */
4858 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4859 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4860 "\\_SB.PCI0.AGP.VID", /* all others */
4863 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4865 static int __init video_init(struct ibm_init_struct *iibm)
4869 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4871 TPACPI_ACPIHANDLE_INIT(vid);
4872 if (tpacpi_is_ibm())
4873 TPACPI_ACPIHANDLE_INIT(vid2);
4875 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4876 /* G41, assume IVGA doesn't change */
4877 vid_handle = vid2_handle;
4880 /* video switching not supported on R30, R31 */
4881 video_supported = TPACPI_VIDEO_NONE;
4882 else if (tpacpi_is_ibm() &&
4883 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4885 video_supported = TPACPI_VIDEO_570;
4886 else if (tpacpi_is_ibm() &&
4887 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4888 /* 600e/x, 770e, 770x */
4889 video_supported = TPACPI_VIDEO_770;
4892 video_supported = TPACPI_VIDEO_NEW;
4894 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4895 str_supported(video_supported != TPACPI_VIDEO_NONE),
4898 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4901 static void video_exit(void)
4903 dbg_printk(TPACPI_DBG_EXIT,
4904 "restoring original video autoswitch mode\n");
4905 if (video_autosw_set(video_orig_autosw))
4906 pr_err("error while trying to restore original video autoswitch mode\n");
4909 static int video_outputsw_get(void)
4914 switch (video_supported) {
4915 case TPACPI_VIDEO_570:
4916 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4917 TP_ACPI_VIDEO_570_PHSCMD))
4919 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4921 case TPACPI_VIDEO_770:
4922 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4925 status |= TP_ACPI_VIDEO_S_LCD;
4926 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4929 status |= TP_ACPI_VIDEO_S_CRT;
4931 case TPACPI_VIDEO_NEW:
4932 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4933 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4936 status |= TP_ACPI_VIDEO_S_CRT;
4938 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4939 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4942 status |= TP_ACPI_VIDEO_S_LCD;
4943 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4946 status |= TP_ACPI_VIDEO_S_DVI;
4955 static int video_outputsw_set(int status)
4960 switch (video_supported) {
4961 case TPACPI_VIDEO_570:
4962 res = acpi_evalf(NULL, NULL,
4963 "\\_SB.PHS2", "vdd",
4964 TP_ACPI_VIDEO_570_PHS2CMD,
4965 status | TP_ACPI_VIDEO_570_PHS2SET);
4967 case TPACPI_VIDEO_770:
4968 autosw = video_autosw_get();
4972 res = video_autosw_set(1);
4975 res = acpi_evalf(vid_handle, NULL,
4976 "ASWT", "vdd", status * 0x100, 0);
4977 if (!autosw && video_autosw_set(autosw)) {
4978 pr_err("video auto-switch left enabled due to error\n");
4982 case TPACPI_VIDEO_NEW:
4983 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4984 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4990 return (res) ? 0 : -EIO;
4993 static int video_autosw_get(void)
4997 switch (video_supported) {
4998 case TPACPI_VIDEO_570:
4999 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5002 case TPACPI_VIDEO_770:
5003 case TPACPI_VIDEO_NEW:
5004 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5014 static int video_autosw_set(int enable)
5016 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5021 static int video_outputsw_cycle(void)
5023 int autosw = video_autosw_get();
5029 switch (video_supported) {
5030 case TPACPI_VIDEO_570:
5031 res = video_autosw_set(1);
5034 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5036 case TPACPI_VIDEO_770:
5037 case TPACPI_VIDEO_NEW:
5038 res = video_autosw_set(1);
5041 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5046 if (!autosw && video_autosw_set(autosw)) {
5047 pr_err("video auto-switch left enabled due to error\n");
5051 return (res) ? 0 : -EIO;
5054 static int video_expand_toggle(void)
5056 switch (video_supported) {
5057 case TPACPI_VIDEO_570:
5058 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5060 case TPACPI_VIDEO_770:
5061 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5063 case TPACPI_VIDEO_NEW:
5064 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5072 static int video_read(struct seq_file *m)
5076 if (video_supported == TPACPI_VIDEO_NONE) {
5077 seq_printf(m, "status:\t\tnot supported\n");
5081 /* Even reads can crash X.org, so... */
5082 if (!capable(CAP_SYS_ADMIN))
5085 status = video_outputsw_get();
5089 autosw = video_autosw_get();
5093 seq_printf(m, "status:\t\tsupported\n");
5094 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5095 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5096 if (video_supported == TPACPI_VIDEO_NEW)
5097 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5098 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5099 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5100 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5101 if (video_supported == TPACPI_VIDEO_NEW)
5102 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5103 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5104 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5109 static int video_write(char *buf)
5112 int enable, disable, status;
5115 if (video_supported == TPACPI_VIDEO_NONE)
5118 /* Even reads can crash X.org, let alone writes... */
5119 if (!capable(CAP_SYS_ADMIN))
5125 while ((cmd = next_cmd(&buf))) {
5126 if (strlencmp(cmd, "lcd_enable") == 0) {
5127 enable |= TP_ACPI_VIDEO_S_LCD;
5128 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5129 disable |= TP_ACPI_VIDEO_S_LCD;
5130 } else if (strlencmp(cmd, "crt_enable") == 0) {
5131 enable |= TP_ACPI_VIDEO_S_CRT;
5132 } else if (strlencmp(cmd, "crt_disable") == 0) {
5133 disable |= TP_ACPI_VIDEO_S_CRT;
5134 } else if (video_supported == TPACPI_VIDEO_NEW &&
5135 strlencmp(cmd, "dvi_enable") == 0) {
5136 enable |= TP_ACPI_VIDEO_S_DVI;
5137 } else if (video_supported == TPACPI_VIDEO_NEW &&
5138 strlencmp(cmd, "dvi_disable") == 0) {
5139 disable |= TP_ACPI_VIDEO_S_DVI;
5140 } else if (strlencmp(cmd, "auto_enable") == 0) {
5141 res = video_autosw_set(1);
5144 } else if (strlencmp(cmd, "auto_disable") == 0) {
5145 res = video_autosw_set(0);
5148 } else if (strlencmp(cmd, "video_switch") == 0) {
5149 res = video_outputsw_cycle();
5152 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5153 res = video_expand_toggle();
5160 if (enable || disable) {
5161 status = video_outputsw_get();
5164 res = video_outputsw_set((status & ~disable) | enable);
5172 static struct ibm_struct video_driver_data = {
5175 .write = video_write,
5179 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5181 /*************************************************************************
5182 * Keyboard backlight subdriver
5185 static enum led_brightness kbdlight_brightness;
5186 static DEFINE_MUTEX(kbdlight_mutex);
5188 static int kbdlight_set_level(int level)
5195 mutex_lock(&kbdlight_mutex);
5197 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5200 kbdlight_brightness = level;
5202 mutex_unlock(&kbdlight_mutex);
5207 static int kbdlight_get_level(void)
5214 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5220 return status & 0x3;
5223 static bool kbdlight_is_supported(void)
5230 if (!acpi_has_method(hkey_handle, "MLCG")) {
5231 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5235 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5236 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5241 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5245 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5247 * Guessed test for keyboard backlight:
5249 * Machines with backlight keyboard return:
5250 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5251 * b110100010010000000XX - ThinkPad x230
5252 * b010100000010000000XX - ThinkPad x240
5253 * b010100000010000000XX - ThinkPad W541
5254 * (XX is current backlight level)
5256 * Machines without backlight keyboard return:
5257 * b10100001000000000000 - ThinkPad x230
5258 * b10110001000000000000 - ThinkPad E430
5259 * b00000000000000000000 - ThinkPad E450
5261 * Candidate BITs for detection test (XOR):
5262 * b01000000001000000000
5265 return status & BIT(9);
5268 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5269 enum led_brightness brightness)
5271 return kbdlight_set_level(brightness);
5274 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5278 level = kbdlight_get_level();
5285 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5287 .name = "tpacpi::kbd_backlight",
5288 .max_brightness = 2,
5289 .flags = LED_BRIGHT_HW_CHANGED,
5290 .brightness_set_blocking = &kbdlight_sysfs_set,
5291 .brightness_get = &kbdlight_sysfs_get,
5295 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5299 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5301 TPACPI_ACPIHANDLE_INIT(hkey);
5303 if (!kbdlight_is_supported()) {
5304 tp_features.kbdlight = 0;
5305 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5309 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5310 tp_features.kbdlight = 1;
5312 rc = led_classdev_register(&tpacpi_pdev->dev,
5313 &tpacpi_led_kbdlight.led_classdev);
5315 tp_features.kbdlight = 0;
5319 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5320 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5324 static void kbdlight_exit(void)
5326 if (tp_features.kbdlight)
5327 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5330 static int kbdlight_set_level_and_update(int level)
5333 struct led_classdev *led_cdev;
5335 ret = kbdlight_set_level(level);
5336 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5338 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5339 led_cdev->brightness = level;
5344 static int kbdlight_read(struct seq_file *m)
5348 if (!tp_features.kbdlight) {
5349 seq_printf(m, "status:\t\tnot supported\n");
5351 level = kbdlight_get_level();
5353 seq_printf(m, "status:\t\terror %d\n", level);
5355 seq_printf(m, "status:\t\t%d\n", level);
5356 seq_printf(m, "commands:\t0, 1, 2\n");
5362 static int kbdlight_write(char *buf)
5367 if (!tp_features.kbdlight)
5370 while ((cmd = next_cmd(&buf))) {
5371 if (strlencmp(cmd, "0") == 0)
5373 else if (strlencmp(cmd, "1") == 0)
5375 else if (strlencmp(cmd, "2") == 0)
5384 return kbdlight_set_level_and_update(level);
5387 static void kbdlight_suspend(void)
5389 struct led_classdev *led_cdev;
5391 if (!tp_features.kbdlight)
5394 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5395 led_update_brightness(led_cdev);
5396 led_classdev_suspend(led_cdev);
5399 static void kbdlight_resume(void)
5401 if (!tp_features.kbdlight)
5404 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5407 static struct ibm_struct kbdlight_driver_data = {
5409 .read = kbdlight_read,
5410 .write = kbdlight_write,
5411 .suspend = kbdlight_suspend,
5412 .resume = kbdlight_resume,
5413 .exit = kbdlight_exit,
5416 /*************************************************************************
5417 * Light (thinklight) subdriver
5420 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5421 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5423 static int light_get_status(void)
5427 if (tp_features.light_status) {
5428 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5436 static int light_set_status(int status)
5440 if (tp_features.light) {
5442 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5444 TP_CMOS_THINKLIGHT_ON :
5445 TP_CMOS_THINKLIGHT_OFF);
5447 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5450 return (rc) ? 0 : -EIO;
5456 static int light_sysfs_set(struct led_classdev *led_cdev,
5457 enum led_brightness brightness)
5459 return light_set_status((brightness != LED_OFF) ?
5460 TPACPI_LED_ON : TPACPI_LED_OFF);
5463 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5465 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5468 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5470 .name = "tpacpi::thinklight",
5471 .brightness_set_blocking = &light_sysfs_set,
5472 .brightness_get = &light_sysfs_get,
5476 static int __init light_init(struct ibm_init_struct *iibm)
5480 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5482 if (tpacpi_is_ibm()) {
5483 TPACPI_ACPIHANDLE_INIT(ledb);
5484 TPACPI_ACPIHANDLE_INIT(lght);
5486 TPACPI_ACPIHANDLE_INIT(cmos);
5488 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5489 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5491 if (tp_features.light)
5492 /* light status not supported on
5493 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5494 tp_features.light_status =
5495 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5497 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5498 str_supported(tp_features.light),
5499 str_supported(tp_features.light_status));
5501 if (!tp_features.light)
5504 rc = led_classdev_register(&tpacpi_pdev->dev,
5505 &tpacpi_led_thinklight.led_classdev);
5508 tp_features.light = 0;
5509 tp_features.light_status = 0;
5517 static void light_exit(void)
5519 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5522 static int light_read(struct seq_file *m)
5526 if (!tp_features.light) {
5527 seq_printf(m, "status:\t\tnot supported\n");
5528 } else if (!tp_features.light_status) {
5529 seq_printf(m, "status:\t\tunknown\n");
5530 seq_printf(m, "commands:\ton, off\n");
5532 status = light_get_status();
5535 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5536 seq_printf(m, "commands:\ton, off\n");
5542 static int light_write(char *buf)
5547 if (!tp_features.light)
5550 while ((cmd = next_cmd(&buf))) {
5551 if (strlencmp(cmd, "on") == 0) {
5553 } else if (strlencmp(cmd, "off") == 0) {
5559 return light_set_status(newstatus);
5562 static struct ibm_struct light_driver_data = {
5565 .write = light_write,
5569 /*************************************************************************
5573 /* sysfs cmos_command -------------------------------------------------- */
5574 static ssize_t cmos_command_store(struct device *dev,
5575 struct device_attribute *attr,
5576 const char *buf, size_t count)
5578 unsigned long cmos_cmd;
5581 if (parse_strtoul(buf, 21, &cmos_cmd))
5584 res = issue_thinkpad_cmos_command(cmos_cmd);
5585 return (res) ? res : count;
5588 static DEVICE_ATTR_WO(cmos_command);
5590 /* --------------------------------------------------------------------- */
5592 static int __init cmos_init(struct ibm_init_struct *iibm)
5596 vdbg_printk(TPACPI_DBG_INIT,
5597 "initializing cmos commands subdriver\n");
5599 TPACPI_ACPIHANDLE_INIT(cmos);
5601 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5602 str_supported(cmos_handle != NULL));
5604 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5608 return (cmos_handle) ? 0 : 1;
5611 static void cmos_exit(void)
5613 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5616 static int cmos_read(struct seq_file *m)
5618 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5619 R30, R31, T20-22, X20-21 */
5621 seq_printf(m, "status:\t\tnot supported\n");
5623 seq_printf(m, "status:\t\tsupported\n");
5624 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5630 static int cmos_write(char *buf)
5635 while ((cmd = next_cmd(&buf))) {
5636 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5637 cmos_cmd >= 0 && cmos_cmd <= 21) {
5642 res = issue_thinkpad_cmos_command(cmos_cmd);
5650 static struct ibm_struct cmos_driver_data = {
5653 .write = cmos_write,
5657 /*************************************************************************
5661 enum led_access_mode {
5662 TPACPI_LED_NONE = 0,
5663 TPACPI_LED_570, /* 570 */
5664 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5665 TPACPI_LED_NEW, /* all others */
5668 enum { /* For TPACPI_LED_OLD */
5669 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5670 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5671 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5674 static enum led_access_mode led_supported;
5676 static acpi_handle led_handle;
5678 #define TPACPI_LED_NUMLEDS 16
5679 static struct tpacpi_led_classdev *tpacpi_leds;
5680 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5681 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5682 /* there's a limit of 19 chars + NULL before 2.6.26 */
5684 "tpacpi:orange:batt",
5685 "tpacpi:green:batt",
5686 "tpacpi::dock_active",
5687 "tpacpi::bay_active",
5688 "tpacpi::dock_batt",
5689 "tpacpi::unknown_led",
5691 "tpacpi::dock_status1",
5692 "tpacpi::dock_status2",
5693 "tpacpi::unknown_led2",
5694 "tpacpi::unknown_led3",
5695 "tpacpi::thinkvantage",
5697 #define TPACPI_SAFE_LEDS 0x1081U
5699 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5701 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5704 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5708 static int led_get_status(const unsigned int led)
5711 enum led_status_t led_s;
5713 switch (led_supported) {
5714 case TPACPI_LED_570:
5715 if (!acpi_evalf(ec_handle,
5716 &status, "GLED", "dd", 1 << led))
5718 led_s = (status == 0) ?
5723 tpacpi_led_state_cache[led] = led_s;
5732 static int led_set_status(const unsigned int led,
5733 const enum led_status_t ledstatus)
5735 /* off, on, blink. Index is led_status_t */
5736 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5737 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5741 switch (led_supported) {
5742 case TPACPI_LED_570:
5744 if (unlikely(led > 7))
5746 if (unlikely(tpacpi_is_led_restricted(led)))
5748 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5749 (1 << led), led_sled_arg1[ledstatus]))
5752 case TPACPI_LED_OLD:
5753 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5754 if (unlikely(led > 7))
5756 if (unlikely(tpacpi_is_led_restricted(led)))
5758 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5760 rc = ec_write(TPACPI_LED_EC_HLBL,
5761 (ledstatus == TPACPI_LED_BLINK) << led);
5763 rc = ec_write(TPACPI_LED_EC_HLCL,
5764 (ledstatus != TPACPI_LED_OFF) << led);
5766 case TPACPI_LED_NEW:
5768 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5770 if (unlikely(tpacpi_is_led_restricted(led)))
5772 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5773 led, led_led_arg1[ledstatus]))
5781 tpacpi_led_state_cache[led] = ledstatus;
5786 static int led_sysfs_set(struct led_classdev *led_cdev,
5787 enum led_brightness brightness)
5789 struct tpacpi_led_classdev *data = container_of(led_cdev,
5790 struct tpacpi_led_classdev, led_classdev);
5791 enum led_status_t new_state;
5793 if (brightness == LED_OFF)
5794 new_state = TPACPI_LED_OFF;
5795 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5796 new_state = TPACPI_LED_ON;
5798 new_state = TPACPI_LED_BLINK;
5800 return led_set_status(data->led, new_state);
5803 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5804 unsigned long *delay_on, unsigned long *delay_off)
5806 struct tpacpi_led_classdev *data = container_of(led_cdev,
5807 struct tpacpi_led_classdev, led_classdev);
5809 /* Can we choose the flash rate? */
5810 if (*delay_on == 0 && *delay_off == 0) {
5811 /* yes. set them to the hardware blink rate (1 Hz) */
5812 *delay_on = 500; /* ms */
5813 *delay_off = 500; /* ms */
5814 } else if ((*delay_on != 500) || (*delay_off != 500))
5817 return led_set_status(data->led, TPACPI_LED_BLINK);
5820 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5824 struct tpacpi_led_classdev *data = container_of(led_cdev,
5825 struct tpacpi_led_classdev, led_classdev);
5827 rc = led_get_status(data->led);
5829 if (rc == TPACPI_LED_OFF || rc < 0)
5830 rc = LED_OFF; /* no error handling in led class :( */
5837 static void led_exit(void)
5841 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5842 if (tpacpi_leds[i].led_classdev.name)
5843 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5849 static int __init tpacpi_init_led(unsigned int led)
5853 tpacpi_leds[led].led = led;
5855 /* LEDs with no name don't get registered */
5856 if (!tpacpi_led_names[led])
5859 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5860 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5861 if (led_supported == TPACPI_LED_570)
5862 tpacpi_leds[led].led_classdev.brightness_get =
5865 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5867 rc = led_classdev_register(&tpacpi_pdev->dev,
5868 &tpacpi_leds[led].led_classdev);
5870 tpacpi_leds[led].led_classdev.name = NULL;
5875 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5876 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5877 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5878 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5880 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5881 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5882 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5883 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5884 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5885 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5886 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5887 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5889 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5890 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5891 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5892 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5893 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5895 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5896 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5897 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5898 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5900 /* (1) - may have excess leds enabled on MSB */
5902 /* Defaults (order matters, keep last, don't reorder!) */
5904 .vendor = PCI_VENDOR_ID_LENOVO,
5905 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5908 { /* IBM ThinkPads with no EC version string */
5909 .vendor = PCI_VENDOR_ID_IBM,
5910 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5913 { /* IBM ThinkPads with EC version string */
5914 .vendor = PCI_VENDOR_ID_IBM,
5915 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5920 #undef TPACPI_LEDQ_IBM
5921 #undef TPACPI_LEDQ_LNV
5923 static enum led_access_mode __init led_init_detect_mode(void)
5927 if (tpacpi_is_ibm()) {
5929 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5930 if (ACPI_SUCCESS(status))
5931 return TPACPI_LED_570;
5933 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5934 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5935 if (ACPI_SUCCESS(status))
5936 return TPACPI_LED_OLD;
5940 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5941 if (ACPI_SUCCESS(status))
5942 return TPACPI_LED_NEW;
5944 /* R30, R31, and unknown firmwares */
5946 return TPACPI_LED_NONE;
5949 static int __init led_init(struct ibm_init_struct *iibm)
5953 unsigned long useful_leds;
5955 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5957 led_supported = led_init_detect_mode();
5959 if (led_supported != TPACPI_LED_NONE) {
5960 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5961 ARRAY_SIZE(led_useful_qtable));
5965 led_supported = TPACPI_LED_NONE;
5969 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5970 str_supported(led_supported), led_supported);
5972 if (led_supported == TPACPI_LED_NONE)
5975 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5978 pr_err("Out of memory for LED data\n");
5982 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5983 tpacpi_leds[i].led = -1;
5985 if (!tpacpi_is_led_restricted(i) &&
5986 test_bit(i, &useful_leds)) {
5987 rc = tpacpi_init_led(i);
5995 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5996 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6001 #define str_led_status(s) \
6002 ((s) == TPACPI_LED_OFF ? "off" : \
6003 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6005 static int led_read(struct seq_file *m)
6007 if (!led_supported) {
6008 seq_printf(m, "status:\t\tnot supported\n");
6011 seq_printf(m, "status:\t\tsupported\n");
6013 if (led_supported == TPACPI_LED_570) {
6016 for (i = 0; i < 8; i++) {
6017 status = led_get_status(i);
6020 seq_printf(m, "%d:\t\t%s\n",
6021 i, str_led_status(status));
6025 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6030 static int led_write(char *buf)
6034 enum led_status_t s;
6039 while ((cmd = next_cmd(&buf))) {
6040 if (sscanf(cmd, "%d", &led) != 1)
6043 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6044 tpacpi_leds[led].led < 0)
6047 if (strstr(cmd, "off")) {
6049 } else if (strstr(cmd, "on")) {
6051 } else if (strstr(cmd, "blink")) {
6052 s = TPACPI_LED_BLINK;
6057 rc = led_set_status(led, s);
6065 static struct ibm_struct led_driver_data = {
6072 /*************************************************************************
6076 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6078 #define TPACPI_BEEP_Q1 0x0001
6080 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6081 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6082 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6085 static int __init beep_init(struct ibm_init_struct *iibm)
6087 unsigned long quirks;
6089 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6091 TPACPI_ACPIHANDLE_INIT(beep);
6093 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6094 str_supported(beep_handle != NULL));
6096 quirks = tpacpi_check_quirks(beep_quirk_table,
6097 ARRAY_SIZE(beep_quirk_table));
6099 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6101 return (beep_handle) ? 0 : 1;
6104 static int beep_read(struct seq_file *m)
6107 seq_printf(m, "status:\t\tnot supported\n");
6109 seq_printf(m, "status:\t\tsupported\n");
6110 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6116 static int beep_write(char *buf)
6124 while ((cmd = next_cmd(&buf))) {
6125 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6126 beep_cmd >= 0 && beep_cmd <= 17) {
6130 if (tp_features.beep_needs_two_args) {
6131 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6135 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6144 static struct ibm_struct beep_driver_data = {
6147 .write = beep_write,
6150 /*************************************************************************
6154 enum thermal_access_mode {
6155 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6156 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6157 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6158 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6159 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6162 enum { /* TPACPI_THERMAL_TPEC_* */
6163 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6164 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6165 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6166 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6168 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6172 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6173 struct ibm_thermal_sensors_struct {
6174 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6177 static enum thermal_access_mode thermal_read_mode;
6179 /* idx is zero-based */
6180 static int thermal_get_sensor(int idx, s32 *value)
6186 t = TP_EC_THERMAL_TMP0;
6188 switch (thermal_read_mode) {
6189 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6190 case TPACPI_THERMAL_TPEC_16:
6191 if (idx >= 8 && idx <= 15) {
6192 t = TP_EC_THERMAL_TMP8;
6197 case TPACPI_THERMAL_TPEC_8:
6199 if (!acpi_ec_read(t + idx, &tmp))
6201 *value = tmp * 1000;
6206 case TPACPI_THERMAL_ACPI_UPDT:
6208 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6209 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6211 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6213 *value = (t - 2732) * 100;
6218 case TPACPI_THERMAL_ACPI_TMP07:
6220 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6221 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6223 if (t > 127 || t < -127)
6224 t = TP_EC_THERMAL_TMP_NA;
6230 case TPACPI_THERMAL_NONE:
6238 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6249 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6252 for (i = 0 ; i < n; i++) {
6253 res = thermal_get_sensor(i, &s->temp[i]);
6261 static void thermal_dump_all_sensors(void)
6264 struct ibm_thermal_sensors_struct t;
6266 n = thermal_get_sensors(&t);
6270 pr_notice("temperatures (Celsius):");
6272 for (i = 0; i < n; i++) {
6273 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6274 pr_cont(" %d", (int)(t.temp[i] / 1000));
6282 /* sysfs temp##_input -------------------------------------------------- */
6284 static ssize_t thermal_temp_input_show(struct device *dev,
6285 struct device_attribute *attr,
6288 struct sensor_device_attribute *sensor_attr =
6289 to_sensor_dev_attr(attr);
6290 int idx = sensor_attr->index;
6294 res = thermal_get_sensor(idx, &value);
6297 if (value == TPACPI_THERMAL_SENSOR_NA)
6300 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6303 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6304 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6305 thermal_temp_input_show, NULL, _idxB)
6307 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6308 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6309 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6310 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6311 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6312 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6313 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6314 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6315 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6316 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6317 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6318 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6319 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6320 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6321 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6322 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6323 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6326 #define THERMAL_ATTRS(X) \
6327 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6329 static struct attribute *thermal_temp_input_attr[] = {
6349 static const struct attribute_group thermal_temp_input16_group = {
6350 .attrs = thermal_temp_input_attr
6353 static const struct attribute_group thermal_temp_input8_group = {
6354 .attrs = &thermal_temp_input_attr[8]
6357 #undef THERMAL_SENSOR_ATTR_TEMP
6358 #undef THERMAL_ATTRS
6360 /* --------------------------------------------------------------------- */
6362 static int __init thermal_init(struct ibm_init_struct *iibm)
6364 u8 t, ta1, ta2, ver = 0;
6369 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6371 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6373 if (thinkpad_id.ec_model) {
6375 * Direct EC access mode: sensors at registers
6376 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6377 * non-implemented, thermal sensors return 0x80 when
6379 * The above rule is unfortunately flawed. This has been seen with
6380 * 0xC2 (power supply ID) causing thermal control problems.
6381 * The EC version can be determined by offset 0xEF and at least for
6382 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6383 * are not thermal registers.
6385 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6386 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6389 for (i = 0; i < 8; i++) {
6390 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6397 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6406 /* This is sheer paranoia, but we handle it anyway */
6408 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6409 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6411 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6412 thermal_read_mode = TPACPI_THERMAL_NONE;
6416 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6420 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6422 } else if (acpi_tmp7) {
6423 if (tpacpi_is_ibm() &&
6424 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6425 /* 600e/x, 770e, 770x */
6426 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6428 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6429 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6432 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6433 thermal_read_mode = TPACPI_THERMAL_NONE;
6436 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6437 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6440 switch (thermal_read_mode) {
6441 case TPACPI_THERMAL_TPEC_16:
6442 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6443 &thermal_temp_input16_group);
6447 case TPACPI_THERMAL_TPEC_8:
6448 case TPACPI_THERMAL_ACPI_TMP07:
6449 case TPACPI_THERMAL_ACPI_UPDT:
6450 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6451 &thermal_temp_input8_group);
6455 case TPACPI_THERMAL_NONE:
6463 static void thermal_exit(void)
6465 switch (thermal_read_mode) {
6466 case TPACPI_THERMAL_TPEC_16:
6467 sysfs_remove_group(&tpacpi_hwmon->kobj,
6468 &thermal_temp_input16_group);
6470 case TPACPI_THERMAL_TPEC_8:
6471 case TPACPI_THERMAL_ACPI_TMP07:
6472 case TPACPI_THERMAL_ACPI_UPDT:
6473 sysfs_remove_group(&tpacpi_hwmon->kobj,
6474 &thermal_temp_input8_group);
6476 case TPACPI_THERMAL_NONE:
6482 static int thermal_read(struct seq_file *m)
6485 struct ibm_thermal_sensors_struct t;
6487 n = thermal_get_sensors(&t);
6488 if (unlikely(n < 0))
6491 seq_printf(m, "temperatures:\t");
6494 for (i = 0; i < (n - 1); i++)
6495 seq_printf(m, "%d ", t.temp[i] / 1000);
6496 seq_printf(m, "%d\n", t.temp[i] / 1000);
6498 seq_printf(m, "not supported\n");
6503 static struct ibm_struct thermal_driver_data = {
6505 .read = thermal_read,
6506 .exit = thermal_exit,
6509 /*************************************************************************
6510 * Backlight/brightness subdriver
6513 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6516 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6517 * CMOS NVRAM byte 0x5E, bits 0-3.
6519 * EC HBRV (0x31) has the following layout
6520 * Bit 7: unknown function
6521 * Bit 6: unknown function
6522 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6523 * Bit 4: must be set to zero to avoid problems
6524 * Bit 3-0: backlight brightness level
6526 * brightness_get_raw returns status data in the HBRV layout
6528 * WARNING: The X61 has been verified to use HBRV for something else, so
6529 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6530 * testing on the very early *60 Lenovo models...
6534 TP_EC_BACKLIGHT = 0x31,
6536 /* TP_EC_BACKLIGHT bitmasks */
6537 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6538 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6539 TP_EC_BACKLIGHT_MAPSW = 0x20,
6542 enum tpacpi_brightness_access_mode {
6543 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6544 TPACPI_BRGHT_MODE_EC, /* EC control */
6545 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6546 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6547 TPACPI_BRGHT_MODE_MAX
6550 static struct backlight_device *ibm_backlight_device;
6552 static enum tpacpi_brightness_access_mode brightness_mode =
6553 TPACPI_BRGHT_MODE_MAX;
6555 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6557 static struct mutex brightness_mutex;
6559 /* NVRAM brightness access,
6560 * call with brightness_mutex held! */
6561 static unsigned int tpacpi_brightness_nvram_get(void)
6565 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6566 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6567 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6568 lnvram &= bright_maxlvl;
6573 static void tpacpi_brightness_checkpoint_nvram(void)
6578 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6581 vdbg_printk(TPACPI_DBG_BRGHT,
6582 "trying to checkpoint backlight level to NVRAM...\n");
6584 if (mutex_lock_killable(&brightness_mutex) < 0)
6587 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6589 lec &= TP_EC_BACKLIGHT_LVLMSK;
6590 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6592 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6593 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6594 /* NVRAM needs update */
6595 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6596 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6598 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6599 dbg_printk(TPACPI_DBG_BRGHT,
6600 "updated NVRAM backlight level to %u (0x%02x)\n",
6601 (unsigned int) lec, (unsigned int) b_nvram);
6603 vdbg_printk(TPACPI_DBG_BRGHT,
6604 "NVRAM backlight level already is %u (0x%02x)\n",
6605 (unsigned int) lec, (unsigned int) b_nvram);
6608 mutex_unlock(&brightness_mutex);
6612 /* call with brightness_mutex held! */
6613 static int tpacpi_brightness_get_raw(int *status)
6617 switch (brightness_mode) {
6618 case TPACPI_BRGHT_MODE_UCMS_STEP:
6619 *status = tpacpi_brightness_nvram_get();
6621 case TPACPI_BRGHT_MODE_EC:
6622 case TPACPI_BRGHT_MODE_ECNVRAM:
6623 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6632 /* call with brightness_mutex held! */
6633 /* do NOT call with illegal backlight level value */
6634 static int tpacpi_brightness_set_ec(unsigned int value)
6638 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6641 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6642 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6643 (value & TP_EC_BACKLIGHT_LVLMSK))))
6649 /* call with brightness_mutex held! */
6650 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6653 unsigned int current_value, i;
6655 current_value = tpacpi_brightness_nvram_get();
6657 if (value == current_value)
6660 cmos_cmd = (value > current_value) ?
6661 TP_CMOS_BRIGHTNESS_UP :
6662 TP_CMOS_BRIGHTNESS_DOWN;
6663 inc = (value > current_value) ? 1 : -1;
6665 for (i = current_value; i != value; i += inc)
6666 if (issue_thinkpad_cmos_command(cmos_cmd))
6672 /* May return EINTR which can always be mapped to ERESTARTSYS */
6673 static int brightness_set(unsigned int value)
6677 if (value > bright_maxlvl)
6680 vdbg_printk(TPACPI_DBG_BRGHT,
6681 "set backlight level to %d\n", value);
6683 res = mutex_lock_killable(&brightness_mutex);
6687 switch (brightness_mode) {
6688 case TPACPI_BRGHT_MODE_EC:
6689 case TPACPI_BRGHT_MODE_ECNVRAM:
6690 res = tpacpi_brightness_set_ec(value);
6692 case TPACPI_BRGHT_MODE_UCMS_STEP:
6693 res = tpacpi_brightness_set_ucmsstep(value);
6699 mutex_unlock(&brightness_mutex);
6703 /* sysfs backlight class ----------------------------------------------- */
6705 static int brightness_update_status(struct backlight_device *bd)
6707 unsigned int level =
6708 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6709 bd->props.power == FB_BLANK_UNBLANK) ?
6710 bd->props.brightness : 0;
6712 dbg_printk(TPACPI_DBG_BRGHT,
6713 "backlight: attempt to set level to %d\n",
6716 /* it is the backlight class's job (caller) to handle
6717 * EINTR and other errors properly */
6718 return brightness_set(level);
6721 static int brightness_get(struct backlight_device *bd)
6725 res = mutex_lock_killable(&brightness_mutex);
6729 res = tpacpi_brightness_get_raw(&status);
6731 mutex_unlock(&brightness_mutex);
6736 return status & TP_EC_BACKLIGHT_LVLMSK;
6739 static void tpacpi_brightness_notify_change(void)
6741 backlight_force_update(ibm_backlight_device,
6742 BACKLIGHT_UPDATE_HOTKEY);
6745 static const struct backlight_ops ibm_backlight_data = {
6746 .get_brightness = brightness_get,
6747 .update_status = brightness_update_status,
6750 /* --------------------------------------------------------------------- */
6753 * Call _BCL method of video device. On some ThinkPads this will
6754 * switch the firmware to the ACPI brightness control mode.
6757 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6759 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6760 union acpi_object *obj;
6761 struct acpi_device *device, *child;
6764 if (acpi_bus_get_device(handle, &device))
6768 list_for_each_entry(child, &device->children, node) {
6769 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6771 if (ACPI_FAILURE(status)) {
6772 buffer.length = ACPI_ALLOCATE_BUFFER;
6776 obj = (union acpi_object *)buffer.pointer;
6777 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6778 pr_err("Unknown _BCL data, please report this to %s\n",
6782 rc = obj->package.count;
6787 kfree(buffer.pointer);
6793 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6795 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6797 acpi_handle video_device;
6800 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6802 bcl_levels = tpacpi_query_bcl_levels(video_device);
6804 tp_features.bright_acpimode = (bcl_levels > 0);
6806 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6810 * These are only useful for models that have only one possibility
6811 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6814 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6815 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6816 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6818 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6819 /* Models with ATI GPUs known to require ECNVRAM mode */
6820 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6822 /* Models with ATI GPUs that can use ECNVRAM */
6823 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6824 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6825 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6826 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6828 /* Models with Intel Extreme Graphics 2 */
6829 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6830 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6831 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6833 /* Models with Intel GMA900 */
6834 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6835 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6836 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6840 * Returns < 0 for error, otherwise sets tp_features.bright_*
6841 * and bright_maxlvl.
6843 static void __init tpacpi_detect_brightness_capabilities(void)
6847 vdbg_printk(TPACPI_DBG_INIT,
6848 "detecting firmware brightness interface capabilities\n");
6850 /* we could run a quirks check here (same table used by
6851 * brightness_init) if needed */
6854 * We always attempt to detect acpi support, so as to switch
6855 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6856 * going to publish a backlight interface
6858 b = tpacpi_check_std_acpi_brightness_support();
6868 tp_features.bright_unkfw = 1;
6869 bright_maxlvl = b - 1;
6871 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6874 static int __init brightness_init(struct ibm_init_struct *iibm)
6876 struct backlight_properties props;
6878 unsigned long quirks;
6880 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6882 mutex_init(&brightness_mutex);
6884 quirks = tpacpi_check_quirks(brightness_quirk_table,
6885 ARRAY_SIZE(brightness_quirk_table));
6887 /* tpacpi_detect_brightness_capabilities() must have run already */
6889 /* if it is unknown, we don't handle it: it wouldn't be safe */
6890 if (tp_features.bright_unkfw)
6893 if (!brightness_enable) {
6894 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6895 "brightness support disabled by module parameter\n");
6899 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6900 if (brightness_enable > 1) {
6901 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6903 } else if (brightness_enable == 1) {
6904 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6907 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6908 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
6912 * Check for module parameter bogosity, note that we
6913 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6914 * able to detect "unspecified"
6916 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6919 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6920 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6921 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6922 if (quirks & TPACPI_BRGHT_Q_EC)
6923 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6925 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6927 dbg_printk(TPACPI_DBG_BRGHT,
6928 "driver auto-selected brightness_mode=%d\n",
6933 if (!tpacpi_is_ibm() &&
6934 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6935 brightness_mode == TPACPI_BRGHT_MODE_EC))
6938 if (tpacpi_brightness_get_raw(&b) < 0)
6941 memset(&props, 0, sizeof(struct backlight_properties));
6942 props.type = BACKLIGHT_PLATFORM;
6943 props.max_brightness = bright_maxlvl;
6944 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6945 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6947 &ibm_backlight_data,
6949 if (IS_ERR(ibm_backlight_device)) {
6950 int rc = PTR_ERR(ibm_backlight_device);
6951 ibm_backlight_device = NULL;
6952 pr_err("Could not register backlight device\n");
6955 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6956 "brightness is supported\n");
6958 if (quirks & TPACPI_BRGHT_Q_ASK) {
6959 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6961 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6965 /* Added by mistake in early 2007. Probably useless, but it could
6966 * be working around some unknown firmware problem where the value
6967 * read at startup doesn't match the real hardware state... so leave
6968 * it in place just in case */
6969 backlight_update_status(ibm_backlight_device);
6971 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6972 "brightness: registering brightness hotkeys as change notification\n");
6973 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6974 | TP_ACPI_HKEY_BRGHTUP_MASK
6975 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6979 static void brightness_suspend(void)
6981 tpacpi_brightness_checkpoint_nvram();
6984 static void brightness_shutdown(void)
6986 tpacpi_brightness_checkpoint_nvram();
6989 static void brightness_exit(void)
6991 if (ibm_backlight_device) {
6992 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6993 "calling backlight_device_unregister()\n");
6994 backlight_device_unregister(ibm_backlight_device);
6997 tpacpi_brightness_checkpoint_nvram();
7000 static int brightness_read(struct seq_file *m)
7004 level = brightness_get(NULL);
7006 seq_printf(m, "level:\t\tunreadable\n");
7008 seq_printf(m, "level:\t\t%d\n", level);
7009 seq_printf(m, "commands:\tup, down\n");
7010 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7017 static int brightness_write(char *buf)
7023 level = brightness_get(NULL);
7027 while ((cmd = next_cmd(&buf))) {
7028 if (strlencmp(cmd, "up") == 0) {
7029 if (level < bright_maxlvl)
7031 } else if (strlencmp(cmd, "down") == 0) {
7034 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7035 level >= 0 && level <= bright_maxlvl) {
7041 tpacpi_disclose_usertask("procfs brightness",
7042 "set level to %d\n", level);
7045 * Now we know what the final level should be, so we try to set it.
7046 * Doing it this way makes the syscall restartable in case of EINTR
7048 rc = brightness_set(level);
7049 if (!rc && ibm_backlight_device)
7050 backlight_force_update(ibm_backlight_device,
7051 BACKLIGHT_UPDATE_SYSFS);
7052 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7055 static struct ibm_struct brightness_driver_data = {
7056 .name = "brightness",
7057 .read = brightness_read,
7058 .write = brightness_write,
7059 .exit = brightness_exit,
7060 .suspend = brightness_suspend,
7061 .shutdown = brightness_shutdown,
7064 /*************************************************************************
7069 * IBM ThinkPads have a simple volume controller with MUTE gating.
7070 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7072 * Since the *61 series (and probably also the later *60 series), Lenovo
7073 * ThinkPads only implement the MUTE gate.
7076 * Bit 6: MUTE (1 mutes sound)
7078 * Other bits should be zero as far as we know.
7080 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7081 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7082 * such as bit 7 which is used to detect repeated presses of MUTE,
7083 * and we leave them unchanged.
7085 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7086 * in response to user input. Unfortunately, this rarely works well.
7087 * The laptop changes the state of its internal MUTE gate and, on some
7088 * models, sends KEY_MUTE, causing any user code that responds to the
7089 * mute button to get confused. The hardware MUTE gate is also
7090 * unnecessary, since user code can handle the mute button without
7091 * kernel or EC help.
7093 * To avoid confusing userspace, we simply disable all EC-based mute
7094 * and volume controls when possible.
7097 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7099 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7100 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7101 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7103 #if SNDRV_CARDS <= 32
7104 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7106 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7108 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7109 static char *alsa_id = "ThinkPadEC";
7110 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7112 struct tpacpi_alsa_data {
7113 struct snd_card *card;
7114 struct snd_ctl_elem_id *ctl_mute_id;
7115 struct snd_ctl_elem_id *ctl_vol_id;
7118 static struct snd_card *alsa_card;
7123 /* TP_EC_AUDIO bits */
7124 TP_EC_AUDIO_MUTESW = 6,
7126 /* TP_EC_AUDIO bitmasks */
7127 TP_EC_AUDIO_LVL_MSK = 0x0F,
7128 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7130 /* Maximum volume */
7131 TP_EC_VOLUME_MAX = 14,
7134 enum tpacpi_volume_access_mode {
7135 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7136 TPACPI_VOL_MODE_EC, /* Pure EC control */
7137 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7138 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7142 enum tpacpi_volume_capabilities {
7143 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7144 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7145 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7149 enum tpacpi_mute_btn_mode {
7150 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7151 /* We don't know what mode 1 is. */
7152 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7153 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7156 static enum tpacpi_volume_access_mode volume_mode =
7157 TPACPI_VOL_MODE_MAX;
7159 static enum tpacpi_volume_capabilities volume_capabilities;
7160 static bool volume_control_allowed;
7161 static bool software_mute_requested = true;
7162 static bool software_mute_active;
7163 static int software_mute_orig_mode;
7166 * Used to syncronize writers to TP_EC_AUDIO and
7167 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7169 static struct mutex volume_mutex;
7171 static void tpacpi_volume_checkpoint_nvram(void)
7177 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7179 if (!volume_control_allowed)
7181 if (software_mute_active)
7184 vdbg_printk(TPACPI_DBG_MIXER,
7185 "trying to checkpoint mixer state to NVRAM...\n");
7187 if (tp_features.mixer_no_level_control)
7188 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7190 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7192 if (mutex_lock_killable(&volume_mutex) < 0)
7195 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7198 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7200 if (lec != (b_nvram & ec_mask)) {
7201 /* NVRAM needs update */
7202 b_nvram &= ~ec_mask;
7204 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7205 dbg_printk(TPACPI_DBG_MIXER,
7206 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7207 (unsigned int) lec, (unsigned int) b_nvram);
7209 vdbg_printk(TPACPI_DBG_MIXER,
7210 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7211 (unsigned int) lec, (unsigned int) b_nvram);
7215 mutex_unlock(&volume_mutex);
7218 static int volume_get_status_ec(u8 *status)
7222 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7227 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7232 static int volume_get_status(u8 *status)
7234 return volume_get_status_ec(status);
7237 static int volume_set_status_ec(const u8 status)
7239 if (!acpi_ec_write(TP_EC_AUDIO, status))
7242 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7245 * On X200s, and possibly on others, it can take a while for
7246 * reads to become correct.
7253 static int volume_set_status(const u8 status)
7255 return volume_set_status_ec(status);
7258 /* returns < 0 on error, 0 on no change, 1 on change */
7259 static int __volume_set_mute_ec(const bool mute)
7264 if (mutex_lock_killable(&volume_mutex) < 0)
7267 rc = volume_get_status_ec(&s);
7271 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7272 s & ~TP_EC_AUDIO_MUTESW_MSK;
7275 rc = volume_set_status_ec(n);
7281 mutex_unlock(&volume_mutex);
7285 static int volume_alsa_set_mute(const bool mute)
7287 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7288 (mute) ? "" : "un");
7289 return __volume_set_mute_ec(mute);
7292 static int volume_set_mute(const bool mute)
7296 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7297 (mute) ? "" : "un");
7299 rc = __volume_set_mute_ec(mute);
7300 return (rc < 0) ? rc : 0;
7303 /* returns < 0 on error, 0 on no change, 1 on change */
7304 static int __volume_set_volume_ec(const u8 vol)
7309 if (vol > TP_EC_VOLUME_MAX)
7312 if (mutex_lock_killable(&volume_mutex) < 0)
7315 rc = volume_get_status_ec(&s);
7319 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7322 rc = volume_set_status_ec(n);
7328 mutex_unlock(&volume_mutex);
7332 static int volume_set_software_mute(bool startup)
7336 if (!tpacpi_is_lenovo())
7340 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7344 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7345 "Initial HAUM setting was %d\n",
7346 software_mute_orig_mode);
7349 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7350 (int)TP_EC_MUTE_BTN_NONE))
7353 if (result != TP_EC_MUTE_BTN_NONE)
7354 pr_warn("Unexpected SAUM result %d\n",
7358 * In software mute mode, the standard codec controls take
7359 * precendence, so we unmute the ThinkPad HW switch at
7360 * startup. Just on case there are SAUM-capable ThinkPads
7361 * with level controls, set max HW volume as well.
7363 if (tp_features.mixer_no_level_control)
7364 result = volume_set_mute(false);
7366 result = volume_set_status(TP_EC_VOLUME_MAX);
7369 pr_warn("Failed to unmute the HW mute switch\n");
7374 static void volume_exit_software_mute(void)
7378 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7379 || r != software_mute_orig_mode)
7380 pr_warn("Failed to restore mute mode\n");
7383 static int volume_alsa_set_volume(const u8 vol)
7385 dbg_printk(TPACPI_DBG_MIXER,
7386 "ALSA: trying to set volume level to %hu\n", vol);
7387 return __volume_set_volume_ec(vol);
7390 static void volume_alsa_notify_change(void)
7392 struct tpacpi_alsa_data *d;
7394 if (alsa_card && alsa_card->private_data) {
7395 d = alsa_card->private_data;
7397 snd_ctl_notify(alsa_card,
7398 SNDRV_CTL_EVENT_MASK_VALUE,
7401 snd_ctl_notify(alsa_card,
7402 SNDRV_CTL_EVENT_MASK_VALUE,
7407 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7408 struct snd_ctl_elem_info *uinfo)
7410 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7412 uinfo->value.integer.min = 0;
7413 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7417 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7418 struct snd_ctl_elem_value *ucontrol)
7423 rc = volume_get_status(&s);
7427 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7431 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7432 struct snd_ctl_elem_value *ucontrol)
7434 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7435 ucontrol->value.integer.value[0]);
7436 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7439 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7441 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7442 struct snd_ctl_elem_value *ucontrol)
7447 rc = volume_get_status(&s);
7451 ucontrol->value.integer.value[0] =
7452 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7456 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7457 struct snd_ctl_elem_value *ucontrol)
7459 tpacpi_disclose_usertask("ALSA", "%smute\n",
7460 ucontrol->value.integer.value[0] ?
7462 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7465 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7466 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7467 .name = "Console Playback Volume",
7469 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7470 .info = volume_alsa_vol_info,
7471 .get = volume_alsa_vol_get,
7474 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7475 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7476 .name = "Console Playback Switch",
7478 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7479 .info = volume_alsa_mute_info,
7480 .get = volume_alsa_mute_get,
7483 static void volume_suspend(void)
7485 tpacpi_volume_checkpoint_nvram();
7488 static void volume_resume(void)
7490 if (software_mute_active) {
7491 if (volume_set_software_mute(false) < 0)
7492 pr_warn("Failed to restore software mute\n");
7494 volume_alsa_notify_change();
7498 static void volume_shutdown(void)
7500 tpacpi_volume_checkpoint_nvram();
7503 static void volume_exit(void)
7506 snd_card_free(alsa_card);
7510 tpacpi_volume_checkpoint_nvram();
7512 if (software_mute_active)
7513 volume_exit_software_mute();
7516 static int __init volume_create_alsa_mixer(void)
7518 struct snd_card *card;
7519 struct tpacpi_alsa_data *data;
7520 struct snd_kcontrol *ctl_vol;
7521 struct snd_kcontrol *ctl_mute;
7524 rc = snd_card_new(&tpacpi_pdev->dev,
7525 alsa_index, alsa_id, THIS_MODULE,
7526 sizeof(struct tpacpi_alsa_data), &card);
7527 if (rc < 0 || !card) {
7528 pr_err("Failed to create ALSA card structures: %d\n", rc);
7532 BUG_ON(!card->private_data);
7533 data = card->private_data;
7536 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7537 sizeof(card->driver));
7538 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7539 sizeof(card->shortname));
7540 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7541 (thinkpad_id.ec_version_str) ?
7542 thinkpad_id.ec_version_str : "(unknown)");
7543 snprintf(card->longname, sizeof(card->longname),
7544 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7545 (thinkpad_id.ec_version_str) ?
7546 thinkpad_id.ec_version_str : "unknown");
7548 if (volume_control_allowed) {
7549 volume_alsa_control_vol.put = volume_alsa_vol_put;
7550 volume_alsa_control_vol.access =
7551 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7553 volume_alsa_control_mute.put = volume_alsa_mute_put;
7554 volume_alsa_control_mute.access =
7555 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7558 if (!tp_features.mixer_no_level_control) {
7559 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7560 rc = snd_ctl_add(card, ctl_vol);
7562 pr_err("Failed to create ALSA volume control: %d\n",
7566 data->ctl_vol_id = &ctl_vol->id;
7569 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7570 rc = snd_ctl_add(card, ctl_mute);
7572 pr_err("Failed to create ALSA mute control: %d\n", rc);
7575 data->ctl_mute_id = &ctl_mute->id;
7577 rc = snd_card_register(card);
7579 pr_err("Failed to register ALSA card: %d\n", rc);
7587 snd_card_free(card);
7591 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7592 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7594 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7595 /* Whitelist volume level on all IBM by default */
7596 { .vendor = PCI_VENDOR_ID_IBM,
7597 .bios = TPACPI_MATCH_ANY,
7598 .ec = TPACPI_MATCH_ANY,
7599 .quirks = TPACPI_VOL_Q_LEVEL },
7601 /* Lenovo models with volume control (needs confirmation) */
7602 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7603 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7604 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7605 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7606 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7607 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7608 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7610 /* Whitelist mute-only on all Lenovo by default */
7611 { .vendor = PCI_VENDOR_ID_LENOVO,
7612 .bios = TPACPI_MATCH_ANY,
7613 .ec = TPACPI_MATCH_ANY,
7614 .quirks = TPACPI_VOL_Q_MUTEONLY }
7617 static int __init volume_init(struct ibm_init_struct *iibm)
7619 unsigned long quirks;
7622 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7624 mutex_init(&volume_mutex);
7627 * Check for module parameter bogosity, note that we
7628 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7629 * able to detect "unspecified"
7631 if (volume_mode > TPACPI_VOL_MODE_MAX)
7634 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7635 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7640 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7644 * The ALSA mixer is our primary interface.
7645 * When disabled, don't install the subdriver at all
7648 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7649 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7653 quirks = tpacpi_check_quirks(volume_quirk_table,
7654 ARRAY_SIZE(volume_quirk_table));
7656 switch (volume_capabilities) {
7657 case TPACPI_VOL_CAP_AUTO:
7658 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7659 tp_features.mixer_no_level_control = 1;
7660 else if (quirks & TPACPI_VOL_Q_LEVEL)
7661 tp_features.mixer_no_level_control = 0;
7663 return 1; /* no mixer */
7665 case TPACPI_VOL_CAP_VOLMUTE:
7666 tp_features.mixer_no_level_control = 0;
7668 case TPACPI_VOL_CAP_MUTEONLY:
7669 tp_features.mixer_no_level_control = 1;
7675 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7676 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7677 "using user-supplied volume_capabilities=%d\n",
7678 volume_capabilities);
7680 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7681 volume_mode == TPACPI_VOL_MODE_MAX) {
7682 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7684 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7685 "driver auto-selected volume_mode=%d\n",
7688 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7689 "using user-supplied volume_mode=%d\n",
7693 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7694 "mute is supported, volume control is %s\n",
7695 str_supported(!tp_features.mixer_no_level_control));
7697 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7698 software_mute_active = true;
7700 rc = volume_create_alsa_mixer();
7702 pr_err("Could not create the ALSA mixer interface\n");
7706 pr_info("Console audio control enabled, mode: %s\n",
7707 (volume_control_allowed) ?
7708 "override (read/write)" :
7709 "monitor (read only)");
7712 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7713 "registering volume hotkeys as change notification\n");
7714 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7715 | TP_ACPI_HKEY_VOLUP_MASK
7716 | TP_ACPI_HKEY_VOLDWN_MASK
7717 | TP_ACPI_HKEY_MUTE_MASK);
7722 static int volume_read(struct seq_file *m)
7726 if (volume_get_status(&status) < 0) {
7727 seq_printf(m, "level:\t\tunreadable\n");
7729 if (tp_features.mixer_no_level_control)
7730 seq_printf(m, "level:\t\tunsupported\n");
7732 seq_printf(m, "level:\t\t%d\n",
7733 status & TP_EC_AUDIO_LVL_MSK);
7735 seq_printf(m, "mute:\t\t%s\n",
7736 onoff(status, TP_EC_AUDIO_MUTESW));
7738 if (volume_control_allowed) {
7739 seq_printf(m, "commands:\tunmute, mute\n");
7740 if (!tp_features.mixer_no_level_control) {
7741 seq_printf(m, "commands:\tup, down\n");
7742 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7751 static int volume_write(char *buf)
7754 u8 new_level, new_mute;
7760 * We do allow volume control at driver startup, so that the
7761 * user can set initial state through the volume=... parameter hack.
7763 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7764 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7765 tp_warned.volume_ctrl_forbidden = 1;
7766 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7767 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7772 rc = volume_get_status(&s);
7776 new_level = s & TP_EC_AUDIO_LVL_MSK;
7777 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7779 while ((cmd = next_cmd(&buf))) {
7780 if (!tp_features.mixer_no_level_control) {
7781 if (strlencmp(cmd, "up") == 0) {
7784 else if (new_level < TP_EC_VOLUME_MAX)
7787 } else if (strlencmp(cmd, "down") == 0) {
7790 else if (new_level > 0)
7793 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7794 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7799 if (strlencmp(cmd, "mute") == 0)
7800 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7801 else if (strlencmp(cmd, "unmute") == 0)
7807 if (tp_features.mixer_no_level_control) {
7808 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7809 new_mute ? "" : "un");
7810 rc = volume_set_mute(!!new_mute);
7812 tpacpi_disclose_usertask("procfs volume",
7813 "%smute and set level to %d\n",
7814 new_mute ? "" : "un", new_level);
7815 rc = volume_set_status(new_mute | new_level);
7817 volume_alsa_notify_change();
7819 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7822 static struct ibm_struct volume_driver_data = {
7824 .read = volume_read,
7825 .write = volume_write,
7826 .exit = volume_exit,
7827 .suspend = volume_suspend,
7828 .resume = volume_resume,
7829 .shutdown = volume_shutdown,
7832 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7834 #define alsa_card NULL
7836 static inline void volume_alsa_notify_change(void)
7840 static int __init volume_init(struct ibm_init_struct *iibm)
7842 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7847 static struct ibm_struct volume_driver_data = {
7851 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7853 /*************************************************************************
7860 * TPACPI_FAN_RD_ACPI_GFAN:
7861 * ACPI GFAN method: returns fan level
7863 * see TPACPI_FAN_WR_ACPI_SFAN
7864 * EC 0x2f (HFSP) not available if GFAN exists
7866 * TPACPI_FAN_WR_ACPI_SFAN:
7867 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7869 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7872 * TPACPI_FAN_WR_TPEC:
7873 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7874 * Supported on almost all ThinkPads
7876 * Fan speed changes of any sort (including those caused by the
7877 * disengaged mode) are usually done slowly by the firmware as the
7878 * maximum amount of fan duty cycle change per second seems to be
7881 * Reading is not available if GFAN exists.
7882 * Writing is not available if SFAN exists.
7885 * 7 automatic mode engaged;
7886 * (default operation mode of the ThinkPad)
7887 * fan level is ignored in this mode.
7888 * 6 full speed mode (takes precedence over bit 7);
7889 * not available on all thinkpads. May disable
7890 * the tachometer while the fan controller ramps up
7891 * the speed (which can take up to a few *minutes*).
7892 * Speeds up fan to 100% duty-cycle, which is far above
7893 * the standard RPM levels. It is not impossible that
7894 * it could cause hardware damage.
7895 * 5-3 unused in some models. Extra bits for fan level
7896 * in others, but still useless as all values above
7897 * 7 map to the same speed as level 7 in these models.
7898 * 2-0 fan level (0..7 usually)
7900 * 0x07 = max (set when temperatures critical)
7901 * Some ThinkPads may have other levels, see
7902 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7904 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7905 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7906 * does so, its initial value is meaningless (0x07).
7908 * For firmware bugs, refer to:
7909 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7913 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7914 * Main fan tachometer reading (in RPM)
7916 * This register is present on all ThinkPads with a new-style EC, and
7917 * it is known not to be present on the A21m/e, and T22, as there is
7918 * something else in offset 0x84 according to the ACPI DSDT. Other
7919 * ThinkPads from this same time period (and earlier) probably lack the
7920 * tachometer as well.
7922 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7923 * was never fixed by IBM to report the EC firmware version string
7924 * probably support the tachometer (like the early X models), so
7925 * detecting it is quite hard. We need more data to know for sure.
7927 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7930 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7933 * For firmware bugs, refer to:
7934 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7938 * ThinkPad EC register 0x31 bit 0 (only on select models)
7940 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7941 * show the speed of the main fan. When bit 0 of EC register 0x31
7942 * is one, the tachometer registers show the speed of the auxiliary
7945 * Fan control seems to affect both fans, regardless of the state
7948 * So far, only the firmware for the X60/X61 non-tablet versions
7949 * seem to support this (firmware TP-7M).
7951 * TPACPI_FAN_WR_ACPI_FANS:
7952 * ThinkPad X31, X40, X41. Not available in the X60.
7954 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7955 * high speed. ACPI DSDT seems to map these three speeds to levels
7956 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7957 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7959 * The speeds are stored on handles
7960 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7962 * There are three default speed sets, accessible as handles:
7963 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7965 * ACPI DSDT switches which set is in use depending on various
7968 * TPACPI_FAN_WR_TPEC is also available and should be used to
7969 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7970 * but the ACPI tables just mention level 7.
7973 enum { /* Fan control constants */
7974 fan_status_offset = 0x2f, /* EC register 0x2f */
7975 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7976 * 0x84 must be read before 0x85 */
7977 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7978 bit 0 selects which fan is active */
7980 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7981 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7983 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7986 enum fan_status_access_mode {
7987 TPACPI_FAN_NONE = 0, /* No fan status or control */
7988 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7989 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7992 enum fan_control_access_mode {
7993 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7994 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7995 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7996 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7999 enum fan_control_commands {
8000 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8001 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8002 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8003 * and also watchdog cmd */
8006 static bool fan_control_allowed;
8008 static enum fan_status_access_mode fan_status_access_mode;
8009 static enum fan_control_access_mode fan_control_access_mode;
8010 static enum fan_control_commands fan_control_commands;
8012 static u8 fan_control_initial_status;
8013 static u8 fan_control_desired_level;
8014 static u8 fan_control_resume_level;
8015 static int fan_watchdog_maxinterval;
8017 static struct mutex fan_mutex;
8019 static void fan_watchdog_fire(struct work_struct *ignored);
8020 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8022 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8023 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8024 "\\FSPD", /* 600e/x, 770e, 770x */
8026 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8027 "JFNS", /* 770x-JL */
8031 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8032 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8033 * be in auto mode (0x80).
8035 * This is corrected by any write to HFSP either by the driver, or
8038 * We assume 0x07 really means auto mode while this quirk is active,
8039 * as this is far more likely than the ThinkPad being in level 7,
8040 * which is only used by the firmware during thermal emergencies.
8042 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8043 * TP-70 (T43, R52), which are known to be buggy.
8046 static void fan_quirk1_setup(void)
8048 if (fan_control_initial_status == 0x07) {
8049 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8050 tp_features.fan_ctrl_status_undef = 1;
8054 static void fan_quirk1_handle(u8 *fan_status)
8056 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8057 if (*fan_status != fan_control_initial_status) {
8058 /* something changed the HFSP regisnter since
8059 * driver init time, so it is not undefined
8061 tp_features.fan_ctrl_status_undef = 0;
8063 /* Return most likely status. In fact, it
8064 * might be the only possible status */
8065 *fan_status = TP_EC_FAN_AUTO;
8070 /* Select main fan on X60/X61, NOOP on others */
8071 static bool fan_select_fan1(void)
8073 if (tp_features.second_fan) {
8076 if (ec_read(fan_select_offset, &val) < 0)
8079 if (ec_write(fan_select_offset, val) < 0)
8085 /* Select secondary fan on X60/X61 */
8086 static bool fan_select_fan2(void)
8090 if (!tp_features.second_fan)
8093 if (ec_read(fan_select_offset, &val) < 0)
8096 if (ec_write(fan_select_offset, val) < 0)
8103 * Call with fan_mutex held
8105 static void fan_update_desired_level(u8 status)
8108 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8110 fan_control_desired_level = 7;
8112 fan_control_desired_level = status;
8116 static int fan_get_status(u8 *status)
8121 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8123 switch (fan_status_access_mode) {
8124 case TPACPI_FAN_RD_ACPI_GFAN: {
8125 /* 570, 600e/x, 770e, 770x */
8128 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8132 *status = res & 0x07;
8136 case TPACPI_FAN_RD_TPEC:
8137 /* all except 570, 600e/x, 770e, 770x */
8138 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8141 if (likely(status)) {
8143 fan_quirk1_handle(status);
8155 static int fan_get_status_safe(u8 *status)
8160 if (mutex_lock_killable(&fan_mutex))
8161 return -ERESTARTSYS;
8162 rc = fan_get_status(&s);
8164 fan_update_desired_level(s);
8165 mutex_unlock(&fan_mutex);
8175 static int fan_get_speed(unsigned int *speed)
8179 switch (fan_status_access_mode) {
8180 case TPACPI_FAN_RD_TPEC:
8181 /* all except 570, 600e/x, 770e, 770x */
8182 if (unlikely(!fan_select_fan1()))
8184 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8185 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8189 *speed = (hi << 8) | lo;
8200 static int fan2_get_speed(unsigned int *speed)
8205 switch (fan_status_access_mode) {
8206 case TPACPI_FAN_RD_TPEC:
8207 /* all except 570, 600e/x, 770e, 770x */
8208 if (unlikely(!fan_select_fan2()))
8210 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8211 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8212 fan_select_fan1(); /* play it safe */
8217 *speed = (hi << 8) | lo;
8228 static int fan_set_level(int level)
8230 if (!fan_control_allowed)
8233 switch (fan_control_access_mode) {
8234 case TPACPI_FAN_WR_ACPI_SFAN:
8235 if (level >= 0 && level <= 7) {
8236 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8242 case TPACPI_FAN_WR_ACPI_FANS:
8243 case TPACPI_FAN_WR_TPEC:
8244 if (!(level & TP_EC_FAN_AUTO) &&
8245 !(level & TP_EC_FAN_FULLSPEED) &&
8246 ((level < 0) || (level > 7)))
8249 /* safety net should the EC not support AUTO
8250 * or FULLSPEED mode bits and just ignore them */
8251 if (level & TP_EC_FAN_FULLSPEED)
8252 level |= 7; /* safety min speed 7 */
8253 else if (level & TP_EC_FAN_AUTO)
8254 level |= 4; /* safety min speed 4 */
8256 if (!acpi_ec_write(fan_status_offset, level))
8259 tp_features.fan_ctrl_status_undef = 0;
8266 vdbg_printk(TPACPI_DBG_FAN,
8267 "fan control: set fan control register to 0x%02x\n", level);
8271 static int fan_set_level_safe(int level)
8275 if (!fan_control_allowed)
8278 if (mutex_lock_killable(&fan_mutex))
8279 return -ERESTARTSYS;
8281 if (level == TPACPI_FAN_LAST_LEVEL)
8282 level = fan_control_desired_level;
8284 rc = fan_set_level(level);
8286 fan_update_desired_level(level);
8288 mutex_unlock(&fan_mutex);
8292 static int fan_set_enable(void)
8297 if (!fan_control_allowed)
8300 if (mutex_lock_killable(&fan_mutex))
8301 return -ERESTARTSYS;
8303 switch (fan_control_access_mode) {
8304 case TPACPI_FAN_WR_ACPI_FANS:
8305 case TPACPI_FAN_WR_TPEC:
8306 rc = fan_get_status(&s);
8310 /* Don't go out of emergency fan mode */
8313 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8316 if (!acpi_ec_write(fan_status_offset, s))
8319 tp_features.fan_ctrl_status_undef = 0;
8324 case TPACPI_FAN_WR_ACPI_SFAN:
8325 rc = fan_get_status(&s);
8331 /* Set fan to at least level 4 */
8334 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8344 mutex_unlock(&fan_mutex);
8347 vdbg_printk(TPACPI_DBG_FAN,
8348 "fan control: set fan control register to 0x%02x\n",
8353 static int fan_set_disable(void)
8357 if (!fan_control_allowed)
8360 if (mutex_lock_killable(&fan_mutex))
8361 return -ERESTARTSYS;
8364 switch (fan_control_access_mode) {
8365 case TPACPI_FAN_WR_ACPI_FANS:
8366 case TPACPI_FAN_WR_TPEC:
8367 if (!acpi_ec_write(fan_status_offset, 0x00))
8370 fan_control_desired_level = 0;
8371 tp_features.fan_ctrl_status_undef = 0;
8375 case TPACPI_FAN_WR_ACPI_SFAN:
8376 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8379 fan_control_desired_level = 0;
8387 vdbg_printk(TPACPI_DBG_FAN,
8388 "fan control: set fan control register to 0\n");
8390 mutex_unlock(&fan_mutex);
8394 static int fan_set_speed(int speed)
8398 if (!fan_control_allowed)
8401 if (mutex_lock_killable(&fan_mutex))
8402 return -ERESTARTSYS;
8405 switch (fan_control_access_mode) {
8406 case TPACPI_FAN_WR_ACPI_FANS:
8407 if (speed >= 0 && speed <= 65535) {
8408 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8409 speed, speed, speed))
8419 mutex_unlock(&fan_mutex);
8423 static void fan_watchdog_reset(void)
8425 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8428 if (fan_watchdog_maxinterval > 0 &&
8429 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8430 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8431 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8433 cancel_delayed_work(&fan_watchdog_task);
8436 static void fan_watchdog_fire(struct work_struct *ignored)
8440 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8443 pr_notice("fan watchdog: enabling fan\n");
8444 rc = fan_set_enable();
8446 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8448 /* reschedule for later */
8449 fan_watchdog_reset();
8454 * SYSFS fan layout: hwmon compatible (device)
8457 * 0: "disengaged" mode
8459 * 2: native EC "auto" mode (recommended, hardware default)
8461 * pwm*: set speed in manual mode, ignored otherwise.
8462 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8465 * fan*_input: tachometer reading, RPM
8468 * SYSFS fan layout: extensions
8470 * fan_watchdog (driver):
8471 * fan watchdog interval in seconds, 0 disables (default), max 120
8474 /* sysfs fan pwm1_enable ----------------------------------------------- */
8475 static ssize_t fan_pwm1_enable_show(struct device *dev,
8476 struct device_attribute *attr,
8482 res = fan_get_status_safe(&status);
8486 if (status & TP_EC_FAN_FULLSPEED) {
8488 } else if (status & TP_EC_FAN_AUTO) {
8493 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8496 static ssize_t fan_pwm1_enable_store(struct device *dev,
8497 struct device_attribute *attr,
8498 const char *buf, size_t count)
8503 if (parse_strtoul(buf, 2, &t))
8506 tpacpi_disclose_usertask("hwmon pwm1_enable",
8507 "set fan mode to %lu\n", t);
8511 level = TP_EC_FAN_FULLSPEED;
8514 level = TPACPI_FAN_LAST_LEVEL;
8517 level = TP_EC_FAN_AUTO;
8520 /* reserved for software-controlled auto mode */
8526 res = fan_set_level_safe(level);
8532 fan_watchdog_reset();
8537 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8538 fan_pwm1_enable_show, fan_pwm1_enable_store);
8540 /* sysfs fan pwm1 ------------------------------------------------------ */
8541 static ssize_t fan_pwm1_show(struct device *dev,
8542 struct device_attribute *attr,
8548 res = fan_get_status_safe(&status);
8553 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8554 status = fan_control_desired_level;
8559 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8562 static ssize_t fan_pwm1_store(struct device *dev,
8563 struct device_attribute *attr,
8564 const char *buf, size_t count)
8568 u8 status, newlevel;
8570 if (parse_strtoul(buf, 255, &s))
8573 tpacpi_disclose_usertask("hwmon pwm1",
8574 "set fan speed to %lu\n", s);
8576 /* scale down from 0-255 to 0-7 */
8577 newlevel = (s >> 5) & 0x07;
8579 if (mutex_lock_killable(&fan_mutex))
8580 return -ERESTARTSYS;
8582 rc = fan_get_status(&status);
8583 if (!rc && (status &
8584 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8585 rc = fan_set_level(newlevel);
8589 fan_update_desired_level(newlevel);
8590 fan_watchdog_reset();
8594 mutex_unlock(&fan_mutex);
8595 return (rc) ? rc : count;
8598 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8600 /* sysfs fan fan1_input ------------------------------------------------ */
8601 static ssize_t fan_fan1_input_show(struct device *dev,
8602 struct device_attribute *attr,
8608 res = fan_get_speed(&speed);
8612 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8615 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8617 /* sysfs fan fan2_input ------------------------------------------------ */
8618 static ssize_t fan_fan2_input_show(struct device *dev,
8619 struct device_attribute *attr,
8625 res = fan2_get_speed(&speed);
8629 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8632 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8634 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8635 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8637 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8640 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8645 if (parse_strtoul(buf, 120, &t))
8648 if (!fan_control_allowed)
8651 fan_watchdog_maxinterval = t;
8652 fan_watchdog_reset();
8654 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8658 static DRIVER_ATTR_RW(fan_watchdog);
8660 /* --------------------------------------------------------------------- */
8661 static struct attribute *fan_attributes[] = {
8662 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8663 &dev_attr_fan1_input.attr,
8664 NULL, /* for fan2_input */
8668 static const struct attribute_group fan_attr_group = {
8669 .attrs = fan_attributes,
8672 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8673 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8675 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8676 { .vendor = PCI_VENDOR_ID_IBM, \
8677 .bios = TPACPI_MATCH_ANY, \
8678 .ec = TPID(__id1, __id2), \
8679 .quirks = __quirks }
8681 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8682 { .vendor = PCI_VENDOR_ID_LENOVO, \
8683 .bios = TPACPI_MATCH_ANY, \
8684 .ec = TPID(__id1, __id2), \
8685 .quirks = __quirks }
8687 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8688 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8689 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8690 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8691 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8692 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8695 #undef TPACPI_FAN_QL
8696 #undef TPACPI_FAN_QI
8698 static int __init fan_init(struct ibm_init_struct *iibm)
8701 unsigned long quirks;
8703 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8704 "initializing fan subdriver\n");
8706 mutex_init(&fan_mutex);
8707 fan_status_access_mode = TPACPI_FAN_NONE;
8708 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8709 fan_control_commands = 0;
8710 fan_watchdog_maxinterval = 0;
8711 tp_features.fan_ctrl_status_undef = 0;
8712 tp_features.second_fan = 0;
8713 fan_control_desired_level = 7;
8715 if (tpacpi_is_ibm()) {
8716 TPACPI_ACPIHANDLE_INIT(fans);
8717 TPACPI_ACPIHANDLE_INIT(gfan);
8718 TPACPI_ACPIHANDLE_INIT(sfan);
8721 quirks = tpacpi_check_quirks(fan_quirk_table,
8722 ARRAY_SIZE(fan_quirk_table));
8725 /* 570, 600e/x, 770e, 770x */
8726 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8728 /* all other ThinkPads: note that even old-style
8729 * ThinkPad ECs supports the fan control register */
8730 if (likely(acpi_ec_read(fan_status_offset,
8731 &fan_control_initial_status))) {
8732 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8733 if (quirks & TPACPI_FAN_Q1)
8735 if (quirks & TPACPI_FAN_2FAN) {
8736 tp_features.second_fan = 1;
8737 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8738 "secondary fan support enabled\n");
8741 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8748 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8749 fan_control_commands |=
8750 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8753 /* gfan without sfan means no fan control */
8754 /* all other models implement TP EC 0x2f control */
8758 fan_control_access_mode =
8759 TPACPI_FAN_WR_ACPI_FANS;
8760 fan_control_commands |=
8761 TPACPI_FAN_CMD_SPEED |
8762 TPACPI_FAN_CMD_LEVEL |
8763 TPACPI_FAN_CMD_ENABLE;
8765 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8766 fan_control_commands |=
8767 TPACPI_FAN_CMD_LEVEL |
8768 TPACPI_FAN_CMD_ENABLE;
8773 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8774 "fan is %s, modes %d, %d\n",
8775 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8776 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8777 fan_status_access_mode, fan_control_access_mode);
8779 /* fan control master switch */
8780 if (!fan_control_allowed) {
8781 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8782 fan_control_commands = 0;
8783 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8784 "fan control features disabled by parameter\n");
8787 /* update fan_control_desired_level */
8788 if (fan_status_access_mode != TPACPI_FAN_NONE)
8789 fan_get_status_safe(NULL);
8791 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8792 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8793 if (tp_features.second_fan) {
8794 /* attach second fan tachometer */
8795 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8796 &dev_attr_fan2_input.attr;
8798 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8803 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8804 &driver_attr_fan_watchdog);
8806 sysfs_remove_group(&tpacpi_hwmon->kobj,
8815 static void fan_exit(void)
8817 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8818 "cancelling any pending fan watchdog tasks\n");
8820 /* FIXME: can we really do this unconditionally? */
8821 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8822 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8823 &driver_attr_fan_watchdog);
8825 cancel_delayed_work(&fan_watchdog_task);
8826 flush_workqueue(tpacpi_wq);
8829 static void fan_suspend(void)
8833 if (!fan_control_allowed)
8836 /* Store fan status in cache */
8837 fan_control_resume_level = 0;
8838 rc = fan_get_status_safe(&fan_control_resume_level);
8840 pr_notice("failed to read fan level for later restore during resume: %d\n",
8843 /* if it is undefined, don't attempt to restore it.
8845 if (tp_features.fan_ctrl_status_undef)
8846 fan_control_resume_level = 0;
8849 static void fan_resume(void)
8851 u8 current_level = 7;
8852 bool do_set = false;
8855 /* DSDT *always* updates status on resume */
8856 tp_features.fan_ctrl_status_undef = 0;
8858 if (!fan_control_allowed ||
8859 !fan_control_resume_level ||
8860 (fan_get_status_safe(¤t_level) < 0))
8863 switch (fan_control_access_mode) {
8864 case TPACPI_FAN_WR_ACPI_SFAN:
8865 /* never decrease fan level */
8866 do_set = (fan_control_resume_level > current_level);
8868 case TPACPI_FAN_WR_ACPI_FANS:
8869 case TPACPI_FAN_WR_TPEC:
8870 /* never decrease fan level, scale is:
8871 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8873 * We expect the firmware to set either 7 or AUTO, but we
8874 * handle FULLSPEED out of paranoia.
8876 * So, we can safely only restore FULLSPEED or 7, anything
8877 * else could slow the fan. Restoring AUTO is useless, at
8878 * best that's exactly what the DSDT already set (it is the
8881 * Always keep in mind that the DSDT *will* have set the
8882 * fans to what the vendor supposes is the best level. We
8883 * muck with it only to speed the fan up.
8885 if (fan_control_resume_level != 7 &&
8886 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8889 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8890 (current_level != fan_control_resume_level);
8896 pr_notice("restoring fan level to 0x%02x\n",
8897 fan_control_resume_level);
8898 rc = fan_set_level_safe(fan_control_resume_level);
8900 pr_notice("failed to restore fan level: %d\n", rc);
8904 static int fan_read(struct seq_file *m)
8908 unsigned int speed = 0;
8910 switch (fan_status_access_mode) {
8911 case TPACPI_FAN_RD_ACPI_GFAN:
8912 /* 570, 600e/x, 770e, 770x */
8913 rc = fan_get_status_safe(&status);
8917 seq_printf(m, "status:\t\t%s\n"
8919 (status != 0) ? "enabled" : "disabled", status);
8922 case TPACPI_FAN_RD_TPEC:
8923 /* all except 570, 600e/x, 770e, 770x */
8924 rc = fan_get_status_safe(&status);
8928 seq_printf(m, "status:\t\t%s\n",
8929 (status != 0) ? "enabled" : "disabled");
8931 rc = fan_get_speed(&speed);
8935 seq_printf(m, "speed:\t\t%d\n", speed);
8937 if (status & TP_EC_FAN_FULLSPEED)
8938 /* Disengaged mode takes precedence */
8939 seq_printf(m, "level:\t\tdisengaged\n");
8940 else if (status & TP_EC_FAN_AUTO)
8941 seq_printf(m, "level:\t\tauto\n");
8943 seq_printf(m, "level:\t\t%d\n", status);
8946 case TPACPI_FAN_NONE:
8948 seq_printf(m, "status:\t\tnot supported\n");
8951 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8952 seq_printf(m, "commands:\tlevel <level>");
8954 switch (fan_control_access_mode) {
8955 case TPACPI_FAN_WR_ACPI_SFAN:
8956 seq_printf(m, " (<level> is 0-7)\n");
8960 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8965 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8966 seq_printf(m, "commands:\tenable, disable\n"
8967 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8969 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8970 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8975 static int fan_write_cmd_level(const char *cmd, int *rc)
8979 if (strlencmp(cmd, "level auto") == 0)
8980 level = TP_EC_FAN_AUTO;
8981 else if ((strlencmp(cmd, "level disengaged") == 0) |
8982 (strlencmp(cmd, "level full-speed") == 0))
8983 level = TP_EC_FAN_FULLSPEED;
8984 else if (sscanf(cmd, "level %d", &level) != 1)
8987 *rc = fan_set_level_safe(level);
8989 pr_err("level command accepted for unsupported access mode %d\n",
8990 fan_control_access_mode);
8992 tpacpi_disclose_usertask("procfs fan",
8993 "set level to %d\n", level);
8998 static int fan_write_cmd_enable(const char *cmd, int *rc)
9000 if (strlencmp(cmd, "enable") != 0)
9003 *rc = fan_set_enable();
9005 pr_err("enable command accepted for unsupported access mode %d\n",
9006 fan_control_access_mode);
9008 tpacpi_disclose_usertask("procfs fan", "enable\n");
9013 static int fan_write_cmd_disable(const char *cmd, int *rc)
9015 if (strlencmp(cmd, "disable") != 0)
9018 *rc = fan_set_disable();
9020 pr_err("disable command accepted for unsupported access mode %d\n",
9021 fan_control_access_mode);
9023 tpacpi_disclose_usertask("procfs fan", "disable\n");
9028 static int fan_write_cmd_speed(const char *cmd, int *rc)
9033 * Support speed <low> <medium> <high> ? */
9035 if (sscanf(cmd, "speed %d", &speed) != 1)
9038 *rc = fan_set_speed(speed);
9040 pr_err("speed command accepted for unsupported access mode %d\n",
9041 fan_control_access_mode);
9043 tpacpi_disclose_usertask("procfs fan",
9044 "set speed to %d\n", speed);
9049 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9053 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9056 if (interval < 0 || interval > 120)
9059 fan_watchdog_maxinterval = interval;
9060 tpacpi_disclose_usertask("procfs fan",
9061 "set watchdog timer to %d\n",
9068 static int fan_write(char *buf)
9073 while (!rc && (cmd = next_cmd(&buf))) {
9074 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9075 fan_write_cmd_level(cmd, &rc)) &&
9076 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9077 (fan_write_cmd_enable(cmd, &rc) ||
9078 fan_write_cmd_disable(cmd, &rc) ||
9079 fan_write_cmd_watchdog(cmd, &rc))) &&
9080 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9081 fan_write_cmd_speed(cmd, &rc))
9085 fan_watchdog_reset();
9091 static struct ibm_struct fan_driver_data = {
9096 .suspend = fan_suspend,
9097 .resume = fan_resume,
9100 /*************************************************************************
9101 * Mute LED subdriver
9105 struct tp_led_table {
9112 static struct tp_led_table led_tables[] = {
9113 [TPACPI_LED_MUTE] = {
9118 [TPACPI_LED_MICMUTE] = {
9125 static int mute_led_on_off(struct tp_led_table *t, bool state)
9130 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9131 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9135 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9136 state ? t->on_value : t->off_value))
9143 int tpacpi_led_set(int whichled, bool on)
9145 struct tp_led_table *t;
9147 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9150 t = &led_tables[whichled];
9151 if (t->state < 0 || t->state == on)
9153 return mute_led_on_off(t, on);
9155 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9157 static int mute_led_init(struct ibm_init_struct *iibm)
9162 for (i = 0; i < TPACPI_LED_MAX; i++) {
9163 struct tp_led_table *t = &led_tables[i];
9164 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9165 mute_led_on_off(t, false);
9172 static void mute_led_exit(void)
9176 for (i = 0; i < TPACPI_LED_MAX; i++)
9177 tpacpi_led_set(i, false);
9180 static void mute_led_resume(void)
9184 for (i = 0; i < TPACPI_LED_MAX; i++) {
9185 struct tp_led_table *t = &led_tables[i];
9187 mute_led_on_off(t, t->state);
9191 static struct ibm_struct mute_led_driver_data = {
9193 .exit = mute_led_exit,
9194 .resume = mute_led_resume,
9197 /****************************************************************************
9198 ****************************************************************************
9202 ****************************************************************************
9203 ****************************************************************************/
9206 * HKEY event callout for other subdrivers go here
9207 * (yes, it is ugly, but it is quick, safe, and gets the job done
9209 static void tpacpi_driver_event(const unsigned int hkey_event)
9211 if (ibm_backlight_device) {
9212 switch (hkey_event) {
9213 case TP_HKEY_EV_BRGHT_UP:
9214 case TP_HKEY_EV_BRGHT_DOWN:
9215 tpacpi_brightness_notify_change();
9219 switch (hkey_event) {
9220 case TP_HKEY_EV_VOL_UP:
9221 case TP_HKEY_EV_VOL_DOWN:
9222 case TP_HKEY_EV_VOL_MUTE:
9223 volume_alsa_notify_change();
9226 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9227 enum led_brightness brightness;
9229 mutex_lock(&kbdlight_mutex);
9232 * Check the brightness actually changed, setting the brightness
9233 * through kbdlight_set_level() also triggers this event.
9235 brightness = kbdlight_sysfs_get(NULL);
9236 if (kbdlight_brightness != brightness) {
9237 kbdlight_brightness = brightness;
9238 led_classdev_notify_brightness_hw_changed(
9239 &tpacpi_led_kbdlight.led_classdev, brightness);
9242 mutex_unlock(&kbdlight_mutex);
9246 static void hotkey_driver_event(const unsigned int scancode)
9248 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9251 /* --------------------------------------------------------------------- */
9254 static struct proc_dir_entry *proc_dir;
9257 * Module and infrastructure proble, init and exit handling
9260 static bool force_load;
9262 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9263 static const char * __init str_supported(int is_supported)
9265 static char text_unsupported[] __initdata = "not supported";
9267 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9269 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9271 static void ibm_exit(struct ibm_struct *ibm)
9273 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9275 list_del_init(&ibm->all_drivers);
9277 if (ibm->flags.acpi_notify_installed) {
9278 dbg_printk(TPACPI_DBG_EXIT,
9279 "%s: acpi_remove_notify_handler\n", ibm->name);
9281 acpi_remove_notify_handler(*ibm->acpi->handle,
9283 dispatch_acpi_notify);
9284 ibm->flags.acpi_notify_installed = 0;
9287 if (ibm->flags.proc_created) {
9288 dbg_printk(TPACPI_DBG_EXIT,
9289 "%s: remove_proc_entry\n", ibm->name);
9290 remove_proc_entry(ibm->name, proc_dir);
9291 ibm->flags.proc_created = 0;
9294 if (ibm->flags.acpi_driver_registered) {
9295 dbg_printk(TPACPI_DBG_EXIT,
9296 "%s: acpi_bus_unregister_driver\n", ibm->name);
9298 acpi_bus_unregister_driver(ibm->acpi->driver);
9299 kfree(ibm->acpi->driver);
9300 ibm->acpi->driver = NULL;
9301 ibm->flags.acpi_driver_registered = 0;
9304 if (ibm->flags.init_called && ibm->exit) {
9306 ibm->flags.init_called = 0;
9309 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9312 static int __init ibm_init(struct ibm_init_struct *iibm)
9315 struct ibm_struct *ibm = iibm->data;
9316 struct proc_dir_entry *entry;
9318 BUG_ON(ibm == NULL);
9320 INIT_LIST_HEAD(&ibm->all_drivers);
9322 if (ibm->flags.experimental && !experimental)
9325 dbg_printk(TPACPI_DBG_INIT,
9326 "probing for %s\n", ibm->name);
9329 ret = iibm->init(iibm);
9331 return 0; /* probe failed */
9335 ibm->flags.init_called = 1;
9339 if (ibm->acpi->hid) {
9340 ret = register_tpacpi_subdriver(ibm);
9345 if (ibm->acpi->notify) {
9346 ret = setup_acpi_notify(ibm);
9347 if (ret == -ENODEV) {
9348 pr_notice("disabling subdriver %s\n",
9358 dbg_printk(TPACPI_DBG_INIT,
9359 "%s installed\n", ibm->name);
9362 umode_t mode = iibm->base_procfs_mode;
9368 entry = proc_create_data(ibm->name, mode, proc_dir,
9369 &dispatch_proc_fops, ibm);
9371 pr_err("unable to create proc entry %s\n", ibm->name);
9375 ibm->flags.proc_created = 1;
9378 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9383 dbg_printk(TPACPI_DBG_INIT,
9384 "%s: at error exit path with result %d\n",
9388 return (ret < 0) ? ret : 0;
9393 static bool __pure __init tpacpi_is_fw_digit(const char c)
9395 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9398 static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
9402 * Most models: xxyTkkWW (#.##c)
9403 * Ancient 570/600 and -SL lacks (#.##c)
9405 if (s && strlen(s) >= 8 &&
9406 tpacpi_is_fw_digit(s[0]) &&
9407 tpacpi_is_fw_digit(s[1]) &&
9409 (s[3] == 'T' || s[3] == 'N') &&
9410 tpacpi_is_fw_digit(s[4]) &&
9411 tpacpi_is_fw_digit(s[5]))
9414 /* New models: xxxyTkkW (#.##c); T550 and some others */
9415 return s && strlen(s) >= 8 &&
9416 tpacpi_is_fw_digit(s[0]) &&
9417 tpacpi_is_fw_digit(s[1]) &&
9418 tpacpi_is_fw_digit(s[2]) &&
9420 (s[4] == 'T' || s[4] == 'N') &&
9421 tpacpi_is_fw_digit(s[5]) &&
9422 tpacpi_is_fw_digit(s[6]);
9425 /* returns 0 - probe ok, or < 0 - probe error.
9426 * Probe ok doesn't mean thinkpad found.
9427 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9428 static int __must_check __init get_thinkpad_model_data(
9429 struct thinkpad_id_data *tp)
9431 const struct dmi_device *dev = NULL;
9432 char ec_fw_string[18];
9438 memset(tp, 0, sizeof(*tp));
9440 if (dmi_name_in_vendors("IBM"))
9441 tp->vendor = PCI_VENDOR_ID_IBM;
9442 else if (dmi_name_in_vendors("LENOVO"))
9443 tp->vendor = PCI_VENDOR_ID_LENOVO;
9447 s = dmi_get_system_info(DMI_BIOS_VERSION);
9448 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9449 if (s && !tp->bios_version_str)
9452 /* Really ancient ThinkPad 240X will fail this, which is fine */
9453 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9454 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
9457 tp->bios_model = tp->bios_version_str[0]
9458 | (tp->bios_version_str[1] << 8);
9459 tp->bios_release = (tp->bios_version_str[4] << 8)
9460 | tp->bios_version_str[5];
9463 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9464 * X32 or newer, all Z series; Some models must have an
9465 * up-to-date BIOS or they will not be detected.
9467 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9469 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9470 if (sscanf(dev->name,
9471 "IBM ThinkPad Embedded Controller -[%17c",
9472 ec_fw_string) == 1) {
9473 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9474 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
9476 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
9477 if (!tp->ec_version_str)
9480 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9481 tp->ec_model = ec_fw_string[0]
9482 | (ec_fw_string[1] << 8);
9483 tp->ec_release = (ec_fw_string[4] << 8)
9486 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
9488 pr_notice("please report this to %s\n",
9495 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
9496 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
9497 tp->model_str = kstrdup(s, GFP_KERNEL);
9501 s = dmi_get_system_info(DMI_BIOS_VENDOR);
9502 if (s && !(strncasecmp(s, "Lenovo", 6))) {
9503 tp->model_str = kstrdup(s, GFP_KERNEL);
9509 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9510 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9511 if (s && !tp->nummodel_str)
9517 static int __init probe_for_thinkpad(void)
9524 /* It would be dangerous to run the driver in this case */
9525 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9529 * Non-ancient models have better DMI tagging, but very old models
9530 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9532 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9533 (thinkpad_id.ec_model != 0) ||
9534 tpacpi_is_fw_known();
9536 /* The EC handler is required */
9537 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
9540 pr_err("Not yet supported ThinkPad detected!\n");
9544 if (!is_thinkpad && !force_load)
9550 static void __init thinkpad_acpi_init_banner(void)
9552 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9553 pr_info("%s\n", TPACPI_URL);
9555 pr_info("ThinkPad BIOS %s, EC %s\n",
9556 (thinkpad_id.bios_version_str) ?
9557 thinkpad_id.bios_version_str : "unknown",
9558 (thinkpad_id.ec_version_str) ?
9559 thinkpad_id.ec_version_str : "unknown");
9561 BUG_ON(!thinkpad_id.vendor);
9563 if (thinkpad_id.model_str)
9564 pr_info("%s %s, model %s\n",
9565 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9566 "IBM" : ((thinkpad_id.vendor ==
9567 PCI_VENDOR_ID_LENOVO) ?
9568 "Lenovo" : "Unknown vendor"),
9569 thinkpad_id.model_str,
9570 (thinkpad_id.nummodel_str) ?
9571 thinkpad_id.nummodel_str : "unknown");
9574 /* Module init, exit, parameters */
9576 static struct ibm_init_struct ibms_init[] __initdata = {
9578 .data = &thinkpad_acpi_driver_data,
9581 .init = hotkey_init,
9582 .data = &hotkey_driver_data,
9585 .init = bluetooth_init,
9586 .data = &bluetooth_driver_data,
9590 .data = &wan_driver_data,
9594 .data = &uwb_driver_data,
9596 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9599 .base_procfs_mode = S_IRUSR,
9600 .data = &video_driver_data,
9604 .init = kbdlight_init,
9605 .data = &kbdlight_driver_data,
9609 .data = &light_driver_data,
9613 .data = &cmos_driver_data,
9617 .data = &led_driver_data,
9621 .data = &beep_driver_data,
9624 .init = thermal_init,
9625 .data = &thermal_driver_data,
9628 .init = brightness_init,
9629 .data = &brightness_driver_data,
9632 .init = volume_init,
9633 .data = &volume_driver_data,
9637 .data = &fan_driver_data,
9640 .init = mute_led_init,
9641 .data = &mute_led_driver_data,
9645 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9648 struct ibm_struct *ibm;
9650 if (!kp || !kp->name || !val)
9653 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9654 ibm = ibms_init[i].data;
9655 WARN_ON(ibm == NULL);
9657 if (!ibm || !ibm->name)
9660 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9661 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
9663 strcpy(ibms_init[i].param, val);
9664 strcat(ibms_init[i].param, ",");
9672 module_param(experimental, int, 0444);
9673 MODULE_PARM_DESC(experimental,
9674 "Enables experimental features when non-zero");
9676 module_param_named(debug, dbg_level, uint, 0);
9677 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
9679 module_param(force_load, bool, 0444);
9680 MODULE_PARM_DESC(force_load,
9681 "Attempts to load the driver even on a mis-identified ThinkPad when true");
9683 module_param_named(fan_control, fan_control_allowed, bool, 0444);
9684 MODULE_PARM_DESC(fan_control,
9685 "Enables setting fan parameters features when true");
9687 module_param_named(brightness_mode, brightness_mode, uint, 0444);
9688 MODULE_PARM_DESC(brightness_mode,
9689 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9691 module_param(brightness_enable, uint, 0444);
9692 MODULE_PARM_DESC(brightness_enable,
9693 "Enables backlight control when 1, disables when 0");
9695 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9696 module_param_named(volume_mode, volume_mode, uint, 0444);
9697 MODULE_PARM_DESC(volume_mode,
9698 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9700 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9701 MODULE_PARM_DESC(volume_capabilities,
9702 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
9704 module_param_named(volume_control, volume_control_allowed, bool, 0444);
9705 MODULE_PARM_DESC(volume_control,
9706 "Enables software override for the console audio control when true");
9708 module_param_named(software_mute, software_mute_requested, bool, 0444);
9709 MODULE_PARM_DESC(software_mute,
9710 "Request full software mute control");
9712 /* ALSA module API parameters */
9713 module_param_named(index, alsa_index, int, 0444);
9714 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9715 module_param_named(id, alsa_id, charp, 0444);
9716 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9717 module_param_named(enable, alsa_enable, bool, 0444);
9718 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
9719 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9721 /* The module parameter can't be read back, that's why 0 is used here */
9722 #define TPACPI_PARAM(feature) \
9723 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9724 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
9726 TPACPI_PARAM(hotkey);
9727 TPACPI_PARAM(bluetooth);
9728 TPACPI_PARAM(video);
9729 TPACPI_PARAM(light);
9733 TPACPI_PARAM(brightness);
9734 TPACPI_PARAM(volume);
9737 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9738 module_param(dbg_wlswemul, uint, 0444);
9739 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9740 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9741 MODULE_PARM_DESC(wlsw_state,
9742 "Initial state of the emulated WLSW switch");
9744 module_param(dbg_bluetoothemul, uint, 0444);
9745 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9746 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9747 MODULE_PARM_DESC(bluetooth_state,
9748 "Initial state of the emulated bluetooth switch");
9750 module_param(dbg_wwanemul, uint, 0444);
9751 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9752 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9753 MODULE_PARM_DESC(wwan_state,
9754 "Initial state of the emulated WWAN switch");
9756 module_param(dbg_uwbemul, uint, 0444);
9757 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9758 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9759 MODULE_PARM_DESC(uwb_state,
9760 "Initial state of the emulated UWB switch");
9763 static void thinkpad_acpi_module_exit(void)
9765 struct ibm_struct *ibm, *itmp;
9767 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9769 list_for_each_entry_safe_reverse(ibm, itmp,
9770 &tpacpi_all_drivers,
9775 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9777 if (tpacpi_inputdev) {
9778 if (tp_features.input_device_registered)
9779 input_unregister_device(tpacpi_inputdev);
9781 input_free_device(tpacpi_inputdev);
9782 kfree(hotkey_keycode_map);
9786 hwmon_device_unregister(tpacpi_hwmon);
9788 if (tpacpi_sensors_pdev)
9789 platform_device_unregister(tpacpi_sensors_pdev);
9791 platform_device_unregister(tpacpi_pdev);
9793 if (tp_features.sensors_pdrv_attrs_registered)
9794 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9795 if (tp_features.platform_drv_attrs_registered)
9796 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9798 if (tp_features.sensors_pdrv_registered)
9799 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9801 if (tp_features.platform_drv_registered)
9802 platform_driver_unregister(&tpacpi_pdriver);
9805 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9808 destroy_workqueue(tpacpi_wq);
9810 kfree(thinkpad_id.bios_version_str);
9811 kfree(thinkpad_id.ec_version_str);
9812 kfree(thinkpad_id.model_str);
9813 kfree(thinkpad_id.nummodel_str);
9817 static int __init thinkpad_acpi_module_init(void)
9821 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9823 /* Driver-level probe */
9825 ret = get_thinkpad_model_data(&thinkpad_id);
9827 pr_err("unable to get DMI data: %d\n", ret);
9828 thinkpad_acpi_module_exit();
9831 ret = probe_for_thinkpad();
9833 thinkpad_acpi_module_exit();
9837 /* Driver initialization */
9839 thinkpad_acpi_init_banner();
9840 tpacpi_check_outdated_fw();
9842 TPACPI_ACPIHANDLE_INIT(ecrd);
9843 TPACPI_ACPIHANDLE_INIT(ecwr);
9845 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9847 thinkpad_acpi_module_exit();
9851 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9853 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9854 thinkpad_acpi_module_exit();
9858 ret = platform_driver_register(&tpacpi_pdriver);
9860 pr_err("unable to register main platform driver\n");
9861 thinkpad_acpi_module_exit();
9864 tp_features.platform_drv_registered = 1;
9866 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9868 pr_err("unable to register hwmon platform driver\n");
9869 thinkpad_acpi_module_exit();
9872 tp_features.sensors_pdrv_registered = 1;
9874 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9876 tp_features.platform_drv_attrs_registered = 1;
9877 ret = tpacpi_create_driver_attributes(
9878 &tpacpi_hwmon_pdriver.driver);
9881 pr_err("unable to create sysfs driver attributes\n");
9882 thinkpad_acpi_module_exit();
9885 tp_features.sensors_pdrv_attrs_registered = 1;
9888 /* Device initialization */
9889 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9891 if (IS_ERR(tpacpi_pdev)) {
9892 ret = PTR_ERR(tpacpi_pdev);
9894 pr_err("unable to register platform device\n");
9895 thinkpad_acpi_module_exit();
9898 tpacpi_sensors_pdev = platform_device_register_simple(
9899 TPACPI_HWMON_DRVR_NAME,
9901 if (IS_ERR(tpacpi_sensors_pdev)) {
9902 ret = PTR_ERR(tpacpi_sensors_pdev);
9903 tpacpi_sensors_pdev = NULL;
9904 pr_err("unable to register hwmon platform device\n");
9905 thinkpad_acpi_module_exit();
9908 tp_features.sensors_pdev_attrs_registered = 1;
9909 tpacpi_hwmon = hwmon_device_register_with_groups(
9910 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
9912 if (IS_ERR(tpacpi_hwmon)) {
9913 ret = PTR_ERR(tpacpi_hwmon);
9914 tpacpi_hwmon = NULL;
9915 pr_err("unable to register hwmon device\n");
9916 thinkpad_acpi_module_exit();
9919 mutex_init(&tpacpi_inputdev_send_mutex);
9920 tpacpi_inputdev = input_allocate_device();
9921 if (!tpacpi_inputdev) {
9922 thinkpad_acpi_module_exit();
9925 /* Prepare input device, but don't register */
9926 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9927 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9928 tpacpi_inputdev->id.bustype = BUS_HOST;
9929 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9930 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9931 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9932 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9935 /* Init subdriver dependencies */
9936 tpacpi_detect_brightness_capabilities();
9938 /* Init subdrivers */
9939 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9940 ret = ibm_init(&ibms_init[i]);
9941 if (ret >= 0 && *ibms_init[i].param)
9942 ret = ibms_init[i].data->write(ibms_init[i].param);
9944 thinkpad_acpi_module_exit();
9949 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9951 ret = input_register_device(tpacpi_inputdev);
9953 pr_err("unable to register input device\n");
9954 thinkpad_acpi_module_exit();
9957 tp_features.input_device_registered = 1;
9963 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9966 * This will autoload the driver in almost every ThinkPad
9967 * in widespread use.
9969 * Only _VERY_ old models, like the 240, 240x and 570 lack
9970 * the HKEY event interface.
9972 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9975 * DMI matching for module autoloading
9977 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9978 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9980 * Only models listed in thinkwiki will be supported, so add yours
9981 * if it is not there yet.
9983 #define IBM_BIOS_MODULE_ALIAS(__type) \
9984 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9986 /* Ancient thinkpad BIOSes have to be identified by
9987 * BIOS type or model number, and there are far less
9988 * BIOS types than model numbers... */
9989 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9991 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9992 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9993 MODULE_DESCRIPTION(TPACPI_DESC);
9994 MODULE_VERSION(TPACPI_VERSION);
9995 MODULE_LICENSE("GPL");
9997 module_init(thinkpad_acpi_module_init);
9998 module_exit(thinkpad_acpi_module_exit);