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_ids.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];
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 case TP_HKEY_EV_KEY_FN_ESC:
3968 /* key press events, we just ignore them as long as the EC
3969 * is still reporting them in the normal keyboard stream */
3970 *send_acpi_ev = false;
3971 *ignore_acpi_ev = true;
3974 case TP_HKEY_EV_TABLET_CHANGED:
3975 tpacpi_input_send_tabletsw();
3976 hotkey_tablet_mode_notify_change();
3977 *send_acpi_ev = false;
3980 case TP_HKEY_EV_PALM_DETECTED:
3981 case TP_HKEY_EV_PALM_UNDETECTED:
3982 /* palm detected hovering the keyboard, forward to user-space
3983 * via netlink for consumption */
3987 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3991 thermal_dump_all_sensors();
3996 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4000 bool ignore_acpi_ev;
4003 if (event != 0x80) {
4004 pr_err("unknown HKEY notification event %d\n", event);
4005 /* forward it to userspace, maybe it knows how to handle it */
4006 acpi_bus_generate_netlink_event(
4007 ibm->acpi->device->pnp.device_class,
4008 dev_name(&ibm->acpi->device->dev),
4014 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4015 pr_err("failed to retrieve HKEY event\n");
4024 send_acpi_ev = true;
4025 ignore_acpi_ev = false;
4027 switch (hkey >> 12) {
4029 /* 0x1000-0x1FFF: key presses */
4030 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4034 /* 0x2000-0x2FFF: Wakeup reason */
4035 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4039 /* 0x3000-0x3FFF: bay-related wakeups */
4041 case TP_HKEY_EV_BAYEJ_ACK:
4042 hotkey_autosleep_ack = 1;
4043 pr_info("bay ejected\n");
4044 hotkey_wakeup_hotunplug_complete_notify_change();
4047 case TP_HKEY_EV_OPTDRV_EJ:
4048 /* FIXME: kick libata if SATA link offline */
4056 /* 0x4000-0x4FFF: dock-related events */
4057 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4061 /* 0x5000-0x5FFF: human interface helpers */
4062 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4066 /* 0x6000-0x6FFF: thermal alarms/notices and
4067 * keyboard events */
4068 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4072 /* 0x7000-0x7FFF: misc */
4073 if (tp_features.hotkey_wlsw &&
4074 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4075 tpacpi_send_radiosw_update();
4080 /* fallthrough to default */
4085 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4086 pr_notice("please report the conditions when this event happened to %s\n",
4090 /* netlink events */
4091 if (!ignore_acpi_ev && send_acpi_ev) {
4092 acpi_bus_generate_netlink_event(
4093 ibm->acpi->device->pnp.device_class,
4094 dev_name(&ibm->acpi->device->dev),
4100 static void hotkey_suspend(void)
4102 /* Do these on suspend, we get the events on early resume! */
4103 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4104 hotkey_autosleep_ack = 0;
4106 /* save previous mode of adaptive keyboard of X1 Carbon */
4107 if (tp_features.has_adaptive_kbd) {
4108 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4110 pr_err("Cannot read adaptive keyboard mode.\n");
4115 static void hotkey_resume(void)
4117 tpacpi_disable_brightness_delay();
4119 if (hotkey_status_set(true) < 0 ||
4120 hotkey_mask_set(hotkey_acpi_mask) < 0)
4121 pr_err("error while attempting to reset the event firmware interface\n");
4123 tpacpi_send_radiosw_update();
4124 hotkey_tablet_mode_notify_change();
4125 hotkey_wakeup_reason_notify_change();
4126 hotkey_wakeup_hotunplug_complete_notify_change();
4127 hotkey_poll_setup_safe(false);
4129 /* restore previous mode of adapive keyboard of X1 Carbon */
4130 if (tp_features.has_adaptive_kbd) {
4131 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4132 adaptive_keyboard_prev_mode)) {
4133 pr_err("Cannot set adaptive keyboard mode.\n");
4138 /* procfs -------------------------------------------------------------- */
4139 static int hotkey_read(struct seq_file *m)
4143 if (!tp_features.hotkey) {
4144 seq_printf(m, "status:\t\tnot supported\n");
4148 if (mutex_lock_killable(&hotkey_mutex))
4149 return -ERESTARTSYS;
4150 res = hotkey_status_get(&status);
4152 res = hotkey_mask_get();
4153 mutex_unlock(&hotkey_mutex);
4157 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4158 if (hotkey_all_mask) {
4159 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4160 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4162 seq_printf(m, "mask:\t\tnot supported\n");
4163 seq_printf(m, "commands:\tenable, disable, reset\n");
4169 static void hotkey_enabledisable_warn(bool enable)
4171 tpacpi_log_usertask("procfs hotkey enable/disable");
4172 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4173 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4174 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4177 static int hotkey_write(char *buf)
4183 if (!tp_features.hotkey)
4186 if (mutex_lock_killable(&hotkey_mutex))
4187 return -ERESTARTSYS;
4189 mask = hotkey_user_mask;
4192 while ((cmd = next_cmd(&buf))) {
4193 if (strlencmp(cmd, "enable") == 0) {
4194 hotkey_enabledisable_warn(1);
4195 } else if (strlencmp(cmd, "disable") == 0) {
4196 hotkey_enabledisable_warn(0);
4198 } else if (strlencmp(cmd, "reset") == 0) {
4199 mask = (hotkey_all_mask | hotkey_source_mask)
4200 & ~hotkey_reserved_mask;
4201 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4203 } else if (sscanf(cmd, "%x", &mask) == 1) {
4212 tpacpi_disclose_usertask("procfs hotkey",
4213 "set mask to 0x%08x\n", mask);
4214 res = hotkey_user_mask_set(mask);
4218 mutex_unlock(&hotkey_mutex);
4222 static const struct acpi_device_id ibm_htk_device_ids[] = {
4223 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4224 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4225 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4229 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4230 .hid = ibm_htk_device_ids,
4231 .notify = hotkey_notify,
4232 .handle = &hkey_handle,
4233 .type = ACPI_DEVICE_NOTIFY,
4236 static struct ibm_struct hotkey_driver_data = {
4238 .read = hotkey_read,
4239 .write = hotkey_write,
4240 .exit = hotkey_exit,
4241 .resume = hotkey_resume,
4242 .suspend = hotkey_suspend,
4243 .acpi = &ibm_hotkey_acpidriver,
4246 /*************************************************************************
4247 * Bluetooth subdriver
4251 /* ACPI GBDC/SBDC bits */
4252 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4253 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4254 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4255 0 = disable, 1 = enable */
4259 /* ACPI \BLTH commands */
4260 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4261 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4262 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4263 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4264 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4267 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4269 static int bluetooth_get_status(void)
4273 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4274 if (dbg_bluetoothemul)
4275 return (tpacpi_bluetooth_emulstate) ?
4276 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4279 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4282 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4283 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4286 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4290 vdbg_printk(TPACPI_DBG_RFKILL,
4291 "will attempt to %s bluetooth\n",
4292 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4294 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4295 if (dbg_bluetoothemul) {
4296 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4301 if (state == TPACPI_RFK_RADIO_ON)
4302 status = TP_ACPI_BLUETOOTH_RADIOSSW
4303 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4307 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4313 /* sysfs bluetooth enable ---------------------------------------------- */
4314 static ssize_t bluetooth_enable_show(struct device *dev,
4315 struct device_attribute *attr,
4318 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4322 static ssize_t bluetooth_enable_store(struct device *dev,
4323 struct device_attribute *attr,
4324 const char *buf, size_t count)
4326 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4330 static DEVICE_ATTR_RW(bluetooth_enable);
4332 /* --------------------------------------------------------------------- */
4334 static struct attribute *bluetooth_attributes[] = {
4335 &dev_attr_bluetooth_enable.attr,
4339 static const struct attribute_group bluetooth_attr_group = {
4340 .attrs = bluetooth_attributes,
4343 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4344 .get_status = bluetooth_get_status,
4345 .set_status = bluetooth_set_status,
4348 static void bluetooth_shutdown(void)
4350 /* Order firmware to save current state to NVRAM */
4351 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4352 TP_ACPI_BLTH_SAVE_STATE))
4353 pr_notice("failed to save bluetooth state to NVRAM\n");
4355 vdbg_printk(TPACPI_DBG_RFKILL,
4356 "bluetooth state saved to NVRAM\n");
4359 static void bluetooth_exit(void)
4361 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4362 &bluetooth_attr_group);
4364 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4366 bluetooth_shutdown();
4369 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4374 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4375 "initializing bluetooth subdriver\n");
4377 TPACPI_ACPIHANDLE_INIT(hkey);
4379 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4380 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4381 tp_features.bluetooth = hkey_handle &&
4382 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4384 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4385 "bluetooth is %s, status 0x%02x\n",
4386 str_supported(tp_features.bluetooth),
4389 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4390 if (dbg_bluetoothemul) {
4391 tp_features.bluetooth = 1;
4392 pr_info("bluetooth switch emulation enabled\n");
4395 if (tp_features.bluetooth &&
4396 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4397 /* no bluetooth hardware present in system */
4398 tp_features.bluetooth = 0;
4399 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4400 "bluetooth hardware not installed\n");
4403 if (!tp_features.bluetooth)
4406 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4407 &bluetooth_tprfk_ops,
4408 RFKILL_TYPE_BLUETOOTH,
4409 TPACPI_RFK_BLUETOOTH_SW_NAME,
4414 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4415 &bluetooth_attr_group);
4417 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4424 /* procfs -------------------------------------------------------------- */
4425 static int bluetooth_read(struct seq_file *m)
4427 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4430 static int bluetooth_write(char *buf)
4432 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4435 static struct ibm_struct bluetooth_driver_data = {
4436 .name = "bluetooth",
4437 .read = bluetooth_read,
4438 .write = bluetooth_write,
4439 .exit = bluetooth_exit,
4440 .shutdown = bluetooth_shutdown,
4443 /*************************************************************************
4448 /* ACPI GWAN/SWAN bits */
4449 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4450 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4451 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4452 0 = disable, 1 = enable */
4455 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4457 static int wan_get_status(void)
4461 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4463 return (tpacpi_wwan_emulstate) ?
4464 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4467 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4470 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4471 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4474 static int wan_set_status(enum tpacpi_rfkill_state state)
4478 vdbg_printk(TPACPI_DBG_RFKILL,
4479 "will attempt to %s wwan\n",
4480 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4482 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4484 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4489 if (state == TPACPI_RFK_RADIO_ON)
4490 status = TP_ACPI_WANCARD_RADIOSSW
4491 | TP_ACPI_WANCARD_RESUMECTRL;
4495 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4501 /* sysfs wan enable ---------------------------------------------------- */
4502 static ssize_t wan_enable_show(struct device *dev,
4503 struct device_attribute *attr,
4506 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4510 static ssize_t wan_enable_store(struct device *dev,
4511 struct device_attribute *attr,
4512 const char *buf, size_t count)
4514 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4518 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4519 wan_enable_show, wan_enable_store);
4521 /* --------------------------------------------------------------------- */
4523 static struct attribute *wan_attributes[] = {
4524 &dev_attr_wwan_enable.attr,
4528 static const struct attribute_group wan_attr_group = {
4529 .attrs = wan_attributes,
4532 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4533 .get_status = wan_get_status,
4534 .set_status = wan_set_status,
4537 static void wan_shutdown(void)
4539 /* Order firmware to save current state to NVRAM */
4540 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4541 TP_ACPI_WGSV_SAVE_STATE))
4542 pr_notice("failed to save WWAN state to NVRAM\n");
4544 vdbg_printk(TPACPI_DBG_RFKILL,
4545 "WWAN state saved to NVRAM\n");
4548 static void wan_exit(void)
4550 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4553 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4558 static int __init wan_init(struct ibm_init_struct *iibm)
4563 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4564 "initializing wan subdriver\n");
4566 TPACPI_ACPIHANDLE_INIT(hkey);
4568 tp_features.wan = hkey_handle &&
4569 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4571 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4572 "wan is %s, status 0x%02x\n",
4573 str_supported(tp_features.wan),
4576 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4578 tp_features.wan = 1;
4579 pr_info("wwan switch emulation enabled\n");
4582 if (tp_features.wan &&
4583 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4584 /* no wan hardware present in system */
4585 tp_features.wan = 0;
4586 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4587 "wan hardware not installed\n");
4590 if (!tp_features.wan)
4593 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4596 TPACPI_RFK_WWAN_SW_NAME,
4601 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4605 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4612 /* procfs -------------------------------------------------------------- */
4613 static int wan_read(struct seq_file *m)
4615 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4618 static int wan_write(char *buf)
4620 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4623 static struct ibm_struct wan_driver_data = {
4628 .shutdown = wan_shutdown,
4631 /*************************************************************************
4636 /* ACPI GUWB/SUWB bits */
4637 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4638 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4641 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4643 static int uwb_get_status(void)
4647 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4649 return (tpacpi_uwb_emulstate) ?
4650 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4653 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4656 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4657 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4660 static int uwb_set_status(enum tpacpi_rfkill_state state)
4664 vdbg_printk(TPACPI_DBG_RFKILL,
4665 "will attempt to %s UWB\n",
4666 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4668 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4670 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4675 if (state == TPACPI_RFK_RADIO_ON)
4676 status = TP_ACPI_UWB_RADIOSSW;
4680 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4686 /* --------------------------------------------------------------------- */
4688 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4689 .get_status = uwb_get_status,
4690 .set_status = uwb_set_status,
4693 static void uwb_exit(void)
4695 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4698 static int __init uwb_init(struct ibm_init_struct *iibm)
4703 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4704 "initializing uwb subdriver\n");
4706 TPACPI_ACPIHANDLE_INIT(hkey);
4708 tp_features.uwb = hkey_handle &&
4709 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4711 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4712 "uwb is %s, status 0x%02x\n",
4713 str_supported(tp_features.uwb),
4716 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4718 tp_features.uwb = 1;
4719 pr_info("uwb switch emulation enabled\n");
4722 if (tp_features.uwb &&
4723 !(status & TP_ACPI_UWB_HWPRESENT)) {
4724 /* no uwb hardware present in system */
4725 tp_features.uwb = 0;
4726 dbg_printk(TPACPI_DBG_INIT,
4727 "uwb hardware not installed\n");
4730 if (!tp_features.uwb)
4733 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4736 TPACPI_RFK_UWB_SW_NAME,
4741 static struct ibm_struct uwb_driver_data = {
4744 .flags.experimental = 1,
4747 /*************************************************************************
4751 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4753 enum video_access_mode {
4754 TPACPI_VIDEO_NONE = 0,
4755 TPACPI_VIDEO_570, /* 570 */
4756 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4757 TPACPI_VIDEO_NEW, /* all others */
4760 enum { /* video status flags, based on VIDEO_570 */
4761 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4762 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4763 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4766 enum { /* TPACPI_VIDEO_570 constants */
4767 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4768 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4769 * video_status_flags */
4770 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4771 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4774 static enum video_access_mode video_supported;
4775 static int video_orig_autosw;
4777 static int video_autosw_get(void);
4778 static int video_autosw_set(int enable);
4780 TPACPI_HANDLE(vid, root,
4781 "\\_SB.PCI.AGP.VGA", /* 570 */
4782 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4783 "\\_SB.PCI0.VID0", /* 770e */
4784 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4785 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4786 "\\_SB.PCI0.AGP.VID", /* all others */
4789 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4791 static int __init video_init(struct ibm_init_struct *iibm)
4795 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4797 TPACPI_ACPIHANDLE_INIT(vid);
4798 if (tpacpi_is_ibm())
4799 TPACPI_ACPIHANDLE_INIT(vid2);
4801 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4802 /* G41, assume IVGA doesn't change */
4803 vid_handle = vid2_handle;
4806 /* video switching not supported on R30, R31 */
4807 video_supported = TPACPI_VIDEO_NONE;
4808 else if (tpacpi_is_ibm() &&
4809 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4811 video_supported = TPACPI_VIDEO_570;
4812 else if (tpacpi_is_ibm() &&
4813 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4814 /* 600e/x, 770e, 770x */
4815 video_supported = TPACPI_VIDEO_770;
4818 video_supported = TPACPI_VIDEO_NEW;
4820 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4821 str_supported(video_supported != TPACPI_VIDEO_NONE),
4824 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4827 static void video_exit(void)
4829 dbg_printk(TPACPI_DBG_EXIT,
4830 "restoring original video autoswitch mode\n");
4831 if (video_autosw_set(video_orig_autosw))
4832 pr_err("error while trying to restore original video autoswitch mode\n");
4835 static int video_outputsw_get(void)
4840 switch (video_supported) {
4841 case TPACPI_VIDEO_570:
4842 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4843 TP_ACPI_VIDEO_570_PHSCMD))
4845 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4847 case TPACPI_VIDEO_770:
4848 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4851 status |= TP_ACPI_VIDEO_S_LCD;
4852 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4855 status |= TP_ACPI_VIDEO_S_CRT;
4857 case TPACPI_VIDEO_NEW:
4858 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4859 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4862 status |= TP_ACPI_VIDEO_S_CRT;
4864 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4865 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4868 status |= TP_ACPI_VIDEO_S_LCD;
4869 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4872 status |= TP_ACPI_VIDEO_S_DVI;
4881 static int video_outputsw_set(int status)
4886 switch (video_supported) {
4887 case TPACPI_VIDEO_570:
4888 res = acpi_evalf(NULL, NULL,
4889 "\\_SB.PHS2", "vdd",
4890 TP_ACPI_VIDEO_570_PHS2CMD,
4891 status | TP_ACPI_VIDEO_570_PHS2SET);
4893 case TPACPI_VIDEO_770:
4894 autosw = video_autosw_get();
4898 res = video_autosw_set(1);
4901 res = acpi_evalf(vid_handle, NULL,
4902 "ASWT", "vdd", status * 0x100, 0);
4903 if (!autosw && video_autosw_set(autosw)) {
4904 pr_err("video auto-switch left enabled due to error\n");
4908 case TPACPI_VIDEO_NEW:
4909 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4910 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4916 return (res) ? 0 : -EIO;
4919 static int video_autosw_get(void)
4923 switch (video_supported) {
4924 case TPACPI_VIDEO_570:
4925 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4928 case TPACPI_VIDEO_770:
4929 case TPACPI_VIDEO_NEW:
4930 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4940 static int video_autosw_set(int enable)
4942 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4947 static int video_outputsw_cycle(void)
4949 int autosw = video_autosw_get();
4955 switch (video_supported) {
4956 case TPACPI_VIDEO_570:
4957 res = video_autosw_set(1);
4960 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4962 case TPACPI_VIDEO_770:
4963 case TPACPI_VIDEO_NEW:
4964 res = video_autosw_set(1);
4967 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4972 if (!autosw && video_autosw_set(autosw)) {
4973 pr_err("video auto-switch left enabled due to error\n");
4977 return (res) ? 0 : -EIO;
4980 static int video_expand_toggle(void)
4982 switch (video_supported) {
4983 case TPACPI_VIDEO_570:
4984 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
4986 case TPACPI_VIDEO_770:
4987 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
4989 case TPACPI_VIDEO_NEW:
4990 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
4998 static int video_read(struct seq_file *m)
5002 if (video_supported == TPACPI_VIDEO_NONE) {
5003 seq_printf(m, "status:\t\tnot supported\n");
5007 /* Even reads can crash X.org, so... */
5008 if (!capable(CAP_SYS_ADMIN))
5011 status = video_outputsw_get();
5015 autosw = video_autosw_get();
5019 seq_printf(m, "status:\t\tsupported\n");
5020 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5021 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5022 if (video_supported == TPACPI_VIDEO_NEW)
5023 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5024 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5025 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5026 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5027 if (video_supported == TPACPI_VIDEO_NEW)
5028 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5029 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5030 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5035 static int video_write(char *buf)
5038 int enable, disable, status;
5041 if (video_supported == TPACPI_VIDEO_NONE)
5044 /* Even reads can crash X.org, let alone writes... */
5045 if (!capable(CAP_SYS_ADMIN))
5051 while ((cmd = next_cmd(&buf))) {
5052 if (strlencmp(cmd, "lcd_enable") == 0) {
5053 enable |= TP_ACPI_VIDEO_S_LCD;
5054 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5055 disable |= TP_ACPI_VIDEO_S_LCD;
5056 } else if (strlencmp(cmd, "crt_enable") == 0) {
5057 enable |= TP_ACPI_VIDEO_S_CRT;
5058 } else if (strlencmp(cmd, "crt_disable") == 0) {
5059 disable |= TP_ACPI_VIDEO_S_CRT;
5060 } else if (video_supported == TPACPI_VIDEO_NEW &&
5061 strlencmp(cmd, "dvi_enable") == 0) {
5062 enable |= TP_ACPI_VIDEO_S_DVI;
5063 } else if (video_supported == TPACPI_VIDEO_NEW &&
5064 strlencmp(cmd, "dvi_disable") == 0) {
5065 disable |= TP_ACPI_VIDEO_S_DVI;
5066 } else if (strlencmp(cmd, "auto_enable") == 0) {
5067 res = video_autosw_set(1);
5070 } else if (strlencmp(cmd, "auto_disable") == 0) {
5071 res = video_autosw_set(0);
5074 } else if (strlencmp(cmd, "video_switch") == 0) {
5075 res = video_outputsw_cycle();
5078 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5079 res = video_expand_toggle();
5086 if (enable || disable) {
5087 status = video_outputsw_get();
5090 res = video_outputsw_set((status & ~disable) | enable);
5098 static struct ibm_struct video_driver_data = {
5101 .write = video_write,
5105 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5107 /*************************************************************************
5108 * Keyboard backlight subdriver
5111 static enum led_brightness kbdlight_brightness;
5112 static DEFINE_MUTEX(kbdlight_mutex);
5114 static int kbdlight_set_level(int level)
5121 mutex_lock(&kbdlight_mutex);
5123 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5126 kbdlight_brightness = level;
5128 mutex_unlock(&kbdlight_mutex);
5133 static int kbdlight_get_level(void)
5140 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5146 return status & 0x3;
5149 static bool kbdlight_is_supported(void)
5156 if (!acpi_has_method(hkey_handle, "MLCG")) {
5157 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5161 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5162 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5167 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5171 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5173 * Guessed test for keyboard backlight:
5175 * Machines with backlight keyboard return:
5176 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5177 * b110100010010000000XX - ThinkPad x230
5178 * b010100000010000000XX - ThinkPad x240
5179 * b010100000010000000XX - ThinkPad W541
5180 * (XX is current backlight level)
5182 * Machines without backlight keyboard return:
5183 * b10100001000000000000 - ThinkPad x230
5184 * b10110001000000000000 - ThinkPad E430
5185 * b00000000000000000000 - ThinkPad E450
5187 * Candidate BITs for detection test (XOR):
5188 * b01000000001000000000
5191 return status & BIT(9);
5194 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5195 enum led_brightness brightness)
5197 return kbdlight_set_level(brightness);
5200 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5204 level = kbdlight_get_level();
5211 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5213 .name = "tpacpi::kbd_backlight",
5214 .max_brightness = 2,
5215 .flags = LED_BRIGHT_HW_CHANGED,
5216 .brightness_set_blocking = &kbdlight_sysfs_set,
5217 .brightness_get = &kbdlight_sysfs_get,
5221 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5225 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5227 TPACPI_ACPIHANDLE_INIT(hkey);
5229 if (!kbdlight_is_supported()) {
5230 tp_features.kbdlight = 0;
5231 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5235 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5236 tp_features.kbdlight = 1;
5238 rc = led_classdev_register(&tpacpi_pdev->dev,
5239 &tpacpi_led_kbdlight.led_classdev);
5241 tp_features.kbdlight = 0;
5245 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5246 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5250 static void kbdlight_exit(void)
5252 if (tp_features.kbdlight)
5253 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5256 static int kbdlight_set_level_and_update(int level)
5259 struct led_classdev *led_cdev;
5261 ret = kbdlight_set_level(level);
5262 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5264 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5265 led_cdev->brightness = level;
5270 static int kbdlight_read(struct seq_file *m)
5274 if (!tp_features.kbdlight) {
5275 seq_printf(m, "status:\t\tnot supported\n");
5277 level = kbdlight_get_level();
5279 seq_printf(m, "status:\t\terror %d\n", level);
5281 seq_printf(m, "status:\t\t%d\n", level);
5282 seq_printf(m, "commands:\t0, 1, 2\n");
5288 static int kbdlight_write(char *buf)
5293 if (!tp_features.kbdlight)
5296 while ((cmd = next_cmd(&buf))) {
5297 if (strlencmp(cmd, "0") == 0)
5299 else if (strlencmp(cmd, "1") == 0)
5301 else if (strlencmp(cmd, "2") == 0)
5310 return kbdlight_set_level_and_update(level);
5313 static void kbdlight_suspend(void)
5315 struct led_classdev *led_cdev;
5317 if (!tp_features.kbdlight)
5320 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5321 led_update_brightness(led_cdev);
5322 led_classdev_suspend(led_cdev);
5325 static void kbdlight_resume(void)
5327 if (!tp_features.kbdlight)
5330 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5333 static struct ibm_struct kbdlight_driver_data = {
5335 .read = kbdlight_read,
5336 .write = kbdlight_write,
5337 .suspend = kbdlight_suspend,
5338 .resume = kbdlight_resume,
5339 .exit = kbdlight_exit,
5342 /*************************************************************************
5343 * Light (thinklight) subdriver
5346 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5347 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5349 static int light_get_status(void)
5353 if (tp_features.light_status) {
5354 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5362 static int light_set_status(int status)
5366 if (tp_features.light) {
5368 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5370 TP_CMOS_THINKLIGHT_ON :
5371 TP_CMOS_THINKLIGHT_OFF);
5373 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5376 return (rc) ? 0 : -EIO;
5382 static int light_sysfs_set(struct led_classdev *led_cdev,
5383 enum led_brightness brightness)
5385 return light_set_status((brightness != LED_OFF) ?
5386 TPACPI_LED_ON : TPACPI_LED_OFF);
5389 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5391 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5394 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5396 .name = "tpacpi::thinklight",
5397 .brightness_set_blocking = &light_sysfs_set,
5398 .brightness_get = &light_sysfs_get,
5402 static int __init light_init(struct ibm_init_struct *iibm)
5406 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5408 if (tpacpi_is_ibm()) {
5409 TPACPI_ACPIHANDLE_INIT(ledb);
5410 TPACPI_ACPIHANDLE_INIT(lght);
5412 TPACPI_ACPIHANDLE_INIT(cmos);
5414 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5415 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5417 if (tp_features.light)
5418 /* light status not supported on
5419 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5420 tp_features.light_status =
5421 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5423 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5424 str_supported(tp_features.light),
5425 str_supported(tp_features.light_status));
5427 if (!tp_features.light)
5430 rc = led_classdev_register(&tpacpi_pdev->dev,
5431 &tpacpi_led_thinklight.led_classdev);
5434 tp_features.light = 0;
5435 tp_features.light_status = 0;
5443 static void light_exit(void)
5445 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5448 static int light_read(struct seq_file *m)
5452 if (!tp_features.light) {
5453 seq_printf(m, "status:\t\tnot supported\n");
5454 } else if (!tp_features.light_status) {
5455 seq_printf(m, "status:\t\tunknown\n");
5456 seq_printf(m, "commands:\ton, off\n");
5458 status = light_get_status();
5461 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5462 seq_printf(m, "commands:\ton, off\n");
5468 static int light_write(char *buf)
5473 if (!tp_features.light)
5476 while ((cmd = next_cmd(&buf))) {
5477 if (strlencmp(cmd, "on") == 0) {
5479 } else if (strlencmp(cmd, "off") == 0) {
5485 return light_set_status(newstatus);
5488 static struct ibm_struct light_driver_data = {
5491 .write = light_write,
5495 /*************************************************************************
5499 /* sysfs cmos_command -------------------------------------------------- */
5500 static ssize_t cmos_command_store(struct device *dev,
5501 struct device_attribute *attr,
5502 const char *buf, size_t count)
5504 unsigned long cmos_cmd;
5507 if (parse_strtoul(buf, 21, &cmos_cmd))
5510 res = issue_thinkpad_cmos_command(cmos_cmd);
5511 return (res) ? res : count;
5514 static DEVICE_ATTR_WO(cmos_command);
5516 /* --------------------------------------------------------------------- */
5518 static int __init cmos_init(struct ibm_init_struct *iibm)
5522 vdbg_printk(TPACPI_DBG_INIT,
5523 "initializing cmos commands subdriver\n");
5525 TPACPI_ACPIHANDLE_INIT(cmos);
5527 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5528 str_supported(cmos_handle != NULL));
5530 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5534 return (cmos_handle) ? 0 : 1;
5537 static void cmos_exit(void)
5539 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5542 static int cmos_read(struct seq_file *m)
5544 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5545 R30, R31, T20-22, X20-21 */
5547 seq_printf(m, "status:\t\tnot supported\n");
5549 seq_printf(m, "status:\t\tsupported\n");
5550 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5556 static int cmos_write(char *buf)
5561 while ((cmd = next_cmd(&buf))) {
5562 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5563 cmos_cmd >= 0 && cmos_cmd <= 21) {
5568 res = issue_thinkpad_cmos_command(cmos_cmd);
5576 static struct ibm_struct cmos_driver_data = {
5579 .write = cmos_write,
5583 /*************************************************************************
5587 enum led_access_mode {
5588 TPACPI_LED_NONE = 0,
5589 TPACPI_LED_570, /* 570 */
5590 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5591 TPACPI_LED_NEW, /* all others */
5594 enum { /* For TPACPI_LED_OLD */
5595 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5596 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5597 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5600 static enum led_access_mode led_supported;
5602 static acpi_handle led_handle;
5604 #define TPACPI_LED_NUMLEDS 16
5605 static struct tpacpi_led_classdev *tpacpi_leds;
5606 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5607 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5608 /* there's a limit of 19 chars + NULL before 2.6.26 */
5610 "tpacpi:orange:batt",
5611 "tpacpi:green:batt",
5612 "tpacpi::dock_active",
5613 "tpacpi::bay_active",
5614 "tpacpi::dock_batt",
5615 "tpacpi::unknown_led",
5617 "tpacpi::dock_status1",
5618 "tpacpi::dock_status2",
5619 "tpacpi::unknown_led2",
5620 "tpacpi::unknown_led3",
5621 "tpacpi::thinkvantage",
5623 #define TPACPI_SAFE_LEDS 0x1081U
5625 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5627 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5630 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5634 static int led_get_status(const unsigned int led)
5637 enum led_status_t led_s;
5639 switch (led_supported) {
5640 case TPACPI_LED_570:
5641 if (!acpi_evalf(ec_handle,
5642 &status, "GLED", "dd", 1 << led))
5644 led_s = (status == 0) ?
5649 tpacpi_led_state_cache[led] = led_s;
5658 static int led_set_status(const unsigned int led,
5659 const enum led_status_t ledstatus)
5661 /* off, on, blink. Index is led_status_t */
5662 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5663 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5667 switch (led_supported) {
5668 case TPACPI_LED_570:
5670 if (unlikely(led > 7))
5672 if (unlikely(tpacpi_is_led_restricted(led)))
5674 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5675 (1 << led), led_sled_arg1[ledstatus]))
5678 case TPACPI_LED_OLD:
5679 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5680 if (unlikely(led > 7))
5682 if (unlikely(tpacpi_is_led_restricted(led)))
5684 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5686 rc = ec_write(TPACPI_LED_EC_HLBL,
5687 (ledstatus == TPACPI_LED_BLINK) << led);
5689 rc = ec_write(TPACPI_LED_EC_HLCL,
5690 (ledstatus != TPACPI_LED_OFF) << led);
5692 case TPACPI_LED_NEW:
5694 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5696 if (unlikely(tpacpi_is_led_restricted(led)))
5698 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5699 led, led_led_arg1[ledstatus]))
5707 tpacpi_led_state_cache[led] = ledstatus;
5712 static int led_sysfs_set(struct led_classdev *led_cdev,
5713 enum led_brightness brightness)
5715 struct tpacpi_led_classdev *data = container_of(led_cdev,
5716 struct tpacpi_led_classdev, led_classdev);
5717 enum led_status_t new_state;
5719 if (brightness == LED_OFF)
5720 new_state = TPACPI_LED_OFF;
5721 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5722 new_state = TPACPI_LED_ON;
5724 new_state = TPACPI_LED_BLINK;
5726 return led_set_status(data->led, new_state);
5729 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5730 unsigned long *delay_on, unsigned long *delay_off)
5732 struct tpacpi_led_classdev *data = container_of(led_cdev,
5733 struct tpacpi_led_classdev, led_classdev);
5735 /* Can we choose the flash rate? */
5736 if (*delay_on == 0 && *delay_off == 0) {
5737 /* yes. set them to the hardware blink rate (1 Hz) */
5738 *delay_on = 500; /* ms */
5739 *delay_off = 500; /* ms */
5740 } else if ((*delay_on != 500) || (*delay_off != 500))
5743 return led_set_status(data->led, TPACPI_LED_BLINK);
5746 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5750 struct tpacpi_led_classdev *data = container_of(led_cdev,
5751 struct tpacpi_led_classdev, led_classdev);
5753 rc = led_get_status(data->led);
5755 if (rc == TPACPI_LED_OFF || rc < 0)
5756 rc = LED_OFF; /* no error handling in led class :( */
5763 static void led_exit(void)
5767 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5768 if (tpacpi_leds[i].led_classdev.name)
5769 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5775 static int __init tpacpi_init_led(unsigned int led)
5779 tpacpi_leds[led].led = led;
5781 /* LEDs with no name don't get registered */
5782 if (!tpacpi_led_names[led])
5785 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5786 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5787 if (led_supported == TPACPI_LED_570)
5788 tpacpi_leds[led].led_classdev.brightness_get =
5791 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5793 rc = led_classdev_register(&tpacpi_pdev->dev,
5794 &tpacpi_leds[led].led_classdev);
5796 tpacpi_leds[led].led_classdev.name = NULL;
5801 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5802 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5803 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5804 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5806 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5807 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5808 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5809 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5810 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5811 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5812 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5813 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5815 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5816 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5817 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5818 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5819 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5821 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5822 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5823 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5824 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5826 /* (1) - may have excess leds enabled on MSB */
5828 /* Defaults (order matters, keep last, don't reorder!) */
5830 .vendor = PCI_VENDOR_ID_LENOVO,
5831 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5834 { /* IBM ThinkPads with no EC version string */
5835 .vendor = PCI_VENDOR_ID_IBM,
5836 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5839 { /* IBM ThinkPads with EC version string */
5840 .vendor = PCI_VENDOR_ID_IBM,
5841 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5846 #undef TPACPI_LEDQ_IBM
5847 #undef TPACPI_LEDQ_LNV
5849 static enum led_access_mode __init led_init_detect_mode(void)
5853 if (tpacpi_is_ibm()) {
5855 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5856 if (ACPI_SUCCESS(status))
5857 return TPACPI_LED_570;
5859 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5860 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5861 if (ACPI_SUCCESS(status))
5862 return TPACPI_LED_OLD;
5866 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5867 if (ACPI_SUCCESS(status))
5868 return TPACPI_LED_NEW;
5870 /* R30, R31, and unknown firmwares */
5872 return TPACPI_LED_NONE;
5875 static int __init led_init(struct ibm_init_struct *iibm)
5879 unsigned long useful_leds;
5881 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5883 led_supported = led_init_detect_mode();
5885 if (led_supported != TPACPI_LED_NONE) {
5886 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5887 ARRAY_SIZE(led_useful_qtable));
5891 led_supported = TPACPI_LED_NONE;
5895 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5896 str_supported(led_supported), led_supported);
5898 if (led_supported == TPACPI_LED_NONE)
5901 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5904 pr_err("Out of memory for LED data\n");
5908 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5909 tpacpi_leds[i].led = -1;
5911 if (!tpacpi_is_led_restricted(i) &&
5912 test_bit(i, &useful_leds)) {
5913 rc = tpacpi_init_led(i);
5921 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5922 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
5927 #define str_led_status(s) \
5928 ((s) == TPACPI_LED_OFF ? "off" : \
5929 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5931 static int led_read(struct seq_file *m)
5933 if (!led_supported) {
5934 seq_printf(m, "status:\t\tnot supported\n");
5937 seq_printf(m, "status:\t\tsupported\n");
5939 if (led_supported == TPACPI_LED_570) {
5942 for (i = 0; i < 8; i++) {
5943 status = led_get_status(i);
5946 seq_printf(m, "%d:\t\t%s\n",
5947 i, str_led_status(status));
5951 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5956 static int led_write(char *buf)
5960 enum led_status_t s;
5965 while ((cmd = next_cmd(&buf))) {
5966 if (sscanf(cmd, "%d", &led) != 1)
5969 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5970 tpacpi_leds[led].led < 0)
5973 if (strstr(cmd, "off")) {
5975 } else if (strstr(cmd, "on")) {
5977 } else if (strstr(cmd, "blink")) {
5978 s = TPACPI_LED_BLINK;
5983 rc = led_set_status(led, s);
5991 static struct ibm_struct led_driver_data = {
5998 /*************************************************************************
6002 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6004 #define TPACPI_BEEP_Q1 0x0001
6006 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6007 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6008 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6011 static int __init beep_init(struct ibm_init_struct *iibm)
6013 unsigned long quirks;
6015 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6017 TPACPI_ACPIHANDLE_INIT(beep);
6019 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6020 str_supported(beep_handle != NULL));
6022 quirks = tpacpi_check_quirks(beep_quirk_table,
6023 ARRAY_SIZE(beep_quirk_table));
6025 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6027 return (beep_handle) ? 0 : 1;
6030 static int beep_read(struct seq_file *m)
6033 seq_printf(m, "status:\t\tnot supported\n");
6035 seq_printf(m, "status:\t\tsupported\n");
6036 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6042 static int beep_write(char *buf)
6050 while ((cmd = next_cmd(&buf))) {
6051 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6052 beep_cmd >= 0 && beep_cmd <= 17) {
6056 if (tp_features.beep_needs_two_args) {
6057 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6061 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6070 static struct ibm_struct beep_driver_data = {
6073 .write = beep_write,
6076 /*************************************************************************
6080 enum thermal_access_mode {
6081 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6082 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6083 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6084 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6085 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6088 enum { /* TPACPI_THERMAL_TPEC_* */
6089 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6090 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6091 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6093 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6097 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6098 struct ibm_thermal_sensors_struct {
6099 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6102 static enum thermal_access_mode thermal_read_mode;
6104 /* idx is zero-based */
6105 static int thermal_get_sensor(int idx, s32 *value)
6111 t = TP_EC_THERMAL_TMP0;
6113 switch (thermal_read_mode) {
6114 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6115 case TPACPI_THERMAL_TPEC_16:
6116 if (idx >= 8 && idx <= 15) {
6117 t = TP_EC_THERMAL_TMP8;
6122 case TPACPI_THERMAL_TPEC_8:
6124 if (!acpi_ec_read(t + idx, &tmp))
6126 *value = tmp * 1000;
6131 case TPACPI_THERMAL_ACPI_UPDT:
6133 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6134 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6136 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6138 *value = (t - 2732) * 100;
6143 case TPACPI_THERMAL_ACPI_TMP07:
6145 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6146 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6148 if (t > 127 || t < -127)
6149 t = TP_EC_THERMAL_TMP_NA;
6155 case TPACPI_THERMAL_NONE:
6163 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6174 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6177 for (i = 0 ; i < n; i++) {
6178 res = thermal_get_sensor(i, &s->temp[i]);
6186 static void thermal_dump_all_sensors(void)
6189 struct ibm_thermal_sensors_struct t;
6191 n = thermal_get_sensors(&t);
6195 pr_notice("temperatures (Celsius):");
6197 for (i = 0; i < n; i++) {
6198 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6199 pr_cont(" %d", (int)(t.temp[i] / 1000));
6207 /* sysfs temp##_input -------------------------------------------------- */
6209 static ssize_t thermal_temp_input_show(struct device *dev,
6210 struct device_attribute *attr,
6213 struct sensor_device_attribute *sensor_attr =
6214 to_sensor_dev_attr(attr);
6215 int idx = sensor_attr->index;
6219 res = thermal_get_sensor(idx, &value);
6222 if (value == TPACPI_THERMAL_SENSOR_NA)
6225 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6228 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6229 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6230 thermal_temp_input_show, NULL, _idxB)
6232 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6233 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6234 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6235 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6236 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6237 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6238 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6239 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6240 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6241 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6242 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6243 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6244 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6245 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6246 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6247 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6248 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6251 #define THERMAL_ATTRS(X) \
6252 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6254 static struct attribute *thermal_temp_input_attr[] = {
6274 static const struct attribute_group thermal_temp_input16_group = {
6275 .attrs = thermal_temp_input_attr
6278 static const struct attribute_group thermal_temp_input8_group = {
6279 .attrs = &thermal_temp_input_attr[8]
6282 #undef THERMAL_SENSOR_ATTR_TEMP
6283 #undef THERMAL_ATTRS
6285 /* --------------------------------------------------------------------- */
6287 static int __init thermal_init(struct ibm_init_struct *iibm)
6294 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6296 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6298 if (thinkpad_id.ec_model) {
6300 * Direct EC access mode: sensors at registers
6301 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6302 * non-implemented, thermal sensors return 0x80 when
6307 for (i = 0; i < 8; i++) {
6308 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6314 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6322 /* This is sheer paranoia, but we handle it anyway */
6324 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6325 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6327 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6328 thermal_read_mode = TPACPI_THERMAL_NONE;
6333 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6335 } else if (acpi_tmp7) {
6336 if (tpacpi_is_ibm() &&
6337 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6338 /* 600e/x, 770e, 770x */
6339 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6341 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6342 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6345 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6346 thermal_read_mode = TPACPI_THERMAL_NONE;
6349 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6350 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6353 switch (thermal_read_mode) {
6354 case TPACPI_THERMAL_TPEC_16:
6355 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6356 &thermal_temp_input16_group);
6360 case TPACPI_THERMAL_TPEC_8:
6361 case TPACPI_THERMAL_ACPI_TMP07:
6362 case TPACPI_THERMAL_ACPI_UPDT:
6363 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6364 &thermal_temp_input8_group);
6368 case TPACPI_THERMAL_NONE:
6376 static void thermal_exit(void)
6378 switch (thermal_read_mode) {
6379 case TPACPI_THERMAL_TPEC_16:
6380 sysfs_remove_group(&tpacpi_hwmon->kobj,
6381 &thermal_temp_input16_group);
6383 case TPACPI_THERMAL_TPEC_8:
6384 case TPACPI_THERMAL_ACPI_TMP07:
6385 case TPACPI_THERMAL_ACPI_UPDT:
6386 sysfs_remove_group(&tpacpi_hwmon->kobj,
6387 &thermal_temp_input8_group);
6389 case TPACPI_THERMAL_NONE:
6395 static int thermal_read(struct seq_file *m)
6398 struct ibm_thermal_sensors_struct t;
6400 n = thermal_get_sensors(&t);
6401 if (unlikely(n < 0))
6404 seq_printf(m, "temperatures:\t");
6407 for (i = 0; i < (n - 1); i++)
6408 seq_printf(m, "%d ", t.temp[i] / 1000);
6409 seq_printf(m, "%d\n", t.temp[i] / 1000);
6411 seq_printf(m, "not supported\n");
6416 static struct ibm_struct thermal_driver_data = {
6418 .read = thermal_read,
6419 .exit = thermal_exit,
6422 /*************************************************************************
6423 * Backlight/brightness subdriver
6426 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6429 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6430 * CMOS NVRAM byte 0x5E, bits 0-3.
6432 * EC HBRV (0x31) has the following layout
6433 * Bit 7: unknown function
6434 * Bit 6: unknown function
6435 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6436 * Bit 4: must be set to zero to avoid problems
6437 * Bit 3-0: backlight brightness level
6439 * brightness_get_raw returns status data in the HBRV layout
6441 * WARNING: The X61 has been verified to use HBRV for something else, so
6442 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6443 * testing on the very early *60 Lenovo models...
6447 TP_EC_BACKLIGHT = 0x31,
6449 /* TP_EC_BACKLIGHT bitmasks */
6450 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6451 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6452 TP_EC_BACKLIGHT_MAPSW = 0x20,
6455 enum tpacpi_brightness_access_mode {
6456 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6457 TPACPI_BRGHT_MODE_EC, /* EC control */
6458 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6459 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6460 TPACPI_BRGHT_MODE_MAX
6463 static struct backlight_device *ibm_backlight_device;
6465 static enum tpacpi_brightness_access_mode brightness_mode =
6466 TPACPI_BRGHT_MODE_MAX;
6468 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6470 static struct mutex brightness_mutex;
6472 /* NVRAM brightness access,
6473 * call with brightness_mutex held! */
6474 static unsigned int tpacpi_brightness_nvram_get(void)
6478 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6479 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6480 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6481 lnvram &= bright_maxlvl;
6486 static void tpacpi_brightness_checkpoint_nvram(void)
6491 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6494 vdbg_printk(TPACPI_DBG_BRGHT,
6495 "trying to checkpoint backlight level to NVRAM...\n");
6497 if (mutex_lock_killable(&brightness_mutex) < 0)
6500 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6502 lec &= TP_EC_BACKLIGHT_LVLMSK;
6503 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6505 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6506 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6507 /* NVRAM needs update */
6508 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6509 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6511 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6512 dbg_printk(TPACPI_DBG_BRGHT,
6513 "updated NVRAM backlight level to %u (0x%02x)\n",
6514 (unsigned int) lec, (unsigned int) b_nvram);
6516 vdbg_printk(TPACPI_DBG_BRGHT,
6517 "NVRAM backlight level already is %u (0x%02x)\n",
6518 (unsigned int) lec, (unsigned int) b_nvram);
6521 mutex_unlock(&brightness_mutex);
6525 /* call with brightness_mutex held! */
6526 static int tpacpi_brightness_get_raw(int *status)
6530 switch (brightness_mode) {
6531 case TPACPI_BRGHT_MODE_UCMS_STEP:
6532 *status = tpacpi_brightness_nvram_get();
6534 case TPACPI_BRGHT_MODE_EC:
6535 case TPACPI_BRGHT_MODE_ECNVRAM:
6536 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6545 /* call with brightness_mutex held! */
6546 /* do NOT call with illegal backlight level value */
6547 static int tpacpi_brightness_set_ec(unsigned int value)
6551 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6554 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6555 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6556 (value & TP_EC_BACKLIGHT_LVLMSK))))
6562 /* call with brightness_mutex held! */
6563 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6566 unsigned int current_value, i;
6568 current_value = tpacpi_brightness_nvram_get();
6570 if (value == current_value)
6573 cmos_cmd = (value > current_value) ?
6574 TP_CMOS_BRIGHTNESS_UP :
6575 TP_CMOS_BRIGHTNESS_DOWN;
6576 inc = (value > current_value) ? 1 : -1;
6578 for (i = current_value; i != value; i += inc)
6579 if (issue_thinkpad_cmos_command(cmos_cmd))
6585 /* May return EINTR which can always be mapped to ERESTARTSYS */
6586 static int brightness_set(unsigned int value)
6590 if (value > bright_maxlvl)
6593 vdbg_printk(TPACPI_DBG_BRGHT,
6594 "set backlight level to %d\n", value);
6596 res = mutex_lock_killable(&brightness_mutex);
6600 switch (brightness_mode) {
6601 case TPACPI_BRGHT_MODE_EC:
6602 case TPACPI_BRGHT_MODE_ECNVRAM:
6603 res = tpacpi_brightness_set_ec(value);
6605 case TPACPI_BRGHT_MODE_UCMS_STEP:
6606 res = tpacpi_brightness_set_ucmsstep(value);
6612 mutex_unlock(&brightness_mutex);
6616 /* sysfs backlight class ----------------------------------------------- */
6618 static int brightness_update_status(struct backlight_device *bd)
6620 unsigned int level =
6621 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6622 bd->props.power == FB_BLANK_UNBLANK) ?
6623 bd->props.brightness : 0;
6625 dbg_printk(TPACPI_DBG_BRGHT,
6626 "backlight: attempt to set level to %d\n",
6629 /* it is the backlight class's job (caller) to handle
6630 * EINTR and other errors properly */
6631 return brightness_set(level);
6634 static int brightness_get(struct backlight_device *bd)
6638 res = mutex_lock_killable(&brightness_mutex);
6642 res = tpacpi_brightness_get_raw(&status);
6644 mutex_unlock(&brightness_mutex);
6649 return status & TP_EC_BACKLIGHT_LVLMSK;
6652 static void tpacpi_brightness_notify_change(void)
6654 backlight_force_update(ibm_backlight_device,
6655 BACKLIGHT_UPDATE_HOTKEY);
6658 static const struct backlight_ops ibm_backlight_data = {
6659 .get_brightness = brightness_get,
6660 .update_status = brightness_update_status,
6663 /* --------------------------------------------------------------------- */
6666 * Call _BCL method of video device. On some ThinkPads this will
6667 * switch the firmware to the ACPI brightness control mode.
6670 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6672 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6673 union acpi_object *obj;
6674 struct acpi_device *device, *child;
6677 if (acpi_bus_get_device(handle, &device))
6681 list_for_each_entry(child, &device->children, node) {
6682 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6684 if (ACPI_FAILURE(status))
6687 obj = (union acpi_object *)buffer.pointer;
6688 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6689 pr_err("Unknown _BCL data, please report this to %s\n",
6693 rc = obj->package.count;
6698 kfree(buffer.pointer);
6704 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6706 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6708 acpi_handle video_device;
6711 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6713 bcl_levels = tpacpi_query_bcl_levels(video_device);
6715 tp_features.bright_acpimode = (bcl_levels > 0);
6717 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6721 * These are only useful for models that have only one possibility
6722 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6725 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6726 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6727 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6729 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6730 /* Models with ATI GPUs known to require ECNVRAM mode */
6731 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6733 /* Models with ATI GPUs that can use ECNVRAM */
6734 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6735 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6736 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6737 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6739 /* Models with Intel Extreme Graphics 2 */
6740 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6741 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6742 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6744 /* Models with Intel GMA900 */
6745 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6746 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6747 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6751 * Returns < 0 for error, otherwise sets tp_features.bright_*
6752 * and bright_maxlvl.
6754 static void __init tpacpi_detect_brightness_capabilities(void)
6758 vdbg_printk(TPACPI_DBG_INIT,
6759 "detecting firmware brightness interface capabilities\n");
6761 /* we could run a quirks check here (same table used by
6762 * brightness_init) if needed */
6765 * We always attempt to detect acpi support, so as to switch
6766 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6767 * going to publish a backlight interface
6769 b = tpacpi_check_std_acpi_brightness_support();
6779 tp_features.bright_unkfw = 1;
6780 bright_maxlvl = b - 1;
6782 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6785 static int __init brightness_init(struct ibm_init_struct *iibm)
6787 struct backlight_properties props;
6789 unsigned long quirks;
6791 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6793 mutex_init(&brightness_mutex);
6795 quirks = tpacpi_check_quirks(brightness_quirk_table,
6796 ARRAY_SIZE(brightness_quirk_table));
6798 /* tpacpi_detect_brightness_capabilities() must have run already */
6800 /* if it is unknown, we don't handle it: it wouldn't be safe */
6801 if (tp_features.bright_unkfw)
6804 if (!brightness_enable) {
6805 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6806 "brightness support disabled by module parameter\n");
6810 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6811 if (brightness_enable > 1) {
6812 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6814 } else if (brightness_enable == 1) {
6815 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6818 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6819 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
6823 * Check for module parameter bogosity, note that we
6824 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6825 * able to detect "unspecified"
6827 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6830 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6831 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6832 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6833 if (quirks & TPACPI_BRGHT_Q_EC)
6834 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6836 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6838 dbg_printk(TPACPI_DBG_BRGHT,
6839 "driver auto-selected brightness_mode=%d\n",
6844 if (!tpacpi_is_ibm() &&
6845 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6846 brightness_mode == TPACPI_BRGHT_MODE_EC))
6849 if (tpacpi_brightness_get_raw(&b) < 0)
6852 memset(&props, 0, sizeof(struct backlight_properties));
6853 props.type = BACKLIGHT_PLATFORM;
6854 props.max_brightness = bright_maxlvl;
6855 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6856 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6858 &ibm_backlight_data,
6860 if (IS_ERR(ibm_backlight_device)) {
6861 int rc = PTR_ERR(ibm_backlight_device);
6862 ibm_backlight_device = NULL;
6863 pr_err("Could not register backlight device\n");
6866 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6867 "brightness is supported\n");
6869 if (quirks & TPACPI_BRGHT_Q_ASK) {
6870 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6872 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6876 /* Added by mistake in early 2007. Probably useless, but it could
6877 * be working around some unknown firmware problem where the value
6878 * read at startup doesn't match the real hardware state... so leave
6879 * it in place just in case */
6880 backlight_update_status(ibm_backlight_device);
6882 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6883 "brightness: registering brightness hotkeys as change notification\n");
6884 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6885 | TP_ACPI_HKEY_BRGHTUP_MASK
6886 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6890 static void brightness_suspend(void)
6892 tpacpi_brightness_checkpoint_nvram();
6895 static void brightness_shutdown(void)
6897 tpacpi_brightness_checkpoint_nvram();
6900 static void brightness_exit(void)
6902 if (ibm_backlight_device) {
6903 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6904 "calling backlight_device_unregister()\n");
6905 backlight_device_unregister(ibm_backlight_device);
6908 tpacpi_brightness_checkpoint_nvram();
6911 static int brightness_read(struct seq_file *m)
6915 level = brightness_get(NULL);
6917 seq_printf(m, "level:\t\tunreadable\n");
6919 seq_printf(m, "level:\t\t%d\n", level);
6920 seq_printf(m, "commands:\tup, down\n");
6921 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6928 static int brightness_write(char *buf)
6934 level = brightness_get(NULL);
6938 while ((cmd = next_cmd(&buf))) {
6939 if (strlencmp(cmd, "up") == 0) {
6940 if (level < bright_maxlvl)
6942 } else if (strlencmp(cmd, "down") == 0) {
6945 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6946 level >= 0 && level <= bright_maxlvl) {
6952 tpacpi_disclose_usertask("procfs brightness",
6953 "set level to %d\n", level);
6956 * Now we know what the final level should be, so we try to set it.
6957 * Doing it this way makes the syscall restartable in case of EINTR
6959 rc = brightness_set(level);
6960 if (!rc && ibm_backlight_device)
6961 backlight_force_update(ibm_backlight_device,
6962 BACKLIGHT_UPDATE_SYSFS);
6963 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6966 static struct ibm_struct brightness_driver_data = {
6967 .name = "brightness",
6968 .read = brightness_read,
6969 .write = brightness_write,
6970 .exit = brightness_exit,
6971 .suspend = brightness_suspend,
6972 .shutdown = brightness_shutdown,
6975 /*************************************************************************
6980 * IBM ThinkPads have a simple volume controller with MUTE gating.
6981 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6983 * Since the *61 series (and probably also the later *60 series), Lenovo
6984 * ThinkPads only implement the MUTE gate.
6987 * Bit 6: MUTE (1 mutes sound)
6989 * Other bits should be zero as far as we know.
6991 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6992 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6993 * such as bit 7 which is used to detect repeated presses of MUTE,
6994 * and we leave them unchanged.
6996 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6997 * in response to user input. Unfortunately, this rarely works well.
6998 * The laptop changes the state of its internal MUTE gate and, on some
6999 * models, sends KEY_MUTE, causing any user code that responds to the
7000 * mute button to get confused. The hardware MUTE gate is also
7001 * unnecessary, since user code can handle the mute button without
7002 * kernel or EC help.
7004 * To avoid confusing userspace, we simply disable all EC-based mute
7005 * and volume controls when possible.
7008 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7010 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7011 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7012 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7014 #if SNDRV_CARDS <= 32
7015 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7017 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7019 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7020 static char *alsa_id = "ThinkPadEC";
7021 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7023 struct tpacpi_alsa_data {
7024 struct snd_card *card;
7025 struct snd_ctl_elem_id *ctl_mute_id;
7026 struct snd_ctl_elem_id *ctl_vol_id;
7029 static struct snd_card *alsa_card;
7034 /* TP_EC_AUDIO bits */
7035 TP_EC_AUDIO_MUTESW = 6,
7037 /* TP_EC_AUDIO bitmasks */
7038 TP_EC_AUDIO_LVL_MSK = 0x0F,
7039 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7041 /* Maximum volume */
7042 TP_EC_VOLUME_MAX = 14,
7045 enum tpacpi_volume_access_mode {
7046 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7047 TPACPI_VOL_MODE_EC, /* Pure EC control */
7048 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7049 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7053 enum tpacpi_volume_capabilities {
7054 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7055 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7056 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7060 enum tpacpi_mute_btn_mode {
7061 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7062 /* We don't know what mode 1 is. */
7063 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7064 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7067 static enum tpacpi_volume_access_mode volume_mode =
7068 TPACPI_VOL_MODE_MAX;
7070 static enum tpacpi_volume_capabilities volume_capabilities;
7071 static bool volume_control_allowed;
7072 static bool software_mute_requested = true;
7073 static bool software_mute_active;
7074 static int software_mute_orig_mode;
7077 * Used to syncronize writers to TP_EC_AUDIO and
7078 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7080 static struct mutex volume_mutex;
7082 static void tpacpi_volume_checkpoint_nvram(void)
7088 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7090 if (!volume_control_allowed)
7092 if (software_mute_active)
7095 vdbg_printk(TPACPI_DBG_MIXER,
7096 "trying to checkpoint mixer state to NVRAM...\n");
7098 if (tp_features.mixer_no_level_control)
7099 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7101 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7103 if (mutex_lock_killable(&volume_mutex) < 0)
7106 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7109 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7111 if (lec != (b_nvram & ec_mask)) {
7112 /* NVRAM needs update */
7113 b_nvram &= ~ec_mask;
7115 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7116 dbg_printk(TPACPI_DBG_MIXER,
7117 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7118 (unsigned int) lec, (unsigned int) b_nvram);
7120 vdbg_printk(TPACPI_DBG_MIXER,
7121 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7122 (unsigned int) lec, (unsigned int) b_nvram);
7126 mutex_unlock(&volume_mutex);
7129 static int volume_get_status_ec(u8 *status)
7133 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7138 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7143 static int volume_get_status(u8 *status)
7145 return volume_get_status_ec(status);
7148 static int volume_set_status_ec(const u8 status)
7150 if (!acpi_ec_write(TP_EC_AUDIO, status))
7153 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7156 * On X200s, and possibly on others, it can take a while for
7157 * reads to become correct.
7164 static int volume_set_status(const u8 status)
7166 return volume_set_status_ec(status);
7169 /* returns < 0 on error, 0 on no change, 1 on change */
7170 static int __volume_set_mute_ec(const bool mute)
7175 if (mutex_lock_killable(&volume_mutex) < 0)
7178 rc = volume_get_status_ec(&s);
7182 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7183 s & ~TP_EC_AUDIO_MUTESW_MSK;
7186 rc = volume_set_status_ec(n);
7192 mutex_unlock(&volume_mutex);
7196 static int volume_alsa_set_mute(const bool mute)
7198 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7199 (mute) ? "" : "un");
7200 return __volume_set_mute_ec(mute);
7203 static int volume_set_mute(const bool mute)
7207 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7208 (mute) ? "" : "un");
7210 rc = __volume_set_mute_ec(mute);
7211 return (rc < 0) ? rc : 0;
7214 /* returns < 0 on error, 0 on no change, 1 on change */
7215 static int __volume_set_volume_ec(const u8 vol)
7220 if (vol > TP_EC_VOLUME_MAX)
7223 if (mutex_lock_killable(&volume_mutex) < 0)
7226 rc = volume_get_status_ec(&s);
7230 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7233 rc = volume_set_status_ec(n);
7239 mutex_unlock(&volume_mutex);
7243 static int volume_set_software_mute(bool startup)
7247 if (!tpacpi_is_lenovo())
7251 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7255 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7256 "Initial HAUM setting was %d\n",
7257 software_mute_orig_mode);
7260 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7261 (int)TP_EC_MUTE_BTN_NONE))
7264 if (result != TP_EC_MUTE_BTN_NONE)
7265 pr_warn("Unexpected SAUM result %d\n",
7269 * In software mute mode, the standard codec controls take
7270 * precendence, so we unmute the ThinkPad HW switch at
7271 * startup. Just on case there are SAUM-capable ThinkPads
7272 * with level controls, set max HW volume as well.
7274 if (tp_features.mixer_no_level_control)
7275 result = volume_set_mute(false);
7277 result = volume_set_status(TP_EC_VOLUME_MAX);
7280 pr_warn("Failed to unmute the HW mute switch\n");
7285 static void volume_exit_software_mute(void)
7289 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7290 || r != software_mute_orig_mode)
7291 pr_warn("Failed to restore mute mode\n");
7294 static int volume_alsa_set_volume(const u8 vol)
7296 dbg_printk(TPACPI_DBG_MIXER,
7297 "ALSA: trying to set volume level to %hu\n", vol);
7298 return __volume_set_volume_ec(vol);
7301 static void volume_alsa_notify_change(void)
7303 struct tpacpi_alsa_data *d;
7305 if (alsa_card && alsa_card->private_data) {
7306 d = alsa_card->private_data;
7308 snd_ctl_notify(alsa_card,
7309 SNDRV_CTL_EVENT_MASK_VALUE,
7312 snd_ctl_notify(alsa_card,
7313 SNDRV_CTL_EVENT_MASK_VALUE,
7318 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7319 struct snd_ctl_elem_info *uinfo)
7321 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7323 uinfo->value.integer.min = 0;
7324 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7328 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7329 struct snd_ctl_elem_value *ucontrol)
7334 rc = volume_get_status(&s);
7338 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7342 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7343 struct snd_ctl_elem_value *ucontrol)
7345 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7346 ucontrol->value.integer.value[0]);
7347 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7350 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7352 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7353 struct snd_ctl_elem_value *ucontrol)
7358 rc = volume_get_status(&s);
7362 ucontrol->value.integer.value[0] =
7363 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7367 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7368 struct snd_ctl_elem_value *ucontrol)
7370 tpacpi_disclose_usertask("ALSA", "%smute\n",
7371 ucontrol->value.integer.value[0] ?
7373 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7376 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7377 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7378 .name = "Console Playback Volume",
7380 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7381 .info = volume_alsa_vol_info,
7382 .get = volume_alsa_vol_get,
7385 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7387 .name = "Console Playback Switch",
7389 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7390 .info = volume_alsa_mute_info,
7391 .get = volume_alsa_mute_get,
7394 static void volume_suspend(void)
7396 tpacpi_volume_checkpoint_nvram();
7399 static void volume_resume(void)
7401 if (software_mute_active) {
7402 if (volume_set_software_mute(false) < 0)
7403 pr_warn("Failed to restore software mute\n");
7405 volume_alsa_notify_change();
7409 static void volume_shutdown(void)
7411 tpacpi_volume_checkpoint_nvram();
7414 static void volume_exit(void)
7417 snd_card_free(alsa_card);
7421 tpacpi_volume_checkpoint_nvram();
7423 if (software_mute_active)
7424 volume_exit_software_mute();
7427 static int __init volume_create_alsa_mixer(void)
7429 struct snd_card *card;
7430 struct tpacpi_alsa_data *data;
7431 struct snd_kcontrol *ctl_vol;
7432 struct snd_kcontrol *ctl_mute;
7435 rc = snd_card_new(&tpacpi_pdev->dev,
7436 alsa_index, alsa_id, THIS_MODULE,
7437 sizeof(struct tpacpi_alsa_data), &card);
7438 if (rc < 0 || !card) {
7439 pr_err("Failed to create ALSA card structures: %d\n", rc);
7443 BUG_ON(!card->private_data);
7444 data = card->private_data;
7447 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7448 sizeof(card->driver));
7449 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7450 sizeof(card->shortname));
7451 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7452 (thinkpad_id.ec_version_str) ?
7453 thinkpad_id.ec_version_str : "(unknown)");
7454 snprintf(card->longname, sizeof(card->longname),
7455 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7456 (thinkpad_id.ec_version_str) ?
7457 thinkpad_id.ec_version_str : "unknown");
7459 if (volume_control_allowed) {
7460 volume_alsa_control_vol.put = volume_alsa_vol_put;
7461 volume_alsa_control_vol.access =
7462 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7464 volume_alsa_control_mute.put = volume_alsa_mute_put;
7465 volume_alsa_control_mute.access =
7466 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7469 if (!tp_features.mixer_no_level_control) {
7470 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7471 rc = snd_ctl_add(card, ctl_vol);
7473 pr_err("Failed to create ALSA volume control: %d\n",
7477 data->ctl_vol_id = &ctl_vol->id;
7480 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7481 rc = snd_ctl_add(card, ctl_mute);
7483 pr_err("Failed to create ALSA mute control: %d\n", rc);
7486 data->ctl_mute_id = &ctl_mute->id;
7488 rc = snd_card_register(card);
7490 pr_err("Failed to register ALSA card: %d\n", rc);
7498 snd_card_free(card);
7502 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7503 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7505 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7506 /* Whitelist volume level on all IBM by default */
7507 { .vendor = PCI_VENDOR_ID_IBM,
7508 .bios = TPACPI_MATCH_ANY,
7509 .ec = TPACPI_MATCH_ANY,
7510 .quirks = TPACPI_VOL_Q_LEVEL },
7512 /* Lenovo models with volume control (needs confirmation) */
7513 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7514 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7515 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7516 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7517 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7518 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7519 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7521 /* Whitelist mute-only on all Lenovo by default */
7522 { .vendor = PCI_VENDOR_ID_LENOVO,
7523 .bios = TPACPI_MATCH_ANY,
7524 .ec = TPACPI_MATCH_ANY,
7525 .quirks = TPACPI_VOL_Q_MUTEONLY }
7528 static int __init volume_init(struct ibm_init_struct *iibm)
7530 unsigned long quirks;
7533 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7535 mutex_init(&volume_mutex);
7538 * Check for module parameter bogosity, note that we
7539 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7540 * able to detect "unspecified"
7542 if (volume_mode > TPACPI_VOL_MODE_MAX)
7545 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7546 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7551 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7555 * The ALSA mixer is our primary interface.
7556 * When disabled, don't install the subdriver at all
7559 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7560 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7564 quirks = tpacpi_check_quirks(volume_quirk_table,
7565 ARRAY_SIZE(volume_quirk_table));
7567 switch (volume_capabilities) {
7568 case TPACPI_VOL_CAP_AUTO:
7569 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7570 tp_features.mixer_no_level_control = 1;
7571 else if (quirks & TPACPI_VOL_Q_LEVEL)
7572 tp_features.mixer_no_level_control = 0;
7574 return 1; /* no mixer */
7576 case TPACPI_VOL_CAP_VOLMUTE:
7577 tp_features.mixer_no_level_control = 0;
7579 case TPACPI_VOL_CAP_MUTEONLY:
7580 tp_features.mixer_no_level_control = 1;
7586 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7587 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7588 "using user-supplied volume_capabilities=%d\n",
7589 volume_capabilities);
7591 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7592 volume_mode == TPACPI_VOL_MODE_MAX) {
7593 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7595 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7596 "driver auto-selected volume_mode=%d\n",
7599 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7600 "using user-supplied volume_mode=%d\n",
7604 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7605 "mute is supported, volume control is %s\n",
7606 str_supported(!tp_features.mixer_no_level_control));
7608 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7609 software_mute_active = true;
7611 rc = volume_create_alsa_mixer();
7613 pr_err("Could not create the ALSA mixer interface\n");
7617 pr_info("Console audio control enabled, mode: %s\n",
7618 (volume_control_allowed) ?
7619 "override (read/write)" :
7620 "monitor (read only)");
7623 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7624 "registering volume hotkeys as change notification\n");
7625 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7626 | TP_ACPI_HKEY_VOLUP_MASK
7627 | TP_ACPI_HKEY_VOLDWN_MASK
7628 | TP_ACPI_HKEY_MUTE_MASK);
7633 static int volume_read(struct seq_file *m)
7637 if (volume_get_status(&status) < 0) {
7638 seq_printf(m, "level:\t\tunreadable\n");
7640 if (tp_features.mixer_no_level_control)
7641 seq_printf(m, "level:\t\tunsupported\n");
7643 seq_printf(m, "level:\t\t%d\n",
7644 status & TP_EC_AUDIO_LVL_MSK);
7646 seq_printf(m, "mute:\t\t%s\n",
7647 onoff(status, TP_EC_AUDIO_MUTESW));
7649 if (volume_control_allowed) {
7650 seq_printf(m, "commands:\tunmute, mute\n");
7651 if (!tp_features.mixer_no_level_control) {
7652 seq_printf(m, "commands:\tup, down\n");
7653 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7662 static int volume_write(char *buf)
7665 u8 new_level, new_mute;
7671 * We do allow volume control at driver startup, so that the
7672 * user can set initial state through the volume=... parameter hack.
7674 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7675 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7676 tp_warned.volume_ctrl_forbidden = 1;
7677 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7678 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7683 rc = volume_get_status(&s);
7687 new_level = s & TP_EC_AUDIO_LVL_MSK;
7688 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7690 while ((cmd = next_cmd(&buf))) {
7691 if (!tp_features.mixer_no_level_control) {
7692 if (strlencmp(cmd, "up") == 0) {
7695 else if (new_level < TP_EC_VOLUME_MAX)
7698 } else if (strlencmp(cmd, "down") == 0) {
7701 else if (new_level > 0)
7704 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7705 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7710 if (strlencmp(cmd, "mute") == 0)
7711 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7712 else if (strlencmp(cmd, "unmute") == 0)
7718 if (tp_features.mixer_no_level_control) {
7719 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7720 new_mute ? "" : "un");
7721 rc = volume_set_mute(!!new_mute);
7723 tpacpi_disclose_usertask("procfs volume",
7724 "%smute and set level to %d\n",
7725 new_mute ? "" : "un", new_level);
7726 rc = volume_set_status(new_mute | new_level);
7728 volume_alsa_notify_change();
7730 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7733 static struct ibm_struct volume_driver_data = {
7735 .read = volume_read,
7736 .write = volume_write,
7737 .exit = volume_exit,
7738 .suspend = volume_suspend,
7739 .resume = volume_resume,
7740 .shutdown = volume_shutdown,
7743 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7745 #define alsa_card NULL
7747 static inline void volume_alsa_notify_change(void)
7751 static int __init volume_init(struct ibm_init_struct *iibm)
7753 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7758 static struct ibm_struct volume_driver_data = {
7762 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7764 /*************************************************************************
7771 * TPACPI_FAN_RD_ACPI_GFAN:
7772 * ACPI GFAN method: returns fan level
7774 * see TPACPI_FAN_WR_ACPI_SFAN
7775 * EC 0x2f (HFSP) not available if GFAN exists
7777 * TPACPI_FAN_WR_ACPI_SFAN:
7778 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7780 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7783 * TPACPI_FAN_WR_TPEC:
7784 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7785 * Supported on almost all ThinkPads
7787 * Fan speed changes of any sort (including those caused by the
7788 * disengaged mode) are usually done slowly by the firmware as the
7789 * maximum amount of fan duty cycle change per second seems to be
7792 * Reading is not available if GFAN exists.
7793 * Writing is not available if SFAN exists.
7796 * 7 automatic mode engaged;
7797 * (default operation mode of the ThinkPad)
7798 * fan level is ignored in this mode.
7799 * 6 full speed mode (takes precedence over bit 7);
7800 * not available on all thinkpads. May disable
7801 * the tachometer while the fan controller ramps up
7802 * the speed (which can take up to a few *minutes*).
7803 * Speeds up fan to 100% duty-cycle, which is far above
7804 * the standard RPM levels. It is not impossible that
7805 * it could cause hardware damage.
7806 * 5-3 unused in some models. Extra bits for fan level
7807 * in others, but still useless as all values above
7808 * 7 map to the same speed as level 7 in these models.
7809 * 2-0 fan level (0..7 usually)
7811 * 0x07 = max (set when temperatures critical)
7812 * Some ThinkPads may have other levels, see
7813 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7815 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7816 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7817 * does so, its initial value is meaningless (0x07).
7819 * For firmware bugs, refer to:
7820 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7824 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7825 * Main fan tachometer reading (in RPM)
7827 * This register is present on all ThinkPads with a new-style EC, and
7828 * it is known not to be present on the A21m/e, and T22, as there is
7829 * something else in offset 0x84 according to the ACPI DSDT. Other
7830 * ThinkPads from this same time period (and earlier) probably lack the
7831 * tachometer as well.
7833 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7834 * was never fixed by IBM to report the EC firmware version string
7835 * probably support the tachometer (like the early X models), so
7836 * detecting it is quite hard. We need more data to know for sure.
7838 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7841 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7844 * For firmware bugs, refer to:
7845 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7849 * ThinkPad EC register 0x31 bit 0 (only on select models)
7851 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7852 * show the speed of the main fan. When bit 0 of EC register 0x31
7853 * is one, the tachometer registers show the speed of the auxiliary
7856 * Fan control seems to affect both fans, regardless of the state
7859 * So far, only the firmware for the X60/X61 non-tablet versions
7860 * seem to support this (firmware TP-7M).
7862 * TPACPI_FAN_WR_ACPI_FANS:
7863 * ThinkPad X31, X40, X41. Not available in the X60.
7865 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7866 * high speed. ACPI DSDT seems to map these three speeds to levels
7867 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7868 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7870 * The speeds are stored on handles
7871 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7873 * There are three default speed sets, accessible as handles:
7874 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7876 * ACPI DSDT switches which set is in use depending on various
7879 * TPACPI_FAN_WR_TPEC is also available and should be used to
7880 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7881 * but the ACPI tables just mention level 7.
7884 enum { /* Fan control constants */
7885 fan_status_offset = 0x2f, /* EC register 0x2f */
7886 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7887 * 0x84 must be read before 0x85 */
7888 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7889 bit 0 selects which fan is active */
7891 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7892 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7894 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7897 enum fan_status_access_mode {
7898 TPACPI_FAN_NONE = 0, /* No fan status or control */
7899 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7900 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7903 enum fan_control_access_mode {
7904 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7905 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7906 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7907 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7910 enum fan_control_commands {
7911 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7912 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7913 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7914 * and also watchdog cmd */
7917 static bool fan_control_allowed;
7919 static enum fan_status_access_mode fan_status_access_mode;
7920 static enum fan_control_access_mode fan_control_access_mode;
7921 static enum fan_control_commands fan_control_commands;
7923 static u8 fan_control_initial_status;
7924 static u8 fan_control_desired_level;
7925 static u8 fan_control_resume_level;
7926 static int fan_watchdog_maxinterval;
7928 static struct mutex fan_mutex;
7930 static void fan_watchdog_fire(struct work_struct *ignored);
7931 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7933 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7934 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7935 "\\FSPD", /* 600e/x, 770e, 770x */
7937 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7938 "JFNS", /* 770x-JL */
7942 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7943 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7944 * be in auto mode (0x80).
7946 * This is corrected by any write to HFSP either by the driver, or
7949 * We assume 0x07 really means auto mode while this quirk is active,
7950 * as this is far more likely than the ThinkPad being in level 7,
7951 * which is only used by the firmware during thermal emergencies.
7953 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7954 * TP-70 (T43, R52), which are known to be buggy.
7957 static void fan_quirk1_setup(void)
7959 if (fan_control_initial_status == 0x07) {
7960 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
7961 tp_features.fan_ctrl_status_undef = 1;
7965 static void fan_quirk1_handle(u8 *fan_status)
7967 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7968 if (*fan_status != fan_control_initial_status) {
7969 /* something changed the HFSP regisnter since
7970 * driver init time, so it is not undefined
7972 tp_features.fan_ctrl_status_undef = 0;
7974 /* Return most likely status. In fact, it
7975 * might be the only possible status */
7976 *fan_status = TP_EC_FAN_AUTO;
7981 /* Select main fan on X60/X61, NOOP on others */
7982 static bool fan_select_fan1(void)
7984 if (tp_features.second_fan) {
7987 if (ec_read(fan_select_offset, &val) < 0)
7990 if (ec_write(fan_select_offset, val) < 0)
7996 /* Select secondary fan on X60/X61 */
7997 static bool fan_select_fan2(void)
8001 if (!tp_features.second_fan)
8004 if (ec_read(fan_select_offset, &val) < 0)
8007 if (ec_write(fan_select_offset, val) < 0)
8014 * Call with fan_mutex held
8016 static void fan_update_desired_level(u8 status)
8019 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8021 fan_control_desired_level = 7;
8023 fan_control_desired_level = status;
8027 static int fan_get_status(u8 *status)
8032 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8034 switch (fan_status_access_mode) {
8035 case TPACPI_FAN_RD_ACPI_GFAN: {
8036 /* 570, 600e/x, 770e, 770x */
8039 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8043 *status = res & 0x07;
8047 case TPACPI_FAN_RD_TPEC:
8048 /* all except 570, 600e/x, 770e, 770x */
8049 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8052 if (likely(status)) {
8054 fan_quirk1_handle(status);
8066 static int fan_get_status_safe(u8 *status)
8071 if (mutex_lock_killable(&fan_mutex))
8072 return -ERESTARTSYS;
8073 rc = fan_get_status(&s);
8075 fan_update_desired_level(s);
8076 mutex_unlock(&fan_mutex);
8086 static int fan_get_speed(unsigned int *speed)
8090 switch (fan_status_access_mode) {
8091 case TPACPI_FAN_RD_TPEC:
8092 /* all except 570, 600e/x, 770e, 770x */
8093 if (unlikely(!fan_select_fan1()))
8095 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8096 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8100 *speed = (hi << 8) | lo;
8111 static int fan2_get_speed(unsigned int *speed)
8116 switch (fan_status_access_mode) {
8117 case TPACPI_FAN_RD_TPEC:
8118 /* all except 570, 600e/x, 770e, 770x */
8119 if (unlikely(!fan_select_fan2()))
8121 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8122 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8123 fan_select_fan1(); /* play it safe */
8128 *speed = (hi << 8) | lo;
8139 static int fan_set_level(int level)
8141 if (!fan_control_allowed)
8144 switch (fan_control_access_mode) {
8145 case TPACPI_FAN_WR_ACPI_SFAN:
8146 if (level >= 0 && level <= 7) {
8147 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8153 case TPACPI_FAN_WR_ACPI_FANS:
8154 case TPACPI_FAN_WR_TPEC:
8155 if (!(level & TP_EC_FAN_AUTO) &&
8156 !(level & TP_EC_FAN_FULLSPEED) &&
8157 ((level < 0) || (level > 7)))
8160 /* safety net should the EC not support AUTO
8161 * or FULLSPEED mode bits and just ignore them */
8162 if (level & TP_EC_FAN_FULLSPEED)
8163 level |= 7; /* safety min speed 7 */
8164 else if (level & TP_EC_FAN_AUTO)
8165 level |= 4; /* safety min speed 4 */
8167 if (!acpi_ec_write(fan_status_offset, level))
8170 tp_features.fan_ctrl_status_undef = 0;
8177 vdbg_printk(TPACPI_DBG_FAN,
8178 "fan control: set fan control register to 0x%02x\n", level);
8182 static int fan_set_level_safe(int level)
8186 if (!fan_control_allowed)
8189 if (mutex_lock_killable(&fan_mutex))
8190 return -ERESTARTSYS;
8192 if (level == TPACPI_FAN_LAST_LEVEL)
8193 level = fan_control_desired_level;
8195 rc = fan_set_level(level);
8197 fan_update_desired_level(level);
8199 mutex_unlock(&fan_mutex);
8203 static int fan_set_enable(void)
8208 if (!fan_control_allowed)
8211 if (mutex_lock_killable(&fan_mutex))
8212 return -ERESTARTSYS;
8214 switch (fan_control_access_mode) {
8215 case TPACPI_FAN_WR_ACPI_FANS:
8216 case TPACPI_FAN_WR_TPEC:
8217 rc = fan_get_status(&s);
8221 /* Don't go out of emergency fan mode */
8224 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8227 if (!acpi_ec_write(fan_status_offset, s))
8230 tp_features.fan_ctrl_status_undef = 0;
8235 case TPACPI_FAN_WR_ACPI_SFAN:
8236 rc = fan_get_status(&s);
8242 /* Set fan to at least level 4 */
8245 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8255 mutex_unlock(&fan_mutex);
8258 vdbg_printk(TPACPI_DBG_FAN,
8259 "fan control: set fan control register to 0x%02x\n",
8264 static int fan_set_disable(void)
8268 if (!fan_control_allowed)
8271 if (mutex_lock_killable(&fan_mutex))
8272 return -ERESTARTSYS;
8275 switch (fan_control_access_mode) {
8276 case TPACPI_FAN_WR_ACPI_FANS:
8277 case TPACPI_FAN_WR_TPEC:
8278 if (!acpi_ec_write(fan_status_offset, 0x00))
8281 fan_control_desired_level = 0;
8282 tp_features.fan_ctrl_status_undef = 0;
8286 case TPACPI_FAN_WR_ACPI_SFAN:
8287 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8290 fan_control_desired_level = 0;
8298 vdbg_printk(TPACPI_DBG_FAN,
8299 "fan control: set fan control register to 0\n");
8301 mutex_unlock(&fan_mutex);
8305 static int fan_set_speed(int speed)
8309 if (!fan_control_allowed)
8312 if (mutex_lock_killable(&fan_mutex))
8313 return -ERESTARTSYS;
8316 switch (fan_control_access_mode) {
8317 case TPACPI_FAN_WR_ACPI_FANS:
8318 if (speed >= 0 && speed <= 65535) {
8319 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8320 speed, speed, speed))
8330 mutex_unlock(&fan_mutex);
8334 static void fan_watchdog_reset(void)
8336 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8339 if (fan_watchdog_maxinterval > 0 &&
8340 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8341 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8342 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8344 cancel_delayed_work(&fan_watchdog_task);
8347 static void fan_watchdog_fire(struct work_struct *ignored)
8351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8354 pr_notice("fan watchdog: enabling fan\n");
8355 rc = fan_set_enable();
8357 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8359 /* reschedule for later */
8360 fan_watchdog_reset();
8365 * SYSFS fan layout: hwmon compatible (device)
8368 * 0: "disengaged" mode
8370 * 2: native EC "auto" mode (recommended, hardware default)
8372 * pwm*: set speed in manual mode, ignored otherwise.
8373 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8376 * fan*_input: tachometer reading, RPM
8379 * SYSFS fan layout: extensions
8381 * fan_watchdog (driver):
8382 * fan watchdog interval in seconds, 0 disables (default), max 120
8385 /* sysfs fan pwm1_enable ----------------------------------------------- */
8386 static ssize_t fan_pwm1_enable_show(struct device *dev,
8387 struct device_attribute *attr,
8393 res = fan_get_status_safe(&status);
8397 if (status & TP_EC_FAN_FULLSPEED) {
8399 } else if (status & TP_EC_FAN_AUTO) {
8404 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8407 static ssize_t fan_pwm1_enable_store(struct device *dev,
8408 struct device_attribute *attr,
8409 const char *buf, size_t count)
8414 if (parse_strtoul(buf, 2, &t))
8417 tpacpi_disclose_usertask("hwmon pwm1_enable",
8418 "set fan mode to %lu\n", t);
8422 level = TP_EC_FAN_FULLSPEED;
8425 level = TPACPI_FAN_LAST_LEVEL;
8428 level = TP_EC_FAN_AUTO;
8431 /* reserved for software-controlled auto mode */
8437 res = fan_set_level_safe(level);
8443 fan_watchdog_reset();
8448 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8449 fan_pwm1_enable_show, fan_pwm1_enable_store);
8451 /* sysfs fan pwm1 ------------------------------------------------------ */
8452 static ssize_t fan_pwm1_show(struct device *dev,
8453 struct device_attribute *attr,
8459 res = fan_get_status_safe(&status);
8464 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8465 status = fan_control_desired_level;
8470 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8473 static ssize_t fan_pwm1_store(struct device *dev,
8474 struct device_attribute *attr,
8475 const char *buf, size_t count)
8479 u8 status, newlevel;
8481 if (parse_strtoul(buf, 255, &s))
8484 tpacpi_disclose_usertask("hwmon pwm1",
8485 "set fan speed to %lu\n", s);
8487 /* scale down from 0-255 to 0-7 */
8488 newlevel = (s >> 5) & 0x07;
8490 if (mutex_lock_killable(&fan_mutex))
8491 return -ERESTARTSYS;
8493 rc = fan_get_status(&status);
8494 if (!rc && (status &
8495 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8496 rc = fan_set_level(newlevel);
8500 fan_update_desired_level(newlevel);
8501 fan_watchdog_reset();
8505 mutex_unlock(&fan_mutex);
8506 return (rc) ? rc : count;
8509 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8511 /* sysfs fan fan1_input ------------------------------------------------ */
8512 static ssize_t fan_fan1_input_show(struct device *dev,
8513 struct device_attribute *attr,
8519 res = fan_get_speed(&speed);
8523 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8526 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8528 /* sysfs fan fan2_input ------------------------------------------------ */
8529 static ssize_t fan_fan2_input_show(struct device *dev,
8530 struct device_attribute *attr,
8536 res = fan2_get_speed(&speed);
8540 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8543 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8545 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8546 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8548 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8551 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8556 if (parse_strtoul(buf, 120, &t))
8559 if (!fan_control_allowed)
8562 fan_watchdog_maxinterval = t;
8563 fan_watchdog_reset();
8565 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8569 static DRIVER_ATTR_RW(fan_watchdog);
8571 /* --------------------------------------------------------------------- */
8572 static struct attribute *fan_attributes[] = {
8573 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8574 &dev_attr_fan1_input.attr,
8575 NULL, /* for fan2_input */
8579 static const struct attribute_group fan_attr_group = {
8580 .attrs = fan_attributes,
8583 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8584 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8586 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8587 { .vendor = PCI_VENDOR_ID_IBM, \
8588 .bios = TPACPI_MATCH_ANY, \
8589 .ec = TPID(__id1, __id2), \
8590 .quirks = __quirks }
8592 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8593 { .vendor = PCI_VENDOR_ID_LENOVO, \
8594 .bios = TPACPI_MATCH_ANY, \
8595 .ec = TPID(__id1, __id2), \
8596 .quirks = __quirks }
8598 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8599 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8600 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8601 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8602 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8603 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8606 #undef TPACPI_FAN_QL
8607 #undef TPACPI_FAN_QI
8609 static int __init fan_init(struct ibm_init_struct *iibm)
8612 unsigned long quirks;
8614 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8615 "initializing fan subdriver\n");
8617 mutex_init(&fan_mutex);
8618 fan_status_access_mode = TPACPI_FAN_NONE;
8619 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8620 fan_control_commands = 0;
8621 fan_watchdog_maxinterval = 0;
8622 tp_features.fan_ctrl_status_undef = 0;
8623 tp_features.second_fan = 0;
8624 fan_control_desired_level = 7;
8626 if (tpacpi_is_ibm()) {
8627 TPACPI_ACPIHANDLE_INIT(fans);
8628 TPACPI_ACPIHANDLE_INIT(gfan);
8629 TPACPI_ACPIHANDLE_INIT(sfan);
8632 quirks = tpacpi_check_quirks(fan_quirk_table,
8633 ARRAY_SIZE(fan_quirk_table));
8636 /* 570, 600e/x, 770e, 770x */
8637 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8639 /* all other ThinkPads: note that even old-style
8640 * ThinkPad ECs supports the fan control register */
8641 if (likely(acpi_ec_read(fan_status_offset,
8642 &fan_control_initial_status))) {
8643 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8644 if (quirks & TPACPI_FAN_Q1)
8646 if (quirks & TPACPI_FAN_2FAN) {
8647 tp_features.second_fan = 1;
8648 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8649 "secondary fan support enabled\n");
8652 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8659 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8660 fan_control_commands |=
8661 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8664 /* gfan without sfan means no fan control */
8665 /* all other models implement TP EC 0x2f control */
8669 fan_control_access_mode =
8670 TPACPI_FAN_WR_ACPI_FANS;
8671 fan_control_commands |=
8672 TPACPI_FAN_CMD_SPEED |
8673 TPACPI_FAN_CMD_LEVEL |
8674 TPACPI_FAN_CMD_ENABLE;
8676 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8677 fan_control_commands |=
8678 TPACPI_FAN_CMD_LEVEL |
8679 TPACPI_FAN_CMD_ENABLE;
8684 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8685 "fan is %s, modes %d, %d\n",
8686 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8687 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8688 fan_status_access_mode, fan_control_access_mode);
8690 /* fan control master switch */
8691 if (!fan_control_allowed) {
8692 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8693 fan_control_commands = 0;
8694 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8695 "fan control features disabled by parameter\n");
8698 /* update fan_control_desired_level */
8699 if (fan_status_access_mode != TPACPI_FAN_NONE)
8700 fan_get_status_safe(NULL);
8702 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8703 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8704 if (tp_features.second_fan) {
8705 /* attach second fan tachometer */
8706 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8707 &dev_attr_fan2_input.attr;
8709 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8714 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8715 &driver_attr_fan_watchdog);
8717 sysfs_remove_group(&tpacpi_hwmon->kobj,
8726 static void fan_exit(void)
8728 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8729 "cancelling any pending fan watchdog tasks\n");
8731 /* FIXME: can we really do this unconditionally? */
8732 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8733 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8734 &driver_attr_fan_watchdog);
8736 cancel_delayed_work(&fan_watchdog_task);
8737 flush_workqueue(tpacpi_wq);
8740 static void fan_suspend(void)
8744 if (!fan_control_allowed)
8747 /* Store fan status in cache */
8748 fan_control_resume_level = 0;
8749 rc = fan_get_status_safe(&fan_control_resume_level);
8751 pr_notice("failed to read fan level for later restore during resume: %d\n",
8754 /* if it is undefined, don't attempt to restore it.
8756 if (tp_features.fan_ctrl_status_undef)
8757 fan_control_resume_level = 0;
8760 static void fan_resume(void)
8762 u8 current_level = 7;
8763 bool do_set = false;
8766 /* DSDT *always* updates status on resume */
8767 tp_features.fan_ctrl_status_undef = 0;
8769 if (!fan_control_allowed ||
8770 !fan_control_resume_level ||
8771 (fan_get_status_safe(¤t_level) < 0))
8774 switch (fan_control_access_mode) {
8775 case TPACPI_FAN_WR_ACPI_SFAN:
8776 /* never decrease fan level */
8777 do_set = (fan_control_resume_level > current_level);
8779 case TPACPI_FAN_WR_ACPI_FANS:
8780 case TPACPI_FAN_WR_TPEC:
8781 /* never decrease fan level, scale is:
8782 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8784 * We expect the firmware to set either 7 or AUTO, but we
8785 * handle FULLSPEED out of paranoia.
8787 * So, we can safely only restore FULLSPEED or 7, anything
8788 * else could slow the fan. Restoring AUTO is useless, at
8789 * best that's exactly what the DSDT already set (it is the
8792 * Always keep in mind that the DSDT *will* have set the
8793 * fans to what the vendor supposes is the best level. We
8794 * muck with it only to speed the fan up.
8796 if (fan_control_resume_level != 7 &&
8797 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8800 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8801 (current_level != fan_control_resume_level);
8807 pr_notice("restoring fan level to 0x%02x\n",
8808 fan_control_resume_level);
8809 rc = fan_set_level_safe(fan_control_resume_level);
8811 pr_notice("failed to restore fan level: %d\n", rc);
8815 static int fan_read(struct seq_file *m)
8819 unsigned int speed = 0;
8821 switch (fan_status_access_mode) {
8822 case TPACPI_FAN_RD_ACPI_GFAN:
8823 /* 570, 600e/x, 770e, 770x */
8824 rc = fan_get_status_safe(&status);
8828 seq_printf(m, "status:\t\t%s\n"
8830 (status != 0) ? "enabled" : "disabled", status);
8833 case TPACPI_FAN_RD_TPEC:
8834 /* all except 570, 600e/x, 770e, 770x */
8835 rc = fan_get_status_safe(&status);
8839 seq_printf(m, "status:\t\t%s\n",
8840 (status != 0) ? "enabled" : "disabled");
8842 rc = fan_get_speed(&speed);
8846 seq_printf(m, "speed:\t\t%d\n", speed);
8848 if (status & TP_EC_FAN_FULLSPEED)
8849 /* Disengaged mode takes precedence */
8850 seq_printf(m, "level:\t\tdisengaged\n");
8851 else if (status & TP_EC_FAN_AUTO)
8852 seq_printf(m, "level:\t\tauto\n");
8854 seq_printf(m, "level:\t\t%d\n", status);
8857 case TPACPI_FAN_NONE:
8859 seq_printf(m, "status:\t\tnot supported\n");
8862 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8863 seq_printf(m, "commands:\tlevel <level>");
8865 switch (fan_control_access_mode) {
8866 case TPACPI_FAN_WR_ACPI_SFAN:
8867 seq_printf(m, " (<level> is 0-7)\n");
8871 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8876 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8877 seq_printf(m, "commands:\tenable, disable\n"
8878 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8880 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8881 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8886 static int fan_write_cmd_level(const char *cmd, int *rc)
8890 if (strlencmp(cmd, "level auto") == 0)
8891 level = TP_EC_FAN_AUTO;
8892 else if ((strlencmp(cmd, "level disengaged") == 0) |
8893 (strlencmp(cmd, "level full-speed") == 0))
8894 level = TP_EC_FAN_FULLSPEED;
8895 else if (sscanf(cmd, "level %d", &level) != 1)
8898 *rc = fan_set_level_safe(level);
8900 pr_err("level command accepted for unsupported access mode %d\n",
8901 fan_control_access_mode);
8903 tpacpi_disclose_usertask("procfs fan",
8904 "set level to %d\n", level);
8909 static int fan_write_cmd_enable(const char *cmd, int *rc)
8911 if (strlencmp(cmd, "enable") != 0)
8914 *rc = fan_set_enable();
8916 pr_err("enable command accepted for unsupported access mode %d\n",
8917 fan_control_access_mode);
8919 tpacpi_disclose_usertask("procfs fan", "enable\n");
8924 static int fan_write_cmd_disable(const char *cmd, int *rc)
8926 if (strlencmp(cmd, "disable") != 0)
8929 *rc = fan_set_disable();
8931 pr_err("disable command accepted for unsupported access mode %d\n",
8932 fan_control_access_mode);
8934 tpacpi_disclose_usertask("procfs fan", "disable\n");
8939 static int fan_write_cmd_speed(const char *cmd, int *rc)
8944 * Support speed <low> <medium> <high> ? */
8946 if (sscanf(cmd, "speed %d", &speed) != 1)
8949 *rc = fan_set_speed(speed);
8951 pr_err("speed command accepted for unsupported access mode %d\n",
8952 fan_control_access_mode);
8954 tpacpi_disclose_usertask("procfs fan",
8955 "set speed to %d\n", speed);
8960 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8964 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8967 if (interval < 0 || interval > 120)
8970 fan_watchdog_maxinterval = interval;
8971 tpacpi_disclose_usertask("procfs fan",
8972 "set watchdog timer to %d\n",
8979 static int fan_write(char *buf)
8984 while (!rc && (cmd = next_cmd(&buf))) {
8985 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8986 fan_write_cmd_level(cmd, &rc)) &&
8987 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8988 (fan_write_cmd_enable(cmd, &rc) ||
8989 fan_write_cmd_disable(cmd, &rc) ||
8990 fan_write_cmd_watchdog(cmd, &rc))) &&
8991 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8992 fan_write_cmd_speed(cmd, &rc))
8996 fan_watchdog_reset();
9002 static struct ibm_struct fan_driver_data = {
9007 .suspend = fan_suspend,
9008 .resume = fan_resume,
9011 /*************************************************************************
9012 * Mute LED subdriver
9016 struct tp_led_table {
9023 static struct tp_led_table led_tables[] = {
9024 [TPACPI_LED_MUTE] = {
9029 [TPACPI_LED_MICMUTE] = {
9036 static int mute_led_on_off(struct tp_led_table *t, bool state)
9041 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9042 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9046 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9047 state ? t->on_value : t->off_value))
9054 int tpacpi_led_set(int whichled, bool on)
9056 struct tp_led_table *t;
9058 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9061 t = &led_tables[whichled];
9062 if (t->state < 0 || t->state == on)
9064 return mute_led_on_off(t, on);
9066 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9068 static int mute_led_init(struct ibm_init_struct *iibm)
9073 for (i = 0; i < TPACPI_LED_MAX; i++) {
9074 struct tp_led_table *t = &led_tables[i];
9075 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9076 mute_led_on_off(t, false);
9083 static void mute_led_exit(void)
9087 for (i = 0; i < TPACPI_LED_MAX; i++)
9088 tpacpi_led_set(i, false);
9091 static void mute_led_resume(void)
9095 for (i = 0; i < TPACPI_LED_MAX; i++) {
9096 struct tp_led_table *t = &led_tables[i];
9098 mute_led_on_off(t, t->state);
9102 static struct ibm_struct mute_led_driver_data = {
9104 .exit = mute_led_exit,
9105 .resume = mute_led_resume,
9108 /****************************************************************************
9109 ****************************************************************************
9113 ****************************************************************************
9114 ****************************************************************************/
9117 * HKEY event callout for other subdrivers go here
9118 * (yes, it is ugly, but it is quick, safe, and gets the job done
9120 static void tpacpi_driver_event(const unsigned int hkey_event)
9122 if (ibm_backlight_device) {
9123 switch (hkey_event) {
9124 case TP_HKEY_EV_BRGHT_UP:
9125 case TP_HKEY_EV_BRGHT_DOWN:
9126 tpacpi_brightness_notify_change();
9130 switch (hkey_event) {
9131 case TP_HKEY_EV_VOL_UP:
9132 case TP_HKEY_EV_VOL_DOWN:
9133 case TP_HKEY_EV_VOL_MUTE:
9134 volume_alsa_notify_change();
9137 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9138 enum led_brightness brightness;
9140 mutex_lock(&kbdlight_mutex);
9143 * Check the brightness actually changed, setting the brightness
9144 * through kbdlight_set_level() also triggers this event.
9146 brightness = kbdlight_sysfs_get(NULL);
9147 if (kbdlight_brightness != brightness) {
9148 kbdlight_brightness = brightness;
9149 led_classdev_notify_brightness_hw_changed(
9150 &tpacpi_led_kbdlight.led_classdev, brightness);
9153 mutex_unlock(&kbdlight_mutex);
9157 static void hotkey_driver_event(const unsigned int scancode)
9159 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9162 /* --------------------------------------------------------------------- */
9165 static struct proc_dir_entry *proc_dir;
9168 * Module and infrastructure proble, init and exit handling
9171 static bool force_load;
9173 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9174 static const char * __init str_supported(int is_supported)
9176 static char text_unsupported[] __initdata = "not supported";
9178 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9180 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9182 static void ibm_exit(struct ibm_struct *ibm)
9184 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9186 list_del_init(&ibm->all_drivers);
9188 if (ibm->flags.acpi_notify_installed) {
9189 dbg_printk(TPACPI_DBG_EXIT,
9190 "%s: acpi_remove_notify_handler\n", ibm->name);
9192 acpi_remove_notify_handler(*ibm->acpi->handle,
9194 dispatch_acpi_notify);
9195 ibm->flags.acpi_notify_installed = 0;
9198 if (ibm->flags.proc_created) {
9199 dbg_printk(TPACPI_DBG_EXIT,
9200 "%s: remove_proc_entry\n", ibm->name);
9201 remove_proc_entry(ibm->name, proc_dir);
9202 ibm->flags.proc_created = 0;
9205 if (ibm->flags.acpi_driver_registered) {
9206 dbg_printk(TPACPI_DBG_EXIT,
9207 "%s: acpi_bus_unregister_driver\n", ibm->name);
9209 acpi_bus_unregister_driver(ibm->acpi->driver);
9210 kfree(ibm->acpi->driver);
9211 ibm->acpi->driver = NULL;
9212 ibm->flags.acpi_driver_registered = 0;
9215 if (ibm->flags.init_called && ibm->exit) {
9217 ibm->flags.init_called = 0;
9220 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9223 static int __init ibm_init(struct ibm_init_struct *iibm)
9226 struct ibm_struct *ibm = iibm->data;
9227 struct proc_dir_entry *entry;
9229 BUG_ON(ibm == NULL);
9231 INIT_LIST_HEAD(&ibm->all_drivers);
9233 if (ibm->flags.experimental && !experimental)
9236 dbg_printk(TPACPI_DBG_INIT,
9237 "probing for %s\n", ibm->name);
9240 ret = iibm->init(iibm);
9242 return 0; /* probe failed */
9246 ibm->flags.init_called = 1;
9250 if (ibm->acpi->hid) {
9251 ret = register_tpacpi_subdriver(ibm);
9256 if (ibm->acpi->notify) {
9257 ret = setup_acpi_notify(ibm);
9258 if (ret == -ENODEV) {
9259 pr_notice("disabling subdriver %s\n",
9269 dbg_printk(TPACPI_DBG_INIT,
9270 "%s installed\n", ibm->name);
9273 umode_t mode = iibm->base_procfs_mode;
9279 entry = proc_create_data(ibm->name, mode, proc_dir,
9280 &dispatch_proc_fops, ibm);
9282 pr_err("unable to create proc entry %s\n", ibm->name);
9286 ibm->flags.proc_created = 1;
9289 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9294 dbg_printk(TPACPI_DBG_INIT,
9295 "%s: at error exit path with result %d\n",
9299 return (ret < 0) ? ret : 0;
9304 static bool __pure __init tpacpi_is_fw_digit(const char c)
9306 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9309 static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
9313 * Most models: xxyTkkWW (#.##c)
9314 * Ancient 570/600 and -SL lacks (#.##c)
9316 if (s && strlen(s) >= 8 &&
9317 tpacpi_is_fw_digit(s[0]) &&
9318 tpacpi_is_fw_digit(s[1]) &&
9320 (s[3] == 'T' || s[3] == 'N') &&
9321 tpacpi_is_fw_digit(s[4]) &&
9322 tpacpi_is_fw_digit(s[5]))
9325 /* New models: xxxyTkkW (#.##c); T550 and some others */
9326 return s && strlen(s) >= 8 &&
9327 tpacpi_is_fw_digit(s[0]) &&
9328 tpacpi_is_fw_digit(s[1]) &&
9329 tpacpi_is_fw_digit(s[2]) &&
9331 (s[4] == 'T' || s[4] == 'N') &&
9332 tpacpi_is_fw_digit(s[5]) &&
9333 tpacpi_is_fw_digit(s[6]);
9336 /* returns 0 - probe ok, or < 0 - probe error.
9337 * Probe ok doesn't mean thinkpad found.
9338 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9339 static int __must_check __init get_thinkpad_model_data(
9340 struct thinkpad_id_data *tp)
9342 const struct dmi_device *dev = NULL;
9343 char ec_fw_string[18];
9349 memset(tp, 0, sizeof(*tp));
9351 if (dmi_name_in_vendors("IBM"))
9352 tp->vendor = PCI_VENDOR_ID_IBM;
9353 else if (dmi_name_in_vendors("LENOVO"))
9354 tp->vendor = PCI_VENDOR_ID_LENOVO;
9358 s = dmi_get_system_info(DMI_BIOS_VERSION);
9359 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9360 if (s && !tp->bios_version_str)
9363 /* Really ancient ThinkPad 240X will fail this, which is fine */
9364 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9365 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
9368 tp->bios_model = tp->bios_version_str[0]
9369 | (tp->bios_version_str[1] << 8);
9370 tp->bios_release = (tp->bios_version_str[4] << 8)
9371 | tp->bios_version_str[5];
9374 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9375 * X32 or newer, all Z series; Some models must have an
9376 * up-to-date BIOS or they will not be detected.
9378 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9380 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9381 if (sscanf(dev->name,
9382 "IBM ThinkPad Embedded Controller -[%17c",
9383 ec_fw_string) == 1) {
9384 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9385 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
9387 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
9388 if (!tp->ec_version_str)
9391 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9392 tp->ec_model = ec_fw_string[0]
9393 | (ec_fw_string[1] << 8);
9394 tp->ec_release = (ec_fw_string[4] << 8)
9397 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
9399 pr_notice("please report this to %s\n",
9406 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
9407 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
9408 tp->model_str = kstrdup(s, GFP_KERNEL);
9412 s = dmi_get_system_info(DMI_BIOS_VENDOR);
9413 if (s && !(strncasecmp(s, "Lenovo", 6))) {
9414 tp->model_str = kstrdup(s, GFP_KERNEL);
9420 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9421 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9422 if (s && !tp->nummodel_str)
9428 static int __init probe_for_thinkpad(void)
9435 /* It would be dangerous to run the driver in this case */
9436 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9440 * Non-ancient models have better DMI tagging, but very old models
9441 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9443 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9444 (thinkpad_id.ec_model != 0) ||
9445 tpacpi_is_fw_known();
9447 /* The EC handler is required */
9448 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
9451 pr_err("Not yet supported ThinkPad detected!\n");
9455 if (!is_thinkpad && !force_load)
9461 static void __init thinkpad_acpi_init_banner(void)
9463 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9464 pr_info("%s\n", TPACPI_URL);
9466 pr_info("ThinkPad BIOS %s, EC %s\n",
9467 (thinkpad_id.bios_version_str) ?
9468 thinkpad_id.bios_version_str : "unknown",
9469 (thinkpad_id.ec_version_str) ?
9470 thinkpad_id.ec_version_str : "unknown");
9472 BUG_ON(!thinkpad_id.vendor);
9474 if (thinkpad_id.model_str)
9475 pr_info("%s %s, model %s\n",
9476 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9477 "IBM" : ((thinkpad_id.vendor ==
9478 PCI_VENDOR_ID_LENOVO) ?
9479 "Lenovo" : "Unknown vendor"),
9480 thinkpad_id.model_str,
9481 (thinkpad_id.nummodel_str) ?
9482 thinkpad_id.nummodel_str : "unknown");
9485 /* Module init, exit, parameters */
9487 static struct ibm_init_struct ibms_init[] __initdata = {
9489 .data = &thinkpad_acpi_driver_data,
9492 .init = hotkey_init,
9493 .data = &hotkey_driver_data,
9496 .init = bluetooth_init,
9497 .data = &bluetooth_driver_data,
9501 .data = &wan_driver_data,
9505 .data = &uwb_driver_data,
9507 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9510 .base_procfs_mode = S_IRUSR,
9511 .data = &video_driver_data,
9515 .init = kbdlight_init,
9516 .data = &kbdlight_driver_data,
9520 .data = &light_driver_data,
9524 .data = &cmos_driver_data,
9528 .data = &led_driver_data,
9532 .data = &beep_driver_data,
9535 .init = thermal_init,
9536 .data = &thermal_driver_data,
9539 .init = brightness_init,
9540 .data = &brightness_driver_data,
9543 .init = volume_init,
9544 .data = &volume_driver_data,
9548 .data = &fan_driver_data,
9551 .init = mute_led_init,
9552 .data = &mute_led_driver_data,
9556 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9559 struct ibm_struct *ibm;
9561 if (!kp || !kp->name || !val)
9564 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9565 ibm = ibms_init[i].data;
9566 WARN_ON(ibm == NULL);
9568 if (!ibm || !ibm->name)
9571 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9572 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
9574 strcpy(ibms_init[i].param, val);
9575 strcat(ibms_init[i].param, ",");
9583 module_param(experimental, int, 0444);
9584 MODULE_PARM_DESC(experimental,
9585 "Enables experimental features when non-zero");
9587 module_param_named(debug, dbg_level, uint, 0);
9588 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
9590 module_param(force_load, bool, 0444);
9591 MODULE_PARM_DESC(force_load,
9592 "Attempts to load the driver even on a mis-identified ThinkPad when true");
9594 module_param_named(fan_control, fan_control_allowed, bool, 0444);
9595 MODULE_PARM_DESC(fan_control,
9596 "Enables setting fan parameters features when true");
9598 module_param_named(brightness_mode, brightness_mode, uint, 0444);
9599 MODULE_PARM_DESC(brightness_mode,
9600 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9602 module_param(brightness_enable, uint, 0444);
9603 MODULE_PARM_DESC(brightness_enable,
9604 "Enables backlight control when 1, disables when 0");
9606 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9607 module_param_named(volume_mode, volume_mode, uint, 0444);
9608 MODULE_PARM_DESC(volume_mode,
9609 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9611 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9612 MODULE_PARM_DESC(volume_capabilities,
9613 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
9615 module_param_named(volume_control, volume_control_allowed, bool, 0444);
9616 MODULE_PARM_DESC(volume_control,
9617 "Enables software override for the console audio control when true");
9619 module_param_named(software_mute, software_mute_requested, bool, 0444);
9620 MODULE_PARM_DESC(software_mute,
9621 "Request full software mute control");
9623 /* ALSA module API parameters */
9624 module_param_named(index, alsa_index, int, 0444);
9625 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9626 module_param_named(id, alsa_id, charp, 0444);
9627 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9628 module_param_named(enable, alsa_enable, bool, 0444);
9629 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
9630 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9632 /* The module parameter can't be read back, that's why 0 is used here */
9633 #define TPACPI_PARAM(feature) \
9634 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9635 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
9637 TPACPI_PARAM(hotkey);
9638 TPACPI_PARAM(bluetooth);
9639 TPACPI_PARAM(video);
9640 TPACPI_PARAM(light);
9644 TPACPI_PARAM(brightness);
9645 TPACPI_PARAM(volume);
9648 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9649 module_param(dbg_wlswemul, uint, 0444);
9650 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9651 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9652 MODULE_PARM_DESC(wlsw_state,
9653 "Initial state of the emulated WLSW switch");
9655 module_param(dbg_bluetoothemul, uint, 0444);
9656 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9657 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9658 MODULE_PARM_DESC(bluetooth_state,
9659 "Initial state of the emulated bluetooth switch");
9661 module_param(dbg_wwanemul, uint, 0444);
9662 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9663 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9664 MODULE_PARM_DESC(wwan_state,
9665 "Initial state of the emulated WWAN switch");
9667 module_param(dbg_uwbemul, uint, 0444);
9668 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9669 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9670 MODULE_PARM_DESC(uwb_state,
9671 "Initial state of the emulated UWB switch");
9674 static void thinkpad_acpi_module_exit(void)
9676 struct ibm_struct *ibm, *itmp;
9678 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9680 list_for_each_entry_safe_reverse(ibm, itmp,
9681 &tpacpi_all_drivers,
9686 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9688 if (tpacpi_inputdev) {
9689 if (tp_features.input_device_registered)
9690 input_unregister_device(tpacpi_inputdev);
9692 input_free_device(tpacpi_inputdev);
9693 kfree(hotkey_keycode_map);
9697 hwmon_device_unregister(tpacpi_hwmon);
9699 if (tpacpi_sensors_pdev)
9700 platform_device_unregister(tpacpi_sensors_pdev);
9702 platform_device_unregister(tpacpi_pdev);
9704 if (tp_features.sensors_pdrv_attrs_registered)
9705 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9706 if (tp_features.platform_drv_attrs_registered)
9707 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9709 if (tp_features.sensors_pdrv_registered)
9710 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9712 if (tp_features.platform_drv_registered)
9713 platform_driver_unregister(&tpacpi_pdriver);
9716 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9719 destroy_workqueue(tpacpi_wq);
9721 kfree(thinkpad_id.bios_version_str);
9722 kfree(thinkpad_id.ec_version_str);
9723 kfree(thinkpad_id.model_str);
9724 kfree(thinkpad_id.nummodel_str);
9728 static int __init thinkpad_acpi_module_init(void)
9732 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9734 /* Driver-level probe */
9736 ret = get_thinkpad_model_data(&thinkpad_id);
9738 pr_err("unable to get DMI data: %d\n", ret);
9739 thinkpad_acpi_module_exit();
9742 ret = probe_for_thinkpad();
9744 thinkpad_acpi_module_exit();
9748 /* Driver initialization */
9750 thinkpad_acpi_init_banner();
9751 tpacpi_check_outdated_fw();
9753 TPACPI_ACPIHANDLE_INIT(ecrd);
9754 TPACPI_ACPIHANDLE_INIT(ecwr);
9756 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9758 thinkpad_acpi_module_exit();
9762 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9764 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9765 thinkpad_acpi_module_exit();
9769 ret = platform_driver_register(&tpacpi_pdriver);
9771 pr_err("unable to register main platform driver\n");
9772 thinkpad_acpi_module_exit();
9775 tp_features.platform_drv_registered = 1;
9777 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9779 pr_err("unable to register hwmon platform driver\n");
9780 thinkpad_acpi_module_exit();
9783 tp_features.sensors_pdrv_registered = 1;
9785 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9787 tp_features.platform_drv_attrs_registered = 1;
9788 ret = tpacpi_create_driver_attributes(
9789 &tpacpi_hwmon_pdriver.driver);
9792 pr_err("unable to create sysfs driver attributes\n");
9793 thinkpad_acpi_module_exit();
9796 tp_features.sensors_pdrv_attrs_registered = 1;
9799 /* Device initialization */
9800 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9802 if (IS_ERR(tpacpi_pdev)) {
9803 ret = PTR_ERR(tpacpi_pdev);
9805 pr_err("unable to register platform device\n");
9806 thinkpad_acpi_module_exit();
9809 tpacpi_sensors_pdev = platform_device_register_simple(
9810 TPACPI_HWMON_DRVR_NAME,
9812 if (IS_ERR(tpacpi_sensors_pdev)) {
9813 ret = PTR_ERR(tpacpi_sensors_pdev);
9814 tpacpi_sensors_pdev = NULL;
9815 pr_err("unable to register hwmon platform device\n");
9816 thinkpad_acpi_module_exit();
9819 tp_features.sensors_pdev_attrs_registered = 1;
9820 tpacpi_hwmon = hwmon_device_register_with_groups(
9821 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
9823 if (IS_ERR(tpacpi_hwmon)) {
9824 ret = PTR_ERR(tpacpi_hwmon);
9825 tpacpi_hwmon = NULL;
9826 pr_err("unable to register hwmon device\n");
9827 thinkpad_acpi_module_exit();
9830 mutex_init(&tpacpi_inputdev_send_mutex);
9831 tpacpi_inputdev = input_allocate_device();
9832 if (!tpacpi_inputdev) {
9833 thinkpad_acpi_module_exit();
9836 /* Prepare input device, but don't register */
9837 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9838 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9839 tpacpi_inputdev->id.bustype = BUS_HOST;
9840 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9841 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9842 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9843 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9846 /* Init subdriver dependencies */
9847 tpacpi_detect_brightness_capabilities();
9849 /* Init subdrivers */
9850 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9851 ret = ibm_init(&ibms_init[i]);
9852 if (ret >= 0 && *ibms_init[i].param)
9853 ret = ibms_init[i].data->write(ibms_init[i].param);
9855 thinkpad_acpi_module_exit();
9860 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9862 ret = input_register_device(tpacpi_inputdev);
9864 pr_err("unable to register input device\n");
9865 thinkpad_acpi_module_exit();
9868 tp_features.input_device_registered = 1;
9874 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9877 * This will autoload the driver in almost every ThinkPad
9878 * in widespread use.
9880 * Only _VERY_ old models, like the 240, 240x and 570 lack
9881 * the HKEY event interface.
9883 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9886 * DMI matching for module autoloading
9888 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9889 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9891 * Only models listed in thinkwiki will be supported, so add yours
9892 * if it is not there yet.
9894 #define IBM_BIOS_MODULE_ALIAS(__type) \
9895 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9897 /* Ancient thinkpad BIOSes have to be identified by
9898 * BIOS type or model number, and there are far less
9899 * BIOS types than model numbers... */
9900 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9902 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9903 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9904 MODULE_DESCRIPTION(TPACPI_DESC);
9905 MODULE_VERSION(TPACPI_VERSION);
9906 MODULE_LICENSE("GPL");
9908 module_init(thinkpad_acpi_module_init);
9909 module_exit(thinkpad_acpi_module_exit);